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> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 39 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 41 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 42 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 43 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 44 | #include <linux/hdreg.h> |
| 45 | #include <linux/proc_fs.h> |
| 46 | #include <linux/random.h> |
| 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | #define DEBUG 0 |
| 58 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 70 | static struct workqueue_struct *md_wq; |
| 71 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 72 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 73 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 76 | * Default number of read corrections we'll attempt on an rdev |
| 77 | * before ejecting it from the array. We divide the read error |
| 78 | * count by 2 for every hour elapsed between read errors. |
| 79 | */ |
| 80 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 81 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 83 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 84 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 85 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 87 | * speed limit - in case reconstruction slows down your system despite |
| 88 | * idle IO detection. |
| 89 | * |
| 90 | * 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] | 91 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | */ |
| 93 | |
| 94 | static int sysctl_speed_limit_min = 1000; |
| 95 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 96 | static inline int speed_min(mddev_t *mddev) |
| 97 | { |
| 98 | return mddev->sync_speed_min ? |
| 99 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 100 | } |
| 101 | |
| 102 | static inline int speed_max(mddev_t *mddev) |
| 103 | { |
| 104 | return mddev->sync_speed_max ? |
| 105 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | static struct ctl_table_header *raid_table_header; |
| 109 | |
| 110 | static ctl_table raid_table[] = { |
| 111 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | .procname = "speed_limit_min", |
| 113 | .data = &sysctl_speed_limit_min, |
| 114 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 115 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 116 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | }, |
| 118 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .procname = "speed_limit_max", |
| 120 | .data = &sysctl_speed_limit_max, |
| 121 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 122 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 123 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 125 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static ctl_table raid_dir_table[] = { |
| 129 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | .procname = "raid", |
| 131 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 132 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .child = raid_table, |
| 134 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 135 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static ctl_table raid_root_table[] = { |
| 139 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | .procname = "dev", |
| 141 | .maxlen = 0, |
| 142 | .mode = 0555, |
| 143 | .child = raid_dir_table, |
| 144 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 145 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 148 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 150 | static int start_readonly; |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 153 | * We have a system wide 'event count' that is incremented |
| 154 | * on any 'interesting' event, and readers of /proc/mdstat |
| 155 | * can use 'poll' or 'select' to find out when the event |
| 156 | * count increases. |
| 157 | * |
| 158 | * Events are: |
| 159 | * start array, stop array, error, add device, remove device, |
| 160 | * start build, activate spare |
| 161 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 162 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 163 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 164 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 165 | { |
| 166 | atomic_inc(&md_event_count); |
| 167 | wake_up(&md_event_waiters); |
| 168 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 169 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 170 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 171 | /* Alternate version that can be called from interrupts |
| 172 | * when calling sysfs_notify isn't needed. |
| 173 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 174 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 175 | { |
| 176 | atomic_inc(&md_event_count); |
| 177 | wake_up(&md_event_waiters); |
| 178 | } |
| 179 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 180 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | * Enables to iterate over all existing md arrays |
| 182 | * all_mddevs_lock protects this list. |
| 183 | */ |
| 184 | static LIST_HEAD(all_mddevs); |
| 185 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 186 | |
| 187 | |
| 188 | /* |
| 189 | * iterates through all used mddevs in the system. |
| 190 | * We take care to grab the all_mddevs_lock whenever navigating |
| 191 | * the list, and to always hold a refcount when unlocked. |
| 192 | * Any code which breaks out of this loop while own |
| 193 | * a reference to the current mddev and must mddev_put it. |
| 194 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 195 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | \ |
| 197 | for (({ spin_lock(&all_mddevs_lock); \ |
| 198 | tmp = all_mddevs.next; \ |
| 199 | mddev = NULL;}); \ |
| 200 | ({ if (tmp != &all_mddevs) \ |
| 201 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 202 | spin_unlock(&all_mddevs_lock); \ |
| 203 | if (mddev) mddev_put(mddev); \ |
| 204 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 205 | tmp != &all_mddevs;}); \ |
| 206 | ({ spin_lock(&all_mddevs_lock); \ |
| 207 | tmp = tmp->next;}) \ |
| 208 | ) |
| 209 | |
| 210 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 211 | /* Rather than calling directly into the personality make_request function, |
| 212 | * IO requests come here first so that we can check if the device is |
| 213 | * being suspended pending a reconfiguration. |
| 214 | * We hold a refcount over the call to ->make_request. By the time that |
| 215 | * call has finished, the bio has been linked into some internal structure |
| 216 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 217 | */ |
| 218 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 220 | const int rw = bio_data_dir(bio); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 221 | mddev_t *mddev = q->queuedata; |
| 222 | int rv; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 223 | int cpu; |
| 224 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 225 | if (mddev == NULL || mddev->pers == NULL) { |
| 226 | bio_io_error(bio); |
| 227 | return 0; |
| 228 | } |
| 229 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 230 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 231 | DEFINE_WAIT(__wait); |
| 232 | for (;;) { |
| 233 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 234 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 235 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 236 | break; |
| 237 | rcu_read_unlock(); |
| 238 | schedule(); |
| 239 | rcu_read_lock(); |
| 240 | } |
| 241 | finish_wait(&mddev->sb_wait, &__wait); |
| 242 | } |
| 243 | atomic_inc(&mddev->active_io); |
| 244 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 245 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 246 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 247 | |
| 248 | cpu = part_stat_lock(); |
| 249 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 250 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 251 | bio_sectors(bio)); |
| 252 | part_stat_unlock(); |
| 253 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 254 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 255 | wake_up(&mddev->sb_wait); |
| 256 | |
| 257 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 260 | /* mddev_suspend makes sure no new requests are submitted |
| 261 | * to the device, and that any requests that have been submitted |
| 262 | * are completely handled. |
| 263 | * Once ->stop is called and completes, the module will be completely |
| 264 | * unused. |
| 265 | */ |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 266 | void mddev_suspend(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 267 | { |
| 268 | BUG_ON(mddev->suspended); |
| 269 | mddev->suspended = 1; |
| 270 | synchronize_rcu(); |
| 271 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 272 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 273 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 274 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 275 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 276 | void mddev_resume(mddev_t *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 277 | { |
| 278 | mddev->suspended = 0; |
| 279 | wake_up(&mddev->sb_wait); |
| 280 | mddev->pers->quiesce(mddev, 0); |
| 281 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 282 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 283 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 284 | int mddev_congested(mddev_t *mddev, int bits) |
| 285 | { |
| 286 | return mddev->suspended; |
| 287 | } |
| 288 | EXPORT_SYMBOL(mddev_congested); |
| 289 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 290 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 291 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 292 | */ |
| 293 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 294 | static void md_end_flush(struct bio *bio, int err) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 295 | { |
| 296 | mdk_rdev_t *rdev = bio->bi_private; |
| 297 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 298 | |
| 299 | rdev_dec_pending(rdev, mddev); |
| 300 | |
| 301 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 302 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 303 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 304 | } |
| 305 | bio_put(bio); |
| 306 | } |
| 307 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 308 | static void submit_flushes(mddev_t *mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 309 | { |
| 310 | mdk_rdev_t *rdev; |
| 311 | |
| 312 | rcu_read_lock(); |
| 313 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 314 | if (rdev->raid_disk >= 0 && |
| 315 | !test_bit(Faulty, &rdev->flags)) { |
| 316 | /* Take two references, one is dropped |
| 317 | * when request finishes, one after |
| 318 | * we reclaim rcu_read_lock |
| 319 | */ |
| 320 | struct bio *bi; |
| 321 | atomic_inc(&rdev->nr_pending); |
| 322 | atomic_inc(&rdev->nr_pending); |
| 323 | rcu_read_unlock(); |
| 324 | bi = bio_alloc(GFP_KERNEL, 0); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 325 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 326 | bi->bi_private = rdev; |
| 327 | bi->bi_bdev = rdev->bdev; |
| 328 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 329 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 330 | rcu_read_lock(); |
| 331 | rdev_dec_pending(rdev, mddev); |
| 332 | } |
| 333 | rcu_read_unlock(); |
| 334 | } |
| 335 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 336 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 337 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 338 | mddev_t *mddev = container_of(ws, mddev_t, flush_work); |
| 339 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 340 | |
| 341 | atomic_set(&mddev->flush_pending, 1); |
| 342 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 343 | if (bio->bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 344 | /* an empty barrier - all done */ |
| 345 | bio_endio(bio, 0); |
| 346 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 347 | bio->bi_rw &= ~REQ_FLUSH; |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 348 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 349 | generic_make_request(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 350 | } |
| 351 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 352 | mddev->flush_bio = NULL; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 353 | wake_up(&mddev->sb_wait); |
| 354 | } |
| 355 | } |
| 356 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 357 | void md_flush_request(mddev_t *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 358 | { |
| 359 | spin_lock_irq(&mddev->write_lock); |
| 360 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 361 | !mddev->flush_bio, |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 362 | mddev->write_lock, /*nothing*/); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 363 | mddev->flush_bio = bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 364 | spin_unlock_irq(&mddev->write_lock); |
| 365 | |
| 366 | atomic_set(&mddev->flush_pending, 1); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 367 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 368 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 369 | submit_flushes(mddev); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 370 | |
| 371 | if (atomic_dec_and_test(&mddev->flush_pending)) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 372 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 373 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 374 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 375 | |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 376 | /* Support for plugging. |
| 377 | * This mirrors the plugging support in request_queue, but does not |
| 378 | * require having a whole queue |
| 379 | */ |
| 380 | static void plugger_work(struct work_struct *work) |
| 381 | { |
| 382 | struct plug_handle *plug = |
| 383 | container_of(work, struct plug_handle, unplug_work); |
| 384 | plug->unplug_fn(plug); |
| 385 | } |
| 386 | static void plugger_timeout(unsigned long data) |
| 387 | { |
| 388 | struct plug_handle *plug = (void *)data; |
| 389 | kblockd_schedule_work(NULL, &plug->unplug_work); |
| 390 | } |
| 391 | void plugger_init(struct plug_handle *plug, |
| 392 | void (*unplug_fn)(struct plug_handle *)) |
| 393 | { |
| 394 | plug->unplug_flag = 0; |
| 395 | plug->unplug_fn = unplug_fn; |
| 396 | init_timer(&plug->unplug_timer); |
| 397 | plug->unplug_timer.function = plugger_timeout; |
| 398 | plug->unplug_timer.data = (unsigned long)plug; |
| 399 | INIT_WORK(&plug->unplug_work, plugger_work); |
| 400 | } |
| 401 | EXPORT_SYMBOL_GPL(plugger_init); |
| 402 | |
| 403 | void plugger_set_plug(struct plug_handle *plug) |
| 404 | { |
| 405 | if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag)) |
| 406 | mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1); |
| 407 | } |
| 408 | EXPORT_SYMBOL_GPL(plugger_set_plug); |
| 409 | |
| 410 | int plugger_remove_plug(struct plug_handle *plug) |
| 411 | { |
| 412 | if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) { |
| 413 | del_timer(&plug->unplug_timer); |
| 414 | return 1; |
| 415 | } else |
| 416 | return 0; |
| 417 | } |
| 418 | EXPORT_SYMBOL_GPL(plugger_remove_plug); |
| 419 | |
| 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 422 | { |
| 423 | atomic_inc(&mddev->active); |
| 424 | return mddev; |
| 425 | } |
| 426 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 427 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | static void mddev_put(mddev_t *mddev) |
| 430 | { |
| 431 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 432 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 433 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 434 | mddev->ctime == 0 && !mddev->hold_active) { |
| 435 | /* Array is not configured at all, and not held active, |
| 436 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 438 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 439 | /* We did a probe so need to clean up. Call |
| 440 | * queue_work inside the spinlock so that |
| 441 | * flush_workqueue() after mddev_find will |
| 442 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 443 | */ |
| 444 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 445 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 446 | } else |
| 447 | kfree(mddev); |
| 448 | } |
| 449 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
| 451 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 452 | void mddev_init(mddev_t *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 453 | { |
| 454 | mutex_init(&mddev->open_mutex); |
| 455 | mutex_init(&mddev->reconfig_mutex); |
| 456 | mutex_init(&mddev->bitmap_info.mutex); |
| 457 | INIT_LIST_HEAD(&mddev->disks); |
| 458 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 459 | init_timer(&mddev->safemode_timer); |
| 460 | atomic_set(&mddev->active, 1); |
| 461 | atomic_set(&mddev->openers, 0); |
| 462 | atomic_set(&mddev->active_io, 0); |
| 463 | spin_lock_init(&mddev->write_lock); |
| 464 | atomic_set(&mddev->flush_pending, 0); |
| 465 | init_waitqueue_head(&mddev->sb_wait); |
| 466 | init_waitqueue_head(&mddev->recovery_wait); |
| 467 | mddev->reshape_position = MaxSector; |
| 468 | mddev->resync_min = 0; |
| 469 | mddev->resync_max = MaxSector; |
| 470 | mddev->level = LEVEL_NONE; |
| 471 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 472 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | static mddev_t * mddev_find(dev_t unit) |
| 475 | { |
| 476 | mddev_t *mddev, *new = NULL; |
| 477 | |
| 478 | retry: |
| 479 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 481 | if (unit) { |
| 482 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 483 | if (mddev->unit == unit) { |
| 484 | mddev_get(mddev); |
| 485 | spin_unlock(&all_mddevs_lock); |
| 486 | kfree(new); |
| 487 | return mddev; |
| 488 | } |
| 489 | |
| 490 | if (new) { |
| 491 | list_add(&new->all_mddevs, &all_mddevs); |
| 492 | spin_unlock(&all_mddevs_lock); |
| 493 | new->hold_active = UNTIL_IOCTL; |
| 494 | return new; |
| 495 | } |
| 496 | } else if (new) { |
| 497 | /* find an unused unit number */ |
| 498 | static int next_minor = 512; |
| 499 | int start = next_minor; |
| 500 | int is_free = 0; |
| 501 | int dev = 0; |
| 502 | while (!is_free) { |
| 503 | dev = MKDEV(MD_MAJOR, next_minor); |
| 504 | next_minor++; |
| 505 | if (next_minor > MINORMASK) |
| 506 | next_minor = 0; |
| 507 | if (next_minor == start) { |
| 508 | /* Oh dear, all in use. */ |
| 509 | spin_unlock(&all_mddevs_lock); |
| 510 | kfree(new); |
| 511 | return NULL; |
| 512 | } |
| 513 | |
| 514 | is_free = 1; |
| 515 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 516 | if (mddev->unit == dev) { |
| 517 | is_free = 0; |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | new->unit = dev; |
| 522 | new->md_minor = MINOR(dev); |
| 523 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | list_add(&new->all_mddevs, &all_mddevs); |
| 525 | spin_unlock(&all_mddevs_lock); |
| 526 | return new; |
| 527 | } |
| 528 | spin_unlock(&all_mddevs_lock); |
| 529 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 530 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (!new) |
| 532 | return NULL; |
| 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | new->unit = unit; |
| 535 | if (MAJOR(unit) == MD_MAJOR) |
| 536 | new->md_minor = MINOR(unit); |
| 537 | else |
| 538 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 539 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 540 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | goto retry; |
| 543 | } |
| 544 | |
| 545 | static inline int mddev_lock(mddev_t * mddev) |
| 546 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 547 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 550 | static inline int mddev_is_locked(mddev_t *mddev) |
| 551 | { |
| 552 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 553 | } |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | static inline int mddev_trylock(mddev_t * mddev) |
| 556 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 557 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 560 | static struct attribute_group md_redundancy_group; |
| 561 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 562 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 564 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 565 | /* These cannot be removed under reconfig_mutex as |
| 566 | * an access to the files will try to take reconfig_mutex |
| 567 | * while holding the file unremovable, which leads to |
| 568 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 569 | * So hold set sysfs_active while the remove in happeing, |
| 570 | * and anything else which might set ->to_remove or my |
| 571 | * otherwise change the sysfs namespace will fail with |
| 572 | * -EBUSY if sysfs_active is still set. |
| 573 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 574 | * test it under the same mutex to ensure its correct value |
| 575 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 576 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 577 | struct attribute_group *to_remove = mddev->to_remove; |
| 578 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 579 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 580 | mutex_unlock(&mddev->reconfig_mutex); |
| 581 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 582 | if (mddev->kobj.sd) { |
| 583 | if (to_remove != &md_redundancy_group) |
| 584 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 585 | if (mddev->pers == NULL || |
| 586 | mddev->pers->sync_request == NULL) { |
| 587 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 588 | if (mddev->sysfs_action) |
| 589 | sysfs_put(mddev->sysfs_action); |
| 590 | mddev->sysfs_action = NULL; |
| 591 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 592 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 593 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 594 | } else |
| 595 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 597 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 600 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 602 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 604 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | if (rdev->desc_nr == nr) |
| 606 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 612 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | mdk_rdev_t *rdev; |
| 614 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 615 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | if (rdev->bdev->bd_dev == dev) |
| 617 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | return NULL; |
| 620 | } |
| 621 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 622 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 623 | { |
| 624 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 625 | list_for_each_entry(pers, &pers_list, list) { |
| 626 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 627 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 628 | if (strcmp(pers->name, clevel)==0) |
| 629 | return pers; |
| 630 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 631 | return NULL; |
| 632 | } |
| 633 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 634 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 635 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 637 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 638 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 642 | { |
| 643 | if (rdev->sb_page) |
| 644 | MD_BUG(); |
| 645 | |
| 646 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 647 | if (!rdev->sb_page) { |
| 648 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 649 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 656 | { |
| 657 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 658 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | rdev->sb_loaded = 0; |
| 660 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 661 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 662 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 667 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 668 | { |
| 669 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 670 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 671 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 672 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 673 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 674 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 675 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 676 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 677 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 678 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 679 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 680 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 681 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 685 | sector_t sector, int size, struct page *page) |
| 686 | { |
| 687 | /* write first size bytes of page to sector of rdev |
| 688 | * Increment mddev->pending_writes before returning |
| 689 | * and decrement it on completion, waking up sb_wait |
| 690 | * if zero is reached. |
| 691 | * If an error occurred, call md_error |
| 692 | */ |
| 693 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
| 694 | |
| 695 | bio->bi_bdev = rdev->bdev; |
| 696 | bio->bi_sector = sector; |
| 697 | bio_add_page(bio, page, size, 0); |
| 698 | bio->bi_private = rdev; |
| 699 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 700 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 701 | atomic_inc(&mddev->pending_writes); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 702 | submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA, |
| 703 | bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | void md_super_wait(mddev_t *mddev) |
| 707 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 708 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 709 | DEFINE_WAIT(wq); |
| 710 | for(;;) { |
| 711 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 712 | if (atomic_read(&mddev->pending_writes)==0) |
| 713 | break; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 714 | schedule(); |
| 715 | } |
| 716 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 717 | } |
| 718 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 719 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | } |
| 723 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame^] | 724 | int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size, |
| 725 | struct page *page, int rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 727 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | struct completion event; |
| 729 | int ret; |
| 730 | |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 731 | rw |= REQ_SYNC | REQ_UNPLUG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame^] | 733 | bio->bi_bdev = rdev->bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | bio->bi_sector = sector; |
| 735 | bio_add_page(bio, page, size, 0); |
| 736 | init_completion(&event); |
| 737 | bio->bi_private = &event; |
| 738 | bio->bi_end_io = bi_complete; |
| 739 | submit_bio(rw, bio); |
| 740 | wait_for_completion(&event); |
| 741 | |
| 742 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 743 | bio_put(bio); |
| 744 | return ret; |
| 745 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 746 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 748 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | { |
| 750 | char b[BDEVNAME_SIZE]; |
| 751 | if (!rdev->sb_page) { |
| 752 | MD_BUG(); |
| 753 | return -EINVAL; |
| 754 | } |
| 755 | if (rdev->sb_loaded) |
| 756 | return 0; |
| 757 | |
| 758 | |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame^] | 759 | if (!sync_page_io(rdev, rdev->sb_start, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | goto fail; |
| 761 | rdev->sb_loaded = 1; |
| 762 | return 0; |
| 763 | |
| 764 | fail: |
| 765 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 766 | bdevname(rdev->bdev,b)); |
| 767 | return -EINVAL; |
| 768 | } |
| 769 | |
| 770 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 771 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 772 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 773 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 774 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 775 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 779 | { |
| 780 | int ret; |
| 781 | mdp_super_t *tmp1, *tmp2; |
| 782 | |
| 783 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 784 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 785 | |
| 786 | if (!tmp1 || !tmp2) { |
| 787 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 788 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | goto abort; |
| 790 | } |
| 791 | |
| 792 | *tmp1 = *sb1; |
| 793 | *tmp2 = *sb2; |
| 794 | |
| 795 | /* |
| 796 | * nr_disks is not constant |
| 797 | */ |
| 798 | tmp1->nr_disks = 0; |
| 799 | tmp2->nr_disks = 0; |
| 800 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 801 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 803 | kfree(tmp1); |
| 804 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | return ret; |
| 806 | } |
| 807 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 808 | |
| 809 | static u32 md_csum_fold(u32 csum) |
| 810 | { |
| 811 | csum = (csum & 0xffff) + (csum >> 16); |
| 812 | return (csum & 0xffff) + (csum >> 16); |
| 813 | } |
| 814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 816 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 817 | u64 newcsum = 0; |
| 818 | u32 *sb32 = (u32*)sb; |
| 819 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | unsigned int disk_csum, csum; |
| 821 | |
| 822 | disk_csum = sb->sb_csum; |
| 823 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 824 | |
| 825 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 826 | newcsum += sb32[i]; |
| 827 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 828 | |
| 829 | |
| 830 | #ifdef CONFIG_ALPHA |
| 831 | /* This used to use csum_partial, which was wrong for several |
| 832 | * reasons including that different results are returned on |
| 833 | * different architectures. It isn't critical that we get exactly |
| 834 | * the same return value as before (we always csum_fold before |
| 835 | * testing, and that removes any differences). However as we |
| 836 | * know that csum_partial always returned a 16bit value on |
| 837 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 838 | */ |
| 839 | sb->sb_csum = md_csum_fold(disk_csum); |
| 840 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 842 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | return csum; |
| 844 | } |
| 845 | |
| 846 | |
| 847 | /* |
| 848 | * Handle superblock details. |
| 849 | * We want to be able to handle multiple superblock formats |
| 850 | * so we have a common interface to them all, and an array of |
| 851 | * different handlers. |
| 852 | * We rely on user-space to write the initial superblock, and support |
| 853 | * reading and updating of superblocks. |
| 854 | * Interface methods are: |
| 855 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 856 | * loads and validates a superblock on dev. |
| 857 | * if refdev != NULL, compare superblocks on both devices |
| 858 | * Return: |
| 859 | * 0 - dev has a superblock that is compatible with refdev |
| 860 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 861 | * so dev should be used as the refdev in future |
| 862 | * -EINVAL superblock incompatible or invalid |
| 863 | * -othererror e.g. -EIO |
| 864 | * |
| 865 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 866 | * Verify that dev is acceptable into mddev. |
| 867 | * The first time, mddev->raid_disks will be 0, and data from |
| 868 | * dev should be merged in. Subsequent calls check that dev |
| 869 | * is new enough. Return 0 or -EINVAL |
| 870 | * |
| 871 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 872 | * Update the superblock for rdev with data in mddev |
| 873 | * This does not write to disc. |
| 874 | * |
| 875 | */ |
| 876 | |
| 877 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 878 | char *name; |
| 879 | struct module *owner; |
| 880 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 881 | int minor_version); |
| 882 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 883 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 884 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 885 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | }; |
| 887 | |
| 888 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 889 | * Check that the given mddev has no bitmap. |
| 890 | * |
| 891 | * This function is called from the run method of all personalities that do not |
| 892 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 893 | * has a bitmap. Otherwise, it returns 0. |
| 894 | * |
| 895 | */ |
| 896 | int md_check_no_bitmap(mddev_t *mddev) |
| 897 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 898 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 899 | return 0; |
| 900 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 901 | mdname(mddev), mddev->pers->name); |
| 902 | return 1; |
| 903 | } |
| 904 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 905 | |
| 906 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | * load_super for 0.90.0 |
| 908 | */ |
| 909 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 910 | { |
| 911 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 912 | mdp_super_t *sb; |
| 913 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
| 915 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 916 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | * it's at the end of the disk. |
| 918 | * |
| 919 | * It also happens to be a multiple of 4Kb. |
| 920 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 921 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 923 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | if (ret) return ret; |
| 925 | |
| 926 | ret = -EINVAL; |
| 927 | |
| 928 | bdevname(rdev->bdev, b); |
| 929 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 930 | |
| 931 | if (sb->md_magic != MD_SB_MAGIC) { |
| 932 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 933 | b); |
| 934 | goto abort; |
| 935 | } |
| 936 | |
| 937 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 938 | sb->minor_version < 90 || |
| 939 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 941 | sb->major_version, sb->minor_version, |
| 942 | b); |
| 943 | goto abort; |
| 944 | } |
| 945 | |
| 946 | if (sb->raid_disks <= 0) |
| 947 | goto abort; |
| 948 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 949 | 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] | 950 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 951 | b); |
| 952 | goto abort; |
| 953 | } |
| 954 | |
| 955 | rdev->preferred_minor = sb->md_minor; |
| 956 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 957 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
| 959 | if (sb->level == LEVEL_MULTIPATH) |
| 960 | rdev->desc_nr = -1; |
| 961 | else |
| 962 | rdev->desc_nr = sb->this_disk.number; |
| 963 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 964 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 966 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | __u64 ev1, ev2; |
| 968 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 969 | if (!uuid_equal(refsb, sb)) { |
| 970 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 971 | b, bdevname(refdev->bdev,b2)); |
| 972 | goto abort; |
| 973 | } |
| 974 | if (!sb_equal(refsb, sb)) { |
| 975 | printk(KERN_WARNING "md: %s has same UUID" |
| 976 | " but different superblock to %s\n", |
| 977 | b, bdevname(refdev->bdev, b2)); |
| 978 | goto abort; |
| 979 | } |
| 980 | ev1 = md_event(sb); |
| 981 | ev2 = md_event(refsb); |
| 982 | if (ev1 > ev2) |
| 983 | ret = 1; |
| 984 | else |
| 985 | ret = 0; |
| 986 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 987 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 989 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 990 | /* "this cannot possibly happen" ... */ |
| 991 | ret = -EINVAL; |
| 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | abort: |
| 994 | return ret; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * validate_super for 0.90.0 |
| 999 | */ |
| 1000 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1001 | { |
| 1002 | mdp_disk_t *desc; |
| 1003 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1004 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1006 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1007 | clear_bit(Faulty, &rdev->flags); |
| 1008 | clear_bit(In_sync, &rdev->flags); |
| 1009 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | if (mddev->raid_disks == 0) { |
| 1012 | mddev->major_version = 0; |
| 1013 | mddev->minor_version = sb->minor_version; |
| 1014 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1015 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1016 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | mddev->ctime = sb->ctime; |
| 1018 | mddev->utime = sb->utime; |
| 1019 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1020 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | mddev->layout = sb->layout; |
| 1022 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1023 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1024 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1025 | mddev->bitmap_info.offset = 0; |
| 1026 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1028 | if (mddev->minor_version >= 91) { |
| 1029 | mddev->reshape_position = sb->reshape_position; |
| 1030 | mddev->delta_disks = sb->delta_disks; |
| 1031 | mddev->new_level = sb->new_level; |
| 1032 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1033 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1034 | } else { |
| 1035 | mddev->reshape_position = MaxSector; |
| 1036 | mddev->delta_disks = 0; |
| 1037 | mddev->new_level = mddev->level; |
| 1038 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1039 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1040 | } |
| 1041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1043 | mddev->recovery_cp = MaxSector; |
| 1044 | else { |
| 1045 | if (sb->events_hi == sb->cp_events_hi && |
| 1046 | sb->events_lo == sb->cp_events_lo) { |
| 1047 | mddev->recovery_cp = sb->recovery_cp; |
| 1048 | } else |
| 1049 | mddev->recovery_cp = 0; |
| 1050 | } |
| 1051 | |
| 1052 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1053 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1054 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1055 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1056 | |
| 1057 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1058 | |
| 1059 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1060 | mddev->bitmap_info.file == NULL) |
| 1061 | mddev->bitmap_info.offset = |
| 1062 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1063 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1064 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1065 | /* Insist on good event counter while assembling, except |
| 1066 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1068 | if (sb->disks[rdev->desc_nr].state & ( |
| 1069 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1070 | if (ev1 < mddev->events) |
| 1071 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1072 | } else if (mddev->bitmap) { |
| 1073 | /* if adding to array with a bitmap, then we can accept an |
| 1074 | * older device ... but not too old. |
| 1075 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1076 | if (ev1 < mddev->bitmap->events_cleared) |
| 1077 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1078 | } else { |
| 1079 | if (ev1 < mddev->events) |
| 1080 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1081 | return 0; |
| 1082 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | desc = sb->disks + rdev->desc_nr; |
| 1086 | |
| 1087 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1088 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1089 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1090 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1091 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1093 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1094 | /* active but not in sync implies recovery up to |
| 1095 | * reshape position. We don't know exactly where |
| 1096 | * that is, so set to zero for now */ |
| 1097 | if (mddev->minor_version >= 91) { |
| 1098 | rdev->recovery_offset = 0; |
| 1099 | rdev->raid_disk = desc->raid_disk; |
| 1100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1102 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1103 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1104 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1105 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | /* |
| 1110 | * sync_super for 0.90.0 |
| 1111 | */ |
| 1112 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1113 | { |
| 1114 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | mdk_rdev_t *rdev2; |
| 1116 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | |
| 1119 | /* make rdev->sb match mddev data.. |
| 1120 | * |
| 1121 | * 1/ zero out disks |
| 1122 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1123 | * 3/ any empty disks < next_spare become removed |
| 1124 | * |
| 1125 | * disks[0] gets initialised to REMOVED because |
| 1126 | * we cannot be sure from other fields if it has |
| 1127 | * been initialised or not. |
| 1128 | */ |
| 1129 | int i; |
| 1130 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1131 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1132 | rdev->sb_size = MD_SB_BYTES; |
| 1133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1135 | |
| 1136 | memset(sb, 0, sizeof(*sb)); |
| 1137 | |
| 1138 | sb->md_magic = MD_SB_MAGIC; |
| 1139 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | sb->patch_version = mddev->patch_version; |
| 1141 | sb->gvalid_words = 0; /* ignored */ |
| 1142 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1143 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1144 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1145 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1146 | |
| 1147 | sb->ctime = mddev->ctime; |
| 1148 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1149 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | sb->raid_disks = mddev->raid_disks; |
| 1151 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1152 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | sb->utime = mddev->utime; |
| 1154 | sb->state = 0; |
| 1155 | sb->events_hi = (mddev->events>>32); |
| 1156 | sb->events_lo = (u32)mddev->events; |
| 1157 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1158 | if (mddev->reshape_position == MaxSector) |
| 1159 | sb->minor_version = 90; |
| 1160 | else { |
| 1161 | sb->minor_version = 91; |
| 1162 | sb->reshape_position = mddev->reshape_position; |
| 1163 | sb->new_level = mddev->new_level; |
| 1164 | sb->delta_disks = mddev->delta_disks; |
| 1165 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1166 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1167 | } |
| 1168 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | if (mddev->in_sync) |
| 1170 | { |
| 1171 | sb->recovery_cp = mddev->recovery_cp; |
| 1172 | sb->cp_events_hi = (mddev->events>>32); |
| 1173 | sb->cp_events_lo = (u32)mddev->events; |
| 1174 | if (mddev->recovery_cp == MaxSector) |
| 1175 | sb->state = (1<< MD_SB_CLEAN); |
| 1176 | } else |
| 1177 | sb->recovery_cp = 0; |
| 1178 | |
| 1179 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1180 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1182 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1183 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1186 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1188 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1189 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1190 | |
| 1191 | if (rdev2->raid_disk >= 0 && |
| 1192 | sb->minor_version >= 91) |
| 1193 | /* we have nowhere to store the recovery_offset, |
| 1194 | * but if it is not below the reshape_position, |
| 1195 | * we can piggy-back on that. |
| 1196 | */ |
| 1197 | is_active = 1; |
| 1198 | if (rdev2->raid_disk < 0 || |
| 1199 | test_bit(Faulty, &rdev2->flags)) |
| 1200 | is_active = 0; |
| 1201 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1202 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1204 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1205 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | d = &sb->disks[rdev2->desc_nr]; |
| 1207 | nr_disks++; |
| 1208 | d->number = rdev2->desc_nr; |
| 1209 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1210 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1211 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | d->raid_disk = rdev2->raid_disk; |
| 1213 | else |
| 1214 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1215 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1217 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1219 | if (test_bit(In_sync, &rdev2->flags)) |
| 1220 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | active++; |
| 1222 | working++; |
| 1223 | } else { |
| 1224 | d->state = 0; |
| 1225 | spare++; |
| 1226 | working++; |
| 1227 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1228 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1229 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1232 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1233 | mdp_disk_t *d = &sb->disks[i]; |
| 1234 | if (d->state == 0 && d->number == 0) { |
| 1235 | d->number = i; |
| 1236 | d->raid_disk = i; |
| 1237 | d->state = (1<<MD_DISK_REMOVED); |
| 1238 | d->state |= (1<<MD_DISK_FAULTY); |
| 1239 | failed++; |
| 1240 | } |
| 1241 | } |
| 1242 | sb->nr_disks = nr_disks; |
| 1243 | sb->active_disks = active; |
| 1244 | sb->working_disks = working; |
| 1245 | sb->failed_disks = failed; |
| 1246 | sb->spare_disks = spare; |
| 1247 | |
| 1248 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1249 | sb->sb_csum = calc_sb_csum(sb); |
| 1250 | } |
| 1251 | |
| 1252 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1253 | * rdev_size_change for 0.90.0 |
| 1254 | */ |
| 1255 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1256 | 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] | 1257 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1258 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1259 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1260 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1261 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1262 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1263 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1264 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1265 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1266 | rdev->sb_page); |
| 1267 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1268 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | * version 1 superblock |
| 1274 | */ |
| 1275 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1276 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1278 | __le32 disk_csum; |
| 1279 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | unsigned long long newcsum; |
| 1281 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1282 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | int i; |
| 1284 | |
| 1285 | disk_csum = sb->sb_csum; |
| 1286 | sb->sb_csum = 0; |
| 1287 | newcsum = 0; |
| 1288 | for (i=0; size>=4; size -= 4 ) |
| 1289 | newcsum += le32_to_cpu(*isuper++); |
| 1290 | |
| 1291 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1292 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
| 1294 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1295 | sb->sb_csum = disk_csum; |
| 1296 | return cpu_to_le32(csum); |
| 1297 | } |
| 1298 | |
| 1299 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1300 | { |
| 1301 | struct mdp_superblock_1 *sb; |
| 1302 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1303 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1305 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | |
| 1307 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1308 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | * It is always aligned to a 4K boundary and |
| 1310 | * depeding on minor_version, it can be: |
| 1311 | * 0: At least 8K, but less than 12K, from end of device |
| 1312 | * 1: At start of device |
| 1313 | * 2: 4K from start of device. |
| 1314 | */ |
| 1315 | switch(minor_version) { |
| 1316 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1317 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1318 | sb_start -= 8*2; |
| 1319 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | break; |
| 1321 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1322 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | break; |
| 1324 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1325 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | break; |
| 1327 | default: |
| 1328 | return -EINVAL; |
| 1329 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1330 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1332 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1333 | * and it is safe to read 4k, so we do that |
| 1334 | */ |
| 1335 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | if (ret) return ret; |
| 1337 | |
| 1338 | |
| 1339 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1340 | |
| 1341 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1342 | sb->major_version != cpu_to_le32(1) || |
| 1343 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1344 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1345 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | return -EINVAL; |
| 1347 | |
| 1348 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1349 | printk("md: invalid superblock checksum on %s\n", |
| 1350 | bdevname(rdev->bdev,b)); |
| 1351 | return -EINVAL; |
| 1352 | } |
| 1353 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1354 | printk("md: data_size too small on %s\n", |
| 1355 | bdevname(rdev->bdev,b)); |
| 1356 | return -EINVAL; |
| 1357 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | rdev->preferred_minor = 0xffff; |
| 1360 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1361 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1363 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1364 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1365 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1366 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1367 | |
| 1368 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1369 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1370 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1371 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1372 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1373 | rdev->desc_nr = -1; |
| 1374 | else |
| 1375 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1376 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1377 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1378 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1379 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | __u64 ev1, ev2; |
| 1381 | struct mdp_superblock_1 *refsb = |
| 1382 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1383 | |
| 1384 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1385 | sb->level != refsb->level || |
| 1386 | sb->layout != refsb->layout || |
| 1387 | sb->chunksize != refsb->chunksize) { |
| 1388 | printk(KERN_WARNING "md: %s has strangely different" |
| 1389 | " superblock to %s\n", |
| 1390 | bdevname(rdev->bdev,b), |
| 1391 | bdevname(refdev->bdev,b2)); |
| 1392 | return -EINVAL; |
| 1393 | } |
| 1394 | ev1 = le64_to_cpu(sb->events); |
| 1395 | ev2 = le64_to_cpu(refsb->events); |
| 1396 | |
| 1397 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1398 | ret = 1; |
| 1399 | else |
| 1400 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1402 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1403 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1404 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1406 | rdev->sectors = rdev->sb_start; |
| 1407 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1409 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1410 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1411 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1412 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1416 | { |
| 1417 | 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] | 1418 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1420 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1421 | clear_bit(Faulty, &rdev->flags); |
| 1422 | clear_bit(In_sync, &rdev->flags); |
| 1423 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | if (mddev->raid_disks == 0) { |
| 1426 | mddev->major_version = 1; |
| 1427 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1428 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1429 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1431 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1432 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1433 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | mddev->layout = le32_to_cpu(sb->layout); |
| 1435 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1436 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1437 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1438 | mddev->bitmap_info.offset = 0; |
| 1439 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | |
| 1441 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1442 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1443 | |
| 1444 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1445 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1446 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1447 | mddev->bitmap_info.file == NULL ) |
| 1448 | mddev->bitmap_info.offset = |
| 1449 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1450 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1451 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1452 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1453 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1454 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1455 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1456 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1457 | } else { |
| 1458 | mddev->reshape_position = MaxSector; |
| 1459 | mddev->delta_disks = 0; |
| 1460 | mddev->new_level = mddev->level; |
| 1461 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1462 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1463 | } |
| 1464 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1465 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1466 | /* Insist of good event counter while assembling, except for |
| 1467 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1469 | if (rdev->desc_nr >= 0 && |
| 1470 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1471 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1472 | if (ev1 < mddev->events) |
| 1473 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1474 | } else if (mddev->bitmap) { |
| 1475 | /* If adding to array with a bitmap, then we can accept an |
| 1476 | * older device, but not too old. |
| 1477 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1478 | if (ev1 < mddev->bitmap->events_cleared) |
| 1479 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1480 | } else { |
| 1481 | if (ev1 < mddev->events) |
| 1482 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1483 | return 0; |
| 1484 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1486 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1487 | if (rdev->desc_nr < 0 || |
| 1488 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1489 | role = 0xffff; |
| 1490 | rdev->desc_nr = -1; |
| 1491 | } else |
| 1492 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | switch(role) { |
| 1494 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | break; |
| 1496 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1497 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | break; |
| 1499 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1500 | if ((le32_to_cpu(sb->feature_map) & |
| 1501 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1502 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1503 | else |
| 1504 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | rdev->raid_disk = role; |
| 1506 | break; |
| 1507 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1508 | if (sb->devflags & WriteMostly1) |
| 1509 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1510 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1511 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | return 0; |
| 1514 | } |
| 1515 | |
| 1516 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1517 | { |
| 1518 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | mdk_rdev_t *rdev2; |
| 1520 | int max_dev, i; |
| 1521 | /* make rdev->sb match mddev and rdev data. */ |
| 1522 | |
| 1523 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1524 | |
| 1525 | sb->feature_map = 0; |
| 1526 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1527 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1529 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1530 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1531 | |
| 1532 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1533 | sb->events = cpu_to_le64(mddev->events); |
| 1534 | if (mddev->in_sync) |
| 1535 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1536 | else |
| 1537 | sb->resync_offset = cpu_to_le64(0); |
| 1538 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1539 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1540 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1541 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1542 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1543 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1544 | sb->level = cpu_to_le32(mddev->level); |
| 1545 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1546 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1547 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1548 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1549 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1550 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1551 | |
| 1552 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1553 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1554 | sb->feature_map |= |
| 1555 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1556 | sb->recovery_offset = |
| 1557 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1558 | } |
| 1559 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1560 | if (mddev->reshape_position != MaxSector) { |
| 1561 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1562 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1563 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1564 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1565 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1566 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1567 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1570 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | if (rdev2->desc_nr+1 > max_dev) |
| 1572 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1573 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1574 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1575 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1576 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1577 | rdev->sb_size = max_dev * 2 + 256; |
| 1578 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1579 | if (rdev->sb_size & bmask) |
| 1580 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1581 | } else |
| 1582 | max_dev = le32_to_cpu(sb->max_dev); |
| 1583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | for (i=0; i<max_dev;i++) |
| 1585 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1586 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1587 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1589 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1591 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1593 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1594 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | else |
| 1596 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1597 | } |
| 1598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1600 | } |
| 1601 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1602 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1603 | 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] | 1604 | { |
| 1605 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1606 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1607 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1608 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1609 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1610 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1611 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1612 | max_sectors -= rdev->data_offset; |
| 1613 | if (!num_sectors || num_sectors > max_sectors) |
| 1614 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1615 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1616 | /* minor version 0 with bitmap we can't move */ |
| 1617 | return 0; |
| 1618 | } else { |
| 1619 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1620 | sector_t sb_start; |
| 1621 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1622 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1623 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1624 | if (!num_sectors || num_sectors > max_sectors) |
| 1625 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1626 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1627 | } |
| 1628 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1629 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1630 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1631 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1632 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1633 | rdev->sb_page); |
| 1634 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1635 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1636 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1638 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | [0] = { |
| 1640 | .name = "0.90.0", |
| 1641 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1642 | .load_super = super_90_load, |
| 1643 | .validate_super = super_90_validate, |
| 1644 | .sync_super = super_90_sync, |
| 1645 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | }, |
| 1647 | [1] = { |
| 1648 | .name = "md-1", |
| 1649 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1650 | .load_super = super_1_load, |
| 1651 | .validate_super = super_1_validate, |
| 1652 | .sync_super = super_1_sync, |
| 1653 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | }, |
| 1655 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | |
| 1657 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1658 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1659 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1661 | rcu_read_lock(); |
| 1662 | rdev_for_each_rcu(rdev, mddev1) |
| 1663 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1664 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1665 | rdev2->bdev->bd_contains) { |
| 1666 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1667 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1668 | } |
| 1669 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | static LIST_HEAD(pending_raid_disks); |
| 1674 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1675 | /* |
| 1676 | * Try to register data integrity profile for an mddev |
| 1677 | * |
| 1678 | * This is called when an array is started and after a disk has been kicked |
| 1679 | * from the array. It only succeeds if all working and active component devices |
| 1680 | * are integrity capable with matching profiles. |
| 1681 | */ |
| 1682 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1683 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1684 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1685 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1686 | if (list_empty(&mddev->disks)) |
| 1687 | return 0; /* nothing to do */ |
| 1688 | if (blk_get_integrity(mddev->gendisk)) |
| 1689 | return 0; /* already registered */ |
| 1690 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1691 | /* skip spares and non-functional disks */ |
| 1692 | if (test_bit(Faulty, &rdev->flags)) |
| 1693 | continue; |
| 1694 | if (rdev->raid_disk < 0) |
| 1695 | continue; |
| 1696 | /* |
| 1697 | * If at least one rdev is not integrity capable, we can not |
| 1698 | * enable data integrity for the md device. |
| 1699 | */ |
| 1700 | if (!bdev_get_integrity(rdev->bdev)) |
| 1701 | return -EINVAL; |
| 1702 | if (!reference) { |
| 1703 | /* Use the first rdev as the reference */ |
| 1704 | reference = rdev; |
| 1705 | continue; |
| 1706 | } |
| 1707 | /* does this rdev's profile match the reference profile? */ |
| 1708 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1709 | rdev->bdev->bd_disk) < 0) |
| 1710 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1711 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1712 | /* |
| 1713 | * All component devices are integrity capable and have matching |
| 1714 | * profiles, register the common profile for the md device. |
| 1715 | */ |
| 1716 | if (blk_integrity_register(mddev->gendisk, |
| 1717 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1718 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1719 | mdname(mddev)); |
| 1720 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1721 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1722 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1723 | mdname(mddev)); |
| 1724 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1725 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1726 | EXPORT_SYMBOL(md_integrity_register); |
| 1727 | |
| 1728 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1729 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1730 | { |
| 1731 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1732 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1733 | |
| 1734 | if (!bi_mddev) /* nothing to do */ |
| 1735 | return; |
| 1736 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1737 | return; |
| 1738 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1739 | rdev->bdev->bd_disk) >= 0) |
| 1740 | return; |
| 1741 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1742 | blk_integrity_unregister(mddev->gendisk); |
| 1743 | } |
| 1744 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1747 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1748 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1749 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1750 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1751 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
| 1753 | if (rdev->mddev) { |
| 1754 | MD_BUG(); |
| 1755 | return -EINVAL; |
| 1756 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1757 | |
| 1758 | /* prevent duplicates */ |
| 1759 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1760 | return -EEXIST; |
| 1761 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1762 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1763 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1764 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1765 | if (mddev->pers) { |
| 1766 | /* Cannot change size, so fail |
| 1767 | * If mddev->level <= 0, then we don't care |
| 1768 | * about aligning sizes (e.g. linear) |
| 1769 | */ |
| 1770 | if (mddev->level > 0) |
| 1771 | return -ENOSPC; |
| 1772 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1773 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
| 1776 | /* Verify rdev->desc_nr is unique. |
| 1777 | * If it is -1, assign a free number, else |
| 1778 | * check number is not in use |
| 1779 | */ |
| 1780 | if (rdev->desc_nr < 0) { |
| 1781 | int choice = 0; |
| 1782 | if (mddev->pers) choice = mddev->raid_disks; |
| 1783 | while (find_rdev_nr(mddev, choice)) |
| 1784 | choice++; |
| 1785 | rdev->desc_nr = choice; |
| 1786 | } else { |
| 1787 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1788 | return -EBUSY; |
| 1789 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1790 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1791 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1792 | mdname(mddev), mddev->max_disks); |
| 1793 | return -EBUSY; |
| 1794 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1795 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1796 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1797 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1800 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1801 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1802 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1803 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1804 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1805 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 1806 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 1807 | /* failure here is OK */; |
| 1808 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1809 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1810 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1811 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1812 | |
| 1813 | /* May as well allow recovery to be retried once */ |
| 1814 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1817 | |
| 1818 | fail: |
| 1819 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1820 | b, mdname(mddev)); |
| 1821 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1824 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1825 | { |
| 1826 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1827 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1828 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1832 | { |
| 1833 | char b[BDEVNAME_SIZE]; |
| 1834 | if (!rdev->mddev) { |
| 1835 | MD_BUG(); |
| 1836 | return; |
| 1837 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1838 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1839 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1841 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1842 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1843 | sysfs_put(rdev->sysfs_state); |
| 1844 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1845 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1846 | * writing to 'remove' to "dev/state". We also need |
| 1847 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1848 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1849 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1850 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1851 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 1852 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | /* |
| 1856 | * prevent the device from being mounted, repartitioned or |
| 1857 | * otherwise reused by a RAID array (or any other kernel |
| 1858 | * subsystem), by bd_claiming the device. |
| 1859 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1860 | 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] | 1861 | { |
| 1862 | int err = 0; |
| 1863 | struct block_device *bdev; |
| 1864 | char b[BDEVNAME_SIZE]; |
| 1865 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1866 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | if (IS_ERR(bdev)) { |
| 1868 | printk(KERN_ERR "md: could not open %s.\n", |
| 1869 | __bdevname(dev, b)); |
| 1870 | return PTR_ERR(bdev); |
| 1871 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1872 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | if (err) { |
| 1874 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1875 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1876 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | return err; |
| 1878 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1879 | if (!shared) |
| 1880 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | rdev->bdev = bdev; |
| 1882 | return err; |
| 1883 | } |
| 1884 | |
| 1885 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1886 | { |
| 1887 | struct block_device *bdev = rdev->bdev; |
| 1888 | rdev->bdev = NULL; |
| 1889 | if (!bdev) |
| 1890 | MD_BUG(); |
| 1891 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1892 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | void md_autodetect_dev(dev_t dev); |
| 1896 | |
| 1897 | static void export_rdev(mdk_rdev_t * rdev) |
| 1898 | { |
| 1899 | char b[BDEVNAME_SIZE]; |
| 1900 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1901 | bdevname(rdev->bdev,b)); |
| 1902 | if (rdev->mddev) |
| 1903 | MD_BUG(); |
| 1904 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1906 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1907 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | #endif |
| 1909 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1910 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1914 | { |
| 1915 | unbind_rdev_from_array(rdev); |
| 1916 | export_rdev(rdev); |
| 1917 | } |
| 1918 | |
| 1919 | static void export_array(mddev_t *mddev) |
| 1920 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1921 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1923 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | if (!rdev->mddev) { |
| 1925 | MD_BUG(); |
| 1926 | continue; |
| 1927 | } |
| 1928 | kick_rdev_from_array(rdev); |
| 1929 | } |
| 1930 | if (!list_empty(&mddev->disks)) |
| 1931 | MD_BUG(); |
| 1932 | mddev->raid_disks = 0; |
| 1933 | mddev->major_version = 0; |
| 1934 | } |
| 1935 | |
| 1936 | static void print_desc(mdp_disk_t *desc) |
| 1937 | { |
| 1938 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1939 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1940 | } |
| 1941 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1942 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | { |
| 1944 | int i; |
| 1945 | |
| 1946 | printk(KERN_INFO |
| 1947 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1948 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1949 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1950 | sb->ctime); |
| 1951 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1952 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1953 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1954 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1955 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1956 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1957 | sb->failed_disks, sb->spare_disks, |
| 1958 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1959 | |
| 1960 | printk(KERN_INFO); |
| 1961 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1962 | mdp_disk_t *desc; |
| 1963 | |
| 1964 | desc = sb->disks + i; |
| 1965 | if (desc->number || desc->major || desc->minor || |
| 1966 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1967 | printk(" D %2d: ", i); |
| 1968 | print_desc(desc); |
| 1969 | } |
| 1970 | } |
| 1971 | printk(KERN_INFO "md: THIS: "); |
| 1972 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1975 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1976 | { |
| 1977 | __u8 *uuid; |
| 1978 | |
| 1979 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1980 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1981 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1982 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1983 | le32_to_cpu(sb->major_version), |
| 1984 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1985 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1986 | sb->set_name, |
| 1987 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1988 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1989 | |
| 1990 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1991 | printk(KERN_INFO |
| 1992 | "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] | 1993 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1994 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1995 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1996 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1997 | le32_to_cpu(sb->level), |
| 1998 | (unsigned long long)le64_to_cpu(sb->size), |
| 1999 | le32_to_cpu(sb->raid_disks), |
| 2000 | le32_to_cpu(sb->layout), |
| 2001 | le32_to_cpu(sb->chunksize), |
| 2002 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2003 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2004 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2005 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2006 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2007 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2008 | sb->devflags, |
| 2009 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2010 | (unsigned long long)le64_to_cpu(sb->events), |
| 2011 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2012 | le32_to_cpu(sb->sb_csum), |
| 2013 | le32_to_cpu(sb->max_dev) |
| 2014 | ); |
| 2015 | } |
| 2016 | |
| 2017 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | { |
| 2019 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2020 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2021 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2022 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2023 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2025 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2026 | switch (major_version) { |
| 2027 | case 0: |
| 2028 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2029 | break; |
| 2030 | case 1: |
| 2031 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2032 | break; |
| 2033 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | } else |
| 2035 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2036 | } |
| 2037 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2038 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2040 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | mdk_rdev_t *rdev; |
| 2042 | mddev_t *mddev; |
| 2043 | char b[BDEVNAME_SIZE]; |
| 2044 | |
| 2045 | printk("\n"); |
| 2046 | printk("md: **********************************\n"); |
| 2047 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2048 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2049 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2051 | if (mddev->bitmap) |
| 2052 | bitmap_print_sb(mddev->bitmap); |
| 2053 | else |
| 2054 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2055 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2057 | printk("\n"); |
| 2058 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2059 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2060 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | } |
| 2062 | printk("md: **********************************\n"); |
| 2063 | printk("\n"); |
| 2064 | } |
| 2065 | |
| 2066 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2067 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2069 | /* Update each superblock (in-memory image), but |
| 2070 | * if we are allowed to, skip spares which already |
| 2071 | * have the right event counter, or have one earlier |
| 2072 | * (which would mean they aren't being marked as dirty |
| 2073 | * with the rest of the array) |
| 2074 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | mdk_rdev_t *rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2076 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2077 | if (rdev->sb_events == mddev->events || |
| 2078 | (nospares && |
| 2079 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2080 | rdev->sb_events+1 == mddev->events)) { |
| 2081 | /* Don't update this superblock */ |
| 2082 | rdev->sb_loaded = 2; |
| 2083 | } else { |
| 2084 | super_types[mddev->major_version]. |
| 2085 | sync_super(mddev, rdev); |
| 2086 | rdev->sb_loaded = 1; |
| 2087 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | } |
| 2089 | } |
| 2090 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2091 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2094 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2095 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2098 | /* First make sure individual recovery_offsets are correct */ |
| 2099 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2100 | if (rdev->raid_disk >= 0 && |
| 2101 | mddev->delta_disks >= 0 && |
| 2102 | !test_bit(In_sync, &rdev->flags) && |
| 2103 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2104 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2105 | |
| 2106 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2107 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2108 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2109 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2110 | if (!mddev->external) |
| 2111 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2112 | wake_up(&mddev->sb_wait); |
| 2113 | return; |
| 2114 | } |
| 2115 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2116 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2117 | |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2118 | mddev->utime = get_seconds(); |
| 2119 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2120 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2121 | force_change = 1; |
| 2122 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2123 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2124 | * though if events isn't the right even/odd, we will have to do |
| 2125 | * spares after all |
| 2126 | */ |
| 2127 | nospares = 1; |
| 2128 | if (force_change) |
| 2129 | nospares = 0; |
| 2130 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2131 | /* If the array is degraded, then skipping spares is both |
| 2132 | * dangerous and fairly pointless. |
| 2133 | * Dangerous because a device that was removed from the array |
| 2134 | * might have a event_count that still looks up-to-date, |
| 2135 | * so it can be re-added without a resync. |
| 2136 | * Pointless because if there are any spares to skip, |
| 2137 | * then a recovery will happen and soon that array won't |
| 2138 | * be degraded any more and the spare can go back to sleep then. |
| 2139 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2140 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2141 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2142 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2143 | |
| 2144 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2145 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2146 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2147 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2148 | && mddev->can_decrease_events |
| 2149 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2150 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2151 | mddev->can_decrease_events = 0; |
| 2152 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2153 | /* otherwise we have to go forward and ... */ |
| 2154 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2155 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | |
| 2158 | if (!mddev->events) { |
| 2159 | /* |
| 2160 | * oops, this 64-bit counter should never wrap. |
| 2161 | * Either we are in around ~1 trillion A.C., assuming |
| 2162 | * 1 reboot per second, or we have a bug: |
| 2163 | */ |
| 2164 | MD_BUG(); |
| 2165 | mddev->events --; |
| 2166 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2167 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2168 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | |
| 2170 | dprintk(KERN_INFO |
| 2171 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2172 | mdname(mddev),mddev->in_sync); |
| 2173 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2174 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2175 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | char b[BDEVNAME_SIZE]; |
| 2177 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2178 | if (rdev->sb_loaded != 1) |
| 2179 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2180 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | dprintk("(skipping faulty "); |
| 2182 | |
| 2183 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2184 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2185 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2186 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2187 | rdev->sb_page); |
| 2188 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2189 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2190 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2191 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | } else |
| 2194 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2195 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | /* only need to write one superblock... */ |
| 2197 | break; |
| 2198 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2199 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2200 | /* 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] | 2201 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2202 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2203 | if (mddev->in_sync != sync_req || |
| 2204 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2205 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2206 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2207 | goto repeat; |
| 2208 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2209 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2210 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2211 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2212 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2213 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | } |
| 2216 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2217 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2218 | * We want to accept with case. For this we use cmd_match. |
| 2219 | */ |
| 2220 | static int cmd_match(const char *cmd, const char *str) |
| 2221 | { |
| 2222 | /* See if cmd, written into a sysfs file, matches |
| 2223 | * str. They must either be the same, or cmd can |
| 2224 | * have a trailing newline |
| 2225 | */ |
| 2226 | while (*cmd && *str && *cmd == *str) { |
| 2227 | cmd++; |
| 2228 | str++; |
| 2229 | } |
| 2230 | if (*cmd == '\n') |
| 2231 | cmd++; |
| 2232 | if (*str || *cmd) |
| 2233 | return 0; |
| 2234 | return 1; |
| 2235 | } |
| 2236 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2237 | struct rdev_sysfs_entry { |
| 2238 | struct attribute attr; |
| 2239 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2240 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2241 | }; |
| 2242 | |
| 2243 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2244 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2245 | { |
| 2246 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2247 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2248 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2249 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2250 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2251 | sep = ","; |
| 2252 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2253 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2254 | len += sprintf(page+len, "%sin_sync",sep); |
| 2255 | sep = ","; |
| 2256 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2257 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2258 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2259 | sep = ","; |
| 2260 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2261 | if (test_bit(Blocked, &rdev->flags)) { |
| 2262 | len += sprintf(page+len, "%sblocked", sep); |
| 2263 | sep = ","; |
| 2264 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2265 | if (!test_bit(Faulty, &rdev->flags) && |
| 2266 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2267 | len += sprintf(page+len, "%sspare", sep); |
| 2268 | sep = ","; |
| 2269 | } |
| 2270 | return len+sprintf(page+len, "\n"); |
| 2271 | } |
| 2272 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2273 | static ssize_t |
| 2274 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2275 | { |
| 2276 | /* can write |
| 2277 | * faulty - simulates and error |
| 2278 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2279 | * writemostly - sets write_mostly |
| 2280 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2281 | * blocked - sets the Blocked flag |
| 2282 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2283 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2284 | */ |
| 2285 | int err = -EINVAL; |
| 2286 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2287 | md_error(rdev->mddev, rdev); |
| 2288 | err = 0; |
| 2289 | } else if (cmd_match(buf, "remove")) { |
| 2290 | if (rdev->raid_disk >= 0) |
| 2291 | err = -EBUSY; |
| 2292 | else { |
| 2293 | mddev_t *mddev = rdev->mddev; |
| 2294 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2295 | if (mddev->pers) |
| 2296 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2297 | md_new_event(mddev); |
| 2298 | err = 0; |
| 2299 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2300 | } else if (cmd_match(buf, "writemostly")) { |
| 2301 | set_bit(WriteMostly, &rdev->flags); |
| 2302 | err = 0; |
| 2303 | } else if (cmd_match(buf, "-writemostly")) { |
| 2304 | clear_bit(WriteMostly, &rdev->flags); |
| 2305 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2306 | } else if (cmd_match(buf, "blocked")) { |
| 2307 | set_bit(Blocked, &rdev->flags); |
| 2308 | err = 0; |
| 2309 | } else if (cmd_match(buf, "-blocked")) { |
| 2310 | clear_bit(Blocked, &rdev->flags); |
| 2311 | wake_up(&rdev->blocked_wait); |
| 2312 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2313 | md_wakeup_thread(rdev->mddev->thread); |
| 2314 | |
| 2315 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2316 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2317 | set_bit(In_sync, &rdev->flags); |
| 2318 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2319 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2320 | if (!err) |
| 2321 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2322 | return err ? err : len; |
| 2323 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2324 | static struct rdev_sysfs_entry rdev_state = |
| 2325 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2326 | |
| 2327 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2328 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2329 | { |
| 2330 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2331 | } |
| 2332 | |
| 2333 | static ssize_t |
| 2334 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2335 | { |
| 2336 | char *e; |
| 2337 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2338 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2339 | atomic_set(&rdev->corrected_errors, n); |
| 2340 | return len; |
| 2341 | } |
| 2342 | return -EINVAL; |
| 2343 | } |
| 2344 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2345 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2346 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2347 | static ssize_t |
| 2348 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2349 | { |
| 2350 | if (rdev->raid_disk < 0) |
| 2351 | return sprintf(page, "none\n"); |
| 2352 | else |
| 2353 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2354 | } |
| 2355 | |
| 2356 | static ssize_t |
| 2357 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2358 | { |
| 2359 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2360 | int err; |
| 2361 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2362 | int slot = simple_strtoul(buf, &e, 10); |
| 2363 | if (strncmp(buf, "none", 4)==0) |
| 2364 | slot = -1; |
| 2365 | else if (e==buf || (*e && *e!= '\n')) |
| 2366 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2367 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2368 | /* Setting 'slot' on an active array requires also |
| 2369 | * updating the 'rd%d' link, and communicating |
| 2370 | * with the personality with ->hot_*_disk. |
| 2371 | * For now we only support removing |
| 2372 | * failed/spare devices. This normally happens automatically, |
| 2373 | * but not when the metadata is externally managed. |
| 2374 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2375 | if (rdev->raid_disk == -1) |
| 2376 | return -EEXIST; |
| 2377 | /* personality does all needed checks */ |
| 2378 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2379 | return -EINVAL; |
| 2380 | err = rdev->mddev->pers-> |
| 2381 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2382 | if (err) |
| 2383 | return err; |
| 2384 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2385 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2386 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2387 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2388 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2389 | } else if (rdev->mddev->pers) { |
| 2390 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2391 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2392 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2393 | */ |
| 2394 | |
| 2395 | if (rdev->raid_disk != -1) |
| 2396 | return -EBUSY; |
| 2397 | |
| 2398 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2399 | return -EINVAL; |
| 2400 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2401 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2402 | if (rdev2->raid_disk == slot) |
| 2403 | return -EEXIST; |
| 2404 | |
| 2405 | rdev->raid_disk = slot; |
| 2406 | if (test_bit(In_sync, &rdev->flags)) |
| 2407 | rdev->saved_raid_disk = slot; |
| 2408 | else |
| 2409 | rdev->saved_raid_disk = -1; |
| 2410 | err = rdev->mddev->pers-> |
| 2411 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2412 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2413 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2414 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2415 | } else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2416 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2417 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2418 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2419 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2420 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2421 | } else { |
| 2422 | if (slot >= rdev->mddev->raid_disks) |
| 2423 | return -ENOSPC; |
| 2424 | rdev->raid_disk = slot; |
| 2425 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2426 | clear_bit(Faulty, &rdev->flags); |
| 2427 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2428 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2429 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2430 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2431 | return len; |
| 2432 | } |
| 2433 | |
| 2434 | |
| 2435 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2436 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2437 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2438 | static ssize_t |
| 2439 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2440 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2441 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | static ssize_t |
| 2445 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2446 | { |
| 2447 | char *e; |
| 2448 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2449 | if (e==buf || (*e && *e != '\n')) |
| 2450 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2451 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2452 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2453 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2454 | /* Must set offset before size, so overlap checks |
| 2455 | * can be sane */ |
| 2456 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2457 | rdev->data_offset = offset; |
| 2458 | return len; |
| 2459 | } |
| 2460 | |
| 2461 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2462 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2463 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2464 | static ssize_t |
| 2465 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2466 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2467 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2468 | } |
| 2469 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2470 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2471 | { |
| 2472 | /* check if two start/length pairs overlap */ |
| 2473 | if (s1+l1 <= s2) |
| 2474 | return 0; |
| 2475 | if (s2+l2 <= s1) |
| 2476 | return 0; |
| 2477 | return 1; |
| 2478 | } |
| 2479 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2480 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2481 | { |
| 2482 | unsigned long long blocks; |
| 2483 | sector_t new; |
| 2484 | |
| 2485 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2486 | return -EINVAL; |
| 2487 | |
| 2488 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2489 | return -EINVAL; /* sector conversion overflow */ |
| 2490 | |
| 2491 | new = blocks * 2; |
| 2492 | if (new != blocks * 2) |
| 2493 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2494 | |
| 2495 | *sectors = new; |
| 2496 | return 0; |
| 2497 | } |
| 2498 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2499 | static ssize_t |
| 2500 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2501 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2502 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2503 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2504 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2505 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2506 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2507 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2508 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2509 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2510 | sectors = super_types[my_mddev->major_version]. |
| 2511 | rdev_size_change(rdev, sectors); |
| 2512 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2513 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2514 | } else if (!sectors) |
| 2515 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2516 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2517 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2518 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2519 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2520 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2521 | rdev->sectors = sectors; |
| 2522 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2523 | /* need to check that all other rdevs with the same ->bdev |
| 2524 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2525 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2526 | * we have to change it back, we will have the lock again. |
| 2527 | */ |
| 2528 | mddev_t *mddev; |
| 2529 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2530 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2531 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2532 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2533 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2534 | mdk_rdev_t *rdev2; |
| 2535 | |
| 2536 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2537 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2538 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2539 | (rdev->bdev == rdev2->bdev && |
| 2540 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2541 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2542 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2543 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2544 | overlap = 1; |
| 2545 | break; |
| 2546 | } |
| 2547 | mddev_unlock(mddev); |
| 2548 | if (overlap) { |
| 2549 | mddev_put(mddev); |
| 2550 | break; |
| 2551 | } |
| 2552 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2553 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2554 | if (overlap) { |
| 2555 | /* Someone else could have slipped in a size |
| 2556 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2557 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2558 | * safe, and trust userspace not to race with |
| 2559 | * itself |
| 2560 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2561 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2562 | return -EBUSY; |
| 2563 | } |
| 2564 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2565 | return len; |
| 2566 | } |
| 2567 | |
| 2568 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2569 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2570 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2571 | |
| 2572 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2573 | { |
| 2574 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2575 | |
| 2576 | if (test_bit(In_sync, &rdev->flags) || |
| 2577 | recovery_start == MaxSector) |
| 2578 | return sprintf(page, "none\n"); |
| 2579 | |
| 2580 | return sprintf(page, "%llu\n", recovery_start); |
| 2581 | } |
| 2582 | |
| 2583 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2584 | { |
| 2585 | unsigned long long recovery_start; |
| 2586 | |
| 2587 | if (cmd_match(buf, "none")) |
| 2588 | recovery_start = MaxSector; |
| 2589 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2590 | return -EINVAL; |
| 2591 | |
| 2592 | if (rdev->mddev->pers && |
| 2593 | rdev->raid_disk >= 0) |
| 2594 | return -EBUSY; |
| 2595 | |
| 2596 | rdev->recovery_offset = recovery_start; |
| 2597 | if (recovery_start == MaxSector) |
| 2598 | set_bit(In_sync, &rdev->flags); |
| 2599 | else |
| 2600 | clear_bit(In_sync, &rdev->flags); |
| 2601 | return len; |
| 2602 | } |
| 2603 | |
| 2604 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2605 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2606 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2607 | static struct attribute *rdev_default_attrs[] = { |
| 2608 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2609 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2610 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2611 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2612 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2613 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2614 | NULL, |
| 2615 | }; |
| 2616 | static ssize_t |
| 2617 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2618 | { |
| 2619 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2620 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2621 | mddev_t *mddev = rdev->mddev; |
| 2622 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2623 | |
| 2624 | if (!entry->show) |
| 2625 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2626 | |
| 2627 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2628 | if (!rv) { |
| 2629 | if (rdev->mddev == NULL) |
| 2630 | rv = -EBUSY; |
| 2631 | else |
| 2632 | rv = entry->show(rdev, page); |
| 2633 | mddev_unlock(mddev); |
| 2634 | } |
| 2635 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | static ssize_t |
| 2639 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2640 | const char *page, size_t length) |
| 2641 | { |
| 2642 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2643 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2644 | ssize_t rv; |
| 2645 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2646 | |
| 2647 | if (!entry->store) |
| 2648 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2649 | if (!capable(CAP_SYS_ADMIN)) |
| 2650 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2651 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2652 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2653 | if (rdev->mddev == NULL) |
| 2654 | rv = -EBUSY; |
| 2655 | else |
| 2656 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2657 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2658 | } |
| 2659 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | static void rdev_free(struct kobject *ko) |
| 2663 | { |
| 2664 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2665 | kfree(rdev); |
| 2666 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2667 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2668 | .show = rdev_attr_show, |
| 2669 | .store = rdev_attr_store, |
| 2670 | }; |
| 2671 | static struct kobj_type rdev_ktype = { |
| 2672 | .release = rdev_free, |
| 2673 | .sysfs_ops = &rdev_sysfs_ops, |
| 2674 | .default_attrs = rdev_default_attrs, |
| 2675 | }; |
| 2676 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2677 | void md_rdev_init(mdk_rdev_t *rdev) |
| 2678 | { |
| 2679 | rdev->desc_nr = -1; |
| 2680 | rdev->saved_raid_disk = -1; |
| 2681 | rdev->raid_disk = -1; |
| 2682 | rdev->flags = 0; |
| 2683 | rdev->data_offset = 0; |
| 2684 | rdev->sb_events = 0; |
| 2685 | rdev->last_read_error.tv_sec = 0; |
| 2686 | rdev->last_read_error.tv_nsec = 0; |
| 2687 | atomic_set(&rdev->nr_pending, 0); |
| 2688 | atomic_set(&rdev->read_errors, 0); |
| 2689 | atomic_set(&rdev->corrected_errors, 0); |
| 2690 | |
| 2691 | INIT_LIST_HEAD(&rdev->same_set); |
| 2692 | init_waitqueue_head(&rdev->blocked_wait); |
| 2693 | } |
| 2694 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | /* |
| 2696 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2697 | * |
| 2698 | * mark the device faulty if: |
| 2699 | * |
| 2700 | * - the device is nonexistent (zero size) |
| 2701 | * - the device has no valid superblock |
| 2702 | * |
| 2703 | * a faulty rdev _never_ has rdev->sb set. |
| 2704 | */ |
| 2705 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2706 | { |
| 2707 | char b[BDEVNAME_SIZE]; |
| 2708 | int err; |
| 2709 | mdk_rdev_t *rdev; |
| 2710 | sector_t size; |
| 2711 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2712 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | if (!rdev) { |
| 2714 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2715 | return ERR_PTR(-ENOMEM); |
| 2716 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 2718 | md_rdev_init(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | if ((err = alloc_disk_sb(rdev))) |
| 2720 | goto abort_free; |
| 2721 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2722 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | if (err) |
| 2724 | goto abort_free; |
| 2725 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2726 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2729 | if (!size) { |
| 2730 | printk(KERN_WARNING |
| 2731 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2732 | bdevname(rdev->bdev,b)); |
| 2733 | err = -EINVAL; |
| 2734 | goto abort_free; |
| 2735 | } |
| 2736 | |
| 2737 | if (super_format >= 0) { |
| 2738 | err = super_types[super_format]. |
| 2739 | load_super(rdev, NULL, super_minor); |
| 2740 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2741 | printk(KERN_WARNING |
| 2742 | "md: %s does not have a valid v%d.%d " |
| 2743 | "superblock, not importing!\n", |
| 2744 | bdevname(rdev->bdev,b), |
| 2745 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | goto abort_free; |
| 2747 | } |
| 2748 | if (err < 0) { |
| 2749 | printk(KERN_WARNING |
| 2750 | "md: could not read %s's sb, not importing!\n", |
| 2751 | bdevname(rdev->bdev,b)); |
| 2752 | goto abort_free; |
| 2753 | } |
| 2754 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | return rdev; |
| 2757 | |
| 2758 | abort_free: |
| 2759 | if (rdev->sb_page) { |
| 2760 | if (rdev->bdev) |
| 2761 | unlock_rdev(rdev); |
| 2762 | free_disk_sb(rdev); |
| 2763 | } |
| 2764 | kfree(rdev); |
| 2765 | return ERR_PTR(err); |
| 2766 | } |
| 2767 | |
| 2768 | /* |
| 2769 | * Check a full RAID array for plausibility |
| 2770 | */ |
| 2771 | |
| 2772 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2773 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | { |
| 2775 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2776 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | char b[BDEVNAME_SIZE]; |
| 2778 | |
| 2779 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2780 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | switch (super_types[mddev->major_version]. |
| 2782 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2783 | case 1: |
| 2784 | freshest = rdev; |
| 2785 | break; |
| 2786 | case 0: |
| 2787 | break; |
| 2788 | default: |
| 2789 | printk( KERN_ERR \ |
| 2790 | "md: fatal superblock inconsistency in %s" |
| 2791 | " -- removing from array\n", |
| 2792 | bdevname(rdev->bdev,b)); |
| 2793 | kick_rdev_from_array(rdev); |
| 2794 | } |
| 2795 | |
| 2796 | |
| 2797 | super_types[mddev->major_version]. |
| 2798 | validate_super(mddev, freshest); |
| 2799 | |
| 2800 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2801 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2802 | if (mddev->max_disks && |
| 2803 | (rdev->desc_nr >= mddev->max_disks || |
| 2804 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2805 | printk(KERN_WARNING |
| 2806 | "md: %s: %s: only %d devices permitted\n", |
| 2807 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2808 | mddev->max_disks); |
| 2809 | kick_rdev_from_array(rdev); |
| 2810 | continue; |
| 2811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | if (rdev != freshest) |
| 2813 | if (super_types[mddev->major_version]. |
| 2814 | validate_super(mddev, rdev)) { |
| 2815 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2816 | " from array!\n", |
| 2817 | bdevname(rdev->bdev,b)); |
| 2818 | kick_rdev_from_array(rdev); |
| 2819 | continue; |
| 2820 | } |
| 2821 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2822 | rdev->desc_nr = i++; |
| 2823 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2824 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2825 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2826 | rdev->raid_disk = -1; |
| 2827 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | } |
| 2829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | } |
| 2831 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2832 | /* Read a fixed-point number. |
| 2833 | * Numbers in sysfs attributes should be in "standard" units where |
| 2834 | * possible, so time should be in seconds. |
| 2835 | * However we internally use a a much smaller unit such as |
| 2836 | * milliseconds or jiffies. |
| 2837 | * This function takes a decimal number with a possible fractional |
| 2838 | * component, and produces an integer which is the result of |
| 2839 | * multiplying that number by 10^'scale'. |
| 2840 | * all without any floating-point arithmetic. |
| 2841 | */ |
| 2842 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2843 | { |
| 2844 | unsigned long result = 0; |
| 2845 | long decimals = -1; |
| 2846 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2847 | if (*cp == '.') |
| 2848 | decimals = 0; |
| 2849 | else if (decimals < scale) { |
| 2850 | unsigned int value; |
| 2851 | value = *cp - '0'; |
| 2852 | result = result * 10 + value; |
| 2853 | if (decimals >= 0) |
| 2854 | decimals++; |
| 2855 | } |
| 2856 | cp++; |
| 2857 | } |
| 2858 | if (*cp == '\n') |
| 2859 | cp++; |
| 2860 | if (*cp) |
| 2861 | return -EINVAL; |
| 2862 | if (decimals < 0) |
| 2863 | decimals = 0; |
| 2864 | while (decimals < scale) { |
| 2865 | result *= 10; |
| 2866 | decimals ++; |
| 2867 | } |
| 2868 | *res = result; |
| 2869 | return 0; |
| 2870 | } |
| 2871 | |
| 2872 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2873 | static void md_safemode_timeout(unsigned long data); |
| 2874 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2875 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2876 | safe_delay_show(mddev_t *mddev, char *page) |
| 2877 | { |
| 2878 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2879 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2880 | } |
| 2881 | static ssize_t |
| 2882 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2883 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2884 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2885 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2886 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2887 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2888 | if (msec == 0) |
| 2889 | mddev->safemode_delay = 0; |
| 2890 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2891 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2892 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2893 | if (mddev->safemode_delay == 0) |
| 2894 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2895 | if (mddev->safemode_delay < old_delay) |
| 2896 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2897 | } |
| 2898 | return len; |
| 2899 | } |
| 2900 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2901 | __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] | 2902 | |
| 2903 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2904 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2905 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2906 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2907 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2908 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2909 | else if (mddev->clevel[0]) |
| 2910 | return sprintf(page, "%s\n", mddev->clevel); |
| 2911 | else if (mddev->level != LEVEL_NONE) |
| 2912 | return sprintf(page, "%d\n", mddev->level); |
| 2913 | else |
| 2914 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2915 | } |
| 2916 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2917 | static ssize_t |
| 2918 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2919 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2920 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2921 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2922 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2923 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2924 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2925 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2926 | |
| 2927 | if (mddev->pers == NULL) { |
| 2928 | if (len == 0) |
| 2929 | return 0; |
| 2930 | if (len >= sizeof(mddev->clevel)) |
| 2931 | return -ENOSPC; |
| 2932 | strncpy(mddev->clevel, buf, len); |
| 2933 | if (mddev->clevel[len-1] == '\n') |
| 2934 | len--; |
| 2935 | mddev->clevel[len] = 0; |
| 2936 | mddev->level = LEVEL_NONE; |
| 2937 | return rv; |
| 2938 | } |
| 2939 | |
| 2940 | /* request to change the personality. Need to ensure: |
| 2941 | * - array is not engaged in resync/recovery/reshape |
| 2942 | * - old personality can be suspended |
| 2943 | * - new personality will access other array. |
| 2944 | */ |
| 2945 | |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 2946 | if (mddev->sync_thread || |
| 2947 | mddev->reshape_position != MaxSector || |
| 2948 | mddev->sysfs_active) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2949 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2950 | |
| 2951 | if (!mddev->pers->quiesce) { |
| 2952 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2953 | mdname(mddev), mddev->pers->name); |
| 2954 | return -EINVAL; |
| 2955 | } |
| 2956 | |
| 2957 | /* Now find the new personality */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2958 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2959 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2960 | strncpy(clevel, buf, len); |
| 2961 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2962 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2963 | clevel[len] = 0; |
| 2964 | if (strict_strtol(clevel, 10, &level)) |
| 2965 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2966 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2967 | if (request_module("md-%s", clevel) != 0) |
| 2968 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2969 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2970 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2971 | if (!pers || !try_module_get(pers->owner)) { |
| 2972 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2973 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2974 | return -EINVAL; |
| 2975 | } |
| 2976 | spin_unlock(&pers_lock); |
| 2977 | |
| 2978 | if (pers == mddev->pers) { |
| 2979 | /* Nothing to do! */ |
| 2980 | module_put(pers->owner); |
| 2981 | return rv; |
| 2982 | } |
| 2983 | if (!pers->takeover) { |
| 2984 | module_put(pers->owner); |
| 2985 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2986 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2987 | return -EINVAL; |
| 2988 | } |
| 2989 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 2990 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2991 | rdev->new_raid_disk = rdev->raid_disk; |
| 2992 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2993 | /* ->takeover must set new_* and/or delta_disks |
| 2994 | * if it succeeds, and may set them when it fails. |
| 2995 | */ |
| 2996 | priv = pers->takeover(mddev); |
| 2997 | if (IS_ERR(priv)) { |
| 2998 | mddev->new_level = mddev->level; |
| 2999 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3000 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3001 | mddev->raid_disks -= mddev->delta_disks; |
| 3002 | mddev->delta_disks = 0; |
| 3003 | module_put(pers->owner); |
| 3004 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3005 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3006 | return PTR_ERR(priv); |
| 3007 | } |
| 3008 | |
| 3009 | /* Looks like we have a winner */ |
| 3010 | mddev_suspend(mddev); |
| 3011 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3012 | |
| 3013 | if (mddev->pers->sync_request == NULL && |
| 3014 | pers->sync_request != NULL) { |
| 3015 | /* need to add the md_redundancy_group */ |
| 3016 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3017 | printk(KERN_WARNING |
| 3018 | "md: cannot register extra attributes for %s\n", |
| 3019 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3020 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3021 | } |
| 3022 | if (mddev->pers->sync_request != NULL && |
| 3023 | pers->sync_request == NULL) { |
| 3024 | /* need to remove the md_redundancy_group */ |
| 3025 | if (mddev->to_remove == NULL) |
| 3026 | mddev->to_remove = &md_redundancy_group; |
| 3027 | } |
| 3028 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3029 | if (mddev->pers->sync_request == NULL && |
| 3030 | mddev->external) { |
| 3031 | /* We are converting from a no-redundancy array |
| 3032 | * to a redundancy array and metadata is managed |
| 3033 | * externally so we need to be sure that writes |
| 3034 | * won't block due to a need to transition |
| 3035 | * clean->dirty |
| 3036 | * until external management is started. |
| 3037 | */ |
| 3038 | mddev->in_sync = 0; |
| 3039 | mddev->safemode_delay = 0; |
| 3040 | mddev->safemode = 0; |
| 3041 | } |
| 3042 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3043 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3044 | char nm[20]; |
| 3045 | if (rdev->raid_disk < 0) |
| 3046 | continue; |
| 3047 | if (rdev->new_raid_disk > mddev->raid_disks) |
| 3048 | rdev->new_raid_disk = -1; |
| 3049 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3050 | continue; |
| 3051 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3052 | sysfs_remove_link(&mddev->kobj, nm); |
| 3053 | } |
| 3054 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3055 | if (rdev->raid_disk < 0) |
| 3056 | continue; |
| 3057 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3058 | continue; |
| 3059 | rdev->raid_disk = rdev->new_raid_disk; |
| 3060 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3061 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3062 | else { |
| 3063 | char nm[20]; |
| 3064 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3065 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3066 | printk("md: cannot register %s for %s after level change\n", |
| 3067 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3068 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3072 | mddev->pers = pers; |
| 3073 | mddev->private = priv; |
| 3074 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3075 | mddev->level = mddev->new_level; |
| 3076 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3077 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3078 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3079 | if (mddev->pers->sync_request == NULL) { |
| 3080 | /* this is now an array without redundancy, so |
| 3081 | * it must always be in_sync |
| 3082 | */ |
| 3083 | mddev->in_sync = 1; |
| 3084 | del_timer_sync(&mddev->safemode_timer); |
| 3085 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3086 | pers->run(mddev); |
| 3087 | mddev_resume(mddev); |
| 3088 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3089 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3090 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3091 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3092 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3093 | return rv; |
| 3094 | } |
| 3095 | |
| 3096 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3097 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3098 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3099 | |
| 3100 | static ssize_t |
| 3101 | layout_show(mddev_t *mddev, char *page) |
| 3102 | { |
| 3103 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3104 | if (mddev->reshape_position != MaxSector && |
| 3105 | mddev->layout != mddev->new_layout) |
| 3106 | return sprintf(page, "%d (%d)\n", |
| 3107 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3108 | return sprintf(page, "%d\n", mddev->layout); |
| 3109 | } |
| 3110 | |
| 3111 | static ssize_t |
| 3112 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3113 | { |
| 3114 | char *e; |
| 3115 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3116 | |
| 3117 | if (!*buf || (*e && *e != '\n')) |
| 3118 | return -EINVAL; |
| 3119 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3120 | if (mddev->pers) { |
| 3121 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3122 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3123 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3124 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3125 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3126 | if (err) { |
| 3127 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3128 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3129 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3130 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3131 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3132 | if (mddev->reshape_position == MaxSector) |
| 3133 | mddev->layout = n; |
| 3134 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3135 | return len; |
| 3136 | } |
| 3137 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3138 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3139 | |
| 3140 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3141 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3142 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3143 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3144 | if (mddev->raid_disks == 0) |
| 3145 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3146 | if (mddev->reshape_position != MaxSector && |
| 3147 | mddev->delta_disks != 0) |
| 3148 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3149 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3150 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3151 | } |
| 3152 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3153 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3154 | |
| 3155 | static ssize_t |
| 3156 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3157 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3158 | char *e; |
| 3159 | int rv = 0; |
| 3160 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3161 | |
| 3162 | if (!*buf || (*e && *e != '\n')) |
| 3163 | return -EINVAL; |
| 3164 | |
| 3165 | if (mddev->pers) |
| 3166 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3167 | else if (mddev->reshape_position != MaxSector) { |
| 3168 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3169 | mddev->delta_disks = n - olddisks; |
| 3170 | mddev->raid_disks = n; |
| 3171 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3172 | mddev->raid_disks = n; |
| 3173 | return rv ? rv : len; |
| 3174 | } |
| 3175 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3176 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3177 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3178 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3179 | chunk_size_show(mddev_t *mddev, char *page) |
| 3180 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3181 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3182 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3183 | return sprintf(page, "%d (%d)\n", |
| 3184 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3185 | mddev->chunk_sectors << 9); |
| 3186 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | static ssize_t |
| 3190 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3191 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3192 | char *e; |
| 3193 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3194 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3195 | if (!*buf || (*e && *e != '\n')) |
| 3196 | return -EINVAL; |
| 3197 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3198 | if (mddev->pers) { |
| 3199 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3200 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3201 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3202 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3203 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3204 | if (err) { |
| 3205 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3206 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3207 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3208 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3209 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3210 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3211 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3212 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3213 | return len; |
| 3214 | } |
| 3215 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3216 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3217 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3218 | static ssize_t |
| 3219 | resync_start_show(mddev_t *mddev, char *page) |
| 3220 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3221 | if (mddev->recovery_cp == MaxSector) |
| 3222 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3223 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3224 | } |
| 3225 | |
| 3226 | static ssize_t |
| 3227 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3228 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3229 | char *e; |
| 3230 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3231 | |
| 3232 | if (mddev->pers) |
| 3233 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3234 | if (cmd_match(buf, "none")) |
| 3235 | n = MaxSector; |
| 3236 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3237 | return -EINVAL; |
| 3238 | |
| 3239 | mddev->recovery_cp = n; |
| 3240 | return len; |
| 3241 | } |
| 3242 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3243 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3244 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3245 | /* |
| 3246 | * The array state can be: |
| 3247 | * |
| 3248 | * clear |
| 3249 | * No devices, no size, no level |
| 3250 | * Equivalent to STOP_ARRAY ioctl |
| 3251 | * inactive |
| 3252 | * May have some settings, but array is not active |
| 3253 | * all IO results in error |
| 3254 | * When written, doesn't tear down array, but just stops it |
| 3255 | * suspended (not supported yet) |
| 3256 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3257 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3258 | * readonly |
| 3259 | * no resync can happen. no superblocks get written. |
| 3260 | * write requests fail |
| 3261 | * read-auto |
| 3262 | * like readonly, but behaves like 'clean' on a write request. |
| 3263 | * |
| 3264 | * clean - no pending writes, but otherwise active. |
| 3265 | * When written to inactive array, starts without resync |
| 3266 | * If a write request arrives then |
| 3267 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3268 | * if not known, block and switch to write-pending |
| 3269 | * If written to an active array that has pending writes, then fails. |
| 3270 | * active |
| 3271 | * fully active: IO and resync can be happening. |
| 3272 | * When written to inactive array, starts with resync |
| 3273 | * |
| 3274 | * write-pending |
| 3275 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3276 | * |
| 3277 | * active-idle |
| 3278 | * like active, but no writes have been seen for a while (100msec). |
| 3279 | * |
| 3280 | */ |
| 3281 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3282 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3283 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3284 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3285 | "write-pending", "active-idle", NULL }; |
| 3286 | |
| 3287 | static int match_word(const char *word, char **list) |
| 3288 | { |
| 3289 | int n; |
| 3290 | for (n=0; list[n]; n++) |
| 3291 | if (cmd_match(word, list[n])) |
| 3292 | break; |
| 3293 | return n; |
| 3294 | } |
| 3295 | |
| 3296 | static ssize_t |
| 3297 | array_state_show(mddev_t *mddev, char *page) |
| 3298 | { |
| 3299 | enum array_state st = inactive; |
| 3300 | |
| 3301 | if (mddev->pers) |
| 3302 | switch(mddev->ro) { |
| 3303 | case 1: |
| 3304 | st = readonly; |
| 3305 | break; |
| 3306 | case 2: |
| 3307 | st = read_auto; |
| 3308 | break; |
| 3309 | case 0: |
| 3310 | if (mddev->in_sync) |
| 3311 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3312 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3313 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3314 | else if (mddev->safemode) |
| 3315 | st = active_idle; |
| 3316 | else |
| 3317 | st = active; |
| 3318 | } |
| 3319 | else { |
| 3320 | if (list_empty(&mddev->disks) && |
| 3321 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3322 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3323 | st = clear; |
| 3324 | else |
| 3325 | st = inactive; |
| 3326 | } |
| 3327 | return sprintf(page, "%s\n", array_states[st]); |
| 3328 | } |
| 3329 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3330 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3331 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3332 | static int do_md_run(mddev_t * mddev); |
| 3333 | static int restart_array(mddev_t *mddev); |
| 3334 | |
| 3335 | static ssize_t |
| 3336 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3337 | { |
| 3338 | int err = -EINVAL; |
| 3339 | enum array_state st = match_word(buf, array_states); |
| 3340 | switch(st) { |
| 3341 | case bad_word: |
| 3342 | break; |
| 3343 | case clear: |
| 3344 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3345 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3346 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3347 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3348 | break; |
| 3349 | case inactive: |
| 3350 | /* stopping an active array */ |
| 3351 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3352 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3353 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3354 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3355 | } else |
| 3356 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3357 | break; |
| 3358 | case suspended: |
| 3359 | break; /* not supported yet */ |
| 3360 | case readonly: |
| 3361 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3362 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3363 | else { |
| 3364 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3365 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3366 | err = do_md_run(mddev); |
| 3367 | } |
| 3368 | break; |
| 3369 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3370 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3371 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3372 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3373 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3374 | err = restart_array(mddev); |
| 3375 | if (err == 0) { |
| 3376 | mddev->ro = 2; |
| 3377 | set_disk_ro(mddev->gendisk, 0); |
| 3378 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3379 | } else { |
| 3380 | mddev->ro = 2; |
| 3381 | err = do_md_run(mddev); |
| 3382 | } |
| 3383 | break; |
| 3384 | case clean: |
| 3385 | if (mddev->pers) { |
| 3386 | restart_array(mddev); |
| 3387 | spin_lock_irq(&mddev->write_lock); |
| 3388 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3389 | if (mddev->in_sync == 0) { |
| 3390 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3391 | if (mddev->safemode == 1) |
| 3392 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3393 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3394 | } |
| 3395 | err = 0; |
| 3396 | } else |
| 3397 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3398 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3399 | } else |
| 3400 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3401 | break; |
| 3402 | case active: |
| 3403 | if (mddev->pers) { |
| 3404 | restart_array(mddev); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3405 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3406 | wake_up(&mddev->sb_wait); |
| 3407 | err = 0; |
| 3408 | } else { |
| 3409 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3410 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3411 | err = do_md_run(mddev); |
| 3412 | } |
| 3413 | break; |
| 3414 | case write_pending: |
| 3415 | case active_idle: |
| 3416 | /* these cannot be set */ |
| 3417 | break; |
| 3418 | } |
| 3419 | if (err) |
| 3420 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3421 | else { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3422 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3423 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3424 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3425 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3426 | static struct md_sysfs_entry md_array_state = |
| 3427 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3428 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3429 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3430 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3431 | return sprintf(page, "%d\n", |
| 3432 | atomic_read(&mddev->max_corr_read_errors)); |
| 3433 | } |
| 3434 | |
| 3435 | static ssize_t |
| 3436 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3437 | { |
| 3438 | char *e; |
| 3439 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3440 | |
| 3441 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3442 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3443 | return len; |
| 3444 | } |
| 3445 | return -EINVAL; |
| 3446 | } |
| 3447 | |
| 3448 | static struct md_sysfs_entry max_corr_read_errors = |
| 3449 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3450 | max_corrected_read_errors_store); |
| 3451 | |
| 3452 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3453 | null_show(mddev_t *mddev, char *page) |
| 3454 | { |
| 3455 | return -EINVAL; |
| 3456 | } |
| 3457 | |
| 3458 | static ssize_t |
| 3459 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3460 | { |
| 3461 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3462 | /* The new device is added to the array. |
| 3463 | * If the array has a persistent superblock, we read the |
| 3464 | * superblock to initialise info and check validity. |
| 3465 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3466 | * which mainly checks size. |
| 3467 | */ |
| 3468 | char *e; |
| 3469 | int major = simple_strtoul(buf, &e, 10); |
| 3470 | int minor; |
| 3471 | dev_t dev; |
| 3472 | mdk_rdev_t *rdev; |
| 3473 | int err; |
| 3474 | |
| 3475 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3476 | return -EINVAL; |
| 3477 | minor = simple_strtoul(e+1, &e, 10); |
| 3478 | if (*e && *e != '\n') |
| 3479 | return -EINVAL; |
| 3480 | dev = MKDEV(major, minor); |
| 3481 | if (major != MAJOR(dev) || |
| 3482 | minor != MINOR(dev)) |
| 3483 | return -EOVERFLOW; |
| 3484 | |
| 3485 | |
| 3486 | if (mddev->persistent) { |
| 3487 | rdev = md_import_device(dev, mddev->major_version, |
| 3488 | mddev->minor_version); |
| 3489 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3490 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3491 | mdk_rdev_t, same_set); |
| 3492 | err = super_types[mddev->major_version] |
| 3493 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3494 | if (err < 0) |
| 3495 | goto out; |
| 3496 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3497 | } else if (mddev->external) |
| 3498 | rdev = md_import_device(dev, -2, -1); |
| 3499 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3500 | rdev = md_import_device(dev, -1, -1); |
| 3501 | |
| 3502 | if (IS_ERR(rdev)) |
| 3503 | return PTR_ERR(rdev); |
| 3504 | err = bind_rdev_to_array(rdev, mddev); |
| 3505 | out: |
| 3506 | if (err) |
| 3507 | export_rdev(rdev); |
| 3508 | return err ? err : len; |
| 3509 | } |
| 3510 | |
| 3511 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3512 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3513 | |
| 3514 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3515 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3516 | { |
| 3517 | char *end; |
| 3518 | unsigned long chunk, end_chunk; |
| 3519 | |
| 3520 | if (!mddev->bitmap) |
| 3521 | goto out; |
| 3522 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3523 | while (*buf) { |
| 3524 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3525 | if (buf == end) break; |
| 3526 | if (*end == '-') { /* range */ |
| 3527 | buf = end + 1; |
| 3528 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3529 | if (buf == end) break; |
| 3530 | } |
| 3531 | if (*end && !isspace(*end)) break; |
| 3532 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3533 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3534 | } |
| 3535 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3536 | out: |
| 3537 | return len; |
| 3538 | } |
| 3539 | |
| 3540 | static struct md_sysfs_entry md_bitmap = |
| 3541 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3542 | |
| 3543 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3544 | size_show(mddev_t *mddev, char *page) |
| 3545 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3546 | return sprintf(page, "%llu\n", |
| 3547 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3548 | } |
| 3549 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3550 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3551 | |
| 3552 | static ssize_t |
| 3553 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3554 | { |
| 3555 | /* If array is inactive, we can reduce the component size, but |
| 3556 | * not increase it (except from 0). |
| 3557 | * If array is active, we can try an on-line resize |
| 3558 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3559 | sector_t sectors; |
| 3560 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3561 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3562 | if (err < 0) |
| 3563 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3564 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3565 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3566 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3567 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3568 | if (mddev->dev_sectors == 0 || |
| 3569 | mddev->dev_sectors > sectors) |
| 3570 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3571 | else |
| 3572 | err = -ENOSPC; |
| 3573 | } |
| 3574 | return err ? err : len; |
| 3575 | } |
| 3576 | |
| 3577 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3578 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3579 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3580 | |
| 3581 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3582 | * This is one of |
| 3583 | * 'none' for arrays with no metadata (good luck...) |
| 3584 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3585 | * or N.M for internally known formats |
| 3586 | */ |
| 3587 | static ssize_t |
| 3588 | metadata_show(mddev_t *mddev, char *page) |
| 3589 | { |
| 3590 | if (mddev->persistent) |
| 3591 | return sprintf(page, "%d.%d\n", |
| 3592 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3593 | else if (mddev->external) |
| 3594 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3595 | else |
| 3596 | return sprintf(page, "none\n"); |
| 3597 | } |
| 3598 | |
| 3599 | static ssize_t |
| 3600 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3601 | { |
| 3602 | int major, minor; |
| 3603 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3604 | /* Changing the details of 'external' metadata is |
| 3605 | * always permitted. Otherwise there must be |
| 3606 | * no devices attached to the array. |
| 3607 | */ |
| 3608 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3609 | ; |
| 3610 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3611 | return -EBUSY; |
| 3612 | |
| 3613 | if (cmd_match(buf, "none")) { |
| 3614 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3615 | mddev->external = 0; |
| 3616 | mddev->major_version = 0; |
| 3617 | mddev->minor_version = 90; |
| 3618 | return len; |
| 3619 | } |
| 3620 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3621 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3622 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3623 | namelen = sizeof(mddev->metadata_type)-1; |
| 3624 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3625 | mddev->metadata_type[namelen] = 0; |
| 3626 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3627 | mddev->metadata_type[--namelen] = 0; |
| 3628 | mddev->persistent = 0; |
| 3629 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3630 | mddev->major_version = 0; |
| 3631 | mddev->minor_version = 90; |
| 3632 | return len; |
| 3633 | } |
| 3634 | major = simple_strtoul(buf, &e, 10); |
| 3635 | if (e==buf || *e != '.') |
| 3636 | return -EINVAL; |
| 3637 | buf = e+1; |
| 3638 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3639 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3640 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3641 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3642 | return -ENOENT; |
| 3643 | mddev->major_version = major; |
| 3644 | mddev->minor_version = minor; |
| 3645 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3646 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3647 | return len; |
| 3648 | } |
| 3649 | |
| 3650 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3651 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3652 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3653 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3654 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3655 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3656 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3657 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3658 | type = "frozen"; |
| 3659 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3660 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3661 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3662 | type = "reshape"; |
| 3663 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3664 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3665 | type = "resync"; |
| 3666 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3667 | type = "check"; |
| 3668 | else |
| 3669 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3670 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3671 | type = "recover"; |
| 3672 | } |
| 3673 | return sprintf(page, "%s\n", type); |
| 3674 | } |
| 3675 | |
| 3676 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3677 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3678 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3679 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3680 | return -EINVAL; |
| 3681 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3682 | if (cmd_match(page, "frozen")) |
| 3683 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3684 | else |
| 3685 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3686 | |
| 3687 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3688 | if (mddev->sync_thread) { |
| 3689 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3690 | md_unregister_thread(mddev->sync_thread); |
| 3691 | mddev->sync_thread = NULL; |
| 3692 | mddev->recovery = 0; |
| 3693 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3694 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3695 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3696 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3697 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3698 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3699 | else if (cmd_match(page, "recover")) { |
| 3700 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3701 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3702 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3703 | int err; |
| 3704 | if (mddev->pers->start_reshape == NULL) |
| 3705 | return -EINVAL; |
| 3706 | err = mddev->pers->start_reshape(mddev); |
| 3707 | if (err) |
| 3708 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3709 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3710 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3711 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3712 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3713 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3714 | return -EINVAL; |
| 3715 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3716 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3717 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3718 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3719 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3720 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3721 | return len; |
| 3722 | } |
| 3723 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3724 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3725 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3726 | { |
| 3727 | return sprintf(page, "%llu\n", |
| 3728 | (unsigned long long) mddev->resync_mismatches); |
| 3729 | } |
| 3730 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3731 | static struct md_sysfs_entry md_scan_mode = |
| 3732 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3733 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3734 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3735 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3736 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3737 | static ssize_t |
| 3738 | sync_min_show(mddev_t *mddev, char *page) |
| 3739 | { |
| 3740 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3741 | mddev->sync_speed_min ? "local": "system"); |
| 3742 | } |
| 3743 | |
| 3744 | static ssize_t |
| 3745 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3746 | { |
| 3747 | int min; |
| 3748 | char *e; |
| 3749 | if (strncmp(buf, "system", 6)==0) { |
| 3750 | mddev->sync_speed_min = 0; |
| 3751 | return len; |
| 3752 | } |
| 3753 | min = simple_strtoul(buf, &e, 10); |
| 3754 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3755 | return -EINVAL; |
| 3756 | mddev->sync_speed_min = min; |
| 3757 | return len; |
| 3758 | } |
| 3759 | |
| 3760 | static struct md_sysfs_entry md_sync_min = |
| 3761 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3762 | |
| 3763 | static ssize_t |
| 3764 | sync_max_show(mddev_t *mddev, char *page) |
| 3765 | { |
| 3766 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3767 | mddev->sync_speed_max ? "local": "system"); |
| 3768 | } |
| 3769 | |
| 3770 | static ssize_t |
| 3771 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3772 | { |
| 3773 | int max; |
| 3774 | char *e; |
| 3775 | if (strncmp(buf, "system", 6)==0) { |
| 3776 | mddev->sync_speed_max = 0; |
| 3777 | return len; |
| 3778 | } |
| 3779 | max = simple_strtoul(buf, &e, 10); |
| 3780 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3781 | return -EINVAL; |
| 3782 | mddev->sync_speed_max = max; |
| 3783 | return len; |
| 3784 | } |
| 3785 | |
| 3786 | static struct md_sysfs_entry md_sync_max = |
| 3787 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3788 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3789 | static ssize_t |
| 3790 | degraded_show(mddev_t *mddev, char *page) |
| 3791 | { |
| 3792 | return sprintf(page, "%d\n", mddev->degraded); |
| 3793 | } |
| 3794 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3795 | |
| 3796 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3797 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3798 | { |
| 3799 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3800 | } |
| 3801 | |
| 3802 | static ssize_t |
| 3803 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3804 | { |
| 3805 | long n; |
| 3806 | |
| 3807 | if (strict_strtol(buf, 10, &n)) |
| 3808 | return -EINVAL; |
| 3809 | |
| 3810 | if (n != 0 && n != 1) |
| 3811 | return -EINVAL; |
| 3812 | |
| 3813 | mddev->parallel_resync = n; |
| 3814 | |
| 3815 | if (mddev->sync_thread) |
| 3816 | wake_up(&resync_wait); |
| 3817 | |
| 3818 | return len; |
| 3819 | } |
| 3820 | |
| 3821 | /* force parallel resync, even with shared block devices */ |
| 3822 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3823 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3824 | sync_force_parallel_show, sync_force_parallel_store); |
| 3825 | |
| 3826 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3827 | sync_speed_show(mddev_t *mddev, char *page) |
| 3828 | { |
| 3829 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3830 | if (mddev->curr_resync == 0) |
| 3831 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3832 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3833 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3834 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3835 | db = resync - mddev->resync_mark_cnt; |
| 3836 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3837 | } |
| 3838 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3839 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3840 | |
| 3841 | static ssize_t |
| 3842 | sync_completed_show(mddev_t *mddev, char *page) |
| 3843 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3844 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3845 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3846 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3847 | return sprintf(page, "none\n"); |
| 3848 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3849 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3850 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3851 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3852 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3853 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3854 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3855 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3856 | } |
| 3857 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3858 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3859 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3860 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3861 | min_sync_show(mddev_t *mddev, char *page) |
| 3862 | { |
| 3863 | return sprintf(page, "%llu\n", |
| 3864 | (unsigned long long)mddev->resync_min); |
| 3865 | } |
| 3866 | static ssize_t |
| 3867 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3868 | { |
| 3869 | unsigned long long min; |
| 3870 | if (strict_strtoull(buf, 10, &min)) |
| 3871 | return -EINVAL; |
| 3872 | if (min > mddev->resync_max) |
| 3873 | return -EINVAL; |
| 3874 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3875 | return -EBUSY; |
| 3876 | |
| 3877 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3878 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3879 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3880 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3881 | return -EINVAL; |
| 3882 | } |
| 3883 | mddev->resync_min = min; |
| 3884 | |
| 3885 | return len; |
| 3886 | } |
| 3887 | |
| 3888 | static struct md_sysfs_entry md_min_sync = |
| 3889 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3890 | |
| 3891 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3892 | max_sync_show(mddev_t *mddev, char *page) |
| 3893 | { |
| 3894 | if (mddev->resync_max == MaxSector) |
| 3895 | return sprintf(page, "max\n"); |
| 3896 | else |
| 3897 | return sprintf(page, "%llu\n", |
| 3898 | (unsigned long long)mddev->resync_max); |
| 3899 | } |
| 3900 | static ssize_t |
| 3901 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3902 | { |
| 3903 | if (strncmp(buf, "max", 3) == 0) |
| 3904 | mddev->resync_max = MaxSector; |
| 3905 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3906 | unsigned long long max; |
| 3907 | if (strict_strtoull(buf, 10, &max)) |
| 3908 | return -EINVAL; |
| 3909 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3910 | return -EINVAL; |
| 3911 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3912 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3913 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3914 | return -EBUSY; |
| 3915 | |
| 3916 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3917 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3918 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3919 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3920 | return -EINVAL; |
| 3921 | } |
| 3922 | mddev->resync_max = max; |
| 3923 | } |
| 3924 | wake_up(&mddev->recovery_wait); |
| 3925 | return len; |
| 3926 | } |
| 3927 | |
| 3928 | static struct md_sysfs_entry md_max_sync = |
| 3929 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3930 | |
| 3931 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3932 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3933 | { |
| 3934 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3935 | } |
| 3936 | |
| 3937 | static ssize_t |
| 3938 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3939 | { |
| 3940 | char *e; |
| 3941 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3942 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3943 | if (mddev->pers == NULL || |
| 3944 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3945 | return -EINVAL; |
| 3946 | if (buf == e || (*e && *e != '\n')) |
| 3947 | return -EINVAL; |
| 3948 | if (new >= mddev->suspend_hi || |
| 3949 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3950 | mddev->suspend_lo = new; |
| 3951 | mddev->pers->quiesce(mddev, 2); |
| 3952 | return len; |
| 3953 | } else |
| 3954 | return -EINVAL; |
| 3955 | } |
| 3956 | static struct md_sysfs_entry md_suspend_lo = |
| 3957 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3958 | |
| 3959 | |
| 3960 | static ssize_t |
| 3961 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3962 | { |
| 3963 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3964 | } |
| 3965 | |
| 3966 | static ssize_t |
| 3967 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3968 | { |
| 3969 | char *e; |
| 3970 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3971 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3972 | if (mddev->pers == NULL || |
| 3973 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3974 | return -EINVAL; |
| 3975 | if (buf == e || (*e && *e != '\n')) |
| 3976 | return -EINVAL; |
| 3977 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3978 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3979 | mddev->suspend_hi = new; |
| 3980 | mddev->pers->quiesce(mddev, 1); |
| 3981 | mddev->pers->quiesce(mddev, 0); |
| 3982 | return len; |
| 3983 | } else |
| 3984 | return -EINVAL; |
| 3985 | } |
| 3986 | static struct md_sysfs_entry md_suspend_hi = |
| 3987 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3988 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3989 | static ssize_t |
| 3990 | reshape_position_show(mddev_t *mddev, char *page) |
| 3991 | { |
| 3992 | if (mddev->reshape_position != MaxSector) |
| 3993 | return sprintf(page, "%llu\n", |
| 3994 | (unsigned long long)mddev->reshape_position); |
| 3995 | strcpy(page, "none\n"); |
| 3996 | return 5; |
| 3997 | } |
| 3998 | |
| 3999 | static ssize_t |
| 4000 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4001 | { |
| 4002 | char *e; |
| 4003 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4004 | if (mddev->pers) |
| 4005 | return -EBUSY; |
| 4006 | if (buf == e || (*e && *e != '\n')) |
| 4007 | return -EINVAL; |
| 4008 | mddev->reshape_position = new; |
| 4009 | mddev->delta_disks = 0; |
| 4010 | mddev->new_level = mddev->level; |
| 4011 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4012 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4013 | return len; |
| 4014 | } |
| 4015 | |
| 4016 | static struct md_sysfs_entry md_reshape_position = |
| 4017 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4018 | reshape_position_store); |
| 4019 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4020 | static ssize_t |
| 4021 | array_size_show(mddev_t *mddev, char *page) |
| 4022 | { |
| 4023 | if (mddev->external_size) |
| 4024 | return sprintf(page, "%llu\n", |
| 4025 | (unsigned long long)mddev->array_sectors/2); |
| 4026 | else |
| 4027 | return sprintf(page, "default\n"); |
| 4028 | } |
| 4029 | |
| 4030 | static ssize_t |
| 4031 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4032 | { |
| 4033 | sector_t sectors; |
| 4034 | |
| 4035 | if (strncmp(buf, "default", 7) == 0) { |
| 4036 | if (mddev->pers) |
| 4037 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4038 | else |
| 4039 | sectors = mddev->array_sectors; |
| 4040 | |
| 4041 | mddev->external_size = 0; |
| 4042 | } else { |
| 4043 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4044 | return -EINVAL; |
| 4045 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4046 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4047 | |
| 4048 | mddev->external_size = 1; |
| 4049 | } |
| 4050 | |
| 4051 | mddev->array_sectors = sectors; |
| 4052 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4053 | if (mddev->pers) |
| 4054 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4055 | |
| 4056 | return len; |
| 4057 | } |
| 4058 | |
| 4059 | static struct md_sysfs_entry md_array_size = |
| 4060 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4061 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4062 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4063 | static struct attribute *md_default_attrs[] = { |
| 4064 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4065 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4066 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4067 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4068 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4069 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4070 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4071 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4072 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4073 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4074 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4075 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4076 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4077 | NULL, |
| 4078 | }; |
| 4079 | |
| 4080 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4081 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4082 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4083 | &md_sync_min.attr, |
| 4084 | &md_sync_max.attr, |
| 4085 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4086 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4087 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4088 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4089 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4090 | &md_suspend_lo.attr, |
| 4091 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4092 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4093 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4094 | NULL, |
| 4095 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4096 | static struct attribute_group md_redundancy_group = { |
| 4097 | .name = NULL, |
| 4098 | .attrs = md_redundancy_attrs, |
| 4099 | }; |
| 4100 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4101 | |
| 4102 | static ssize_t |
| 4103 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4104 | { |
| 4105 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4106 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4107 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4108 | |
| 4109 | if (!entry->show) |
| 4110 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4111 | rv = mddev_lock(mddev); |
| 4112 | if (!rv) { |
| 4113 | rv = entry->show(mddev, page); |
| 4114 | mddev_unlock(mddev); |
| 4115 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4116 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4117 | } |
| 4118 | |
| 4119 | static ssize_t |
| 4120 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4121 | const char *page, size_t length) |
| 4122 | { |
| 4123 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4124 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4125 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4126 | |
| 4127 | if (!entry->store) |
| 4128 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4129 | if (!capable(CAP_SYS_ADMIN)) |
| 4130 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4131 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4132 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4133 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4134 | if (!rv) { |
| 4135 | rv = entry->store(mddev, page, length); |
| 4136 | mddev_unlock(mddev); |
| 4137 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4138 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | static void md_free(struct kobject *ko) |
| 4142 | { |
| 4143 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4144 | |
| 4145 | if (mddev->sysfs_state) |
| 4146 | sysfs_put(mddev->sysfs_state); |
| 4147 | |
| 4148 | if (mddev->gendisk) { |
| 4149 | del_gendisk(mddev->gendisk); |
| 4150 | put_disk(mddev->gendisk); |
| 4151 | } |
| 4152 | if (mddev->queue) |
| 4153 | blk_cleanup_queue(mddev->queue); |
| 4154 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4155 | kfree(mddev); |
| 4156 | } |
| 4157 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4158 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4159 | .show = md_attr_show, |
| 4160 | .store = md_attr_store, |
| 4161 | }; |
| 4162 | static struct kobj_type md_ktype = { |
| 4163 | .release = md_free, |
| 4164 | .sysfs_ops = &md_sysfs_ops, |
| 4165 | .default_attrs = md_default_attrs, |
| 4166 | }; |
| 4167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | int mdp_major = 0; |
| 4169 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4170 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4171 | { |
| 4172 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4173 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4174 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4175 | kobject_del(&mddev->kobj); |
| 4176 | kobject_put(&mddev->kobj); |
| 4177 | } |
| 4178 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4179 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4181 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | mddev_t *mddev = mddev_find(dev); |
| 4183 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4184 | int partitioned; |
| 4185 | int shift; |
| 4186 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4187 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | |
| 4189 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4190 | return -ENODEV; |
| 4191 | |
| 4192 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4193 | shift = partitioned ? MdpMinorShift : 0; |
| 4194 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4196 | /* wait for any previous instance of this device to be |
| 4197 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4198 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4199 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4200 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4201 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4202 | error = -EEXIST; |
| 4203 | if (mddev->gendisk) |
| 4204 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4205 | |
| 4206 | if (name) { |
| 4207 | /* Need to ensure that 'name' is not a duplicate. |
| 4208 | */ |
| 4209 | mddev_t *mddev2; |
| 4210 | spin_lock(&all_mddevs_lock); |
| 4211 | |
| 4212 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4213 | if (mddev2->gendisk && |
| 4214 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4215 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4216 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4217 | } |
| 4218 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4220 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4221 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4222 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4223 | if (!mddev->queue) |
| 4224 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4225 | mddev->queue->queuedata = mddev; |
| 4226 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4227 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4228 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4229 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4230 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | disk = alloc_disk(1 << shift); |
| 4233 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4234 | blk_cleanup_queue(mddev->queue); |
| 4235 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4236 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4238 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4239 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4240 | if (name) |
| 4241 | strcpy(disk->disk_name, name); |
| 4242 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4244 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | disk->fops = &md_fops; |
| 4247 | disk->private_data = mddev; |
| 4248 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4249 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4250 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4251 | * remove it now. |
| 4252 | */ |
| 4253 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | add_disk(disk); |
| 4255 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4256 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4257 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4258 | if (error) { |
| 4259 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4260 | * __must_check, we must do something with the result |
| 4261 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4262 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4263 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4264 | error = 0; |
| 4265 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4266 | if (mddev->kobj.sd && |
| 4267 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4268 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4269 | abort: |
| 4270 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4271 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4272 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4273 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4274 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4275 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4276 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4277 | } |
| 4278 | |
| 4279 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4280 | { |
| 4281 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | return NULL; |
| 4283 | } |
| 4284 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4285 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4286 | { |
| 4287 | /* val must be "md_*" where * is not all digits. |
| 4288 | * We allocate an array with a large free minor number, and |
| 4289 | * set the name to val. val must not already be an active name. |
| 4290 | */ |
| 4291 | int len = strlen(val); |
| 4292 | char buf[DISK_NAME_LEN]; |
| 4293 | |
| 4294 | while (len && val[len-1] == '\n') |
| 4295 | len--; |
| 4296 | if (len >= DISK_NAME_LEN) |
| 4297 | return -E2BIG; |
| 4298 | strlcpy(buf, val, len+1); |
| 4299 | if (strncmp(buf, "md_", 3) != 0) |
| 4300 | return -EINVAL; |
| 4301 | return md_alloc(0, buf); |
| 4302 | } |
| 4303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | static void md_safemode_timeout(unsigned long data) |
| 4305 | { |
| 4306 | mddev_t *mddev = (mddev_t *) data; |
| 4307 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4308 | if (!atomic_read(&mddev->writes_pending)) { |
| 4309 | mddev->safemode = 1; |
| 4310 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4311 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | md_wakeup_thread(mddev->thread); |
| 4314 | } |
| 4315 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4316 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4318 | int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4320 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4322 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4324 | if (list_empty(&mddev->disks)) |
| 4325 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | |
| 4328 | if (mddev->pers) |
| 4329 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4330 | /* Cannot run until previous stop completes properly */ |
| 4331 | if (mddev->sysfs_active) |
| 4332 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | /* |
| 4335 | * Analyze all RAID superblock(s) |
| 4336 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4337 | if (!mddev->raid_disks) { |
| 4338 | if (!mddev->persistent) |
| 4339 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4340 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4341 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4343 | if (mddev->level != LEVEL_NONE) |
| 4344 | request_module("md-level-%d", mddev->level); |
| 4345 | else if (mddev->clevel[0]) |
| 4346 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | |
| 4348 | /* |
| 4349 | * Drop all container device buffers, from now on |
| 4350 | * the only valid external interface is through the md |
| 4351 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4353 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4354 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | continue; |
| 4356 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4357 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4358 | |
| 4359 | /* perform some consistency tests on the device. |
| 4360 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4361 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4362 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4363 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4364 | if (mddev->dev_sectors && |
| 4365 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4366 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4367 | printk("md: %s: data overlaps metadata\n", |
| 4368 | mdname(mddev)); |
| 4369 | return -EINVAL; |
| 4370 | } |
| 4371 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4372 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4373 | > rdev->data_offset) { |
| 4374 | printk("md: %s: metadata overlaps data\n", |
| 4375 | mdname(mddev)); |
| 4376 | return -EINVAL; |
| 4377 | } |
| 4378 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4379 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | } |
| 4381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4382 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4383 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4384 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4385 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4386 | if (mddev->level != LEVEL_NONE) |
| 4387 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4388 | mddev->level); |
| 4389 | else |
| 4390 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4391 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | return -EINVAL; |
| 4393 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4394 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4396 | if (mddev->level != pers->level) { |
| 4397 | mddev->level = pers->level; |
| 4398 | mddev->new_level = pers->level; |
| 4399 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4400 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4402 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4403 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4404 | /* This personality cannot handle reshaping... */ |
| 4405 | mddev->pers = NULL; |
| 4406 | module_put(pers->owner); |
| 4407 | return -EINVAL; |
| 4408 | } |
| 4409 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4410 | if (pers->sync_request) { |
| 4411 | /* Warn if this is a potentially silly |
| 4412 | * configuration. |
| 4413 | */ |
| 4414 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4415 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4416 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4417 | |
| 4418 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4419 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4420 | if (rdev < rdev2 && |
| 4421 | rdev->bdev->bd_contains == |
| 4422 | rdev2->bdev->bd_contains) { |
| 4423 | printk(KERN_WARNING |
| 4424 | "%s: WARNING: %s appears to be" |
| 4425 | " on the same physical disk as" |
| 4426 | " %s.\n", |
| 4427 | mdname(mddev), |
| 4428 | bdevname(rdev->bdev,b), |
| 4429 | bdevname(rdev2->bdev,b2)); |
| 4430 | warned = 1; |
| 4431 | } |
| 4432 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4433 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4434 | if (warned) |
| 4435 | printk(KERN_WARNING |
| 4436 | "True protection against single-disk" |
| 4437 | " failure might be compromised.\n"); |
| 4438 | } |
| 4439 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4440 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4441 | /* may be over-ridden by personality */ |
| 4442 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4443 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4444 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4446 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4447 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4448 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4449 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4450 | if (err) |
| 4451 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4452 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4453 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4454 | " but 'external_size' not in effect?\n", __func__); |
| 4455 | printk(KERN_ERR |
| 4456 | "md: invalid array_size %llu > default size %llu\n", |
| 4457 | (unsigned long long)mddev->array_sectors / 2, |
| 4458 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4459 | err = -EINVAL; |
| 4460 | mddev->pers->stop(mddev); |
| 4461 | } |
| 4462 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4463 | err = bitmap_create(mddev); |
| 4464 | if (err) { |
| 4465 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4466 | mdname(mddev), err); |
| 4467 | mddev->pers->stop(mddev); |
| 4468 | } |
| 4469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | module_put(mddev->pers->owner); |
| 4472 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4473 | bitmap_destroy(mddev); |
| 4474 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4476 | if (mddev->pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4477 | if (mddev->kobj.sd && |
| 4478 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4479 | printk(KERN_WARNING |
| 4480 | "md: cannot register extra attributes for %s\n", |
| 4481 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4482 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4483 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4484 | mddev->ro = 0; |
| 4485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4486 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4487 | atomic_set(&mddev->max_corr_read_errors, |
| 4488 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | mddev->safemode = 0; |
| 4490 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4491 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4492 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4494 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4495 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4496 | if (rdev->raid_disk >= 0) { |
| 4497 | char nm[20]; |
| 4498 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4499 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4500 | /* failure here is OK */; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4501 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | |
| 4503 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4504 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4505 | if (mddev->flags) |
| 4506 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4508 | md_wakeup_thread(mddev->thread); |
| 4509 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4510 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4511 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4512 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 4513 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4514 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | return 0; |
| 4516 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4517 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4519 | static int do_md_run(mddev_t *mddev) |
| 4520 | { |
| 4521 | int err; |
| 4522 | |
| 4523 | err = md_run(mddev); |
| 4524 | if (err) |
| 4525 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 4526 | err = bitmap_load(mddev); |
| 4527 | if (err) { |
| 4528 | bitmap_destroy(mddev); |
| 4529 | goto out; |
| 4530 | } |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4531 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4532 | revalidate_disk(mddev->gendisk); |
| 4533 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4534 | out: |
| 4535 | return err; |
| 4536 | } |
| 4537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | static int restart_array(mddev_t *mddev) |
| 4539 | { |
| 4540 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4541 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4542 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4544 | return -ENXIO; |
| 4545 | if (!mddev->pers) |
| 4546 | return -EINVAL; |
| 4547 | if (!mddev->ro) |
| 4548 | return -EBUSY; |
| 4549 | mddev->safemode = 0; |
| 4550 | mddev->ro = 0; |
| 4551 | set_disk_ro(disk, 0); |
| 4552 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4553 | mdname(mddev)); |
| 4554 | /* Kick recovery or resync if necessary */ |
| 4555 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4556 | md_wakeup_thread(mddev->thread); |
| 4557 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4558 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4559 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | } |
| 4561 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4562 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4563 | * to the file ourselves */ |
| 4564 | static int deny_bitmap_write_access(struct file * file) |
| 4565 | { |
| 4566 | struct inode *inode = file->f_mapping->host; |
| 4567 | |
| 4568 | spin_lock(&inode->i_lock); |
| 4569 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4570 | spin_unlock(&inode->i_lock); |
| 4571 | return -ETXTBSY; |
| 4572 | } |
| 4573 | atomic_set(&inode->i_writecount, -1); |
| 4574 | spin_unlock(&inode->i_lock); |
| 4575 | |
| 4576 | return 0; |
| 4577 | } |
| 4578 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4579 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4580 | { |
| 4581 | struct inode *inode = file->f_mapping->host; |
| 4582 | |
| 4583 | spin_lock(&inode->i_lock); |
| 4584 | atomic_set(&inode->i_writecount, 1); |
| 4585 | spin_unlock(&inode->i_lock); |
| 4586 | } |
| 4587 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4588 | static void md_clean(mddev_t *mddev) |
| 4589 | { |
| 4590 | mddev->array_sectors = 0; |
| 4591 | mddev->external_size = 0; |
| 4592 | mddev->dev_sectors = 0; |
| 4593 | mddev->raid_disks = 0; |
| 4594 | mddev->recovery_cp = 0; |
| 4595 | mddev->resync_min = 0; |
| 4596 | mddev->resync_max = MaxSector; |
| 4597 | mddev->reshape_position = MaxSector; |
| 4598 | mddev->external = 0; |
| 4599 | mddev->persistent = 0; |
| 4600 | mddev->level = LEVEL_NONE; |
| 4601 | mddev->clevel[0] = 0; |
| 4602 | mddev->flags = 0; |
| 4603 | mddev->ro = 0; |
| 4604 | mddev->metadata_type[0] = 0; |
| 4605 | mddev->chunk_sectors = 0; |
| 4606 | mddev->ctime = mddev->utime = 0; |
| 4607 | mddev->layout = 0; |
| 4608 | mddev->max_disks = 0; |
| 4609 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4610 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4611 | mddev->delta_disks = 0; |
| 4612 | mddev->new_level = LEVEL_NONE; |
| 4613 | mddev->new_layout = 0; |
| 4614 | mddev->new_chunk_sectors = 0; |
| 4615 | mddev->curr_resync = 0; |
| 4616 | mddev->resync_mismatches = 0; |
| 4617 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4618 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4619 | mddev->recovery = 0; |
| 4620 | mddev->in_sync = 0; |
| 4621 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4622 | mddev->safemode = 0; |
| 4623 | mddev->bitmap_info.offset = 0; |
| 4624 | mddev->bitmap_info.default_offset = 0; |
| 4625 | mddev->bitmap_info.chunksize = 0; |
| 4626 | mddev->bitmap_info.daemon_sleep = 0; |
| 4627 | mddev->bitmap_info.max_write_behind = 0; |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 4628 | mddev->plug = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4629 | } |
| 4630 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4631 | void md_stop_writes(mddev_t *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4632 | { |
| 4633 | if (mddev->sync_thread) { |
| 4634 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4635 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4636 | md_unregister_thread(mddev->sync_thread); |
| 4637 | mddev->sync_thread = NULL; |
| 4638 | } |
| 4639 | |
| 4640 | del_timer_sync(&mddev->safemode_timer); |
| 4641 | |
| 4642 | bitmap_flush(mddev); |
| 4643 | md_super_wait(mddev); |
| 4644 | |
| 4645 | if (!mddev->in_sync || mddev->flags) { |
| 4646 | /* mark array as shutdown cleanly */ |
| 4647 | mddev->in_sync = 1; |
| 4648 | md_update_sb(mddev, 1); |
| 4649 | } |
| 4650 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4651 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4652 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4653 | void md_stop(mddev_t *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4654 | { |
| 4655 | mddev->pers->stop(mddev); |
| 4656 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4657 | mddev->to_remove = &md_redundancy_group; |
| 4658 | module_put(mddev->pers->owner); |
| 4659 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4660 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4661 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4662 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4663 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4664 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4665 | { |
| 4666 | int err = 0; |
| 4667 | mutex_lock(&mddev->open_mutex); |
| 4668 | if (atomic_read(&mddev->openers) > is_open) { |
| 4669 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4670 | err = -EBUSY; |
| 4671 | goto out; |
| 4672 | } |
| 4673 | if (mddev->pers) { |
| 4674 | md_stop_writes(mddev); |
| 4675 | |
| 4676 | err = -ENXIO; |
| 4677 | if (mddev->ro==1) |
| 4678 | goto out; |
| 4679 | mddev->ro = 1; |
| 4680 | set_disk_ro(mddev->gendisk, 1); |
| 4681 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4682 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4683 | err = 0; |
| 4684 | } |
| 4685 | out: |
| 4686 | mutex_unlock(&mddev->open_mutex); |
| 4687 | return err; |
| 4688 | } |
| 4689 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4690 | /* mode: |
| 4691 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4692 | * 2 - stop but do not disassemble array |
| 4693 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4694 | 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] | 4695 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4697 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4698 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4699 | mutex_lock(&mddev->open_mutex); |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 4700 | if (atomic_read(&mddev->openers) > is_open || |
| 4701 | mddev->sysfs_active) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4702 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4703 | mutex_unlock(&mddev->open_mutex); |
| 4704 | return -EBUSY; |
| 4705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4707 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4708 | if (mddev->ro) |
| 4709 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4710 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 4711 | md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4712 | md_stop(mddev); |
| 4713 | mddev->queue->merge_bvec_fn = NULL; |
| 4714 | mddev->queue->unplug_fn = NULL; |
| 4715 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4716 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4717 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4718 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4719 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4720 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4721 | if (rdev->raid_disk >= 0) { |
| 4722 | char nm[20]; |
| 4723 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4724 | sysfs_remove_link(&mddev->kobj, nm); |
| 4725 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4726 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4727 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4728 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4729 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4730 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4731 | if (mddev->ro) |
| 4732 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4733 | } else |
| 4734 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | /* |
| 4736 | * Free resources if final stop |
| 4737 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4738 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4739 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4740 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4741 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4742 | if (mddev->bitmap_info.file) { |
| 4743 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4744 | fput(mddev->bitmap_info.file); |
| 4745 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4746 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4747 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | export_array(mddev); |
| 4750 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4751 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4752 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4753 | if (mddev->hold_active == UNTIL_STOP) |
| 4754 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4755 | } |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4756 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4757 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4758 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 4759 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | } |
| 4761 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4762 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4763 | static void autorun_array(mddev_t *mddev) |
| 4764 | { |
| 4765 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | int err; |
| 4767 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4768 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | |
| 4771 | printk(KERN_INFO "md: running: "); |
| 4772 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4773 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | char b[BDEVNAME_SIZE]; |
| 4775 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4776 | } |
| 4777 | printk("\n"); |
| 4778 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4779 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4780 | if (err) { |
| 4781 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4782 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | } |
| 4784 | } |
| 4785 | |
| 4786 | /* |
| 4787 | * lets try to run arrays based on all disks that have arrived |
| 4788 | * until now. (those are in pending_raid_disks) |
| 4789 | * |
| 4790 | * the method: pick the first pending disk, collect all disks with |
| 4791 | * the same UUID, remove all from the pending list and put them into |
| 4792 | * the 'same_array' list. Then order this list based on superblock |
| 4793 | * update time (freshest comes first), kick out 'old' disks and |
| 4794 | * compare superblocks. If everything's fine then run it. |
| 4795 | * |
| 4796 | * If "unit" is allocated, then bump its reference count |
| 4797 | */ |
| 4798 | static void autorun_devices(int part) |
| 4799 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4800 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | mddev_t *mddev; |
| 4802 | char b[BDEVNAME_SIZE]; |
| 4803 | |
| 4804 | printk(KERN_INFO "md: autorun ...\n"); |
| 4805 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4806 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4808 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | rdev0 = list_entry(pending_raid_disks.next, |
| 4810 | mdk_rdev_t, same_set); |
| 4811 | |
| 4812 | printk(KERN_INFO "md: considering %s ...\n", |
| 4813 | bdevname(rdev0->bdev,b)); |
| 4814 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4815 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4816 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4817 | printk(KERN_INFO "md: adding %s ...\n", |
| 4818 | bdevname(rdev->bdev,b)); |
| 4819 | list_move(&rdev->same_set, &candidates); |
| 4820 | } |
| 4821 | /* |
| 4822 | * now we have a set of devices, with all of them having |
| 4823 | * mostly sane superblocks. It's time to allocate the |
| 4824 | * mddev. |
| 4825 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4826 | if (part) { |
| 4827 | dev = MKDEV(mdp_major, |
| 4828 | rdev0->preferred_minor << MdpMinorShift); |
| 4829 | unit = MINOR(dev) >> MdpMinorShift; |
| 4830 | } else { |
| 4831 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4832 | unit = MINOR(dev); |
| 4833 | } |
| 4834 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4835 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4836 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4837 | break; |
| 4838 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4839 | |
| 4840 | md_probe(dev, NULL, NULL); |
| 4841 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4842 | if (!mddev || !mddev->gendisk) { |
| 4843 | if (mddev) |
| 4844 | mddev_put(mddev); |
| 4845 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | "md: cannot allocate memory for md drive.\n"); |
| 4847 | break; |
| 4848 | } |
| 4849 | if (mddev_lock(mddev)) |
| 4850 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4851 | mdname(mddev)); |
| 4852 | else if (mddev->raid_disks || mddev->major_version |
| 4853 | || !list_empty(&mddev->disks)) { |
| 4854 | printk(KERN_WARNING |
| 4855 | "md: %s already running, cannot run %s\n", |
| 4856 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4857 | mddev_unlock(mddev); |
| 4858 | } else { |
| 4859 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4860 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4861 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4862 | list_del_init(&rdev->same_set); |
| 4863 | if (bind_rdev_to_array(rdev, mddev)) |
| 4864 | export_rdev(rdev); |
| 4865 | } |
| 4866 | autorun_array(mddev); |
| 4867 | mddev_unlock(mddev); |
| 4868 | } |
| 4869 | /* on success, candidates will be empty, on error |
| 4870 | * it won't... |
| 4871 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4872 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4873 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4874 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4875 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4876 | mddev_put(mddev); |
| 4877 | } |
| 4878 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4879 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4880 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | static int get_version(void __user * arg) |
| 4883 | { |
| 4884 | mdu_version_t ver; |
| 4885 | |
| 4886 | ver.major = MD_MAJOR_VERSION; |
| 4887 | ver.minor = MD_MINOR_VERSION; |
| 4888 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4889 | |
| 4890 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4891 | return -EFAULT; |
| 4892 | |
| 4893 | return 0; |
| 4894 | } |
| 4895 | |
| 4896 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4897 | { |
| 4898 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4899 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4902 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4903 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4904 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4905 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | failed++; |
| 4907 | else { |
| 4908 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4909 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4910 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | else |
| 4912 | spare++; |
| 4913 | } |
| 4914 | } |
| 4915 | |
| 4916 | info.major_version = mddev->major_version; |
| 4917 | info.minor_version = mddev->minor_version; |
| 4918 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4919 | info.ctime = mddev->ctime; |
| 4920 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4921 | info.size = mddev->dev_sectors / 2; |
| 4922 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4923 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | info.nr_disks = nr; |
| 4925 | info.raid_disks = mddev->raid_disks; |
| 4926 | info.md_minor = mddev->md_minor; |
| 4927 | info.not_persistent= !mddev->persistent; |
| 4928 | |
| 4929 | info.utime = mddev->utime; |
| 4930 | info.state = 0; |
| 4931 | if (mddev->in_sync) |
| 4932 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4933 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4934 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4935 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | info.working_disks = working; |
| 4937 | info.failed_disks = failed; |
| 4938 | info.spare_disks = spare; |
| 4939 | |
| 4940 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4941 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4942 | |
| 4943 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4944 | return -EFAULT; |
| 4945 | |
| 4946 | return 0; |
| 4947 | } |
| 4948 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4949 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4950 | { |
| 4951 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4952 | char *ptr, *buf = NULL; |
| 4953 | int err = -ENOMEM; |
| 4954 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4955 | if (md_allow_write(mddev)) |
| 4956 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4957 | else |
| 4958 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4959 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4960 | if (!file) |
| 4961 | goto out; |
| 4962 | |
| 4963 | /* bitmap disabled, zero the first byte and copy out */ |
| 4964 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4965 | file->pathname[0] = '\0'; |
| 4966 | goto copy_out; |
| 4967 | } |
| 4968 | |
| 4969 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4970 | if (!buf) |
| 4971 | goto out; |
| 4972 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4973 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4974 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4975 | goto out; |
| 4976 | |
| 4977 | strcpy(file->pathname, ptr); |
| 4978 | |
| 4979 | copy_out: |
| 4980 | err = 0; |
| 4981 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4982 | err = -EFAULT; |
| 4983 | out: |
| 4984 | kfree(buf); |
| 4985 | kfree(file); |
| 4986 | return err; |
| 4987 | } |
| 4988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4990 | { |
| 4991 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4992 | mdk_rdev_t *rdev; |
| 4993 | |
| 4994 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4995 | return -EFAULT; |
| 4996 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4997 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | if (rdev) { |
| 4999 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5000 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5001 | info.raid_disk = rdev->raid_disk; |
| 5002 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5003 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5004 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5005 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5007 | info.state |= (1<<MD_DISK_SYNC); |
| 5008 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5009 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5010 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5011 | } else { |
| 5012 | info.major = info.minor = 0; |
| 5013 | info.raid_disk = -1; |
| 5014 | info.state = (1<<MD_DISK_REMOVED); |
| 5015 | } |
| 5016 | |
| 5017 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5018 | return -EFAULT; |
| 5019 | |
| 5020 | return 0; |
| 5021 | } |
| 5022 | |
| 5023 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5024 | { |
| 5025 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5026 | mdk_rdev_t *rdev; |
| 5027 | dev_t dev = MKDEV(info->major,info->minor); |
| 5028 | |
| 5029 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5030 | return -EOVERFLOW; |
| 5031 | |
| 5032 | if (!mddev->raid_disks) { |
| 5033 | int err; |
| 5034 | /* expecting a device which has a superblock */ |
| 5035 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5036 | if (IS_ERR(rdev)) { |
| 5037 | printk(KERN_WARNING |
| 5038 | "md: md_import_device returned %ld\n", |
| 5039 | PTR_ERR(rdev)); |
| 5040 | return PTR_ERR(rdev); |
| 5041 | } |
| 5042 | if (!list_empty(&mddev->disks)) { |
| 5043 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5044 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5045 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5047 | if (err < 0) { |
| 5048 | printk(KERN_WARNING |
| 5049 | "md: %s has different UUID to %s\n", |
| 5050 | bdevname(rdev->bdev,b), |
| 5051 | bdevname(rdev0->bdev,b2)); |
| 5052 | export_rdev(rdev); |
| 5053 | return -EINVAL; |
| 5054 | } |
| 5055 | } |
| 5056 | err = bind_rdev_to_array(rdev, mddev); |
| 5057 | if (err) |
| 5058 | export_rdev(rdev); |
| 5059 | return err; |
| 5060 | } |
| 5061 | |
| 5062 | /* |
| 5063 | * add_new_disk can be used once the array is assembled |
| 5064 | * to add "hot spares". They must already have a superblock |
| 5065 | * written |
| 5066 | */ |
| 5067 | if (mddev->pers) { |
| 5068 | int err; |
| 5069 | if (!mddev->pers->hot_add_disk) { |
| 5070 | printk(KERN_WARNING |
| 5071 | "%s: personality does not support diskops!\n", |
| 5072 | mdname(mddev)); |
| 5073 | return -EINVAL; |
| 5074 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5075 | if (mddev->persistent) |
| 5076 | rdev = md_import_device(dev, mddev->major_version, |
| 5077 | mddev->minor_version); |
| 5078 | else |
| 5079 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | if (IS_ERR(rdev)) { |
| 5081 | printk(KERN_WARNING |
| 5082 | "md: md_import_device returned %ld\n", |
| 5083 | PTR_ERR(rdev)); |
| 5084 | return PTR_ERR(rdev); |
| 5085 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5086 | /* set save_raid_disk if appropriate */ |
| 5087 | if (!mddev->persistent) { |
| 5088 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5089 | info->raid_disk < mddev->raid_disks) |
| 5090 | rdev->raid_disk = info->raid_disk; |
| 5091 | else |
| 5092 | rdev->raid_disk = -1; |
| 5093 | } else |
| 5094 | super_types[mddev->major_version]. |
| 5095 | validate_super(mddev, rdev); |
| 5096 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5097 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5098 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5099 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5100 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5101 | else |
| 5102 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | rdev->raid_disk = -1; |
| 5105 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5106 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5107 | /* If there is hot_add_disk but no hot_remove_disk |
| 5108 | * then added disks for geometry changes, |
| 5109 | * and should be added immediately. |
| 5110 | */ |
| 5111 | super_types[mddev->major_version]. |
| 5112 | validate_super(mddev, rdev); |
| 5113 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5114 | if (err) |
| 5115 | unbind_rdev_from_array(rdev); |
| 5116 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | if (err) |
| 5118 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5119 | else |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5120 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5121 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5122 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5123 | if (mddev->degraded) |
| 5124 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5125 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5126 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5127 | return err; |
| 5128 | } |
| 5129 | |
| 5130 | /* otherwise, add_new_disk is only allowed |
| 5131 | * for major_version==0 superblocks |
| 5132 | */ |
| 5133 | if (mddev->major_version != 0) { |
| 5134 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5135 | mdname(mddev)); |
| 5136 | return -EINVAL; |
| 5137 | } |
| 5138 | |
| 5139 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5140 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5141 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | if (IS_ERR(rdev)) { |
| 5143 | printk(KERN_WARNING |
| 5144 | "md: error, md_import_device() returned %ld\n", |
| 5145 | PTR_ERR(rdev)); |
| 5146 | return PTR_ERR(rdev); |
| 5147 | } |
| 5148 | rdev->desc_nr = info->number; |
| 5149 | if (info->raid_disk < mddev->raid_disks) |
| 5150 | rdev->raid_disk = info->raid_disk; |
| 5151 | else |
| 5152 | rdev->raid_disk = -1; |
| 5153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5155 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5156 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5158 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5159 | set_bit(WriteMostly, &rdev->flags); |
| 5160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5161 | if (!mddev->persistent) { |
| 5162 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5163 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5165 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5166 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5167 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5168 | err = bind_rdev_to_array(rdev, mddev); |
| 5169 | if (err) { |
| 5170 | export_rdev(rdev); |
| 5171 | return err; |
| 5172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | return 0; |
| 5176 | } |
| 5177 | |
| 5178 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5179 | { |
| 5180 | char b[BDEVNAME_SIZE]; |
| 5181 | mdk_rdev_t *rdev; |
| 5182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | rdev = find_rdev(mddev, dev); |
| 5184 | if (!rdev) |
| 5185 | return -ENXIO; |
| 5186 | |
| 5187 | if (rdev->raid_disk >= 0) |
| 5188 | goto busy; |
| 5189 | |
| 5190 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5191 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5192 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | |
| 5194 | return 0; |
| 5195 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5196 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5198 | return -EBUSY; |
| 5199 | } |
| 5200 | |
| 5201 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5202 | { |
| 5203 | char b[BDEVNAME_SIZE]; |
| 5204 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | mdk_rdev_t *rdev; |
| 5206 | |
| 5207 | if (!mddev->pers) |
| 5208 | return -ENODEV; |
| 5209 | |
| 5210 | if (mddev->major_version != 0) { |
| 5211 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5212 | " version-0 superblocks.\n", |
| 5213 | mdname(mddev)); |
| 5214 | return -EINVAL; |
| 5215 | } |
| 5216 | if (!mddev->pers->hot_add_disk) { |
| 5217 | printk(KERN_WARNING |
| 5218 | "%s: personality does not support diskops!\n", |
| 5219 | mdname(mddev)); |
| 5220 | return -EINVAL; |
| 5221 | } |
| 5222 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5223 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | if (IS_ERR(rdev)) { |
| 5225 | printk(KERN_WARNING |
| 5226 | "md: error, md_import_device() returned %ld\n", |
| 5227 | PTR_ERR(rdev)); |
| 5228 | return -EINVAL; |
| 5229 | } |
| 5230 | |
| 5231 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5232 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5234 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5235 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5236 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5238 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5239 | printk(KERN_WARNING |
| 5240 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5241 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5242 | err = -EINVAL; |
| 5243 | goto abort_export; |
| 5244 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5245 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5247 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5248 | err = bind_rdev_to_array(rdev, mddev); |
| 5249 | if (err) |
| 5250 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | |
| 5252 | /* |
| 5253 | * The rest should better be atomic, we can have disk failures |
| 5254 | * noticed in interrupt contexts ... |
| 5255 | */ |
| 5256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | rdev->raid_disk = -1; |
| 5258 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5259 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | |
| 5261 | /* |
| 5262 | * Kick recovery, maybe this spare has to be added to the |
| 5263 | * array immediately. |
| 5264 | */ |
| 5265 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5266 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5267 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | return 0; |
| 5269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | abort_export: |
| 5271 | export_rdev(rdev); |
| 5272 | return err; |
| 5273 | } |
| 5274 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5275 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5276 | { |
| 5277 | int err; |
| 5278 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5279 | if (mddev->pers) { |
| 5280 | if (!mddev->pers->quiesce) |
| 5281 | return -EBUSY; |
| 5282 | if (mddev->recovery || mddev->sync_thread) |
| 5283 | return -EBUSY; |
| 5284 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5285 | } |
| 5286 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5287 | |
| 5288 | if (fd >= 0) { |
| 5289 | if (mddev->bitmap) |
| 5290 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5291 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5292 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5293 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5294 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5295 | mdname(mddev)); |
| 5296 | return -EBADF; |
| 5297 | } |
| 5298 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5299 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5300 | if (err) { |
| 5301 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5302 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5303 | fput(mddev->bitmap_info.file); |
| 5304 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5305 | return err; |
| 5306 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5307 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5308 | } else if (mddev->bitmap == NULL) |
| 5309 | return -ENOENT; /* cannot remove what isn't there */ |
| 5310 | err = 0; |
| 5311 | if (mddev->pers) { |
| 5312 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5313 | if (fd >= 0) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5314 | err = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5315 | if (!err) |
| 5316 | err = bitmap_load(mddev); |
| 5317 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5318 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5319 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5320 | fd = -1; /* make sure to put the file */ |
| 5321 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5322 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5323 | } |
| 5324 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5325 | if (mddev->bitmap_info.file) { |
| 5326 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5327 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5328 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5329 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5330 | } |
| 5331 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5332 | return err; |
| 5333 | } |
| 5334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | /* |
| 5336 | * set_array_info is used two different ways |
| 5337 | * The original usage is when creating a new array. |
| 5338 | * In this usage, raid_disks is > 0 and it together with |
| 5339 | * level, size, not_persistent,layout,chunksize determine the |
| 5340 | * shape of the array. |
| 5341 | * This will always create an array with a type-0.90.0 superblock. |
| 5342 | * The newer usage is when assembling an array. |
| 5343 | * In this case raid_disks will be 0, and the major_version field is |
| 5344 | * use to determine which style super-blocks are to be found on the devices. |
| 5345 | * The minor and patch _version numbers are also kept incase the |
| 5346 | * super_block handler wishes to interpret them. |
| 5347 | */ |
| 5348 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5349 | { |
| 5350 | |
| 5351 | if (info->raid_disks == 0) { |
| 5352 | /* just setting version number for superblock loading */ |
| 5353 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5354 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | super_types[info->major_version].name == NULL) { |
| 5356 | /* maybe try to auto-load a module? */ |
| 5357 | printk(KERN_INFO |
| 5358 | "md: superblock version %d not known\n", |
| 5359 | info->major_version); |
| 5360 | return -EINVAL; |
| 5361 | } |
| 5362 | mddev->major_version = info->major_version; |
| 5363 | mddev->minor_version = info->minor_version; |
| 5364 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5365 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5366 | /* ensure mddev_put doesn't delete this now that there |
| 5367 | * is some minimal configuration. |
| 5368 | */ |
| 5369 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | return 0; |
| 5371 | } |
| 5372 | mddev->major_version = MD_MAJOR_VERSION; |
| 5373 | mddev->minor_version = MD_MINOR_VERSION; |
| 5374 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5375 | mddev->ctime = get_seconds(); |
| 5376 | |
| 5377 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5378 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5379 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | mddev->raid_disks = info->raid_disks; |
| 5381 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5382 | * openned |
| 5383 | */ |
| 5384 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5385 | mddev->recovery_cp = MaxSector; |
| 5386 | else |
| 5387 | mddev->recovery_cp = 0; |
| 5388 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5389 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5390 | |
| 5391 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5392 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | |
| 5394 | mddev->max_disks = MD_SB_DISKS; |
| 5395 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5396 | if (mddev->persistent) |
| 5397 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5398 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5400 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5401 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5402 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5403 | mddev->reshape_position = MaxSector; |
| 5404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | /* |
| 5406 | * Generate a 128 bit UUID |
| 5407 | */ |
| 5408 | get_random_bytes(mddev->uuid, 16); |
| 5409 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5410 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5411 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5412 | mddev->new_layout = mddev->layout; |
| 5413 | mddev->delta_disks = 0; |
| 5414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | return 0; |
| 5416 | } |
| 5417 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5418 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5419 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5420 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5421 | |
| 5422 | if (mddev->external_size) |
| 5423 | return; |
| 5424 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5425 | mddev->array_sectors = array_sectors; |
| 5426 | } |
| 5427 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5428 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5429 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5430 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5431 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5432 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5433 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5434 | |
| 5435 | if (mddev->pers->resize == NULL) |
| 5436 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5437 | /* The "num_sectors" is the number of sectors of each device that |
| 5438 | * is used. This can only make sense for arrays with redundancy. |
| 5439 | * linear and raid0 always use whatever space is available. We can only |
| 5440 | * consider changing this number if no resync or reconstruction is |
| 5441 | * 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] | 5442 | * 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] | 5443 | * of each device. If num_sectors is zero, we find the largest size |
| 5444 | * that fits. |
| 5445 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5446 | */ |
| 5447 | if (mddev->sync_thread) |
| 5448 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5449 | if (mddev->bitmap) |
| 5450 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5451 | * grow, and re-add. |
| 5452 | */ |
| 5453 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5454 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5455 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5456 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5457 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5458 | num_sectors = avail; |
| 5459 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5460 | return -ENOSPC; |
| 5461 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5462 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5463 | if (!rv) |
| 5464 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5465 | return rv; |
| 5466 | } |
| 5467 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5468 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5469 | { |
| 5470 | int rv; |
| 5471 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5472 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5473 | return -EINVAL; |
| 5474 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5475 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5476 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5477 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5478 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5479 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5480 | |
| 5481 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5482 | return rv; |
| 5483 | } |
| 5484 | |
| 5485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5486 | /* |
| 5487 | * update_array_info is used to change the configuration of an |
| 5488 | * on-line array. |
| 5489 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5490 | * fields in the info are checked against the array. |
| 5491 | * Any differences that cannot be handled will cause an error. |
| 5492 | * Normally, only one change can be managed at a time. |
| 5493 | */ |
| 5494 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5495 | { |
| 5496 | int rv = 0; |
| 5497 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5498 | int state = 0; |
| 5499 | |
| 5500 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5501 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5502 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | |
| 5504 | if (mddev->major_version != info->major_version || |
| 5505 | mddev->minor_version != info->minor_version || |
| 5506 | /* mddev->patch_version != info->patch_version || */ |
| 5507 | mddev->ctime != info->ctime || |
| 5508 | mddev->level != info->level || |
| 5509 | /* mddev->layout != info->layout || */ |
| 5510 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5511 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5512 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5513 | ((state^info->state) & 0xfffffe00) |
| 5514 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5515 | return -EINVAL; |
| 5516 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5517 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5518 | cnt++; |
| 5519 | if (mddev->raid_disks != info->raid_disks) |
| 5520 | cnt++; |
| 5521 | if (mddev->layout != info->layout) |
| 5522 | cnt++; |
| 5523 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5524 | cnt++; |
| 5525 | if (cnt == 0) |
| 5526 | return 0; |
| 5527 | if (cnt > 1) |
| 5528 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5529 | |
| 5530 | if (mddev->layout != info->layout) { |
| 5531 | /* Change layout |
| 5532 | * we don't need to do anything at the md level, the |
| 5533 | * personality will take care of it all. |
| 5534 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5535 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5537 | else { |
| 5538 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5539 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5540 | if (rv) |
| 5541 | mddev->new_layout = mddev->layout; |
| 5542 | return rv; |
| 5543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5544 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5545 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5546 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5548 | if (mddev->raid_disks != info->raid_disks) |
| 5549 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5551 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5552 | if (mddev->pers->quiesce == NULL) |
| 5553 | return -EINVAL; |
| 5554 | if (mddev->recovery || mddev->sync_thread) |
| 5555 | return -EBUSY; |
| 5556 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5557 | /* add the bitmap */ |
| 5558 | if (mddev->bitmap) |
| 5559 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5560 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5561 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5562 | mddev->bitmap_info.offset = |
| 5563 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5564 | mddev->pers->quiesce(mddev, 1); |
| 5565 | rv = bitmap_create(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5566 | if (!rv) |
| 5567 | rv = bitmap_load(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5568 | if (rv) |
| 5569 | bitmap_destroy(mddev); |
| 5570 | mddev->pers->quiesce(mddev, 0); |
| 5571 | } else { |
| 5572 | /* remove the bitmap */ |
| 5573 | if (!mddev->bitmap) |
| 5574 | return -ENOENT; |
| 5575 | if (mddev->bitmap->file) |
| 5576 | return -EINVAL; |
| 5577 | mddev->pers->quiesce(mddev, 1); |
| 5578 | bitmap_destroy(mddev); |
| 5579 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5580 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5581 | } |
| 5582 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5583 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | return rv; |
| 5585 | } |
| 5586 | |
| 5587 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5588 | { |
| 5589 | mdk_rdev_t *rdev; |
| 5590 | |
| 5591 | if (mddev->pers == NULL) |
| 5592 | return -ENODEV; |
| 5593 | |
| 5594 | rdev = find_rdev(mddev, dev); |
| 5595 | if (!rdev) |
| 5596 | return -ENODEV; |
| 5597 | |
| 5598 | md_error(mddev, rdev); |
| 5599 | return 0; |
| 5600 | } |
| 5601 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5602 | /* |
| 5603 | * We have a problem here : there is no easy way to give a CHS |
| 5604 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5605 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5606 | * dosfs just mad... ;-) |
| 5607 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5608 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5609 | { |
| 5610 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5611 | |
| 5612 | geo->heads = 2; |
| 5613 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5614 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5615 | return 0; |
| 5616 | } |
| 5617 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5618 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5619 | unsigned int cmd, unsigned long arg) |
| 5620 | { |
| 5621 | int err = 0; |
| 5622 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5623 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5624 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | |
| 5626 | if (!capable(CAP_SYS_ADMIN)) |
| 5627 | return -EACCES; |
| 5628 | |
| 5629 | /* |
| 5630 | * Commands dealing with the RAID driver but not any |
| 5631 | * particular array: |
| 5632 | */ |
| 5633 | switch (cmd) |
| 5634 | { |
| 5635 | case RAID_VERSION: |
| 5636 | err = get_version(argp); |
| 5637 | goto done; |
| 5638 | |
| 5639 | case PRINT_RAID_DEBUG: |
| 5640 | err = 0; |
| 5641 | md_print_devices(); |
| 5642 | goto done; |
| 5643 | |
| 5644 | #ifndef MODULE |
| 5645 | case RAID_AUTORUN: |
| 5646 | err = 0; |
| 5647 | autostart_arrays(arg); |
| 5648 | goto done; |
| 5649 | #endif |
| 5650 | default:; |
| 5651 | } |
| 5652 | |
| 5653 | /* |
| 5654 | * Commands creating/starting a new array: |
| 5655 | */ |
| 5656 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5657 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | |
| 5659 | if (!mddev) { |
| 5660 | BUG(); |
| 5661 | goto abort; |
| 5662 | } |
| 5663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | err = mddev_lock(mddev); |
| 5665 | if (err) { |
| 5666 | printk(KERN_INFO |
| 5667 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5668 | err, cmd); |
| 5669 | goto abort; |
| 5670 | } |
| 5671 | |
| 5672 | switch (cmd) |
| 5673 | { |
| 5674 | case SET_ARRAY_INFO: |
| 5675 | { |
| 5676 | mdu_array_info_t info; |
| 5677 | if (!arg) |
| 5678 | memset(&info, 0, sizeof(info)); |
| 5679 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5680 | err = -EFAULT; |
| 5681 | goto abort_unlock; |
| 5682 | } |
| 5683 | if (mddev->pers) { |
| 5684 | err = update_array_info(mddev, &info); |
| 5685 | if (err) { |
| 5686 | printk(KERN_WARNING "md: couldn't update" |
| 5687 | " array info. %d\n", err); |
| 5688 | goto abort_unlock; |
| 5689 | } |
| 5690 | goto done_unlock; |
| 5691 | } |
| 5692 | if (!list_empty(&mddev->disks)) { |
| 5693 | printk(KERN_WARNING |
| 5694 | "md: array %s already has disks!\n", |
| 5695 | mdname(mddev)); |
| 5696 | err = -EBUSY; |
| 5697 | goto abort_unlock; |
| 5698 | } |
| 5699 | if (mddev->raid_disks) { |
| 5700 | printk(KERN_WARNING |
| 5701 | "md: array %s already initialised!\n", |
| 5702 | mdname(mddev)); |
| 5703 | err = -EBUSY; |
| 5704 | goto abort_unlock; |
| 5705 | } |
| 5706 | err = set_array_info(mddev, &info); |
| 5707 | if (err) { |
| 5708 | printk(KERN_WARNING "md: couldn't set" |
| 5709 | " array info. %d\n", err); |
| 5710 | goto abort_unlock; |
| 5711 | } |
| 5712 | } |
| 5713 | goto done_unlock; |
| 5714 | |
| 5715 | default:; |
| 5716 | } |
| 5717 | |
| 5718 | /* |
| 5719 | * Commands querying/configuring an existing array: |
| 5720 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5721 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5722 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5723 | if ((!mddev->raid_disks && !mddev->external) |
| 5724 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5725 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5726 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | err = -ENODEV; |
| 5728 | goto abort_unlock; |
| 5729 | } |
| 5730 | |
| 5731 | /* |
| 5732 | * Commands even a read-only array can execute: |
| 5733 | */ |
| 5734 | switch (cmd) |
| 5735 | { |
| 5736 | case GET_ARRAY_INFO: |
| 5737 | err = get_array_info(mddev, argp); |
| 5738 | goto done_unlock; |
| 5739 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5740 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5741 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5742 | goto done_unlock; |
| 5743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | case GET_DISK_INFO: |
| 5745 | err = get_disk_info(mddev, argp); |
| 5746 | goto done_unlock; |
| 5747 | |
| 5748 | case RESTART_ARRAY_RW: |
| 5749 | err = restart_array(mddev); |
| 5750 | goto done_unlock; |
| 5751 | |
| 5752 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5753 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5754 | goto done_unlock; |
| 5755 | |
| 5756 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5757 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | goto done_unlock; |
| 5759 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5760 | case BLKROSET: |
| 5761 | if (get_user(ro, (int __user *)(arg))) { |
| 5762 | err = -EFAULT; |
| 5763 | goto done_unlock; |
| 5764 | } |
| 5765 | err = -EINVAL; |
| 5766 | |
| 5767 | /* if the bdev is going readonly the value of mddev->ro |
| 5768 | * does not matter, no writes are coming |
| 5769 | */ |
| 5770 | if (ro) |
| 5771 | goto done_unlock; |
| 5772 | |
| 5773 | /* are we are already prepared for writes? */ |
| 5774 | if (mddev->ro != 1) |
| 5775 | goto done_unlock; |
| 5776 | |
| 5777 | /* transitioning to readauto need only happen for |
| 5778 | * arrays that call md_write_start |
| 5779 | */ |
| 5780 | if (mddev->pers) { |
| 5781 | err = restart_array(mddev); |
| 5782 | if (err == 0) { |
| 5783 | mddev->ro = 2; |
| 5784 | set_disk_ro(mddev->gendisk, 0); |
| 5785 | } |
| 5786 | } |
| 5787 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5788 | } |
| 5789 | |
| 5790 | /* |
| 5791 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5792 | * superblock, so we do not allow them on read-only arrays. |
| 5793 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5794 | * here and hit the 'default' below, so only disallow |
| 5795 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5796 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5797 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5798 | if (mddev->ro == 2) { |
| 5799 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5800 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5801 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5802 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5803 | } else { |
| 5804 | err = -EROFS; |
| 5805 | goto abort_unlock; |
| 5806 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | } |
| 5808 | |
| 5809 | switch (cmd) |
| 5810 | { |
| 5811 | case ADD_NEW_DISK: |
| 5812 | { |
| 5813 | mdu_disk_info_t info; |
| 5814 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5815 | err = -EFAULT; |
| 5816 | else |
| 5817 | err = add_new_disk(mddev, &info); |
| 5818 | goto done_unlock; |
| 5819 | } |
| 5820 | |
| 5821 | case HOT_REMOVE_DISK: |
| 5822 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5823 | goto done_unlock; |
| 5824 | |
| 5825 | case HOT_ADD_DISK: |
| 5826 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5827 | goto done_unlock; |
| 5828 | |
| 5829 | case SET_DISK_FAULTY: |
| 5830 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5831 | goto done_unlock; |
| 5832 | |
| 5833 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5834 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5835 | goto done_unlock; |
| 5836 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5837 | case SET_BITMAP_FILE: |
| 5838 | err = set_bitmap_file(mddev, (int)arg); |
| 5839 | goto done_unlock; |
| 5840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5841 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5842 | err = -EINVAL; |
| 5843 | goto abort_unlock; |
| 5844 | } |
| 5845 | |
| 5846 | done_unlock: |
| 5847 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5848 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5849 | err != -EINVAL) |
| 5850 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | mddev_unlock(mddev); |
| 5852 | |
| 5853 | return err; |
| 5854 | done: |
| 5855 | if (err) |
| 5856 | MD_BUG(); |
| 5857 | abort: |
| 5858 | return err; |
| 5859 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5860 | #ifdef CONFIG_COMPAT |
| 5861 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5862 | unsigned int cmd, unsigned long arg) |
| 5863 | { |
| 5864 | switch (cmd) { |
| 5865 | case HOT_REMOVE_DISK: |
| 5866 | case HOT_ADD_DISK: |
| 5867 | case SET_DISK_FAULTY: |
| 5868 | case SET_BITMAP_FILE: |
| 5869 | /* These take in integer arg, do not convert */ |
| 5870 | break; |
| 5871 | default: |
| 5872 | arg = (unsigned long)compat_ptr(arg); |
| 5873 | break; |
| 5874 | } |
| 5875 | |
| 5876 | return md_ioctl(bdev, mode, cmd, arg); |
| 5877 | } |
| 5878 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5880 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | { |
| 5882 | /* |
| 5883 | * Succeed if we can lock the mddev, which confirms that |
| 5884 | * it isn't being stopped right now. |
| 5885 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5886 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | int err; |
| 5888 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5889 | if (mddev->gendisk != bdev->bd_disk) { |
| 5890 | /* we are racing with mddev_put which is discarding this |
| 5891 | * bd_disk. |
| 5892 | */ |
| 5893 | mddev_put(mddev); |
| 5894 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5895 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5896 | /* Then retry the open from the top */ |
| 5897 | return -ERESTARTSYS; |
| 5898 | } |
| 5899 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5900 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5901 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | goto out; |
| 5903 | |
| 5904 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5905 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5906 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5907 | |
NeilBrown | f3b99be | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 5908 | check_disk_size_change(mddev->gendisk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | out: |
| 5910 | return err; |
| 5911 | } |
| 5912 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5913 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5915 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5916 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5917 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5918 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5919 | mddev_put(mddev); |
| 5920 | |
| 5921 | return 0; |
| 5922 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5923 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | { |
| 5925 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5926 | .open = md_open, |
| 5927 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5928 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5929 | #ifdef CONFIG_COMPAT |
| 5930 | .compat_ioctl = md_compat_ioctl, |
| 5931 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5932 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | }; |
| 5934 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5935 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | { |
| 5937 | mdk_thread_t *thread = arg; |
| 5938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | /* |
| 5940 | * md_thread is a 'system-thread', it's priority should be very |
| 5941 | * high. We avoid resource deadlocks individually in each |
| 5942 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5943 | * the very same RT priority as kswapd, thus we will never get |
| 5944 | * into a priority inversion deadlock. |
| 5945 | * |
| 5946 | * we definitely have to have equal or higher priority than |
| 5947 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5948 | * many dirty RAID5 blocks. |
| 5949 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5951 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5952 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5954 | /* We need to wait INTERRUPTIBLE so that |
| 5955 | * we don't add to the load-average. |
| 5956 | * That means we need to be sure no signals are |
| 5957 | * pending |
| 5958 | */ |
| 5959 | if (signal_pending(current)) |
| 5960 | flush_signals(current); |
| 5961 | |
| 5962 | wait_event_interruptible_timeout |
| 5963 | (thread->wqueue, |
| 5964 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5965 | || kthread_should_stop(), |
| 5966 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5967 | |
| 5968 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5969 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5970 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5971 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5973 | return 0; |
| 5974 | } |
| 5975 | |
| 5976 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5977 | { |
| 5978 | if (thread) { |
| 5979 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5980 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5981 | wake_up(&thread->wqueue); |
| 5982 | } |
| 5983 | } |
| 5984 | |
| 5985 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5986 | const char *name) |
| 5987 | { |
| 5988 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5990 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | if (!thread) |
| 5992 | return NULL; |
| 5993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5994 | init_waitqueue_head(&thread->wqueue); |
| 5995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5996 | thread->run = run; |
| 5997 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5998 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5999 | thread->tsk = kthread_run(md_thread, thread, |
| 6000 | "%s_%s", |
| 6001 | mdname(thread->mddev), |
| 6002 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6003 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | kfree(thread); |
| 6005 | return NULL; |
| 6006 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | return thread; |
| 6008 | } |
| 6009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | void md_unregister_thread(mdk_thread_t *thread) |
| 6011 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6012 | if (!thread) |
| 6013 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6014 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6015 | |
| 6016 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | kfree(thread); |
| 6018 | } |
| 6019 | |
| 6020 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6021 | { |
| 6022 | if (!mddev) { |
| 6023 | MD_BUG(); |
| 6024 | return; |
| 6025 | } |
| 6026 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6027 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6029 | |
| 6030 | if (mddev->external) |
| 6031 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6032 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6034 | mdname(mddev), |
| 6035 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6036 | __builtin_return_address(0),__builtin_return_address(1), |
| 6037 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6038 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6039 | if (!mddev->pers) |
| 6040 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6041 | if (!mddev->pers->error_handler) |
| 6042 | return; |
| 6043 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6044 | if (mddev->degraded) |
| 6045 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6046 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6048 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6049 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 6050 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 6051 | queue_work(md_misc_wq, &mddev->event_work); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6052 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6053 | } |
| 6054 | |
| 6055 | /* seq_file implementation /proc/mdstat */ |
| 6056 | |
| 6057 | static void status_unused(struct seq_file *seq) |
| 6058 | { |
| 6059 | int i = 0; |
| 6060 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | |
| 6062 | seq_printf(seq, "unused devices: "); |
| 6063 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6064 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | char b[BDEVNAME_SIZE]; |
| 6066 | i++; |
| 6067 | seq_printf(seq, "%s ", |
| 6068 | bdevname(rdev->bdev,b)); |
| 6069 | } |
| 6070 | if (!i) |
| 6071 | seq_printf(seq, "<none>"); |
| 6072 | |
| 6073 | seq_printf(seq, "\n"); |
| 6074 | } |
| 6075 | |
| 6076 | |
| 6077 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6078 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6079 | sector_t max_sectors, resync, res; |
| 6080 | unsigned long dt, db; |
| 6081 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6082 | int scale; |
| 6083 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6085 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6086 | |
| 6087 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6088 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6089 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6090 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | |
| 6092 | /* |
| 6093 | * Should not happen. |
| 6094 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6095 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | MD_BUG(); |
| 6097 | return; |
| 6098 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6099 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6100 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6101 | * u32, as those are the requirements for sector_div. |
| 6102 | * Thus 'scale' must be at least 10 |
| 6103 | */ |
| 6104 | scale = 10; |
| 6105 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6106 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6107 | scale++; |
| 6108 | } |
| 6109 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6110 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6111 | |
| 6112 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6113 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6114 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | seq_printf(seq, "["); |
| 6116 | for (i = 0; i < x; i++) |
| 6117 | seq_printf(seq, "="); |
| 6118 | seq_printf(seq, ">"); |
| 6119 | for (i = 0; i < y; i++) |
| 6120 | seq_printf(seq, "."); |
| 6121 | seq_printf(seq, "] "); |
| 6122 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6123 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6124 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6125 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6126 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6127 | "check" : |
| 6128 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6129 | "resync" : "recovery"))), |
| 6130 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6131 | (unsigned long long) resync/2, |
| 6132 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6133 | |
| 6134 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | * dt: time from mark until now |
| 6136 | * db: blocks written from mark until now |
| 6137 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6138 | * |
| 6139 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6140 | * So we divide before multiply in case it is 32bit and close |
| 6141 | * to the limit. |
| 6142 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6143 | * near the end of resync when the number of remaining sectors |
| 6144 | * is close to 'db'. |
| 6145 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6146 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | */ |
| 6148 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6149 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6150 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6151 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6152 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6153 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6154 | sector_div(rt, db/32+1); |
| 6155 | rt *= dt; |
| 6156 | rt >>= 5; |
| 6157 | |
| 6158 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6159 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6161 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | } |
| 6163 | |
| 6164 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6165 | { |
| 6166 | struct list_head *tmp; |
| 6167 | loff_t l = *pos; |
| 6168 | mddev_t *mddev; |
| 6169 | |
| 6170 | if (l >= 0x10000) |
| 6171 | return NULL; |
| 6172 | if (!l--) |
| 6173 | /* header */ |
| 6174 | return (void*)1; |
| 6175 | |
| 6176 | spin_lock(&all_mddevs_lock); |
| 6177 | list_for_each(tmp,&all_mddevs) |
| 6178 | if (!l--) { |
| 6179 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6180 | mddev_get(mddev); |
| 6181 | spin_unlock(&all_mddevs_lock); |
| 6182 | return mddev; |
| 6183 | } |
| 6184 | spin_unlock(&all_mddevs_lock); |
| 6185 | if (!l--) |
| 6186 | return (void*)2;/* tail */ |
| 6187 | return NULL; |
| 6188 | } |
| 6189 | |
| 6190 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6191 | { |
| 6192 | struct list_head *tmp; |
| 6193 | mddev_t *next_mddev, *mddev = v; |
| 6194 | |
| 6195 | ++*pos; |
| 6196 | if (v == (void*)2) |
| 6197 | return NULL; |
| 6198 | |
| 6199 | spin_lock(&all_mddevs_lock); |
| 6200 | if (v == (void*)1) |
| 6201 | tmp = all_mddevs.next; |
| 6202 | else |
| 6203 | tmp = mddev->all_mddevs.next; |
| 6204 | if (tmp != &all_mddevs) |
| 6205 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6206 | else { |
| 6207 | next_mddev = (void*)2; |
| 6208 | *pos = 0x10000; |
| 6209 | } |
| 6210 | spin_unlock(&all_mddevs_lock); |
| 6211 | |
| 6212 | if (v != (void*)1) |
| 6213 | mddev_put(mddev); |
| 6214 | return next_mddev; |
| 6215 | |
| 6216 | } |
| 6217 | |
| 6218 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6219 | { |
| 6220 | mddev_t *mddev = v; |
| 6221 | |
| 6222 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6223 | mddev_put(mddev); |
| 6224 | } |
| 6225 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6226 | struct mdstat_info { |
| 6227 | int event; |
| 6228 | }; |
| 6229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6230 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6231 | { |
| 6232 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6233 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6234 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6235 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6236 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | |
| 6238 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6239 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | seq_printf(seq, "Personalities : "); |
| 6241 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6242 | list_for_each_entry(pers, &pers_list, list) |
| 6243 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6244 | |
| 6245 | spin_unlock(&pers_lock); |
| 6246 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6247 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | return 0; |
| 6249 | } |
| 6250 | if (v == (void*)2) { |
| 6251 | status_unused(seq); |
| 6252 | return 0; |
| 6253 | } |
| 6254 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6255 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6258 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6259 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6260 | mddev->pers ? "" : "in"); |
| 6261 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6262 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6263 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6264 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6265 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6266 | seq_printf(seq, " %s", mddev->pers->name); |
| 6267 | } |
| 6268 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6269 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6270 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | char b[BDEVNAME_SIZE]; |
| 6272 | seq_printf(seq, " %s[%d]", |
| 6273 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6274 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6275 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6276 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6277 | seq_printf(seq, "(F)"); |
| 6278 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6279 | } else if (rdev->raid_disk < 0) |
| 6280 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6281 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | } |
| 6283 | |
| 6284 | if (!list_empty(&mddev->disks)) { |
| 6285 | if (mddev->pers) |
| 6286 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6287 | (unsigned long long) |
| 6288 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6289 | else |
| 6290 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6291 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6292 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6293 | if (mddev->persistent) { |
| 6294 | if (mddev->major_version != 0 || |
| 6295 | mddev->minor_version != 90) { |
| 6296 | seq_printf(seq," super %d.%d", |
| 6297 | mddev->major_version, |
| 6298 | mddev->minor_version); |
| 6299 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6300 | } else if (mddev->external) |
| 6301 | seq_printf(seq, " super external:%s", |
| 6302 | mddev->metadata_type); |
| 6303 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6304 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6305 | |
| 6306 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6307 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6309 | if (mddev->pers->sync_request) { |
| 6310 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6311 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6312 | seq_printf(seq, "\n "); |
| 6313 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6314 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6315 | else if (mddev->recovery_cp < MaxSector) |
| 6316 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6317 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6318 | } else |
| 6319 | seq_printf(seq, "\n "); |
| 6320 | |
| 6321 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6322 | unsigned long chunk_kb; |
| 6323 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6324 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6325 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6326 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6327 | "%lu%s chunk", |
| 6328 | bitmap->pages - bitmap->missing_pages, |
| 6329 | bitmap->pages, |
| 6330 | (bitmap->pages - bitmap->missing_pages) |
| 6331 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6332 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6333 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6334 | if (bitmap->file) { |
| 6335 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6336 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6337 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6338 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6339 | seq_printf(seq, "\n"); |
| 6340 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6341 | } |
| 6342 | |
| 6343 | seq_printf(seq, "\n"); |
| 6344 | } |
| 6345 | mddev_unlock(mddev); |
| 6346 | |
| 6347 | return 0; |
| 6348 | } |
| 6349 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6350 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | .start = md_seq_start, |
| 6352 | .next = md_seq_next, |
| 6353 | .stop = md_seq_stop, |
| 6354 | .show = md_seq_show, |
| 6355 | }; |
| 6356 | |
| 6357 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6358 | { |
| 6359 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6360 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6361 | if (mi == NULL) |
| 6362 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | |
| 6364 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6365 | if (error) |
| 6366 | kfree(mi); |
| 6367 | else { |
| 6368 | struct seq_file *p = file->private_data; |
| 6369 | p->private = mi; |
| 6370 | mi->event = atomic_read(&md_event_count); |
| 6371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | return error; |
| 6373 | } |
| 6374 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6375 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6376 | { |
| 6377 | struct seq_file *m = filp->private_data; |
| 6378 | struct mdstat_info *mi = m->private; |
| 6379 | int mask; |
| 6380 | |
| 6381 | poll_wait(filp, &md_event_waiters, wait); |
| 6382 | |
| 6383 | /* always allow read */ |
| 6384 | mask = POLLIN | POLLRDNORM; |
| 6385 | |
| 6386 | if (mi->event != atomic_read(&md_event_count)) |
| 6387 | mask |= POLLERR | POLLPRI; |
| 6388 | return mask; |
| 6389 | } |
| 6390 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6391 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6392 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | .open = md_seq_open, |
| 6394 | .read = seq_read, |
| 6395 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6396 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6397 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | }; |
| 6399 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6400 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6403 | list_add_tail(&p->list, &pers_list); |
| 6404 | 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] | 6405 | spin_unlock(&pers_lock); |
| 6406 | return 0; |
| 6407 | } |
| 6408 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6409 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6410 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6411 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6412 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6413 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6414 | spin_unlock(&pers_lock); |
| 6415 | return 0; |
| 6416 | } |
| 6417 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6418 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6419 | { |
| 6420 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6422 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | |
| 6424 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6425 | rcu_read_lock(); |
| 6426 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6427 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6428 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6429 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6430 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6431 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6432 | * as sync_io is counted when a request starts, and |
| 6433 | * disk_stats is counted when it completes. |
| 6434 | * So resync activity will cause curr_events to be smaller than |
| 6435 | * when there was no such activity. |
| 6436 | * non-sync IO will cause disk_stat to increase without |
| 6437 | * increasing sync_io so curr_events will (eventually) |
| 6438 | * be larger than it was before. Once it becomes |
| 6439 | * substantially larger, the test below will cause |
| 6440 | * the array to appear non-idle, and resync will slow |
| 6441 | * down. |
| 6442 | * If there is a lot of outstanding resync activity when |
| 6443 | * we set last_event to curr_events, then all that activity |
| 6444 | * completing might cause the array to appear non-idle |
| 6445 | * and resync will be slowed down even though there might |
| 6446 | * not have been non-resync activity. This will only |
| 6447 | * happen once though. 'last_events' will soon reflect |
| 6448 | * the state where there is little or no outstanding |
| 6449 | * resync requests, and further resync activity will |
| 6450 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6451 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6452 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6453 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | rdev->last_events = curr_events; |
| 6455 | idle = 0; |
| 6456 | } |
| 6457 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6458 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6459 | return idle; |
| 6460 | } |
| 6461 | |
| 6462 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6463 | { |
| 6464 | /* another "blocks" (512byte) blocks have been synced */ |
| 6465 | atomic_sub(blocks, &mddev->recovery_active); |
| 6466 | wake_up(&mddev->recovery_wait); |
| 6467 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6468 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | md_wakeup_thread(mddev->thread); |
| 6470 | // stop recovery, signal do_sync .... |
| 6471 | } |
| 6472 | } |
| 6473 | |
| 6474 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6475 | /* md_write_start(mddev, bi) |
| 6476 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6477 | * in superblock) before writing, schedule a superblock update |
| 6478 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6479 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6480 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6481 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6482 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6483 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6484 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6485 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6486 | BUG_ON(mddev->ro == 1); |
| 6487 | if (mddev->ro == 2) { |
| 6488 | /* need to switch to read/write */ |
| 6489 | mddev->ro = 0; |
| 6490 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6491 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6492 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6493 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6494 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6495 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6496 | if (mddev->safemode == 1) |
| 6497 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6498 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6499 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6500 | if (mddev->in_sync) { |
| 6501 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6502 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6503 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6504 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6505 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6506 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6507 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6508 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6509 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6510 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6511 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6512 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6513 | } |
| 6514 | |
| 6515 | void md_write_end(mddev_t *mddev) |
| 6516 | { |
| 6517 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6518 | if (mddev->safemode == 2) |
| 6519 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6520 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6521 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6522 | } |
| 6523 | } |
| 6524 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6525 | /* md_allow_write(mddev) |
| 6526 | * Calling this ensures that the array is marked 'active' so that writes |
| 6527 | * may proceed without blocking. It is important to call this before |
| 6528 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6529 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6530 | * |
| 6531 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6532 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6533 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6534 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6535 | { |
| 6536 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6537 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6538 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6539 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6540 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6541 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6542 | |
| 6543 | spin_lock_irq(&mddev->write_lock); |
| 6544 | if (mddev->in_sync) { |
| 6545 | mddev->in_sync = 0; |
| 6546 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6547 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6548 | if (mddev->safemode_delay && |
| 6549 | mddev->safemode == 0) |
| 6550 | mddev->safemode = 1; |
| 6551 | spin_unlock_irq(&mddev->write_lock); |
| 6552 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6553 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6554 | } else |
| 6555 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6556 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6557 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6558 | return -EAGAIN; |
| 6559 | else |
| 6560 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6561 | } |
| 6562 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6563 | |
NeilBrown | b63d7c2 | 2010-06-01 19:37:33 +1000 | [diff] [blame] | 6564 | void md_unplug(mddev_t *mddev) |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6565 | { |
| 6566 | if (mddev->queue) |
| 6567 | blk_unplug(mddev->queue); |
| 6568 | if (mddev->plug) |
| 6569 | mddev->plug->unplug_fn(mddev->plug); |
| 6570 | } |
| 6571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6572 | #define SYNC_MARKS 10 |
| 6573 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6574 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6575 | { |
| 6576 | mddev_t *mddev2; |
| 6577 | unsigned int currspeed = 0, |
| 6578 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6579 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6580 | unsigned long mark[SYNC_MARKS]; |
| 6581 | sector_t mark_cnt[SYNC_MARKS]; |
| 6582 | int last_mark,m; |
| 6583 | struct list_head *tmp; |
| 6584 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6585 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6586 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6587 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6588 | |
| 6589 | /* just incase thread restarts... */ |
| 6590 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6591 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6592 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6593 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6594 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6595 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6596 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6597 | desc = "data-check"; |
| 6598 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6599 | desc = "requested-resync"; |
| 6600 | else |
| 6601 | desc = "resync"; |
| 6602 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6603 | desc = "reshape"; |
| 6604 | else |
| 6605 | desc = "recovery"; |
| 6606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 | /* we overload curr_resync somewhat here. |
| 6608 | * 0 == not engaged in resync at all |
| 6609 | * 2 == checking that there is no conflict with another sync |
| 6610 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6611 | * commense |
| 6612 | * other == active in resync - this many blocks |
| 6613 | * |
| 6614 | * Before starting a resync we must have set curr_resync to |
| 6615 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6616 | * less than ours. When we find one that is the same or higher |
| 6617 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6618 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6619 | * This will mean we have to start checking from the beginning again. |
| 6620 | * |
| 6621 | */ |
| 6622 | |
| 6623 | do { |
| 6624 | mddev->curr_resync = 2; |
| 6625 | |
| 6626 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6627 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6628 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6629 | |
| 6630 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6631 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6632 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | if (mddev2 == mddev) |
| 6634 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6635 | if (!mddev->parallel_resync |
| 6636 | && mddev2->curr_resync |
| 6637 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6638 | DEFINE_WAIT(wq); |
| 6639 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6640 | /* arbitrarily yield */ |
| 6641 | mddev->curr_resync = 1; |
| 6642 | wake_up(&resync_wait); |
| 6643 | } |
| 6644 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6645 | /* no need to wait here, we can wait the next |
| 6646 | * time 'round when curr_resync == 2 |
| 6647 | */ |
| 6648 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6649 | /* We need to wait 'interruptible' so as not to |
| 6650 | * contribute to the load average, and not to |
| 6651 | * be caught by 'softlockup' |
| 6652 | */ |
| 6653 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6654 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6655 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6656 | printk(KERN_INFO "md: delaying %s of %s" |
| 6657 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6658 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6659 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6661 | if (signal_pending(current)) |
| 6662 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | schedule(); |
| 6664 | finish_wait(&resync_wait, &wq); |
| 6665 | goto try_again; |
| 6666 | } |
| 6667 | finish_wait(&resync_wait, &wq); |
| 6668 | } |
| 6669 | } |
| 6670 | } while (mddev->curr_resync < 2); |
| 6671 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6672 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6673 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6674 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6675 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6676 | */ |
| 6677 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6678 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6679 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6680 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6681 | j = mddev->resync_min; |
| 6682 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6683 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6684 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6685 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6686 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6687 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6688 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6689 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6690 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6691 | rcu_read_lock(); |
| 6692 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6693 | if (rdev->raid_disk >= 0 && |
| 6694 | !test_bit(Faulty, &rdev->flags) && |
| 6695 | !test_bit(In_sync, &rdev->flags) && |
| 6696 | rdev->recovery_offset < j) |
| 6697 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6698 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6700 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6701 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6702 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6703 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6704 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6705 | "(but not more than %d KB/sec) for %s.\n", |
| 6706 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6707 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6708 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6709 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6710 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6712 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6713 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6714 | } |
| 6715 | last_mark = 0; |
| 6716 | mddev->resync_mark = mark[last_mark]; |
| 6717 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6718 | |
| 6719 | /* |
| 6720 | * Tune reconstruction: |
| 6721 | */ |
| 6722 | window = 32*(PAGE_SIZE/512); |
| 6723 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6724 | window/2,(unsigned long long) max_sectors/2); |
| 6725 | |
| 6726 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | last_check = 0; |
| 6728 | |
| 6729 | if (j>2) { |
| 6730 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6731 | "md: resuming %s of %s from checkpoint.\n", |
| 6732 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6733 | mddev->curr_resync = j; |
| 6734 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6735 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6736 | |
| 6737 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6738 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6739 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6740 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6741 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6742 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6743 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6744 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6745 | > (max_sectors >> 4)) || |
| 6746 | (j - mddev->curr_resync_completed)*2 |
| 6747 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6748 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6749 | /* time to update curr_resync_completed */ |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6750 | md_unplug(mddev); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6751 | wait_event(mddev->recovery_wait, |
| 6752 | atomic_read(&mddev->recovery_active) == 0); |
| 6753 | mddev->curr_resync_completed = |
| 6754 | mddev->curr_resync; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 6755 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6756 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6757 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6758 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6759 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6760 | /* As this condition is controlled by user-space, |
| 6761 | * we can block indefinitely, so use '_interruptible' |
| 6762 | * to avoid triggering warnings. |
| 6763 | */ |
| 6764 | flush_signals(current); /* just in case */ |
| 6765 | wait_event_interruptible(mddev->recovery_wait, |
| 6766 | mddev->resync_max > j |
| 6767 | || kthread_should_stop()); |
| 6768 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6769 | |
| 6770 | if (kthread_should_stop()) |
| 6771 | goto interrupted; |
| 6772 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6773 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6774 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6775 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6776 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6777 | goto out; |
| 6778 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6779 | |
| 6780 | if (!skipped) { /* actual IO requested */ |
| 6781 | io_sectors += sectors; |
| 6782 | atomic_add(sectors, &mddev->recovery_active); |
| 6783 | } |
| 6784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6785 | j += sectors; |
| 6786 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6787 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6788 | if (last_check == 0) |
| 6789 | /* this is the earliers that rebuilt will be |
| 6790 | * visible in /proc/mdstat |
| 6791 | */ |
| 6792 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6793 | |
| 6794 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6795 | continue; |
| 6796 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6797 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6799 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6800 | break; |
| 6801 | |
| 6802 | repeat: |
| 6803 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6804 | /* step marks */ |
| 6805 | int next = (last_mark+1) % SYNC_MARKS; |
| 6806 | |
| 6807 | mddev->resync_mark = mark[next]; |
| 6808 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6809 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6810 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6811 | last_mark = next; |
| 6812 | } |
| 6813 | |
| 6814 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6815 | if (kthread_should_stop()) |
| 6816 | goto interrupted; |
| 6817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6818 | |
| 6819 | /* |
| 6820 | * this loop exits only if either when we are slower than |
| 6821 | * the 'hard' speed limit, or the system was IO-idle for |
| 6822 | * a jiffy. |
| 6823 | * the system might be non-idle CPU-wise, but we only care |
| 6824 | * about not overloading the IO subsystem. (things like an |
| 6825 | * e2fsck being done on the RAID array should execute fast) |
| 6826 | */ |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6827 | md_unplug(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | cond_resched(); |
| 6829 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6830 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6831 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6833 | if (currspeed > speed_min(mddev)) { |
| 6834 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6835 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6836 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | goto repeat; |
| 6838 | } |
| 6839 | } |
| 6840 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6841 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | /* |
| 6843 | * this also signals 'finished resyncing' to md_stop |
| 6844 | */ |
| 6845 | out: |
NeilBrown | 252ac52 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 6846 | md_unplug(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6847 | |
| 6848 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6849 | |
| 6850 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6851 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6853 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6854 | mddev->curr_resync > 2) { |
| 6855 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6856 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6857 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6858 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6859 | "md: checkpointing %s of %s.\n", |
| 6860 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6861 | mddev->recovery_cp = mddev->curr_resync; |
| 6862 | } |
| 6863 | } else |
| 6864 | mddev->recovery_cp = MaxSector; |
| 6865 | } else { |
| 6866 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6867 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6868 | rcu_read_lock(); |
| 6869 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6870 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6871 | mddev->delta_disks >= 0 && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6872 | !test_bit(Faulty, &rdev->flags) && |
| 6873 | !test_bit(In_sync, &rdev->flags) && |
| 6874 | rdev->recovery_offset < mddev->curr_resync) |
| 6875 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6876 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6877 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6879 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6881 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6882 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6883 | /* We completed so min/max setting can be forgotten if used. */ |
| 6884 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6885 | mddev->resync_min = 0; |
| 6886 | mddev->resync_max = MaxSector; |
| 6887 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6888 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6889 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6890 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6891 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6892 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6893 | wake_up(&resync_wait); |
| 6894 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6895 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6896 | return; |
| 6897 | |
| 6898 | interrupted: |
| 6899 | /* |
| 6900 | * got a signal, exit. |
| 6901 | */ |
| 6902 | printk(KERN_INFO |
| 6903 | "md: md_do_sync() got signal ... exiting\n"); |
| 6904 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6905 | goto out; |
| 6906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6907 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6908 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6909 | |
| 6910 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6911 | static int remove_and_add_spares(mddev_t *mddev) |
| 6912 | { |
| 6913 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6914 | int spares = 0; |
| 6915 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6916 | mddev->curr_resync_completed = 0; |
| 6917 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6918 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6919 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6920 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6921 | (test_bit(Faulty, &rdev->flags) || |
| 6922 | ! test_bit(In_sync, &rdev->flags)) && |
| 6923 | atomic_read(&rdev->nr_pending)==0) { |
| 6924 | if (mddev->pers->hot_remove_disk( |
| 6925 | mddev, rdev->raid_disk)==0) { |
| 6926 | char nm[20]; |
| 6927 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6928 | sysfs_remove_link(&mddev->kobj, nm); |
| 6929 | rdev->raid_disk = -1; |
| 6930 | } |
| 6931 | } |
| 6932 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6933 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6934 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6935 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6936 | !test_bit(In_sync, &rdev->flags) && |
| 6937 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6938 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6939 | if (rdev->raid_disk < 0 |
| 6940 | && !test_bit(Faulty, &rdev->flags)) { |
| 6941 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6942 | if (mddev->pers-> |
| 6943 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6944 | char nm[20]; |
| 6945 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6946 | if (sysfs_create_link(&mddev->kobj, |
| 6947 | &rdev->kobj, nm)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6948 | /* failure here is OK */; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6949 | spares++; |
| 6950 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 6951 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6952 | } else |
| 6953 | break; |
| 6954 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6955 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6956 | } |
| 6957 | return spares; |
| 6958 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | /* |
| 6960 | * This routine is regularly called by all per-raid-array threads to |
| 6961 | * deal with generic issues like resync and super-block update. |
| 6962 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6963 | * need this as they never do any recovery or update the superblock. |
| 6964 | * |
| 6965 | * It does not do any resync itself, but rather "forks" off other threads |
| 6966 | * to do that as needed. |
| 6967 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6968 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6969 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | * and wakeups up this thread which will reap the thread and finish up. |
| 6971 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6972 | * |
| 6973 | * The overall approach is: |
| 6974 | * 1/ if the superblock needs updating, update it. |
| 6975 | * 2/ If a recovery thread is running, don't do anything else. |
| 6976 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6977 | * 4/ If there are any faulty devices, remove them. |
| 6978 | * 5/ If array is degraded, try to add spares devices |
| 6979 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6980 | */ |
| 6981 | void md_check_recovery(mddev_t *mddev) |
| 6982 | { |
| 6983 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6984 | |
| 6985 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6986 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6987 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6988 | |
| 6989 | if (mddev->ro) |
| 6990 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6991 | |
| 6992 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6993 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6994 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6995 | mdname(mddev)); |
| 6996 | mddev->safemode = 2; |
| 6997 | } |
| 6998 | flush_signals(current); |
| 6999 | } |
| 7000 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7001 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7002 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7003 | if ( ! ( |
NeilBrown | 126925c | 2010-09-07 17:02:47 +1000 | [diff] [blame] | 7004 | (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7005 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7006 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7007 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7008 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7009 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7010 | )) |
| 7011 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7012 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7013 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7014 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7015 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7016 | if (mddev->ro) { |
| 7017 | /* Only thing we do on a ro array is remove |
| 7018 | * failed devices. |
| 7019 | */ |
| 7020 | remove_and_add_spares(mddev); |
| 7021 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7022 | goto unlock; |
| 7023 | } |
| 7024 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7025 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7026 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7027 | spin_lock_irq(&mddev->write_lock); |
| 7028 | if (mddev->safemode && |
| 7029 | !atomic_read(&mddev->writes_pending) && |
| 7030 | !mddev->in_sync && |
| 7031 | mddev->recovery_cp == MaxSector) { |
| 7032 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7033 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7034 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7035 | } |
| 7036 | if (mddev->safemode == 1) |
| 7037 | mddev->safemode = 0; |
| 7038 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7039 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7040 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7041 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7042 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7043 | if (mddev->flags) |
| 7044 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7046 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7047 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7048 | /* resync/recovery still happening */ |
| 7049 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7050 | goto unlock; |
| 7051 | } |
| 7052 | if (mddev->sync_thread) { |
| 7053 | /* resync has finished, collect result */ |
| 7054 | md_unregister_thread(mddev->sync_thread); |
| 7055 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7056 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7057 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7058 | /* success...*/ |
| 7059 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 7060 | if (mddev->pers->spare_active(mddev)) |
| 7061 | sysfs_notify(&mddev->kobj, NULL, |
| 7062 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7063 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7064 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7065 | mddev->pers->finish_reshape) |
| 7066 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7067 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7068 | |
| 7069 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7070 | * information must be scrapped |
| 7071 | */ |
| 7072 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7073 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7074 | rdev->saved_raid_disk = -1; |
| 7075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7076 | mddev->recovery = 0; |
| 7077 | /* flag recovery needed just to double check */ |
| 7078 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7079 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7080 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7081 | goto unlock; |
| 7082 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7083 | /* Set RUNNING before clearing NEEDED to avoid |
| 7084 | * any transients in the value of "sync_action". |
| 7085 | */ |
| 7086 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7087 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7088 | /* Clear some bits that don't mean anything, but |
| 7089 | * might be left set |
| 7090 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7091 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7092 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7093 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7094 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7095 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | /* no recovery is running. |
| 7097 | * remove any failed drives, then |
| 7098 | * add spares if possible. |
| 7099 | * Spare are also removed and re-added, to allow |
| 7100 | * the personality to fail the re-add. |
| 7101 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7102 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7103 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7104 | if (mddev->pers->check_reshape == NULL || |
| 7105 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7106 | /* Cannot proceed */ |
| 7107 | goto unlock; |
| 7108 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7109 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7110 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7111 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7112 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7113 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7114 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7115 | } else if (mddev->recovery_cp < MaxSector) { |
| 7116 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7117 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7118 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7119 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7122 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7123 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7124 | /* We are adding a device or devices to an array |
| 7125 | * which has the bitmap stored on all devices. |
| 7126 | * So make sure all bitmap pages get written |
| 7127 | */ |
| 7128 | bitmap_write_all(mddev->bitmap); |
| 7129 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7130 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7131 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7132 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | if (!mddev->sync_thread) { |
| 7134 | printk(KERN_ERR "%s: could not start resync" |
| 7135 | " thread...\n", |
| 7136 | mdname(mddev)); |
| 7137 | /* leave the spares where they are, it shouldn't hurt */ |
| 7138 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7139 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7141 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7142 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | } |
| 7144 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7145 | if (!mddev->sync_thread) { |
| 7146 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7147 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7148 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7149 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7150 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | mddev_unlock(mddev); |
| 7153 | } |
| 7154 | } |
| 7155 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7156 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7157 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7158 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7159 | wait_event_timeout(rdev->blocked_wait, |
| 7160 | !test_bit(Blocked, &rdev->flags), |
| 7161 | msecs_to_jiffies(5000)); |
| 7162 | rdev_dec_pending(rdev, mddev); |
| 7163 | } |
| 7164 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7165 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7166 | static int md_notify_reboot(struct notifier_block *this, |
| 7167 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7168 | { |
| 7169 | struct list_head *tmp; |
| 7170 | mddev_t *mddev; |
| 7171 | |
| 7172 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7173 | |
| 7174 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7175 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7176 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7177 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7178 | /* Force a switch to readonly even array |
| 7179 | * appears to still be in use. Hence |
| 7180 | * the '100'. |
| 7181 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7182 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7183 | mddev_unlock(mddev); |
| 7184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7185 | /* |
| 7186 | * certain more exotic SCSI devices are known to be |
| 7187 | * volatile wrt too early system reboots. While the |
| 7188 | * right place to handle this issue is the given |
| 7189 | * driver, we do want to have a safe RAID driver ... |
| 7190 | */ |
| 7191 | mdelay(1000*1); |
| 7192 | } |
| 7193 | return NOTIFY_DONE; |
| 7194 | } |
| 7195 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7196 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | .notifier_call = md_notify_reboot, |
| 7198 | .next = NULL, |
| 7199 | .priority = INT_MAX, /* before any real devices */ |
| 7200 | }; |
| 7201 | |
| 7202 | static void md_geninit(void) |
| 7203 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7205 | |
Denis V. Lunev | c7705f3 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7206 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | } |
| 7208 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7209 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7210 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7211 | int ret = -ENOMEM; |
| 7212 | |
| 7213 | md_wq = alloc_workqueue("md", WQ_RESCUER, 0); |
| 7214 | if (!md_wq) |
| 7215 | goto err_wq; |
| 7216 | |
| 7217 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 7218 | if (!md_misc_wq) |
| 7219 | goto err_misc_wq; |
| 7220 | |
| 7221 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 7222 | goto err_md; |
| 7223 | |
| 7224 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 7225 | goto err_mdp; |
| 7226 | mdp_major = ret; |
| 7227 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7228 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7229 | md_probe, NULL, NULL); |
| 7230 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7231 | md_probe, NULL, NULL); |
| 7232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7233 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7234 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7235 | |
| 7236 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7237 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7238 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7239 | err_mdp: |
| 7240 | unregister_blkdev(MD_MAJOR, "md"); |
| 7241 | err_md: |
| 7242 | destroy_workqueue(md_misc_wq); |
| 7243 | err_misc_wq: |
| 7244 | destroy_workqueue(md_wq); |
| 7245 | err_wq: |
| 7246 | return ret; |
| 7247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7248 | |
| 7249 | #ifndef MODULE |
| 7250 | |
| 7251 | /* |
| 7252 | * Searches all registered partitions for autorun RAID arrays |
| 7253 | * at boot time. |
| 7254 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7255 | |
| 7256 | static LIST_HEAD(all_detected_devices); |
| 7257 | struct detected_devices_node { |
| 7258 | struct list_head list; |
| 7259 | dev_t dev; |
| 7260 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7261 | |
| 7262 | void md_autodetect_dev(dev_t dev) |
| 7263 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7264 | struct detected_devices_node *node_detected_dev; |
| 7265 | |
| 7266 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7267 | if (node_detected_dev) { |
| 7268 | node_detected_dev->dev = dev; |
| 7269 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7270 | } else { |
| 7271 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7272 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7274 | } |
| 7275 | |
| 7276 | |
| 7277 | static void autostart_arrays(int part) |
| 7278 | { |
| 7279 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7280 | struct detected_devices_node *node_detected_dev; |
| 7281 | dev_t dev; |
| 7282 | int i_scanned, i_passed; |
| 7283 | |
| 7284 | i_scanned = 0; |
| 7285 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7286 | |
| 7287 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7288 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7289 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7290 | i_scanned++; |
| 7291 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7292 | struct detected_devices_node, list); |
| 7293 | list_del(&node_detected_dev->list); |
| 7294 | dev = node_detected_dev->dev; |
| 7295 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7296 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7297 | if (IS_ERR(rdev)) |
| 7298 | continue; |
| 7299 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7300 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7301 | MD_BUG(); |
| 7302 | continue; |
| 7303 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7304 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7305 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7306 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7308 | |
| 7309 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7310 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7311 | |
| 7312 | autorun_devices(part); |
| 7313 | } |
| 7314 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7315 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7316 | |
| 7317 | static __exit void md_exit(void) |
| 7318 | { |
| 7319 | mddev_t *mddev; |
| 7320 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7321 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7322 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7323 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7324 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7325 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7326 | unregister_blkdev(mdp_major, "mdp"); |
| 7327 | unregister_reboot_notifier(&md_notifier); |
| 7328 | unregister_sysctl_table(raid_table_header); |
| 7329 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7330 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7331 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7332 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7333 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7334 | destroy_workqueue(md_misc_wq); |
| 7335 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7336 | } |
| 7337 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7338 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7339 | module_exit(md_exit) |
| 7340 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7341 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7342 | { |
| 7343 | return sprintf(buffer, "%d", start_readonly); |
| 7344 | } |
| 7345 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7346 | { |
| 7347 | char *e; |
| 7348 | int num = simple_strtoul(val, &e, 10); |
| 7349 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7350 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7351 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7352 | } |
| 7353 | return -EINVAL; |
| 7354 | } |
| 7355 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7356 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7357 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7358 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7359 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | EXPORT_SYMBOL(register_md_personality); |
| 7362 | EXPORT_SYMBOL(unregister_md_personality); |
| 7363 | EXPORT_SYMBOL(md_error); |
| 7364 | EXPORT_SYMBOL(md_done_sync); |
| 7365 | EXPORT_SYMBOL(md_write_start); |
| 7366 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7367 | EXPORT_SYMBOL(md_register_thread); |
| 7368 | EXPORT_SYMBOL(md_unregister_thread); |
| 7369 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7370 | EXPORT_SYMBOL(md_check_recovery); |
| 7371 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7372 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7373 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7374 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |