Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
| 46 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 47 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 48 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 49 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 50 | #include <linux/raid/md_p.h> |
| 51 | #include <linux/raid/md_u.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 52 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 53 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | #define DEBUG 0 |
| 56 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 57 | |
| 58 | |
| 59 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 60 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #endif |
| 62 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 63 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | static DEFINE_SPINLOCK(pers_lock); |
| 65 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 66 | static void md_print_devices(void); |
| 67 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 68 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 69 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 70 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 73 | * Default number of read corrections we'll attempt on an rdev |
| 74 | * before ejecting it from the array. We divide the read error |
| 75 | * count by 2 for every hour elapsed between read errors. |
| 76 | */ |
| 77 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 78 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 80 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 81 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 82 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 84 | * speed limit - in case reconstruction slows down your system despite |
| 85 | * idle IO detection. |
| 86 | * |
| 87 | * 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] | 88 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | */ |
| 90 | |
| 91 | static int sysctl_speed_limit_min = 1000; |
| 92 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 93 | static inline int speed_min(mddev_t *mddev) |
| 94 | { |
| 95 | return mddev->sync_speed_min ? |
| 96 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 97 | } |
| 98 | |
| 99 | static inline int speed_max(mddev_t *mddev) |
| 100 | { |
| 101 | return mddev->sync_speed_max ? |
| 102 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
| 105 | static struct ctl_table_header *raid_table_header; |
| 106 | |
| 107 | static ctl_table raid_table[] = { |
| 108 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | .procname = "speed_limit_min", |
| 110 | .data = &sysctl_speed_limit_min, |
| 111 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 112 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 113 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | }, |
| 115 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | .procname = "speed_limit_max", |
| 117 | .data = &sysctl_speed_limit_max, |
| 118 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 119 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 120 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 122 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static ctl_table raid_dir_table[] = { |
| 126 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | .procname = "raid", |
| 128 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 129 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | .child = raid_table, |
| 131 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 132 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | static ctl_table raid_root_table[] = { |
| 136 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | .procname = "dev", |
| 138 | .maxlen = 0, |
| 139 | .mode = 0555, |
| 140 | .child = raid_dir_table, |
| 141 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 142 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 145 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 147 | static int start_readonly; |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 150 | * We have a system wide 'event count' that is incremented |
| 151 | * on any 'interesting' event, and readers of /proc/mdstat |
| 152 | * can use 'poll' or 'select' to find out when the event |
| 153 | * count increases. |
| 154 | * |
| 155 | * Events are: |
| 156 | * start array, stop array, error, add device, remove device, |
| 157 | * start build, activate spare |
| 158 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 159 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 160 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 161 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 162 | { |
| 163 | atomic_inc(&md_event_count); |
| 164 | wake_up(&md_event_waiters); |
| 165 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 166 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 167 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 168 | /* Alternate version that can be called from interrupts |
| 169 | * when calling sysfs_notify isn't needed. |
| 170 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 171 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 172 | { |
| 173 | atomic_inc(&md_event_count); |
| 174 | wake_up(&md_event_waiters); |
| 175 | } |
| 176 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 177 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | * Enables to iterate over all existing md arrays |
| 179 | * all_mddevs_lock protects this list. |
| 180 | */ |
| 181 | static LIST_HEAD(all_mddevs); |
| 182 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 183 | |
| 184 | |
| 185 | /* |
| 186 | * iterates through all used mddevs in the system. |
| 187 | * We take care to grab the all_mddevs_lock whenever navigating |
| 188 | * the list, and to always hold a refcount when unlocked. |
| 189 | * Any code which breaks out of this loop while own |
| 190 | * a reference to the current mddev and must mddev_put it. |
| 191 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 192 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | \ |
| 194 | for (({ spin_lock(&all_mddevs_lock); \ |
| 195 | tmp = all_mddevs.next; \ |
| 196 | mddev = NULL;}); \ |
| 197 | ({ if (tmp != &all_mddevs) \ |
| 198 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 199 | spin_unlock(&all_mddevs_lock); \ |
| 200 | if (mddev) mddev_put(mddev); \ |
| 201 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 202 | tmp != &all_mddevs;}); \ |
| 203 | ({ spin_lock(&all_mddevs_lock); \ |
| 204 | tmp = tmp->next;}) \ |
| 205 | ) |
| 206 | |
| 207 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 208 | /* Rather than calling directly into the personality make_request function, |
| 209 | * IO requests come here first so that we can check if the device is |
| 210 | * being suspended pending a reconfiguration. |
| 211 | * We hold a refcount over the call to ->make_request. By the time that |
| 212 | * call has finished, the bio has been linked into some internal structure |
| 213 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 214 | */ |
| 215 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 217 | const int rw = bio_data_dir(bio); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 218 | mddev_t *mddev = q->queuedata; |
| 219 | int rv; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 220 | int cpu; |
| 221 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 222 | if (mddev == NULL || mddev->pers == NULL) { |
| 223 | bio_io_error(bio); |
| 224 | return 0; |
| 225 | } |
| 226 | rcu_read_lock(); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 227 | if (mddev->suspended || mddev->barrier) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 228 | DEFINE_WAIT(__wait); |
| 229 | for (;;) { |
| 230 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 231 | TASK_UNINTERRUPTIBLE); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 232 | if (!mddev->suspended && !mddev->barrier) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 233 | break; |
| 234 | rcu_read_unlock(); |
| 235 | schedule(); |
| 236 | rcu_read_lock(); |
| 237 | } |
| 238 | finish_wait(&mddev->sb_wait, &__wait); |
| 239 | } |
| 240 | atomic_inc(&mddev->active_io); |
| 241 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 242 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 243 | rv = mddev->pers->make_request(q, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 244 | |
| 245 | cpu = part_stat_lock(); |
| 246 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 247 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 248 | bio_sectors(bio)); |
| 249 | part_stat_unlock(); |
| 250 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 251 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 252 | wake_up(&mddev->sb_wait); |
| 253 | |
| 254 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 257 | static void mddev_suspend(mddev_t *mddev) |
| 258 | { |
| 259 | BUG_ON(mddev->suspended); |
| 260 | mddev->suspended = 1; |
| 261 | synchronize_rcu(); |
| 262 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 263 | mddev->pers->quiesce(mddev, 1); |
| 264 | md_unregister_thread(mddev->thread); |
| 265 | mddev->thread = NULL; |
| 266 | /* we now know that no code is executing in the personality module, |
| 267 | * except possibly the tail end of a ->bi_end_io function, but that |
| 268 | * is certain to complete before the module has a chance to get |
| 269 | * unloaded |
| 270 | */ |
| 271 | } |
| 272 | |
| 273 | static void mddev_resume(mddev_t *mddev) |
| 274 | { |
| 275 | mddev->suspended = 0; |
| 276 | wake_up(&mddev->sb_wait); |
| 277 | mddev->pers->quiesce(mddev, 0); |
| 278 | } |
| 279 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 280 | int mddev_congested(mddev_t *mddev, int bits) |
| 281 | { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 282 | if (mddev->barrier) |
| 283 | return 1; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 284 | return mddev->suspended; |
| 285 | } |
| 286 | EXPORT_SYMBOL(mddev_congested); |
| 287 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 288 | /* |
| 289 | * Generic barrier handling for md |
| 290 | */ |
| 291 | |
| 292 | #define POST_REQUEST_BARRIER ((void*)1) |
| 293 | |
| 294 | static void md_end_barrier(struct bio *bio, int err) |
| 295 | { |
| 296 | mdk_rdev_t *rdev = bio->bi_private; |
| 297 | mddev_t *mddev = rdev->mddev; |
| 298 | if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER) |
| 299 | set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags); |
| 300 | |
| 301 | rdev_dec_pending(rdev, mddev); |
| 302 | |
| 303 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 304 | if (mddev->barrier == POST_REQUEST_BARRIER) { |
| 305 | /* This was a post-request barrier */ |
| 306 | mddev->barrier = NULL; |
| 307 | wake_up(&mddev->sb_wait); |
| 308 | } else |
| 309 | /* The pre-request barrier has finished */ |
| 310 | schedule_work(&mddev->barrier_work); |
| 311 | } |
| 312 | bio_put(bio); |
| 313 | } |
| 314 | |
| 315 | static void submit_barriers(mddev_t *mddev) |
| 316 | { |
| 317 | mdk_rdev_t *rdev; |
| 318 | |
| 319 | rcu_read_lock(); |
| 320 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 321 | if (rdev->raid_disk >= 0 && |
| 322 | !test_bit(Faulty, &rdev->flags)) { |
| 323 | /* Take two references, one is dropped |
| 324 | * when request finishes, one after |
| 325 | * we reclaim rcu_read_lock |
| 326 | */ |
| 327 | struct bio *bi; |
| 328 | atomic_inc(&rdev->nr_pending); |
| 329 | atomic_inc(&rdev->nr_pending); |
| 330 | rcu_read_unlock(); |
| 331 | bi = bio_alloc(GFP_KERNEL, 0); |
| 332 | bi->bi_end_io = md_end_barrier; |
| 333 | bi->bi_private = rdev; |
| 334 | bi->bi_bdev = rdev->bdev; |
| 335 | atomic_inc(&mddev->flush_pending); |
| 336 | submit_bio(WRITE_BARRIER, bi); |
| 337 | rcu_read_lock(); |
| 338 | rdev_dec_pending(rdev, mddev); |
| 339 | } |
| 340 | rcu_read_unlock(); |
| 341 | } |
| 342 | |
| 343 | static void md_submit_barrier(struct work_struct *ws) |
| 344 | { |
| 345 | mddev_t *mddev = container_of(ws, mddev_t, barrier_work); |
| 346 | struct bio *bio = mddev->barrier; |
| 347 | |
| 348 | atomic_set(&mddev->flush_pending, 1); |
| 349 | |
| 350 | if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags)) |
| 351 | bio_endio(bio, -EOPNOTSUPP); |
| 352 | else if (bio->bi_size == 0) |
| 353 | /* an empty barrier - all done */ |
| 354 | bio_endio(bio, 0); |
| 355 | else { |
| 356 | bio->bi_rw &= ~(1<<BIO_RW_BARRIER); |
| 357 | if (mddev->pers->make_request(mddev->queue, bio)) |
| 358 | generic_make_request(bio); |
| 359 | mddev->barrier = POST_REQUEST_BARRIER; |
| 360 | submit_barriers(mddev); |
| 361 | } |
| 362 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 363 | mddev->barrier = NULL; |
| 364 | wake_up(&mddev->sb_wait); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | void md_barrier_request(mddev_t *mddev, struct bio *bio) |
| 369 | { |
| 370 | spin_lock_irq(&mddev->write_lock); |
| 371 | wait_event_lock_irq(mddev->sb_wait, |
| 372 | !mddev->barrier, |
| 373 | mddev->write_lock, /*nothing*/); |
| 374 | mddev->barrier = bio; |
| 375 | spin_unlock_irq(&mddev->write_lock); |
| 376 | |
| 377 | atomic_set(&mddev->flush_pending, 1); |
| 378 | INIT_WORK(&mddev->barrier_work, md_submit_barrier); |
| 379 | |
| 380 | submit_barriers(mddev); |
| 381 | |
| 382 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 383 | schedule_work(&mddev->barrier_work); |
| 384 | } |
| 385 | EXPORT_SYMBOL(md_barrier_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 388 | { |
| 389 | atomic_inc(&mddev->active); |
| 390 | return mddev; |
| 391 | } |
| 392 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 393 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | static void mddev_put(mddev_t *mddev) |
| 396 | { |
| 397 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 398 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 399 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 400 | mddev->ctime == 0 && !mddev->hold_active) { |
| 401 | /* Array is not configured at all, and not held active, |
| 402 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 404 | if (mddev->gendisk) { |
| 405 | /* we did a probe so need to clean up. |
| 406 | * Call schedule_work inside the spinlock |
| 407 | * so that flush_scheduled_work() after |
| 408 | * mddev_find will succeed in waiting for the |
| 409 | * work to be done. |
| 410 | */ |
| 411 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 412 | schedule_work(&mddev->del_work); |
| 413 | } else |
| 414 | kfree(mddev); |
| 415 | } |
| 416 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | static mddev_t * mddev_find(dev_t unit) |
| 420 | { |
| 421 | mddev_t *mddev, *new = NULL; |
| 422 | |
| 423 | retry: |
| 424 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 426 | if (unit) { |
| 427 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 428 | if (mddev->unit == unit) { |
| 429 | mddev_get(mddev); |
| 430 | spin_unlock(&all_mddevs_lock); |
| 431 | kfree(new); |
| 432 | return mddev; |
| 433 | } |
| 434 | |
| 435 | if (new) { |
| 436 | list_add(&new->all_mddevs, &all_mddevs); |
| 437 | spin_unlock(&all_mddevs_lock); |
| 438 | new->hold_active = UNTIL_IOCTL; |
| 439 | return new; |
| 440 | } |
| 441 | } else if (new) { |
| 442 | /* find an unused unit number */ |
| 443 | static int next_minor = 512; |
| 444 | int start = next_minor; |
| 445 | int is_free = 0; |
| 446 | int dev = 0; |
| 447 | while (!is_free) { |
| 448 | dev = MKDEV(MD_MAJOR, next_minor); |
| 449 | next_minor++; |
| 450 | if (next_minor > MINORMASK) |
| 451 | next_minor = 0; |
| 452 | if (next_minor == start) { |
| 453 | /* Oh dear, all in use. */ |
| 454 | spin_unlock(&all_mddevs_lock); |
| 455 | kfree(new); |
| 456 | return NULL; |
| 457 | } |
| 458 | |
| 459 | is_free = 1; |
| 460 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 461 | if (mddev->unit == dev) { |
| 462 | is_free = 0; |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | new->unit = dev; |
| 467 | new->md_minor = MINOR(dev); |
| 468 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | list_add(&new->all_mddevs, &all_mddevs); |
| 470 | spin_unlock(&all_mddevs_lock); |
| 471 | return new; |
| 472 | } |
| 473 | spin_unlock(&all_mddevs_lock); |
| 474 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 475 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | if (!new) |
| 477 | return NULL; |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | new->unit = unit; |
| 480 | if (MAJOR(unit) == MD_MAJOR) |
| 481 | new->md_minor = MINOR(unit); |
| 482 | else |
| 483 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 484 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 485 | mutex_init(&new->open_mutex); |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 486 | mutex_init(&new->reconfig_mutex); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 487 | mutex_init(&new->bitmap_info.mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | INIT_LIST_HEAD(&new->disks); |
| 489 | INIT_LIST_HEAD(&new->all_mddevs); |
| 490 | init_timer(&new->safemode_timer); |
| 491 | atomic_set(&new->active, 1); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 492 | atomic_set(&new->openers, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 493 | atomic_set(&new->active_io, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 494 | spin_lock_init(&new->write_lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 495 | atomic_set(&new->flush_pending, 0); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 496 | init_waitqueue_head(&new->sb_wait); |
Dan Williams | a6d8113 | 2008-06-05 22:45:53 -0700 | [diff] [blame] | 497 | init_waitqueue_head(&new->recovery_wait); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 498 | new->reshape_position = MaxSector; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 499 | new->resync_min = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 500 | new->resync_max = MaxSector; |
NeilBrown | d897dbf | 2008-04-30 00:52:29 -0700 | [diff] [blame] | 501 | new->level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | goto retry; |
| 504 | } |
| 505 | |
| 506 | static inline int mddev_lock(mddev_t * mddev) |
| 507 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 508 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 511 | static inline int mddev_is_locked(mddev_t *mddev) |
| 512 | { |
| 513 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 514 | } |
| 515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | static inline int mddev_trylock(mddev_t * mddev) |
| 517 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 518 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 521 | static struct attribute_group md_redundancy_group; |
| 522 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 523 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 525 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 526 | /* These cannot be removed under reconfig_mutex as |
| 527 | * an access to the files will try to take reconfig_mutex |
| 528 | * while holding the file unremovable, which leads to |
| 529 | * a deadlock. |
| 530 | * So hold open_mutex instead - we are allowed to take |
| 531 | * it while holding reconfig_mutex, and md_run can |
| 532 | * use it to wait for the remove to complete. |
| 533 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 534 | struct attribute_group *to_remove = mddev->to_remove; |
| 535 | mddev->to_remove = NULL; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 536 | mutex_lock(&mddev->open_mutex); |
| 537 | mutex_unlock(&mddev->reconfig_mutex); |
| 538 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 539 | if (to_remove != &md_redundancy_group) |
| 540 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 541 | if (mddev->pers == NULL || |
| 542 | mddev->pers->sync_request == NULL) { |
| 543 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 544 | if (mddev->sysfs_action) |
| 545 | sysfs_put(mddev->sysfs_action); |
| 546 | mddev->sysfs_action = NULL; |
| 547 | } |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 548 | mutex_unlock(&mddev->open_mutex); |
| 549 | } else |
| 550 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 552 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 555 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 557 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 559 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | if (rdev->desc_nr == nr) |
| 561 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 567 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | mdk_rdev_t *rdev; |
| 569 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 570 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | if (rdev->bdev->bd_dev == dev) |
| 572 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | return NULL; |
| 575 | } |
| 576 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 577 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 578 | { |
| 579 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 580 | list_for_each_entry(pers, &pers_list, list) { |
| 581 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 582 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 583 | if (strcmp(pers->name, clevel)==0) |
| 584 | return pers; |
| 585 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 586 | return NULL; |
| 587 | } |
| 588 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 589 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 590 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 592 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 593 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 597 | { |
| 598 | if (rdev->sb_page) |
| 599 | MD_BUG(); |
| 600 | |
| 601 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 602 | if (!rdev->sb_page) { |
| 603 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 604 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 611 | { |
| 612 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 613 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | rdev->sb_loaded = 0; |
| 615 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 616 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 617 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
| 621 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 622 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 623 | { |
| 624 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 625 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 626 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 627 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 628 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 629 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 630 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 631 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 632 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 633 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 634 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 635 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 636 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 637 | } |
| 638 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 639 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 640 | { |
| 641 | struct bio *bio2 = bio->bi_private; |
| 642 | mdk_rdev_t *rdev = bio2->bi_private; |
| 643 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 644 | |
| 645 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 646 | error == -EOPNOTSUPP) { |
| 647 | unsigned long flags; |
| 648 | /* barriers don't appear to be supported :-( */ |
| 649 | set_bit(BarriersNotsupp, &rdev->flags); |
| 650 | mddev->barriers_work = 0; |
| 651 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 652 | bio2->bi_next = mddev->biolist; |
| 653 | mddev->biolist = bio2; |
| 654 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 655 | wake_up(&mddev->sb_wait); |
| 656 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 657 | } else { |
| 658 | bio_put(bio2); |
| 659 | bio->bi_private = rdev; |
| 660 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 661 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 662 | } |
| 663 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 664 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 665 | sector_t sector, int size, struct page *page) |
| 666 | { |
| 667 | /* write first size bytes of page to sector of rdev |
| 668 | * Increment mddev->pending_writes before returning |
| 669 | * and decrement it on completion, waking up sb_wait |
| 670 | * if zero is reached. |
| 671 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 672 | * |
| 673 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 674 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 675 | */ |
| 676 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 677 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 678 | |
| 679 | bio->bi_bdev = rdev->bdev; |
| 680 | bio->bi_sector = sector; |
| 681 | bio_add_page(bio, page, size, 0); |
| 682 | bio->bi_private = rdev; |
| 683 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 684 | bio->bi_rw = rw; |
| 685 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 686 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 687 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 688 | struct bio *rbio; |
| 689 | rw |= (1<<BIO_RW_BARRIER); |
| 690 | rbio = bio_clone(bio, GFP_NOIO); |
| 691 | rbio->bi_private = bio; |
| 692 | rbio->bi_end_io = super_written_barrier; |
| 693 | submit_bio(rw, rbio); |
| 694 | } else |
| 695 | submit_bio(rw, bio); |
| 696 | } |
| 697 | |
| 698 | void md_super_wait(mddev_t *mddev) |
| 699 | { |
| 700 | /* wait for all superblock writes that were scheduled to complete. |
| 701 | * if any had to be retried (due to BARRIER problems), retry them |
| 702 | */ |
| 703 | DEFINE_WAIT(wq); |
| 704 | for(;;) { |
| 705 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 706 | if (atomic_read(&mddev->pending_writes)==0) |
| 707 | break; |
| 708 | while (mddev->biolist) { |
| 709 | struct bio *bio; |
| 710 | spin_lock_irq(&mddev->write_lock); |
| 711 | bio = mddev->biolist; |
| 712 | mddev->biolist = bio->bi_next ; |
| 713 | bio->bi_next = NULL; |
| 714 | spin_unlock_irq(&mddev->write_lock); |
| 715 | submit_bio(bio->bi_rw, bio); |
| 716 | } |
| 717 | schedule(); |
| 718 | } |
| 719 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 720 | } |
| 721 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 722 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
| 726 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 727 | int sync_page_io(struct block_device *bdev, sector_t sector, int size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | struct page *page, int rw) |
| 729 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 730 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | struct completion event; |
| 732 | int ret; |
| 733 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 734 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
| 736 | bio->bi_bdev = bdev; |
| 737 | bio->bi_sector = sector; |
| 738 | bio_add_page(bio, page, size, 0); |
| 739 | init_completion(&event); |
| 740 | bio->bi_private = &event; |
| 741 | bio->bi_end_io = bi_complete; |
| 742 | submit_bio(rw, bio); |
| 743 | wait_for_completion(&event); |
| 744 | |
| 745 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 746 | bio_put(bio); |
| 747 | return ret; |
| 748 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 749 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 751 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | { |
| 753 | char b[BDEVNAME_SIZE]; |
| 754 | if (!rdev->sb_page) { |
| 755 | MD_BUG(); |
| 756 | return -EINVAL; |
| 757 | } |
| 758 | if (rdev->sb_loaded) |
| 759 | return 0; |
| 760 | |
| 761 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 762 | if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | goto fail; |
| 764 | rdev->sb_loaded = 1; |
| 765 | return 0; |
| 766 | |
| 767 | fail: |
| 768 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 769 | bdevname(rdev->bdev,b)); |
| 770 | return -EINVAL; |
| 771 | } |
| 772 | |
| 773 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 774 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 775 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 776 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 777 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 778 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 782 | { |
| 783 | int ret; |
| 784 | mdp_super_t *tmp1, *tmp2; |
| 785 | |
| 786 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 787 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 788 | |
| 789 | if (!tmp1 || !tmp2) { |
| 790 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 791 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | goto abort; |
| 793 | } |
| 794 | |
| 795 | *tmp1 = *sb1; |
| 796 | *tmp2 = *sb2; |
| 797 | |
| 798 | /* |
| 799 | * nr_disks is not constant |
| 800 | */ |
| 801 | tmp1->nr_disks = 0; |
| 802 | tmp2->nr_disks = 0; |
| 803 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 804 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 806 | kfree(tmp1); |
| 807 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | return ret; |
| 809 | } |
| 810 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 811 | |
| 812 | static u32 md_csum_fold(u32 csum) |
| 813 | { |
| 814 | csum = (csum & 0xffff) + (csum >> 16); |
| 815 | return (csum & 0xffff) + (csum >> 16); |
| 816 | } |
| 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 819 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 820 | u64 newcsum = 0; |
| 821 | u32 *sb32 = (u32*)sb; |
| 822 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | unsigned int disk_csum, csum; |
| 824 | |
| 825 | disk_csum = sb->sb_csum; |
| 826 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 827 | |
| 828 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 829 | newcsum += sb32[i]; |
| 830 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 831 | |
| 832 | |
| 833 | #ifdef CONFIG_ALPHA |
| 834 | /* This used to use csum_partial, which was wrong for several |
| 835 | * reasons including that different results are returned on |
| 836 | * different architectures. It isn't critical that we get exactly |
| 837 | * the same return value as before (we always csum_fold before |
| 838 | * testing, and that removes any differences). However as we |
| 839 | * know that csum_partial always returned a 16bit value on |
| 840 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 841 | */ |
| 842 | sb->sb_csum = md_csum_fold(disk_csum); |
| 843 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 845 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | return csum; |
| 847 | } |
| 848 | |
| 849 | |
| 850 | /* |
| 851 | * Handle superblock details. |
| 852 | * We want to be able to handle multiple superblock formats |
| 853 | * so we have a common interface to them all, and an array of |
| 854 | * different handlers. |
| 855 | * We rely on user-space to write the initial superblock, and support |
| 856 | * reading and updating of superblocks. |
| 857 | * Interface methods are: |
| 858 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 859 | * loads and validates a superblock on dev. |
| 860 | * if refdev != NULL, compare superblocks on both devices |
| 861 | * Return: |
| 862 | * 0 - dev has a superblock that is compatible with refdev |
| 863 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 864 | * so dev should be used as the refdev in future |
| 865 | * -EINVAL superblock incompatible or invalid |
| 866 | * -othererror e.g. -EIO |
| 867 | * |
| 868 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 869 | * Verify that dev is acceptable into mddev. |
| 870 | * The first time, mddev->raid_disks will be 0, and data from |
| 871 | * dev should be merged in. Subsequent calls check that dev |
| 872 | * is new enough. Return 0 or -EINVAL |
| 873 | * |
| 874 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 875 | * Update the superblock for rdev with data in mddev |
| 876 | * This does not write to disc. |
| 877 | * |
| 878 | */ |
| 879 | |
| 880 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 881 | char *name; |
| 882 | struct module *owner; |
| 883 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 884 | int minor_version); |
| 885 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 886 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 887 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 888 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | }; |
| 890 | |
| 891 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 892 | * Check that the given mddev has no bitmap. |
| 893 | * |
| 894 | * This function is called from the run method of all personalities that do not |
| 895 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 896 | * has a bitmap. Otherwise, it returns 0. |
| 897 | * |
| 898 | */ |
| 899 | int md_check_no_bitmap(mddev_t *mddev) |
| 900 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 901 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 902 | return 0; |
| 903 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 904 | mdname(mddev), mddev->pers->name); |
| 905 | return 1; |
| 906 | } |
| 907 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 908 | |
| 909 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | * load_super for 0.90.0 |
| 911 | */ |
| 912 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 913 | { |
| 914 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 915 | mdp_super_t *sb; |
| 916 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
| 918 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 919 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | * it's at the end of the disk. |
| 921 | * |
| 922 | * It also happens to be a multiple of 4Kb. |
| 923 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 924 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 926 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | if (ret) return ret; |
| 928 | |
| 929 | ret = -EINVAL; |
| 930 | |
| 931 | bdevname(rdev->bdev, b); |
| 932 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 933 | |
| 934 | if (sb->md_magic != MD_SB_MAGIC) { |
| 935 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 936 | b); |
| 937 | goto abort; |
| 938 | } |
| 939 | |
| 940 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 941 | sb->minor_version < 90 || |
| 942 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 944 | sb->major_version, sb->minor_version, |
| 945 | b); |
| 946 | goto abort; |
| 947 | } |
| 948 | |
| 949 | if (sb->raid_disks <= 0) |
| 950 | goto abort; |
| 951 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 952 | 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] | 953 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 954 | b); |
| 955 | goto abort; |
| 956 | } |
| 957 | |
| 958 | rdev->preferred_minor = sb->md_minor; |
| 959 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 960 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | if (sb->level == LEVEL_MULTIPATH) |
| 963 | rdev->desc_nr = -1; |
| 964 | else |
| 965 | rdev->desc_nr = sb->this_disk.number; |
| 966 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 967 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 969 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | __u64 ev1, ev2; |
| 971 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 972 | if (!uuid_equal(refsb, sb)) { |
| 973 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 974 | b, bdevname(refdev->bdev,b2)); |
| 975 | goto abort; |
| 976 | } |
| 977 | if (!sb_equal(refsb, sb)) { |
| 978 | printk(KERN_WARNING "md: %s has same UUID" |
| 979 | " but different superblock to %s\n", |
| 980 | b, bdevname(refdev->bdev, b2)); |
| 981 | goto abort; |
| 982 | } |
| 983 | ev1 = md_event(sb); |
| 984 | ev2 = md_event(refsb); |
| 985 | if (ev1 > ev2) |
| 986 | ret = 1; |
| 987 | else |
| 988 | ret = 0; |
| 989 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 990 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 992 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 993 | /* "this cannot possibly happen" ... */ |
| 994 | ret = -EINVAL; |
| 995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | abort: |
| 997 | return ret; |
| 998 | } |
| 999 | |
| 1000 | /* |
| 1001 | * validate_super for 0.90.0 |
| 1002 | */ |
| 1003 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1004 | { |
| 1005 | mdp_disk_t *desc; |
| 1006 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1007 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1009 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1010 | clear_bit(Faulty, &rdev->flags); |
| 1011 | clear_bit(In_sync, &rdev->flags); |
| 1012 | clear_bit(WriteMostly, &rdev->flags); |
| 1013 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | if (mddev->raid_disks == 0) { |
| 1016 | mddev->major_version = 0; |
| 1017 | mddev->minor_version = sb->minor_version; |
| 1018 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1019 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1020 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | mddev->ctime = sb->ctime; |
| 1022 | mddev->utime = sb->utime; |
| 1023 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1024 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | mddev->layout = sb->layout; |
| 1026 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1027 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1028 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1029 | mddev->bitmap_info.offset = 0; |
| 1030 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1032 | if (mddev->minor_version >= 91) { |
| 1033 | mddev->reshape_position = sb->reshape_position; |
| 1034 | mddev->delta_disks = sb->delta_disks; |
| 1035 | mddev->new_level = sb->new_level; |
| 1036 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1037 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1038 | } else { |
| 1039 | mddev->reshape_position = MaxSector; |
| 1040 | mddev->delta_disks = 0; |
| 1041 | mddev->new_level = mddev->level; |
| 1042 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1043 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1047 | mddev->recovery_cp = MaxSector; |
| 1048 | else { |
| 1049 | if (sb->events_hi == sb->cp_events_hi && |
| 1050 | sb->events_lo == sb->cp_events_lo) { |
| 1051 | mddev->recovery_cp = sb->recovery_cp; |
| 1052 | } else |
| 1053 | mddev->recovery_cp = 0; |
| 1054 | } |
| 1055 | |
| 1056 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1057 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1058 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1059 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1060 | |
| 1061 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1062 | |
| 1063 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1064 | mddev->bitmap_info.file == NULL) |
| 1065 | mddev->bitmap_info.offset = |
| 1066 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1067 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1068 | } else if (mddev->pers == NULL) { |
| 1069 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | ++ev1; |
| 1071 | if (ev1 < mddev->events) |
| 1072 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1073 | } else if (mddev->bitmap) { |
| 1074 | /* if adding to array with a bitmap, then we can accept an |
| 1075 | * older device ... but not too old. |
| 1076 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1077 | if (ev1 < mddev->bitmap->events_cleared) |
| 1078 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1079 | } else { |
| 1080 | if (ev1 < mddev->events) |
| 1081 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1082 | return 0; |
| 1083 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | desc = sb->disks + rdev->desc_nr; |
| 1087 | |
| 1088 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1089 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1090 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1091 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1092 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1094 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1095 | /* active but not in sync implies recovery up to |
| 1096 | * reshape position. We don't know exactly where |
| 1097 | * that is, so set to zero for now */ |
| 1098 | if (mddev->minor_version >= 91) { |
| 1099 | rdev->recovery_offset = 0; |
| 1100 | rdev->raid_disk = desc->raid_disk; |
| 1101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1103 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1104 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1105 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1106 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * sync_super for 0.90.0 |
| 1112 | */ |
| 1113 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1114 | { |
| 1115 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | mdk_rdev_t *rdev2; |
| 1117 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | |
| 1120 | /* make rdev->sb match mddev data.. |
| 1121 | * |
| 1122 | * 1/ zero out disks |
| 1123 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1124 | * 3/ any empty disks < next_spare become removed |
| 1125 | * |
| 1126 | * disks[0] gets initialised to REMOVED because |
| 1127 | * we cannot be sure from other fields if it has |
| 1128 | * been initialised or not. |
| 1129 | */ |
| 1130 | int i; |
| 1131 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1132 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1133 | rdev->sb_size = MD_SB_BYTES; |
| 1134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1136 | |
| 1137 | memset(sb, 0, sizeof(*sb)); |
| 1138 | |
| 1139 | sb->md_magic = MD_SB_MAGIC; |
| 1140 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | sb->patch_version = mddev->patch_version; |
| 1142 | sb->gvalid_words = 0; /* ignored */ |
| 1143 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1144 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1145 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1146 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1147 | |
| 1148 | sb->ctime = mddev->ctime; |
| 1149 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1150 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | sb->raid_disks = mddev->raid_disks; |
| 1152 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1153 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | sb->utime = mddev->utime; |
| 1155 | sb->state = 0; |
| 1156 | sb->events_hi = (mddev->events>>32); |
| 1157 | sb->events_lo = (u32)mddev->events; |
| 1158 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1159 | if (mddev->reshape_position == MaxSector) |
| 1160 | sb->minor_version = 90; |
| 1161 | else { |
| 1162 | sb->minor_version = 91; |
| 1163 | sb->reshape_position = mddev->reshape_position; |
| 1164 | sb->new_level = mddev->new_level; |
| 1165 | sb->delta_disks = mddev->delta_disks; |
| 1166 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1167 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1168 | } |
| 1169 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | if (mddev->in_sync) |
| 1171 | { |
| 1172 | sb->recovery_cp = mddev->recovery_cp; |
| 1173 | sb->cp_events_hi = (mddev->events>>32); |
| 1174 | sb->cp_events_lo = (u32)mddev->events; |
| 1175 | if (mddev->recovery_cp == MaxSector) |
| 1176 | sb->state = (1<< MD_SB_CLEAN); |
| 1177 | } else |
| 1178 | sb->recovery_cp = 0; |
| 1179 | |
| 1180 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1181 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1183 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1184 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1187 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1189 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1190 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1191 | |
| 1192 | if (rdev2->raid_disk >= 0 && |
| 1193 | sb->minor_version >= 91) |
| 1194 | /* we have nowhere to store the recovery_offset, |
| 1195 | * but if it is not below the reshape_position, |
| 1196 | * we can piggy-back on that. |
| 1197 | */ |
| 1198 | is_active = 1; |
| 1199 | if (rdev2->raid_disk < 0 || |
| 1200 | test_bit(Faulty, &rdev2->flags)) |
| 1201 | is_active = 0; |
| 1202 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1203 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1205 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1206 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | d = &sb->disks[rdev2->desc_nr]; |
| 1208 | nr_disks++; |
| 1209 | d->number = rdev2->desc_nr; |
| 1210 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1211 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1212 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | d->raid_disk = rdev2->raid_disk; |
| 1214 | else |
| 1215 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1216 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1218 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1220 | if (test_bit(In_sync, &rdev2->flags)) |
| 1221 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | active++; |
| 1223 | working++; |
| 1224 | } else { |
| 1225 | d->state = 0; |
| 1226 | spare++; |
| 1227 | working++; |
| 1228 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1229 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1230 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1233 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1234 | mdp_disk_t *d = &sb->disks[i]; |
| 1235 | if (d->state == 0 && d->number == 0) { |
| 1236 | d->number = i; |
| 1237 | d->raid_disk = i; |
| 1238 | d->state = (1<<MD_DISK_REMOVED); |
| 1239 | d->state |= (1<<MD_DISK_FAULTY); |
| 1240 | failed++; |
| 1241 | } |
| 1242 | } |
| 1243 | sb->nr_disks = nr_disks; |
| 1244 | sb->active_disks = active; |
| 1245 | sb->working_disks = working; |
| 1246 | sb->failed_disks = failed; |
| 1247 | sb->spare_disks = spare; |
| 1248 | |
| 1249 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1250 | sb->sb_csum = calc_sb_csum(sb); |
| 1251 | } |
| 1252 | |
| 1253 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1254 | * rdev_size_change for 0.90.0 |
| 1255 | */ |
| 1256 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1257 | 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] | 1258 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1259 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1260 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1261 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1262 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1263 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1264 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1265 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1266 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1267 | rdev->sb_page); |
| 1268 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1269 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | * version 1 superblock |
| 1275 | */ |
| 1276 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1277 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1279 | __le32 disk_csum; |
| 1280 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | unsigned long long newcsum; |
| 1282 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1283 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | int i; |
| 1285 | |
| 1286 | disk_csum = sb->sb_csum; |
| 1287 | sb->sb_csum = 0; |
| 1288 | newcsum = 0; |
| 1289 | for (i=0; size>=4; size -= 4 ) |
| 1290 | newcsum += le32_to_cpu(*isuper++); |
| 1291 | |
| 1292 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1293 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
| 1295 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1296 | sb->sb_csum = disk_csum; |
| 1297 | return cpu_to_le32(csum); |
| 1298 | } |
| 1299 | |
| 1300 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1301 | { |
| 1302 | struct mdp_superblock_1 *sb; |
| 1303 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1304 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1306 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | |
| 1308 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1309 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | * It is always aligned to a 4K boundary and |
| 1311 | * depeding on minor_version, it can be: |
| 1312 | * 0: At least 8K, but less than 12K, from end of device |
| 1313 | * 1: At start of device |
| 1314 | * 2: 4K from start of device. |
| 1315 | */ |
| 1316 | switch(minor_version) { |
| 1317 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1318 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1319 | sb_start -= 8*2; |
| 1320 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | break; |
| 1322 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1323 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | break; |
| 1325 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1326 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | break; |
| 1328 | default: |
| 1329 | return -EINVAL; |
| 1330 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1331 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1333 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1334 | * and it is safe to read 4k, so we do that |
| 1335 | */ |
| 1336 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | if (ret) return ret; |
| 1338 | |
| 1339 | |
| 1340 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1341 | |
| 1342 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1343 | sb->major_version != cpu_to_le32(1) || |
| 1344 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1345 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1346 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | return -EINVAL; |
| 1348 | |
| 1349 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1350 | printk("md: invalid superblock checksum on %s\n", |
| 1351 | bdevname(rdev->bdev,b)); |
| 1352 | return -EINVAL; |
| 1353 | } |
| 1354 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1355 | printk("md: data_size too small on %s\n", |
| 1356 | bdevname(rdev->bdev,b)); |
| 1357 | return -EINVAL; |
| 1358 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | rdev->preferred_minor = 0xffff; |
| 1361 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1362 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1364 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1365 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1366 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1367 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1368 | |
| 1369 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1370 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1371 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1372 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1373 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1374 | rdev->desc_nr = -1; |
| 1375 | else |
| 1376 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1377 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1378 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1379 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1380 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | __u64 ev1, ev2; |
| 1382 | struct mdp_superblock_1 *refsb = |
| 1383 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1384 | |
| 1385 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1386 | sb->level != refsb->level || |
| 1387 | sb->layout != refsb->layout || |
| 1388 | sb->chunksize != refsb->chunksize) { |
| 1389 | printk(KERN_WARNING "md: %s has strangely different" |
| 1390 | " superblock to %s\n", |
| 1391 | bdevname(rdev->bdev,b), |
| 1392 | bdevname(refdev->bdev,b2)); |
| 1393 | return -EINVAL; |
| 1394 | } |
| 1395 | ev1 = le64_to_cpu(sb->events); |
| 1396 | ev2 = le64_to_cpu(refsb->events); |
| 1397 | |
| 1398 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1399 | ret = 1; |
| 1400 | else |
| 1401 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1403 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1404 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1405 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1407 | rdev->sectors = rdev->sb_start; |
| 1408 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1410 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1411 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1412 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1413 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1417 | { |
| 1418 | 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] | 1419 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1421 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1422 | clear_bit(Faulty, &rdev->flags); |
| 1423 | clear_bit(In_sync, &rdev->flags); |
| 1424 | clear_bit(WriteMostly, &rdev->flags); |
| 1425 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | if (mddev->raid_disks == 0) { |
| 1428 | mddev->major_version = 1; |
| 1429 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1430 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1431 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1433 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1434 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1435 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | mddev->layout = le32_to_cpu(sb->layout); |
| 1437 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1438 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1439 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1440 | mddev->bitmap_info.offset = 0; |
| 1441 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | |
| 1443 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1444 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1445 | |
| 1446 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1447 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1448 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1449 | mddev->bitmap_info.file == NULL ) |
| 1450 | mddev->bitmap_info.offset = |
| 1451 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1452 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1453 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1454 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1455 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1456 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1457 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1458 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1459 | } else { |
| 1460 | mddev->reshape_position = MaxSector; |
| 1461 | mddev->delta_disks = 0; |
| 1462 | mddev->new_level = mddev->level; |
| 1463 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1464 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1467 | } else if (mddev->pers == NULL) { |
| 1468 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | ++ev1; |
| 1470 | if (ev1 < mddev->events) |
| 1471 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1472 | } else if (mddev->bitmap) { |
| 1473 | /* If adding to array with a bitmap, then we can accept an |
| 1474 | * older device, but not too old. |
| 1475 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1476 | if (ev1 < mddev->bitmap->events_cleared) |
| 1477 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1478 | } else { |
| 1479 | if (ev1 < mddev->events) |
| 1480 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1481 | return 0; |
| 1482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1484 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1485 | if (rdev->desc_nr < 0 || |
| 1486 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1487 | role = 0xffff; |
| 1488 | rdev->desc_nr = -1; |
| 1489 | } else |
| 1490 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | switch(role) { |
| 1492 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | break; |
| 1494 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1495 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | break; |
| 1497 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1498 | if ((le32_to_cpu(sb->feature_map) & |
| 1499 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1500 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1501 | else |
| 1502 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | rdev->raid_disk = role; |
| 1504 | break; |
| 1505 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1506 | if (sb->devflags & WriteMostly1) |
| 1507 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1508 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1509 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | return 0; |
| 1512 | } |
| 1513 | |
| 1514 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1515 | { |
| 1516 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | mdk_rdev_t *rdev2; |
| 1518 | int max_dev, i; |
| 1519 | /* make rdev->sb match mddev and rdev data. */ |
| 1520 | |
| 1521 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1522 | |
| 1523 | sb->feature_map = 0; |
| 1524 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1525 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1527 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1528 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1529 | |
| 1530 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1531 | sb->events = cpu_to_le64(mddev->events); |
| 1532 | if (mddev->in_sync) |
| 1533 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1534 | else |
| 1535 | sb->resync_offset = cpu_to_le64(0); |
| 1536 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1537 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1538 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1539 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1540 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1541 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1542 | sb->level = cpu_to_le32(mddev->level); |
| 1543 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1544 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1545 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1546 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1547 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1548 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1549 | |
| 1550 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1551 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1552 | sb->feature_map |= |
| 1553 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1554 | sb->recovery_offset = |
| 1555 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1558 | if (mddev->reshape_position != MaxSector) { |
| 1559 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1560 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1561 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1562 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1563 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1564 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1565 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1568 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | if (rdev2->desc_nr+1 > max_dev) |
| 1570 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1571 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1572 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1573 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1574 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1575 | rdev->sb_size = max_dev * 2 + 256; |
| 1576 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1577 | if (rdev->sb_size & bmask) |
| 1578 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1579 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | for (i=0; i<max_dev;i++) |
| 1581 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1582 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1583 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1585 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1587 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1589 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1590 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | else |
| 1592 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1593 | } |
| 1594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1596 | } |
| 1597 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1598 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1599 | 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] | 1600 | { |
| 1601 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1602 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1603 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1604 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1605 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1606 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1607 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1608 | max_sectors -= rdev->data_offset; |
| 1609 | if (!num_sectors || num_sectors > max_sectors) |
| 1610 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1611 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1612 | /* minor version 0 with bitmap we can't move */ |
| 1613 | return 0; |
| 1614 | } else { |
| 1615 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1616 | sector_t sb_start; |
| 1617 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1618 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1619 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1620 | if (!num_sectors || num_sectors > max_sectors) |
| 1621 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1622 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1623 | } |
| 1624 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1625 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1626 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1627 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1628 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1629 | rdev->sb_page); |
| 1630 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1631 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1632 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1634 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | [0] = { |
| 1636 | .name = "0.90.0", |
| 1637 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1638 | .load_super = super_90_load, |
| 1639 | .validate_super = super_90_validate, |
| 1640 | .sync_super = super_90_sync, |
| 1641 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | }, |
| 1643 | [1] = { |
| 1644 | .name = "md-1", |
| 1645 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1646 | .load_super = super_1_load, |
| 1647 | .validate_super = super_1_validate, |
| 1648 | .sync_super = super_1_sync, |
| 1649 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | }, |
| 1651 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | |
| 1653 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1654 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1655 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1657 | rcu_read_lock(); |
| 1658 | rdev_for_each_rcu(rdev, mddev1) |
| 1659 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1660 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1661 | rdev2->bdev->bd_contains) { |
| 1662 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1663 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1664 | } |
| 1665 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | return 0; |
| 1667 | } |
| 1668 | |
| 1669 | static LIST_HEAD(pending_raid_disks); |
| 1670 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1671 | /* |
| 1672 | * Try to register data integrity profile for an mddev |
| 1673 | * |
| 1674 | * This is called when an array is started and after a disk has been kicked |
| 1675 | * from the array. It only succeeds if all working and active component devices |
| 1676 | * are integrity capable with matching profiles. |
| 1677 | */ |
| 1678 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1679 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1680 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1681 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1682 | if (list_empty(&mddev->disks)) |
| 1683 | return 0; /* nothing to do */ |
| 1684 | if (blk_get_integrity(mddev->gendisk)) |
| 1685 | return 0; /* already registered */ |
| 1686 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1687 | /* skip spares and non-functional disks */ |
| 1688 | if (test_bit(Faulty, &rdev->flags)) |
| 1689 | continue; |
| 1690 | if (rdev->raid_disk < 0) |
| 1691 | continue; |
| 1692 | /* |
| 1693 | * If at least one rdev is not integrity capable, we can not |
| 1694 | * enable data integrity for the md device. |
| 1695 | */ |
| 1696 | if (!bdev_get_integrity(rdev->bdev)) |
| 1697 | return -EINVAL; |
| 1698 | if (!reference) { |
| 1699 | /* Use the first rdev as the reference */ |
| 1700 | reference = rdev; |
| 1701 | continue; |
| 1702 | } |
| 1703 | /* does this rdev's profile match the reference profile? */ |
| 1704 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1705 | rdev->bdev->bd_disk) < 0) |
| 1706 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1707 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1708 | /* |
| 1709 | * All component devices are integrity capable and have matching |
| 1710 | * profiles, register the common profile for the md device. |
| 1711 | */ |
| 1712 | if (blk_integrity_register(mddev->gendisk, |
| 1713 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1714 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1715 | mdname(mddev)); |
| 1716 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1717 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1718 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1719 | mdname(mddev)); |
| 1720 | return 0; |
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 | EXPORT_SYMBOL(md_integrity_register); |
| 1723 | |
| 1724 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1725 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1726 | { |
| 1727 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1728 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1729 | |
| 1730 | if (!bi_mddev) /* nothing to do */ |
| 1731 | return; |
| 1732 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1733 | return; |
| 1734 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1735 | rdev->bdev->bd_disk) >= 0) |
| 1736 | return; |
| 1737 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1738 | blk_integrity_unregister(mddev->gendisk); |
| 1739 | } |
| 1740 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1743 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1744 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1745 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1746 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1747 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | |
| 1749 | if (rdev->mddev) { |
| 1750 | MD_BUG(); |
| 1751 | return -EINVAL; |
| 1752 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1753 | |
| 1754 | /* prevent duplicates */ |
| 1755 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1756 | return -EEXIST; |
| 1757 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1758 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1759 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1760 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1761 | if (mddev->pers) { |
| 1762 | /* Cannot change size, so fail |
| 1763 | * If mddev->level <= 0, then we don't care |
| 1764 | * about aligning sizes (e.g. linear) |
| 1765 | */ |
| 1766 | if (mddev->level > 0) |
| 1767 | return -ENOSPC; |
| 1768 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1769 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
| 1772 | /* Verify rdev->desc_nr is unique. |
| 1773 | * If it is -1, assign a free number, else |
| 1774 | * check number is not in use |
| 1775 | */ |
| 1776 | if (rdev->desc_nr < 0) { |
| 1777 | int choice = 0; |
| 1778 | if (mddev->pers) choice = mddev->raid_disks; |
| 1779 | while (find_rdev_nr(mddev, choice)) |
| 1780 | choice++; |
| 1781 | rdev->desc_nr = choice; |
| 1782 | } else { |
| 1783 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1784 | return -EBUSY; |
| 1785 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1786 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1787 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1788 | mdname(mddev), mddev->max_disks); |
| 1789 | return -EBUSY; |
| 1790 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1791 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1792 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1793 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1796 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1797 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1798 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1799 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1800 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1801 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1802 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1803 | kobject_del(&rdev->kobj); |
| 1804 | goto fail; |
| 1805 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1806 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1807 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1808 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1809 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1810 | |
| 1811 | /* May as well allow recovery to be retried once */ |
| 1812 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1815 | |
| 1816 | fail: |
| 1817 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1818 | b, mdname(mddev)); |
| 1819 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1822 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1823 | { |
| 1824 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1825 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1826 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1830 | { |
| 1831 | char b[BDEVNAME_SIZE]; |
| 1832 | if (!rdev->mddev) { |
| 1833 | MD_BUG(); |
| 1834 | return; |
| 1835 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1836 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1837 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1839 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1840 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1841 | sysfs_put(rdev->sysfs_state); |
| 1842 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1843 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1844 | * writing to 'remove' to "dev/state". We also need |
| 1845 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1846 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1847 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1848 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1849 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1850 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | /* |
| 1854 | * prevent the device from being mounted, repartitioned or |
| 1855 | * otherwise reused by a RAID array (or any other kernel |
| 1856 | * subsystem), by bd_claiming the device. |
| 1857 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1858 | 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] | 1859 | { |
| 1860 | int err = 0; |
| 1861 | struct block_device *bdev; |
| 1862 | char b[BDEVNAME_SIZE]; |
| 1863 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1864 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | if (IS_ERR(bdev)) { |
| 1866 | printk(KERN_ERR "md: could not open %s.\n", |
| 1867 | __bdevname(dev, b)); |
| 1868 | return PTR_ERR(bdev); |
| 1869 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1870 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | if (err) { |
| 1872 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1873 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1874 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | return err; |
| 1876 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1877 | if (!shared) |
| 1878 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | rdev->bdev = bdev; |
| 1880 | return err; |
| 1881 | } |
| 1882 | |
| 1883 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1884 | { |
| 1885 | struct block_device *bdev = rdev->bdev; |
| 1886 | rdev->bdev = NULL; |
| 1887 | if (!bdev) |
| 1888 | MD_BUG(); |
| 1889 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1890 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | void md_autodetect_dev(dev_t dev); |
| 1894 | |
| 1895 | static void export_rdev(mdk_rdev_t * rdev) |
| 1896 | { |
| 1897 | char b[BDEVNAME_SIZE]; |
| 1898 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1899 | bdevname(rdev->bdev,b)); |
| 1900 | if (rdev->mddev) |
| 1901 | MD_BUG(); |
| 1902 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1904 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1905 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | #endif |
| 1907 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1908 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1912 | { |
| 1913 | unbind_rdev_from_array(rdev); |
| 1914 | export_rdev(rdev); |
| 1915 | } |
| 1916 | |
| 1917 | static void export_array(mddev_t *mddev) |
| 1918 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1919 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1921 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | if (!rdev->mddev) { |
| 1923 | MD_BUG(); |
| 1924 | continue; |
| 1925 | } |
| 1926 | kick_rdev_from_array(rdev); |
| 1927 | } |
| 1928 | if (!list_empty(&mddev->disks)) |
| 1929 | MD_BUG(); |
| 1930 | mddev->raid_disks = 0; |
| 1931 | mddev->major_version = 0; |
| 1932 | } |
| 1933 | |
| 1934 | static void print_desc(mdp_disk_t *desc) |
| 1935 | { |
| 1936 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1937 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1938 | } |
| 1939 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1940 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | { |
| 1942 | int i; |
| 1943 | |
| 1944 | printk(KERN_INFO |
| 1945 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1946 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1947 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1948 | sb->ctime); |
| 1949 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1950 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1951 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1952 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1953 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1954 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1955 | sb->failed_disks, sb->spare_disks, |
| 1956 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1957 | |
| 1958 | printk(KERN_INFO); |
| 1959 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1960 | mdp_disk_t *desc; |
| 1961 | |
| 1962 | desc = sb->disks + i; |
| 1963 | if (desc->number || desc->major || desc->minor || |
| 1964 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1965 | printk(" D %2d: ", i); |
| 1966 | print_desc(desc); |
| 1967 | } |
| 1968 | } |
| 1969 | printk(KERN_INFO "md: THIS: "); |
| 1970 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1973 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1974 | { |
| 1975 | __u8 *uuid; |
| 1976 | |
| 1977 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1978 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1979 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1980 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1981 | le32_to_cpu(sb->major_version), |
| 1982 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1983 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1984 | sb->set_name, |
| 1985 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1986 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1987 | |
| 1988 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1989 | printk(KERN_INFO |
| 1990 | "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] | 1991 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1992 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1993 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1994 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1995 | le32_to_cpu(sb->level), |
| 1996 | (unsigned long long)le64_to_cpu(sb->size), |
| 1997 | le32_to_cpu(sb->raid_disks), |
| 1998 | le32_to_cpu(sb->layout), |
| 1999 | le32_to_cpu(sb->chunksize), |
| 2000 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2001 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2002 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2003 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2004 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2005 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2006 | sb->devflags, |
| 2007 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2008 | (unsigned long long)le64_to_cpu(sb->events), |
| 2009 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2010 | le32_to_cpu(sb->sb_csum), |
| 2011 | le32_to_cpu(sb->max_dev) |
| 2012 | ); |
| 2013 | } |
| 2014 | |
| 2015 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | { |
| 2017 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2018 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2019 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2020 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2021 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2023 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2024 | switch (major_version) { |
| 2025 | case 0: |
| 2026 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2027 | break; |
| 2028 | case 1: |
| 2029 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2030 | break; |
| 2031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | } else |
| 2033 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2034 | } |
| 2035 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2036 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2038 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | mdk_rdev_t *rdev; |
| 2040 | mddev_t *mddev; |
| 2041 | char b[BDEVNAME_SIZE]; |
| 2042 | |
| 2043 | printk("\n"); |
| 2044 | printk("md: **********************************\n"); |
| 2045 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2046 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2047 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2049 | if (mddev->bitmap) |
| 2050 | bitmap_print_sb(mddev->bitmap); |
| 2051 | else |
| 2052 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2053 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2055 | printk("\n"); |
| 2056 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2057 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2058 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | } |
| 2060 | printk("md: **********************************\n"); |
| 2061 | printk("\n"); |
| 2062 | } |
| 2063 | |
| 2064 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2065 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2067 | /* Update each superblock (in-memory image), but |
| 2068 | * if we are allowed to, skip spares which already |
| 2069 | * have the right event counter, or have one earlier |
| 2070 | * (which would mean they aren't being marked as dirty |
| 2071 | * with the rest of the array) |
| 2072 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2075 | /* First make sure individual recovery_offsets are correct */ |
| 2076 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2077 | if (rdev->raid_disk >= 0 && |
| 2078 | !test_bit(In_sync, &rdev->flags) && |
| 2079 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2080 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2081 | |
| 2082 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2083 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2084 | if (rdev->sb_events == mddev->events || |
| 2085 | (nospares && |
| 2086 | rdev->raid_disk < 0 && |
| 2087 | (rdev->sb_events&1)==0 && |
| 2088 | rdev->sb_events+1 == mddev->events)) { |
| 2089 | /* Don't update this superblock */ |
| 2090 | rdev->sb_loaded = 2; |
| 2091 | } else { |
| 2092 | super_types[mddev->major_version]. |
| 2093 | sync_super(mddev, rdev); |
| 2094 | rdev->sb_loaded = 1; |
| 2095 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | } |
| 2097 | } |
| 2098 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2099 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2102 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2103 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2105 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2106 | if (mddev->external) |
| 2107 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2109 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2110 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2111 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2112 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2113 | force_change = 1; |
| 2114 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2115 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2116 | * though if events isn't the right even/odd, we will have to do |
| 2117 | * spares after all |
| 2118 | */ |
| 2119 | nospares = 1; |
| 2120 | if (force_change) |
| 2121 | nospares = 0; |
| 2122 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2123 | /* If the array is degraded, then skipping spares is both |
| 2124 | * dangerous and fairly pointless. |
| 2125 | * Dangerous because a device that was removed from the array |
| 2126 | * might have a event_count that still looks up-to-date, |
| 2127 | * so it can be re-added without a resync. |
| 2128 | * Pointless because if there are any spares to skip, |
| 2129 | * then a recovery will happen and soon that array won't |
| 2130 | * be degraded any more and the spare can go back to sleep then. |
| 2131 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2132 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2133 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2134 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2135 | |
| 2136 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2137 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2138 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2139 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 2140 | && (mddev->events & 1) |
| 2141 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2142 | mddev->events--; |
| 2143 | else { |
| 2144 | /* otherwise we have to go forward and ... */ |
| 2145 | mddev->events ++; |
| 2146 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2147 | /* .. if the array isn't clean, an 'even' event must also go |
| 2148 | * to spares. */ |
| 2149 | if ((mddev->events&1)==0) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2150 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2151 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2152 | /* otherwise an 'odd' event must go to spares */ |
| 2153 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2154 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2155 | } |
| 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | |
| 2168 | /* |
| 2169 | * do not write anything to disk if using |
| 2170 | * nonpersistent superblocks |
| 2171 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2172 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2173 | if (!mddev->external) |
| 2174 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2175 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2176 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2177 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2179 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2180 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2181 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | |
| 2183 | dprintk(KERN_INFO |
| 2184 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2185 | mdname(mddev),mddev->in_sync); |
| 2186 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2187 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2188 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | char b[BDEVNAME_SIZE]; |
| 2190 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2191 | if (rdev->sb_loaded != 1) |
| 2192 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2193 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | dprintk("(skipping faulty "); |
| 2195 | |
| 2196 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2197 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2198 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2199 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2200 | rdev->sb_page); |
| 2201 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2202 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2203 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2204 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | } else |
| 2207 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2208 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | /* only need to write one superblock... */ |
| 2210 | break; |
| 2211 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2212 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2213 | /* 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] | 2214 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2215 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2216 | if (mddev->in_sync != sync_req || |
| 2217 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2218 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2219 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2220 | goto repeat; |
| 2221 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2222 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2223 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2224 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2225 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2226 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2230 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2231 | * We want to accept with case. For this we use cmd_match. |
| 2232 | */ |
| 2233 | static int cmd_match(const char *cmd, const char *str) |
| 2234 | { |
| 2235 | /* See if cmd, written into a sysfs file, matches |
| 2236 | * str. They must either be the same, or cmd can |
| 2237 | * have a trailing newline |
| 2238 | */ |
| 2239 | while (*cmd && *str && *cmd == *str) { |
| 2240 | cmd++; |
| 2241 | str++; |
| 2242 | } |
| 2243 | if (*cmd == '\n') |
| 2244 | cmd++; |
| 2245 | if (*str || *cmd) |
| 2246 | return 0; |
| 2247 | return 1; |
| 2248 | } |
| 2249 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2250 | struct rdev_sysfs_entry { |
| 2251 | struct attribute attr; |
| 2252 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2253 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2254 | }; |
| 2255 | |
| 2256 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2257 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2258 | { |
| 2259 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2260 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2261 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2262 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2263 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2264 | sep = ","; |
| 2265 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2266 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2267 | len += sprintf(page+len, "%sin_sync",sep); |
| 2268 | sep = ","; |
| 2269 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2270 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2271 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2272 | sep = ","; |
| 2273 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2274 | if (test_bit(Blocked, &rdev->flags)) { |
| 2275 | len += sprintf(page+len, "%sblocked", sep); |
| 2276 | sep = ","; |
| 2277 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2278 | if (!test_bit(Faulty, &rdev->flags) && |
| 2279 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2280 | len += sprintf(page+len, "%sspare", sep); |
| 2281 | sep = ","; |
| 2282 | } |
| 2283 | return len+sprintf(page+len, "\n"); |
| 2284 | } |
| 2285 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2286 | static ssize_t |
| 2287 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2288 | { |
| 2289 | /* can write |
| 2290 | * faulty - simulates and error |
| 2291 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2292 | * writemostly - sets write_mostly |
| 2293 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2294 | * blocked - sets the Blocked flag |
| 2295 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2296 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2297 | */ |
| 2298 | int err = -EINVAL; |
| 2299 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2300 | md_error(rdev->mddev, rdev); |
| 2301 | err = 0; |
| 2302 | } else if (cmd_match(buf, "remove")) { |
| 2303 | if (rdev->raid_disk >= 0) |
| 2304 | err = -EBUSY; |
| 2305 | else { |
| 2306 | mddev_t *mddev = rdev->mddev; |
| 2307 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2308 | if (mddev->pers) |
| 2309 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2310 | md_new_event(mddev); |
| 2311 | err = 0; |
| 2312 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2313 | } else if (cmd_match(buf, "writemostly")) { |
| 2314 | set_bit(WriteMostly, &rdev->flags); |
| 2315 | err = 0; |
| 2316 | } else if (cmd_match(buf, "-writemostly")) { |
| 2317 | clear_bit(WriteMostly, &rdev->flags); |
| 2318 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2319 | } else if (cmd_match(buf, "blocked")) { |
| 2320 | set_bit(Blocked, &rdev->flags); |
| 2321 | err = 0; |
| 2322 | } else if (cmd_match(buf, "-blocked")) { |
| 2323 | clear_bit(Blocked, &rdev->flags); |
| 2324 | wake_up(&rdev->blocked_wait); |
| 2325 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2326 | md_wakeup_thread(rdev->mddev->thread); |
| 2327 | |
| 2328 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2329 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2330 | set_bit(In_sync, &rdev->flags); |
| 2331 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2332 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2333 | if (!err && rdev->sysfs_state) |
| 2334 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2335 | return err ? err : len; |
| 2336 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2337 | static struct rdev_sysfs_entry rdev_state = |
| 2338 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2339 | |
| 2340 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2341 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2342 | { |
| 2343 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2344 | } |
| 2345 | |
| 2346 | static ssize_t |
| 2347 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2348 | { |
| 2349 | char *e; |
| 2350 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2351 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2352 | atomic_set(&rdev->corrected_errors, n); |
| 2353 | return len; |
| 2354 | } |
| 2355 | return -EINVAL; |
| 2356 | } |
| 2357 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2358 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2359 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2360 | static ssize_t |
| 2361 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2362 | { |
| 2363 | if (rdev->raid_disk < 0) |
| 2364 | return sprintf(page, "none\n"); |
| 2365 | else |
| 2366 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2367 | } |
| 2368 | |
| 2369 | static ssize_t |
| 2370 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2371 | { |
| 2372 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2373 | int err; |
| 2374 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2375 | int slot = simple_strtoul(buf, &e, 10); |
| 2376 | if (strncmp(buf, "none", 4)==0) |
| 2377 | slot = -1; |
| 2378 | else if (e==buf || (*e && *e!= '\n')) |
| 2379 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2380 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2381 | /* Setting 'slot' on an active array requires also |
| 2382 | * updating the 'rd%d' link, and communicating |
| 2383 | * with the personality with ->hot_*_disk. |
| 2384 | * For now we only support removing |
| 2385 | * failed/spare devices. This normally happens automatically, |
| 2386 | * but not when the metadata is externally managed. |
| 2387 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2388 | if (rdev->raid_disk == -1) |
| 2389 | return -EEXIST; |
| 2390 | /* personality does all needed checks */ |
| 2391 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2392 | return -EINVAL; |
| 2393 | err = rdev->mddev->pers-> |
| 2394 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2395 | if (err) |
| 2396 | return err; |
| 2397 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2398 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2399 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2400 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2401 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2402 | } else if (rdev->mddev->pers) { |
| 2403 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2404 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2405 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2406 | */ |
| 2407 | |
| 2408 | if (rdev->raid_disk != -1) |
| 2409 | return -EBUSY; |
| 2410 | |
| 2411 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2412 | return -EINVAL; |
| 2413 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2414 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2415 | if (rdev2->raid_disk == slot) |
| 2416 | return -EEXIST; |
| 2417 | |
| 2418 | rdev->raid_disk = slot; |
| 2419 | if (test_bit(In_sync, &rdev->flags)) |
| 2420 | rdev->saved_raid_disk = slot; |
| 2421 | else |
| 2422 | rdev->saved_raid_disk = -1; |
| 2423 | err = rdev->mddev->pers-> |
| 2424 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2425 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2426 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2427 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2428 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2429 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2430 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2431 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2432 | printk(KERN_WARNING |
| 2433 | "md: cannot register " |
| 2434 | "%s for %s\n", |
| 2435 | nm, mdname(rdev->mddev)); |
| 2436 | |
| 2437 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2438 | } else { |
| 2439 | if (slot >= rdev->mddev->raid_disks) |
| 2440 | return -ENOSPC; |
| 2441 | rdev->raid_disk = slot; |
| 2442 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2443 | clear_bit(Faulty, &rdev->flags); |
| 2444 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2445 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2446 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2447 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2448 | return len; |
| 2449 | } |
| 2450 | |
| 2451 | |
| 2452 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2453 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2454 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2455 | static ssize_t |
| 2456 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2457 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2458 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | static ssize_t |
| 2462 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2463 | { |
| 2464 | char *e; |
| 2465 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2466 | if (e==buf || (*e && *e != '\n')) |
| 2467 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2468 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2469 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2470 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2471 | /* Must set offset before size, so overlap checks |
| 2472 | * can be sane */ |
| 2473 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2474 | rdev->data_offset = offset; |
| 2475 | return len; |
| 2476 | } |
| 2477 | |
| 2478 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2479 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2480 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2481 | static ssize_t |
| 2482 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2483 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2484 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2485 | } |
| 2486 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2487 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2488 | { |
| 2489 | /* check if two start/length pairs overlap */ |
| 2490 | if (s1+l1 <= s2) |
| 2491 | return 0; |
| 2492 | if (s2+l2 <= s1) |
| 2493 | return 0; |
| 2494 | return 1; |
| 2495 | } |
| 2496 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2497 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2498 | { |
| 2499 | unsigned long long blocks; |
| 2500 | sector_t new; |
| 2501 | |
| 2502 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2503 | return -EINVAL; |
| 2504 | |
| 2505 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2506 | return -EINVAL; /* sector conversion overflow */ |
| 2507 | |
| 2508 | new = blocks * 2; |
| 2509 | if (new != blocks * 2) |
| 2510 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2511 | |
| 2512 | *sectors = new; |
| 2513 | return 0; |
| 2514 | } |
| 2515 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2516 | static ssize_t |
| 2517 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2518 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2519 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2520 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2521 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2522 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2523 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2524 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2525 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2526 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2527 | sectors = super_types[my_mddev->major_version]. |
| 2528 | rdev_size_change(rdev, sectors); |
| 2529 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2530 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2531 | } else if (!sectors) |
| 2532 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2533 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2534 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2535 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2536 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2537 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2538 | rdev->sectors = sectors; |
| 2539 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2540 | /* need to check that all other rdevs with the same ->bdev |
| 2541 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2542 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2543 | * we have to change it back, we will have the lock again. |
| 2544 | */ |
| 2545 | mddev_t *mddev; |
| 2546 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2547 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2548 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2549 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2550 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2551 | mdk_rdev_t *rdev2; |
| 2552 | |
| 2553 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2554 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2555 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2556 | (rdev->bdev == rdev2->bdev && |
| 2557 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2558 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2559 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2560 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2561 | overlap = 1; |
| 2562 | break; |
| 2563 | } |
| 2564 | mddev_unlock(mddev); |
| 2565 | if (overlap) { |
| 2566 | mddev_put(mddev); |
| 2567 | break; |
| 2568 | } |
| 2569 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2570 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2571 | if (overlap) { |
| 2572 | /* Someone else could have slipped in a size |
| 2573 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2574 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2575 | * safe, and trust userspace not to race with |
| 2576 | * itself |
| 2577 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2578 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2579 | return -EBUSY; |
| 2580 | } |
| 2581 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2582 | return len; |
| 2583 | } |
| 2584 | |
| 2585 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2586 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2587 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2588 | |
| 2589 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2590 | { |
| 2591 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2592 | |
| 2593 | if (test_bit(In_sync, &rdev->flags) || |
| 2594 | recovery_start == MaxSector) |
| 2595 | return sprintf(page, "none\n"); |
| 2596 | |
| 2597 | return sprintf(page, "%llu\n", recovery_start); |
| 2598 | } |
| 2599 | |
| 2600 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2601 | { |
| 2602 | unsigned long long recovery_start; |
| 2603 | |
| 2604 | if (cmd_match(buf, "none")) |
| 2605 | recovery_start = MaxSector; |
| 2606 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2607 | return -EINVAL; |
| 2608 | |
| 2609 | if (rdev->mddev->pers && |
| 2610 | rdev->raid_disk >= 0) |
| 2611 | return -EBUSY; |
| 2612 | |
| 2613 | rdev->recovery_offset = recovery_start; |
| 2614 | if (recovery_start == MaxSector) |
| 2615 | set_bit(In_sync, &rdev->flags); |
| 2616 | else |
| 2617 | clear_bit(In_sync, &rdev->flags); |
| 2618 | return len; |
| 2619 | } |
| 2620 | |
| 2621 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2622 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2623 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2624 | static struct attribute *rdev_default_attrs[] = { |
| 2625 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2626 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2627 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2628 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2629 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2630 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2631 | NULL, |
| 2632 | }; |
| 2633 | static ssize_t |
| 2634 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2635 | { |
| 2636 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2637 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2638 | mddev_t *mddev = rdev->mddev; |
| 2639 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2640 | |
| 2641 | if (!entry->show) |
| 2642 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2643 | |
| 2644 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2645 | if (!rv) { |
| 2646 | if (rdev->mddev == NULL) |
| 2647 | rv = -EBUSY; |
| 2648 | else |
| 2649 | rv = entry->show(rdev, page); |
| 2650 | mddev_unlock(mddev); |
| 2651 | } |
| 2652 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | static ssize_t |
| 2656 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2657 | const char *page, size_t length) |
| 2658 | { |
| 2659 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2660 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2661 | ssize_t rv; |
| 2662 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2663 | |
| 2664 | if (!entry->store) |
| 2665 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2666 | if (!capable(CAP_SYS_ADMIN)) |
| 2667 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2668 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2669 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2670 | if (rdev->mddev == NULL) |
| 2671 | rv = -EBUSY; |
| 2672 | else |
| 2673 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2674 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2675 | } |
| 2676 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | static void rdev_free(struct kobject *ko) |
| 2680 | { |
| 2681 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2682 | kfree(rdev); |
| 2683 | } |
| 2684 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2685 | .show = rdev_attr_show, |
| 2686 | .store = rdev_attr_store, |
| 2687 | }; |
| 2688 | static struct kobj_type rdev_ktype = { |
| 2689 | .release = rdev_free, |
| 2690 | .sysfs_ops = &rdev_sysfs_ops, |
| 2691 | .default_attrs = rdev_default_attrs, |
| 2692 | }; |
| 2693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | /* |
| 2695 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2696 | * |
| 2697 | * mark the device faulty if: |
| 2698 | * |
| 2699 | * - the device is nonexistent (zero size) |
| 2700 | * - the device has no valid superblock |
| 2701 | * |
| 2702 | * a faulty rdev _never_ has rdev->sb set. |
| 2703 | */ |
| 2704 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2705 | { |
| 2706 | char b[BDEVNAME_SIZE]; |
| 2707 | int err; |
| 2708 | mdk_rdev_t *rdev; |
| 2709 | sector_t size; |
| 2710 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2711 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | if (!rdev) { |
| 2713 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2714 | return ERR_PTR(-ENOMEM); |
| 2715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | |
| 2717 | if ((err = alloc_disk_sb(rdev))) |
| 2718 | goto abort_free; |
| 2719 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2720 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | if (err) |
| 2722 | goto abort_free; |
| 2723 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2724 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2727 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2728 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2729 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2731 | rdev->sb_events = 0; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2732 | rdev->last_read_error.tv_sec = 0; |
| 2733 | rdev->last_read_error.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2735 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2736 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | |
| 2738 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2739 | if (!size) { |
| 2740 | printk(KERN_WARNING |
| 2741 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2742 | bdevname(rdev->bdev,b)); |
| 2743 | err = -EINVAL; |
| 2744 | goto abort_free; |
| 2745 | } |
| 2746 | |
| 2747 | if (super_format >= 0) { |
| 2748 | err = super_types[super_format]. |
| 2749 | load_super(rdev, NULL, super_minor); |
| 2750 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2751 | printk(KERN_WARNING |
| 2752 | "md: %s does not have a valid v%d.%d " |
| 2753 | "superblock, not importing!\n", |
| 2754 | bdevname(rdev->bdev,b), |
| 2755 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | goto abort_free; |
| 2757 | } |
| 2758 | if (err < 0) { |
| 2759 | printk(KERN_WARNING |
| 2760 | "md: could not read %s's sb, not importing!\n", |
| 2761 | bdevname(rdev->bdev,b)); |
| 2762 | goto abort_free; |
| 2763 | } |
| 2764 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2766 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2767 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | |
| 2769 | return rdev; |
| 2770 | |
| 2771 | abort_free: |
| 2772 | if (rdev->sb_page) { |
| 2773 | if (rdev->bdev) |
| 2774 | unlock_rdev(rdev); |
| 2775 | free_disk_sb(rdev); |
| 2776 | } |
| 2777 | kfree(rdev); |
| 2778 | return ERR_PTR(err); |
| 2779 | } |
| 2780 | |
| 2781 | /* |
| 2782 | * Check a full RAID array for plausibility |
| 2783 | */ |
| 2784 | |
| 2785 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2786 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2787 | { |
| 2788 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2789 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | char b[BDEVNAME_SIZE]; |
| 2791 | |
| 2792 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2793 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | switch (super_types[mddev->major_version]. |
| 2795 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2796 | case 1: |
| 2797 | freshest = rdev; |
| 2798 | break; |
| 2799 | case 0: |
| 2800 | break; |
| 2801 | default: |
| 2802 | printk( KERN_ERR \ |
| 2803 | "md: fatal superblock inconsistency in %s" |
| 2804 | " -- removing from array\n", |
| 2805 | bdevname(rdev->bdev,b)); |
| 2806 | kick_rdev_from_array(rdev); |
| 2807 | } |
| 2808 | |
| 2809 | |
| 2810 | super_types[mddev->major_version]. |
| 2811 | validate_super(mddev, freshest); |
| 2812 | |
| 2813 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2814 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2815 | if (mddev->max_disks && |
| 2816 | (rdev->desc_nr >= mddev->max_disks || |
| 2817 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2818 | printk(KERN_WARNING |
| 2819 | "md: %s: %s: only %d devices permitted\n", |
| 2820 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2821 | mddev->max_disks); |
| 2822 | kick_rdev_from_array(rdev); |
| 2823 | continue; |
| 2824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | if (rdev != freshest) |
| 2826 | if (super_types[mddev->major_version]. |
| 2827 | validate_super(mddev, rdev)) { |
| 2828 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2829 | " from array!\n", |
| 2830 | bdevname(rdev->bdev,b)); |
| 2831 | kick_rdev_from_array(rdev); |
| 2832 | continue; |
| 2833 | } |
| 2834 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2835 | rdev->desc_nr = i++; |
| 2836 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2837 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2838 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2839 | rdev->raid_disk = -1; |
| 2840 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | } |
| 2842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | } |
| 2844 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2845 | /* Read a fixed-point number. |
| 2846 | * Numbers in sysfs attributes should be in "standard" units where |
| 2847 | * possible, so time should be in seconds. |
| 2848 | * However we internally use a a much smaller unit such as |
| 2849 | * milliseconds or jiffies. |
| 2850 | * This function takes a decimal number with a possible fractional |
| 2851 | * component, and produces an integer which is the result of |
| 2852 | * multiplying that number by 10^'scale'. |
| 2853 | * all without any floating-point arithmetic. |
| 2854 | */ |
| 2855 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2856 | { |
| 2857 | unsigned long result = 0; |
| 2858 | long decimals = -1; |
| 2859 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2860 | if (*cp == '.') |
| 2861 | decimals = 0; |
| 2862 | else if (decimals < scale) { |
| 2863 | unsigned int value; |
| 2864 | value = *cp - '0'; |
| 2865 | result = result * 10 + value; |
| 2866 | if (decimals >= 0) |
| 2867 | decimals++; |
| 2868 | } |
| 2869 | cp++; |
| 2870 | } |
| 2871 | if (*cp == '\n') |
| 2872 | cp++; |
| 2873 | if (*cp) |
| 2874 | return -EINVAL; |
| 2875 | if (decimals < 0) |
| 2876 | decimals = 0; |
| 2877 | while (decimals < scale) { |
| 2878 | result *= 10; |
| 2879 | decimals ++; |
| 2880 | } |
| 2881 | *res = result; |
| 2882 | return 0; |
| 2883 | } |
| 2884 | |
| 2885 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2886 | static void md_safemode_timeout(unsigned long data); |
| 2887 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2888 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2889 | safe_delay_show(mddev_t *mddev, char *page) |
| 2890 | { |
| 2891 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2892 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2893 | } |
| 2894 | static ssize_t |
| 2895 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2896 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2897 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2898 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2899 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2900 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2901 | if (msec == 0) |
| 2902 | mddev->safemode_delay = 0; |
| 2903 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2904 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2905 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2906 | if (mddev->safemode_delay == 0) |
| 2907 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2908 | if (mddev->safemode_delay < old_delay) |
| 2909 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2910 | } |
| 2911 | return len; |
| 2912 | } |
| 2913 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2914 | __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] | 2915 | |
| 2916 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2917 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2918 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2919 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2920 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2921 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2922 | else if (mddev->clevel[0]) |
| 2923 | return sprintf(page, "%s\n", mddev->clevel); |
| 2924 | else if (mddev->level != LEVEL_NONE) |
| 2925 | return sprintf(page, "%d\n", mddev->level); |
| 2926 | else |
| 2927 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2928 | } |
| 2929 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2930 | static ssize_t |
| 2931 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2932 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2933 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2934 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2935 | struct mdk_personality *pers; |
| 2936 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2937 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2938 | |
| 2939 | if (mddev->pers == NULL) { |
| 2940 | if (len == 0) |
| 2941 | return 0; |
| 2942 | if (len >= sizeof(mddev->clevel)) |
| 2943 | return -ENOSPC; |
| 2944 | strncpy(mddev->clevel, buf, len); |
| 2945 | if (mddev->clevel[len-1] == '\n') |
| 2946 | len--; |
| 2947 | mddev->clevel[len] = 0; |
| 2948 | mddev->level = LEVEL_NONE; |
| 2949 | return rv; |
| 2950 | } |
| 2951 | |
| 2952 | /* request to change the personality. Need to ensure: |
| 2953 | * - array is not engaged in resync/recovery/reshape |
| 2954 | * - old personality can be suspended |
| 2955 | * - new personality will access other array. |
| 2956 | */ |
| 2957 | |
| 2958 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2959 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2960 | |
| 2961 | if (!mddev->pers->quiesce) { |
| 2962 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2963 | mdname(mddev), mddev->pers->name); |
| 2964 | return -EINVAL; |
| 2965 | } |
| 2966 | |
| 2967 | /* Now find the new personality */ |
| 2968 | if (len == 0 || len >= sizeof(level)) |
| 2969 | return -EINVAL; |
| 2970 | strncpy(level, buf, len); |
| 2971 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2972 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2973 | level[len] = 0; |
| 2974 | |
| 2975 | request_module("md-%s", level); |
| 2976 | spin_lock(&pers_lock); |
| 2977 | pers = find_pers(LEVEL_NONE, level); |
| 2978 | if (!pers || !try_module_get(pers->owner)) { |
| 2979 | spin_unlock(&pers_lock); |
| 2980 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2981 | return -EINVAL; |
| 2982 | } |
| 2983 | spin_unlock(&pers_lock); |
| 2984 | |
| 2985 | if (pers == mddev->pers) { |
| 2986 | /* Nothing to do! */ |
| 2987 | module_put(pers->owner); |
| 2988 | return rv; |
| 2989 | } |
| 2990 | if (!pers->takeover) { |
| 2991 | module_put(pers->owner); |
| 2992 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2993 | mdname(mddev), level); |
| 2994 | return -EINVAL; |
| 2995 | } |
| 2996 | |
| 2997 | /* ->takeover must set new_* and/or delta_disks |
| 2998 | * if it succeeds, and may set them when it fails. |
| 2999 | */ |
| 3000 | priv = pers->takeover(mddev); |
| 3001 | if (IS_ERR(priv)) { |
| 3002 | mddev->new_level = mddev->level; |
| 3003 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3004 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3005 | mddev->raid_disks -= mddev->delta_disks; |
| 3006 | mddev->delta_disks = 0; |
| 3007 | module_put(pers->owner); |
| 3008 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 3009 | mdname(mddev), level); |
| 3010 | return PTR_ERR(priv); |
| 3011 | } |
| 3012 | |
| 3013 | /* Looks like we have a winner */ |
| 3014 | mddev_suspend(mddev); |
| 3015 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3016 | |
| 3017 | if (mddev->pers->sync_request == NULL && |
| 3018 | pers->sync_request != NULL) { |
| 3019 | /* need to add the md_redundancy_group */ |
| 3020 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3021 | printk(KERN_WARNING |
| 3022 | "md: cannot register extra attributes for %s\n", |
| 3023 | mdname(mddev)); |
| 3024 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3025 | } |
| 3026 | if (mddev->pers->sync_request != NULL && |
| 3027 | pers->sync_request == NULL) { |
| 3028 | /* need to remove the md_redundancy_group */ |
| 3029 | if (mddev->to_remove == NULL) |
| 3030 | mddev->to_remove = &md_redundancy_group; |
| 3031 | } |
| 3032 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3033 | if (mddev->pers->sync_request == NULL && |
| 3034 | mddev->external) { |
| 3035 | /* We are converting from a no-redundancy array |
| 3036 | * to a redundancy array and metadata is managed |
| 3037 | * externally so we need to be sure that writes |
| 3038 | * won't block due to a need to transition |
| 3039 | * clean->dirty |
| 3040 | * until external management is started. |
| 3041 | */ |
| 3042 | mddev->in_sync = 0; |
| 3043 | mddev->safemode_delay = 0; |
| 3044 | mddev->safemode = 0; |
| 3045 | } |
| 3046 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3047 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3048 | /* Invalidate devices that are now superfluous */ |
| 3049 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3050 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 3051 | rdev->raid_disk = -1; |
| 3052 | clear_bit(In_sync, &rdev->flags); |
| 3053 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3054 | mddev->pers = pers; |
| 3055 | mddev->private = priv; |
| 3056 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3057 | mddev->level = mddev->new_level; |
| 3058 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3059 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3060 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3061 | if (mddev->pers->sync_request == NULL) { |
| 3062 | /* this is now an array without redundancy, so |
| 3063 | * it must always be in_sync |
| 3064 | */ |
| 3065 | mddev->in_sync = 1; |
| 3066 | del_timer_sync(&mddev->safemode_timer); |
| 3067 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3068 | pers->run(mddev); |
| 3069 | mddev_resume(mddev); |
| 3070 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3071 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3072 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3073 | sysfs_notify(&mddev->kobj, NULL, "level"); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3074 | return rv; |
| 3075 | } |
| 3076 | |
| 3077 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3078 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3079 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3080 | |
| 3081 | static ssize_t |
| 3082 | layout_show(mddev_t *mddev, char *page) |
| 3083 | { |
| 3084 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3085 | if (mddev->reshape_position != MaxSector && |
| 3086 | mddev->layout != mddev->new_layout) |
| 3087 | return sprintf(page, "%d (%d)\n", |
| 3088 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3089 | return sprintf(page, "%d\n", mddev->layout); |
| 3090 | } |
| 3091 | |
| 3092 | static ssize_t |
| 3093 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3094 | { |
| 3095 | char *e; |
| 3096 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3097 | |
| 3098 | if (!*buf || (*e && *e != '\n')) |
| 3099 | return -EINVAL; |
| 3100 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3101 | if (mddev->pers) { |
| 3102 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3103 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3104 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3105 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3106 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3107 | if (err) { |
| 3108 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3109 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3110 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3111 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3112 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3113 | if (mddev->reshape_position == MaxSector) |
| 3114 | mddev->layout = n; |
| 3115 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3116 | return len; |
| 3117 | } |
| 3118 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3119 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3120 | |
| 3121 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3122 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3123 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3124 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3125 | if (mddev->raid_disks == 0) |
| 3126 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3127 | if (mddev->reshape_position != MaxSector && |
| 3128 | mddev->delta_disks != 0) |
| 3129 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3130 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3131 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3132 | } |
| 3133 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3134 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3135 | |
| 3136 | static ssize_t |
| 3137 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3138 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3139 | char *e; |
| 3140 | int rv = 0; |
| 3141 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3142 | |
| 3143 | if (!*buf || (*e && *e != '\n')) |
| 3144 | return -EINVAL; |
| 3145 | |
| 3146 | if (mddev->pers) |
| 3147 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3148 | else if (mddev->reshape_position != MaxSector) { |
| 3149 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3150 | mddev->delta_disks = n - olddisks; |
| 3151 | mddev->raid_disks = n; |
| 3152 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3153 | mddev->raid_disks = n; |
| 3154 | return rv ? rv : len; |
| 3155 | } |
| 3156 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3157 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3158 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3159 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3160 | chunk_size_show(mddev_t *mddev, char *page) |
| 3161 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3162 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3163 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3164 | return sprintf(page, "%d (%d)\n", |
| 3165 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3166 | mddev->chunk_sectors << 9); |
| 3167 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | static ssize_t |
| 3171 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3172 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3173 | char *e; |
| 3174 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3175 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3176 | if (!*buf || (*e && *e != '\n')) |
| 3177 | return -EINVAL; |
| 3178 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3179 | if (mddev->pers) { |
| 3180 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3181 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3182 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3183 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3184 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3185 | if (err) { |
| 3186 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3187 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3188 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3189 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3190 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3191 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3192 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3193 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3194 | return len; |
| 3195 | } |
| 3196 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3197 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3198 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3199 | static ssize_t |
| 3200 | resync_start_show(mddev_t *mddev, char *page) |
| 3201 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3202 | if (mddev->recovery_cp == MaxSector) |
| 3203 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3204 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3205 | } |
| 3206 | |
| 3207 | static ssize_t |
| 3208 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3209 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3210 | char *e; |
| 3211 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3212 | |
| 3213 | if (mddev->pers) |
| 3214 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3215 | if (cmd_match(buf, "none")) |
| 3216 | n = MaxSector; |
| 3217 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3218 | return -EINVAL; |
| 3219 | |
| 3220 | mddev->recovery_cp = n; |
| 3221 | return len; |
| 3222 | } |
| 3223 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3224 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3225 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3226 | /* |
| 3227 | * The array state can be: |
| 3228 | * |
| 3229 | * clear |
| 3230 | * No devices, no size, no level |
| 3231 | * Equivalent to STOP_ARRAY ioctl |
| 3232 | * inactive |
| 3233 | * May have some settings, but array is not active |
| 3234 | * all IO results in error |
| 3235 | * When written, doesn't tear down array, but just stops it |
| 3236 | * suspended (not supported yet) |
| 3237 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3238 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3239 | * readonly |
| 3240 | * no resync can happen. no superblocks get written. |
| 3241 | * write requests fail |
| 3242 | * read-auto |
| 3243 | * like readonly, but behaves like 'clean' on a write request. |
| 3244 | * |
| 3245 | * clean - no pending writes, but otherwise active. |
| 3246 | * When written to inactive array, starts without resync |
| 3247 | * If a write request arrives then |
| 3248 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3249 | * if not known, block and switch to write-pending |
| 3250 | * If written to an active array that has pending writes, then fails. |
| 3251 | * active |
| 3252 | * fully active: IO and resync can be happening. |
| 3253 | * When written to inactive array, starts with resync |
| 3254 | * |
| 3255 | * write-pending |
| 3256 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3257 | * |
| 3258 | * active-idle |
| 3259 | * like active, but no writes have been seen for a while (100msec). |
| 3260 | * |
| 3261 | */ |
| 3262 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3263 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3264 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3265 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3266 | "write-pending", "active-idle", NULL }; |
| 3267 | |
| 3268 | static int match_word(const char *word, char **list) |
| 3269 | { |
| 3270 | int n; |
| 3271 | for (n=0; list[n]; n++) |
| 3272 | if (cmd_match(word, list[n])) |
| 3273 | break; |
| 3274 | return n; |
| 3275 | } |
| 3276 | |
| 3277 | static ssize_t |
| 3278 | array_state_show(mddev_t *mddev, char *page) |
| 3279 | { |
| 3280 | enum array_state st = inactive; |
| 3281 | |
| 3282 | if (mddev->pers) |
| 3283 | switch(mddev->ro) { |
| 3284 | case 1: |
| 3285 | st = readonly; |
| 3286 | break; |
| 3287 | case 2: |
| 3288 | st = read_auto; |
| 3289 | break; |
| 3290 | case 0: |
| 3291 | if (mddev->in_sync) |
| 3292 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3293 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3294 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3295 | else if (mddev->safemode) |
| 3296 | st = active_idle; |
| 3297 | else |
| 3298 | st = active; |
| 3299 | } |
| 3300 | else { |
| 3301 | if (list_empty(&mddev->disks) && |
| 3302 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3303 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3304 | st = clear; |
| 3305 | else |
| 3306 | st = inactive; |
| 3307 | } |
| 3308 | return sprintf(page, "%s\n", array_states[st]); |
| 3309 | } |
| 3310 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3311 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 3312 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3313 | static int do_md_run(mddev_t * mddev); |
| 3314 | static int restart_array(mddev_t *mddev); |
| 3315 | |
| 3316 | static ssize_t |
| 3317 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3318 | { |
| 3319 | int err = -EINVAL; |
| 3320 | enum array_state st = match_word(buf, array_states); |
| 3321 | switch(st) { |
| 3322 | case bad_word: |
| 3323 | break; |
| 3324 | case clear: |
| 3325 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3326 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3327 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3328 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3329 | break; |
| 3330 | case inactive: |
| 3331 | /* stopping an active array */ |
| 3332 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3333 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3334 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3335 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3336 | } else |
| 3337 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3338 | break; |
| 3339 | case suspended: |
| 3340 | break; /* not supported yet */ |
| 3341 | case readonly: |
| 3342 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 3343 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3344 | else { |
| 3345 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3346 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3347 | err = do_md_run(mddev); |
| 3348 | } |
| 3349 | break; |
| 3350 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3351 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3352 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 3353 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3354 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3355 | err = restart_array(mddev); |
| 3356 | if (err == 0) { |
| 3357 | mddev->ro = 2; |
| 3358 | set_disk_ro(mddev->gendisk, 0); |
| 3359 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3360 | } else { |
| 3361 | mddev->ro = 2; |
| 3362 | err = do_md_run(mddev); |
| 3363 | } |
| 3364 | break; |
| 3365 | case clean: |
| 3366 | if (mddev->pers) { |
| 3367 | restart_array(mddev); |
| 3368 | spin_lock_irq(&mddev->write_lock); |
| 3369 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3370 | if (mddev->in_sync == 0) { |
| 3371 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3372 | if (mddev->safemode == 1) |
| 3373 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3374 | if (mddev->persistent) |
| 3375 | set_bit(MD_CHANGE_CLEAN, |
| 3376 | &mddev->flags); |
| 3377 | } |
| 3378 | err = 0; |
| 3379 | } else |
| 3380 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3381 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3382 | } else |
| 3383 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3384 | break; |
| 3385 | case active: |
| 3386 | if (mddev->pers) { |
| 3387 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3388 | if (mddev->external) |
| 3389 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3390 | wake_up(&mddev->sb_wait); |
| 3391 | err = 0; |
| 3392 | } else { |
| 3393 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3394 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3395 | err = do_md_run(mddev); |
| 3396 | } |
| 3397 | break; |
| 3398 | case write_pending: |
| 3399 | case active_idle: |
| 3400 | /* these cannot be set */ |
| 3401 | break; |
| 3402 | } |
| 3403 | if (err) |
| 3404 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3405 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3406 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3407 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3408 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3409 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3410 | static struct md_sysfs_entry md_array_state = |
| 3411 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3412 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3413 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3414 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3415 | return sprintf(page, "%d\n", |
| 3416 | atomic_read(&mddev->max_corr_read_errors)); |
| 3417 | } |
| 3418 | |
| 3419 | static ssize_t |
| 3420 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3421 | { |
| 3422 | char *e; |
| 3423 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3424 | |
| 3425 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3426 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3427 | return len; |
| 3428 | } |
| 3429 | return -EINVAL; |
| 3430 | } |
| 3431 | |
| 3432 | static struct md_sysfs_entry max_corr_read_errors = |
| 3433 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3434 | max_corrected_read_errors_store); |
| 3435 | |
| 3436 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3437 | null_show(mddev_t *mddev, char *page) |
| 3438 | { |
| 3439 | return -EINVAL; |
| 3440 | } |
| 3441 | |
| 3442 | static ssize_t |
| 3443 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3444 | { |
| 3445 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3446 | /* The new device is added to the array. |
| 3447 | * If the array has a persistent superblock, we read the |
| 3448 | * superblock to initialise info and check validity. |
| 3449 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3450 | * which mainly checks size. |
| 3451 | */ |
| 3452 | char *e; |
| 3453 | int major = simple_strtoul(buf, &e, 10); |
| 3454 | int minor; |
| 3455 | dev_t dev; |
| 3456 | mdk_rdev_t *rdev; |
| 3457 | int err; |
| 3458 | |
| 3459 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3460 | return -EINVAL; |
| 3461 | minor = simple_strtoul(e+1, &e, 10); |
| 3462 | if (*e && *e != '\n') |
| 3463 | return -EINVAL; |
| 3464 | dev = MKDEV(major, minor); |
| 3465 | if (major != MAJOR(dev) || |
| 3466 | minor != MINOR(dev)) |
| 3467 | return -EOVERFLOW; |
| 3468 | |
| 3469 | |
| 3470 | if (mddev->persistent) { |
| 3471 | rdev = md_import_device(dev, mddev->major_version, |
| 3472 | mddev->minor_version); |
| 3473 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3474 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3475 | mdk_rdev_t, same_set); |
| 3476 | err = super_types[mddev->major_version] |
| 3477 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3478 | if (err < 0) |
| 3479 | goto out; |
| 3480 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3481 | } else if (mddev->external) |
| 3482 | rdev = md_import_device(dev, -2, -1); |
| 3483 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3484 | rdev = md_import_device(dev, -1, -1); |
| 3485 | |
| 3486 | if (IS_ERR(rdev)) |
| 3487 | return PTR_ERR(rdev); |
| 3488 | err = bind_rdev_to_array(rdev, mddev); |
| 3489 | out: |
| 3490 | if (err) |
| 3491 | export_rdev(rdev); |
| 3492 | return err ? err : len; |
| 3493 | } |
| 3494 | |
| 3495 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3496 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3497 | |
| 3498 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3499 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3500 | { |
| 3501 | char *end; |
| 3502 | unsigned long chunk, end_chunk; |
| 3503 | |
| 3504 | if (!mddev->bitmap) |
| 3505 | goto out; |
| 3506 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3507 | while (*buf) { |
| 3508 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3509 | if (buf == end) break; |
| 3510 | if (*end == '-') { /* range */ |
| 3511 | buf = end + 1; |
| 3512 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3513 | if (buf == end) break; |
| 3514 | } |
| 3515 | if (*end && !isspace(*end)) break; |
| 3516 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3517 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3518 | } |
| 3519 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3520 | out: |
| 3521 | return len; |
| 3522 | } |
| 3523 | |
| 3524 | static struct md_sysfs_entry md_bitmap = |
| 3525 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3526 | |
| 3527 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3528 | size_show(mddev_t *mddev, char *page) |
| 3529 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3530 | return sprintf(page, "%llu\n", |
| 3531 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3532 | } |
| 3533 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3534 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3535 | |
| 3536 | static ssize_t |
| 3537 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3538 | { |
| 3539 | /* If array is inactive, we can reduce the component size, but |
| 3540 | * not increase it (except from 0). |
| 3541 | * If array is active, we can try an on-line resize |
| 3542 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3543 | sector_t sectors; |
| 3544 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3545 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3546 | if (err < 0) |
| 3547 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3548 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3549 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3550 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3551 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3552 | if (mddev->dev_sectors == 0 || |
| 3553 | mddev->dev_sectors > sectors) |
| 3554 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3555 | else |
| 3556 | err = -ENOSPC; |
| 3557 | } |
| 3558 | return err ? err : len; |
| 3559 | } |
| 3560 | |
| 3561 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3562 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3563 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3564 | |
| 3565 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3566 | * This is one of |
| 3567 | * 'none' for arrays with no metadata (good luck...) |
| 3568 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3569 | * or N.M for internally known formats |
| 3570 | */ |
| 3571 | static ssize_t |
| 3572 | metadata_show(mddev_t *mddev, char *page) |
| 3573 | { |
| 3574 | if (mddev->persistent) |
| 3575 | return sprintf(page, "%d.%d\n", |
| 3576 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3577 | else if (mddev->external) |
| 3578 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3579 | else |
| 3580 | return sprintf(page, "none\n"); |
| 3581 | } |
| 3582 | |
| 3583 | static ssize_t |
| 3584 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3585 | { |
| 3586 | int major, minor; |
| 3587 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3588 | /* Changing the details of 'external' metadata is |
| 3589 | * always permitted. Otherwise there must be |
| 3590 | * no devices attached to the array. |
| 3591 | */ |
| 3592 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3593 | ; |
| 3594 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3595 | return -EBUSY; |
| 3596 | |
| 3597 | if (cmd_match(buf, "none")) { |
| 3598 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3599 | mddev->external = 0; |
| 3600 | mddev->major_version = 0; |
| 3601 | mddev->minor_version = 90; |
| 3602 | return len; |
| 3603 | } |
| 3604 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3605 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3606 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3607 | namelen = sizeof(mddev->metadata_type)-1; |
| 3608 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3609 | mddev->metadata_type[namelen] = 0; |
| 3610 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3611 | mddev->metadata_type[--namelen] = 0; |
| 3612 | mddev->persistent = 0; |
| 3613 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3614 | mddev->major_version = 0; |
| 3615 | mddev->minor_version = 90; |
| 3616 | return len; |
| 3617 | } |
| 3618 | major = simple_strtoul(buf, &e, 10); |
| 3619 | if (e==buf || *e != '.') |
| 3620 | return -EINVAL; |
| 3621 | buf = e+1; |
| 3622 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3623 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3624 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3625 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3626 | return -ENOENT; |
| 3627 | mddev->major_version = major; |
| 3628 | mddev->minor_version = minor; |
| 3629 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3630 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3631 | return len; |
| 3632 | } |
| 3633 | |
| 3634 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3635 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3636 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3637 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3638 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3639 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3640 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3641 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3642 | type = "frozen"; |
| 3643 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3644 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3645 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3646 | type = "reshape"; |
| 3647 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3648 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3649 | type = "resync"; |
| 3650 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3651 | type = "check"; |
| 3652 | else |
| 3653 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3654 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3655 | type = "recover"; |
| 3656 | } |
| 3657 | return sprintf(page, "%s\n", type); |
| 3658 | } |
| 3659 | |
| 3660 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3661 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3662 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3663 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3664 | return -EINVAL; |
| 3665 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3666 | if (cmd_match(page, "frozen")) |
| 3667 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3668 | else |
| 3669 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3670 | |
| 3671 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3672 | if (mddev->sync_thread) { |
| 3673 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3674 | md_unregister_thread(mddev->sync_thread); |
| 3675 | mddev->sync_thread = NULL; |
| 3676 | mddev->recovery = 0; |
| 3677 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3678 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3679 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3680 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3681 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3682 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3683 | else if (cmd_match(page, "recover")) { |
| 3684 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3685 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3686 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3687 | int err; |
| 3688 | if (mddev->pers->start_reshape == NULL) |
| 3689 | return -EINVAL; |
| 3690 | err = mddev->pers->start_reshape(mddev); |
| 3691 | if (err) |
| 3692 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3693 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3694 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3695 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3696 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3697 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3698 | return -EINVAL; |
| 3699 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3700 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3701 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3702 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3703 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3704 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3705 | return len; |
| 3706 | } |
| 3707 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3708 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3709 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3710 | { |
| 3711 | return sprintf(page, "%llu\n", |
| 3712 | (unsigned long long) mddev->resync_mismatches); |
| 3713 | } |
| 3714 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3715 | static struct md_sysfs_entry md_scan_mode = |
| 3716 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3717 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3718 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3719 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3720 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3721 | static ssize_t |
| 3722 | sync_min_show(mddev_t *mddev, char *page) |
| 3723 | { |
| 3724 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3725 | mddev->sync_speed_min ? "local": "system"); |
| 3726 | } |
| 3727 | |
| 3728 | static ssize_t |
| 3729 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3730 | { |
| 3731 | int min; |
| 3732 | char *e; |
| 3733 | if (strncmp(buf, "system", 6)==0) { |
| 3734 | mddev->sync_speed_min = 0; |
| 3735 | return len; |
| 3736 | } |
| 3737 | min = simple_strtoul(buf, &e, 10); |
| 3738 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3739 | return -EINVAL; |
| 3740 | mddev->sync_speed_min = min; |
| 3741 | return len; |
| 3742 | } |
| 3743 | |
| 3744 | static struct md_sysfs_entry md_sync_min = |
| 3745 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3746 | |
| 3747 | static ssize_t |
| 3748 | sync_max_show(mddev_t *mddev, char *page) |
| 3749 | { |
| 3750 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3751 | mddev->sync_speed_max ? "local": "system"); |
| 3752 | } |
| 3753 | |
| 3754 | static ssize_t |
| 3755 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3756 | { |
| 3757 | int max; |
| 3758 | char *e; |
| 3759 | if (strncmp(buf, "system", 6)==0) { |
| 3760 | mddev->sync_speed_max = 0; |
| 3761 | return len; |
| 3762 | } |
| 3763 | max = simple_strtoul(buf, &e, 10); |
| 3764 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3765 | return -EINVAL; |
| 3766 | mddev->sync_speed_max = max; |
| 3767 | return len; |
| 3768 | } |
| 3769 | |
| 3770 | static struct md_sysfs_entry md_sync_max = |
| 3771 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3772 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3773 | static ssize_t |
| 3774 | degraded_show(mddev_t *mddev, char *page) |
| 3775 | { |
| 3776 | return sprintf(page, "%d\n", mddev->degraded); |
| 3777 | } |
| 3778 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3779 | |
| 3780 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3781 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3782 | { |
| 3783 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3784 | } |
| 3785 | |
| 3786 | static ssize_t |
| 3787 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3788 | { |
| 3789 | long n; |
| 3790 | |
| 3791 | if (strict_strtol(buf, 10, &n)) |
| 3792 | return -EINVAL; |
| 3793 | |
| 3794 | if (n != 0 && n != 1) |
| 3795 | return -EINVAL; |
| 3796 | |
| 3797 | mddev->parallel_resync = n; |
| 3798 | |
| 3799 | if (mddev->sync_thread) |
| 3800 | wake_up(&resync_wait); |
| 3801 | |
| 3802 | return len; |
| 3803 | } |
| 3804 | |
| 3805 | /* force parallel resync, even with shared block devices */ |
| 3806 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3807 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3808 | sync_force_parallel_show, sync_force_parallel_store); |
| 3809 | |
| 3810 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3811 | sync_speed_show(mddev_t *mddev, char *page) |
| 3812 | { |
| 3813 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3814 | if (mddev->curr_resync == 0) |
| 3815 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3816 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3817 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3818 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3819 | db = resync - mddev->resync_mark_cnt; |
| 3820 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3821 | } |
| 3822 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3823 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3824 | |
| 3825 | static ssize_t |
| 3826 | sync_completed_show(mddev_t *mddev, char *page) |
| 3827 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3828 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3829 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3830 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3831 | return sprintf(page, "none\n"); |
| 3832 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3833 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3834 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3835 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3836 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3837 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3838 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3839 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3840 | } |
| 3841 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3842 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3843 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3844 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3845 | min_sync_show(mddev_t *mddev, char *page) |
| 3846 | { |
| 3847 | return sprintf(page, "%llu\n", |
| 3848 | (unsigned long long)mddev->resync_min); |
| 3849 | } |
| 3850 | static ssize_t |
| 3851 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3852 | { |
| 3853 | unsigned long long min; |
| 3854 | if (strict_strtoull(buf, 10, &min)) |
| 3855 | return -EINVAL; |
| 3856 | if (min > mddev->resync_max) |
| 3857 | return -EINVAL; |
| 3858 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3859 | return -EBUSY; |
| 3860 | |
| 3861 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3862 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3863 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3864 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3865 | return -EINVAL; |
| 3866 | } |
| 3867 | mddev->resync_min = min; |
| 3868 | |
| 3869 | return len; |
| 3870 | } |
| 3871 | |
| 3872 | static struct md_sysfs_entry md_min_sync = |
| 3873 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3874 | |
| 3875 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3876 | max_sync_show(mddev_t *mddev, char *page) |
| 3877 | { |
| 3878 | if (mddev->resync_max == MaxSector) |
| 3879 | return sprintf(page, "max\n"); |
| 3880 | else |
| 3881 | return sprintf(page, "%llu\n", |
| 3882 | (unsigned long long)mddev->resync_max); |
| 3883 | } |
| 3884 | static ssize_t |
| 3885 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3886 | { |
| 3887 | if (strncmp(buf, "max", 3) == 0) |
| 3888 | mddev->resync_max = MaxSector; |
| 3889 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3890 | unsigned long long max; |
| 3891 | if (strict_strtoull(buf, 10, &max)) |
| 3892 | return -EINVAL; |
| 3893 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3894 | return -EINVAL; |
| 3895 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3896 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3897 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3898 | return -EBUSY; |
| 3899 | |
| 3900 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3901 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3902 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3903 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3904 | return -EINVAL; |
| 3905 | } |
| 3906 | mddev->resync_max = max; |
| 3907 | } |
| 3908 | wake_up(&mddev->recovery_wait); |
| 3909 | return len; |
| 3910 | } |
| 3911 | |
| 3912 | static struct md_sysfs_entry md_max_sync = |
| 3913 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3914 | |
| 3915 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3916 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3917 | { |
| 3918 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3919 | } |
| 3920 | |
| 3921 | static ssize_t |
| 3922 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3923 | { |
| 3924 | char *e; |
| 3925 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3926 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3927 | if (mddev->pers == NULL || |
| 3928 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3929 | return -EINVAL; |
| 3930 | if (buf == e || (*e && *e != '\n')) |
| 3931 | return -EINVAL; |
| 3932 | if (new >= mddev->suspend_hi || |
| 3933 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3934 | mddev->suspend_lo = new; |
| 3935 | mddev->pers->quiesce(mddev, 2); |
| 3936 | return len; |
| 3937 | } else |
| 3938 | return -EINVAL; |
| 3939 | } |
| 3940 | static struct md_sysfs_entry md_suspend_lo = |
| 3941 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3942 | |
| 3943 | |
| 3944 | static ssize_t |
| 3945 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3946 | { |
| 3947 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3948 | } |
| 3949 | |
| 3950 | static ssize_t |
| 3951 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3952 | { |
| 3953 | char *e; |
| 3954 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3955 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3956 | if (mddev->pers == NULL || |
| 3957 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3958 | return -EINVAL; |
| 3959 | if (buf == e || (*e && *e != '\n')) |
| 3960 | return -EINVAL; |
| 3961 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3962 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3963 | mddev->suspend_hi = new; |
| 3964 | mddev->pers->quiesce(mddev, 1); |
| 3965 | mddev->pers->quiesce(mddev, 0); |
| 3966 | return len; |
| 3967 | } else |
| 3968 | return -EINVAL; |
| 3969 | } |
| 3970 | static struct md_sysfs_entry md_suspend_hi = |
| 3971 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3972 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3973 | static ssize_t |
| 3974 | reshape_position_show(mddev_t *mddev, char *page) |
| 3975 | { |
| 3976 | if (mddev->reshape_position != MaxSector) |
| 3977 | return sprintf(page, "%llu\n", |
| 3978 | (unsigned long long)mddev->reshape_position); |
| 3979 | strcpy(page, "none\n"); |
| 3980 | return 5; |
| 3981 | } |
| 3982 | |
| 3983 | static ssize_t |
| 3984 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3985 | { |
| 3986 | char *e; |
| 3987 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3988 | if (mddev->pers) |
| 3989 | return -EBUSY; |
| 3990 | if (buf == e || (*e && *e != '\n')) |
| 3991 | return -EINVAL; |
| 3992 | mddev->reshape_position = new; |
| 3993 | mddev->delta_disks = 0; |
| 3994 | mddev->new_level = mddev->level; |
| 3995 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3996 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3997 | return len; |
| 3998 | } |
| 3999 | |
| 4000 | static struct md_sysfs_entry md_reshape_position = |
| 4001 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4002 | reshape_position_store); |
| 4003 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4004 | static ssize_t |
| 4005 | array_size_show(mddev_t *mddev, char *page) |
| 4006 | { |
| 4007 | if (mddev->external_size) |
| 4008 | return sprintf(page, "%llu\n", |
| 4009 | (unsigned long long)mddev->array_sectors/2); |
| 4010 | else |
| 4011 | return sprintf(page, "default\n"); |
| 4012 | } |
| 4013 | |
| 4014 | static ssize_t |
| 4015 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4016 | { |
| 4017 | sector_t sectors; |
| 4018 | |
| 4019 | if (strncmp(buf, "default", 7) == 0) { |
| 4020 | if (mddev->pers) |
| 4021 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4022 | else |
| 4023 | sectors = mddev->array_sectors; |
| 4024 | |
| 4025 | mddev->external_size = 0; |
| 4026 | } else { |
| 4027 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4028 | return -EINVAL; |
| 4029 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4030 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4031 | |
| 4032 | mddev->external_size = 1; |
| 4033 | } |
| 4034 | |
| 4035 | mddev->array_sectors = sectors; |
| 4036 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4037 | if (mddev->pers) |
| 4038 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4039 | |
| 4040 | return len; |
| 4041 | } |
| 4042 | |
| 4043 | static struct md_sysfs_entry md_array_size = |
| 4044 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4045 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4046 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4047 | static struct attribute *md_default_attrs[] = { |
| 4048 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4049 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4050 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4051 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4052 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4053 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4054 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4055 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4056 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4057 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4058 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4059 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4060 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4061 | NULL, |
| 4062 | }; |
| 4063 | |
| 4064 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4065 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4066 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4067 | &md_sync_min.attr, |
| 4068 | &md_sync_max.attr, |
| 4069 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4070 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4071 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4072 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4073 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4074 | &md_suspend_lo.attr, |
| 4075 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4076 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4077 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4078 | NULL, |
| 4079 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4080 | static struct attribute_group md_redundancy_group = { |
| 4081 | .name = NULL, |
| 4082 | .attrs = md_redundancy_attrs, |
| 4083 | }; |
| 4084 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4085 | |
| 4086 | static ssize_t |
| 4087 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4088 | { |
| 4089 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4090 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4091 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4092 | |
| 4093 | if (!entry->show) |
| 4094 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4095 | rv = mddev_lock(mddev); |
| 4096 | if (!rv) { |
| 4097 | rv = entry->show(mddev, page); |
| 4098 | mddev_unlock(mddev); |
| 4099 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4100 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | static ssize_t |
| 4104 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4105 | const char *page, size_t length) |
| 4106 | { |
| 4107 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4108 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4109 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4110 | |
| 4111 | if (!entry->store) |
| 4112 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4113 | if (!capable(CAP_SYS_ADMIN)) |
| 4114 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4115 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4116 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4117 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4118 | if (!rv) { |
| 4119 | rv = entry->store(mddev, page, length); |
| 4120 | mddev_unlock(mddev); |
| 4121 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4122 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | static void md_free(struct kobject *ko) |
| 4126 | { |
| 4127 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4128 | |
| 4129 | if (mddev->sysfs_state) |
| 4130 | sysfs_put(mddev->sysfs_state); |
| 4131 | |
| 4132 | if (mddev->gendisk) { |
| 4133 | del_gendisk(mddev->gendisk); |
| 4134 | put_disk(mddev->gendisk); |
| 4135 | } |
| 4136 | if (mddev->queue) |
| 4137 | blk_cleanup_queue(mddev->queue); |
| 4138 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4139 | kfree(mddev); |
| 4140 | } |
| 4141 | |
| 4142 | static struct sysfs_ops md_sysfs_ops = { |
| 4143 | .show = md_attr_show, |
| 4144 | .store = md_attr_store, |
| 4145 | }; |
| 4146 | static struct kobj_type md_ktype = { |
| 4147 | .release = md_free, |
| 4148 | .sysfs_ops = &md_sysfs_ops, |
| 4149 | .default_attrs = md_default_attrs, |
| 4150 | }; |
| 4151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | int mdp_major = 0; |
| 4153 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4154 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4155 | { |
| 4156 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4157 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4158 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4159 | kobject_del(&mddev->kobj); |
| 4160 | kobject_put(&mddev->kobj); |
| 4161 | } |
| 4162 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4163 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4165 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | mddev_t *mddev = mddev_find(dev); |
| 4167 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4168 | int partitioned; |
| 4169 | int shift; |
| 4170 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4171 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | |
| 4173 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4174 | return -ENODEV; |
| 4175 | |
| 4176 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4177 | shift = partitioned ? MdpMinorShift : 0; |
| 4178 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4180 | /* wait for any previous instance if this device |
| 4181 | * to be completed removed (mddev_delayed_delete). |
| 4182 | */ |
| 4183 | flush_scheduled_work(); |
| 4184 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4185 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4186 | error = -EEXIST; |
| 4187 | if (mddev->gendisk) |
| 4188 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4189 | |
| 4190 | if (name) { |
| 4191 | /* Need to ensure that 'name' is not a duplicate. |
| 4192 | */ |
| 4193 | mddev_t *mddev2; |
| 4194 | spin_lock(&all_mddevs_lock); |
| 4195 | |
| 4196 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4197 | if (mddev2->gendisk && |
| 4198 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4199 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4200 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4201 | } |
| 4202 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4204 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4205 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4206 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4207 | if (!mddev->queue) |
| 4208 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4209 | mddev->queue->queuedata = mddev; |
| 4210 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4211 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4212 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4213 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4214 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | disk = alloc_disk(1 << shift); |
| 4217 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4218 | blk_cleanup_queue(mddev->queue); |
| 4219 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4220 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4222 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4223 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4224 | if (name) |
| 4225 | strcpy(disk->disk_name, name); |
| 4226 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4228 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | disk->fops = &md_fops; |
| 4231 | disk->private_data = mddev; |
| 4232 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4233 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4234 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4235 | * remove it now. |
| 4236 | */ |
| 4237 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | add_disk(disk); |
| 4239 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4240 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4241 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4242 | if (error) { |
| 4243 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4244 | * __must_check, we must do something with the result |
| 4245 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4246 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4247 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4248 | error = 0; |
| 4249 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4250 | if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
| 4251 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4252 | abort: |
| 4253 | mutex_unlock(&disks_mutex); |
| 4254 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4255 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4256 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 4257 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4258 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4259 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4260 | } |
| 4261 | |
| 4262 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4263 | { |
| 4264 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | return NULL; |
| 4266 | } |
| 4267 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4268 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4269 | { |
| 4270 | /* val must be "md_*" where * is not all digits. |
| 4271 | * We allocate an array with a large free minor number, and |
| 4272 | * set the name to val. val must not already be an active name. |
| 4273 | */ |
| 4274 | int len = strlen(val); |
| 4275 | char buf[DISK_NAME_LEN]; |
| 4276 | |
| 4277 | while (len && val[len-1] == '\n') |
| 4278 | len--; |
| 4279 | if (len >= DISK_NAME_LEN) |
| 4280 | return -E2BIG; |
| 4281 | strlcpy(buf, val, len+1); |
| 4282 | if (strncmp(buf, "md_", 3) != 0) |
| 4283 | return -EINVAL; |
| 4284 | return md_alloc(0, buf); |
| 4285 | } |
| 4286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | static void md_safemode_timeout(unsigned long data) |
| 4288 | { |
| 4289 | mddev_t *mddev = (mddev_t *) data; |
| 4290 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4291 | if (!atomic_read(&mddev->writes_pending)) { |
| 4292 | mddev->safemode = 1; |
| 4293 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4294 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | md_wakeup_thread(mddev->thread); |
| 4297 | } |
| 4298 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4299 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4301 | static int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4303 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4305 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4307 | if (list_empty(&mddev->disks)) |
| 4308 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | |
| 4311 | if (mddev->pers) |
| 4312 | return -EBUSY; |
| 4313 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4314 | /* These two calls synchronise us with the |
| 4315 | * sysfs_remove_group calls in mddev_unlock, |
| 4316 | * so they must have completed. |
| 4317 | */ |
| 4318 | mutex_lock(&mddev->open_mutex); |
| 4319 | mutex_unlock(&mddev->open_mutex); |
| 4320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | /* |
| 4322 | * Analyze all RAID superblock(s) |
| 4323 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4324 | if (!mddev->raid_disks) { |
| 4325 | if (!mddev->persistent) |
| 4326 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4327 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4330 | if (mddev->level != LEVEL_NONE) |
| 4331 | request_module("md-level-%d", mddev->level); |
| 4332 | else if (mddev->clevel[0]) |
| 4333 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | |
| 4335 | /* |
| 4336 | * Drop all container device buffers, from now on |
| 4337 | * the only valid external interface is through the md |
| 4338 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4340 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4341 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | continue; |
| 4343 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4344 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4345 | |
| 4346 | /* perform some consistency tests on the device. |
| 4347 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4348 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4349 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4350 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4351 | if (mddev->dev_sectors && |
| 4352 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4353 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4354 | printk("md: %s: data overlaps metadata\n", |
| 4355 | mdname(mddev)); |
| 4356 | return -EINVAL; |
| 4357 | } |
| 4358 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4359 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4360 | > rdev->data_offset) { |
| 4361 | printk("md: %s: metadata overlaps data\n", |
| 4362 | mdname(mddev)); |
| 4363 | return -EINVAL; |
| 4364 | } |
| 4365 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4366 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | } |
| 4368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4370 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4371 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4373 | if (mddev->level != LEVEL_NONE) |
| 4374 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4375 | mddev->level); |
| 4376 | else |
| 4377 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4378 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | return -EINVAL; |
| 4380 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4381 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4382 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4383 | if (mddev->level != pers->level) { |
| 4384 | mddev->level = pers->level; |
| 4385 | mddev->new_level = pers->level; |
| 4386 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4387 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4389 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4390 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4391 | /* This personality cannot handle reshaping... */ |
| 4392 | mddev->pers = NULL; |
| 4393 | module_put(pers->owner); |
| 4394 | return -EINVAL; |
| 4395 | } |
| 4396 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4397 | if (pers->sync_request) { |
| 4398 | /* Warn if this is a potentially silly |
| 4399 | * configuration. |
| 4400 | */ |
| 4401 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4402 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4403 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4404 | |
| 4405 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4406 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4407 | if (rdev < rdev2 && |
| 4408 | rdev->bdev->bd_contains == |
| 4409 | rdev2->bdev->bd_contains) { |
| 4410 | printk(KERN_WARNING |
| 4411 | "%s: WARNING: %s appears to be" |
| 4412 | " on the same physical disk as" |
| 4413 | " %s.\n", |
| 4414 | mdname(mddev), |
| 4415 | bdevname(rdev->bdev,b), |
| 4416 | bdevname(rdev2->bdev,b2)); |
| 4417 | warned = 1; |
| 4418 | } |
| 4419 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4420 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4421 | if (warned) |
| 4422 | printk(KERN_WARNING |
| 4423 | "True protection against single-disk" |
| 4424 | " failure might be compromised.\n"); |
| 4425 | } |
| 4426 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4427 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4428 | /* may be over-ridden by personality */ |
| 4429 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4430 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4431 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4432 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4434 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4435 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4436 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4437 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4438 | if (err) |
| 4439 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4440 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4441 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4442 | " but 'external_size' not in effect?\n", __func__); |
| 4443 | printk(KERN_ERR |
| 4444 | "md: invalid array_size %llu > default size %llu\n", |
| 4445 | (unsigned long long)mddev->array_sectors / 2, |
| 4446 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4447 | err = -EINVAL; |
| 4448 | mddev->pers->stop(mddev); |
| 4449 | } |
| 4450 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4451 | err = bitmap_create(mddev); |
| 4452 | if (err) { |
| 4453 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4454 | mdname(mddev), err); |
| 4455 | mddev->pers->stop(mddev); |
| 4456 | } |
| 4457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4459 | module_put(mddev->pers->owner); |
| 4460 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4461 | bitmap_destroy(mddev); |
| 4462 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4464 | if (mddev->pers->sync_request) { |
| 4465 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4466 | printk(KERN_WARNING |
| 4467 | "md: cannot register extra attributes for %s\n", |
| 4468 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4469 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4470 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4471 | mddev->ro = 0; |
| 4472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4473 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4474 | atomic_set(&mddev->max_corr_read_errors, |
| 4475 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4476 | mddev->safemode = 0; |
| 4477 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4478 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4479 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4481 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4482 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4483 | if (rdev->raid_disk >= 0) { |
| 4484 | char nm[20]; |
| 4485 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4486 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4487 | printk("md: cannot register %s for %s\n", |
| 4488 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4489 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4490 | |
| 4491 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4492 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4493 | if (mddev->flags) |
| 4494 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4496 | md_wakeup_thread(mddev->thread); |
| 4497 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4498 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4499 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4500 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4501 | if (mddev->sysfs_action) |
| 4502 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4503 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | return 0; |
| 4505 | } |
| 4506 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4507 | static int do_md_run(mddev_t *mddev) |
| 4508 | { |
| 4509 | int err; |
| 4510 | |
| 4511 | err = md_run(mddev); |
| 4512 | if (err) |
| 4513 | goto out; |
| 4514 | |
| 4515 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4516 | revalidate_disk(mddev->gendisk); |
| 4517 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4518 | out: |
| 4519 | return err; |
| 4520 | } |
| 4521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | static int restart_array(mddev_t *mddev) |
| 4523 | { |
| 4524 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4526 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4528 | return -ENXIO; |
| 4529 | if (!mddev->pers) |
| 4530 | return -EINVAL; |
| 4531 | if (!mddev->ro) |
| 4532 | return -EBUSY; |
| 4533 | mddev->safemode = 0; |
| 4534 | mddev->ro = 0; |
| 4535 | set_disk_ro(disk, 0); |
| 4536 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4537 | mdname(mddev)); |
| 4538 | /* Kick recovery or resync if necessary */ |
| 4539 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4540 | md_wakeup_thread(mddev->thread); |
| 4541 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4542 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4543 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | } |
| 4545 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4546 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4547 | * to the file ourselves */ |
| 4548 | static int deny_bitmap_write_access(struct file * file) |
| 4549 | { |
| 4550 | struct inode *inode = file->f_mapping->host; |
| 4551 | |
| 4552 | spin_lock(&inode->i_lock); |
| 4553 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4554 | spin_unlock(&inode->i_lock); |
| 4555 | return -ETXTBSY; |
| 4556 | } |
| 4557 | atomic_set(&inode->i_writecount, -1); |
| 4558 | spin_unlock(&inode->i_lock); |
| 4559 | |
| 4560 | return 0; |
| 4561 | } |
| 4562 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4563 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4564 | { |
| 4565 | struct inode *inode = file->f_mapping->host; |
| 4566 | |
| 4567 | spin_lock(&inode->i_lock); |
| 4568 | atomic_set(&inode->i_writecount, 1); |
| 4569 | spin_unlock(&inode->i_lock); |
| 4570 | } |
| 4571 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4572 | static void md_clean(mddev_t *mddev) |
| 4573 | { |
| 4574 | mddev->array_sectors = 0; |
| 4575 | mddev->external_size = 0; |
| 4576 | mddev->dev_sectors = 0; |
| 4577 | mddev->raid_disks = 0; |
| 4578 | mddev->recovery_cp = 0; |
| 4579 | mddev->resync_min = 0; |
| 4580 | mddev->resync_max = MaxSector; |
| 4581 | mddev->reshape_position = MaxSector; |
| 4582 | mddev->external = 0; |
| 4583 | mddev->persistent = 0; |
| 4584 | mddev->level = LEVEL_NONE; |
| 4585 | mddev->clevel[0] = 0; |
| 4586 | mddev->flags = 0; |
| 4587 | mddev->ro = 0; |
| 4588 | mddev->metadata_type[0] = 0; |
| 4589 | mddev->chunk_sectors = 0; |
| 4590 | mddev->ctime = mddev->utime = 0; |
| 4591 | mddev->layout = 0; |
| 4592 | mddev->max_disks = 0; |
| 4593 | mddev->events = 0; |
| 4594 | mddev->delta_disks = 0; |
| 4595 | mddev->new_level = LEVEL_NONE; |
| 4596 | mddev->new_layout = 0; |
| 4597 | mddev->new_chunk_sectors = 0; |
| 4598 | mddev->curr_resync = 0; |
| 4599 | mddev->resync_mismatches = 0; |
| 4600 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4601 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4602 | mddev->recovery = 0; |
| 4603 | mddev->in_sync = 0; |
| 4604 | mddev->degraded = 0; |
| 4605 | mddev->barriers_work = 0; |
| 4606 | mddev->safemode = 0; |
| 4607 | mddev->bitmap_info.offset = 0; |
| 4608 | mddev->bitmap_info.default_offset = 0; |
| 4609 | mddev->bitmap_info.chunksize = 0; |
| 4610 | mddev->bitmap_info.daemon_sleep = 0; |
| 4611 | mddev->bitmap_info.max_write_behind = 0; |
| 4612 | } |
| 4613 | |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4614 | static void md_stop_writes(mddev_t *mddev) |
| 4615 | { |
| 4616 | if (mddev->sync_thread) { |
| 4617 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4618 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4619 | md_unregister_thread(mddev->sync_thread); |
| 4620 | mddev->sync_thread = NULL; |
| 4621 | } |
| 4622 | |
| 4623 | del_timer_sync(&mddev->safemode_timer); |
| 4624 | |
| 4625 | bitmap_flush(mddev); |
| 4626 | md_super_wait(mddev); |
| 4627 | |
| 4628 | if (!mddev->in_sync || mddev->flags) { |
| 4629 | /* mark array as shutdown cleanly */ |
| 4630 | mddev->in_sync = 1; |
| 4631 | md_update_sb(mddev, 1); |
| 4632 | } |
| 4633 | } |
| 4634 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4635 | static void md_stop(mddev_t *mddev) |
| 4636 | { |
| 4637 | mddev->pers->stop(mddev); |
| 4638 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4639 | mddev->to_remove = &md_redundancy_group; |
| 4640 | module_put(mddev->pers->owner); |
| 4641 | mddev->pers = NULL; |
| 4642 | |
| 4643 | } |
| 4644 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 4645 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4646 | { |
| 4647 | int err = 0; |
| 4648 | mutex_lock(&mddev->open_mutex); |
| 4649 | if (atomic_read(&mddev->openers) > is_open) { |
| 4650 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4651 | err = -EBUSY; |
| 4652 | goto out; |
| 4653 | } |
| 4654 | if (mddev->pers) { |
| 4655 | md_stop_writes(mddev); |
| 4656 | |
| 4657 | err = -ENXIO; |
| 4658 | if (mddev->ro==1) |
| 4659 | goto out; |
| 4660 | mddev->ro = 1; |
| 4661 | set_disk_ro(mddev->gendisk, 1); |
| 4662 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4663 | sysfs_notify_dirent(mddev->sysfs_state); |
| 4664 | err = 0; |
| 4665 | } |
| 4666 | out: |
| 4667 | mutex_unlock(&mddev->open_mutex); |
| 4668 | return err; |
| 4669 | } |
| 4670 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4671 | /* mode: |
| 4672 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4673 | * 2 - stop but do not disassemble array |
| 4674 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4675 | 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] | 4676 | { |
| 4677 | int err = 0; |
| 4678 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4679 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4681 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4682 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4683 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4684 | err = -EBUSY; |
| 4685 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4687 | md_stop_writes(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4688 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 4689 | if (mddev->ro) |
| 4690 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4691 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 4692 | md_stop(mddev); |
| 4693 | mddev->queue->merge_bvec_fn = NULL; |
| 4694 | mddev->queue->unplug_fn = NULL; |
| 4695 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4696 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 4697 | /* tell userspace to handle 'inactive' */ |
| 4698 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4699 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 4700 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4701 | if (rdev->raid_disk >= 0) { |
| 4702 | char nm[20]; |
| 4703 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4704 | sysfs_remove_link(&mddev->kobj, nm); |
| 4705 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4706 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 4707 | set_capacity(disk, 0); |
| 4708 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4709 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 4710 | if (mddev->ro) |
| 4711 | mddev->ro = 0; |
| 4712 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4713 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4714 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4715 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4716 | mutex_unlock(&mddev->open_mutex); |
| 4717 | if (err) |
| 4718 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | /* |
| 4720 | * Free resources if final stop |
| 4721 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4722 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4725 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4726 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4727 | if (mddev->bitmap_info.file) { |
| 4728 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4729 | fput(mddev->bitmap_info.file); |
| 4730 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4731 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4732 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | export_array(mddev); |
| 4735 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4736 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4737 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4738 | if (mddev->hold_active == UNTIL_STOP) |
| 4739 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4740 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 4741 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4743 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4744 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4745 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | return err; |
| 4747 | } |
| 4748 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4749 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | static void autorun_array(mddev_t *mddev) |
| 4751 | { |
| 4752 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | int err; |
| 4754 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4755 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | |
| 4758 | printk(KERN_INFO "md: running: "); |
| 4759 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4760 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | char b[BDEVNAME_SIZE]; |
| 4762 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4763 | } |
| 4764 | printk("\n"); |
| 4765 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4766 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | if (err) { |
| 4768 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4769 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | /* |
| 4774 | * lets try to run arrays based on all disks that have arrived |
| 4775 | * until now. (those are in pending_raid_disks) |
| 4776 | * |
| 4777 | * the method: pick the first pending disk, collect all disks with |
| 4778 | * the same UUID, remove all from the pending list and put them into |
| 4779 | * the 'same_array' list. Then order this list based on superblock |
| 4780 | * update time (freshest comes first), kick out 'old' disks and |
| 4781 | * compare superblocks. If everything's fine then run it. |
| 4782 | * |
| 4783 | * If "unit" is allocated, then bump its reference count |
| 4784 | */ |
| 4785 | static void autorun_devices(int part) |
| 4786 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4787 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4788 | mddev_t *mddev; |
| 4789 | char b[BDEVNAME_SIZE]; |
| 4790 | |
| 4791 | printk(KERN_INFO "md: autorun ...\n"); |
| 4792 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4793 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4795 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | rdev0 = list_entry(pending_raid_disks.next, |
| 4797 | mdk_rdev_t, same_set); |
| 4798 | |
| 4799 | printk(KERN_INFO "md: considering %s ...\n", |
| 4800 | bdevname(rdev0->bdev,b)); |
| 4801 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4802 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4804 | printk(KERN_INFO "md: adding %s ...\n", |
| 4805 | bdevname(rdev->bdev,b)); |
| 4806 | list_move(&rdev->same_set, &candidates); |
| 4807 | } |
| 4808 | /* |
| 4809 | * now we have a set of devices, with all of them having |
| 4810 | * mostly sane superblocks. It's time to allocate the |
| 4811 | * mddev. |
| 4812 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4813 | if (part) { |
| 4814 | dev = MKDEV(mdp_major, |
| 4815 | rdev0->preferred_minor << MdpMinorShift); |
| 4816 | unit = MINOR(dev) >> MdpMinorShift; |
| 4817 | } else { |
| 4818 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4819 | unit = MINOR(dev); |
| 4820 | } |
| 4821 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4823 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4824 | break; |
| 4825 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | |
| 4827 | md_probe(dev, NULL, NULL); |
| 4828 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4829 | if (!mddev || !mddev->gendisk) { |
| 4830 | if (mddev) |
| 4831 | mddev_put(mddev); |
| 4832 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4833 | "md: cannot allocate memory for md drive.\n"); |
| 4834 | break; |
| 4835 | } |
| 4836 | if (mddev_lock(mddev)) |
| 4837 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4838 | mdname(mddev)); |
| 4839 | else if (mddev->raid_disks || mddev->major_version |
| 4840 | || !list_empty(&mddev->disks)) { |
| 4841 | printk(KERN_WARNING |
| 4842 | "md: %s already running, cannot run %s\n", |
| 4843 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4844 | mddev_unlock(mddev); |
| 4845 | } else { |
| 4846 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4847 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4848 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4849 | list_del_init(&rdev->same_set); |
| 4850 | if (bind_rdev_to_array(rdev, mddev)) |
| 4851 | export_rdev(rdev); |
| 4852 | } |
| 4853 | autorun_array(mddev); |
| 4854 | mddev_unlock(mddev); |
| 4855 | } |
| 4856 | /* on success, candidates will be empty, on error |
| 4857 | * it won't... |
| 4858 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4859 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4860 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4862 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4863 | mddev_put(mddev); |
| 4864 | } |
| 4865 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4866 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4867 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4868 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4869 | static int get_version(void __user * arg) |
| 4870 | { |
| 4871 | mdu_version_t ver; |
| 4872 | |
| 4873 | ver.major = MD_MAJOR_VERSION; |
| 4874 | ver.minor = MD_MINOR_VERSION; |
| 4875 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4876 | |
| 4877 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4878 | return -EFAULT; |
| 4879 | |
| 4880 | return 0; |
| 4881 | } |
| 4882 | |
| 4883 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4884 | { |
| 4885 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4886 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4888 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4889 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4890 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4892 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4893 | failed++; |
| 4894 | else { |
| 4895 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4896 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4897 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4898 | else |
| 4899 | spare++; |
| 4900 | } |
| 4901 | } |
| 4902 | |
| 4903 | info.major_version = mddev->major_version; |
| 4904 | info.minor_version = mddev->minor_version; |
| 4905 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4906 | info.ctime = mddev->ctime; |
| 4907 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4908 | info.size = mddev->dev_sectors / 2; |
| 4909 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4910 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4911 | info.nr_disks = nr; |
| 4912 | info.raid_disks = mddev->raid_disks; |
| 4913 | info.md_minor = mddev->md_minor; |
| 4914 | info.not_persistent= !mddev->persistent; |
| 4915 | |
| 4916 | info.utime = mddev->utime; |
| 4917 | info.state = 0; |
| 4918 | if (mddev->in_sync) |
| 4919 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4920 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4921 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4922 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4923 | info.working_disks = working; |
| 4924 | info.failed_disks = failed; |
| 4925 | info.spare_disks = spare; |
| 4926 | |
| 4927 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4928 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4929 | |
| 4930 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4931 | return -EFAULT; |
| 4932 | |
| 4933 | return 0; |
| 4934 | } |
| 4935 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4936 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4937 | { |
| 4938 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4939 | char *ptr, *buf = NULL; |
| 4940 | int err = -ENOMEM; |
| 4941 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4942 | if (md_allow_write(mddev)) |
| 4943 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4944 | else |
| 4945 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4946 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4947 | if (!file) |
| 4948 | goto out; |
| 4949 | |
| 4950 | /* bitmap disabled, zero the first byte and copy out */ |
| 4951 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4952 | file->pathname[0] = '\0'; |
| 4953 | goto copy_out; |
| 4954 | } |
| 4955 | |
| 4956 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4957 | if (!buf) |
| 4958 | goto out; |
| 4959 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4960 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4961 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4962 | goto out; |
| 4963 | |
| 4964 | strcpy(file->pathname, ptr); |
| 4965 | |
| 4966 | copy_out: |
| 4967 | err = 0; |
| 4968 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4969 | err = -EFAULT; |
| 4970 | out: |
| 4971 | kfree(buf); |
| 4972 | kfree(file); |
| 4973 | return err; |
| 4974 | } |
| 4975 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4977 | { |
| 4978 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | mdk_rdev_t *rdev; |
| 4980 | |
| 4981 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4982 | return -EFAULT; |
| 4983 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4984 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4985 | if (rdev) { |
| 4986 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4987 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4988 | info.raid_disk = rdev->raid_disk; |
| 4989 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4990 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4991 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4992 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4993 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4994 | info.state |= (1<<MD_DISK_SYNC); |
| 4995 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4996 | if (test_bit(WriteMostly, &rdev->flags)) |
| 4997 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | } else { |
| 4999 | info.major = info.minor = 0; |
| 5000 | info.raid_disk = -1; |
| 5001 | info.state = (1<<MD_DISK_REMOVED); |
| 5002 | } |
| 5003 | |
| 5004 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5005 | return -EFAULT; |
| 5006 | |
| 5007 | return 0; |
| 5008 | } |
| 5009 | |
| 5010 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5011 | { |
| 5012 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5013 | mdk_rdev_t *rdev; |
| 5014 | dev_t dev = MKDEV(info->major,info->minor); |
| 5015 | |
| 5016 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5017 | return -EOVERFLOW; |
| 5018 | |
| 5019 | if (!mddev->raid_disks) { |
| 5020 | int err; |
| 5021 | /* expecting a device which has a superblock */ |
| 5022 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5023 | if (IS_ERR(rdev)) { |
| 5024 | printk(KERN_WARNING |
| 5025 | "md: md_import_device returned %ld\n", |
| 5026 | PTR_ERR(rdev)); |
| 5027 | return PTR_ERR(rdev); |
| 5028 | } |
| 5029 | if (!list_empty(&mddev->disks)) { |
| 5030 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5031 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5032 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5033 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5034 | if (err < 0) { |
| 5035 | printk(KERN_WARNING |
| 5036 | "md: %s has different UUID to %s\n", |
| 5037 | bdevname(rdev->bdev,b), |
| 5038 | bdevname(rdev0->bdev,b2)); |
| 5039 | export_rdev(rdev); |
| 5040 | return -EINVAL; |
| 5041 | } |
| 5042 | } |
| 5043 | err = bind_rdev_to_array(rdev, mddev); |
| 5044 | if (err) |
| 5045 | export_rdev(rdev); |
| 5046 | return err; |
| 5047 | } |
| 5048 | |
| 5049 | /* |
| 5050 | * add_new_disk can be used once the array is assembled |
| 5051 | * to add "hot spares". They must already have a superblock |
| 5052 | * written |
| 5053 | */ |
| 5054 | if (mddev->pers) { |
| 5055 | int err; |
| 5056 | if (!mddev->pers->hot_add_disk) { |
| 5057 | printk(KERN_WARNING |
| 5058 | "%s: personality does not support diskops!\n", |
| 5059 | mdname(mddev)); |
| 5060 | return -EINVAL; |
| 5061 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5062 | if (mddev->persistent) |
| 5063 | rdev = md_import_device(dev, mddev->major_version, |
| 5064 | mddev->minor_version); |
| 5065 | else |
| 5066 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | if (IS_ERR(rdev)) { |
| 5068 | printk(KERN_WARNING |
| 5069 | "md: md_import_device returned %ld\n", |
| 5070 | PTR_ERR(rdev)); |
| 5071 | return PTR_ERR(rdev); |
| 5072 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5073 | /* set save_raid_disk if appropriate */ |
| 5074 | if (!mddev->persistent) { |
| 5075 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5076 | info->raid_disk < mddev->raid_disks) |
| 5077 | rdev->raid_disk = info->raid_disk; |
| 5078 | else |
| 5079 | rdev->raid_disk = -1; |
| 5080 | } else |
| 5081 | super_types[mddev->major_version]. |
| 5082 | validate_super(mddev, rdev); |
| 5083 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5084 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5085 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5086 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5087 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5088 | else |
| 5089 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | rdev->raid_disk = -1; |
| 5092 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5093 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5094 | /* If there is hot_add_disk but no hot_remove_disk |
| 5095 | * then added disks for geometry changes, |
| 5096 | * and should be added immediately. |
| 5097 | */ |
| 5098 | super_types[mddev->major_version]. |
| 5099 | validate_super(mddev, rdev); |
| 5100 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5101 | if (err) |
| 5102 | unbind_rdev_from_array(rdev); |
| 5103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | if (err) |
| 5105 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5106 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 5107 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5108 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5109 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5110 | if (mddev->degraded) |
| 5111 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5112 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5113 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5114 | return err; |
| 5115 | } |
| 5116 | |
| 5117 | /* otherwise, add_new_disk is only allowed |
| 5118 | * for major_version==0 superblocks |
| 5119 | */ |
| 5120 | if (mddev->major_version != 0) { |
| 5121 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5122 | mdname(mddev)); |
| 5123 | return -EINVAL; |
| 5124 | } |
| 5125 | |
| 5126 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5127 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5128 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5129 | if (IS_ERR(rdev)) { |
| 5130 | printk(KERN_WARNING |
| 5131 | "md: error, md_import_device() returned %ld\n", |
| 5132 | PTR_ERR(rdev)); |
| 5133 | return PTR_ERR(rdev); |
| 5134 | } |
| 5135 | rdev->desc_nr = info->number; |
| 5136 | if (info->raid_disk < mddev->raid_disks) |
| 5137 | rdev->raid_disk = info->raid_disk; |
| 5138 | else |
| 5139 | rdev->raid_disk = -1; |
| 5140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5142 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5143 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5145 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5146 | set_bit(WriteMostly, &rdev->flags); |
| 5147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | if (!mddev->persistent) { |
| 5149 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5150 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5152 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5153 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5155 | err = bind_rdev_to_array(rdev, mddev); |
| 5156 | if (err) { |
| 5157 | export_rdev(rdev); |
| 5158 | return err; |
| 5159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | } |
| 5161 | |
| 5162 | return 0; |
| 5163 | } |
| 5164 | |
| 5165 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5166 | { |
| 5167 | char b[BDEVNAME_SIZE]; |
| 5168 | mdk_rdev_t *rdev; |
| 5169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | rdev = find_rdev(mddev, dev); |
| 5171 | if (!rdev) |
| 5172 | return -ENXIO; |
| 5173 | |
| 5174 | if (rdev->raid_disk >= 0) |
| 5175 | goto busy; |
| 5176 | |
| 5177 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5178 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5179 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | |
| 5181 | return 0; |
| 5182 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5183 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5185 | return -EBUSY; |
| 5186 | } |
| 5187 | |
| 5188 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5189 | { |
| 5190 | char b[BDEVNAME_SIZE]; |
| 5191 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | mdk_rdev_t *rdev; |
| 5193 | |
| 5194 | if (!mddev->pers) |
| 5195 | return -ENODEV; |
| 5196 | |
| 5197 | if (mddev->major_version != 0) { |
| 5198 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5199 | " version-0 superblocks.\n", |
| 5200 | mdname(mddev)); |
| 5201 | return -EINVAL; |
| 5202 | } |
| 5203 | if (!mddev->pers->hot_add_disk) { |
| 5204 | printk(KERN_WARNING |
| 5205 | "%s: personality does not support diskops!\n", |
| 5206 | mdname(mddev)); |
| 5207 | return -EINVAL; |
| 5208 | } |
| 5209 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5210 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | if (IS_ERR(rdev)) { |
| 5212 | printk(KERN_WARNING |
| 5213 | "md: error, md_import_device() returned %ld\n", |
| 5214 | PTR_ERR(rdev)); |
| 5215 | return -EINVAL; |
| 5216 | } |
| 5217 | |
| 5218 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5219 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5220 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5221 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5223 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5225 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | printk(KERN_WARNING |
| 5227 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5228 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5229 | err = -EINVAL; |
| 5230 | goto abort_export; |
| 5231 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5232 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5234 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5235 | err = bind_rdev_to_array(rdev, mddev); |
| 5236 | if (err) |
| 5237 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | |
| 5239 | /* |
| 5240 | * The rest should better be atomic, we can have disk failures |
| 5241 | * noticed in interrupt contexts ... |
| 5242 | */ |
| 5243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | rdev->raid_disk = -1; |
| 5245 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5246 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | |
| 5248 | /* |
| 5249 | * Kick recovery, maybe this spare has to be added to the |
| 5250 | * array immediately. |
| 5251 | */ |
| 5252 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5253 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5254 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | return 0; |
| 5256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | abort_export: |
| 5258 | export_rdev(rdev); |
| 5259 | return err; |
| 5260 | } |
| 5261 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5262 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5263 | { |
| 5264 | int err; |
| 5265 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5266 | if (mddev->pers) { |
| 5267 | if (!mddev->pers->quiesce) |
| 5268 | return -EBUSY; |
| 5269 | if (mddev->recovery || mddev->sync_thread) |
| 5270 | return -EBUSY; |
| 5271 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5272 | } |
| 5273 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5274 | |
| 5275 | if (fd >= 0) { |
| 5276 | if (mddev->bitmap) |
| 5277 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5278 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5279 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5280 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5281 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5282 | mdname(mddev)); |
| 5283 | return -EBADF; |
| 5284 | } |
| 5285 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5286 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5287 | if (err) { |
| 5288 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5289 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5290 | fput(mddev->bitmap_info.file); |
| 5291 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5292 | return err; |
| 5293 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5294 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5295 | } else if (mddev->bitmap == NULL) |
| 5296 | return -ENOENT; /* cannot remove what isn't there */ |
| 5297 | err = 0; |
| 5298 | if (mddev->pers) { |
| 5299 | mddev->pers->quiesce(mddev, 1); |
| 5300 | if (fd >= 0) |
| 5301 | err = bitmap_create(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5302 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5303 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5304 | fd = -1; /* make sure to put the file */ |
| 5305 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5306 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5307 | } |
| 5308 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5309 | if (mddev->bitmap_info.file) { |
| 5310 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5311 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5312 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5313 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5314 | } |
| 5315 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5316 | return err; |
| 5317 | } |
| 5318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | /* |
| 5320 | * set_array_info is used two different ways |
| 5321 | * The original usage is when creating a new array. |
| 5322 | * In this usage, raid_disks is > 0 and it together with |
| 5323 | * level, size, not_persistent,layout,chunksize determine the |
| 5324 | * shape of the array. |
| 5325 | * This will always create an array with a type-0.90.0 superblock. |
| 5326 | * The newer usage is when assembling an array. |
| 5327 | * In this case raid_disks will be 0, and the major_version field is |
| 5328 | * use to determine which style super-blocks are to be found on the devices. |
| 5329 | * The minor and patch _version numbers are also kept incase the |
| 5330 | * super_block handler wishes to interpret them. |
| 5331 | */ |
| 5332 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5333 | { |
| 5334 | |
| 5335 | if (info->raid_disks == 0) { |
| 5336 | /* just setting version number for superblock loading */ |
| 5337 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5338 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | super_types[info->major_version].name == NULL) { |
| 5340 | /* maybe try to auto-load a module? */ |
| 5341 | printk(KERN_INFO |
| 5342 | "md: superblock version %d not known\n", |
| 5343 | info->major_version); |
| 5344 | return -EINVAL; |
| 5345 | } |
| 5346 | mddev->major_version = info->major_version; |
| 5347 | mddev->minor_version = info->minor_version; |
| 5348 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5349 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5350 | /* ensure mddev_put doesn't delete this now that there |
| 5351 | * is some minimal configuration. |
| 5352 | */ |
| 5353 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | return 0; |
| 5355 | } |
| 5356 | mddev->major_version = MD_MAJOR_VERSION; |
| 5357 | mddev->minor_version = MD_MINOR_VERSION; |
| 5358 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5359 | mddev->ctime = get_seconds(); |
| 5360 | |
| 5361 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5362 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5363 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5364 | mddev->raid_disks = info->raid_disks; |
| 5365 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5366 | * openned |
| 5367 | */ |
| 5368 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5369 | mddev->recovery_cp = MaxSector; |
| 5370 | else |
| 5371 | mddev->recovery_cp = 0; |
| 5372 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5373 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | |
| 5375 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5376 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | |
| 5378 | mddev->max_disks = MD_SB_DISKS; |
| 5379 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5380 | if (mddev->persistent) |
| 5381 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5382 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5383 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5384 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5385 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5386 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5387 | mddev->reshape_position = MaxSector; |
| 5388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5389 | /* |
| 5390 | * Generate a 128 bit UUID |
| 5391 | */ |
| 5392 | get_random_bytes(mddev->uuid, 16); |
| 5393 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5394 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5395 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5396 | mddev->new_layout = mddev->layout; |
| 5397 | mddev->delta_disks = 0; |
| 5398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | return 0; |
| 5400 | } |
| 5401 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5402 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5403 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5404 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5405 | |
| 5406 | if (mddev->external_size) |
| 5407 | return; |
| 5408 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5409 | mddev->array_sectors = array_sectors; |
| 5410 | } |
| 5411 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5412 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5413 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5414 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5415 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5416 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5417 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5418 | |
| 5419 | if (mddev->pers->resize == NULL) |
| 5420 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5421 | /* The "num_sectors" is the number of sectors of each device that |
| 5422 | * is used. This can only make sense for arrays with redundancy. |
| 5423 | * linear and raid0 always use whatever space is available. We can only |
| 5424 | * consider changing this number if no resync or reconstruction is |
| 5425 | * 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] | 5426 | * 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] | 5427 | * of each device. If num_sectors is zero, we find the largest size |
| 5428 | * that fits. |
| 5429 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5430 | */ |
| 5431 | if (mddev->sync_thread) |
| 5432 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5433 | if (mddev->bitmap) |
| 5434 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5435 | * grow, and re-add. |
| 5436 | */ |
| 5437 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5438 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5439 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5440 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5441 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5442 | num_sectors = avail; |
| 5443 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5444 | return -ENOSPC; |
| 5445 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5446 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5447 | if (!rv) |
| 5448 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5449 | return rv; |
| 5450 | } |
| 5451 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5452 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5453 | { |
| 5454 | int rv; |
| 5455 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5456 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5457 | return -EINVAL; |
| 5458 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5459 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5460 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5461 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5462 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5463 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5464 | |
| 5465 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5466 | return rv; |
| 5467 | } |
| 5468 | |
| 5469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | /* |
| 5471 | * update_array_info is used to change the configuration of an |
| 5472 | * on-line array. |
| 5473 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5474 | * fields in the info are checked against the array. |
| 5475 | * Any differences that cannot be handled will cause an error. |
| 5476 | * Normally, only one change can be managed at a time. |
| 5477 | */ |
| 5478 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5479 | { |
| 5480 | int rv = 0; |
| 5481 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5482 | int state = 0; |
| 5483 | |
| 5484 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5485 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5486 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | |
| 5488 | if (mddev->major_version != info->major_version || |
| 5489 | mddev->minor_version != info->minor_version || |
| 5490 | /* mddev->patch_version != info->patch_version || */ |
| 5491 | mddev->ctime != info->ctime || |
| 5492 | mddev->level != info->level || |
| 5493 | /* mddev->layout != info->layout || */ |
| 5494 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5495 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5496 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5497 | ((state^info->state) & 0xfffffe00) |
| 5498 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | return -EINVAL; |
| 5500 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5501 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5502 | cnt++; |
| 5503 | if (mddev->raid_disks != info->raid_disks) |
| 5504 | cnt++; |
| 5505 | if (mddev->layout != info->layout) |
| 5506 | cnt++; |
| 5507 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5508 | cnt++; |
| 5509 | if (cnt == 0) |
| 5510 | return 0; |
| 5511 | if (cnt > 1) |
| 5512 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5513 | |
| 5514 | if (mddev->layout != info->layout) { |
| 5515 | /* Change layout |
| 5516 | * we don't need to do anything at the md level, the |
| 5517 | * personality will take care of it all. |
| 5518 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5519 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5521 | else { |
| 5522 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5523 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5524 | if (rv) |
| 5525 | mddev->new_layout = mddev->layout; |
| 5526 | return rv; |
| 5527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5529 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5530 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5531 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5532 | if (mddev->raid_disks != info->raid_disks) |
| 5533 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5535 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5536 | if (mddev->pers->quiesce == NULL) |
| 5537 | return -EINVAL; |
| 5538 | if (mddev->recovery || mddev->sync_thread) |
| 5539 | return -EBUSY; |
| 5540 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5541 | /* add the bitmap */ |
| 5542 | if (mddev->bitmap) |
| 5543 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5544 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5545 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5546 | mddev->bitmap_info.offset = |
| 5547 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5548 | mddev->pers->quiesce(mddev, 1); |
| 5549 | rv = bitmap_create(mddev); |
| 5550 | if (rv) |
| 5551 | bitmap_destroy(mddev); |
| 5552 | mddev->pers->quiesce(mddev, 0); |
| 5553 | } else { |
| 5554 | /* remove the bitmap */ |
| 5555 | if (!mddev->bitmap) |
| 5556 | return -ENOENT; |
| 5557 | if (mddev->bitmap->file) |
| 5558 | return -EINVAL; |
| 5559 | mddev->pers->quiesce(mddev, 1); |
| 5560 | bitmap_destroy(mddev); |
| 5561 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5562 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5563 | } |
| 5564 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5565 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5566 | return rv; |
| 5567 | } |
| 5568 | |
| 5569 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5570 | { |
| 5571 | mdk_rdev_t *rdev; |
| 5572 | |
| 5573 | if (mddev->pers == NULL) |
| 5574 | return -ENODEV; |
| 5575 | |
| 5576 | rdev = find_rdev(mddev, dev); |
| 5577 | if (!rdev) |
| 5578 | return -ENODEV; |
| 5579 | |
| 5580 | md_error(mddev, rdev); |
| 5581 | return 0; |
| 5582 | } |
| 5583 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5584 | /* |
| 5585 | * We have a problem here : there is no easy way to give a CHS |
| 5586 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5587 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5588 | * dosfs just mad... ;-) |
| 5589 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5590 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5591 | { |
| 5592 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5593 | |
| 5594 | geo->heads = 2; |
| 5595 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5596 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5597 | return 0; |
| 5598 | } |
| 5599 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5600 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | unsigned int cmd, unsigned long arg) |
| 5602 | { |
| 5603 | int err = 0; |
| 5604 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5606 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | |
| 5608 | if (!capable(CAP_SYS_ADMIN)) |
| 5609 | return -EACCES; |
| 5610 | |
| 5611 | /* |
| 5612 | * Commands dealing with the RAID driver but not any |
| 5613 | * particular array: |
| 5614 | */ |
| 5615 | switch (cmd) |
| 5616 | { |
| 5617 | case RAID_VERSION: |
| 5618 | err = get_version(argp); |
| 5619 | goto done; |
| 5620 | |
| 5621 | case PRINT_RAID_DEBUG: |
| 5622 | err = 0; |
| 5623 | md_print_devices(); |
| 5624 | goto done; |
| 5625 | |
| 5626 | #ifndef MODULE |
| 5627 | case RAID_AUTORUN: |
| 5628 | err = 0; |
| 5629 | autostart_arrays(arg); |
| 5630 | goto done; |
| 5631 | #endif |
| 5632 | default:; |
| 5633 | } |
| 5634 | |
| 5635 | /* |
| 5636 | * Commands creating/starting a new array: |
| 5637 | */ |
| 5638 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5639 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | |
| 5641 | if (!mddev) { |
| 5642 | BUG(); |
| 5643 | goto abort; |
| 5644 | } |
| 5645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | err = mddev_lock(mddev); |
| 5647 | if (err) { |
| 5648 | printk(KERN_INFO |
| 5649 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5650 | err, cmd); |
| 5651 | goto abort; |
| 5652 | } |
| 5653 | |
| 5654 | switch (cmd) |
| 5655 | { |
| 5656 | case SET_ARRAY_INFO: |
| 5657 | { |
| 5658 | mdu_array_info_t info; |
| 5659 | if (!arg) |
| 5660 | memset(&info, 0, sizeof(info)); |
| 5661 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5662 | err = -EFAULT; |
| 5663 | goto abort_unlock; |
| 5664 | } |
| 5665 | if (mddev->pers) { |
| 5666 | err = update_array_info(mddev, &info); |
| 5667 | if (err) { |
| 5668 | printk(KERN_WARNING "md: couldn't update" |
| 5669 | " array info. %d\n", err); |
| 5670 | goto abort_unlock; |
| 5671 | } |
| 5672 | goto done_unlock; |
| 5673 | } |
| 5674 | if (!list_empty(&mddev->disks)) { |
| 5675 | printk(KERN_WARNING |
| 5676 | "md: array %s already has disks!\n", |
| 5677 | mdname(mddev)); |
| 5678 | err = -EBUSY; |
| 5679 | goto abort_unlock; |
| 5680 | } |
| 5681 | if (mddev->raid_disks) { |
| 5682 | printk(KERN_WARNING |
| 5683 | "md: array %s already initialised!\n", |
| 5684 | mdname(mddev)); |
| 5685 | err = -EBUSY; |
| 5686 | goto abort_unlock; |
| 5687 | } |
| 5688 | err = set_array_info(mddev, &info); |
| 5689 | if (err) { |
| 5690 | printk(KERN_WARNING "md: couldn't set" |
| 5691 | " array info. %d\n", err); |
| 5692 | goto abort_unlock; |
| 5693 | } |
| 5694 | } |
| 5695 | goto done_unlock; |
| 5696 | |
| 5697 | default:; |
| 5698 | } |
| 5699 | |
| 5700 | /* |
| 5701 | * Commands querying/configuring an existing array: |
| 5702 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5703 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5704 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5705 | if ((!mddev->raid_disks && !mddev->external) |
| 5706 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5707 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5708 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | err = -ENODEV; |
| 5710 | goto abort_unlock; |
| 5711 | } |
| 5712 | |
| 5713 | /* |
| 5714 | * Commands even a read-only array can execute: |
| 5715 | */ |
| 5716 | switch (cmd) |
| 5717 | { |
| 5718 | case GET_ARRAY_INFO: |
| 5719 | err = get_array_info(mddev, argp); |
| 5720 | goto done_unlock; |
| 5721 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5722 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5723 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5724 | goto done_unlock; |
| 5725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | case GET_DISK_INFO: |
| 5727 | err = get_disk_info(mddev, argp); |
| 5728 | goto done_unlock; |
| 5729 | |
| 5730 | case RESTART_ARRAY_RW: |
| 5731 | err = restart_array(mddev); |
| 5732 | goto done_unlock; |
| 5733 | |
| 5734 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5735 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | goto done_unlock; |
| 5737 | |
| 5738 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 5739 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | goto done_unlock; |
| 5741 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5742 | case BLKROSET: |
| 5743 | if (get_user(ro, (int __user *)(arg))) { |
| 5744 | err = -EFAULT; |
| 5745 | goto done_unlock; |
| 5746 | } |
| 5747 | err = -EINVAL; |
| 5748 | |
| 5749 | /* if the bdev is going readonly the value of mddev->ro |
| 5750 | * does not matter, no writes are coming |
| 5751 | */ |
| 5752 | if (ro) |
| 5753 | goto done_unlock; |
| 5754 | |
| 5755 | /* are we are already prepared for writes? */ |
| 5756 | if (mddev->ro != 1) |
| 5757 | goto done_unlock; |
| 5758 | |
| 5759 | /* transitioning to readauto need only happen for |
| 5760 | * arrays that call md_write_start |
| 5761 | */ |
| 5762 | if (mddev->pers) { |
| 5763 | err = restart_array(mddev); |
| 5764 | if (err == 0) { |
| 5765 | mddev->ro = 2; |
| 5766 | set_disk_ro(mddev->gendisk, 0); |
| 5767 | } |
| 5768 | } |
| 5769 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | /* |
| 5773 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5774 | * superblock, so we do not allow them on read-only arrays. |
| 5775 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5776 | * here and hit the 'default' below, so only disallow |
| 5777 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5778 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5779 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5780 | if (mddev->ro == 2) { |
| 5781 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5782 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5783 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5784 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5785 | } else { |
| 5786 | err = -EROFS; |
| 5787 | goto abort_unlock; |
| 5788 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5789 | } |
| 5790 | |
| 5791 | switch (cmd) |
| 5792 | { |
| 5793 | case ADD_NEW_DISK: |
| 5794 | { |
| 5795 | mdu_disk_info_t info; |
| 5796 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5797 | err = -EFAULT; |
| 5798 | else |
| 5799 | err = add_new_disk(mddev, &info); |
| 5800 | goto done_unlock; |
| 5801 | } |
| 5802 | |
| 5803 | case HOT_REMOVE_DISK: |
| 5804 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5805 | goto done_unlock; |
| 5806 | |
| 5807 | case HOT_ADD_DISK: |
| 5808 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5809 | goto done_unlock; |
| 5810 | |
| 5811 | case SET_DISK_FAULTY: |
| 5812 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5813 | goto done_unlock; |
| 5814 | |
| 5815 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5816 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | goto done_unlock; |
| 5818 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5819 | case SET_BITMAP_FILE: |
| 5820 | err = set_bitmap_file(mddev, (int)arg); |
| 5821 | goto done_unlock; |
| 5822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | err = -EINVAL; |
| 5825 | goto abort_unlock; |
| 5826 | } |
| 5827 | |
| 5828 | done_unlock: |
| 5829 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5830 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5831 | err != -EINVAL) |
| 5832 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | mddev_unlock(mddev); |
| 5834 | |
| 5835 | return err; |
| 5836 | done: |
| 5837 | if (err) |
| 5838 | MD_BUG(); |
| 5839 | abort: |
| 5840 | return err; |
| 5841 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5842 | #ifdef CONFIG_COMPAT |
| 5843 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5844 | unsigned int cmd, unsigned long arg) |
| 5845 | { |
| 5846 | switch (cmd) { |
| 5847 | case HOT_REMOVE_DISK: |
| 5848 | case HOT_ADD_DISK: |
| 5849 | case SET_DISK_FAULTY: |
| 5850 | case SET_BITMAP_FILE: |
| 5851 | /* These take in integer arg, do not convert */ |
| 5852 | break; |
| 5853 | default: |
| 5854 | arg = (unsigned long)compat_ptr(arg); |
| 5855 | break; |
| 5856 | } |
| 5857 | |
| 5858 | return md_ioctl(bdev, mode, cmd, arg); |
| 5859 | } |
| 5860 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5861 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5862 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5863 | { |
| 5864 | /* |
| 5865 | * Succeed if we can lock the mddev, which confirms that |
| 5866 | * it isn't being stopped right now. |
| 5867 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5868 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | int err; |
| 5870 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5871 | if (mddev->gendisk != bdev->bd_disk) { |
| 5872 | /* we are racing with mddev_put which is discarding this |
| 5873 | * bd_disk. |
| 5874 | */ |
| 5875 | mddev_put(mddev); |
| 5876 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5877 | flush_scheduled_work(); |
| 5878 | /* Then retry the open from the top */ |
| 5879 | return -ERESTARTSYS; |
| 5880 | } |
| 5881 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5882 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5883 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5884 | goto out; |
| 5885 | |
| 5886 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5887 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5888 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5890 | out: |
| 5891 | return err; |
| 5892 | } |
| 5893 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5894 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5896 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5897 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5898 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5899 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | mddev_put(mddev); |
| 5901 | |
| 5902 | return 0; |
| 5903 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5904 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | { |
| 5906 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5907 | .open = md_open, |
| 5908 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5909 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5910 | #ifdef CONFIG_COMPAT |
| 5911 | .compat_ioctl = md_compat_ioctl, |
| 5912 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5913 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | }; |
| 5915 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5916 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | { |
| 5918 | mdk_thread_t *thread = arg; |
| 5919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | /* |
| 5921 | * md_thread is a 'system-thread', it's priority should be very |
| 5922 | * high. We avoid resource deadlocks individually in each |
| 5923 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5924 | * the very same RT priority as kswapd, thus we will never get |
| 5925 | * into a priority inversion deadlock. |
| 5926 | * |
| 5927 | * we definitely have to have equal or higher priority than |
| 5928 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5929 | * many dirty RAID5 blocks. |
| 5930 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5932 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5933 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5935 | /* We need to wait INTERRUPTIBLE so that |
| 5936 | * we don't add to the load-average. |
| 5937 | * That means we need to be sure no signals are |
| 5938 | * pending |
| 5939 | */ |
| 5940 | if (signal_pending(current)) |
| 5941 | flush_signals(current); |
| 5942 | |
| 5943 | wait_event_interruptible_timeout |
| 5944 | (thread->wqueue, |
| 5945 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5946 | || kthread_should_stop(), |
| 5947 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | |
| 5949 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5950 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5951 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | return 0; |
| 5955 | } |
| 5956 | |
| 5957 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5958 | { |
| 5959 | if (thread) { |
| 5960 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5961 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5962 | wake_up(&thread->wqueue); |
| 5963 | } |
| 5964 | } |
| 5965 | |
| 5966 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5967 | const char *name) |
| 5968 | { |
| 5969 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5971 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | if (!thread) |
| 5973 | return NULL; |
| 5974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | init_waitqueue_head(&thread->wqueue); |
| 5976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5977 | thread->run = run; |
| 5978 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5979 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5980 | thread->tsk = kthread_run(md_thread, thread, |
| 5981 | "%s_%s", |
| 5982 | mdname(thread->mddev), |
| 5983 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5984 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5985 | kfree(thread); |
| 5986 | return NULL; |
| 5987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | return thread; |
| 5989 | } |
| 5990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | void md_unregister_thread(mdk_thread_t *thread) |
| 5992 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5993 | if (!thread) |
| 5994 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5995 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5996 | |
| 5997 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5998 | kfree(thread); |
| 5999 | } |
| 6000 | |
| 6001 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6002 | { |
| 6003 | if (!mddev) { |
| 6004 | MD_BUG(); |
| 6005 | return; |
| 6006 | } |
| 6007 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6008 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6009 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6010 | |
| 6011 | if (mddev->external) |
| 6012 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6013 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6015 | mdname(mddev), |
| 6016 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6017 | __builtin_return_address(0),__builtin_return_address(1), |
| 6018 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6019 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6020 | if (!mddev->pers) |
| 6021 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | if (!mddev->pers->error_handler) |
| 6023 | return; |
| 6024 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6025 | if (mddev->degraded) |
| 6026 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c0cc75f | 2010-03-22 10:28:51 +1100 | [diff] [blame] | 6027 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6029 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6030 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6031 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | } |
| 6033 | |
| 6034 | /* seq_file implementation /proc/mdstat */ |
| 6035 | |
| 6036 | static void status_unused(struct seq_file *seq) |
| 6037 | { |
| 6038 | int i = 0; |
| 6039 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | |
| 6041 | seq_printf(seq, "unused devices: "); |
| 6042 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6043 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | char b[BDEVNAME_SIZE]; |
| 6045 | i++; |
| 6046 | seq_printf(seq, "%s ", |
| 6047 | bdevname(rdev->bdev,b)); |
| 6048 | } |
| 6049 | if (!i) |
| 6050 | seq_printf(seq, "<none>"); |
| 6051 | |
| 6052 | seq_printf(seq, "\n"); |
| 6053 | } |
| 6054 | |
| 6055 | |
| 6056 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6057 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6058 | sector_t max_sectors, resync, res; |
| 6059 | unsigned long dt, db; |
| 6060 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6061 | int scale; |
| 6062 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6063 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6064 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6065 | |
| 6066 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6067 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6069 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | |
| 6071 | /* |
| 6072 | * Should not happen. |
| 6073 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6074 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | MD_BUG(); |
| 6076 | return; |
| 6077 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6078 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6079 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6080 | * u32, as those are the requirements for sector_div. |
| 6081 | * Thus 'scale' must be at least 10 |
| 6082 | */ |
| 6083 | scale = 10; |
| 6084 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6085 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6086 | scale++; |
| 6087 | } |
| 6088 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6089 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6090 | |
| 6091 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6093 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6094 | seq_printf(seq, "["); |
| 6095 | for (i = 0; i < x; i++) |
| 6096 | seq_printf(seq, "="); |
| 6097 | seq_printf(seq, ">"); |
| 6098 | for (i = 0; i < y; i++) |
| 6099 | seq_printf(seq, "."); |
| 6100 | seq_printf(seq, "] "); |
| 6101 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6102 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6103 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6104 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6105 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6106 | "check" : |
| 6107 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6108 | "resync" : "recovery"))), |
| 6109 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6110 | (unsigned long long) resync/2, |
| 6111 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | |
| 6113 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | * dt: time from mark until now |
| 6115 | * db: blocks written from mark until now |
| 6116 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6117 | * |
| 6118 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6119 | * So we divide before multiply in case it is 32bit and close |
| 6120 | * to the limit. |
| 6121 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6122 | * near the end of resync when the number of remaining sectors |
| 6123 | * is close to 'db'. |
| 6124 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6125 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6126 | */ |
| 6127 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6128 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6129 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6130 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6132 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6133 | sector_div(rt, db/32+1); |
| 6134 | rt *= dt; |
| 6135 | rt >>= 5; |
| 6136 | |
| 6137 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6138 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6139 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6140 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | } |
| 6142 | |
| 6143 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6144 | { |
| 6145 | struct list_head *tmp; |
| 6146 | loff_t l = *pos; |
| 6147 | mddev_t *mddev; |
| 6148 | |
| 6149 | if (l >= 0x10000) |
| 6150 | return NULL; |
| 6151 | if (!l--) |
| 6152 | /* header */ |
| 6153 | return (void*)1; |
| 6154 | |
| 6155 | spin_lock(&all_mddevs_lock); |
| 6156 | list_for_each(tmp,&all_mddevs) |
| 6157 | if (!l--) { |
| 6158 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6159 | mddev_get(mddev); |
| 6160 | spin_unlock(&all_mddevs_lock); |
| 6161 | return mddev; |
| 6162 | } |
| 6163 | spin_unlock(&all_mddevs_lock); |
| 6164 | if (!l--) |
| 6165 | return (void*)2;/* tail */ |
| 6166 | return NULL; |
| 6167 | } |
| 6168 | |
| 6169 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6170 | { |
| 6171 | struct list_head *tmp; |
| 6172 | mddev_t *next_mddev, *mddev = v; |
| 6173 | |
| 6174 | ++*pos; |
| 6175 | if (v == (void*)2) |
| 6176 | return NULL; |
| 6177 | |
| 6178 | spin_lock(&all_mddevs_lock); |
| 6179 | if (v == (void*)1) |
| 6180 | tmp = all_mddevs.next; |
| 6181 | else |
| 6182 | tmp = mddev->all_mddevs.next; |
| 6183 | if (tmp != &all_mddevs) |
| 6184 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6185 | else { |
| 6186 | next_mddev = (void*)2; |
| 6187 | *pos = 0x10000; |
| 6188 | } |
| 6189 | spin_unlock(&all_mddevs_lock); |
| 6190 | |
| 6191 | if (v != (void*)1) |
| 6192 | mddev_put(mddev); |
| 6193 | return next_mddev; |
| 6194 | |
| 6195 | } |
| 6196 | |
| 6197 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6198 | { |
| 6199 | mddev_t *mddev = v; |
| 6200 | |
| 6201 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6202 | mddev_put(mddev); |
| 6203 | } |
| 6204 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6205 | struct mdstat_info { |
| 6206 | int event; |
| 6207 | }; |
| 6208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6209 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6210 | { |
| 6211 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6212 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6214 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6215 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | |
| 6217 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6218 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | seq_printf(seq, "Personalities : "); |
| 6220 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6221 | list_for_each_entry(pers, &pers_list, list) |
| 6222 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | |
| 6224 | spin_unlock(&pers_lock); |
| 6225 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6226 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | return 0; |
| 6228 | } |
| 6229 | if (v == (void*)2) { |
| 6230 | status_unused(seq); |
| 6231 | return 0; |
| 6232 | } |
| 6233 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6234 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6235 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6238 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6239 | mddev->pers ? "" : "in"); |
| 6240 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6241 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6242 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6243 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6244 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | seq_printf(seq, " %s", mddev->pers->name); |
| 6246 | } |
| 6247 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6248 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6249 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | char b[BDEVNAME_SIZE]; |
| 6251 | seq_printf(seq, " %s[%d]", |
| 6252 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6253 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6254 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6255 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | seq_printf(seq, "(F)"); |
| 6257 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6258 | } else if (rdev->raid_disk < 0) |
| 6259 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6260 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6261 | } |
| 6262 | |
| 6263 | if (!list_empty(&mddev->disks)) { |
| 6264 | if (mddev->pers) |
| 6265 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6266 | (unsigned long long) |
| 6267 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | else |
| 6269 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6270 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6272 | if (mddev->persistent) { |
| 6273 | if (mddev->major_version != 0 || |
| 6274 | mddev->minor_version != 90) { |
| 6275 | seq_printf(seq," super %d.%d", |
| 6276 | mddev->major_version, |
| 6277 | mddev->minor_version); |
| 6278 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6279 | } else if (mddev->external) |
| 6280 | seq_printf(seq, " super external:%s", |
| 6281 | mddev->metadata_type); |
| 6282 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6283 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6284 | |
| 6285 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6286 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6287 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6288 | if (mddev->pers->sync_request) { |
| 6289 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6290 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6291 | seq_printf(seq, "\n "); |
| 6292 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6293 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6294 | else if (mddev->recovery_cp < MaxSector) |
| 6295 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6296 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6297 | } else |
| 6298 | seq_printf(seq, "\n "); |
| 6299 | |
| 6300 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6301 | unsigned long chunk_kb; |
| 6302 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6303 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6304 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6305 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6306 | "%lu%s chunk", |
| 6307 | bitmap->pages - bitmap->missing_pages, |
| 6308 | bitmap->pages, |
| 6309 | (bitmap->pages - bitmap->missing_pages) |
| 6310 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6311 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6312 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6313 | if (bitmap->file) { |
| 6314 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6315 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6316 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6317 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6318 | seq_printf(seq, "\n"); |
| 6319 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6320 | } |
| 6321 | |
| 6322 | seq_printf(seq, "\n"); |
| 6323 | } |
| 6324 | mddev_unlock(mddev); |
| 6325 | |
| 6326 | return 0; |
| 6327 | } |
| 6328 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6329 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | .start = md_seq_start, |
| 6331 | .next = md_seq_next, |
| 6332 | .stop = md_seq_stop, |
| 6333 | .show = md_seq_show, |
| 6334 | }; |
| 6335 | |
| 6336 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6337 | { |
| 6338 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6339 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6340 | if (mi == NULL) |
| 6341 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6342 | |
| 6343 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6344 | if (error) |
| 6345 | kfree(mi); |
| 6346 | else { |
| 6347 | struct seq_file *p = file->private_data; |
| 6348 | p->private = mi; |
| 6349 | mi->event = atomic_read(&md_event_count); |
| 6350 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | return error; |
| 6352 | } |
| 6353 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6354 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6355 | { |
| 6356 | struct seq_file *m = filp->private_data; |
| 6357 | struct mdstat_info *mi = m->private; |
| 6358 | int mask; |
| 6359 | |
| 6360 | poll_wait(filp, &md_event_waiters, wait); |
| 6361 | |
| 6362 | /* always allow read */ |
| 6363 | mask = POLLIN | POLLRDNORM; |
| 6364 | |
| 6365 | if (mi->event != atomic_read(&md_event_count)) |
| 6366 | mask |= POLLERR | POLLPRI; |
| 6367 | return mask; |
| 6368 | } |
| 6369 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6370 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6371 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | .open = md_seq_open, |
| 6373 | .read = seq_read, |
| 6374 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6375 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6376 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | }; |
| 6378 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6379 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6382 | list_add_tail(&p->list, &pers_list); |
| 6383 | 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] | 6384 | spin_unlock(&pers_lock); |
| 6385 | return 0; |
| 6386 | } |
| 6387 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6388 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6389 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6390 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6391 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6392 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | spin_unlock(&pers_lock); |
| 6394 | return 0; |
| 6395 | } |
| 6396 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6397 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | { |
| 6399 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6400 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6401 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | |
| 6403 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6404 | rcu_read_lock(); |
| 6405 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6407 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6408 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6409 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6410 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6411 | * as sync_io is counted when a request starts, and |
| 6412 | * disk_stats is counted when it completes. |
| 6413 | * So resync activity will cause curr_events to be smaller than |
| 6414 | * when there was no such activity. |
| 6415 | * non-sync IO will cause disk_stat to increase without |
| 6416 | * increasing sync_io so curr_events will (eventually) |
| 6417 | * be larger than it was before. Once it becomes |
| 6418 | * substantially larger, the test below will cause |
| 6419 | * the array to appear non-idle, and resync will slow |
| 6420 | * down. |
| 6421 | * If there is a lot of outstanding resync activity when |
| 6422 | * we set last_event to curr_events, then all that activity |
| 6423 | * completing might cause the array to appear non-idle |
| 6424 | * and resync will be slowed down even though there might |
| 6425 | * not have been non-resync activity. This will only |
| 6426 | * happen once though. 'last_events' will soon reflect |
| 6427 | * the state where there is little or no outstanding |
| 6428 | * resync requests, and further resync activity will |
| 6429 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6430 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6431 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6432 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6433 | rdev->last_events = curr_events; |
| 6434 | idle = 0; |
| 6435 | } |
| 6436 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6437 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6438 | return idle; |
| 6439 | } |
| 6440 | |
| 6441 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6442 | { |
| 6443 | /* another "blocks" (512byte) blocks have been synced */ |
| 6444 | atomic_sub(blocks, &mddev->recovery_active); |
| 6445 | wake_up(&mddev->recovery_wait); |
| 6446 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6447 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6448 | md_wakeup_thread(mddev->thread); |
| 6449 | // stop recovery, signal do_sync .... |
| 6450 | } |
| 6451 | } |
| 6452 | |
| 6453 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6454 | /* md_write_start(mddev, bi) |
| 6455 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6456 | * in superblock) before writing, schedule a superblock update |
| 6457 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6458 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6459 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6461 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6462 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6463 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6464 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6465 | BUG_ON(mddev->ro == 1); |
| 6466 | if (mddev->ro == 2) { |
| 6467 | /* need to switch to read/write */ |
| 6468 | mddev->ro = 0; |
| 6469 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6470 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6471 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6472 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6473 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6474 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6475 | if (mddev->safemode == 1) |
| 6476 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6477 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6478 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6479 | if (mddev->in_sync) { |
| 6480 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6481 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6482 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6483 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6484 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6485 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6486 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6487 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6488 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6489 | wait_event(mddev->sb_wait, |
| 6490 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6491 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6492 | } |
| 6493 | |
| 6494 | void md_write_end(mddev_t *mddev) |
| 6495 | { |
| 6496 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6497 | if (mddev->safemode == 2) |
| 6498 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6499 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6501 | } |
| 6502 | } |
| 6503 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6504 | /* md_allow_write(mddev) |
| 6505 | * Calling this ensures that the array is marked 'active' so that writes |
| 6506 | * may proceed without blocking. It is important to call this before |
| 6507 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6508 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6509 | * |
| 6510 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6511 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6512 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6513 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6514 | { |
| 6515 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6516 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6517 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6518 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6519 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6520 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6521 | |
| 6522 | spin_lock_irq(&mddev->write_lock); |
| 6523 | if (mddev->in_sync) { |
| 6524 | mddev->in_sync = 0; |
| 6525 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6526 | if (mddev->safemode_delay && |
| 6527 | mddev->safemode == 0) |
| 6528 | mddev->safemode = 1; |
| 6529 | spin_unlock_irq(&mddev->write_lock); |
| 6530 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6531 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6532 | } else |
| 6533 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6534 | |
| 6535 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6536 | return -EAGAIN; |
| 6537 | else |
| 6538 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6539 | } |
| 6540 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6542 | #define SYNC_MARKS 10 |
| 6543 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6544 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6545 | { |
| 6546 | mddev_t *mddev2; |
| 6547 | unsigned int currspeed = 0, |
| 6548 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6549 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | unsigned long mark[SYNC_MARKS]; |
| 6551 | sector_t mark_cnt[SYNC_MARKS]; |
| 6552 | int last_mark,m; |
| 6553 | struct list_head *tmp; |
| 6554 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6555 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6556 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6557 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6558 | |
| 6559 | /* just incase thread restarts... */ |
| 6560 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6561 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6562 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6563 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6564 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6565 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6566 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6567 | desc = "data-check"; |
| 6568 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6569 | desc = "requested-resync"; |
| 6570 | else |
| 6571 | desc = "resync"; |
| 6572 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6573 | desc = "reshape"; |
| 6574 | else |
| 6575 | desc = "recovery"; |
| 6576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6577 | /* we overload curr_resync somewhat here. |
| 6578 | * 0 == not engaged in resync at all |
| 6579 | * 2 == checking that there is no conflict with another sync |
| 6580 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6581 | * commense |
| 6582 | * other == active in resync - this many blocks |
| 6583 | * |
| 6584 | * Before starting a resync we must have set curr_resync to |
| 6585 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6586 | * less than ours. When we find one that is the same or higher |
| 6587 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6588 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6589 | * This will mean we have to start checking from the beginning again. |
| 6590 | * |
| 6591 | */ |
| 6592 | |
| 6593 | do { |
| 6594 | mddev->curr_resync = 2; |
| 6595 | |
| 6596 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6597 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6598 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6599 | |
| 6600 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6601 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6602 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6603 | if (mddev2 == mddev) |
| 6604 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6605 | if (!mddev->parallel_resync |
| 6606 | && mddev2->curr_resync |
| 6607 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6608 | DEFINE_WAIT(wq); |
| 6609 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6610 | /* arbitrarily yield */ |
| 6611 | mddev->curr_resync = 1; |
| 6612 | wake_up(&resync_wait); |
| 6613 | } |
| 6614 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6615 | /* no need to wait here, we can wait the next |
| 6616 | * time 'round when curr_resync == 2 |
| 6617 | */ |
| 6618 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6619 | /* We need to wait 'interruptible' so as not to |
| 6620 | * contribute to the load average, and not to |
| 6621 | * be caught by 'softlockup' |
| 6622 | */ |
| 6623 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6624 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6625 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6626 | printk(KERN_INFO "md: delaying %s of %s" |
| 6627 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6628 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6629 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6631 | if (signal_pending(current)) |
| 6632 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | schedule(); |
| 6634 | finish_wait(&resync_wait, &wq); |
| 6635 | goto try_again; |
| 6636 | } |
| 6637 | finish_wait(&resync_wait, &wq); |
| 6638 | } |
| 6639 | } |
| 6640 | } while (mddev->curr_resync < 2); |
| 6641 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6642 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6643 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6644 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6645 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6646 | */ |
| 6647 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6648 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6649 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6650 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6651 | j = mddev->resync_min; |
| 6652 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6653 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6654 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6655 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6656 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6657 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6658 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6659 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6660 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6661 | rcu_read_lock(); |
| 6662 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6663 | if (rdev->raid_disk >= 0 && |
| 6664 | !test_bit(Faulty, &rdev->flags) && |
| 6665 | !test_bit(In_sync, &rdev->flags) && |
| 6666 | rdev->recovery_offset < j) |
| 6667 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6668 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6670 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6671 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6672 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6673 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6674 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6675 | "(but not more than %d KB/sec) for %s.\n", |
| 6676 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6677 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6678 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6679 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6680 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6681 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6682 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6683 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6684 | } |
| 6685 | last_mark = 0; |
| 6686 | mddev->resync_mark = mark[last_mark]; |
| 6687 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6688 | |
| 6689 | /* |
| 6690 | * Tune reconstruction: |
| 6691 | */ |
| 6692 | window = 32*(PAGE_SIZE/512); |
| 6693 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6694 | window/2,(unsigned long long) max_sectors/2); |
| 6695 | |
| 6696 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6697 | last_check = 0; |
| 6698 | |
| 6699 | if (j>2) { |
| 6700 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6701 | "md: resuming %s of %s from checkpoint.\n", |
| 6702 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6703 | mddev->curr_resync = j; |
| 6704 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6705 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | |
| 6707 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6708 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6710 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6711 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6712 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6713 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6714 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6715 | > (max_sectors >> 4)) || |
| 6716 | (j - mddev->curr_resync_completed)*2 |
| 6717 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6718 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6719 | /* time to update curr_resync_completed */ |
| 6720 | blk_unplug(mddev->queue); |
| 6721 | wait_event(mddev->recovery_wait, |
| 6722 | atomic_read(&mddev->recovery_active) == 0); |
| 6723 | mddev->curr_resync_completed = |
| 6724 | mddev->curr_resync; |
| 6725 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6726 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6727 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6728 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6729 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6730 | /* As this condition is controlled by user-space, |
| 6731 | * we can block indefinitely, so use '_interruptible' |
| 6732 | * to avoid triggering warnings. |
| 6733 | */ |
| 6734 | flush_signals(current); /* just in case */ |
| 6735 | wait_event_interruptible(mddev->recovery_wait, |
| 6736 | mddev->resync_max > j |
| 6737 | || kthread_should_stop()); |
| 6738 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6739 | |
| 6740 | if (kthread_should_stop()) |
| 6741 | goto interrupted; |
| 6742 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6743 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6744 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6745 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6746 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6747 | goto out; |
| 6748 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6749 | |
| 6750 | if (!skipped) { /* actual IO requested */ |
| 6751 | io_sectors += sectors; |
| 6752 | atomic_add(sectors, &mddev->recovery_active); |
| 6753 | } |
| 6754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6755 | j += sectors; |
| 6756 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6757 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6758 | if (last_check == 0) |
| 6759 | /* this is the earliers that rebuilt will be |
| 6760 | * visible in /proc/mdstat |
| 6761 | */ |
| 6762 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6763 | |
| 6764 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | continue; |
| 6766 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6767 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6768 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6769 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6770 | break; |
| 6771 | |
| 6772 | repeat: |
| 6773 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6774 | /* step marks */ |
| 6775 | int next = (last_mark+1) % SYNC_MARKS; |
| 6776 | |
| 6777 | mddev->resync_mark = mark[next]; |
| 6778 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6779 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6780 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | last_mark = next; |
| 6782 | } |
| 6783 | |
| 6784 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6785 | if (kthread_should_stop()) |
| 6786 | goto interrupted; |
| 6787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | |
| 6789 | /* |
| 6790 | * this loop exits only if either when we are slower than |
| 6791 | * the 'hard' speed limit, or the system was IO-idle for |
| 6792 | * a jiffy. |
| 6793 | * the system might be non-idle CPU-wise, but we only care |
| 6794 | * about not overloading the IO subsystem. (things like an |
| 6795 | * e2fsck being done on the RAID array should execute fast) |
| 6796 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6797 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | cond_resched(); |
| 6799 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6800 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6801 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6803 | if (currspeed > speed_min(mddev)) { |
| 6804 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6805 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6806 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6807 | goto repeat; |
| 6808 | } |
| 6809 | } |
| 6810 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6811 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6812 | /* |
| 6813 | * this also signals 'finished resyncing' to md_stop |
| 6814 | */ |
| 6815 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6816 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6817 | |
| 6818 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6819 | |
| 6820 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6821 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6823 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6824 | mddev->curr_resync > 2) { |
| 6825 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6826 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6827 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6828 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6829 | "md: checkpointing %s of %s.\n", |
| 6830 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6831 | mddev->recovery_cp = mddev->curr_resync; |
| 6832 | } |
| 6833 | } else |
| 6834 | mddev->recovery_cp = MaxSector; |
| 6835 | } else { |
| 6836 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6837 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6838 | rcu_read_lock(); |
| 6839 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6840 | if (rdev->raid_disk >= 0 && |
| 6841 | !test_bit(Faulty, &rdev->flags) && |
| 6842 | !test_bit(In_sync, &rdev->flags) && |
| 6843 | rdev->recovery_offset < mddev->curr_resync) |
| 6844 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6845 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6846 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6847 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6848 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6851 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6852 | /* We completed so min/max setting can be forgotten if used. */ |
| 6853 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6854 | mddev->resync_min = 0; |
| 6855 | mddev->resync_max = MaxSector; |
| 6856 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6857 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6858 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6859 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6860 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6861 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | wake_up(&resync_wait); |
| 6863 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6864 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6865 | return; |
| 6866 | |
| 6867 | interrupted: |
| 6868 | /* |
| 6869 | * got a signal, exit. |
| 6870 | */ |
| 6871 | printk(KERN_INFO |
| 6872 | "md: md_do_sync() got signal ... exiting\n"); |
| 6873 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6874 | goto out; |
| 6875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6876 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6877 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6878 | |
| 6879 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6880 | static int remove_and_add_spares(mddev_t *mddev) |
| 6881 | { |
| 6882 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6883 | int spares = 0; |
| 6884 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6885 | mddev->curr_resync_completed = 0; |
| 6886 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6887 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6888 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6889 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6890 | (test_bit(Faulty, &rdev->flags) || |
| 6891 | ! test_bit(In_sync, &rdev->flags)) && |
| 6892 | atomic_read(&rdev->nr_pending)==0) { |
| 6893 | if (mddev->pers->hot_remove_disk( |
| 6894 | mddev, rdev->raid_disk)==0) { |
| 6895 | char nm[20]; |
| 6896 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6897 | sysfs_remove_link(&mddev->kobj, nm); |
| 6898 | rdev->raid_disk = -1; |
| 6899 | } |
| 6900 | } |
| 6901 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6902 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6903 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6904 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6905 | !test_bit(In_sync, &rdev->flags) && |
| 6906 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6907 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6908 | if (rdev->raid_disk < 0 |
| 6909 | && !test_bit(Faulty, &rdev->flags)) { |
| 6910 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6911 | if (mddev->pers-> |
| 6912 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6913 | char nm[20]; |
| 6914 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6915 | if (sysfs_create_link(&mddev->kobj, |
| 6916 | &rdev->kobj, nm)) |
| 6917 | printk(KERN_WARNING |
| 6918 | "md: cannot register " |
| 6919 | "%s for %s\n", |
| 6920 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6921 | spares++; |
| 6922 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 6923 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6924 | } else |
| 6925 | break; |
| 6926 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6927 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6928 | } |
| 6929 | return spares; |
| 6930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | /* |
| 6932 | * This routine is regularly called by all per-raid-array threads to |
| 6933 | * deal with generic issues like resync and super-block update. |
| 6934 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6935 | * need this as they never do any recovery or update the superblock. |
| 6936 | * |
| 6937 | * It does not do any resync itself, but rather "forks" off other threads |
| 6938 | * to do that as needed. |
| 6939 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6940 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6941 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6942 | * and wakeups up this thread which will reap the thread and finish up. |
| 6943 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6944 | * |
| 6945 | * The overall approach is: |
| 6946 | * 1/ if the superblock needs updating, update it. |
| 6947 | * 2/ If a recovery thread is running, don't do anything else. |
| 6948 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6949 | * 4/ If there are any faulty devices, remove them. |
| 6950 | * 5/ If array is degraded, try to add spares devices |
| 6951 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6952 | */ |
| 6953 | void md_check_recovery(mddev_t *mddev) |
| 6954 | { |
| 6955 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6956 | |
| 6957 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6958 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6959 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | |
| 6961 | if (mddev->ro) |
| 6962 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6963 | |
| 6964 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6965 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6966 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6967 | mdname(mddev)); |
| 6968 | mddev->safemode = 2; |
| 6969 | } |
| 6970 | flush_signals(current); |
| 6971 | } |
| 6972 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6973 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6974 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6976 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6977 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6978 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6979 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6980 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6981 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | )) |
| 6983 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6984 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6985 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6986 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6987 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6988 | if (mddev->ro) { |
| 6989 | /* Only thing we do on a ro array is remove |
| 6990 | * failed devices. |
| 6991 | */ |
| 6992 | remove_and_add_spares(mddev); |
| 6993 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6994 | goto unlock; |
| 6995 | } |
| 6996 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6997 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6998 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6999 | spin_lock_irq(&mddev->write_lock); |
| 7000 | if (mddev->safemode && |
| 7001 | !atomic_read(&mddev->writes_pending) && |
| 7002 | !mddev->in_sync && |
| 7003 | mddev->recovery_cp == MaxSector) { |
| 7004 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7005 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7006 | if (mddev->persistent) |
| 7007 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 7008 | } |
| 7009 | if (mddev->safemode == 1) |
| 7010 | mddev->safemode = 0; |
| 7011 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7012 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 7013 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7014 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7015 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7016 | if (mddev->flags) |
| 7017 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7019 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7020 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7021 | /* resync/recovery still happening */ |
| 7022 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7023 | goto unlock; |
| 7024 | } |
| 7025 | if (mddev->sync_thread) { |
| 7026 | /* resync has finished, collect result */ |
| 7027 | md_unregister_thread(mddev->sync_thread); |
| 7028 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7029 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7030 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7031 | /* success...*/ |
| 7032 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 7033 | if (mddev->pers->spare_active(mddev)) |
| 7034 | sysfs_notify(&mddev->kobj, NULL, |
| 7035 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7036 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7037 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7038 | mddev->pers->finish_reshape) |
| 7039 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7040 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7041 | |
| 7042 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7043 | * information must be scrapped |
| 7044 | */ |
| 7045 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7046 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7047 | rdev->saved_raid_disk = -1; |
| 7048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | mddev->recovery = 0; |
| 7050 | /* flag recovery needed just to double check */ |
| 7051 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7052 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7053 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7054 | goto unlock; |
| 7055 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7056 | /* Set RUNNING before clearing NEEDED to avoid |
| 7057 | * any transients in the value of "sync_action". |
| 7058 | */ |
| 7059 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7060 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7061 | /* Clear some bits that don't mean anything, but |
| 7062 | * might be left set |
| 7063 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7064 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7065 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7067 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7068 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7069 | /* no recovery is running. |
| 7070 | * remove any failed drives, then |
| 7071 | * add spares if possible. |
| 7072 | * Spare are also removed and re-added, to allow |
| 7073 | * the personality to fail the re-add. |
| 7074 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7075 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7076 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7077 | if (mddev->pers->check_reshape == NULL || |
| 7078 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7079 | /* Cannot proceed */ |
| 7080 | goto unlock; |
| 7081 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7082 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7083 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7084 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7085 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7086 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7087 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7088 | } else if (mddev->recovery_cp < MaxSector) { |
| 7089 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7090 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7091 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7092 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7093 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7095 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7096 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7097 | /* We are adding a device or devices to an array |
| 7098 | * which has the bitmap stored on all devices. |
| 7099 | * So make sure all bitmap pages get written |
| 7100 | */ |
| 7101 | bitmap_write_all(mddev->bitmap); |
| 7102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7103 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7104 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7105 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7106 | if (!mddev->sync_thread) { |
| 7107 | printk(KERN_ERR "%s: could not start resync" |
| 7108 | " thread...\n", |
| 7109 | mdname(mddev)); |
| 7110 | /* leave the spares where they are, it shouldn't hurt */ |
| 7111 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7112 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7113 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7114 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7115 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7116 | } |
| 7117 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7118 | if (!mddev->sync_thread) { |
| 7119 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7120 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7121 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7122 | if (mddev->sysfs_action) |
| 7123 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7125 | mddev_unlock(mddev); |
| 7126 | } |
| 7127 | } |
| 7128 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7129 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7130 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 7131 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7132 | wait_event_timeout(rdev->blocked_wait, |
| 7133 | !test_bit(Blocked, &rdev->flags), |
| 7134 | msecs_to_jiffies(5000)); |
| 7135 | rdev_dec_pending(rdev, mddev); |
| 7136 | } |
| 7137 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7138 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7139 | static int md_notify_reboot(struct notifier_block *this, |
| 7140 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | { |
| 7142 | struct list_head *tmp; |
| 7143 | mddev_t *mddev; |
| 7144 | |
| 7145 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7146 | |
| 7147 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7148 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7149 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7150 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7151 | /* Force a switch to readonly even array |
| 7152 | * appears to still be in use. Hence |
| 7153 | * the '100'. |
| 7154 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame^] | 7155 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7156 | mddev_unlock(mddev); |
| 7157 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7158 | /* |
| 7159 | * certain more exotic SCSI devices are known to be |
| 7160 | * volatile wrt too early system reboots. While the |
| 7161 | * right place to handle this issue is the given |
| 7162 | * driver, we do want to have a safe RAID driver ... |
| 7163 | */ |
| 7164 | mdelay(1000*1); |
| 7165 | } |
| 7166 | return NOTIFY_DONE; |
| 7167 | } |
| 7168 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7169 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7170 | .notifier_call = md_notify_reboot, |
| 7171 | .next = NULL, |
| 7172 | .priority = INT_MAX, /* before any real devices */ |
| 7173 | }; |
| 7174 | |
| 7175 | static void md_geninit(void) |
| 7176 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7177 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7178 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7179 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7180 | } |
| 7181 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7182 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7183 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7184 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7185 | return -1; |
| 7186 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7187 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7188 | return -1; |
| 7189 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7190 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7191 | md_probe, NULL, NULL); |
| 7192 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7193 | md_probe, NULL, NULL); |
| 7194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7195 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7196 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | |
| 7198 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7199 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | } |
| 7201 | |
| 7202 | |
| 7203 | #ifndef MODULE |
| 7204 | |
| 7205 | /* |
| 7206 | * Searches all registered partitions for autorun RAID arrays |
| 7207 | * at boot time. |
| 7208 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7209 | |
| 7210 | static LIST_HEAD(all_detected_devices); |
| 7211 | struct detected_devices_node { |
| 7212 | struct list_head list; |
| 7213 | dev_t dev; |
| 7214 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7215 | |
| 7216 | void md_autodetect_dev(dev_t dev) |
| 7217 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7218 | struct detected_devices_node *node_detected_dev; |
| 7219 | |
| 7220 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7221 | if (node_detected_dev) { |
| 7222 | node_detected_dev->dev = dev; |
| 7223 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7224 | } else { |
| 7225 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7226 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7227 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7228 | } |
| 7229 | |
| 7230 | |
| 7231 | static void autostart_arrays(int part) |
| 7232 | { |
| 7233 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7234 | struct detected_devices_node *node_detected_dev; |
| 7235 | dev_t dev; |
| 7236 | int i_scanned, i_passed; |
| 7237 | |
| 7238 | i_scanned = 0; |
| 7239 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7240 | |
| 7241 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7242 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7243 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7244 | i_scanned++; |
| 7245 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7246 | struct detected_devices_node, list); |
| 7247 | list_del(&node_detected_dev->list); |
| 7248 | dev = node_detected_dev->dev; |
| 7249 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7250 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7251 | if (IS_ERR(rdev)) |
| 7252 | continue; |
| 7253 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7254 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7255 | MD_BUG(); |
| 7256 | continue; |
| 7257 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7258 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7259 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7260 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7261 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7262 | |
| 7263 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7264 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7265 | |
| 7266 | autorun_devices(part); |
| 7267 | } |
| 7268 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7269 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7270 | |
| 7271 | static __exit void md_exit(void) |
| 7272 | { |
| 7273 | mddev_t *mddev; |
| 7274 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7275 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7276 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7277 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7278 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7279 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7280 | unregister_blkdev(mdp_major, "mdp"); |
| 7281 | unregister_reboot_notifier(&md_notifier); |
| 7282 | unregister_sysctl_table(raid_table_header); |
| 7283 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7284 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7285 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7286 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7287 | } |
| 7288 | } |
| 7289 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7290 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7291 | module_exit(md_exit) |
| 7292 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7293 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7294 | { |
| 7295 | return sprintf(buffer, "%d", start_readonly); |
| 7296 | } |
| 7297 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7298 | { |
| 7299 | char *e; |
| 7300 | int num = simple_strtoul(val, &e, 10); |
| 7301 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7302 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7303 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7304 | } |
| 7305 | return -EINVAL; |
| 7306 | } |
| 7307 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7308 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7309 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7310 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7311 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7313 | EXPORT_SYMBOL(register_md_personality); |
| 7314 | EXPORT_SYMBOL(unregister_md_personality); |
| 7315 | EXPORT_SYMBOL(md_error); |
| 7316 | EXPORT_SYMBOL(md_done_sync); |
| 7317 | EXPORT_SYMBOL(md_write_start); |
| 7318 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7319 | EXPORT_SYMBOL(md_register_thread); |
| 7320 | EXPORT_SYMBOL(md_unregister_thread); |
| 7321 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7322 | EXPORT_SYMBOL(md_check_recovery); |
| 7323 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7324 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7325 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7326 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |