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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
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> |
| 46 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 47 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 48 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 49 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 50 | #include <linux/raid/md_p.h> |
| 51 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 52 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 53 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 54 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | #define DEBUG 0 |
| 57 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 58 | |
| 59 | |
| 60 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 61 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 64 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(pers_lock); |
| 66 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 67 | static void md_print_devices(void); |
| 68 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 69 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 70 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 71 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 74 | * Default number of read corrections we'll attempt on an rdev |
| 75 | * before ejecting it from the array. We divide the read error |
| 76 | * count by 2 for every hour elapsed between read errors. |
| 77 | */ |
| 78 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 79 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 81 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 82 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 83 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 85 | * speed limit - in case reconstruction slows down your system despite |
| 86 | * idle IO detection. |
| 87 | * |
| 88 | * 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] | 89 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | */ |
| 91 | |
| 92 | static int sysctl_speed_limit_min = 1000; |
| 93 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 94 | static inline int speed_min(mddev_t *mddev) |
| 95 | { |
| 96 | return mddev->sync_speed_min ? |
| 97 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 98 | } |
| 99 | |
| 100 | static inline int speed_max(mddev_t *mddev) |
| 101 | { |
| 102 | return mddev->sync_speed_max ? |
| 103 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | static struct ctl_table_header *raid_table_header; |
| 107 | |
| 108 | static ctl_table raid_table[] = { |
| 109 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | .procname = "speed_limit_min", |
| 111 | .data = &sysctl_speed_limit_min, |
| 112 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 113 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 114 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | }, |
| 116 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .procname = "speed_limit_max", |
| 118 | .data = &sysctl_speed_limit_max, |
| 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 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 123 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | static ctl_table raid_dir_table[] = { |
| 127 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | .procname = "raid", |
| 129 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 130 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .child = raid_table, |
| 132 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 133 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | static ctl_table raid_root_table[] = { |
| 137 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .procname = "dev", |
| 139 | .maxlen = 0, |
| 140 | .mode = 0555, |
| 141 | .child = raid_dir_table, |
| 142 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 143 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 146 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 148 | static int start_readonly; |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 151 | * We have a system wide 'event count' that is incremented |
| 152 | * on any 'interesting' event, and readers of /proc/mdstat |
| 153 | * can use 'poll' or 'select' to find out when the event |
| 154 | * count increases. |
| 155 | * |
| 156 | * Events are: |
| 157 | * start array, stop array, error, add device, remove device, |
| 158 | * start build, activate spare |
| 159 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 160 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 161 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 162 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 163 | { |
| 164 | atomic_inc(&md_event_count); |
| 165 | wake_up(&md_event_waiters); |
| 166 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 167 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 168 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 169 | /* Alternate version that can be called from interrupts |
| 170 | * when calling sysfs_notify isn't needed. |
| 171 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 172 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 173 | { |
| 174 | atomic_inc(&md_event_count); |
| 175 | wake_up(&md_event_waiters); |
| 176 | } |
| 177 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 178 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | * Enables to iterate over all existing md arrays |
| 180 | * all_mddevs_lock protects this list. |
| 181 | */ |
| 182 | static LIST_HEAD(all_mddevs); |
| 183 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 184 | |
| 185 | |
| 186 | /* |
| 187 | * iterates through all used mddevs in the system. |
| 188 | * We take care to grab the all_mddevs_lock whenever navigating |
| 189 | * the list, and to always hold a refcount when unlocked. |
| 190 | * Any code which breaks out of this loop while own |
| 191 | * a reference to the current mddev and must mddev_put it. |
| 192 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 193 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | \ |
| 195 | for (({ spin_lock(&all_mddevs_lock); \ |
| 196 | tmp = all_mddevs.next; \ |
| 197 | mddev = NULL;}); \ |
| 198 | ({ if (tmp != &all_mddevs) \ |
| 199 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 200 | spin_unlock(&all_mddevs_lock); \ |
| 201 | if (mddev) mddev_put(mddev); \ |
| 202 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 203 | tmp != &all_mddevs;}); \ |
| 204 | ({ spin_lock(&all_mddevs_lock); \ |
| 205 | tmp = tmp->next;}) \ |
| 206 | ) |
| 207 | |
| 208 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 209 | /* Rather than calling directly into the personality make_request function, |
| 210 | * IO requests come here first so that we can check if the device is |
| 211 | * being suspended pending a reconfiguration. |
| 212 | * We hold a refcount over the call to ->make_request. By the time that |
| 213 | * call has finished, the bio has been linked into some internal structure |
| 214 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 215 | */ |
| 216 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 218 | const int rw = bio_data_dir(bio); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 219 | mddev_t *mddev = q->queuedata; |
| 220 | int rv; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 221 | int cpu; |
| 222 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 223 | if (mddev == NULL || mddev->pers == NULL) { |
| 224 | bio_io_error(bio); |
| 225 | return 0; |
| 226 | } |
| 227 | rcu_read_lock(); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 228 | if (mddev->suspended || mddev->barrier) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 229 | DEFINE_WAIT(__wait); |
| 230 | for (;;) { |
| 231 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 232 | TASK_UNINTERRUPTIBLE); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 233 | if (!mddev->suspended && !mddev->barrier) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 234 | break; |
| 235 | rcu_read_unlock(); |
| 236 | schedule(); |
| 237 | rcu_read_lock(); |
| 238 | } |
| 239 | finish_wait(&mddev->sb_wait, &__wait); |
| 240 | } |
| 241 | atomic_inc(&mddev->active_io); |
| 242 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 243 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 244 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 245 | |
| 246 | cpu = part_stat_lock(); |
| 247 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 248 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 249 | bio_sectors(bio)); |
| 250 | part_stat_unlock(); |
| 251 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 252 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 253 | wake_up(&mddev->sb_wait); |
| 254 | |
| 255 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 258 | /* mddev_suspend makes sure no new requests are submitted |
| 259 | * to the device, and that any requests that have been submitted |
| 260 | * are completely handled. |
| 261 | * Once ->stop is called and completes, the module will be completely |
| 262 | * unused. |
| 263 | */ |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 264 | void mddev_suspend(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 265 | { |
| 266 | BUG_ON(mddev->suspended); |
| 267 | mddev->suspended = 1; |
| 268 | synchronize_rcu(); |
| 269 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 270 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 271 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 272 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 273 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 274 | void mddev_resume(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 275 | { |
| 276 | mddev->suspended = 0; |
| 277 | wake_up(&mddev->sb_wait); |
| 278 | mddev->pers->quiesce(mddev, 0); |
| 279 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 280 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 281 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 282 | int mddev_congested(mddev_t *mddev, int bits) |
| 283 | { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 284 | if (mddev->barrier) |
| 285 | return 1; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 286 | return mddev->suspended; |
| 287 | } |
| 288 | EXPORT_SYMBOL(mddev_congested); |
| 289 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 290 | /* |
| 291 | * Generic barrier handling for md |
| 292 | */ |
| 293 | |
| 294 | #define POST_REQUEST_BARRIER ((void*)1) |
| 295 | |
| 296 | static void md_end_barrier(struct bio *bio, int err) |
| 297 | { |
| 298 | mdk_rdev_t *rdev = bio->bi_private; |
| 299 | mddev_t *mddev = rdev->mddev; |
| 300 | if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER) |
| 301 | set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags); |
| 302 | |
| 303 | rdev_dec_pending(rdev, mddev); |
| 304 | |
| 305 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 306 | if (mddev->barrier == POST_REQUEST_BARRIER) { |
| 307 | /* This was a post-request barrier */ |
| 308 | mddev->barrier = NULL; |
| 309 | wake_up(&mddev->sb_wait); |
| 310 | } else |
| 311 | /* The pre-request barrier has finished */ |
| 312 | schedule_work(&mddev->barrier_work); |
| 313 | } |
| 314 | bio_put(bio); |
| 315 | } |
| 316 | |
| 317 | static void submit_barriers(mddev_t *mddev) |
| 318 | { |
| 319 | mdk_rdev_t *rdev; |
| 320 | |
| 321 | rcu_read_lock(); |
| 322 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 323 | if (rdev->raid_disk >= 0 && |
| 324 | !test_bit(Faulty, &rdev->flags)) { |
| 325 | /* Take two references, one is dropped |
| 326 | * when request finishes, one after |
| 327 | * we reclaim rcu_read_lock |
| 328 | */ |
| 329 | struct bio *bi; |
| 330 | atomic_inc(&rdev->nr_pending); |
| 331 | atomic_inc(&rdev->nr_pending); |
| 332 | rcu_read_unlock(); |
| 333 | bi = bio_alloc(GFP_KERNEL, 0); |
| 334 | bi->bi_end_io = md_end_barrier; |
| 335 | bi->bi_private = rdev; |
| 336 | bi->bi_bdev = rdev->bdev; |
| 337 | atomic_inc(&mddev->flush_pending); |
| 338 | submit_bio(WRITE_BARRIER, bi); |
| 339 | rcu_read_lock(); |
| 340 | rdev_dec_pending(rdev, mddev); |
| 341 | } |
| 342 | rcu_read_unlock(); |
| 343 | } |
| 344 | |
| 345 | static void md_submit_barrier(struct work_struct *ws) |
| 346 | { |
| 347 | mddev_t *mddev = container_of(ws, mddev_t, barrier_work); |
| 348 | struct bio *bio = mddev->barrier; |
| 349 | |
| 350 | atomic_set(&mddev->flush_pending, 1); |
| 351 | |
| 352 | if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags)) |
| 353 | bio_endio(bio, -EOPNOTSUPP); |
| 354 | else if (bio->bi_size == 0) |
| 355 | /* an empty barrier - all done */ |
| 356 | bio_endio(bio, 0); |
| 357 | else { |
| 358 | bio->bi_rw &= ~(1<<BIO_RW_BARRIER); |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 359 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 360 | generic_make_request(bio); |
| 361 | mddev->barrier = POST_REQUEST_BARRIER; |
| 362 | submit_barriers(mddev); |
| 363 | } |
| 364 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 365 | mddev->barrier = NULL; |
| 366 | wake_up(&mddev->sb_wait); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void md_barrier_request(mddev_t *mddev, struct bio *bio) |
| 371 | { |
| 372 | spin_lock_irq(&mddev->write_lock); |
| 373 | wait_event_lock_irq(mddev->sb_wait, |
| 374 | !mddev->barrier, |
| 375 | mddev->write_lock, /*nothing*/); |
| 376 | mddev->barrier = bio; |
| 377 | spin_unlock_irq(&mddev->write_lock); |
| 378 | |
| 379 | atomic_set(&mddev->flush_pending, 1); |
| 380 | INIT_WORK(&mddev->barrier_work, md_submit_barrier); |
| 381 | |
| 382 | submit_barriers(mddev); |
| 383 | |
| 384 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 385 | schedule_work(&mddev->barrier_work); |
| 386 | } |
| 387 | EXPORT_SYMBOL(md_barrier_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 388 | |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 389 | /* Support for plugging. |
| 390 | * This mirrors the plugging support in request_queue, but does not |
| 391 | * require having a whole queue |
| 392 | */ |
| 393 | static void plugger_work(struct work_struct *work) |
| 394 | { |
| 395 | struct plug_handle *plug = |
| 396 | container_of(work, struct plug_handle, unplug_work); |
| 397 | plug->unplug_fn(plug); |
| 398 | } |
| 399 | static void plugger_timeout(unsigned long data) |
| 400 | { |
| 401 | struct plug_handle *plug = (void *)data; |
| 402 | kblockd_schedule_work(NULL, &plug->unplug_work); |
| 403 | } |
| 404 | void plugger_init(struct plug_handle *plug, |
| 405 | void (*unplug_fn)(struct plug_handle *)) |
| 406 | { |
| 407 | plug->unplug_flag = 0; |
| 408 | plug->unplug_fn = unplug_fn; |
| 409 | init_timer(&plug->unplug_timer); |
| 410 | plug->unplug_timer.function = plugger_timeout; |
| 411 | plug->unplug_timer.data = (unsigned long)plug; |
| 412 | INIT_WORK(&plug->unplug_work, plugger_work); |
| 413 | } |
| 414 | EXPORT_SYMBOL_GPL(plugger_init); |
| 415 | |
| 416 | void plugger_set_plug(struct plug_handle *plug) |
| 417 | { |
| 418 | if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag)) |
| 419 | mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1); |
| 420 | } |
| 421 | EXPORT_SYMBOL_GPL(plugger_set_plug); |
| 422 | |
| 423 | int plugger_remove_plug(struct plug_handle *plug) |
| 424 | { |
| 425 | if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) { |
| 426 | del_timer(&plug->unplug_timer); |
| 427 | return 1; |
| 428 | } else |
| 429 | return 0; |
| 430 | } |
| 431 | EXPORT_SYMBOL_GPL(plugger_remove_plug); |
| 432 | |
| 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 435 | { |
| 436 | atomic_inc(&mddev->active); |
| 437 | return mddev; |
| 438 | } |
| 439 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 440 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | static void mddev_put(mddev_t *mddev) |
| 443 | { |
| 444 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 445 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 446 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 447 | mddev->ctime == 0 && !mddev->hold_active) { |
| 448 | /* Array is not configured at all, and not held active, |
| 449 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 451 | if (mddev->gendisk) { |
| 452 | /* we did a probe so need to clean up. |
| 453 | * Call schedule_work inside the spinlock |
| 454 | * so that flush_scheduled_work() after |
| 455 | * mddev_find will succeed in waiting for the |
| 456 | * work to be done. |
| 457 | */ |
| 458 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 459 | schedule_work(&mddev->del_work); |
| 460 | } else |
| 461 | kfree(mddev); |
| 462 | } |
| 463 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 466 | void mddev_init(mddev_t *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 467 | { |
| 468 | mutex_init(&mddev->open_mutex); |
| 469 | mutex_init(&mddev->reconfig_mutex); |
| 470 | mutex_init(&mddev->bitmap_info.mutex); |
| 471 | INIT_LIST_HEAD(&mddev->disks); |
| 472 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 473 | init_timer(&mddev->safemode_timer); |
| 474 | atomic_set(&mddev->active, 1); |
| 475 | atomic_set(&mddev->openers, 0); |
| 476 | atomic_set(&mddev->active_io, 0); |
| 477 | spin_lock_init(&mddev->write_lock); |
| 478 | atomic_set(&mddev->flush_pending, 0); |
| 479 | init_waitqueue_head(&mddev->sb_wait); |
| 480 | init_waitqueue_head(&mddev->recovery_wait); |
| 481 | mddev->reshape_position = MaxSector; |
| 482 | mddev->resync_min = 0; |
| 483 | mddev->resync_max = MaxSector; |
| 484 | mddev->level = LEVEL_NONE; |
| 485 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 486 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | static mddev_t * mddev_find(dev_t unit) |
| 489 | { |
| 490 | mddev_t *mddev, *new = NULL; |
| 491 | |
| 492 | retry: |
| 493 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 495 | if (unit) { |
| 496 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 497 | if (mddev->unit == unit) { |
| 498 | mddev_get(mddev); |
| 499 | spin_unlock(&all_mddevs_lock); |
| 500 | kfree(new); |
| 501 | return mddev; |
| 502 | } |
| 503 | |
| 504 | if (new) { |
| 505 | list_add(&new->all_mddevs, &all_mddevs); |
| 506 | spin_unlock(&all_mddevs_lock); |
| 507 | new->hold_active = UNTIL_IOCTL; |
| 508 | return new; |
| 509 | } |
| 510 | } else if (new) { |
| 511 | /* find an unused unit number */ |
| 512 | static int next_minor = 512; |
| 513 | int start = next_minor; |
| 514 | int is_free = 0; |
| 515 | int dev = 0; |
| 516 | while (!is_free) { |
| 517 | dev = MKDEV(MD_MAJOR, next_minor); |
| 518 | next_minor++; |
| 519 | if (next_minor > MINORMASK) |
| 520 | next_minor = 0; |
| 521 | if (next_minor == start) { |
| 522 | /* Oh dear, all in use. */ |
| 523 | spin_unlock(&all_mddevs_lock); |
| 524 | kfree(new); |
| 525 | return NULL; |
| 526 | } |
| 527 | |
| 528 | is_free = 1; |
| 529 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 530 | if (mddev->unit == dev) { |
| 531 | is_free = 0; |
| 532 | break; |
| 533 | } |
| 534 | } |
| 535 | new->unit = dev; |
| 536 | new->md_minor = MINOR(dev); |
| 537 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | list_add(&new->all_mddevs, &all_mddevs); |
| 539 | spin_unlock(&all_mddevs_lock); |
| 540 | return new; |
| 541 | } |
| 542 | spin_unlock(&all_mddevs_lock); |
| 543 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 544 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | if (!new) |
| 546 | return NULL; |
| 547 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | new->unit = unit; |
| 549 | if (MAJOR(unit) == MD_MAJOR) |
| 550 | new->md_minor = MINOR(unit); |
| 551 | else |
| 552 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 553 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 554 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | goto retry; |
| 557 | } |
| 558 | |
| 559 | static inline int mddev_lock(mddev_t * mddev) |
| 560 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 561 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 564 | static inline int mddev_is_locked(mddev_t *mddev) |
| 565 | { |
| 566 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 567 | } |
| 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | static inline int mddev_trylock(mddev_t * mddev) |
| 570 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 571 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 574 | static struct attribute_group md_redundancy_group; |
| 575 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 576 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 578 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 579 | /* These cannot be removed under reconfig_mutex as |
| 580 | * an access to the files will try to take reconfig_mutex |
| 581 | * while holding the file unremovable, which leads to |
| 582 | * a deadlock. |
| 583 | * So hold open_mutex instead - we are allowed to take |
| 584 | * it while holding reconfig_mutex, and md_run can |
| 585 | * use it to wait for the remove to complete. |
| 586 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 587 | struct attribute_group *to_remove = mddev->to_remove; |
| 588 | mddev->to_remove = NULL; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 589 | mutex_lock(&mddev->open_mutex); |
| 590 | mutex_unlock(&mddev->reconfig_mutex); |
| 591 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 592 | if (mddev->kobj.sd) { |
| 593 | if (to_remove != &md_redundancy_group) |
| 594 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 595 | if (mddev->pers == NULL || |
| 596 | mddev->pers->sync_request == NULL) { |
| 597 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 598 | if (mddev->sysfs_action) |
| 599 | sysfs_put(mddev->sysfs_action); |
| 600 | mddev->sysfs_action = NULL; |
| 601 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 602 | } |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 603 | mutex_unlock(&mddev->open_mutex); |
| 604 | } else |
| 605 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 607 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
| 609 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 610 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 612 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 614 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | if (rdev->desc_nr == nr) |
| 616 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | return NULL; |
| 619 | } |
| 620 | |
| 621 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 622 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | mdk_rdev_t *rdev; |
| 624 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 625 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | if (rdev->bdev->bd_dev == dev) |
| 627 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | return NULL; |
| 630 | } |
| 631 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 632 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 633 | { |
| 634 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 635 | list_for_each_entry(pers, &pers_list, list) { |
| 636 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 637 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 638 | if (strcmp(pers->name, clevel)==0) |
| 639 | return pers; |
| 640 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 641 | return NULL; |
| 642 | } |
| 643 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 644 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 645 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 647 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 648 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 652 | { |
| 653 | if (rdev->sb_page) |
| 654 | MD_BUG(); |
| 655 | |
| 656 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 657 | if (!rdev->sb_page) { |
| 658 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 659 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 666 | { |
| 667 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 668 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | rdev->sb_loaded = 0; |
| 670 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 671 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 672 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
| 676 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 677 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 678 | { |
| 679 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 680 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 681 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 682 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 683 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 684 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 685 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 686 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 687 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 688 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 689 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 690 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 691 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 692 | } |
| 693 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 694 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 695 | { |
| 696 | struct bio *bio2 = bio->bi_private; |
| 697 | mdk_rdev_t *rdev = bio2->bi_private; |
| 698 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 699 | |
| 700 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 701 | error == -EOPNOTSUPP) { |
| 702 | unsigned long flags; |
| 703 | /* barriers don't appear to be supported :-( */ |
| 704 | set_bit(BarriersNotsupp, &rdev->flags); |
| 705 | mddev->barriers_work = 0; |
| 706 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 707 | bio2->bi_next = mddev->biolist; |
| 708 | mddev->biolist = bio2; |
| 709 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 710 | wake_up(&mddev->sb_wait); |
| 711 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 712 | } else { |
| 713 | bio_put(bio2); |
| 714 | bio->bi_private = rdev; |
| 715 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 716 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 717 | } |
| 718 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 719 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 720 | sector_t sector, int size, struct page *page) |
| 721 | { |
| 722 | /* write first size bytes of page to sector of rdev |
| 723 | * Increment mddev->pending_writes before returning |
| 724 | * and decrement it on completion, waking up sb_wait |
| 725 | * if zero is reached. |
| 726 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 727 | * |
| 728 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 729 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 730 | */ |
| 731 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 732 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 733 | |
| 734 | bio->bi_bdev = rdev->bdev; |
| 735 | bio->bi_sector = sector; |
| 736 | bio_add_page(bio, page, size, 0); |
| 737 | bio->bi_private = rdev; |
| 738 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 739 | bio->bi_rw = rw; |
| 740 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 741 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 742 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 743 | struct bio *rbio; |
| 744 | rw |= (1<<BIO_RW_BARRIER); |
| 745 | rbio = bio_clone(bio, GFP_NOIO); |
| 746 | rbio->bi_private = bio; |
| 747 | rbio->bi_end_io = super_written_barrier; |
| 748 | submit_bio(rw, rbio); |
| 749 | } else |
| 750 | submit_bio(rw, bio); |
| 751 | } |
| 752 | |
| 753 | void md_super_wait(mddev_t *mddev) |
| 754 | { |
| 755 | /* wait for all superblock writes that were scheduled to complete. |
| 756 | * if any had to be retried (due to BARRIER problems), retry them |
| 757 | */ |
| 758 | DEFINE_WAIT(wq); |
| 759 | for(;;) { |
| 760 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 761 | if (atomic_read(&mddev->pending_writes)==0) |
| 762 | break; |
| 763 | while (mddev->biolist) { |
| 764 | struct bio *bio; |
| 765 | spin_lock_irq(&mddev->write_lock); |
| 766 | bio = mddev->biolist; |
| 767 | mddev->biolist = bio->bi_next ; |
| 768 | bio->bi_next = NULL; |
| 769 | spin_unlock_irq(&mddev->write_lock); |
| 770 | submit_bio(bio->bi_rw, bio); |
| 771 | } |
| 772 | schedule(); |
| 773 | } |
| 774 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 775 | } |
| 776 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 777 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | } |
| 781 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 782 | int sync_page_io(struct block_device *bdev, sector_t sector, int size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | struct page *page, int rw) |
| 784 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 785 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | struct completion event; |
| 787 | int ret; |
| 788 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 789 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | |
| 791 | bio->bi_bdev = bdev; |
| 792 | bio->bi_sector = sector; |
| 793 | bio_add_page(bio, page, size, 0); |
| 794 | init_completion(&event); |
| 795 | bio->bi_private = &event; |
| 796 | bio->bi_end_io = bi_complete; |
| 797 | submit_bio(rw, bio); |
| 798 | wait_for_completion(&event); |
| 799 | |
| 800 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 801 | bio_put(bio); |
| 802 | return ret; |
| 803 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 804 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 806 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
| 808 | char b[BDEVNAME_SIZE]; |
| 809 | if (!rdev->sb_page) { |
| 810 | MD_BUG(); |
| 811 | return -EINVAL; |
| 812 | } |
| 813 | if (rdev->sb_loaded) |
| 814 | return 0; |
| 815 | |
| 816 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 817 | if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | goto fail; |
| 819 | rdev->sb_loaded = 1; |
| 820 | return 0; |
| 821 | |
| 822 | fail: |
| 823 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 824 | bdevname(rdev->bdev,b)); |
| 825 | return -EINVAL; |
| 826 | } |
| 827 | |
| 828 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 829 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 830 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 831 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 832 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 833 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 837 | { |
| 838 | int ret; |
| 839 | mdp_super_t *tmp1, *tmp2; |
| 840 | |
| 841 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 842 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 843 | |
| 844 | if (!tmp1 || !tmp2) { |
| 845 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 846 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | goto abort; |
| 848 | } |
| 849 | |
| 850 | *tmp1 = *sb1; |
| 851 | *tmp2 = *sb2; |
| 852 | |
| 853 | /* |
| 854 | * nr_disks is not constant |
| 855 | */ |
| 856 | tmp1->nr_disks = 0; |
| 857 | tmp2->nr_disks = 0; |
| 858 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 859 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 861 | kfree(tmp1); |
| 862 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | return ret; |
| 864 | } |
| 865 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 866 | |
| 867 | static u32 md_csum_fold(u32 csum) |
| 868 | { |
| 869 | csum = (csum & 0xffff) + (csum >> 16); |
| 870 | return (csum & 0xffff) + (csum >> 16); |
| 871 | } |
| 872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 874 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 875 | u64 newcsum = 0; |
| 876 | u32 *sb32 = (u32*)sb; |
| 877 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | unsigned int disk_csum, csum; |
| 879 | |
| 880 | disk_csum = sb->sb_csum; |
| 881 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 882 | |
| 883 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 884 | newcsum += sb32[i]; |
| 885 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 886 | |
| 887 | |
| 888 | #ifdef CONFIG_ALPHA |
| 889 | /* This used to use csum_partial, which was wrong for several |
| 890 | * reasons including that different results are returned on |
| 891 | * different architectures. It isn't critical that we get exactly |
| 892 | * the same return value as before (we always csum_fold before |
| 893 | * testing, and that removes any differences). However as we |
| 894 | * know that csum_partial always returned a 16bit value on |
| 895 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 896 | */ |
| 897 | sb->sb_csum = md_csum_fold(disk_csum); |
| 898 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 900 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return csum; |
| 902 | } |
| 903 | |
| 904 | |
| 905 | /* |
| 906 | * Handle superblock details. |
| 907 | * We want to be able to handle multiple superblock formats |
| 908 | * so we have a common interface to them all, and an array of |
| 909 | * different handlers. |
| 910 | * We rely on user-space to write the initial superblock, and support |
| 911 | * reading and updating of superblocks. |
| 912 | * Interface methods are: |
| 913 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 914 | * loads and validates a superblock on dev. |
| 915 | * if refdev != NULL, compare superblocks on both devices |
| 916 | * Return: |
| 917 | * 0 - dev has a superblock that is compatible with refdev |
| 918 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 919 | * so dev should be used as the refdev in future |
| 920 | * -EINVAL superblock incompatible or invalid |
| 921 | * -othererror e.g. -EIO |
| 922 | * |
| 923 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 924 | * Verify that dev is acceptable into mddev. |
| 925 | * The first time, mddev->raid_disks will be 0, and data from |
| 926 | * dev should be merged in. Subsequent calls check that dev |
| 927 | * is new enough. Return 0 or -EINVAL |
| 928 | * |
| 929 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 930 | * Update the superblock for rdev with data in mddev |
| 931 | * This does not write to disc. |
| 932 | * |
| 933 | */ |
| 934 | |
| 935 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 936 | char *name; |
| 937 | struct module *owner; |
| 938 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 939 | int minor_version); |
| 940 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 941 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 942 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 943 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | }; |
| 945 | |
| 946 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 947 | * Check that the given mddev has no bitmap. |
| 948 | * |
| 949 | * This function is called from the run method of all personalities that do not |
| 950 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 951 | * has a bitmap. Otherwise, it returns 0. |
| 952 | * |
| 953 | */ |
| 954 | int md_check_no_bitmap(mddev_t *mddev) |
| 955 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 956 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 957 | return 0; |
| 958 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 959 | mdname(mddev), mddev->pers->name); |
| 960 | return 1; |
| 961 | } |
| 962 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 963 | |
| 964 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | * load_super for 0.90.0 |
| 966 | */ |
| 967 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 968 | { |
| 969 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 970 | mdp_super_t *sb; |
| 971 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | |
| 973 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 974 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | * it's at the end of the disk. |
| 976 | * |
| 977 | * It also happens to be a multiple of 4Kb. |
| 978 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 979 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 981 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | if (ret) return ret; |
| 983 | |
| 984 | ret = -EINVAL; |
| 985 | |
| 986 | bdevname(rdev->bdev, b); |
| 987 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 988 | |
| 989 | if (sb->md_magic != MD_SB_MAGIC) { |
| 990 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 991 | b); |
| 992 | goto abort; |
| 993 | } |
| 994 | |
| 995 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 996 | sb->minor_version < 90 || |
| 997 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 999 | sb->major_version, sb->minor_version, |
| 1000 | b); |
| 1001 | goto abort; |
| 1002 | } |
| 1003 | |
| 1004 | if (sb->raid_disks <= 0) |
| 1005 | goto abort; |
| 1006 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1007 | 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] | 1008 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 1009 | b); |
| 1010 | goto abort; |
| 1011 | } |
| 1012 | |
| 1013 | rdev->preferred_minor = sb->md_minor; |
| 1014 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1015 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
| 1017 | if (sb->level == LEVEL_MULTIPATH) |
| 1018 | rdev->desc_nr = -1; |
| 1019 | else |
| 1020 | rdev->desc_nr = sb->this_disk.number; |
| 1021 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1022 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1024 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | __u64 ev1, ev2; |
| 1026 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 1027 | if (!uuid_equal(refsb, sb)) { |
| 1028 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 1029 | b, bdevname(refdev->bdev,b2)); |
| 1030 | goto abort; |
| 1031 | } |
| 1032 | if (!sb_equal(refsb, sb)) { |
| 1033 | printk(KERN_WARNING "md: %s has same UUID" |
| 1034 | " but different superblock to %s\n", |
| 1035 | b, bdevname(refdev->bdev, b2)); |
| 1036 | goto abort; |
| 1037 | } |
| 1038 | ev1 = md_event(sb); |
| 1039 | ev2 = md_event(refsb); |
| 1040 | if (ev1 > ev2) |
| 1041 | ret = 1; |
| 1042 | else |
| 1043 | ret = 0; |
| 1044 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1045 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1047 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1048 | /* "this cannot possibly happen" ... */ |
| 1049 | ret = -EINVAL; |
| 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | abort: |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | * validate_super for 0.90.0 |
| 1057 | */ |
| 1058 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1059 | { |
| 1060 | mdp_disk_t *desc; |
| 1061 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1062 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1064 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1065 | clear_bit(Faulty, &rdev->flags); |
| 1066 | clear_bit(In_sync, &rdev->flags); |
| 1067 | clear_bit(WriteMostly, &rdev->flags); |
| 1068 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | if (mddev->raid_disks == 0) { |
| 1071 | mddev->major_version = 0; |
| 1072 | mddev->minor_version = sb->minor_version; |
| 1073 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1074 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1075 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | mddev->ctime = sb->ctime; |
| 1077 | mddev->utime = sb->utime; |
| 1078 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1079 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | mddev->layout = sb->layout; |
| 1081 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1082 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1083 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1084 | mddev->bitmap_info.offset = 0; |
| 1085 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1087 | if (mddev->minor_version >= 91) { |
| 1088 | mddev->reshape_position = sb->reshape_position; |
| 1089 | mddev->delta_disks = sb->delta_disks; |
| 1090 | mddev->new_level = sb->new_level; |
| 1091 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1092 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1093 | } else { |
| 1094 | mddev->reshape_position = MaxSector; |
| 1095 | mddev->delta_disks = 0; |
| 1096 | mddev->new_level = mddev->level; |
| 1097 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1098 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1102 | mddev->recovery_cp = MaxSector; |
| 1103 | else { |
| 1104 | if (sb->events_hi == sb->cp_events_hi && |
| 1105 | sb->events_lo == sb->cp_events_lo) { |
| 1106 | mddev->recovery_cp = sb->recovery_cp; |
| 1107 | } else |
| 1108 | mddev->recovery_cp = 0; |
| 1109 | } |
| 1110 | |
| 1111 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1112 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1113 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1114 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1115 | |
| 1116 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1117 | |
| 1118 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1119 | mddev->bitmap_info.file == NULL) |
| 1120 | mddev->bitmap_info.offset = |
| 1121 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1122 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1123 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1124 | /* Insist on good event counter while assembling, except |
| 1125 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1127 | if (sb->disks[rdev->desc_nr].state & ( |
| 1128 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1129 | if (ev1 < mddev->events) |
| 1130 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1131 | } else if (mddev->bitmap) { |
| 1132 | /* if adding to array with a bitmap, then we can accept an |
| 1133 | * older device ... but not too old. |
| 1134 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1135 | if (ev1 < mddev->bitmap->events_cleared) |
| 1136 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1137 | } else { |
| 1138 | if (ev1 < mddev->events) |
| 1139 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1140 | return 0; |
| 1141 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | desc = sb->disks + rdev->desc_nr; |
| 1145 | |
| 1146 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1147 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1148 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1149 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1150 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1152 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1153 | /* active but not in sync implies recovery up to |
| 1154 | * reshape position. We don't know exactly where |
| 1155 | * that is, so set to zero for now */ |
| 1156 | if (mddev->minor_version >= 91) { |
| 1157 | rdev->recovery_offset = 0; |
| 1158 | rdev->raid_disk = desc->raid_disk; |
| 1159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1161 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1162 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1163 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1164 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | /* |
| 1169 | * sync_super for 0.90.0 |
| 1170 | */ |
| 1171 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1172 | { |
| 1173 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | mdk_rdev_t *rdev2; |
| 1175 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
| 1178 | /* make rdev->sb match mddev data.. |
| 1179 | * |
| 1180 | * 1/ zero out disks |
| 1181 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1182 | * 3/ any empty disks < next_spare become removed |
| 1183 | * |
| 1184 | * disks[0] gets initialised to REMOVED because |
| 1185 | * we cannot be sure from other fields if it has |
| 1186 | * been initialised or not. |
| 1187 | */ |
| 1188 | int i; |
| 1189 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1190 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1191 | rdev->sb_size = MD_SB_BYTES; |
| 1192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1194 | |
| 1195 | memset(sb, 0, sizeof(*sb)); |
| 1196 | |
| 1197 | sb->md_magic = MD_SB_MAGIC; |
| 1198 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | sb->patch_version = mddev->patch_version; |
| 1200 | sb->gvalid_words = 0; /* ignored */ |
| 1201 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1202 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1203 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1204 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1205 | |
| 1206 | sb->ctime = mddev->ctime; |
| 1207 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1208 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | sb->raid_disks = mddev->raid_disks; |
| 1210 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1211 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | sb->utime = mddev->utime; |
| 1213 | sb->state = 0; |
| 1214 | sb->events_hi = (mddev->events>>32); |
| 1215 | sb->events_lo = (u32)mddev->events; |
| 1216 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1217 | if (mddev->reshape_position == MaxSector) |
| 1218 | sb->minor_version = 90; |
| 1219 | else { |
| 1220 | sb->minor_version = 91; |
| 1221 | sb->reshape_position = mddev->reshape_position; |
| 1222 | sb->new_level = mddev->new_level; |
| 1223 | sb->delta_disks = mddev->delta_disks; |
| 1224 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1225 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1226 | } |
| 1227 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | if (mddev->in_sync) |
| 1229 | { |
| 1230 | sb->recovery_cp = mddev->recovery_cp; |
| 1231 | sb->cp_events_hi = (mddev->events>>32); |
| 1232 | sb->cp_events_lo = (u32)mddev->events; |
| 1233 | if (mddev->recovery_cp == MaxSector) |
| 1234 | sb->state = (1<< MD_SB_CLEAN); |
| 1235 | } else |
| 1236 | sb->recovery_cp = 0; |
| 1237 | |
| 1238 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1239 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1241 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1242 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1245 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1247 | int desc_nr; |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1248 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1249 | |
| 1250 | if (rdev2->raid_disk >= 0 && |
| 1251 | sb->minor_version >= 91) |
| 1252 | /* we have nowhere to store the recovery_offset, |
| 1253 | * but if it is not below the reshape_position, |
| 1254 | * we can piggy-back on that. |
| 1255 | */ |
| 1256 | is_active = 1; |
| 1257 | if (rdev2->raid_disk < 0 || |
| 1258 | test_bit(Faulty, &rdev2->flags)) |
| 1259 | is_active = 0; |
| 1260 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1261 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1263 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1264 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | d = &sb->disks[rdev2->desc_nr]; |
| 1266 | nr_disks++; |
| 1267 | d->number = rdev2->desc_nr; |
| 1268 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1269 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1270 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | d->raid_disk = rdev2->raid_disk; |
| 1272 | else |
| 1273 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1274 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1276 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9 | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1278 | if (test_bit(In_sync, &rdev2->flags)) |
| 1279 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | active++; |
| 1281 | working++; |
| 1282 | } else { |
| 1283 | d->state = 0; |
| 1284 | spare++; |
| 1285 | working++; |
| 1286 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1287 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1288 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1291 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1292 | mdp_disk_t *d = &sb->disks[i]; |
| 1293 | if (d->state == 0 && d->number == 0) { |
| 1294 | d->number = i; |
| 1295 | d->raid_disk = i; |
| 1296 | d->state = (1<<MD_DISK_REMOVED); |
| 1297 | d->state |= (1<<MD_DISK_FAULTY); |
| 1298 | failed++; |
| 1299 | } |
| 1300 | } |
| 1301 | sb->nr_disks = nr_disks; |
| 1302 | sb->active_disks = active; |
| 1303 | sb->working_disks = working; |
| 1304 | sb->failed_disks = failed; |
| 1305 | sb->spare_disks = spare; |
| 1306 | |
| 1307 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1308 | sb->sb_csum = calc_sb_csum(sb); |
| 1309 | } |
| 1310 | |
| 1311 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1312 | * rdev_size_change for 0.90.0 |
| 1313 | */ |
| 1314 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1315 | super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1316 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1317 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1318 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1319 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1320 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1321 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1322 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1323 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1324 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1325 | rdev->sb_page); |
| 1326 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1327 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | |
| 1331 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | * version 1 superblock |
| 1333 | */ |
| 1334 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1335 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1337 | __le32 disk_csum; |
| 1338 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | unsigned long long newcsum; |
| 1340 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1341 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | int i; |
| 1343 | |
| 1344 | disk_csum = sb->sb_csum; |
| 1345 | sb->sb_csum = 0; |
| 1346 | newcsum = 0; |
| 1347 | for (i=0; size>=4; size -= 4 ) |
| 1348 | newcsum += le32_to_cpu(*isuper++); |
| 1349 | |
| 1350 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1351 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | |
| 1353 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1354 | sb->sb_csum = disk_csum; |
| 1355 | return cpu_to_le32(csum); |
| 1356 | } |
| 1357 | |
| 1358 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1359 | { |
| 1360 | struct mdp_superblock_1 *sb; |
| 1361 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1362 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1364 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1367 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | * It is always aligned to a 4K boundary and |
| 1369 | * depeding on minor_version, it can be: |
| 1370 | * 0: At least 8K, but less than 12K, from end of device |
| 1371 | * 1: At start of device |
| 1372 | * 2: 4K from start of device. |
| 1373 | */ |
| 1374 | switch(minor_version) { |
| 1375 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1376 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1377 | sb_start -= 8*2; |
| 1378 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | break; |
| 1380 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1381 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | break; |
| 1383 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1384 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | break; |
| 1386 | default: |
| 1387 | return -EINVAL; |
| 1388 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1389 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1391 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1392 | * and it is safe to read 4k, so we do that |
| 1393 | */ |
| 1394 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | if (ret) return ret; |
| 1396 | |
| 1397 | |
| 1398 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1399 | |
| 1400 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1401 | sb->major_version != cpu_to_le32(1) || |
| 1402 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1403 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1404 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | return -EINVAL; |
| 1406 | |
| 1407 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1408 | printk("md: invalid superblock checksum on %s\n", |
| 1409 | bdevname(rdev->bdev,b)); |
| 1410 | return -EINVAL; |
| 1411 | } |
| 1412 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1413 | printk("md: data_size too small on %s\n", |
| 1414 | bdevname(rdev->bdev,b)); |
| 1415 | return -EINVAL; |
| 1416 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | rdev->preferred_minor = 0xffff; |
| 1419 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1420 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1422 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1423 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1424 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1425 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1426 | |
| 1427 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1428 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1429 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1430 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1431 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1432 | rdev->desc_nr = -1; |
| 1433 | else |
| 1434 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1435 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1436 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1437 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1438 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | __u64 ev1, ev2; |
| 1440 | struct mdp_superblock_1 *refsb = |
| 1441 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1442 | |
| 1443 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1444 | sb->level != refsb->level || |
| 1445 | sb->layout != refsb->layout || |
| 1446 | sb->chunksize != refsb->chunksize) { |
| 1447 | printk(KERN_WARNING "md: %s has strangely different" |
| 1448 | " superblock to %s\n", |
| 1449 | bdevname(rdev->bdev,b), |
| 1450 | bdevname(refdev->bdev,b2)); |
| 1451 | return -EINVAL; |
| 1452 | } |
| 1453 | ev1 = le64_to_cpu(sb->events); |
| 1454 | ev2 = le64_to_cpu(refsb->events); |
| 1455 | |
| 1456 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1457 | ret = 1; |
| 1458 | else |
| 1459 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1461 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1462 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1463 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1465 | rdev->sectors = rdev->sb_start; |
| 1466 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1468 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1469 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1470 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1471 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1475 | { |
| 1476 | struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1477 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1479 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1480 | clear_bit(Faulty, &rdev->flags); |
| 1481 | clear_bit(In_sync, &rdev->flags); |
| 1482 | clear_bit(WriteMostly, &rdev->flags); |
| 1483 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | if (mddev->raid_disks == 0) { |
| 1486 | mddev->major_version = 1; |
| 1487 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1488 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1489 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1491 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1492 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1493 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | mddev->layout = le32_to_cpu(sb->layout); |
| 1495 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1496 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1497 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1498 | mddev->bitmap_info.offset = 0; |
| 1499 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | |
| 1501 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1502 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1503 | |
| 1504 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1505 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1506 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1507 | mddev->bitmap_info.file == NULL ) |
| 1508 | mddev->bitmap_info.offset = |
| 1509 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1510 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1511 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1512 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1513 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1514 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1515 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1516 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1517 | } else { |
| 1518 | mddev->reshape_position = MaxSector; |
| 1519 | mddev->delta_disks = 0; |
| 1520 | mddev->new_level = mddev->level; |
| 1521 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1522 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1523 | } |
| 1524 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1525 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1526 | /* Insist of good event counter while assembling, except for |
| 1527 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1529 | if (rdev->desc_nr >= 0 && |
| 1530 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1531 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1532 | if (ev1 < mddev->events) |
| 1533 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1534 | } else if (mddev->bitmap) { |
| 1535 | /* If adding to array with a bitmap, then we can accept an |
| 1536 | * older device, but not too old. |
| 1537 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1538 | if (ev1 < mddev->bitmap->events_cleared) |
| 1539 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1540 | } else { |
| 1541 | if (ev1 < mddev->events) |
| 1542 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1543 | return 0; |
| 1544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1546 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1547 | if (rdev->desc_nr < 0 || |
| 1548 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1549 | role = 0xffff; |
| 1550 | rdev->desc_nr = -1; |
| 1551 | } else |
| 1552 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | switch(role) { |
| 1554 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | break; |
| 1556 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1557 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | break; |
| 1559 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1560 | if ((le32_to_cpu(sb->feature_map) & |
| 1561 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1562 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1563 | else |
| 1564 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | rdev->raid_disk = role; |
| 1566 | break; |
| 1567 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1568 | if (sb->devflags & WriteMostly1) |
| 1569 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1570 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1571 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | return 0; |
| 1574 | } |
| 1575 | |
| 1576 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1577 | { |
| 1578 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | mdk_rdev_t *rdev2; |
| 1580 | int max_dev, i; |
| 1581 | /* make rdev->sb match mddev and rdev data. */ |
| 1582 | |
| 1583 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1584 | |
| 1585 | sb->feature_map = 0; |
| 1586 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1587 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1589 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1590 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1591 | |
| 1592 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1593 | sb->events = cpu_to_le64(mddev->events); |
| 1594 | if (mddev->in_sync) |
| 1595 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1596 | else |
| 1597 | sb->resync_offset = cpu_to_le64(0); |
| 1598 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1599 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1600 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1601 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1602 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1603 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1604 | sb->level = cpu_to_le32(mddev->level); |
| 1605 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1606 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1607 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1608 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1609 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1610 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1611 | |
| 1612 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1613 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1614 | sb->feature_map |= |
| 1615 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1616 | sb->recovery_offset = |
| 1617 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1618 | } |
| 1619 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1620 | if (mddev->reshape_position != MaxSector) { |
| 1621 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1622 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1623 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1624 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1625 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1626 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1627 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1630 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | if (rdev2->desc_nr+1 > max_dev) |
| 1632 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1633 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1634 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1635 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1636 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1637 | rdev->sb_size = max_dev * 2 + 256; |
| 1638 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1639 | if (rdev->sb_size & bmask) |
| 1640 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | for (i=0; i<max_dev;i++) |
| 1643 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1644 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1645 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1647 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1649 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1651 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1652 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | else |
| 1654 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1655 | } |
| 1656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1658 | } |
| 1659 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1660 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1661 | super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1662 | { |
| 1663 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1664 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1665 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1666 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1667 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1668 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1669 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1670 | max_sectors -= rdev->data_offset; |
| 1671 | if (!num_sectors || num_sectors > max_sectors) |
| 1672 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1673 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1674 | /* minor version 0 with bitmap we can't move */ |
| 1675 | return 0; |
| 1676 | } else { |
| 1677 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1678 | sector_t sb_start; |
| 1679 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1680 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1681 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1682 | if (!num_sectors || num_sectors > max_sectors) |
| 1683 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1684 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1685 | } |
| 1686 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1687 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1688 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1689 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1690 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1691 | rdev->sb_page); |
| 1692 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1693 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1694 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1696 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | [0] = { |
| 1698 | .name = "0.90.0", |
| 1699 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1700 | .load_super = super_90_load, |
| 1701 | .validate_super = super_90_validate, |
| 1702 | .sync_super = super_90_sync, |
| 1703 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | }, |
| 1705 | [1] = { |
| 1706 | .name = "md-1", |
| 1707 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1708 | .load_super = super_1_load, |
| 1709 | .validate_super = super_1_validate, |
| 1710 | .sync_super = super_1_sync, |
| 1711 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | }, |
| 1713 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | |
| 1715 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1716 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1717 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1719 | rcu_read_lock(); |
| 1720 | rdev_for_each_rcu(rdev, mddev1) |
| 1721 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1722 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1723 | rdev2->bdev->bd_contains) { |
| 1724 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1725 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1726 | } |
| 1727 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | return 0; |
| 1729 | } |
| 1730 | |
| 1731 | static LIST_HEAD(pending_raid_disks); |
| 1732 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1733 | /* |
| 1734 | * Try to register data integrity profile for an mddev |
| 1735 | * |
| 1736 | * This is called when an array is started and after a disk has been kicked |
| 1737 | * from the array. It only succeeds if all working and active component devices |
| 1738 | * are integrity capable with matching profiles. |
| 1739 | */ |
| 1740 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1741 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1742 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1743 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1744 | if (list_empty(&mddev->disks)) |
| 1745 | return 0; /* nothing to do */ |
| 1746 | if (blk_get_integrity(mddev->gendisk)) |
| 1747 | return 0; /* already registered */ |
| 1748 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1749 | /* skip spares and non-functional disks */ |
| 1750 | if (test_bit(Faulty, &rdev->flags)) |
| 1751 | continue; |
| 1752 | if (rdev->raid_disk < 0) |
| 1753 | continue; |
| 1754 | /* |
| 1755 | * If at least one rdev is not integrity capable, we can not |
| 1756 | * enable data integrity for the md device. |
| 1757 | */ |
| 1758 | if (!bdev_get_integrity(rdev->bdev)) |
| 1759 | return -EINVAL; |
| 1760 | if (!reference) { |
| 1761 | /* Use the first rdev as the reference */ |
| 1762 | reference = rdev; |
| 1763 | continue; |
| 1764 | } |
| 1765 | /* does this rdev's profile match the reference profile? */ |
| 1766 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1767 | rdev->bdev->bd_disk) < 0) |
| 1768 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1769 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1770 | /* |
| 1771 | * All component devices are integrity capable and have matching |
| 1772 | * profiles, register the common profile for the md device. |
| 1773 | */ |
| 1774 | if (blk_integrity_register(mddev->gendisk, |
| 1775 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1776 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1777 | mdname(mddev)); |
| 1778 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1779 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1780 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1781 | mdname(mddev)); |
| 1782 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1783 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1784 | EXPORT_SYMBOL(md_integrity_register); |
| 1785 | |
| 1786 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1787 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1788 | { |
| 1789 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1790 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1791 | |
| 1792 | if (!bi_mddev) /* nothing to do */ |
| 1793 | return; |
| 1794 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1795 | return; |
| 1796 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1797 | rdev->bdev->bd_disk) >= 0) |
| 1798 | return; |
| 1799 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1800 | blk_integrity_unregister(mddev->gendisk); |
| 1801 | } |
| 1802 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1805 | { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1806 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1807 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1808 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1809 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | |
| 1811 | if (rdev->mddev) { |
| 1812 | MD_BUG(); |
| 1813 | return -EINVAL; |
| 1814 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1815 | |
| 1816 | /* prevent duplicates */ |
| 1817 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1818 | return -EEXIST; |
| 1819 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1820 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1821 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1822 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1823 | if (mddev->pers) { |
| 1824 | /* Cannot change size, so fail |
| 1825 | * If mddev->level <= 0, then we don't care |
| 1826 | * about aligning sizes (e.g. linear) |
| 1827 | */ |
| 1828 | if (mddev->level > 0) |
| 1829 | return -ENOSPC; |
| 1830 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1831 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1832 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | |
| 1834 | /* Verify rdev->desc_nr is unique. |
| 1835 | * If it is -1, assign a free number, else |
| 1836 | * check number is not in use |
| 1837 | */ |
| 1838 | if (rdev->desc_nr < 0) { |
| 1839 | int choice = 0; |
| 1840 | if (mddev->pers) choice = mddev->raid_disks; |
| 1841 | while (find_rdev_nr(mddev, choice)) |
| 1842 | choice++; |
| 1843 | rdev->desc_nr = choice; |
| 1844 | } else { |
| 1845 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1846 | return -EBUSY; |
| 1847 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1848 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1849 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1850 | mdname(mddev), mddev->max_disks); |
| 1851 | return -EBUSY; |
| 1852 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1853 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1854 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1855 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1858 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1859 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1860 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1861 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1862 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1863 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1864 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1865 | /* failure here is OK */; |
| 1866 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1867 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1868 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1869 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1870 | |
| 1871 | /* May as well allow recovery to be retried once */ |
| 1872 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1875 | |
| 1876 | fail: |
| 1877 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1878 | b, mdname(mddev)); |
| 1879 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1882 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1883 | { |
| 1884 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1885 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1886 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1890 | { |
| 1891 | char b[BDEVNAME_SIZE]; |
| 1892 | if (!rdev->mddev) { |
| 1893 | MD_BUG(); |
| 1894 | return; |
| 1895 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1896 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1897 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1899 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1900 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1901 | sysfs_put(rdev->sysfs_state); |
| 1902 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1903 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1904 | * writing to 'remove' to "dev/state". We also need |
| 1905 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1906 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1907 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1908 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1909 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1910 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | /* |
| 1914 | * prevent the device from being mounted, repartitioned or |
| 1915 | * otherwise reused by a RAID array (or any other kernel |
| 1916 | * subsystem), by bd_claiming the device. |
| 1917 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1918 | static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | { |
| 1920 | int err = 0; |
| 1921 | struct block_device *bdev; |
| 1922 | char b[BDEVNAME_SIZE]; |
| 1923 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1924 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | if (IS_ERR(bdev)) { |
| 1926 | printk(KERN_ERR "md: could not open %s.\n", |
| 1927 | __bdevname(dev, b)); |
| 1928 | return PTR_ERR(bdev); |
| 1929 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1930 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | if (err) { |
| 1932 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1933 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1934 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | return err; |
| 1936 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1937 | if (!shared) |
| 1938 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | rdev->bdev = bdev; |
| 1940 | return err; |
| 1941 | } |
| 1942 | |
| 1943 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1944 | { |
| 1945 | struct block_device *bdev = rdev->bdev; |
| 1946 | rdev->bdev = NULL; |
| 1947 | if (!bdev) |
| 1948 | MD_BUG(); |
| 1949 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1950 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | void md_autodetect_dev(dev_t dev); |
| 1954 | |
| 1955 | static void export_rdev(mdk_rdev_t * rdev) |
| 1956 | { |
| 1957 | char b[BDEVNAME_SIZE]; |
| 1958 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1959 | bdevname(rdev->bdev,b)); |
| 1960 | if (rdev->mddev) |
| 1961 | MD_BUG(); |
| 1962 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1964 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1965 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | #endif |
| 1967 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1968 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1972 | { |
| 1973 | unbind_rdev_from_array(rdev); |
| 1974 | export_rdev(rdev); |
| 1975 | } |
| 1976 | |
| 1977 | static void export_array(mddev_t *mddev) |
| 1978 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1979 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1981 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | if (!rdev->mddev) { |
| 1983 | MD_BUG(); |
| 1984 | continue; |
| 1985 | } |
| 1986 | kick_rdev_from_array(rdev); |
| 1987 | } |
| 1988 | if (!list_empty(&mddev->disks)) |
| 1989 | MD_BUG(); |
| 1990 | mddev->raid_disks = 0; |
| 1991 | mddev->major_version = 0; |
| 1992 | } |
| 1993 | |
| 1994 | static void print_desc(mdp_disk_t *desc) |
| 1995 | { |
| 1996 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1997 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1998 | } |
| 1999 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2000 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | { |
| 2002 | int i; |
| 2003 | |
| 2004 | printk(KERN_INFO |
| 2005 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 2006 | sb->major_version, sb->minor_version, sb->patch_version, |
| 2007 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 2008 | sb->ctime); |
| 2009 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 2010 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 2011 | sb->md_minor, sb->layout, sb->chunk_size); |
| 2012 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 2013 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 2014 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 2015 | sb->failed_disks, sb->spare_disks, |
| 2016 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 2017 | |
| 2018 | printk(KERN_INFO); |
| 2019 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 2020 | mdp_disk_t *desc; |
| 2021 | |
| 2022 | desc = sb->disks + i; |
| 2023 | if (desc->number || desc->major || desc->minor || |
| 2024 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 2025 | printk(" D %2d: ", i); |
| 2026 | print_desc(desc); |
| 2027 | } |
| 2028 | } |
| 2029 | printk(KERN_INFO "md: THIS: "); |
| 2030 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2033 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 2034 | { |
| 2035 | __u8 *uuid; |
| 2036 | |
| 2037 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2038 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2039 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2040 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2041 | le32_to_cpu(sb->major_version), |
| 2042 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2043 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2044 | sb->set_name, |
| 2045 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 2046 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 2047 | |
| 2048 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2049 | printk(KERN_INFO |
| 2050 | "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] | 2051 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2052 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2053 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2054 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2055 | le32_to_cpu(sb->level), |
| 2056 | (unsigned long long)le64_to_cpu(sb->size), |
| 2057 | le32_to_cpu(sb->raid_disks), |
| 2058 | le32_to_cpu(sb->layout), |
| 2059 | le32_to_cpu(sb->chunksize), |
| 2060 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2061 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2062 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2063 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2064 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2065 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2066 | sb->devflags, |
| 2067 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2068 | (unsigned long long)le64_to_cpu(sb->events), |
| 2069 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2070 | le32_to_cpu(sb->sb_csum), |
| 2071 | le32_to_cpu(sb->max_dev) |
| 2072 | ); |
| 2073 | } |
| 2074 | |
| 2075 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | { |
| 2077 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2078 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2079 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2080 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2081 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2083 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2084 | switch (major_version) { |
| 2085 | case 0: |
| 2086 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2087 | break; |
| 2088 | case 1: |
| 2089 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2090 | break; |
| 2091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | } else |
| 2093 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2094 | } |
| 2095 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2096 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2098 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | mdk_rdev_t *rdev; |
| 2100 | mddev_t *mddev; |
| 2101 | char b[BDEVNAME_SIZE]; |
| 2102 | |
| 2103 | printk("\n"); |
| 2104 | printk("md: **********************************\n"); |
| 2105 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2106 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2107 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2109 | if (mddev->bitmap) |
| 2110 | bitmap_print_sb(mddev->bitmap); |
| 2111 | else |
| 2112 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2113 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2115 | printk("\n"); |
| 2116 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2117 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2118 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | } |
| 2120 | printk("md: **********************************\n"); |
| 2121 | printk("\n"); |
| 2122 | } |
| 2123 | |
| 2124 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2125 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2127 | /* Update each superblock (in-memory image), but |
| 2128 | * if we are allowed to, skip spares which already |
| 2129 | * have the right event counter, or have one earlier |
| 2130 | * (which would mean they aren't being marked as dirty |
| 2131 | * with the rest of the array) |
| 2132 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2135 | /* First make sure individual recovery_offsets are correct */ |
| 2136 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2137 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 2138 | mddev->delta_disks >= 0 && |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2139 | !test_bit(In_sync, &rdev->flags) && |
| 2140 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2141 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2142 | |
| 2143 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2144 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2145 | if (rdev->sb_events == mddev->events || |
| 2146 | (nospares && |
| 2147 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2148 | rdev->sb_events+1 == mddev->events)) { |
| 2149 | /* Don't update this superblock */ |
| 2150 | rdev->sb_loaded = 2; |
| 2151 | } else { |
| 2152 | super_types[mddev->major_version]. |
| 2153 | sync_super(mddev, rdev); |
| 2154 | rdev->sb_loaded = 1; |
| 2155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | } |
| 2157 | } |
| 2158 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2159 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2162 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2163 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2165 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2166 | if (mddev->external) |
| 2167 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2169 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2170 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2171 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2172 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2173 | force_change = 1; |
| 2174 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2175 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2176 | * though if events isn't the right even/odd, we will have to do |
| 2177 | * spares after all |
| 2178 | */ |
| 2179 | nospares = 1; |
| 2180 | if (force_change) |
| 2181 | nospares = 0; |
| 2182 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2183 | /* If the array is degraded, then skipping spares is both |
| 2184 | * dangerous and fairly pointless. |
| 2185 | * Dangerous because a device that was removed from the array |
| 2186 | * might have a event_count that still looks up-to-date, |
| 2187 | * so it can be re-added without a resync. |
| 2188 | * Pointless because if there are any spares to skip, |
| 2189 | * then a recovery will happen and soon that array won't |
| 2190 | * be degraded any more and the spare can go back to sleep then. |
| 2191 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2192 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2193 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2194 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2195 | |
| 2196 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2197 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2198 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2199 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2200 | && mddev->can_decrease_events |
| 2201 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2202 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2203 | mddev->can_decrease_events = 0; |
| 2204 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2205 | /* otherwise we have to go forward and ... */ |
| 2206 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2207 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | |
| 2210 | if (!mddev->events) { |
| 2211 | /* |
| 2212 | * oops, this 64-bit counter should never wrap. |
| 2213 | * Either we are in around ~1 trillion A.C., assuming |
| 2214 | * 1 reboot per second, or we have a bug: |
| 2215 | */ |
| 2216 | MD_BUG(); |
| 2217 | mddev->events --; |
| 2218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | |
| 2220 | /* |
| 2221 | * do not write anything to disk if using |
| 2222 | * nonpersistent superblocks |
| 2223 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2224 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2225 | if (!mddev->external) |
| 2226 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2227 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2228 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2229 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2231 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2232 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2233 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | |
| 2235 | dprintk(KERN_INFO |
| 2236 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2237 | mdname(mddev),mddev->in_sync); |
| 2238 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2239 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2240 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | char b[BDEVNAME_SIZE]; |
| 2242 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2243 | if (rdev->sb_loaded != 1) |
| 2244 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2245 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | dprintk("(skipping faulty "); |
| 2247 | |
| 2248 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2249 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2250 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2251 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2252 | rdev->sb_page); |
| 2253 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2254 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2255 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2256 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | } else |
| 2259 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2260 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | /* only need to write one superblock... */ |
| 2262 | break; |
| 2263 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2264 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2265 | /* 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] | 2266 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2267 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2268 | if (mddev->in_sync != sync_req || |
| 2269 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2270 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2271 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2272 | goto repeat; |
| 2273 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2274 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2275 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2276 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2277 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2278 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2282 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2283 | * We want to accept with case. For this we use cmd_match. |
| 2284 | */ |
| 2285 | static int cmd_match(const char *cmd, const char *str) |
| 2286 | { |
| 2287 | /* See if cmd, written into a sysfs file, matches |
| 2288 | * str. They must either be the same, or cmd can |
| 2289 | * have a trailing newline |
| 2290 | */ |
| 2291 | while (*cmd && *str && *cmd == *str) { |
| 2292 | cmd++; |
| 2293 | str++; |
| 2294 | } |
| 2295 | if (*cmd == '\n') |
| 2296 | cmd++; |
| 2297 | if (*str || *cmd) |
| 2298 | return 0; |
| 2299 | return 1; |
| 2300 | } |
| 2301 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2302 | struct rdev_sysfs_entry { |
| 2303 | struct attribute attr; |
| 2304 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2305 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2306 | }; |
| 2307 | |
| 2308 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2309 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2310 | { |
| 2311 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2312 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2313 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2314 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2315 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2316 | sep = ","; |
| 2317 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2318 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2319 | len += sprintf(page+len, "%sin_sync",sep); |
| 2320 | sep = ","; |
| 2321 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2322 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2323 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2324 | sep = ","; |
| 2325 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2326 | if (test_bit(Blocked, &rdev->flags)) { |
| 2327 | len += sprintf(page+len, "%sblocked", sep); |
| 2328 | sep = ","; |
| 2329 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2330 | if (!test_bit(Faulty, &rdev->flags) && |
| 2331 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2332 | len += sprintf(page+len, "%sspare", sep); |
| 2333 | sep = ","; |
| 2334 | } |
| 2335 | return len+sprintf(page+len, "\n"); |
| 2336 | } |
| 2337 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2338 | static ssize_t |
| 2339 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2340 | { |
| 2341 | /* can write |
| 2342 | * faulty - simulates and error |
| 2343 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2344 | * writemostly - sets write_mostly |
| 2345 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2346 | * blocked - sets the Blocked flag |
| 2347 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2348 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2349 | */ |
| 2350 | int err = -EINVAL; |
| 2351 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2352 | md_error(rdev->mddev, rdev); |
| 2353 | err = 0; |
| 2354 | } else if (cmd_match(buf, "remove")) { |
| 2355 | if (rdev->raid_disk >= 0) |
| 2356 | err = -EBUSY; |
| 2357 | else { |
| 2358 | mddev_t *mddev = rdev->mddev; |
| 2359 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2360 | if (mddev->pers) |
| 2361 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2362 | md_new_event(mddev); |
| 2363 | err = 0; |
| 2364 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2365 | } else if (cmd_match(buf, "writemostly")) { |
| 2366 | set_bit(WriteMostly, &rdev->flags); |
| 2367 | err = 0; |
| 2368 | } else if (cmd_match(buf, "-writemostly")) { |
| 2369 | clear_bit(WriteMostly, &rdev->flags); |
| 2370 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2371 | } else if (cmd_match(buf, "blocked")) { |
| 2372 | set_bit(Blocked, &rdev->flags); |
| 2373 | err = 0; |
| 2374 | } else if (cmd_match(buf, "-blocked")) { |
| 2375 | clear_bit(Blocked, &rdev->flags); |
| 2376 | wake_up(&rdev->blocked_wait); |
| 2377 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2378 | md_wakeup_thread(rdev->mddev->thread); |
| 2379 | |
| 2380 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2381 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2382 | set_bit(In_sync, &rdev->flags); |
| 2383 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2384 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2385 | if (!err) |
| 2386 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2387 | return err ? err : len; |
| 2388 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2389 | static struct rdev_sysfs_entry rdev_state = |
| 2390 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2391 | |
| 2392 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2393 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2394 | { |
| 2395 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2396 | } |
| 2397 | |
| 2398 | static ssize_t |
| 2399 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2400 | { |
| 2401 | char *e; |
| 2402 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2403 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2404 | atomic_set(&rdev->corrected_errors, n); |
| 2405 | return len; |
| 2406 | } |
| 2407 | return -EINVAL; |
| 2408 | } |
| 2409 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2410 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2411 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2412 | static ssize_t |
| 2413 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2414 | { |
| 2415 | if (rdev->raid_disk < 0) |
| 2416 | return sprintf(page, "none\n"); |
| 2417 | else |
| 2418 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2419 | } |
| 2420 | |
| 2421 | static ssize_t |
| 2422 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2423 | { |
| 2424 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2425 | int err; |
| 2426 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2427 | int slot = simple_strtoul(buf, &e, 10); |
| 2428 | if (strncmp(buf, "none", 4)==0) |
| 2429 | slot = -1; |
| 2430 | else if (e==buf || (*e && *e!= '\n')) |
| 2431 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2432 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2433 | /* Setting 'slot' on an active array requires also |
| 2434 | * updating the 'rd%d' link, and communicating |
| 2435 | * with the personality with ->hot_*_disk. |
| 2436 | * For now we only support removing |
| 2437 | * failed/spare devices. This normally happens automatically, |
| 2438 | * but not when the metadata is externally managed. |
| 2439 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2440 | if (rdev->raid_disk == -1) |
| 2441 | return -EEXIST; |
| 2442 | /* personality does all needed checks */ |
| 2443 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2444 | return -EINVAL; |
| 2445 | err = rdev->mddev->pers-> |
| 2446 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2447 | if (err) |
| 2448 | return err; |
| 2449 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2450 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2451 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2452 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2453 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2454 | } else if (rdev->mddev->pers) { |
| 2455 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2456 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2457 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2458 | */ |
| 2459 | |
| 2460 | if (rdev->raid_disk != -1) |
| 2461 | return -EBUSY; |
| 2462 | |
| 2463 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2464 | return -EINVAL; |
| 2465 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2466 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2467 | if (rdev2->raid_disk == slot) |
| 2468 | return -EEXIST; |
| 2469 | |
| 2470 | rdev->raid_disk = slot; |
| 2471 | if (test_bit(In_sync, &rdev->flags)) |
| 2472 | rdev->saved_raid_disk = slot; |
| 2473 | else |
| 2474 | rdev->saved_raid_disk = -1; |
| 2475 | err = rdev->mddev->pers-> |
| 2476 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2477 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2478 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2479 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2480 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2481 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2482 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2483 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2484 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2485 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2486 | } else { |
| 2487 | if (slot >= rdev->mddev->raid_disks) |
| 2488 | return -ENOSPC; |
| 2489 | rdev->raid_disk = slot; |
| 2490 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2491 | clear_bit(Faulty, &rdev->flags); |
| 2492 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2493 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2494 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2495 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2496 | return len; |
| 2497 | } |
| 2498 | |
| 2499 | |
| 2500 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2501 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2502 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2503 | static ssize_t |
| 2504 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2505 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2506 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | static ssize_t |
| 2510 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2511 | { |
| 2512 | char *e; |
| 2513 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2514 | if (e==buf || (*e && *e != '\n')) |
| 2515 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2516 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2517 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2518 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2519 | /* Must set offset before size, so overlap checks |
| 2520 | * can be sane */ |
| 2521 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2522 | rdev->data_offset = offset; |
| 2523 | return len; |
| 2524 | } |
| 2525 | |
| 2526 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2527 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2528 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2529 | static ssize_t |
| 2530 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2531 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2532 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2533 | } |
| 2534 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2535 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2536 | { |
| 2537 | /* check if two start/length pairs overlap */ |
| 2538 | if (s1+l1 <= s2) |
| 2539 | return 0; |
| 2540 | if (s2+l2 <= s1) |
| 2541 | return 0; |
| 2542 | return 1; |
| 2543 | } |
| 2544 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2545 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2546 | { |
| 2547 | unsigned long long blocks; |
| 2548 | sector_t new; |
| 2549 | |
| 2550 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2551 | return -EINVAL; |
| 2552 | |
| 2553 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2554 | return -EINVAL; /* sector conversion overflow */ |
| 2555 | |
| 2556 | new = blocks * 2; |
| 2557 | if (new != blocks * 2) |
| 2558 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2559 | |
| 2560 | *sectors = new; |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2564 | static ssize_t |
| 2565 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2566 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2567 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2568 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2569 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2570 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2571 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2572 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2573 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2574 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2575 | sectors = super_types[my_mddev->major_version]. |
| 2576 | rdev_size_change(rdev, sectors); |
| 2577 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2578 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2579 | } else if (!sectors) |
| 2580 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2581 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2582 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2583 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2584 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2585 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2586 | rdev->sectors = sectors; |
| 2587 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2588 | /* need to check that all other rdevs with the same ->bdev |
| 2589 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2590 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2591 | * we have to change it back, we will have the lock again. |
| 2592 | */ |
| 2593 | mddev_t *mddev; |
| 2594 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2595 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2596 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2597 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2598 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2599 | mdk_rdev_t *rdev2; |
| 2600 | |
| 2601 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2602 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2603 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2604 | (rdev->bdev == rdev2->bdev && |
| 2605 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2606 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2607 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2608 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2609 | overlap = 1; |
| 2610 | break; |
| 2611 | } |
| 2612 | mddev_unlock(mddev); |
| 2613 | if (overlap) { |
| 2614 | mddev_put(mddev); |
| 2615 | break; |
| 2616 | } |
| 2617 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2618 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2619 | if (overlap) { |
| 2620 | /* Someone else could have slipped in a size |
| 2621 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2622 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2623 | * safe, and trust userspace not to race with |
| 2624 | * itself |
| 2625 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2626 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2627 | return -EBUSY; |
| 2628 | } |
| 2629 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2630 | return len; |
| 2631 | } |
| 2632 | |
| 2633 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2634 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2635 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2636 | |
| 2637 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2638 | { |
| 2639 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2640 | |
| 2641 | if (test_bit(In_sync, &rdev->flags) || |
| 2642 | recovery_start == MaxSector) |
| 2643 | return sprintf(page, "none\n"); |
| 2644 | |
| 2645 | return sprintf(page, "%llu\n", recovery_start); |
| 2646 | } |
| 2647 | |
| 2648 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2649 | { |
| 2650 | unsigned long long recovery_start; |
| 2651 | |
| 2652 | if (cmd_match(buf, "none")) |
| 2653 | recovery_start = MaxSector; |
| 2654 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2655 | return -EINVAL; |
| 2656 | |
| 2657 | if (rdev->mddev->pers && |
| 2658 | rdev->raid_disk >= 0) |
| 2659 | return -EBUSY; |
| 2660 | |
| 2661 | rdev->recovery_offset = recovery_start; |
| 2662 | if (recovery_start == MaxSector) |
| 2663 | set_bit(In_sync, &rdev->flags); |
| 2664 | else |
| 2665 | clear_bit(In_sync, &rdev->flags); |
| 2666 | return len; |
| 2667 | } |
| 2668 | |
| 2669 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2670 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2671 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2672 | static struct attribute *rdev_default_attrs[] = { |
| 2673 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2674 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2675 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2676 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2677 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2678 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2679 | NULL, |
| 2680 | }; |
| 2681 | static ssize_t |
| 2682 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2683 | { |
| 2684 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2685 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2686 | mddev_t *mddev = rdev->mddev; |
| 2687 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2688 | |
| 2689 | if (!entry->show) |
| 2690 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2691 | |
| 2692 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2693 | if (!rv) { |
| 2694 | if (rdev->mddev == NULL) |
| 2695 | rv = -EBUSY; |
| 2696 | else |
| 2697 | rv = entry->show(rdev, page); |
| 2698 | mddev_unlock(mddev); |
| 2699 | } |
| 2700 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | static ssize_t |
| 2704 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2705 | const char *page, size_t length) |
| 2706 | { |
| 2707 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2708 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2709 | ssize_t rv; |
| 2710 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2711 | |
| 2712 | if (!entry->store) |
| 2713 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2714 | if (!capable(CAP_SYS_ADMIN)) |
| 2715 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2716 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2717 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2718 | if (rdev->mddev == NULL) |
| 2719 | rv = -EBUSY; |
| 2720 | else |
| 2721 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2722 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2723 | } |
| 2724 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | static void rdev_free(struct kobject *ko) |
| 2728 | { |
| 2729 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2730 | kfree(rdev); |
| 2731 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2732 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2733 | .show = rdev_attr_show, |
| 2734 | .store = rdev_attr_store, |
| 2735 | }; |
| 2736 | static struct kobj_type rdev_ktype = { |
| 2737 | .release = rdev_free, |
| 2738 | .sysfs_ops = &rdev_sysfs_ops, |
| 2739 | .default_attrs = rdev_default_attrs, |
| 2740 | }; |
| 2741 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2742 | void md_rdev_init(mdk_rdev_t *rdev) |
| 2743 | { |
| 2744 | rdev->desc_nr = -1; |
| 2745 | rdev->saved_raid_disk = -1; |
| 2746 | rdev->raid_disk = -1; |
| 2747 | rdev->flags = 0; |
| 2748 | rdev->data_offset = 0; |
| 2749 | rdev->sb_events = 0; |
| 2750 | rdev->last_read_error.tv_sec = 0; |
| 2751 | rdev->last_read_error.tv_nsec = 0; |
| 2752 | atomic_set(&rdev->nr_pending, 0); |
| 2753 | atomic_set(&rdev->read_errors, 0); |
| 2754 | atomic_set(&rdev->corrected_errors, 0); |
| 2755 | |
| 2756 | INIT_LIST_HEAD(&rdev->same_set); |
| 2757 | init_waitqueue_head(&rdev->blocked_wait); |
| 2758 | } |
| 2759 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | /* |
| 2761 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2762 | * |
| 2763 | * mark the device faulty if: |
| 2764 | * |
| 2765 | * - the device is nonexistent (zero size) |
| 2766 | * - the device has no valid superblock |
| 2767 | * |
| 2768 | * a faulty rdev _never_ has rdev->sb set. |
| 2769 | */ |
| 2770 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2771 | { |
| 2772 | char b[BDEVNAME_SIZE]; |
| 2773 | int err; |
| 2774 | mdk_rdev_t *rdev; |
| 2775 | sector_t size; |
| 2776 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2777 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | if (!rdev) { |
| 2779 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2780 | return ERR_PTR(-ENOMEM); |
| 2781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2783 | md_rdev_init(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | if ((err = alloc_disk_sb(rdev))) |
| 2785 | goto abort_free; |
| 2786 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2787 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | if (err) |
| 2789 | goto abort_free; |
| 2790 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2791 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2794 | if (!size) { |
| 2795 | printk(KERN_WARNING |
| 2796 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2797 | bdevname(rdev->bdev,b)); |
| 2798 | err = -EINVAL; |
| 2799 | goto abort_free; |
| 2800 | } |
| 2801 | |
| 2802 | if (super_format >= 0) { |
| 2803 | err = super_types[super_format]. |
| 2804 | load_super(rdev, NULL, super_minor); |
| 2805 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2806 | printk(KERN_WARNING |
| 2807 | "md: %s does not have a valid v%d.%d " |
| 2808 | "superblock, not importing!\n", |
| 2809 | bdevname(rdev->bdev,b), |
| 2810 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | goto abort_free; |
| 2812 | } |
| 2813 | if (err < 0) { |
| 2814 | printk(KERN_WARNING |
| 2815 | "md: could not read %s's sb, not importing!\n", |
| 2816 | bdevname(rdev->bdev,b)); |
| 2817 | goto abort_free; |
| 2818 | } |
| 2819 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | return rdev; |
| 2822 | |
| 2823 | abort_free: |
| 2824 | if (rdev->sb_page) { |
| 2825 | if (rdev->bdev) |
| 2826 | unlock_rdev(rdev); |
| 2827 | free_disk_sb(rdev); |
| 2828 | } |
| 2829 | kfree(rdev); |
| 2830 | return ERR_PTR(err); |
| 2831 | } |
| 2832 | |
| 2833 | /* |
| 2834 | * Check a full RAID array for plausibility |
| 2835 | */ |
| 2836 | |
| 2837 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2838 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | { |
| 2840 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2841 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | char b[BDEVNAME_SIZE]; |
| 2843 | |
| 2844 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2845 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | switch (super_types[mddev->major_version]. |
| 2847 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2848 | case 1: |
| 2849 | freshest = rdev; |
| 2850 | break; |
| 2851 | case 0: |
| 2852 | break; |
| 2853 | default: |
| 2854 | printk( KERN_ERR \ |
| 2855 | "md: fatal superblock inconsistency in %s" |
| 2856 | " -- removing from array\n", |
| 2857 | bdevname(rdev->bdev,b)); |
| 2858 | kick_rdev_from_array(rdev); |
| 2859 | } |
| 2860 | |
| 2861 | |
| 2862 | super_types[mddev->major_version]. |
| 2863 | validate_super(mddev, freshest); |
| 2864 | |
| 2865 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2866 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2867 | if (mddev->max_disks && |
| 2868 | (rdev->desc_nr >= mddev->max_disks || |
| 2869 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2870 | printk(KERN_WARNING |
| 2871 | "md: %s: %s: only %d devices permitted\n", |
| 2872 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2873 | mddev->max_disks); |
| 2874 | kick_rdev_from_array(rdev); |
| 2875 | continue; |
| 2876 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | if (rdev != freshest) |
| 2878 | if (super_types[mddev->major_version]. |
| 2879 | validate_super(mddev, rdev)) { |
| 2880 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2881 | " from array!\n", |
| 2882 | bdevname(rdev->bdev,b)); |
| 2883 | kick_rdev_from_array(rdev); |
| 2884 | continue; |
| 2885 | } |
| 2886 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2887 | rdev->desc_nr = i++; |
| 2888 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2889 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2890 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2891 | rdev->raid_disk = -1; |
| 2892 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | } |
| 2894 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | } |
| 2896 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2897 | /* Read a fixed-point number. |
| 2898 | * Numbers in sysfs attributes should be in "standard" units where |
| 2899 | * possible, so time should be in seconds. |
| 2900 | * However we internally use a a much smaller unit such as |
| 2901 | * milliseconds or jiffies. |
| 2902 | * This function takes a decimal number with a possible fractional |
| 2903 | * component, and produces an integer which is the result of |
| 2904 | * multiplying that number by 10^'scale'. |
| 2905 | * all without any floating-point arithmetic. |
| 2906 | */ |
| 2907 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2908 | { |
| 2909 | unsigned long result = 0; |
| 2910 | long decimals = -1; |
| 2911 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2912 | if (*cp == '.') |
| 2913 | decimals = 0; |
| 2914 | else if (decimals < scale) { |
| 2915 | unsigned int value; |
| 2916 | value = *cp - '0'; |
| 2917 | result = result * 10 + value; |
| 2918 | if (decimals >= 0) |
| 2919 | decimals++; |
| 2920 | } |
| 2921 | cp++; |
| 2922 | } |
| 2923 | if (*cp == '\n') |
| 2924 | cp++; |
| 2925 | if (*cp) |
| 2926 | return -EINVAL; |
| 2927 | if (decimals < 0) |
| 2928 | decimals = 0; |
| 2929 | while (decimals < scale) { |
| 2930 | result *= 10; |
| 2931 | decimals ++; |
| 2932 | } |
| 2933 | *res = result; |
| 2934 | return 0; |
| 2935 | } |
| 2936 | |
| 2937 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2938 | static void md_safemode_timeout(unsigned long data); |
| 2939 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2940 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2941 | safe_delay_show(mddev_t *mddev, char *page) |
| 2942 | { |
| 2943 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2944 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2945 | } |
| 2946 | static ssize_t |
| 2947 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2948 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2949 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2950 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2951 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2952 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2953 | if (msec == 0) |
| 2954 | mddev->safemode_delay = 0; |
| 2955 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2956 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2957 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2958 | if (mddev->safemode_delay == 0) |
| 2959 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2960 | if (mddev->safemode_delay < old_delay) |
| 2961 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2962 | } |
| 2963 | return len; |
| 2964 | } |
| 2965 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2966 | __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] | 2967 | |
| 2968 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2969 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2970 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2971 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2972 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2973 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2974 | else if (mddev->clevel[0]) |
| 2975 | return sprintf(page, "%s\n", mddev->clevel); |
| 2976 | else if (mddev->level != LEVEL_NONE) |
| 2977 | return sprintf(page, "%d\n", mddev->level); |
| 2978 | else |
| 2979 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2980 | } |
| 2981 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2982 | static ssize_t |
| 2983 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2984 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2985 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2986 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2987 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2988 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2989 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2990 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2991 | |
| 2992 | if (mddev->pers == NULL) { |
| 2993 | if (len == 0) |
| 2994 | return 0; |
| 2995 | if (len >= sizeof(mddev->clevel)) |
| 2996 | return -ENOSPC; |
| 2997 | strncpy(mddev->clevel, buf, len); |
| 2998 | if (mddev->clevel[len-1] == '\n') |
| 2999 | len--; |
| 3000 | mddev->clevel[len] = 0; |
| 3001 | mddev->level = LEVEL_NONE; |
| 3002 | return rv; |
| 3003 | } |
| 3004 | |
| 3005 | /* request to change the personality. Need to ensure: |
| 3006 | * - array is not engaged in resync/recovery/reshape |
| 3007 | * - old personality can be suspended |
| 3008 | * - new personality will access other array. |
| 3009 | */ |
| 3010 | |
| 3011 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3012 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3013 | |
| 3014 | if (!mddev->pers->quiesce) { |
| 3015 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3016 | mdname(mddev), mddev->pers->name); |
| 3017 | return -EINVAL; |
| 3018 | } |
| 3019 | |
| 3020 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3021 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3022 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3023 | strncpy(clevel, buf, len); |
| 3024 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3025 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3026 | clevel[len] = 0; |
| 3027 | if (strict_strtol(clevel, 10, &level)) |
| 3028 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3029 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3030 | if (request_module("md-%s", clevel) != 0) |
| 3031 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3032 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3033 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3034 | if (!pers || !try_module_get(pers->owner)) { |
| 3035 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3036 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3037 | return -EINVAL; |
| 3038 | } |
| 3039 | spin_unlock(&pers_lock); |
| 3040 | |
| 3041 | if (pers == mddev->pers) { |
| 3042 | /* Nothing to do! */ |
| 3043 | module_put(pers->owner); |
| 3044 | return rv; |
| 3045 | } |
| 3046 | if (!pers->takeover) { |
| 3047 | module_put(pers->owner); |
| 3048 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3049 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3050 | return -EINVAL; |
| 3051 | } |
| 3052 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3053 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3054 | rdev->new_raid_disk = rdev->raid_disk; |
| 3055 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3056 | /* ->takeover must set new_* and/or delta_disks |
| 3057 | * if it succeeds, and may set them when it fails. |
| 3058 | */ |
| 3059 | priv = pers->takeover(mddev); |
| 3060 | if (IS_ERR(priv)) { |
| 3061 | mddev->new_level = mddev->level; |
| 3062 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3063 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3064 | mddev->raid_disks -= mddev->delta_disks; |
| 3065 | mddev->delta_disks = 0; |
| 3066 | module_put(pers->owner); |
| 3067 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3068 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3069 | return PTR_ERR(priv); |
| 3070 | } |
| 3071 | |
| 3072 | /* Looks like we have a winner */ |
| 3073 | mddev_suspend(mddev); |
| 3074 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3075 | |
| 3076 | if (mddev->pers->sync_request == NULL && |
| 3077 | pers->sync_request != NULL) { |
| 3078 | /* need to add the md_redundancy_group */ |
| 3079 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3080 | printk(KERN_WARNING |
| 3081 | "md: cannot register extra attributes for %s\n", |
| 3082 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3083 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3084 | } |
| 3085 | if (mddev->pers->sync_request != NULL && |
| 3086 | pers->sync_request == NULL) { |
| 3087 | /* need to remove the md_redundancy_group */ |
| 3088 | if (mddev->to_remove == NULL) |
| 3089 | mddev->to_remove = &md_redundancy_group; |
| 3090 | } |
| 3091 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3092 | if (mddev->pers->sync_request == NULL && |
| 3093 | mddev->external) { |
| 3094 | /* We are converting from a no-redundancy array |
| 3095 | * to a redundancy array and metadata is managed |
| 3096 | * externally so we need to be sure that writes |
| 3097 | * won't block due to a need to transition |
| 3098 | * clean->dirty |
| 3099 | * until external management is started. |
| 3100 | */ |
| 3101 | mddev->in_sync = 0; |
| 3102 | mddev->safemode_delay = 0; |
| 3103 | mddev->safemode = 0; |
| 3104 | } |
| 3105 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3106 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3107 | char nm[20]; |
| 3108 | if (rdev->raid_disk < 0) |
| 3109 | continue; |
| 3110 | if (rdev->new_raid_disk > mddev->raid_disks) |
| 3111 | rdev->new_raid_disk = -1; |
| 3112 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3113 | continue; |
| 3114 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3115 | sysfs_remove_link(&mddev->kobj, nm); |
| 3116 | } |
| 3117 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3118 | if (rdev->raid_disk < 0) |
| 3119 | continue; |
| 3120 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3121 | continue; |
| 3122 | rdev->raid_disk = rdev->new_raid_disk; |
| 3123 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3124 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3125 | else { |
| 3126 | char nm[20]; |
| 3127 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3128 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3129 | printk("md: cannot register %s for %s after level change\n", |
| 3130 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3131 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3135 | mddev->pers = pers; |
| 3136 | mddev->private = priv; |
| 3137 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3138 | mddev->level = mddev->new_level; |
| 3139 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3140 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3141 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3142 | if (mddev->pers->sync_request == NULL) { |
| 3143 | /* this is now an array without redundancy, so |
| 3144 | * it must always be in_sync |
| 3145 | */ |
| 3146 | mddev->in_sync = 1; |
| 3147 | del_timer_sync(&mddev->safemode_timer); |
| 3148 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3149 | pers->run(mddev); |
| 3150 | mddev_resume(mddev); |
| 3151 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3152 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3153 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3154 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3155 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3156 | return rv; |
| 3157 | } |
| 3158 | |
| 3159 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3160 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3161 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3162 | |
| 3163 | static ssize_t |
| 3164 | layout_show(mddev_t *mddev, char *page) |
| 3165 | { |
| 3166 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3167 | if (mddev->reshape_position != MaxSector && |
| 3168 | mddev->layout != mddev->new_layout) |
| 3169 | return sprintf(page, "%d (%d)\n", |
| 3170 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3171 | return sprintf(page, "%d\n", mddev->layout); |
| 3172 | } |
| 3173 | |
| 3174 | static ssize_t |
| 3175 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3176 | { |
| 3177 | char *e; |
| 3178 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3179 | |
| 3180 | if (!*buf || (*e && *e != '\n')) |
| 3181 | return -EINVAL; |
| 3182 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3183 | if (mddev->pers) { |
| 3184 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3185 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3186 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3187 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3188 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3189 | if (err) { |
| 3190 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3191 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3192 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3193 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3194 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3195 | if (mddev->reshape_position == MaxSector) |
| 3196 | mddev->layout = n; |
| 3197 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3198 | return len; |
| 3199 | } |
| 3200 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3201 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3202 | |
| 3203 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3204 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3205 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3206 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3207 | if (mddev->raid_disks == 0) |
| 3208 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3209 | if (mddev->reshape_position != MaxSector && |
| 3210 | mddev->delta_disks != 0) |
| 3211 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3212 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3213 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3214 | } |
| 3215 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3216 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3217 | |
| 3218 | static ssize_t |
| 3219 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3220 | { |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3221 | char *e; |
| 3222 | int rv = 0; |
| 3223 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3224 | |
| 3225 | if (!*buf || (*e && *e != '\n')) |
| 3226 | return -EINVAL; |
| 3227 | |
| 3228 | if (mddev->pers) |
| 3229 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3230 | else if (mddev->reshape_position != MaxSector) { |
| 3231 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3232 | mddev->delta_disks = n - olddisks; |
| 3233 | mddev->raid_disks = n; |
| 3234 | } else |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3235 | mddev->raid_disks = n; |
| 3236 | return rv ? rv : len; |
| 3237 | } |
| 3238 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3239 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3240 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3241 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3242 | chunk_size_show(mddev_t *mddev, char *page) |
| 3243 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3244 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3245 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3246 | return sprintf(page, "%d (%d)\n", |
| 3247 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3248 | mddev->chunk_sectors << 9); |
| 3249 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | static ssize_t |
| 3253 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3254 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3255 | char *e; |
| 3256 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3257 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3258 | if (!*buf || (*e && *e != '\n')) |
| 3259 | return -EINVAL; |
| 3260 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3261 | if (mddev->pers) { |
| 3262 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3263 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3264 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3265 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3266 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3267 | if (err) { |
| 3268 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3269 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3270 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3271 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3272 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3273 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3274 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3275 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3276 | return len; |
| 3277 | } |
| 3278 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3279 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3280 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3281 | static ssize_t |
| 3282 | resync_start_show(mddev_t *mddev, char *page) |
| 3283 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3284 | if (mddev->recovery_cp == MaxSector) |
| 3285 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3286 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3287 | } |
| 3288 | |
| 3289 | static ssize_t |
| 3290 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3291 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3292 | char *e; |
| 3293 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3294 | |
| 3295 | if (mddev->pers) |
| 3296 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3297 | if (cmd_match(buf, "none")) |
| 3298 | n = MaxSector; |
| 3299 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3300 | return -EINVAL; |
| 3301 | |
| 3302 | mddev->recovery_cp = n; |
| 3303 | return len; |
| 3304 | } |
| 3305 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3306 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3307 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3308 | /* |
| 3309 | * The array state can be: |
| 3310 | * |
| 3311 | * clear |
| 3312 | * No devices, no size, no level |
| 3313 | * Equivalent to STOP_ARRAY ioctl |
| 3314 | * inactive |
| 3315 | * May have some settings, but array is not active |
| 3316 | * all IO results in error |
| 3317 | * When written, doesn't tear down array, but just stops it |
| 3318 | * suspended (not supported yet) |
| 3319 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3320 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3321 | * readonly |
| 3322 | * no resync can happen. no superblocks get written. |
| 3323 | * write requests fail |
| 3324 | * read-auto |
| 3325 | * like readonly, but behaves like 'clean' on a write request. |
| 3326 | * |
| 3327 | * clean - no pending writes, but otherwise active. |
| 3328 | * When written to inactive array, starts without resync |
| 3329 | * If a write request arrives then |
| 3330 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3331 | * if not known, block and switch to write-pending |
| 3332 | * If written to an active array that has pending writes, then fails. |
| 3333 | * active |
| 3334 | * fully active: IO and resync can be happening. |
| 3335 | * When written to inactive array, starts with resync |
| 3336 | * |
| 3337 | * write-pending |
| 3338 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3339 | * |
| 3340 | * active-idle |
| 3341 | * like active, but no writes have been seen for a while (100msec). |
| 3342 | * |
| 3343 | */ |
| 3344 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3345 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3346 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3347 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3348 | "write-pending", "active-idle", NULL }; |
| 3349 | |
| 3350 | static int match_word(const char *word, char **list) |
| 3351 | { |
| 3352 | int n; |
| 3353 | for (n=0; list[n]; n++) |
| 3354 | if (cmd_match(word, list[n])) |
| 3355 | break; |
| 3356 | return n; |
| 3357 | } |
| 3358 | |
| 3359 | static ssize_t |
| 3360 | array_state_show(mddev_t *mddev, char *page) |
| 3361 | { |
| 3362 | enum array_state st = inactive; |
| 3363 | |
| 3364 | if (mddev->pers) |
| 3365 | switch(mddev->ro) { |
| 3366 | case 1: |
| 3367 | st = readonly; |
| 3368 | break; |
| 3369 | case 2: |
| 3370 | st = read_auto; |
| 3371 | break; |
| 3372 | case 0: |
| 3373 | if (mddev->in_sync) |
| 3374 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3375 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3376 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3377 | else if (mddev->safemode) |
| 3378 | st = active_idle; |
| 3379 | else |
| 3380 | st = active; |
| 3381 | } |
| 3382 | else { |
| 3383 | if (list_empty(&mddev->disks) && |
| 3384 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3385 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3386 | st = clear; |
| 3387 | else |
| 3388 | st = inactive; |
| 3389 | } |
| 3390 | return sprintf(page, "%s\n", array_states[st]); |
| 3391 | } |
| 3392 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3393 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3394 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3395 | static int do_md_run(mddev_t * mddev); |
| 3396 | static int restart_array(mddev_t *mddev); |
| 3397 | |
| 3398 | static ssize_t |
| 3399 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3400 | { |
| 3401 | int err = -EINVAL; |
| 3402 | enum array_state st = match_word(buf, array_states); |
| 3403 | switch(st) { |
| 3404 | case bad_word: |
| 3405 | break; |
| 3406 | case clear: |
| 3407 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3408 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3409 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3410 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3411 | break; |
| 3412 | case inactive: |
| 3413 | /* stopping an active array */ |
| 3414 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3415 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3416 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3417 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3418 | } else |
| 3419 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3420 | break; |
| 3421 | case suspended: |
| 3422 | break; /* not supported yet */ |
| 3423 | case readonly: |
| 3424 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3425 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3426 | else { |
| 3427 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3428 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3429 | err = do_md_run(mddev); |
| 3430 | } |
| 3431 | break; |
| 3432 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3433 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3434 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3435 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3436 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3437 | err = restart_array(mddev); |
| 3438 | if (err == 0) { |
| 3439 | mddev->ro = 2; |
| 3440 | set_disk_ro(mddev->gendisk, 0); |
| 3441 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3442 | } else { |
| 3443 | mddev->ro = 2; |
| 3444 | err = do_md_run(mddev); |
| 3445 | } |
| 3446 | break; |
| 3447 | case clean: |
| 3448 | if (mddev->pers) { |
| 3449 | restart_array(mddev); |
| 3450 | spin_lock_irq(&mddev->write_lock); |
| 3451 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3452 | if (mddev->in_sync == 0) { |
| 3453 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3454 | if (mddev->safemode == 1) |
| 3455 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3456 | if (mddev->persistent) |
| 3457 | set_bit(MD_CHANGE_CLEAN, |
| 3458 | &mddev->flags); |
| 3459 | } |
| 3460 | err = 0; |
| 3461 | } else |
| 3462 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3463 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3464 | } else |
| 3465 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3466 | break; |
| 3467 | case active: |
| 3468 | if (mddev->pers) { |
| 3469 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3470 | if (mddev->external) |
| 3471 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3472 | wake_up(&mddev->sb_wait); |
| 3473 | err = 0; |
| 3474 | } else { |
| 3475 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3476 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3477 | err = do_md_run(mddev); |
| 3478 | } |
| 3479 | break; |
| 3480 | case write_pending: |
| 3481 | case active_idle: |
| 3482 | /* these cannot be set */ |
| 3483 | break; |
| 3484 | } |
| 3485 | if (err) |
| 3486 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3487 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3488 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3489 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3490 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3491 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3492 | static struct md_sysfs_entry md_array_state = |
| 3493 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3494 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3495 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3496 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3497 | return sprintf(page, "%d\n", |
| 3498 | atomic_read(&mddev->max_corr_read_errors)); |
| 3499 | } |
| 3500 | |
| 3501 | static ssize_t |
| 3502 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3503 | { |
| 3504 | char *e; |
| 3505 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3506 | |
| 3507 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3508 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3509 | return len; |
| 3510 | } |
| 3511 | return -EINVAL; |
| 3512 | } |
| 3513 | |
| 3514 | static struct md_sysfs_entry max_corr_read_errors = |
| 3515 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3516 | max_corrected_read_errors_store); |
| 3517 | |
| 3518 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3519 | null_show(mddev_t *mddev, char *page) |
| 3520 | { |
| 3521 | return -EINVAL; |
| 3522 | } |
| 3523 | |
| 3524 | static ssize_t |
| 3525 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3526 | { |
| 3527 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3528 | /* The new device is added to the array. |
| 3529 | * If the array has a persistent superblock, we read the |
| 3530 | * superblock to initialise info and check validity. |
| 3531 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3532 | * which mainly checks size. |
| 3533 | */ |
| 3534 | char *e; |
| 3535 | int major = simple_strtoul(buf, &e, 10); |
| 3536 | int minor; |
| 3537 | dev_t dev; |
| 3538 | mdk_rdev_t *rdev; |
| 3539 | int err; |
| 3540 | |
| 3541 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3542 | return -EINVAL; |
| 3543 | minor = simple_strtoul(e+1, &e, 10); |
| 3544 | if (*e && *e != '\n') |
| 3545 | return -EINVAL; |
| 3546 | dev = MKDEV(major, minor); |
| 3547 | if (major != MAJOR(dev) || |
| 3548 | minor != MINOR(dev)) |
| 3549 | return -EOVERFLOW; |
| 3550 | |
| 3551 | |
| 3552 | if (mddev->persistent) { |
| 3553 | rdev = md_import_device(dev, mddev->major_version, |
| 3554 | mddev->minor_version); |
| 3555 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3556 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3557 | mdk_rdev_t, same_set); |
| 3558 | err = super_types[mddev->major_version] |
| 3559 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3560 | if (err < 0) |
| 3561 | goto out; |
| 3562 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3563 | } else if (mddev->external) |
| 3564 | rdev = md_import_device(dev, -2, -1); |
| 3565 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3566 | rdev = md_import_device(dev, -1, -1); |
| 3567 | |
| 3568 | if (IS_ERR(rdev)) |
| 3569 | return PTR_ERR(rdev); |
| 3570 | err = bind_rdev_to_array(rdev, mddev); |
| 3571 | out: |
| 3572 | if (err) |
| 3573 | export_rdev(rdev); |
| 3574 | return err ? err : len; |
| 3575 | } |
| 3576 | |
| 3577 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3578 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3579 | |
| 3580 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3581 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3582 | { |
| 3583 | char *end; |
| 3584 | unsigned long chunk, end_chunk; |
| 3585 | |
| 3586 | if (!mddev->bitmap) |
| 3587 | goto out; |
| 3588 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3589 | while (*buf) { |
| 3590 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3591 | if (buf == end) break; |
| 3592 | if (*end == '-') { /* range */ |
| 3593 | buf = end + 1; |
| 3594 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3595 | if (buf == end) break; |
| 3596 | } |
| 3597 | if (*end && !isspace(*end)) break; |
| 3598 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3599 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3600 | } |
| 3601 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3602 | out: |
| 3603 | return len; |
| 3604 | } |
| 3605 | |
| 3606 | static struct md_sysfs_entry md_bitmap = |
| 3607 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3608 | |
| 3609 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3610 | size_show(mddev_t *mddev, char *page) |
| 3611 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3612 | return sprintf(page, "%llu\n", |
| 3613 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3614 | } |
| 3615 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3616 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3617 | |
| 3618 | static ssize_t |
| 3619 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3620 | { |
| 3621 | /* If array is inactive, we can reduce the component size, but |
| 3622 | * not increase it (except from 0). |
| 3623 | * If array is active, we can try an on-line resize |
| 3624 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3625 | sector_t sectors; |
| 3626 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3627 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3628 | if (err < 0) |
| 3629 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3630 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3631 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3632 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3633 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3634 | if (mddev->dev_sectors == 0 || |
| 3635 | mddev->dev_sectors > sectors) |
| 3636 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3637 | else |
| 3638 | err = -ENOSPC; |
| 3639 | } |
| 3640 | return err ? err : len; |
| 3641 | } |
| 3642 | |
| 3643 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3644 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3645 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3646 | |
| 3647 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3648 | * This is one of |
| 3649 | * 'none' for arrays with no metadata (good luck...) |
| 3650 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3651 | * or N.M for internally known formats |
| 3652 | */ |
| 3653 | static ssize_t |
| 3654 | metadata_show(mddev_t *mddev, char *page) |
| 3655 | { |
| 3656 | if (mddev->persistent) |
| 3657 | return sprintf(page, "%d.%d\n", |
| 3658 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3659 | else if (mddev->external) |
| 3660 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3661 | else |
| 3662 | return sprintf(page, "none\n"); |
| 3663 | } |
| 3664 | |
| 3665 | static ssize_t |
| 3666 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3667 | { |
| 3668 | int major, minor; |
| 3669 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3670 | /* Changing the details of 'external' metadata is |
| 3671 | * always permitted. Otherwise there must be |
| 3672 | * no devices attached to the array. |
| 3673 | */ |
| 3674 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3675 | ; |
| 3676 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3677 | return -EBUSY; |
| 3678 | |
| 3679 | if (cmd_match(buf, "none")) { |
| 3680 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3681 | mddev->external = 0; |
| 3682 | mddev->major_version = 0; |
| 3683 | mddev->minor_version = 90; |
| 3684 | return len; |
| 3685 | } |
| 3686 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3687 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3688 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3689 | namelen = sizeof(mddev->metadata_type)-1; |
| 3690 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3691 | mddev->metadata_type[namelen] = 0; |
| 3692 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3693 | mddev->metadata_type[--namelen] = 0; |
| 3694 | mddev->persistent = 0; |
| 3695 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3696 | mddev->major_version = 0; |
| 3697 | mddev->minor_version = 90; |
| 3698 | return len; |
| 3699 | } |
| 3700 | major = simple_strtoul(buf, &e, 10); |
| 3701 | if (e==buf || *e != '.') |
| 3702 | return -EINVAL; |
| 3703 | buf = e+1; |
| 3704 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3705 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3706 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3707 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3708 | return -ENOENT; |
| 3709 | mddev->major_version = major; |
| 3710 | mddev->minor_version = minor; |
| 3711 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3712 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3713 | return len; |
| 3714 | } |
| 3715 | |
| 3716 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3717 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3718 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3719 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3720 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3721 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3722 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3723 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3724 | type = "frozen"; |
| 3725 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3726 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3727 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3728 | type = "reshape"; |
| 3729 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3730 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3731 | type = "resync"; |
| 3732 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3733 | type = "check"; |
| 3734 | else |
| 3735 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3736 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3737 | type = "recover"; |
| 3738 | } |
| 3739 | return sprintf(page, "%s\n", type); |
| 3740 | } |
| 3741 | |
| 3742 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3743 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3744 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3745 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3746 | return -EINVAL; |
| 3747 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3748 | if (cmd_match(page, "frozen")) |
| 3749 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3750 | else |
| 3751 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3752 | |
| 3753 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3754 | if (mddev->sync_thread) { |
| 3755 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3756 | md_unregister_thread(mddev->sync_thread); |
| 3757 | mddev->sync_thread = NULL; |
| 3758 | mddev->recovery = 0; |
| 3759 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3760 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3761 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3762 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3763 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3764 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3765 | else if (cmd_match(page, "recover")) { |
| 3766 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3767 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3768 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3769 | int err; |
| 3770 | if (mddev->pers->start_reshape == NULL) |
| 3771 | return -EINVAL; |
| 3772 | err = mddev->pers->start_reshape(mddev); |
| 3773 | if (err) |
| 3774 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3775 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3776 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3777 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3778 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3779 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3780 | return -EINVAL; |
| 3781 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3782 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3783 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3784 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3785 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3786 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3787 | return len; |
| 3788 | } |
| 3789 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3790 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3791 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3792 | { |
| 3793 | return sprintf(page, "%llu\n", |
| 3794 | (unsigned long long) mddev->resync_mismatches); |
| 3795 | } |
| 3796 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3797 | static struct md_sysfs_entry md_scan_mode = |
| 3798 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3799 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3800 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3801 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3802 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3803 | static ssize_t |
| 3804 | sync_min_show(mddev_t *mddev, char *page) |
| 3805 | { |
| 3806 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3807 | mddev->sync_speed_min ? "local": "system"); |
| 3808 | } |
| 3809 | |
| 3810 | static ssize_t |
| 3811 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3812 | { |
| 3813 | int min; |
| 3814 | char *e; |
| 3815 | if (strncmp(buf, "system", 6)==0) { |
| 3816 | mddev->sync_speed_min = 0; |
| 3817 | return len; |
| 3818 | } |
| 3819 | min = simple_strtoul(buf, &e, 10); |
| 3820 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3821 | return -EINVAL; |
| 3822 | mddev->sync_speed_min = min; |
| 3823 | return len; |
| 3824 | } |
| 3825 | |
| 3826 | static struct md_sysfs_entry md_sync_min = |
| 3827 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3828 | |
| 3829 | static ssize_t |
| 3830 | sync_max_show(mddev_t *mddev, char *page) |
| 3831 | { |
| 3832 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3833 | mddev->sync_speed_max ? "local": "system"); |
| 3834 | } |
| 3835 | |
| 3836 | static ssize_t |
| 3837 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3838 | { |
| 3839 | int max; |
| 3840 | char *e; |
| 3841 | if (strncmp(buf, "system", 6)==0) { |
| 3842 | mddev->sync_speed_max = 0; |
| 3843 | return len; |
| 3844 | } |
| 3845 | max = simple_strtoul(buf, &e, 10); |
| 3846 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3847 | return -EINVAL; |
| 3848 | mddev->sync_speed_max = max; |
| 3849 | return len; |
| 3850 | } |
| 3851 | |
| 3852 | static struct md_sysfs_entry md_sync_max = |
| 3853 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3854 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3855 | static ssize_t |
| 3856 | degraded_show(mddev_t *mddev, char *page) |
| 3857 | { |
| 3858 | return sprintf(page, "%d\n", mddev->degraded); |
| 3859 | } |
| 3860 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3861 | |
| 3862 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3863 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3864 | { |
| 3865 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3866 | } |
| 3867 | |
| 3868 | static ssize_t |
| 3869 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3870 | { |
| 3871 | long n; |
| 3872 | |
| 3873 | if (strict_strtol(buf, 10, &n)) |
| 3874 | return -EINVAL; |
| 3875 | |
| 3876 | if (n != 0 && n != 1) |
| 3877 | return -EINVAL; |
| 3878 | |
| 3879 | mddev->parallel_resync = n; |
| 3880 | |
| 3881 | if (mddev->sync_thread) |
| 3882 | wake_up(&resync_wait); |
| 3883 | |
| 3884 | return len; |
| 3885 | } |
| 3886 | |
| 3887 | /* force parallel resync, even with shared block devices */ |
| 3888 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3889 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3890 | sync_force_parallel_show, sync_force_parallel_store); |
| 3891 | |
| 3892 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3893 | sync_speed_show(mddev_t *mddev, char *page) |
| 3894 | { |
| 3895 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3896 | if (mddev->curr_resync == 0) |
| 3897 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3898 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3899 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3900 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3901 | db = resync - mddev->resync_mark_cnt; |
| 3902 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3903 | } |
| 3904 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3905 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3906 | |
| 3907 | static ssize_t |
| 3908 | sync_completed_show(mddev_t *mddev, char *page) |
| 3909 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3910 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3911 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3912 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3913 | return sprintf(page, "none\n"); |
| 3914 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3915 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3916 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3917 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3918 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3919 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3920 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3921 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3922 | } |
| 3923 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3924 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3925 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3926 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3927 | min_sync_show(mddev_t *mddev, char *page) |
| 3928 | { |
| 3929 | return sprintf(page, "%llu\n", |
| 3930 | (unsigned long long)mddev->resync_min); |
| 3931 | } |
| 3932 | static ssize_t |
| 3933 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3934 | { |
| 3935 | unsigned long long min; |
| 3936 | if (strict_strtoull(buf, 10, &min)) |
| 3937 | return -EINVAL; |
| 3938 | if (min > mddev->resync_max) |
| 3939 | return -EINVAL; |
| 3940 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3941 | return -EBUSY; |
| 3942 | |
| 3943 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3944 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3945 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3946 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3947 | return -EINVAL; |
| 3948 | } |
| 3949 | mddev->resync_min = min; |
| 3950 | |
| 3951 | return len; |
| 3952 | } |
| 3953 | |
| 3954 | static struct md_sysfs_entry md_min_sync = |
| 3955 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3956 | |
| 3957 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3958 | max_sync_show(mddev_t *mddev, char *page) |
| 3959 | { |
| 3960 | if (mddev->resync_max == MaxSector) |
| 3961 | return sprintf(page, "max\n"); |
| 3962 | else |
| 3963 | return sprintf(page, "%llu\n", |
| 3964 | (unsigned long long)mddev->resync_max); |
| 3965 | } |
| 3966 | static ssize_t |
| 3967 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3968 | { |
| 3969 | if (strncmp(buf, "max", 3) == 0) |
| 3970 | mddev->resync_max = MaxSector; |
| 3971 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3972 | unsigned long long max; |
| 3973 | if (strict_strtoull(buf, 10, &max)) |
| 3974 | return -EINVAL; |
| 3975 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3976 | return -EINVAL; |
| 3977 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3978 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3979 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3980 | return -EBUSY; |
| 3981 | |
| 3982 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3983 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3984 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3985 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3986 | return -EINVAL; |
| 3987 | } |
| 3988 | mddev->resync_max = max; |
| 3989 | } |
| 3990 | wake_up(&mddev->recovery_wait); |
| 3991 | return len; |
| 3992 | } |
| 3993 | |
| 3994 | static struct md_sysfs_entry md_max_sync = |
| 3995 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3996 | |
| 3997 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3998 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3999 | { |
| 4000 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4001 | } |
| 4002 | |
| 4003 | static ssize_t |
| 4004 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 4005 | { |
| 4006 | char *e; |
| 4007 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4008 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4009 | if (mddev->pers == NULL || |
| 4010 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4011 | return -EINVAL; |
| 4012 | if (buf == e || (*e && *e != '\n')) |
| 4013 | return -EINVAL; |
| 4014 | if (new >= mddev->suspend_hi || |
| 4015 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 4016 | mddev->suspend_lo = new; |
| 4017 | mddev->pers->quiesce(mddev, 2); |
| 4018 | return len; |
| 4019 | } else |
| 4020 | return -EINVAL; |
| 4021 | } |
| 4022 | static struct md_sysfs_entry md_suspend_lo = |
| 4023 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4024 | |
| 4025 | |
| 4026 | static ssize_t |
| 4027 | suspend_hi_show(mddev_t *mddev, char *page) |
| 4028 | { |
| 4029 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4030 | } |
| 4031 | |
| 4032 | static ssize_t |
| 4033 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 4034 | { |
| 4035 | char *e; |
| 4036 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4037 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 4038 | if (mddev->pers == NULL || |
| 4039 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4040 | return -EINVAL; |
| 4041 | if (buf == e || (*e && *e != '\n')) |
| 4042 | return -EINVAL; |
| 4043 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 4044 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 4045 | mddev->suspend_hi = new; |
| 4046 | mddev->pers->quiesce(mddev, 1); |
| 4047 | mddev->pers->quiesce(mddev, 0); |
| 4048 | return len; |
| 4049 | } else |
| 4050 | return -EINVAL; |
| 4051 | } |
| 4052 | static struct md_sysfs_entry md_suspend_hi = |
| 4053 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4054 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4055 | static ssize_t |
| 4056 | reshape_position_show(mddev_t *mddev, char *page) |
| 4057 | { |
| 4058 | if (mddev->reshape_position != MaxSector) |
| 4059 | return sprintf(page, "%llu\n", |
| 4060 | (unsigned long long)mddev->reshape_position); |
| 4061 | strcpy(page, "none\n"); |
| 4062 | return 5; |
| 4063 | } |
| 4064 | |
| 4065 | static ssize_t |
| 4066 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4067 | { |
| 4068 | char *e; |
| 4069 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4070 | if (mddev->pers) |
| 4071 | return -EBUSY; |
| 4072 | if (buf == e || (*e && *e != '\n')) |
| 4073 | return -EINVAL; |
| 4074 | mddev->reshape_position = new; |
| 4075 | mddev->delta_disks = 0; |
| 4076 | mddev->new_level = mddev->level; |
| 4077 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4078 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4079 | return len; |
| 4080 | } |
| 4081 | |
| 4082 | static struct md_sysfs_entry md_reshape_position = |
| 4083 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4084 | reshape_position_store); |
| 4085 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4086 | static ssize_t |
| 4087 | array_size_show(mddev_t *mddev, char *page) |
| 4088 | { |
| 4089 | if (mddev->external_size) |
| 4090 | return sprintf(page, "%llu\n", |
| 4091 | (unsigned long long)mddev->array_sectors/2); |
| 4092 | else |
| 4093 | return sprintf(page, "default\n"); |
| 4094 | } |
| 4095 | |
| 4096 | static ssize_t |
| 4097 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4098 | { |
| 4099 | sector_t sectors; |
| 4100 | |
| 4101 | if (strncmp(buf, "default", 7) == 0) { |
| 4102 | if (mddev->pers) |
| 4103 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4104 | else |
| 4105 | sectors = mddev->array_sectors; |
| 4106 | |
| 4107 | mddev->external_size = 0; |
| 4108 | } else { |
| 4109 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4110 | return -EINVAL; |
| 4111 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4112 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4113 | |
| 4114 | mddev->external_size = 1; |
| 4115 | } |
| 4116 | |
| 4117 | mddev->array_sectors = sectors; |
| 4118 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4119 | if (mddev->pers) |
| 4120 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4121 | |
| 4122 | return len; |
| 4123 | } |
| 4124 | |
| 4125 | static struct md_sysfs_entry md_array_size = |
| 4126 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4127 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4128 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4129 | static struct attribute *md_default_attrs[] = { |
| 4130 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4131 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4132 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4133 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4134 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4135 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4136 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4137 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4138 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4139 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4140 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4141 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4142 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4143 | NULL, |
| 4144 | }; |
| 4145 | |
| 4146 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4147 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4148 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4149 | &md_sync_min.attr, |
| 4150 | &md_sync_max.attr, |
| 4151 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4152 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4153 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4154 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4155 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4156 | &md_suspend_lo.attr, |
| 4157 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4158 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4159 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4160 | NULL, |
| 4161 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4162 | static struct attribute_group md_redundancy_group = { |
| 4163 | .name = NULL, |
| 4164 | .attrs = md_redundancy_attrs, |
| 4165 | }; |
| 4166 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4167 | |
| 4168 | static ssize_t |
| 4169 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4170 | { |
| 4171 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4172 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4173 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4174 | |
| 4175 | if (!entry->show) |
| 4176 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4177 | rv = mddev_lock(mddev); |
| 4178 | if (!rv) { |
| 4179 | rv = entry->show(mddev, page); |
| 4180 | mddev_unlock(mddev); |
| 4181 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4182 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | static ssize_t |
| 4186 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4187 | const char *page, size_t length) |
| 4188 | { |
| 4189 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4190 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4191 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4192 | |
| 4193 | if (!entry->store) |
| 4194 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4195 | if (!capable(CAP_SYS_ADMIN)) |
| 4196 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4197 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4198 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4199 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4200 | if (!rv) { |
| 4201 | rv = entry->store(mddev, page, length); |
| 4202 | mddev_unlock(mddev); |
| 4203 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4204 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | static void md_free(struct kobject *ko) |
| 4208 | { |
| 4209 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4210 | |
| 4211 | if (mddev->sysfs_state) |
| 4212 | sysfs_put(mddev->sysfs_state); |
| 4213 | |
| 4214 | if (mddev->gendisk) { |
| 4215 | del_gendisk(mddev->gendisk); |
| 4216 | put_disk(mddev->gendisk); |
| 4217 | } |
| 4218 | if (mddev->queue) |
| 4219 | blk_cleanup_queue(mddev->queue); |
| 4220 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4221 | kfree(mddev); |
| 4222 | } |
| 4223 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4224 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4225 | .show = md_attr_show, |
| 4226 | .store = md_attr_store, |
| 4227 | }; |
| 4228 | static struct kobj_type md_ktype = { |
| 4229 | .release = md_free, |
| 4230 | .sysfs_ops = &md_sysfs_ops, |
| 4231 | .default_attrs = md_default_attrs, |
| 4232 | }; |
| 4233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | int mdp_major = 0; |
| 4235 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4236 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4237 | { |
| 4238 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4239 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4240 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4241 | kobject_del(&mddev->kobj); |
| 4242 | kobject_put(&mddev->kobj); |
| 4243 | } |
| 4244 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4245 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4247 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | mddev_t *mddev = mddev_find(dev); |
| 4249 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4250 | int partitioned; |
| 4251 | int shift; |
| 4252 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4253 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | |
| 4255 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4256 | return -ENODEV; |
| 4257 | |
| 4258 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4259 | shift = partitioned ? MdpMinorShift : 0; |
| 4260 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4261 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4262 | /* wait for any previous instance if this device |
| 4263 | * to be completed removed (mddev_delayed_delete). |
| 4264 | */ |
| 4265 | flush_scheduled_work(); |
| 4266 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4267 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4268 | error = -EEXIST; |
| 4269 | if (mddev->gendisk) |
| 4270 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4271 | |
| 4272 | if (name) { |
| 4273 | /* Need to ensure that 'name' is not a duplicate. |
| 4274 | */ |
| 4275 | mddev_t *mddev2; |
| 4276 | spin_lock(&all_mddevs_lock); |
| 4277 | |
| 4278 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4279 | if (mddev2->gendisk && |
| 4280 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4281 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4282 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4283 | } |
| 4284 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4286 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4287 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4288 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4289 | if (!mddev->queue) |
| 4290 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4291 | mddev->queue->queuedata = mddev; |
| 4292 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4293 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4294 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4295 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4296 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | disk = alloc_disk(1 << shift); |
| 4299 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4300 | blk_cleanup_queue(mddev->queue); |
| 4301 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4302 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4304 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4306 | if (name) |
| 4307 | strcpy(disk->disk_name, name); |
| 4308 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4310 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | disk->fops = &md_fops; |
| 4313 | disk->private_data = mddev; |
| 4314 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4315 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4316 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4317 | * remove it now. |
| 4318 | */ |
| 4319 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | add_disk(disk); |
| 4321 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4322 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4323 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4324 | if (error) { |
| 4325 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4326 | * __must_check, we must do something with the result |
| 4327 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4328 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4329 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4330 | error = 0; |
| 4331 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4332 | if (mddev->kobj.sd && |
| 4333 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4334 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4335 | abort: |
| 4336 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4337 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4338 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4339 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4340 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4341 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4342 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4343 | } |
| 4344 | |
| 4345 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4346 | { |
| 4347 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | return NULL; |
| 4349 | } |
| 4350 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4351 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4352 | { |
| 4353 | /* val must be "md_*" where * is not all digits. |
| 4354 | * We allocate an array with a large free minor number, and |
| 4355 | * set the name to val. val must not already be an active name. |
| 4356 | */ |
| 4357 | int len = strlen(val); |
| 4358 | char buf[DISK_NAME_LEN]; |
| 4359 | |
| 4360 | while (len && val[len-1] == '\n') |
| 4361 | len--; |
| 4362 | if (len >= DISK_NAME_LEN) |
| 4363 | return -E2BIG; |
| 4364 | strlcpy(buf, val, len+1); |
| 4365 | if (strncmp(buf, "md_", 3) != 0) |
| 4366 | return -EINVAL; |
| 4367 | return md_alloc(0, buf); |
| 4368 | } |
| 4369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | static void md_safemode_timeout(unsigned long data) |
| 4371 | { |
| 4372 | mddev_t *mddev = (mddev_t *) data; |
| 4373 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4374 | if (!atomic_read(&mddev->writes_pending)) { |
| 4375 | mddev->safemode = 1; |
| 4376 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4377 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | md_wakeup_thread(mddev->thread); |
| 4380 | } |
| 4381 | |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4382 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4384 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4385 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4386 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4388 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4390 | if (list_empty(&mddev->disks)) |
| 4391 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4393 | |
| 4394 | if (mddev->pers) |
| 4395 | return -EBUSY; |
| 4396 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4397 | /* These two calls synchronise us with the |
| 4398 | * sysfs_remove_group calls in mddev_unlock, |
| 4399 | * so they must have completed. |
| 4400 | */ |
| 4401 | mutex_lock(&mddev->open_mutex); |
| 4402 | mutex_unlock(&mddev->open_mutex); |
| 4403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4404 | /* |
| 4405 | * Analyze all RAID superblock(s) |
| 4406 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4407 | if (!mddev->raid_disks) { |
| 4408 | if (!mddev->persistent) |
| 4409 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4410 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4413 | if (mddev->level != LEVEL_NONE) |
| 4414 | request_module("md-level-%d", mddev->level); |
| 4415 | else if (mddev->clevel[0]) |
| 4416 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | |
| 4418 | /* |
| 4419 | * Drop all container device buffers, from now on |
| 4420 | * the only valid external interface is through the md |
| 4421 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4423 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4424 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | continue; |
| 4426 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4427 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4428 | |
| 4429 | /* perform some consistency tests on the device. |
| 4430 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4431 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4432 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4433 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4434 | if (mddev->dev_sectors && |
| 4435 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4436 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4437 | printk("md: %s: data overlaps metadata\n", |
| 4438 | mdname(mddev)); |
| 4439 | return -EINVAL; |
| 4440 | } |
| 4441 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4442 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4443 | > rdev->data_offset) { |
| 4444 | printk("md: %s: metadata overlaps data\n", |
| 4445 | mdname(mddev)); |
| 4446 | return -EINVAL; |
| 4447 | } |
| 4448 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4449 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4450 | } |
| 4451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4452 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4453 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4454 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4456 | if (mddev->level != LEVEL_NONE) |
| 4457 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4458 | mddev->level); |
| 4459 | else |
| 4460 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4461 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | return -EINVAL; |
| 4463 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4464 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4465 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4466 | if (mddev->level != pers->level) { |
| 4467 | mddev->level = pers->level; |
| 4468 | mddev->new_level = pers->level; |
| 4469 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4470 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4472 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4473 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4474 | /* This personality cannot handle reshaping... */ |
| 4475 | mddev->pers = NULL; |
| 4476 | module_put(pers->owner); |
| 4477 | return -EINVAL; |
| 4478 | } |
| 4479 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4480 | if (pers->sync_request) { |
| 4481 | /* Warn if this is a potentially silly |
| 4482 | * configuration. |
| 4483 | */ |
| 4484 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4485 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4486 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4487 | |
| 4488 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4489 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4490 | if (rdev < rdev2 && |
| 4491 | rdev->bdev->bd_contains == |
| 4492 | rdev2->bdev->bd_contains) { |
| 4493 | printk(KERN_WARNING |
| 4494 | "%s: WARNING: %s appears to be" |
| 4495 | " on the same physical disk as" |
| 4496 | " %s.\n", |
| 4497 | mdname(mddev), |
| 4498 | bdevname(rdev->bdev,b), |
| 4499 | bdevname(rdev2->bdev,b2)); |
| 4500 | warned = 1; |
| 4501 | } |
| 4502 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4503 | |
NeilBrown | 7dd5e7c | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4504 | if (warned) |
| 4505 | printk(KERN_WARNING |
| 4506 | "True protection against single-disk" |
| 4507 | " failure might be compromised.\n"); |
| 4508 | } |
| 4509 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4510 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4511 | /* may be over-ridden by personality */ |
| 4512 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4513 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4514 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4515 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4517 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4518 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4519 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4520 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4521 | if (err) |
| 4522 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4523 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4524 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4525 | " but 'external_size' not in effect?\n", __func__); |
| 4526 | printk(KERN_ERR |
| 4527 | "md: invalid array_size %llu > default size %llu\n", |
| 4528 | (unsigned long long)mddev->array_sectors / 2, |
| 4529 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4530 | err = -EINVAL; |
| 4531 | mddev->pers->stop(mddev); |
| 4532 | } |
| 4533 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4534 | err = bitmap_create(mddev); |
| 4535 | if (err) { |
| 4536 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4537 | mdname(mddev), err); |
| 4538 | mddev->pers->stop(mddev); |
| 4539 | } |
| 4540 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4542 | module_put(mddev->pers->owner); |
| 4543 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4544 | bitmap_destroy(mddev); |
| 4545 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4546 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4547 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4548 | if (mddev->kobj.sd && |
| 4549 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4550 | printk(KERN_WARNING |
| 4551 | "md: cannot register extra attributes for %s\n", |
| 4552 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4553 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4554 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4555 | mddev->ro = 0; |
| 4556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4558 | atomic_set(&mddev->max_corr_read_errors, |
| 4559 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | mddev->safemode = 0; |
| 4561 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4562 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4563 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4564 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4565 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4566 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4567 | if (rdev->raid_disk >= 0) { |
| 4568 | char nm[20]; |
| 4569 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4570 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4571 | /* failure here is OK */; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4572 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4573 | |
| 4574 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4575 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4576 | if (mddev->flags) |
| 4577 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4579 | md_wakeup_thread(mddev->thread); |
| 4580 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4581 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4582 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4583 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4584 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4585 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4586 | return 0; |
| 4587 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4588 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4590 | static int do_md_run(mddev_t *mddev) |
| 4591 | { |
| 4592 | int err; |
| 4593 | |
| 4594 | err = md_run(mddev); |
| 4595 | if (err) |
| 4596 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame^] | 4597 | err = bitmap_load(mddev); |
| 4598 | if (err) { |
| 4599 | bitmap_destroy(mddev); |
| 4600 | goto out; |
| 4601 | } |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4602 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4603 | revalidate_disk(mddev->gendisk); |
| 4604 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4605 | out: |
| 4606 | return err; |
| 4607 | } |
| 4608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | static int restart_array(mddev_t *mddev) |
| 4610 | { |
| 4611 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4613 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4615 | return -ENXIO; |
| 4616 | if (!mddev->pers) |
| 4617 | return -EINVAL; |
| 4618 | if (!mddev->ro) |
| 4619 | return -EBUSY; |
| 4620 | mddev->safemode = 0; |
| 4621 | mddev->ro = 0; |
| 4622 | set_disk_ro(disk, 0); |
| 4623 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4624 | mdname(mddev)); |
| 4625 | /* Kick recovery or resync if necessary */ |
| 4626 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4627 | md_wakeup_thread(mddev->thread); |
| 4628 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4629 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4630 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4631 | } |
| 4632 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4633 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4634 | * to the file ourselves */ |
| 4635 | static int deny_bitmap_write_access(struct file * file) |
| 4636 | { |
| 4637 | struct inode *inode = file->f_mapping->host; |
| 4638 | |
| 4639 | spin_lock(&inode->i_lock); |
| 4640 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4641 | spin_unlock(&inode->i_lock); |
| 4642 | return -ETXTBSY; |
| 4643 | } |
| 4644 | atomic_set(&inode->i_writecount, -1); |
| 4645 | spin_unlock(&inode->i_lock); |
| 4646 | |
| 4647 | return 0; |
| 4648 | } |
| 4649 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4650 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4651 | { |
| 4652 | struct inode *inode = file->f_mapping->host; |
| 4653 | |
| 4654 | spin_lock(&inode->i_lock); |
| 4655 | atomic_set(&inode->i_writecount, 1); |
| 4656 | spin_unlock(&inode->i_lock); |
| 4657 | } |
| 4658 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4659 | static void md_clean(mddev_t *mddev) |
| 4660 | { |
| 4661 | mddev->array_sectors = 0; |
| 4662 | mddev->external_size = 0; |
| 4663 | mddev->dev_sectors = 0; |
| 4664 | mddev->raid_disks = 0; |
| 4665 | mddev->recovery_cp = 0; |
| 4666 | mddev->resync_min = 0; |
| 4667 | mddev->resync_max = MaxSector; |
| 4668 | mddev->reshape_position = MaxSector; |
| 4669 | mddev->external = 0; |
| 4670 | mddev->persistent = 0; |
| 4671 | mddev->level = LEVEL_NONE; |
| 4672 | mddev->clevel[0] = 0; |
| 4673 | mddev->flags = 0; |
| 4674 | mddev->ro = 0; |
| 4675 | mddev->metadata_type[0] = 0; |
| 4676 | mddev->chunk_sectors = 0; |
| 4677 | mddev->ctime = mddev->utime = 0; |
| 4678 | mddev->layout = 0; |
| 4679 | mddev->max_disks = 0; |
| 4680 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4681 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4682 | mddev->delta_disks = 0; |
| 4683 | mddev->new_level = LEVEL_NONE; |
| 4684 | mddev->new_layout = 0; |
| 4685 | mddev->new_chunk_sectors = 0; |
| 4686 | mddev->curr_resync = 0; |
| 4687 | mddev->resync_mismatches = 0; |
| 4688 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4689 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4690 | mddev->recovery = 0; |
| 4691 | mddev->in_sync = 0; |
| 4692 | mddev->degraded = 0; |
| 4693 | mddev->barriers_work = 0; |
| 4694 | mddev->safemode = 0; |
| 4695 | mddev->bitmap_info.offset = 0; |
| 4696 | mddev->bitmap_info.default_offset = 0; |
| 4697 | mddev->bitmap_info.chunksize = 0; |
| 4698 | mddev->bitmap_info.daemon_sleep = 0; |
| 4699 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 4700 | mddev->plug = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4701 | } |
| 4702 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4703 | void md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4704 | { |
| 4705 | if (mddev->sync_thread) { |
| 4706 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4707 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4708 | md_unregister_thread(mddev->sync_thread); |
| 4709 | mddev->sync_thread = NULL; |
| 4710 | } |
| 4711 | |
| 4712 | del_timer_sync(&mddev->safemode_timer); |
| 4713 | |
| 4714 | bitmap_flush(mddev); |
| 4715 | md_super_wait(mddev); |
| 4716 | |
| 4717 | if (!mddev->in_sync || mddev->flags) { |
| 4718 | /* mark array as shutdown cleanly */ |
| 4719 | mddev->in_sync = 1; |
| 4720 | md_update_sb(mddev, 1); |
| 4721 | } |
| 4722 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4723 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4724 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4725 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4726 | { |
| 4727 | mddev->pers->stop(mddev); |
| 4728 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4729 | mddev->to_remove = &md_redundancy_group; |
| 4730 | module_put(mddev->pers->owner); |
| 4731 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4732 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4733 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4734 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4735 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4736 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4737 | { |
| 4738 | int err = 0; |
| 4739 | mutex_lock(&mddev->open_mutex); |
| 4740 | if (atomic_read(&mddev->openers) > is_open) { |
| 4741 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4742 | err = -EBUSY; |
| 4743 | goto out; |
| 4744 | } |
| 4745 | if (mddev->pers) { |
| 4746 | md_stop_writes(mddev); |
| 4747 | |
| 4748 | err = -ENXIO; |
| 4749 | if (mddev->ro==1) |
| 4750 | goto out; |
| 4751 | mddev->ro = 1; |
| 4752 | set_disk_ro(mddev->gendisk, 1); |
| 4753 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4754 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4755 | err = 0; |
| 4756 | } |
| 4757 | out: |
| 4758 | mutex_unlock(&mddev->open_mutex); |
| 4759 | return err; |
| 4760 | } |
| 4761 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4762 | /* mode: |
| 4763 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4764 | * 2 - stop but do not disassemble array |
| 4765 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4766 | static int do_md_stop(mddev_t * mddev, int mode, int is_open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | { |
| 4768 | int err = 0; |
| 4769 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4770 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4772 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4773 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4774 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4775 | err = -EBUSY; |
| 4776 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4777 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4778 | if (mddev->ro) |
| 4779 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4780 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4781 | md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4782 | md_stop(mddev); |
| 4783 | mddev->queue->merge_bvec_fn = NULL; |
| 4784 | mddev->queue->unplug_fn = NULL; |
| 4785 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4786 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4787 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4788 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4789 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4790 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4791 | if (rdev->raid_disk >= 0) { |
| 4792 | char nm[20]; |
| 4793 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4794 | sysfs_remove_link(&mddev->kobj, nm); |
| 4795 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4796 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4797 | set_capacity(disk, 0); |
| 4798 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4799 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4800 | if (mddev->ro) |
| 4801 | mddev->ro = 0; |
| 4802 | |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4803 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4805 | mutex_unlock(&mddev->open_mutex); |
| 4806 | if (err) |
| 4807 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | /* |
| 4809 | * Free resources if final stop |
| 4810 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4811 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4814 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4815 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4816 | if (mddev->bitmap_info.file) { |
| 4817 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4818 | fput(mddev->bitmap_info.file); |
| 4819 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4820 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4821 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | export_array(mddev); |
| 4824 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4825 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4826 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4827 | if (mddev->hold_active == UNTIL_STOP) |
| 4828 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4829 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4830 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4832 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4833 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4834 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | return err; |
| 4836 | } |
| 4837 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4838 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | static void autorun_array(mddev_t *mddev) |
| 4840 | { |
| 4841 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | int err; |
| 4843 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4844 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | |
| 4847 | printk(KERN_INFO "md: running: "); |
| 4848 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4849 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | char b[BDEVNAME_SIZE]; |
| 4851 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4852 | } |
| 4853 | printk("\n"); |
| 4854 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4855 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4856 | if (err) { |
| 4857 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4858 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | } |
| 4860 | } |
| 4861 | |
| 4862 | /* |
| 4863 | * lets try to run arrays based on all disks that have arrived |
| 4864 | * until now. (those are in pending_raid_disks) |
| 4865 | * |
| 4866 | * the method: pick the first pending disk, collect all disks with |
| 4867 | * the same UUID, remove all from the pending list and put them into |
| 4868 | * the 'same_array' list. Then order this list based on superblock |
| 4869 | * update time (freshest comes first), kick out 'old' disks and |
| 4870 | * compare superblocks. If everything's fine then run it. |
| 4871 | * |
| 4872 | * If "unit" is allocated, then bump its reference count |
| 4873 | */ |
| 4874 | static void autorun_devices(int part) |
| 4875 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4876 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | mddev_t *mddev; |
| 4878 | char b[BDEVNAME_SIZE]; |
| 4879 | |
| 4880 | printk(KERN_INFO "md: autorun ...\n"); |
| 4881 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4882 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4883 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4884 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4885 | rdev0 = list_entry(pending_raid_disks.next, |
| 4886 | mdk_rdev_t, same_set); |
| 4887 | |
| 4888 | printk(KERN_INFO "md: considering %s ...\n", |
| 4889 | bdevname(rdev0->bdev,b)); |
| 4890 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4891 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4892 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4893 | printk(KERN_INFO "md: adding %s ...\n", |
| 4894 | bdevname(rdev->bdev,b)); |
| 4895 | list_move(&rdev->same_set, &candidates); |
| 4896 | } |
| 4897 | /* |
| 4898 | * now we have a set of devices, with all of them having |
| 4899 | * mostly sane superblocks. It's time to allocate the |
| 4900 | * mddev. |
| 4901 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4902 | if (part) { |
| 4903 | dev = MKDEV(mdp_major, |
| 4904 | rdev0->preferred_minor << MdpMinorShift); |
| 4905 | unit = MINOR(dev) >> MdpMinorShift; |
| 4906 | } else { |
| 4907 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4908 | unit = MINOR(dev); |
| 4909 | } |
| 4910 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4912 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4913 | break; |
| 4914 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | |
| 4916 | md_probe(dev, NULL, NULL); |
| 4917 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4918 | if (!mddev || !mddev->gendisk) { |
| 4919 | if (mddev) |
| 4920 | mddev_put(mddev); |
| 4921 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4922 | "md: cannot allocate memory for md drive.\n"); |
| 4923 | break; |
| 4924 | } |
| 4925 | if (mddev_lock(mddev)) |
| 4926 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4927 | mdname(mddev)); |
| 4928 | else if (mddev->raid_disks || mddev->major_version |
| 4929 | || !list_empty(&mddev->disks)) { |
| 4930 | printk(KERN_WARNING |
| 4931 | "md: %s already running, cannot run %s\n", |
| 4932 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4933 | mddev_unlock(mddev); |
| 4934 | } else { |
| 4935 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4936 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4937 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | list_del_init(&rdev->same_set); |
| 4939 | if (bind_rdev_to_array(rdev, mddev)) |
| 4940 | export_rdev(rdev); |
| 4941 | } |
| 4942 | autorun_array(mddev); |
| 4943 | mddev_unlock(mddev); |
| 4944 | } |
| 4945 | /* on success, candidates will be empty, on error |
| 4946 | * it won't... |
| 4947 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4948 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4949 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | mddev_put(mddev); |
| 4953 | } |
| 4954 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4955 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4956 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4958 | static int get_version(void __user * arg) |
| 4959 | { |
| 4960 | mdu_version_t ver; |
| 4961 | |
| 4962 | ver.major = MD_MAJOR_VERSION; |
| 4963 | ver.minor = MD_MINOR_VERSION; |
| 4964 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4965 | |
| 4966 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4967 | return -EFAULT; |
| 4968 | |
| 4969 | return 0; |
| 4970 | } |
| 4971 | |
| 4972 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4973 | { |
| 4974 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4975 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4978 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4979 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4981 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | failed++; |
| 4983 | else { |
| 4984 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4985 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4986 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4987 | else |
| 4988 | spare++; |
| 4989 | } |
| 4990 | } |
| 4991 | |
| 4992 | info.major_version = mddev->major_version; |
| 4993 | info.minor_version = mddev->minor_version; |
| 4994 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4995 | info.ctime = mddev->ctime; |
| 4996 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4997 | info.size = mddev->dev_sectors / 2; |
| 4998 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4999 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5000 | info.nr_disks = nr; |
| 5001 | info.raid_disks = mddev->raid_disks; |
| 5002 | info.md_minor = mddev->md_minor; |
| 5003 | info.not_persistent= !mddev->persistent; |
| 5004 | |
| 5005 | info.utime = mddev->utime; |
| 5006 | info.state = 0; |
| 5007 | if (mddev->in_sync) |
| 5008 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5009 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5010 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5011 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | info.working_disks = working; |
| 5013 | info.failed_disks = failed; |
| 5014 | info.spare_disks = spare; |
| 5015 | |
| 5016 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5017 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | |
| 5019 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5020 | return -EFAULT; |
| 5021 | |
| 5022 | return 0; |
| 5023 | } |
| 5024 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5025 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5026 | { |
| 5027 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 5028 | char *ptr, *buf = NULL; |
| 5029 | int err = -ENOMEM; |
| 5030 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5031 | if (md_allow_write(mddev)) |
| 5032 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 5033 | else |
| 5034 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 5035 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5036 | if (!file) |
| 5037 | goto out; |
| 5038 | |
| 5039 | /* bitmap disabled, zero the first byte and copy out */ |
| 5040 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 5041 | file->pathname[0] = '\0'; |
| 5042 | goto copy_out; |
| 5043 | } |
| 5044 | |
| 5045 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 5046 | if (!buf) |
| 5047 | goto out; |
| 5048 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 5049 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 5050 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5051 | goto out; |
| 5052 | |
| 5053 | strcpy(file->pathname, ptr); |
| 5054 | |
| 5055 | copy_out: |
| 5056 | err = 0; |
| 5057 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5058 | err = -EFAULT; |
| 5059 | out: |
| 5060 | kfree(buf); |
| 5061 | kfree(file); |
| 5062 | return err; |
| 5063 | } |
| 5064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5066 | { |
| 5067 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | mdk_rdev_t *rdev; |
| 5069 | |
| 5070 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5071 | return -EFAULT; |
| 5072 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5073 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | if (rdev) { |
| 5075 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5076 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5077 | info.raid_disk = rdev->raid_disk; |
| 5078 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5079 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5081 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5082 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5083 | info.state |= (1<<MD_DISK_SYNC); |
| 5084 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5085 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5086 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | } else { |
| 5088 | info.major = info.minor = 0; |
| 5089 | info.raid_disk = -1; |
| 5090 | info.state = (1<<MD_DISK_REMOVED); |
| 5091 | } |
| 5092 | |
| 5093 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5094 | return -EFAULT; |
| 5095 | |
| 5096 | return 0; |
| 5097 | } |
| 5098 | |
| 5099 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5100 | { |
| 5101 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5102 | mdk_rdev_t *rdev; |
| 5103 | dev_t dev = MKDEV(info->major,info->minor); |
| 5104 | |
| 5105 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5106 | return -EOVERFLOW; |
| 5107 | |
| 5108 | if (!mddev->raid_disks) { |
| 5109 | int err; |
| 5110 | /* expecting a device which has a superblock */ |
| 5111 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5112 | if (IS_ERR(rdev)) { |
| 5113 | printk(KERN_WARNING |
| 5114 | "md: md_import_device returned %ld\n", |
| 5115 | PTR_ERR(rdev)); |
| 5116 | return PTR_ERR(rdev); |
| 5117 | } |
| 5118 | if (!list_empty(&mddev->disks)) { |
| 5119 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5120 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5121 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5123 | if (err < 0) { |
| 5124 | printk(KERN_WARNING |
| 5125 | "md: %s has different UUID to %s\n", |
| 5126 | bdevname(rdev->bdev,b), |
| 5127 | bdevname(rdev0->bdev,b2)); |
| 5128 | export_rdev(rdev); |
| 5129 | return -EINVAL; |
| 5130 | } |
| 5131 | } |
| 5132 | err = bind_rdev_to_array(rdev, mddev); |
| 5133 | if (err) |
| 5134 | export_rdev(rdev); |
| 5135 | return err; |
| 5136 | } |
| 5137 | |
| 5138 | /* |
| 5139 | * add_new_disk can be used once the array is assembled |
| 5140 | * to add "hot spares". They must already have a superblock |
| 5141 | * written |
| 5142 | */ |
| 5143 | if (mddev->pers) { |
| 5144 | int err; |
| 5145 | if (!mddev->pers->hot_add_disk) { |
| 5146 | printk(KERN_WARNING |
| 5147 | "%s: personality does not support diskops!\n", |
| 5148 | mdname(mddev)); |
| 5149 | return -EINVAL; |
| 5150 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5151 | if (mddev->persistent) |
| 5152 | rdev = md_import_device(dev, mddev->major_version, |
| 5153 | mddev->minor_version); |
| 5154 | else |
| 5155 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5156 | if (IS_ERR(rdev)) { |
| 5157 | printk(KERN_WARNING |
| 5158 | "md: md_import_device returned %ld\n", |
| 5159 | PTR_ERR(rdev)); |
| 5160 | return PTR_ERR(rdev); |
| 5161 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5162 | /* set save_raid_disk if appropriate */ |
| 5163 | if (!mddev->persistent) { |
| 5164 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5165 | info->raid_disk < mddev->raid_disks) |
| 5166 | rdev->raid_disk = info->raid_disk; |
| 5167 | else |
| 5168 | rdev->raid_disk = -1; |
| 5169 | } else |
| 5170 | super_types[mddev->major_version]. |
| 5171 | validate_super(mddev, rdev); |
| 5172 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5173 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5174 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5175 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5176 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5177 | else |
| 5178 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | rdev->raid_disk = -1; |
| 5181 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5182 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5183 | /* If there is hot_add_disk but no hot_remove_disk |
| 5184 | * then added disks for geometry changes, |
| 5185 | * and should be added immediately. |
| 5186 | */ |
| 5187 | super_types[mddev->major_version]. |
| 5188 | validate_super(mddev, rdev); |
| 5189 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5190 | if (err) |
| 5191 | unbind_rdev_from_array(rdev); |
| 5192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | if (err) |
| 5194 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5195 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5196 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5197 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5198 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5199 | if (mddev->degraded) |
| 5200 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5201 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5202 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | return err; |
| 5204 | } |
| 5205 | |
| 5206 | /* otherwise, add_new_disk is only allowed |
| 5207 | * for major_version==0 superblocks |
| 5208 | */ |
| 5209 | if (mddev->major_version != 0) { |
| 5210 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5211 | mdname(mddev)); |
| 5212 | return -EINVAL; |
| 5213 | } |
| 5214 | |
| 5215 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5216 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5217 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5218 | if (IS_ERR(rdev)) { |
| 5219 | printk(KERN_WARNING |
| 5220 | "md: error, md_import_device() returned %ld\n", |
| 5221 | PTR_ERR(rdev)); |
| 5222 | return PTR_ERR(rdev); |
| 5223 | } |
| 5224 | rdev->desc_nr = info->number; |
| 5225 | if (info->raid_disk < mddev->raid_disks) |
| 5226 | rdev->raid_disk = info->raid_disk; |
| 5227 | else |
| 5228 | rdev->raid_disk = -1; |
| 5229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5231 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5232 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5234 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5235 | set_bit(WriteMostly, &rdev->flags); |
| 5236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | if (!mddev->persistent) { |
| 5238 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5239 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5241 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5242 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5244 | err = bind_rdev_to_array(rdev, mddev); |
| 5245 | if (err) { |
| 5246 | export_rdev(rdev); |
| 5247 | return err; |
| 5248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | return 0; |
| 5252 | } |
| 5253 | |
| 5254 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5255 | { |
| 5256 | char b[BDEVNAME_SIZE]; |
| 5257 | mdk_rdev_t *rdev; |
| 5258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | rdev = find_rdev(mddev, dev); |
| 5260 | if (!rdev) |
| 5261 | return -ENXIO; |
| 5262 | |
| 5263 | if (rdev->raid_disk >= 0) |
| 5264 | goto busy; |
| 5265 | |
| 5266 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5267 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5268 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | |
| 5270 | return 0; |
| 5271 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5272 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5274 | return -EBUSY; |
| 5275 | } |
| 5276 | |
| 5277 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5278 | { |
| 5279 | char b[BDEVNAME_SIZE]; |
| 5280 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | mdk_rdev_t *rdev; |
| 5282 | |
| 5283 | if (!mddev->pers) |
| 5284 | return -ENODEV; |
| 5285 | |
| 5286 | if (mddev->major_version != 0) { |
| 5287 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5288 | " version-0 superblocks.\n", |
| 5289 | mdname(mddev)); |
| 5290 | return -EINVAL; |
| 5291 | } |
| 5292 | if (!mddev->pers->hot_add_disk) { |
| 5293 | printk(KERN_WARNING |
| 5294 | "%s: personality does not support diskops!\n", |
| 5295 | mdname(mddev)); |
| 5296 | return -EINVAL; |
| 5297 | } |
| 5298 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5299 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | if (IS_ERR(rdev)) { |
| 5301 | printk(KERN_WARNING |
| 5302 | "md: error, md_import_device() returned %ld\n", |
| 5303 | PTR_ERR(rdev)); |
| 5304 | return -EINVAL; |
| 5305 | } |
| 5306 | |
| 5307 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5308 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5310 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5312 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5314 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5315 | printk(KERN_WARNING |
| 5316 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5317 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5318 | err = -EINVAL; |
| 5319 | goto abort_export; |
| 5320 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5321 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5323 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5324 | err = bind_rdev_to_array(rdev, mddev); |
| 5325 | if (err) |
| 5326 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5327 | |
| 5328 | /* |
| 5329 | * The rest should better be atomic, we can have disk failures |
| 5330 | * noticed in interrupt contexts ... |
| 5331 | */ |
| 5332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | rdev->raid_disk = -1; |
| 5334 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5335 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | |
| 5337 | /* |
| 5338 | * Kick recovery, maybe this spare has to be added to the |
| 5339 | * array immediately. |
| 5340 | */ |
| 5341 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5342 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5343 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5344 | return 0; |
| 5345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | abort_export: |
| 5347 | export_rdev(rdev); |
| 5348 | return err; |
| 5349 | } |
| 5350 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5351 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5352 | { |
| 5353 | int err; |
| 5354 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5355 | if (mddev->pers) { |
| 5356 | if (!mddev->pers->quiesce) |
| 5357 | return -EBUSY; |
| 5358 | if (mddev->recovery || mddev->sync_thread) |
| 5359 | return -EBUSY; |
| 5360 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5361 | } |
| 5362 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5363 | |
| 5364 | if (fd >= 0) { |
| 5365 | if (mddev->bitmap) |
| 5366 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5367 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5368 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5369 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5370 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5371 | mdname(mddev)); |
| 5372 | return -EBADF; |
| 5373 | } |
| 5374 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5375 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5376 | if (err) { |
| 5377 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5378 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5379 | fput(mddev->bitmap_info.file); |
| 5380 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5381 | return err; |
| 5382 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5383 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5384 | } else if (mddev->bitmap == NULL) |
| 5385 | return -ENOENT; /* cannot remove what isn't there */ |
| 5386 | err = 0; |
| 5387 | if (mddev->pers) { |
| 5388 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame^] | 5389 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5390 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame^] | 5391 | if (!err) |
| 5392 | err = bitmap_load(mddev); |
| 5393 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5394 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5395 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5396 | fd = -1; /* make sure to put the file */ |
| 5397 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5398 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5399 | } |
| 5400 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5401 | if (mddev->bitmap_info.file) { |
| 5402 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5403 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5404 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5405 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5406 | } |
| 5407 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5408 | return err; |
| 5409 | } |
| 5410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | /* |
| 5412 | * set_array_info is used two different ways |
| 5413 | * The original usage is when creating a new array. |
| 5414 | * In this usage, raid_disks is > 0 and it together with |
| 5415 | * level, size, not_persistent,layout,chunksize determine the |
| 5416 | * shape of the array. |
| 5417 | * This will always create an array with a type-0.90.0 superblock. |
| 5418 | * The newer usage is when assembling an array. |
| 5419 | * In this case raid_disks will be 0, and the major_version field is |
| 5420 | * use to determine which style super-blocks are to be found on the devices. |
| 5421 | * The minor and patch _version numbers are also kept incase the |
| 5422 | * super_block handler wishes to interpret them. |
| 5423 | */ |
| 5424 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5425 | { |
| 5426 | |
| 5427 | if (info->raid_disks == 0) { |
| 5428 | /* just setting version number for superblock loading */ |
| 5429 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5430 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | super_types[info->major_version].name == NULL) { |
| 5432 | /* maybe try to auto-load a module? */ |
| 5433 | printk(KERN_INFO |
| 5434 | "md: superblock version %d not known\n", |
| 5435 | info->major_version); |
| 5436 | return -EINVAL; |
| 5437 | } |
| 5438 | mddev->major_version = info->major_version; |
| 5439 | mddev->minor_version = info->minor_version; |
| 5440 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5441 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5442 | /* ensure mddev_put doesn't delete this now that there |
| 5443 | * is some minimal configuration. |
| 5444 | */ |
| 5445 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | return 0; |
| 5447 | } |
| 5448 | mddev->major_version = MD_MAJOR_VERSION; |
| 5449 | mddev->minor_version = MD_MINOR_VERSION; |
| 5450 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5451 | mddev->ctime = get_seconds(); |
| 5452 | |
| 5453 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5454 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5455 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | mddev->raid_disks = info->raid_disks; |
| 5457 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5458 | * openned |
| 5459 | */ |
| 5460 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5461 | mddev->recovery_cp = MaxSector; |
| 5462 | else |
| 5463 | mddev->recovery_cp = 0; |
| 5464 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5465 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5466 | |
| 5467 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5468 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5469 | |
| 5470 | mddev->max_disks = MD_SB_DISKS; |
| 5471 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5472 | if (mddev->persistent) |
| 5473 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5474 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5476 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5477 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5478 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5479 | mddev->reshape_position = MaxSector; |
| 5480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | /* |
| 5482 | * Generate a 128 bit UUID |
| 5483 | */ |
| 5484 | get_random_bytes(mddev->uuid, 16); |
| 5485 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5486 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5487 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5488 | mddev->new_layout = mddev->layout; |
| 5489 | mddev->delta_disks = 0; |
| 5490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | return 0; |
| 5492 | } |
| 5493 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5494 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5495 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5496 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5497 | |
| 5498 | if (mddev->external_size) |
| 5499 | return; |
| 5500 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5501 | mddev->array_sectors = array_sectors; |
| 5502 | } |
| 5503 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5504 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5505 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5506 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5507 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5508 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5509 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5510 | |
| 5511 | if (mddev->pers->resize == NULL) |
| 5512 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5513 | /* The "num_sectors" is the number of sectors of each device that |
| 5514 | * is used. This can only make sense for arrays with redundancy. |
| 5515 | * linear and raid0 always use whatever space is available. We can only |
| 5516 | * consider changing this number if no resync or reconstruction is |
| 5517 | * 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] | 5518 | * 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] | 5519 | * of each device. If num_sectors is zero, we find the largest size |
| 5520 | * that fits. |
| 5521 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5522 | */ |
| 5523 | if (mddev->sync_thread) |
| 5524 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5525 | if (mddev->bitmap) |
| 5526 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5527 | * grow, and re-add. |
| 5528 | */ |
| 5529 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5530 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5531 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5532 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5533 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5534 | num_sectors = avail; |
| 5535 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5536 | return -ENOSPC; |
| 5537 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5538 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5539 | if (!rv) |
| 5540 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5541 | return rv; |
| 5542 | } |
| 5543 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5544 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5545 | { |
| 5546 | int rv; |
| 5547 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5548 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5549 | return -EINVAL; |
| 5550 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5551 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5552 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5553 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5554 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5555 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5556 | |
| 5557 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5558 | return rv; |
| 5559 | } |
| 5560 | |
| 5561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5562 | /* |
| 5563 | * update_array_info is used to change the configuration of an |
| 5564 | * on-line array. |
| 5565 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5566 | * fields in the info are checked against the array. |
| 5567 | * Any differences that cannot be handled will cause an error. |
| 5568 | * Normally, only one change can be managed at a time. |
| 5569 | */ |
| 5570 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5571 | { |
| 5572 | int rv = 0; |
| 5573 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5574 | int state = 0; |
| 5575 | |
| 5576 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5577 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5578 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5579 | |
| 5580 | if (mddev->major_version != info->major_version || |
| 5581 | mddev->minor_version != info->minor_version || |
| 5582 | /* mddev->patch_version != info->patch_version || */ |
| 5583 | mddev->ctime != info->ctime || |
| 5584 | mddev->level != info->level || |
| 5585 | /* mddev->layout != info->layout || */ |
| 5586 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5587 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5588 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5589 | ((state^info->state) & 0xfffffe00) |
| 5590 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | return -EINVAL; |
| 5592 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5593 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5594 | cnt++; |
| 5595 | if (mddev->raid_disks != info->raid_disks) |
| 5596 | cnt++; |
| 5597 | if (mddev->layout != info->layout) |
| 5598 | cnt++; |
| 5599 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5600 | cnt++; |
| 5601 | if (cnt == 0) |
| 5602 | return 0; |
| 5603 | if (cnt > 1) |
| 5604 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | |
| 5606 | if (mddev->layout != info->layout) { |
| 5607 | /* Change layout |
| 5608 | * we don't need to do anything at the md level, the |
| 5609 | * personality will take care of it all. |
| 5610 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5611 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5612 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5613 | else { |
| 5614 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5615 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5616 | if (rv) |
| 5617 | mddev->new_layout = mddev->layout; |
| 5618 | return rv; |
| 5619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5620 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5621 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5622 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | |
NeilBrown | da943b9 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5624 | if (mddev->raid_disks != info->raid_disks) |
| 5625 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5627 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5628 | if (mddev->pers->quiesce == NULL) |
| 5629 | return -EINVAL; |
| 5630 | if (mddev->recovery || mddev->sync_thread) |
| 5631 | return -EBUSY; |
| 5632 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5633 | /* add the bitmap */ |
| 5634 | if (mddev->bitmap) |
| 5635 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5636 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5637 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5638 | mddev->bitmap_info.offset = |
| 5639 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5640 | mddev->pers->quiesce(mddev, 1); |
| 5641 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame^] | 5642 | if (!rv) |
| 5643 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5644 | if (rv) |
| 5645 | bitmap_destroy(mddev); |
| 5646 | mddev->pers->quiesce(mddev, 0); |
| 5647 | } else { |
| 5648 | /* remove the bitmap */ |
| 5649 | if (!mddev->bitmap) |
| 5650 | return -ENOENT; |
| 5651 | if (mddev->bitmap->file) |
| 5652 | return -EINVAL; |
| 5653 | mddev->pers->quiesce(mddev, 1); |
| 5654 | bitmap_destroy(mddev); |
| 5655 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5656 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5657 | } |
| 5658 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5659 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5660 | return rv; |
| 5661 | } |
| 5662 | |
| 5663 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5664 | { |
| 5665 | mdk_rdev_t *rdev; |
| 5666 | |
| 5667 | if (mddev->pers == NULL) |
| 5668 | return -ENODEV; |
| 5669 | |
| 5670 | rdev = find_rdev(mddev, dev); |
| 5671 | if (!rdev) |
| 5672 | return -ENODEV; |
| 5673 | |
| 5674 | md_error(mddev, rdev); |
| 5675 | return 0; |
| 5676 | } |
| 5677 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5678 | /* |
| 5679 | * We have a problem here : there is no easy way to give a CHS |
| 5680 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5681 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5682 | * dosfs just mad... ;-) |
| 5683 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5684 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5685 | { |
| 5686 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5687 | |
| 5688 | geo->heads = 2; |
| 5689 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5690 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5691 | return 0; |
| 5692 | } |
| 5693 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5694 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | unsigned int cmd, unsigned long arg) |
| 5696 | { |
| 5697 | int err = 0; |
| 5698 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5700 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | |
| 5702 | if (!capable(CAP_SYS_ADMIN)) |
| 5703 | return -EACCES; |
| 5704 | |
| 5705 | /* |
| 5706 | * Commands dealing with the RAID driver but not any |
| 5707 | * particular array: |
| 5708 | */ |
| 5709 | switch (cmd) |
| 5710 | { |
| 5711 | case RAID_VERSION: |
| 5712 | err = get_version(argp); |
| 5713 | goto done; |
| 5714 | |
| 5715 | case PRINT_RAID_DEBUG: |
| 5716 | err = 0; |
| 5717 | md_print_devices(); |
| 5718 | goto done; |
| 5719 | |
| 5720 | #ifndef MODULE |
| 5721 | case RAID_AUTORUN: |
| 5722 | err = 0; |
| 5723 | autostart_arrays(arg); |
| 5724 | goto done; |
| 5725 | #endif |
| 5726 | default:; |
| 5727 | } |
| 5728 | |
| 5729 | /* |
| 5730 | * Commands creating/starting a new array: |
| 5731 | */ |
| 5732 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5733 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5734 | |
| 5735 | if (!mddev) { |
| 5736 | BUG(); |
| 5737 | goto abort; |
| 5738 | } |
| 5739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | err = mddev_lock(mddev); |
| 5741 | if (err) { |
| 5742 | printk(KERN_INFO |
| 5743 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5744 | err, cmd); |
| 5745 | goto abort; |
| 5746 | } |
| 5747 | |
| 5748 | switch (cmd) |
| 5749 | { |
| 5750 | case SET_ARRAY_INFO: |
| 5751 | { |
| 5752 | mdu_array_info_t info; |
| 5753 | if (!arg) |
| 5754 | memset(&info, 0, sizeof(info)); |
| 5755 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5756 | err = -EFAULT; |
| 5757 | goto abort_unlock; |
| 5758 | } |
| 5759 | if (mddev->pers) { |
| 5760 | err = update_array_info(mddev, &info); |
| 5761 | if (err) { |
| 5762 | printk(KERN_WARNING "md: couldn't update" |
| 5763 | " array info. %d\n", err); |
| 5764 | goto abort_unlock; |
| 5765 | } |
| 5766 | goto done_unlock; |
| 5767 | } |
| 5768 | if (!list_empty(&mddev->disks)) { |
| 5769 | printk(KERN_WARNING |
| 5770 | "md: array %s already has disks!\n", |
| 5771 | mdname(mddev)); |
| 5772 | err = -EBUSY; |
| 5773 | goto abort_unlock; |
| 5774 | } |
| 5775 | if (mddev->raid_disks) { |
| 5776 | printk(KERN_WARNING |
| 5777 | "md: array %s already initialised!\n", |
| 5778 | mdname(mddev)); |
| 5779 | err = -EBUSY; |
| 5780 | goto abort_unlock; |
| 5781 | } |
| 5782 | err = set_array_info(mddev, &info); |
| 5783 | if (err) { |
| 5784 | printk(KERN_WARNING "md: couldn't set" |
| 5785 | " array info. %d\n", err); |
| 5786 | goto abort_unlock; |
| 5787 | } |
| 5788 | } |
| 5789 | goto done_unlock; |
| 5790 | |
| 5791 | default:; |
| 5792 | } |
| 5793 | |
| 5794 | /* |
| 5795 | * Commands querying/configuring an existing array: |
| 5796 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5797 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5798 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5799 | if ((!mddev->raid_disks && !mddev->external) |
| 5800 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5801 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5802 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | err = -ENODEV; |
| 5804 | goto abort_unlock; |
| 5805 | } |
| 5806 | |
| 5807 | /* |
| 5808 | * Commands even a read-only array can execute: |
| 5809 | */ |
| 5810 | switch (cmd) |
| 5811 | { |
| 5812 | case GET_ARRAY_INFO: |
| 5813 | err = get_array_info(mddev, argp); |
| 5814 | goto done_unlock; |
| 5815 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5816 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5817 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5818 | goto done_unlock; |
| 5819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | case GET_DISK_INFO: |
| 5821 | err = get_disk_info(mddev, argp); |
| 5822 | goto done_unlock; |
| 5823 | |
| 5824 | case RESTART_ARRAY_RW: |
| 5825 | err = restart_array(mddev); |
| 5826 | goto done_unlock; |
| 5827 | |
| 5828 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5829 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5830 | goto done_unlock; |
| 5831 | |
| 5832 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5833 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5834 | goto done_unlock; |
| 5835 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5836 | case BLKROSET: |
| 5837 | if (get_user(ro, (int __user *)(arg))) { |
| 5838 | err = -EFAULT; |
| 5839 | goto done_unlock; |
| 5840 | } |
| 5841 | err = -EINVAL; |
| 5842 | |
| 5843 | /* if the bdev is going readonly the value of mddev->ro |
| 5844 | * does not matter, no writes are coming |
| 5845 | */ |
| 5846 | if (ro) |
| 5847 | goto done_unlock; |
| 5848 | |
| 5849 | /* are we are already prepared for writes? */ |
| 5850 | if (mddev->ro != 1) |
| 5851 | goto done_unlock; |
| 5852 | |
| 5853 | /* transitioning to readauto need only happen for |
| 5854 | * arrays that call md_write_start |
| 5855 | */ |
| 5856 | if (mddev->pers) { |
| 5857 | err = restart_array(mddev); |
| 5858 | if (err == 0) { |
| 5859 | mddev->ro = 2; |
| 5860 | set_disk_ro(mddev->gendisk, 0); |
| 5861 | } |
| 5862 | } |
| 5863 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | /* |
| 5867 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5868 | * superblock, so we do not allow them on read-only arrays. |
| 5869 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5870 | * here and hit the 'default' below, so only disallow |
| 5871 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5873 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5874 | if (mddev->ro == 2) { |
| 5875 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5876 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5877 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5878 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5879 | } else { |
| 5880 | err = -EROFS; |
| 5881 | goto abort_unlock; |
| 5882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | } |
| 5884 | |
| 5885 | switch (cmd) |
| 5886 | { |
| 5887 | case ADD_NEW_DISK: |
| 5888 | { |
| 5889 | mdu_disk_info_t info; |
| 5890 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5891 | err = -EFAULT; |
| 5892 | else |
| 5893 | err = add_new_disk(mddev, &info); |
| 5894 | goto done_unlock; |
| 5895 | } |
| 5896 | |
| 5897 | case HOT_REMOVE_DISK: |
| 5898 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5899 | goto done_unlock; |
| 5900 | |
| 5901 | case HOT_ADD_DISK: |
| 5902 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5903 | goto done_unlock; |
| 5904 | |
| 5905 | case SET_DISK_FAULTY: |
| 5906 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5907 | goto done_unlock; |
| 5908 | |
| 5909 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5910 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | goto done_unlock; |
| 5912 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5913 | case SET_BITMAP_FILE: |
| 5914 | err = set_bitmap_file(mddev, (int)arg); |
| 5915 | goto done_unlock; |
| 5916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | err = -EINVAL; |
| 5919 | goto abort_unlock; |
| 5920 | } |
| 5921 | |
| 5922 | done_unlock: |
| 5923 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5924 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5925 | err != -EINVAL) |
| 5926 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | mddev_unlock(mddev); |
| 5928 | |
| 5929 | return err; |
| 5930 | done: |
| 5931 | if (err) |
| 5932 | MD_BUG(); |
| 5933 | abort: |
| 5934 | return err; |
| 5935 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5936 | #ifdef CONFIG_COMPAT |
| 5937 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5938 | unsigned int cmd, unsigned long arg) |
| 5939 | { |
| 5940 | switch (cmd) { |
| 5941 | case HOT_REMOVE_DISK: |
| 5942 | case HOT_ADD_DISK: |
| 5943 | case SET_DISK_FAULTY: |
| 5944 | case SET_BITMAP_FILE: |
| 5945 | /* These take in integer arg, do not convert */ |
| 5946 | break; |
| 5947 | default: |
| 5948 | arg = (unsigned long)compat_ptr(arg); |
| 5949 | break; |
| 5950 | } |
| 5951 | |
| 5952 | return md_ioctl(bdev, mode, cmd, arg); |
| 5953 | } |
| 5954 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5956 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | { |
| 5958 | /* |
| 5959 | * Succeed if we can lock the mddev, which confirms that |
| 5960 | * it isn't being stopped right now. |
| 5961 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5962 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | int err; |
| 5964 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5965 | if (mddev->gendisk != bdev->bd_disk) { |
| 5966 | /* we are racing with mddev_put which is discarding this |
| 5967 | * bd_disk. |
| 5968 | */ |
| 5969 | mddev_put(mddev); |
| 5970 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5971 | flush_scheduled_work(); |
| 5972 | /* Then retry the open from the top */ |
| 5973 | return -ERESTARTSYS; |
| 5974 | } |
| 5975 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5976 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5977 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5978 | goto out; |
| 5979 | |
| 5980 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5981 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5982 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5983 | |
NeilBrown | f3b99be | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 5984 | check_disk_size_change(mddev->gendisk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | out: |
| 5986 | return err; |
| 5987 | } |
| 5988 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5989 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5990 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5991 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5993 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5994 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | mddev_put(mddev); |
| 5996 | |
| 5997 | return 0; |
| 5998 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5999 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6000 | { |
| 6001 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6002 | .open = md_open, |
| 6003 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 6004 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6005 | #ifdef CONFIG_COMPAT |
| 6006 | .compat_ioctl = md_compat_ioctl, |
| 6007 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6008 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6009 | }; |
| 6010 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 6011 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | { |
| 6013 | mdk_thread_t *thread = arg; |
| 6014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | /* |
| 6016 | * md_thread is a 'system-thread', it's priority should be very |
| 6017 | * high. We avoid resource deadlocks individually in each |
| 6018 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 6019 | * the very same RT priority as kswapd, thus we will never get |
| 6020 | * into a priority inversion deadlock. |
| 6021 | * |
| 6022 | * we definitely have to have equal or higher priority than |
| 6023 | * bdflush, otherwise bdflush will deadlock if there are too |
| 6024 | * many dirty RAID5 blocks. |
| 6025 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6027 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6028 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 6030 | /* We need to wait INTERRUPTIBLE so that |
| 6031 | * we don't add to the load-average. |
| 6032 | * That means we need to be sure no signals are |
| 6033 | * pending |
| 6034 | */ |
| 6035 | if (signal_pending(current)) |
| 6036 | flush_signals(current); |
| 6037 | |
| 6038 | wait_event_interruptible_timeout |
| 6039 | (thread->wqueue, |
| 6040 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 6041 | || kthread_should_stop(), |
| 6042 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | |
| 6044 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 6045 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6046 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | return 0; |
| 6050 | } |
| 6051 | |
| 6052 | void md_wakeup_thread(mdk_thread_t *thread) |
| 6053 | { |
| 6054 | if (thread) { |
| 6055 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 6056 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 6057 | wake_up(&thread->wqueue); |
| 6058 | } |
| 6059 | } |
| 6060 | |
| 6061 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6062 | const char *name) |
| 6063 | { |
| 6064 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6066 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | if (!thread) |
| 6068 | return NULL; |
| 6069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | init_waitqueue_head(&thread->wqueue); |
| 6071 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | thread->run = run; |
| 6073 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6074 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6075 | thread->tsk = kthread_run(md_thread, thread, |
| 6076 | "%s_%s", |
| 6077 | mdname(thread->mddev), |
| 6078 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6079 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | kfree(thread); |
| 6081 | return NULL; |
| 6082 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6083 | return thread; |
| 6084 | } |
| 6085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | void md_unregister_thread(mdk_thread_t *thread) |
| 6087 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6088 | if (!thread) |
| 6089 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6090 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6091 | |
| 6092 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6093 | kfree(thread); |
| 6094 | } |
| 6095 | |
| 6096 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6097 | { |
| 6098 | if (!mddev) { |
| 6099 | MD_BUG(); |
| 6100 | return; |
| 6101 | } |
| 6102 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6103 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6105 | |
| 6106 | if (mddev->external) |
| 6107 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6108 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6110 | mdname(mddev), |
| 6111 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6112 | __builtin_return_address(0),__builtin_return_address(1), |
| 6113 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6114 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6115 | if (!mddev->pers) |
| 6116 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | if (!mddev->pers->error_handler) |
| 6118 | return; |
| 6119 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6120 | if (mddev->degraded) |
| 6121 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6122 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6124 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6125 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6126 | if (mddev->event_work.func) |
| 6127 | schedule_work(&mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6128 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6129 | } |
| 6130 | |
| 6131 | /* seq_file implementation /proc/mdstat */ |
| 6132 | |
| 6133 | static void status_unused(struct seq_file *seq) |
| 6134 | { |
| 6135 | int i = 0; |
| 6136 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | |
| 6138 | seq_printf(seq, "unused devices: "); |
| 6139 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6140 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | char b[BDEVNAME_SIZE]; |
| 6142 | i++; |
| 6143 | seq_printf(seq, "%s ", |
| 6144 | bdevname(rdev->bdev,b)); |
| 6145 | } |
| 6146 | if (!i) |
| 6147 | seq_printf(seq, "<none>"); |
| 6148 | |
| 6149 | seq_printf(seq, "\n"); |
| 6150 | } |
| 6151 | |
| 6152 | |
| 6153 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6154 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6155 | sector_t max_sectors, resync, res; |
| 6156 | unsigned long dt, db; |
| 6157 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6158 | int scale; |
| 6159 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6161 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | |
| 6163 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6164 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6166 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6167 | |
| 6168 | /* |
| 6169 | * Should not happen. |
| 6170 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6171 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | MD_BUG(); |
| 6173 | return; |
| 6174 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6175 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6176 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6177 | * u32, as those are the requirements for sector_div. |
| 6178 | * Thus 'scale' must be at least 10 |
| 6179 | */ |
| 6180 | scale = 10; |
| 6181 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6182 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6183 | scale++; |
| 6184 | } |
| 6185 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6186 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6187 | |
| 6188 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6189 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6190 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6191 | seq_printf(seq, "["); |
| 6192 | for (i = 0; i < x; i++) |
| 6193 | seq_printf(seq, "="); |
| 6194 | seq_printf(seq, ">"); |
| 6195 | for (i = 0; i < y; i++) |
| 6196 | seq_printf(seq, "."); |
| 6197 | seq_printf(seq, "] "); |
| 6198 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6199 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6200 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6201 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6202 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6203 | "check" : |
| 6204 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6205 | "resync" : "recovery"))), |
| 6206 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6207 | (unsigned long long) resync/2, |
| 6208 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6209 | |
| 6210 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | * dt: time from mark until now |
| 6212 | * db: blocks written from mark until now |
| 6213 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6214 | * |
| 6215 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6216 | * So we divide before multiply in case it is 32bit and close |
| 6217 | * to the limit. |
| 6218 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6219 | * near the end of resync when the number of remaining sectors |
| 6220 | * is close to 'db'. |
| 6221 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6222 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | */ |
| 6224 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6225 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6226 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6227 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6228 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6229 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6230 | sector_div(rt, db/32+1); |
| 6231 | rt *= dt; |
| 6232 | rt >>= 5; |
| 6233 | |
| 6234 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6235 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6236 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6237 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6238 | } |
| 6239 | |
| 6240 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6241 | { |
| 6242 | struct list_head *tmp; |
| 6243 | loff_t l = *pos; |
| 6244 | mddev_t *mddev; |
| 6245 | |
| 6246 | if (l >= 0x10000) |
| 6247 | return NULL; |
| 6248 | if (!l--) |
| 6249 | /* header */ |
| 6250 | return (void*)1; |
| 6251 | |
| 6252 | spin_lock(&all_mddevs_lock); |
| 6253 | list_for_each(tmp,&all_mddevs) |
| 6254 | if (!l--) { |
| 6255 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6256 | mddev_get(mddev); |
| 6257 | spin_unlock(&all_mddevs_lock); |
| 6258 | return mddev; |
| 6259 | } |
| 6260 | spin_unlock(&all_mddevs_lock); |
| 6261 | if (!l--) |
| 6262 | return (void*)2;/* tail */ |
| 6263 | return NULL; |
| 6264 | } |
| 6265 | |
| 6266 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6267 | { |
| 6268 | struct list_head *tmp; |
| 6269 | mddev_t *next_mddev, *mddev = v; |
| 6270 | |
| 6271 | ++*pos; |
| 6272 | if (v == (void*)2) |
| 6273 | return NULL; |
| 6274 | |
| 6275 | spin_lock(&all_mddevs_lock); |
| 6276 | if (v == (void*)1) |
| 6277 | tmp = all_mddevs.next; |
| 6278 | else |
| 6279 | tmp = mddev->all_mddevs.next; |
| 6280 | if (tmp != &all_mddevs) |
| 6281 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6282 | else { |
| 6283 | next_mddev = (void*)2; |
| 6284 | *pos = 0x10000; |
| 6285 | } |
| 6286 | spin_unlock(&all_mddevs_lock); |
| 6287 | |
| 6288 | if (v != (void*)1) |
| 6289 | mddev_put(mddev); |
| 6290 | return next_mddev; |
| 6291 | |
| 6292 | } |
| 6293 | |
| 6294 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6295 | { |
| 6296 | mddev_t *mddev = v; |
| 6297 | |
| 6298 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6299 | mddev_put(mddev); |
| 6300 | } |
| 6301 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6302 | struct mdstat_info { |
| 6303 | int event; |
| 6304 | }; |
| 6305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6307 | { |
| 6308 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6309 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6310 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6311 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6312 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | |
| 6314 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6315 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | seq_printf(seq, "Personalities : "); |
| 6317 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6318 | list_for_each_entry(pers, &pers_list, list) |
| 6319 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6320 | |
| 6321 | spin_unlock(&pers_lock); |
| 6322 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6323 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | return 0; |
| 6325 | } |
| 6326 | if (v == (void*)2) { |
| 6327 | status_unused(seq); |
| 6328 | return 0; |
| 6329 | } |
| 6330 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6331 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6332 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6334 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6335 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6336 | mddev->pers ? "" : "in"); |
| 6337 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6338 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6339 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6340 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6341 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6342 | seq_printf(seq, " %s", mddev->pers->name); |
| 6343 | } |
| 6344 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6345 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6346 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6347 | char b[BDEVNAME_SIZE]; |
| 6348 | seq_printf(seq, " %s[%d]", |
| 6349 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6350 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6351 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6352 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6353 | seq_printf(seq, "(F)"); |
| 6354 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6355 | } else if (rdev->raid_disk < 0) |
| 6356 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6357 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6358 | } |
| 6359 | |
| 6360 | if (!list_empty(&mddev->disks)) { |
| 6361 | if (mddev->pers) |
| 6362 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6363 | (unsigned long long) |
| 6364 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | else |
| 6366 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6367 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6368 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6369 | if (mddev->persistent) { |
| 6370 | if (mddev->major_version != 0 || |
| 6371 | mddev->minor_version != 90) { |
| 6372 | seq_printf(seq," super %d.%d", |
| 6373 | mddev->major_version, |
| 6374 | mddev->minor_version); |
| 6375 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6376 | } else if (mddev->external) |
| 6377 | seq_printf(seq, " super external:%s", |
| 6378 | mddev->metadata_type); |
| 6379 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6380 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | |
| 6382 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6383 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6384 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6385 | if (mddev->pers->sync_request) { |
| 6386 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6387 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6388 | seq_printf(seq, "\n "); |
| 6389 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6390 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6391 | else if (mddev->recovery_cp < MaxSector) |
| 6392 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6393 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6394 | } else |
| 6395 | seq_printf(seq, "\n "); |
| 6396 | |
| 6397 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6398 | unsigned long chunk_kb; |
| 6399 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6400 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6401 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6402 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6403 | "%lu%s chunk", |
| 6404 | bitmap->pages - bitmap->missing_pages, |
| 6405 | bitmap->pages, |
| 6406 | (bitmap->pages - bitmap->missing_pages) |
| 6407 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6408 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6409 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6410 | if (bitmap->file) { |
| 6411 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6412 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6413 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6414 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6415 | seq_printf(seq, "\n"); |
| 6416 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6417 | } |
| 6418 | |
| 6419 | seq_printf(seq, "\n"); |
| 6420 | } |
| 6421 | mddev_unlock(mddev); |
| 6422 | |
| 6423 | return 0; |
| 6424 | } |
| 6425 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6426 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6427 | .start = md_seq_start, |
| 6428 | .next = md_seq_next, |
| 6429 | .stop = md_seq_stop, |
| 6430 | .show = md_seq_show, |
| 6431 | }; |
| 6432 | |
| 6433 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6434 | { |
| 6435 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6436 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6437 | if (mi == NULL) |
| 6438 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | |
| 6440 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6441 | if (error) |
| 6442 | kfree(mi); |
| 6443 | else { |
| 6444 | struct seq_file *p = file->private_data; |
| 6445 | p->private = mi; |
| 6446 | mi->event = atomic_read(&md_event_count); |
| 6447 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6448 | return error; |
| 6449 | } |
| 6450 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6451 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6452 | { |
| 6453 | struct seq_file *m = filp->private_data; |
| 6454 | struct mdstat_info *mi = m->private; |
| 6455 | int mask; |
| 6456 | |
| 6457 | poll_wait(filp, &md_event_waiters, wait); |
| 6458 | |
| 6459 | /* always allow read */ |
| 6460 | mask = POLLIN | POLLRDNORM; |
| 6461 | |
| 6462 | if (mi->event != atomic_read(&md_event_count)) |
| 6463 | mask |= POLLERR | POLLPRI; |
| 6464 | return mask; |
| 6465 | } |
| 6466 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6467 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6468 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | .open = md_seq_open, |
| 6470 | .read = seq_read, |
| 6471 | .llseek = seq_lseek, |
Martin Peschke | c3f94b4 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6472 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6473 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6474 | }; |
| 6475 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6476 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6478 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6479 | list_add_tail(&p->list, &pers_list); |
| 6480 | 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] | 6481 | spin_unlock(&pers_lock); |
| 6482 | return 0; |
| 6483 | } |
| 6484 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6485 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6487 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6488 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6489 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6490 | spin_unlock(&pers_lock); |
| 6491 | return 0; |
| 6492 | } |
| 6493 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6494 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | { |
| 6496 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6497 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6498 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6499 | |
| 6500 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6501 | rcu_read_lock(); |
| 6502 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6504 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6505 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6506 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6507 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6508 | * as sync_io is counted when a request starts, and |
| 6509 | * disk_stats is counted when it completes. |
| 6510 | * So resync activity will cause curr_events to be smaller than |
| 6511 | * when there was no such activity. |
| 6512 | * non-sync IO will cause disk_stat to increase without |
| 6513 | * increasing sync_io so curr_events will (eventually) |
| 6514 | * be larger than it was before. Once it becomes |
| 6515 | * substantially larger, the test below will cause |
| 6516 | * the array to appear non-idle, and resync will slow |
| 6517 | * down. |
| 6518 | * If there is a lot of outstanding resync activity when |
| 6519 | * we set last_event to curr_events, then all that activity |
| 6520 | * completing might cause the array to appear non-idle |
| 6521 | * and resync will be slowed down even though there might |
| 6522 | * not have been non-resync activity. This will only |
| 6523 | * happen once though. 'last_events' will soon reflect |
| 6524 | * the state where there is little or no outstanding |
| 6525 | * resync requests, and further resync activity will |
| 6526 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6527 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6529 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6530 | rdev->last_events = curr_events; |
| 6531 | idle = 0; |
| 6532 | } |
| 6533 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6534 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | return idle; |
| 6536 | } |
| 6537 | |
| 6538 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6539 | { |
| 6540 | /* another "blocks" (512byte) blocks have been synced */ |
| 6541 | atomic_sub(blocks, &mddev->recovery_active); |
| 6542 | wake_up(&mddev->recovery_wait); |
| 6543 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6544 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6545 | md_wakeup_thread(mddev->thread); |
| 6546 | // stop recovery, signal do_sync .... |
| 6547 | } |
| 6548 | } |
| 6549 | |
| 6550 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6551 | /* md_write_start(mddev, bi) |
| 6552 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6553 | * in superblock) before writing, schedule a superblock update |
| 6554 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6555 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6556 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6557 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6558 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6559 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6560 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6561 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6562 | BUG_ON(mddev->ro == 1); |
| 6563 | if (mddev->ro == 2) { |
| 6564 | /* need to switch to read/write */ |
| 6565 | mddev->ro = 0; |
| 6566 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6567 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6568 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6569 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6570 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6571 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6572 | if (mddev->safemode == 1) |
| 6573 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6574 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6575 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6576 | if (mddev->in_sync) { |
| 6577 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6578 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6579 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6580 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6581 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6582 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6583 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6584 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6585 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6586 | wait_event(mddev->sb_wait, |
| 6587 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6588 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6589 | } |
| 6590 | |
| 6591 | void md_write_end(mddev_t *mddev) |
| 6592 | { |
| 6593 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6594 | if (mddev->safemode == 2) |
| 6595 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6596 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6598 | } |
| 6599 | } |
| 6600 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6601 | /* md_allow_write(mddev) |
| 6602 | * Calling this ensures that the array is marked 'active' so that writes |
| 6603 | * may proceed without blocking. It is important to call this before |
| 6604 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6605 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6606 | * |
| 6607 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6608 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6609 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6610 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6611 | { |
| 6612 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6613 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6614 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6615 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6616 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6617 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6618 | |
| 6619 | spin_lock_irq(&mddev->write_lock); |
| 6620 | if (mddev->in_sync) { |
| 6621 | mddev->in_sync = 0; |
| 6622 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6623 | if (mddev->safemode_delay && |
| 6624 | mddev->safemode == 0) |
| 6625 | mddev->safemode = 1; |
| 6626 | spin_unlock_irq(&mddev->write_lock); |
| 6627 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6628 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6629 | } else |
| 6630 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6631 | |
| 6632 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6633 | return -EAGAIN; |
| 6634 | else |
| 6635 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6636 | } |
| 6637 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6638 | |
NeilBrown | b63d7c2 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 6639 | void md_unplug(mddev_t *mddev) |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6640 | { |
| 6641 | if (mddev->queue) |
| 6642 | blk_unplug(mddev->queue); |
| 6643 | if (mddev->plug) |
| 6644 | mddev->plug->unplug_fn(mddev->plug); |
| 6645 | } |
| 6646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | #define SYNC_MARKS 10 |
| 6648 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6649 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | { |
| 6651 | mddev_t *mddev2; |
| 6652 | unsigned int currspeed = 0, |
| 6653 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6654 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6655 | unsigned long mark[SYNC_MARKS]; |
| 6656 | sector_t mark_cnt[SYNC_MARKS]; |
| 6657 | int last_mark,m; |
| 6658 | struct list_head *tmp; |
| 6659 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6660 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6661 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6662 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | |
| 6664 | /* just incase thread restarts... */ |
| 6665 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6666 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6667 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6668 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6669 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6670 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6671 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6672 | desc = "data-check"; |
| 6673 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6674 | desc = "requested-resync"; |
| 6675 | else |
| 6676 | desc = "resync"; |
| 6677 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6678 | desc = "reshape"; |
| 6679 | else |
| 6680 | desc = "recovery"; |
| 6681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6682 | /* we overload curr_resync somewhat here. |
| 6683 | * 0 == not engaged in resync at all |
| 6684 | * 2 == checking that there is no conflict with another sync |
| 6685 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6686 | * commense |
| 6687 | * other == active in resync - this many blocks |
| 6688 | * |
| 6689 | * Before starting a resync we must have set curr_resync to |
| 6690 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6691 | * less than ours. When we find one that is the same or higher |
| 6692 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6693 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6694 | * This will mean we have to start checking from the beginning again. |
| 6695 | * |
| 6696 | */ |
| 6697 | |
| 6698 | do { |
| 6699 | mddev->curr_resync = 2; |
| 6700 | |
| 6701 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6702 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6703 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6704 | |
| 6705 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6707 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | if (mddev2 == mddev) |
| 6709 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6710 | if (!mddev->parallel_resync |
| 6711 | && mddev2->curr_resync |
| 6712 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | DEFINE_WAIT(wq); |
| 6714 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6715 | /* arbitrarily yield */ |
| 6716 | mddev->curr_resync = 1; |
| 6717 | wake_up(&resync_wait); |
| 6718 | } |
| 6719 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6720 | /* no need to wait here, we can wait the next |
| 6721 | * time 'round when curr_resync == 2 |
| 6722 | */ |
| 6723 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6724 | /* We need to wait 'interruptible' so as not to |
| 6725 | * contribute to the load average, and not to |
| 6726 | * be caught by 'softlockup' |
| 6727 | */ |
| 6728 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6729 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6730 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6731 | printk(KERN_INFO "md: delaying %s of %s" |
| 6732 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6733 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6734 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6736 | if (signal_pending(current)) |
| 6737 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6738 | schedule(); |
| 6739 | finish_wait(&resync_wait, &wq); |
| 6740 | goto try_again; |
| 6741 | } |
| 6742 | finish_wait(&resync_wait, &wq); |
| 6743 | } |
| 6744 | } |
| 6745 | } while (mddev->curr_resync < 2); |
| 6746 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6747 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6748 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6749 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6750 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6751 | */ |
| 6752 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6753 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6754 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6755 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6756 | j = mddev->resync_min; |
| 6757 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6758 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6759 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6760 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6761 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6762 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6763 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6764 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6765 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6766 | rcu_read_lock(); |
| 6767 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6768 | if (rdev->raid_disk >= 0 && |
| 6769 | !test_bit(Faulty, &rdev->flags) && |
| 6770 | !test_bit(In_sync, &rdev->flags) && |
| 6771 | rdev->recovery_offset < j) |
| 6772 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6773 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6775 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6776 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6777 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6778 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6779 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6780 | "(but not more than %d KB/sec) for %s.\n", |
| 6781 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6782 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6783 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6784 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6785 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6786 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6787 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6788 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | } |
| 6790 | last_mark = 0; |
| 6791 | mddev->resync_mark = mark[last_mark]; |
| 6792 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6793 | |
| 6794 | /* |
| 6795 | * Tune reconstruction: |
| 6796 | */ |
| 6797 | window = 32*(PAGE_SIZE/512); |
| 6798 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6799 | window/2,(unsigned long long) max_sectors/2); |
| 6800 | |
| 6801 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | last_check = 0; |
| 6803 | |
| 6804 | if (j>2) { |
| 6805 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6806 | "md: resuming %s of %s from checkpoint.\n", |
| 6807 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | mddev->curr_resync = j; |
| 6809 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6810 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6811 | |
| 6812 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6813 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6814 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6815 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6816 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6817 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6818 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6819 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6820 | > (max_sectors >> 4)) || |
| 6821 | (j - mddev->curr_resync_completed)*2 |
| 6822 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6823 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6824 | /* time to update curr_resync_completed */ |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6825 | md_unplug(mddev); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6826 | wait_event(mddev->recovery_wait, |
| 6827 | atomic_read(&mddev->recovery_active) == 0); |
| 6828 | mddev->curr_resync_completed = |
| 6829 | mddev->curr_resync; |
NeilBrown | 676e42d | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 6830 | if (mddev->persistent) |
| 6831 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6832 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6833 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6834 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6835 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6836 | /* As this condition is controlled by user-space, |
| 6837 | * we can block indefinitely, so use '_interruptible' |
| 6838 | * to avoid triggering warnings. |
| 6839 | */ |
| 6840 | flush_signals(current); /* just in case */ |
| 6841 | wait_event_interruptible(mddev->recovery_wait, |
| 6842 | mddev->resync_max > j |
| 6843 | || kthread_should_stop()); |
| 6844 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6845 | |
| 6846 | if (kthread_should_stop()) |
| 6847 | goto interrupted; |
| 6848 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6849 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6850 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6851 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6852 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | goto out; |
| 6854 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6855 | |
| 6856 | if (!skipped) { /* actual IO requested */ |
| 6857 | io_sectors += sectors; |
| 6858 | atomic_add(sectors, &mddev->recovery_active); |
| 6859 | } |
| 6860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | j += sectors; |
| 6862 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6863 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6864 | if (last_check == 0) |
| 6865 | /* this is the earliers that rebuilt will be |
| 6866 | * visible in /proc/mdstat |
| 6867 | */ |
| 6868 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6869 | |
| 6870 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | continue; |
| 6872 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6873 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6875 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6876 | break; |
| 6877 | |
| 6878 | repeat: |
| 6879 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6880 | /* step marks */ |
| 6881 | int next = (last_mark+1) % SYNC_MARKS; |
| 6882 | |
| 6883 | mddev->resync_mark = mark[next]; |
| 6884 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6885 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6886 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6887 | last_mark = next; |
| 6888 | } |
| 6889 | |
| 6890 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6891 | if (kthread_should_stop()) |
| 6892 | goto interrupted; |
| 6893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6894 | |
| 6895 | /* |
| 6896 | * this loop exits only if either when we are slower than |
| 6897 | * the 'hard' speed limit, or the system was IO-idle for |
| 6898 | * a jiffy. |
| 6899 | * the system might be non-idle CPU-wise, but we only care |
| 6900 | * about not overloading the IO subsystem. (things like an |
| 6901 | * e2fsck being done on the RAID array should execute fast) |
| 6902 | */ |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6903 | md_unplug(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6904 | cond_resched(); |
| 6905 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6906 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6907 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6908 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6909 | if (currspeed > speed_min(mddev)) { |
| 6910 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6911 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6912 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6913 | goto repeat; |
| 6914 | } |
| 6915 | } |
| 6916 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6917 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6918 | /* |
| 6919 | * this also signals 'finished resyncing' to md_stop |
| 6920 | */ |
| 6921 | out: |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6922 | md_unplug(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | |
| 6924 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6925 | |
| 6926 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6927 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6929 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6930 | mddev->curr_resync > 2) { |
| 6931 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6932 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6933 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6934 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6935 | "md: checkpointing %s of %s.\n", |
| 6936 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6937 | mddev->recovery_cp = mddev->curr_resync; |
| 6938 | } |
| 6939 | } else |
| 6940 | mddev->recovery_cp = MaxSector; |
| 6941 | } else { |
| 6942 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6943 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6944 | rcu_read_lock(); |
| 6945 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6946 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6947 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6948 | !test_bit(Faulty, &rdev->flags) && |
| 6949 | !test_bit(In_sync, &rdev->flags) && |
| 6950 | rdev->recovery_offset < mddev->curr_resync) |
| 6951 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6952 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6953 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6954 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6955 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6957 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6958 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6959 | /* We completed so min/max setting can be forgotten if used. */ |
| 6960 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6961 | mddev->resync_min = 0; |
| 6962 | mddev->resync_max = MaxSector; |
| 6963 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6964 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6966 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6967 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6968 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6969 | wake_up(&resync_wait); |
| 6970 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6971 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6972 | return; |
| 6973 | |
| 6974 | interrupted: |
| 6975 | /* |
| 6976 | * got a signal, exit. |
| 6977 | */ |
| 6978 | printk(KERN_INFO |
| 6979 | "md: md_do_sync() got signal ... exiting\n"); |
| 6980 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6981 | goto out; |
| 6982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6983 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6984 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6985 | |
| 6986 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6987 | static int remove_and_add_spares(mddev_t *mddev) |
| 6988 | { |
| 6989 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6990 | int spares = 0; |
| 6991 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6992 | mddev->curr_resync_completed = 0; |
| 6993 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6994 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6995 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6996 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6997 | (test_bit(Faulty, &rdev->flags) || |
| 6998 | ! test_bit(In_sync, &rdev->flags)) && |
| 6999 | atomic_read(&rdev->nr_pending)==0) { |
| 7000 | if (mddev->pers->hot_remove_disk( |
| 7001 | mddev, rdev->raid_disk)==0) { |
| 7002 | char nm[20]; |
| 7003 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 7004 | sysfs_remove_link(&mddev->kobj, nm); |
| 7005 | rdev->raid_disk = -1; |
| 7006 | } |
| 7007 | } |
| 7008 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 7009 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7010 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7011 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 7012 | !test_bit(In_sync, &rdev->flags) && |
| 7013 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7014 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7015 | if (rdev->raid_disk < 0 |
| 7016 | && !test_bit(Faulty, &rdev->flags)) { |
| 7017 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7018 | if (mddev->pers-> |
| 7019 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7020 | char nm[20]; |
| 7021 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 7022 | if (sysfs_create_link(&mddev->kobj, |
| 7023 | &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7024 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7025 | spares++; |
| 7026 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 7027 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7028 | } else |
| 7029 | break; |
| 7030 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7031 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7032 | } |
| 7033 | return spares; |
| 7034 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7035 | /* |
| 7036 | * This routine is regularly called by all per-raid-array threads to |
| 7037 | * deal with generic issues like resync and super-block update. |
| 7038 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 7039 | * need this as they never do any recovery or update the superblock. |
| 7040 | * |
| 7041 | * It does not do any resync itself, but rather "forks" off other threads |
| 7042 | * to do that as needed. |
| 7043 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 7044 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7045 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7046 | * and wakeups up this thread which will reap the thread and finish up. |
| 7047 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 7048 | * |
| 7049 | * The overall approach is: |
| 7050 | * 1/ if the superblock needs updating, update it. |
| 7051 | * 2/ If a recovery thread is running, don't do anything else. |
| 7052 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 7053 | * 4/ If there are any faulty devices, remove them. |
| 7054 | * 5/ If array is degraded, try to add spares devices |
| 7055 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 7056 | */ |
| 7057 | void md_check_recovery(mddev_t *mddev) |
| 7058 | { |
| 7059 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | |
| 7061 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 7062 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 7063 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7064 | |
| 7065 | if (mddev->ro) |
| 7066 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7067 | |
| 7068 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7069 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7070 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7071 | mdname(mddev)); |
| 7072 | mddev->safemode = 2; |
| 7073 | } |
| 7074 | flush_signals(current); |
| 7075 | } |
| 7076 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7077 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7078 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7079 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7080 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7081 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7082 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7083 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7084 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7085 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7086 | )) |
| 7087 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7088 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7089 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7090 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7091 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7092 | if (mddev->ro) { |
| 7093 | /* Only thing we do on a ro array is remove |
| 7094 | * failed devices. |
| 7095 | */ |
| 7096 | remove_and_add_spares(mddev); |
| 7097 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7098 | goto unlock; |
| 7099 | } |
| 7100 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7101 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7102 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7103 | spin_lock_irq(&mddev->write_lock); |
| 7104 | if (mddev->safemode && |
| 7105 | !atomic_read(&mddev->writes_pending) && |
| 7106 | !mddev->in_sync && |
| 7107 | mddev->recovery_cp == MaxSector) { |
| 7108 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7109 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7110 | if (mddev->persistent) |
| 7111 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 7112 | } |
| 7113 | if (mddev->safemode == 1) |
| 7114 | mddev->safemode = 0; |
| 7115 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7116 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7117 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7118 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7119 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7120 | if (mddev->flags) |
| 7121 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7123 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7124 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7125 | /* resync/recovery still happening */ |
| 7126 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7127 | goto unlock; |
| 7128 | } |
| 7129 | if (mddev->sync_thread) { |
| 7130 | /* resync has finished, collect result */ |
| 7131 | md_unregister_thread(mddev->sync_thread); |
| 7132 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7133 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7134 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | /* success...*/ |
| 7136 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 7137 | if (mddev->pers->spare_active(mddev)) |
| 7138 | sysfs_notify(&mddev->kobj, NULL, |
| 7139 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7141 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7142 | mddev->pers->finish_reshape) |
| 7143 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7144 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7145 | |
| 7146 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7147 | * information must be scrapped |
| 7148 | */ |
| 7149 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7150 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7151 | rdev->saved_raid_disk = -1; |
| 7152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7153 | mddev->recovery = 0; |
| 7154 | /* flag recovery needed just to double check */ |
| 7155 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7156 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7157 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7158 | goto unlock; |
| 7159 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7160 | /* Set RUNNING before clearing NEEDED to avoid |
| 7161 | * any transients in the value of "sync_action". |
| 7162 | */ |
| 7163 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7164 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7165 | /* Clear some bits that don't mean anything, but |
| 7166 | * might be left set |
| 7167 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7168 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7169 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7170 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7171 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7172 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7173 | /* no recovery is running. |
| 7174 | * remove any failed drives, then |
| 7175 | * add spares if possible. |
| 7176 | * Spare are also removed and re-added, to allow |
| 7177 | * the personality to fail the re-add. |
| 7178 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7179 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7180 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7181 | if (mddev->pers->check_reshape == NULL || |
| 7182 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7183 | /* Cannot proceed */ |
| 7184 | goto unlock; |
| 7185 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7186 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7187 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7188 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7189 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7190 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7191 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7192 | } else if (mddev->recovery_cp < MaxSector) { |
| 7193 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7194 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7195 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7196 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7199 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7200 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7201 | /* We are adding a device or devices to an array |
| 7202 | * which has the bitmap stored on all devices. |
| 7203 | * So make sure all bitmap pages get written |
| 7204 | */ |
| 7205 | bitmap_write_all(mddev->bitmap); |
| 7206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7208 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7209 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7210 | if (!mddev->sync_thread) { |
| 7211 | printk(KERN_ERR "%s: could not start resync" |
| 7212 | " thread...\n", |
| 7213 | mdname(mddev)); |
| 7214 | /* leave the spares where they are, it shouldn't hurt */ |
| 7215 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7216 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7218 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7219 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7220 | } |
| 7221 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7222 | if (!mddev->sync_thread) { |
| 7223 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7224 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7225 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7226 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7227 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7228 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7229 | mddev_unlock(mddev); |
| 7230 | } |
| 7231 | } |
| 7232 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7233 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7234 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7235 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7236 | wait_event_timeout(rdev->blocked_wait, |
| 7237 | !test_bit(Blocked, &rdev->flags), |
| 7238 | msecs_to_jiffies(5000)); |
| 7239 | rdev_dec_pending(rdev, mddev); |
| 7240 | } |
| 7241 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7242 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7243 | static int md_notify_reboot(struct notifier_block *this, |
| 7244 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7245 | { |
| 7246 | struct list_head *tmp; |
| 7247 | mddev_t *mddev; |
| 7248 | |
| 7249 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7250 | |
| 7251 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7252 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7253 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7254 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7255 | /* Force a switch to readonly even array |
| 7256 | * appears to still be in use. Hence |
| 7257 | * the '100'. |
| 7258 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7259 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7260 | mddev_unlock(mddev); |
| 7261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7262 | /* |
| 7263 | * certain more exotic SCSI devices are known to be |
| 7264 | * volatile wrt too early system reboots. While the |
| 7265 | * right place to handle this issue is the given |
| 7266 | * driver, we do want to have a safe RAID driver ... |
| 7267 | */ |
| 7268 | mdelay(1000*1); |
| 7269 | } |
| 7270 | return NOTIFY_DONE; |
| 7271 | } |
| 7272 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7273 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7274 | .notifier_call = md_notify_reboot, |
| 7275 | .next = NULL, |
| 7276 | .priority = INT_MAX, /* before any real devices */ |
| 7277 | }; |
| 7278 | |
| 7279 | static void md_geninit(void) |
| 7280 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7282 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7283 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7284 | } |
| 7285 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7286 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7287 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7288 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7289 | return -1; |
| 7290 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7291 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7292 | return -1; |
| 7293 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7294 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7295 | md_probe, NULL, NULL); |
| 7296 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7297 | md_probe, NULL, NULL); |
| 7298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7299 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7300 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7301 | |
| 7302 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7303 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7304 | } |
| 7305 | |
| 7306 | |
| 7307 | #ifndef MODULE |
| 7308 | |
| 7309 | /* |
| 7310 | * Searches all registered partitions for autorun RAID arrays |
| 7311 | * at boot time. |
| 7312 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7313 | |
| 7314 | static LIST_HEAD(all_detected_devices); |
| 7315 | struct detected_devices_node { |
| 7316 | struct list_head list; |
| 7317 | dev_t dev; |
| 7318 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7319 | |
| 7320 | void md_autodetect_dev(dev_t dev) |
| 7321 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7322 | struct detected_devices_node *node_detected_dev; |
| 7323 | |
| 7324 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7325 | if (node_detected_dev) { |
| 7326 | node_detected_dev->dev = dev; |
| 7327 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7328 | } else { |
| 7329 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7330 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7332 | } |
| 7333 | |
| 7334 | |
| 7335 | static void autostart_arrays(int part) |
| 7336 | { |
| 7337 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7338 | struct detected_devices_node *node_detected_dev; |
| 7339 | dev_t dev; |
| 7340 | int i_scanned, i_passed; |
| 7341 | |
| 7342 | i_scanned = 0; |
| 7343 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7344 | |
| 7345 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7346 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7347 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7348 | i_scanned++; |
| 7349 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7350 | struct detected_devices_node, list); |
| 7351 | list_del(&node_detected_dev->list); |
| 7352 | dev = node_detected_dev->dev; |
| 7353 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7354 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7355 | if (IS_ERR(rdev)) |
| 7356 | continue; |
| 7357 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7358 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | MD_BUG(); |
| 7360 | continue; |
| 7361 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7362 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7363 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7364 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7365 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7366 | |
| 7367 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7368 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7369 | |
| 7370 | autorun_devices(part); |
| 7371 | } |
| 7372 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7373 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7374 | |
| 7375 | static __exit void md_exit(void) |
| 7376 | { |
| 7377 | mddev_t *mddev; |
| 7378 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7379 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7380 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7381 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7382 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7383 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7384 | unregister_blkdev(mdp_major, "mdp"); |
| 7385 | unregister_reboot_notifier(&md_notifier); |
| 7386 | unregister_sysctl_table(raid_table_header); |
| 7387 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7388 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7389 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7390 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7391 | } |
| 7392 | } |
| 7393 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7394 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7395 | module_exit(md_exit) |
| 7396 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7397 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7398 | { |
| 7399 | return sprintf(buffer, "%d", start_readonly); |
| 7400 | } |
| 7401 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7402 | { |
| 7403 | char *e; |
| 7404 | int num = simple_strtoul(val, &e, 10); |
| 7405 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7406 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7407 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7408 | } |
| 7409 | return -EINVAL; |
| 7410 | } |
| 7411 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7412 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7413 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8e | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7414 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7415 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7417 | EXPORT_SYMBOL(register_md_personality); |
| 7418 | EXPORT_SYMBOL(unregister_md_personality); |
| 7419 | EXPORT_SYMBOL(md_error); |
| 7420 | EXPORT_SYMBOL(md_done_sync); |
| 7421 | EXPORT_SYMBOL(md_write_start); |
| 7422 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7423 | EXPORT_SYMBOL(md_register_thread); |
| 7424 | EXPORT_SYMBOL(md_unregister_thread); |
| 7425 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7426 | EXPORT_SYMBOL(md_check_recovery); |
| 7427 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7428 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7429 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7430 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |