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 | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 243 | rv = mddev->pers->make_request(mddev, 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 | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 257 | /* mddev_suspend makes sure no new requests are submitted |
| 258 | * to the device, and that any requests that have been submitted |
| 259 | * are completely handled. |
| 260 | * Once ->stop is called and completes, the module will be completely |
| 261 | * unused. |
| 262 | */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 263 | static void mddev_suspend(mddev_t *mddev) |
| 264 | { |
| 265 | BUG_ON(mddev->suspended); |
| 266 | mddev->suspended = 1; |
| 267 | synchronize_rcu(); |
| 268 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 269 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static void mddev_resume(mddev_t *mddev) |
| 273 | { |
| 274 | mddev->suspended = 0; |
| 275 | wake_up(&mddev->sb_wait); |
| 276 | mddev->pers->quiesce(mddev, 0); |
| 277 | } |
| 278 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 279 | int mddev_congested(mddev_t *mddev, int bits) |
| 280 | { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 281 | if (mddev->barrier) |
| 282 | return 1; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 283 | return mddev->suspended; |
| 284 | } |
| 285 | EXPORT_SYMBOL(mddev_congested); |
| 286 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 287 | /* |
| 288 | * Generic barrier handling for md |
| 289 | */ |
| 290 | |
| 291 | #define POST_REQUEST_BARRIER ((void*)1) |
| 292 | |
| 293 | static void md_end_barrier(struct bio *bio, int err) |
| 294 | { |
| 295 | mdk_rdev_t *rdev = bio->bi_private; |
| 296 | mddev_t *mddev = rdev->mddev; |
| 297 | if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER) |
| 298 | set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags); |
| 299 | |
| 300 | rdev_dec_pending(rdev, mddev); |
| 301 | |
| 302 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 303 | if (mddev->barrier == POST_REQUEST_BARRIER) { |
| 304 | /* This was a post-request barrier */ |
| 305 | mddev->barrier = NULL; |
| 306 | wake_up(&mddev->sb_wait); |
| 307 | } else |
| 308 | /* The pre-request barrier has finished */ |
| 309 | schedule_work(&mddev->barrier_work); |
| 310 | } |
| 311 | bio_put(bio); |
| 312 | } |
| 313 | |
| 314 | static void submit_barriers(mddev_t *mddev) |
| 315 | { |
| 316 | mdk_rdev_t *rdev; |
| 317 | |
| 318 | rcu_read_lock(); |
| 319 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 320 | if (rdev->raid_disk >= 0 && |
| 321 | !test_bit(Faulty, &rdev->flags)) { |
| 322 | /* Take two references, one is dropped |
| 323 | * when request finishes, one after |
| 324 | * we reclaim rcu_read_lock |
| 325 | */ |
| 326 | struct bio *bi; |
| 327 | atomic_inc(&rdev->nr_pending); |
| 328 | atomic_inc(&rdev->nr_pending); |
| 329 | rcu_read_unlock(); |
| 330 | bi = bio_alloc(GFP_KERNEL, 0); |
| 331 | bi->bi_end_io = md_end_barrier; |
| 332 | bi->bi_private = rdev; |
| 333 | bi->bi_bdev = rdev->bdev; |
| 334 | atomic_inc(&mddev->flush_pending); |
| 335 | submit_bio(WRITE_BARRIER, bi); |
| 336 | rcu_read_lock(); |
| 337 | rdev_dec_pending(rdev, mddev); |
| 338 | } |
| 339 | rcu_read_unlock(); |
| 340 | } |
| 341 | |
| 342 | static void md_submit_barrier(struct work_struct *ws) |
| 343 | { |
| 344 | mddev_t *mddev = container_of(ws, mddev_t, barrier_work); |
| 345 | struct bio *bio = mddev->barrier; |
| 346 | |
| 347 | atomic_set(&mddev->flush_pending, 1); |
| 348 | |
| 349 | if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags)) |
| 350 | bio_endio(bio, -EOPNOTSUPP); |
| 351 | else if (bio->bi_size == 0) |
| 352 | /* an empty barrier - all done */ |
| 353 | bio_endio(bio, 0); |
| 354 | else { |
| 355 | bio->bi_rw &= ~(1<<BIO_RW_BARRIER); |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 356 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 357 | generic_make_request(bio); |
| 358 | mddev->barrier = POST_REQUEST_BARRIER; |
| 359 | submit_barriers(mddev); |
| 360 | } |
| 361 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 362 | mddev->barrier = NULL; |
| 363 | wake_up(&mddev->sb_wait); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void md_barrier_request(mddev_t *mddev, struct bio *bio) |
| 368 | { |
| 369 | spin_lock_irq(&mddev->write_lock); |
| 370 | wait_event_lock_irq(mddev->sb_wait, |
| 371 | !mddev->barrier, |
| 372 | mddev->write_lock, /*nothing*/); |
| 373 | mddev->barrier = bio; |
| 374 | spin_unlock_irq(&mddev->write_lock); |
| 375 | |
| 376 | atomic_set(&mddev->flush_pending, 1); |
| 377 | INIT_WORK(&mddev->barrier_work, md_submit_barrier); |
| 378 | |
| 379 | submit_barriers(mddev); |
| 380 | |
| 381 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 382 | schedule_work(&mddev->barrier_work); |
| 383 | } |
| 384 | EXPORT_SYMBOL(md_barrier_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 387 | { |
| 388 | atomic_inc(&mddev->active); |
| 389 | return mddev; |
| 390 | } |
| 391 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 392 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | static void mddev_put(mddev_t *mddev) |
| 395 | { |
| 396 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 397 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 398 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 399 | mddev->ctime == 0 && !mddev->hold_active) { |
| 400 | /* Array is not configured at all, and not held active, |
| 401 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 403 | if (mddev->gendisk) { |
| 404 | /* we did a probe so need to clean up. |
| 405 | * Call schedule_work inside the spinlock |
| 406 | * so that flush_scheduled_work() after |
| 407 | * mddev_find will succeed in waiting for the |
| 408 | * work to be done. |
| 409 | */ |
| 410 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 411 | schedule_work(&mddev->del_work); |
| 412 | } else |
| 413 | kfree(mddev); |
| 414 | } |
| 415 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 418 | static void mddev_init(mddev_t *mddev) |
| 419 | { |
| 420 | mutex_init(&mddev->open_mutex); |
| 421 | mutex_init(&mddev->reconfig_mutex); |
| 422 | mutex_init(&mddev->bitmap_info.mutex); |
| 423 | INIT_LIST_HEAD(&mddev->disks); |
| 424 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 425 | init_timer(&mddev->safemode_timer); |
| 426 | atomic_set(&mddev->active, 1); |
| 427 | atomic_set(&mddev->openers, 0); |
| 428 | atomic_set(&mddev->active_io, 0); |
| 429 | spin_lock_init(&mddev->write_lock); |
| 430 | atomic_set(&mddev->flush_pending, 0); |
| 431 | init_waitqueue_head(&mddev->sb_wait); |
| 432 | init_waitqueue_head(&mddev->recovery_wait); |
| 433 | mddev->reshape_position = MaxSector; |
| 434 | mddev->resync_min = 0; |
| 435 | mddev->resync_max = MaxSector; |
| 436 | mddev->level = LEVEL_NONE; |
| 437 | } |
| 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | static mddev_t * mddev_find(dev_t unit) |
| 440 | { |
| 441 | mddev_t *mddev, *new = NULL; |
| 442 | |
| 443 | retry: |
| 444 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 446 | if (unit) { |
| 447 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 448 | if (mddev->unit == unit) { |
| 449 | mddev_get(mddev); |
| 450 | spin_unlock(&all_mddevs_lock); |
| 451 | kfree(new); |
| 452 | return mddev; |
| 453 | } |
| 454 | |
| 455 | if (new) { |
| 456 | list_add(&new->all_mddevs, &all_mddevs); |
| 457 | spin_unlock(&all_mddevs_lock); |
| 458 | new->hold_active = UNTIL_IOCTL; |
| 459 | return new; |
| 460 | } |
| 461 | } else if (new) { |
| 462 | /* find an unused unit number */ |
| 463 | static int next_minor = 512; |
| 464 | int start = next_minor; |
| 465 | int is_free = 0; |
| 466 | int dev = 0; |
| 467 | while (!is_free) { |
| 468 | dev = MKDEV(MD_MAJOR, next_minor); |
| 469 | next_minor++; |
| 470 | if (next_minor > MINORMASK) |
| 471 | next_minor = 0; |
| 472 | if (next_minor == start) { |
| 473 | /* Oh dear, all in use. */ |
| 474 | spin_unlock(&all_mddevs_lock); |
| 475 | kfree(new); |
| 476 | return NULL; |
| 477 | } |
| 478 | |
| 479 | is_free = 1; |
| 480 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 481 | if (mddev->unit == dev) { |
| 482 | is_free = 0; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | new->unit = dev; |
| 487 | new->md_minor = MINOR(dev); |
| 488 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | list_add(&new->all_mddevs, &all_mddevs); |
| 490 | spin_unlock(&all_mddevs_lock); |
| 491 | return new; |
| 492 | } |
| 493 | spin_unlock(&all_mddevs_lock); |
| 494 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 495 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (!new) |
| 497 | return NULL; |
| 498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | new->unit = unit; |
| 500 | if (MAJOR(unit) == MD_MAJOR) |
| 501 | new->md_minor = MINOR(unit); |
| 502 | else |
| 503 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 504 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 505 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | goto retry; |
| 508 | } |
| 509 | |
| 510 | static inline int mddev_lock(mddev_t * mddev) |
| 511 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 512 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 515 | static inline int mddev_is_locked(mddev_t *mddev) |
| 516 | { |
| 517 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 518 | } |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | static inline int mddev_trylock(mddev_t * mddev) |
| 521 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 522 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
| 524 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 525 | static struct attribute_group md_redundancy_group; |
| 526 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 527 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 529 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 530 | /* These cannot be removed under reconfig_mutex as |
| 531 | * an access to the files will try to take reconfig_mutex |
| 532 | * while holding the file unremovable, which leads to |
| 533 | * a deadlock. |
| 534 | * So hold open_mutex instead - we are allowed to take |
| 535 | * it while holding reconfig_mutex, and md_run can |
| 536 | * use it to wait for the remove to complete. |
| 537 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 538 | struct attribute_group *to_remove = mddev->to_remove; |
| 539 | mddev->to_remove = NULL; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 540 | mutex_lock(&mddev->open_mutex); |
| 541 | mutex_unlock(&mddev->reconfig_mutex); |
| 542 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 543 | if (to_remove != &md_redundancy_group) |
| 544 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 545 | if (mddev->pers == NULL || |
| 546 | mddev->pers->sync_request == NULL) { |
| 547 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 548 | if (mddev->sysfs_action) |
| 549 | sysfs_put(mddev->sysfs_action); |
| 550 | mddev->sysfs_action = NULL; |
| 551 | } |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 552 | mutex_unlock(&mddev->open_mutex); |
| 553 | } else |
| 554 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 556 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 559 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 561 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 563 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | if (rdev->desc_nr == nr) |
| 565 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | return NULL; |
| 568 | } |
| 569 | |
| 570 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 571 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | mdk_rdev_t *rdev; |
| 573 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 574 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | if (rdev->bdev->bd_dev == dev) |
| 576 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | return NULL; |
| 579 | } |
| 580 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 581 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 582 | { |
| 583 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 584 | list_for_each_entry(pers, &pers_list, list) { |
| 585 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 586 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 587 | if (strcmp(pers->name, clevel)==0) |
| 588 | return pers; |
| 589 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 590 | return NULL; |
| 591 | } |
| 592 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 593 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 594 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 596 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 597 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 601 | { |
| 602 | if (rdev->sb_page) |
| 603 | MD_BUG(); |
| 604 | |
| 605 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 606 | if (!rdev->sb_page) { |
| 607 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 608 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 615 | { |
| 616 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 617 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | rdev->sb_loaded = 0; |
| 619 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 620 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 621 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
| 625 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 626 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 627 | { |
| 628 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 629 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 630 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 631 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 632 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 633 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 634 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 635 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 636 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 637 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 638 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 639 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 640 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 641 | } |
| 642 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 643 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 644 | { |
| 645 | struct bio *bio2 = bio->bi_private; |
| 646 | mdk_rdev_t *rdev = bio2->bi_private; |
| 647 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 648 | |
| 649 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 650 | error == -EOPNOTSUPP) { |
| 651 | unsigned long flags; |
| 652 | /* barriers don't appear to be supported :-( */ |
| 653 | set_bit(BarriersNotsupp, &rdev->flags); |
| 654 | mddev->barriers_work = 0; |
| 655 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 656 | bio2->bi_next = mddev->biolist; |
| 657 | mddev->biolist = bio2; |
| 658 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 659 | wake_up(&mddev->sb_wait); |
| 660 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 661 | } else { |
| 662 | bio_put(bio2); |
| 663 | bio->bi_private = rdev; |
| 664 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 665 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 666 | } |
| 667 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 668 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 669 | sector_t sector, int size, struct page *page) |
| 670 | { |
| 671 | /* write first size bytes of page to sector of rdev |
| 672 | * Increment mddev->pending_writes before returning |
| 673 | * and decrement it on completion, waking up sb_wait |
| 674 | * if zero is reached. |
| 675 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 676 | * |
| 677 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 678 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 679 | */ |
| 680 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 681 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 682 | |
| 683 | bio->bi_bdev = rdev->bdev; |
| 684 | bio->bi_sector = sector; |
| 685 | bio_add_page(bio, page, size, 0); |
| 686 | bio->bi_private = rdev; |
| 687 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 688 | bio->bi_rw = rw; |
| 689 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 690 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 691 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 692 | struct bio *rbio; |
| 693 | rw |= (1<<BIO_RW_BARRIER); |
| 694 | rbio = bio_clone(bio, GFP_NOIO); |
| 695 | rbio->bi_private = bio; |
| 696 | rbio->bi_end_io = super_written_barrier; |
| 697 | submit_bio(rw, rbio); |
| 698 | } else |
| 699 | submit_bio(rw, bio); |
| 700 | } |
| 701 | |
| 702 | void md_super_wait(mddev_t *mddev) |
| 703 | { |
| 704 | /* wait for all superblock writes that were scheduled to complete. |
| 705 | * if any had to be retried (due to BARRIER problems), retry them |
| 706 | */ |
| 707 | DEFINE_WAIT(wq); |
| 708 | for(;;) { |
| 709 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 710 | if (atomic_read(&mddev->pending_writes)==0) |
| 711 | break; |
| 712 | while (mddev->biolist) { |
| 713 | struct bio *bio; |
| 714 | spin_lock_irq(&mddev->write_lock); |
| 715 | bio = mddev->biolist; |
| 716 | mddev->biolist = bio->bi_next ; |
| 717 | bio->bi_next = NULL; |
| 718 | spin_unlock_irq(&mddev->write_lock); |
| 719 | submit_bio(bio->bi_rw, bio); |
| 720 | } |
| 721 | schedule(); |
| 722 | } |
| 723 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 724 | } |
| 725 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 726 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | } |
| 730 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 731 | 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] | 732 | struct page *page, int rw) |
| 733 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 734 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | struct completion event; |
| 736 | int ret; |
| 737 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 738 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | |
| 740 | bio->bi_bdev = bdev; |
| 741 | bio->bi_sector = sector; |
| 742 | bio_add_page(bio, page, size, 0); |
| 743 | init_completion(&event); |
| 744 | bio->bi_private = &event; |
| 745 | bio->bi_end_io = bi_complete; |
| 746 | submit_bio(rw, bio); |
| 747 | wait_for_completion(&event); |
| 748 | |
| 749 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 750 | bio_put(bio); |
| 751 | return ret; |
| 752 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 753 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 755 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | { |
| 757 | char b[BDEVNAME_SIZE]; |
| 758 | if (!rdev->sb_page) { |
| 759 | MD_BUG(); |
| 760 | return -EINVAL; |
| 761 | } |
| 762 | if (rdev->sb_loaded) |
| 763 | return 0; |
| 764 | |
| 765 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 766 | 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] | 767 | goto fail; |
| 768 | rdev->sb_loaded = 1; |
| 769 | return 0; |
| 770 | |
| 771 | fail: |
| 772 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 773 | bdevname(rdev->bdev,b)); |
| 774 | return -EINVAL; |
| 775 | } |
| 776 | |
| 777 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 778 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 779 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 780 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 781 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 782 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 786 | { |
| 787 | int ret; |
| 788 | mdp_super_t *tmp1, *tmp2; |
| 789 | |
| 790 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 791 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 792 | |
| 793 | if (!tmp1 || !tmp2) { |
| 794 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 795 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | goto abort; |
| 797 | } |
| 798 | |
| 799 | *tmp1 = *sb1; |
| 800 | *tmp2 = *sb2; |
| 801 | |
| 802 | /* |
| 803 | * nr_disks is not constant |
| 804 | */ |
| 805 | tmp1->nr_disks = 0; |
| 806 | tmp2->nr_disks = 0; |
| 807 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 808 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 810 | kfree(tmp1); |
| 811 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | return ret; |
| 813 | } |
| 814 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 815 | |
| 816 | static u32 md_csum_fold(u32 csum) |
| 817 | { |
| 818 | csum = (csum & 0xffff) + (csum >> 16); |
| 819 | return (csum & 0xffff) + (csum >> 16); |
| 820 | } |
| 821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 823 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 824 | u64 newcsum = 0; |
| 825 | u32 *sb32 = (u32*)sb; |
| 826 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | unsigned int disk_csum, csum; |
| 828 | |
| 829 | disk_csum = sb->sb_csum; |
| 830 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 831 | |
| 832 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 833 | newcsum += sb32[i]; |
| 834 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 835 | |
| 836 | |
| 837 | #ifdef CONFIG_ALPHA |
| 838 | /* This used to use csum_partial, which was wrong for several |
| 839 | * reasons including that different results are returned on |
| 840 | * different architectures. It isn't critical that we get exactly |
| 841 | * the same return value as before (we always csum_fold before |
| 842 | * testing, and that removes any differences). However as we |
| 843 | * know that csum_partial always returned a 16bit value on |
| 844 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 845 | */ |
| 846 | sb->sb_csum = md_csum_fold(disk_csum); |
| 847 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 849 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | return csum; |
| 851 | } |
| 852 | |
| 853 | |
| 854 | /* |
| 855 | * Handle superblock details. |
| 856 | * We want to be able to handle multiple superblock formats |
| 857 | * so we have a common interface to them all, and an array of |
| 858 | * different handlers. |
| 859 | * We rely on user-space to write the initial superblock, and support |
| 860 | * reading and updating of superblocks. |
| 861 | * Interface methods are: |
| 862 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 863 | * loads and validates a superblock on dev. |
| 864 | * if refdev != NULL, compare superblocks on both devices |
| 865 | * Return: |
| 866 | * 0 - dev has a superblock that is compatible with refdev |
| 867 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 868 | * so dev should be used as the refdev in future |
| 869 | * -EINVAL superblock incompatible or invalid |
| 870 | * -othererror e.g. -EIO |
| 871 | * |
| 872 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 873 | * Verify that dev is acceptable into mddev. |
| 874 | * The first time, mddev->raid_disks will be 0, and data from |
| 875 | * dev should be merged in. Subsequent calls check that dev |
| 876 | * is new enough. Return 0 or -EINVAL |
| 877 | * |
| 878 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 879 | * Update the superblock for rdev with data in mddev |
| 880 | * This does not write to disc. |
| 881 | * |
| 882 | */ |
| 883 | |
| 884 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 885 | char *name; |
| 886 | struct module *owner; |
| 887 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 888 | int minor_version); |
| 889 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 890 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 891 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 892 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | }; |
| 894 | |
| 895 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 896 | * Check that the given mddev has no bitmap. |
| 897 | * |
| 898 | * This function is called from the run method of all personalities that do not |
| 899 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 900 | * has a bitmap. Otherwise, it returns 0. |
| 901 | * |
| 902 | */ |
| 903 | int md_check_no_bitmap(mddev_t *mddev) |
| 904 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 905 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 906 | return 0; |
| 907 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 908 | mdname(mddev), mddev->pers->name); |
| 909 | return 1; |
| 910 | } |
| 911 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 912 | |
| 913 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | * load_super for 0.90.0 |
| 915 | */ |
| 916 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 917 | { |
| 918 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 919 | mdp_super_t *sb; |
| 920 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
| 922 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 923 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | * it's at the end of the disk. |
| 925 | * |
| 926 | * It also happens to be a multiple of 4Kb. |
| 927 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 928 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 930 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | if (ret) return ret; |
| 932 | |
| 933 | ret = -EINVAL; |
| 934 | |
| 935 | bdevname(rdev->bdev, b); |
| 936 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 937 | |
| 938 | if (sb->md_magic != MD_SB_MAGIC) { |
| 939 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 940 | b); |
| 941 | goto abort; |
| 942 | } |
| 943 | |
| 944 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 945 | sb->minor_version < 90 || |
| 946 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 948 | sb->major_version, sb->minor_version, |
| 949 | b); |
| 950 | goto abort; |
| 951 | } |
| 952 | |
| 953 | if (sb->raid_disks <= 0) |
| 954 | goto abort; |
| 955 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 956 | 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] | 957 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 958 | b); |
| 959 | goto abort; |
| 960 | } |
| 961 | |
| 962 | rdev->preferred_minor = sb->md_minor; |
| 963 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 964 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
| 966 | if (sb->level == LEVEL_MULTIPATH) |
| 967 | rdev->desc_nr = -1; |
| 968 | else |
| 969 | rdev->desc_nr = sb->this_disk.number; |
| 970 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 971 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 973 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | __u64 ev1, ev2; |
| 975 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 976 | if (!uuid_equal(refsb, sb)) { |
| 977 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 978 | b, bdevname(refdev->bdev,b2)); |
| 979 | goto abort; |
| 980 | } |
| 981 | if (!sb_equal(refsb, sb)) { |
| 982 | printk(KERN_WARNING "md: %s has same UUID" |
| 983 | " but different superblock to %s\n", |
| 984 | b, bdevname(refdev->bdev, b2)); |
| 985 | goto abort; |
| 986 | } |
| 987 | ev1 = md_event(sb); |
| 988 | ev2 = md_event(refsb); |
| 989 | if (ev1 > ev2) |
| 990 | ret = 1; |
| 991 | else |
| 992 | ret = 0; |
| 993 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 994 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 996 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 997 | /* "this cannot possibly happen" ... */ |
| 998 | ret = -EINVAL; |
| 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | abort: |
| 1001 | return ret; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * validate_super for 0.90.0 |
| 1006 | */ |
| 1007 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1008 | { |
| 1009 | mdp_disk_t *desc; |
| 1010 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1011 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1013 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1014 | clear_bit(Faulty, &rdev->flags); |
| 1015 | clear_bit(In_sync, &rdev->flags); |
| 1016 | clear_bit(WriteMostly, &rdev->flags); |
| 1017 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | if (mddev->raid_disks == 0) { |
| 1020 | mddev->major_version = 0; |
| 1021 | mddev->minor_version = sb->minor_version; |
| 1022 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1023 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1024 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | mddev->ctime = sb->ctime; |
| 1026 | mddev->utime = sb->utime; |
| 1027 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1028 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | mddev->layout = sb->layout; |
| 1030 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1031 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1032 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1033 | mddev->bitmap_info.offset = 0; |
| 1034 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1036 | if (mddev->minor_version >= 91) { |
| 1037 | mddev->reshape_position = sb->reshape_position; |
| 1038 | mddev->delta_disks = sb->delta_disks; |
| 1039 | mddev->new_level = sb->new_level; |
| 1040 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1041 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1042 | } else { |
| 1043 | mddev->reshape_position = MaxSector; |
| 1044 | mddev->delta_disks = 0; |
| 1045 | mddev->new_level = mddev->level; |
| 1046 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1047 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1051 | mddev->recovery_cp = MaxSector; |
| 1052 | else { |
| 1053 | if (sb->events_hi == sb->cp_events_hi && |
| 1054 | sb->events_lo == sb->cp_events_lo) { |
| 1055 | mddev->recovery_cp = sb->recovery_cp; |
| 1056 | } else |
| 1057 | mddev->recovery_cp = 0; |
| 1058 | } |
| 1059 | |
| 1060 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1061 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1062 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1063 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1064 | |
| 1065 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1066 | |
| 1067 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1068 | mddev->bitmap_info.file == NULL) |
| 1069 | mddev->bitmap_info.offset = |
| 1070 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1071 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1072 | } else if (mddev->pers == NULL) { |
| 1073 | /* Insist on good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | ++ev1; |
| 1075 | if (ev1 < mddev->events) |
| 1076 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1077 | } else if (mddev->bitmap) { |
| 1078 | /* if adding to array with a bitmap, then we can accept an |
| 1079 | * older device ... but not too old. |
| 1080 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1081 | if (ev1 < mddev->bitmap->events_cleared) |
| 1082 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1083 | } else { |
| 1084 | if (ev1 < mddev->events) |
| 1085 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1086 | return 0; |
| 1087 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | desc = sb->disks + rdev->desc_nr; |
| 1091 | |
| 1092 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1093 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1094 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1095 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1096 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1098 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1099 | /* active but not in sync implies recovery up to |
| 1100 | * reshape position. We don't know exactly where |
| 1101 | * that is, so set to zero for now */ |
| 1102 | if (mddev->minor_version >= 91) { |
| 1103 | rdev->recovery_offset = 0; |
| 1104 | rdev->raid_disk = desc->raid_disk; |
| 1105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1107 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1108 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1109 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1110 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | /* |
| 1115 | * sync_super for 0.90.0 |
| 1116 | */ |
| 1117 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1118 | { |
| 1119 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | mdk_rdev_t *rdev2; |
| 1121 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
| 1124 | /* make rdev->sb match mddev data.. |
| 1125 | * |
| 1126 | * 1/ zero out disks |
| 1127 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1128 | * 3/ any empty disks < next_spare become removed |
| 1129 | * |
| 1130 | * disks[0] gets initialised to REMOVED because |
| 1131 | * we cannot be sure from other fields if it has |
| 1132 | * been initialised or not. |
| 1133 | */ |
| 1134 | int i; |
| 1135 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1136 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1137 | rdev->sb_size = MD_SB_BYTES; |
| 1138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1140 | |
| 1141 | memset(sb, 0, sizeof(*sb)); |
| 1142 | |
| 1143 | sb->md_magic = MD_SB_MAGIC; |
| 1144 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | sb->patch_version = mddev->patch_version; |
| 1146 | sb->gvalid_words = 0; /* ignored */ |
| 1147 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1148 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1149 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1150 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1151 | |
| 1152 | sb->ctime = mddev->ctime; |
| 1153 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1154 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | sb->raid_disks = mddev->raid_disks; |
| 1156 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1157 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | sb->utime = mddev->utime; |
| 1159 | sb->state = 0; |
| 1160 | sb->events_hi = (mddev->events>>32); |
| 1161 | sb->events_lo = (u32)mddev->events; |
| 1162 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1163 | if (mddev->reshape_position == MaxSector) |
| 1164 | sb->minor_version = 90; |
| 1165 | else { |
| 1166 | sb->minor_version = 91; |
| 1167 | sb->reshape_position = mddev->reshape_position; |
| 1168 | sb->new_level = mddev->new_level; |
| 1169 | sb->delta_disks = mddev->delta_disks; |
| 1170 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1171 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1172 | } |
| 1173 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | if (mddev->in_sync) |
| 1175 | { |
| 1176 | sb->recovery_cp = mddev->recovery_cp; |
| 1177 | sb->cp_events_hi = (mddev->events>>32); |
| 1178 | sb->cp_events_lo = (u32)mddev->events; |
| 1179 | if (mddev->recovery_cp == MaxSector) |
| 1180 | sb->state = (1<< MD_SB_CLEAN); |
| 1181 | } else |
| 1182 | sb->recovery_cp = 0; |
| 1183 | |
| 1184 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1185 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1187 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1188 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1191 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1193 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1194 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1195 | |
| 1196 | if (rdev2->raid_disk >= 0 && |
| 1197 | sb->minor_version >= 91) |
| 1198 | /* we have nowhere to store the recovery_offset, |
| 1199 | * but if it is not below the reshape_position, |
| 1200 | * we can piggy-back on that. |
| 1201 | */ |
| 1202 | is_active = 1; |
| 1203 | if (rdev2->raid_disk < 0 || |
| 1204 | test_bit(Faulty, &rdev2->flags)) |
| 1205 | is_active = 0; |
| 1206 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1207 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1209 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1210 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | d = &sb->disks[rdev2->desc_nr]; |
| 1212 | nr_disks++; |
| 1213 | d->number = rdev2->desc_nr; |
| 1214 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1215 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1216 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | d->raid_disk = rdev2->raid_disk; |
| 1218 | else |
| 1219 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1220 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1222 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1224 | if (test_bit(In_sync, &rdev2->flags)) |
| 1225 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | active++; |
| 1227 | working++; |
| 1228 | } else { |
| 1229 | d->state = 0; |
| 1230 | spare++; |
| 1231 | working++; |
| 1232 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1233 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1234 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1237 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1238 | mdp_disk_t *d = &sb->disks[i]; |
| 1239 | if (d->state == 0 && d->number == 0) { |
| 1240 | d->number = i; |
| 1241 | d->raid_disk = i; |
| 1242 | d->state = (1<<MD_DISK_REMOVED); |
| 1243 | d->state |= (1<<MD_DISK_FAULTY); |
| 1244 | failed++; |
| 1245 | } |
| 1246 | } |
| 1247 | sb->nr_disks = nr_disks; |
| 1248 | sb->active_disks = active; |
| 1249 | sb->working_disks = working; |
| 1250 | sb->failed_disks = failed; |
| 1251 | sb->spare_disks = spare; |
| 1252 | |
| 1253 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1254 | sb->sb_csum = calc_sb_csum(sb); |
| 1255 | } |
| 1256 | |
| 1257 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1258 | * rdev_size_change for 0.90.0 |
| 1259 | */ |
| 1260 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1261 | 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] | 1262 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1263 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1264 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1265 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1266 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1267 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1268 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1269 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1270 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1271 | rdev->sb_page); |
| 1272 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1273 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | * version 1 superblock |
| 1279 | */ |
| 1280 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1281 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1283 | __le32 disk_csum; |
| 1284 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | unsigned long long newcsum; |
| 1286 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1287 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | int i; |
| 1289 | |
| 1290 | disk_csum = sb->sb_csum; |
| 1291 | sb->sb_csum = 0; |
| 1292 | newcsum = 0; |
| 1293 | for (i=0; size>=4; size -= 4 ) |
| 1294 | newcsum += le32_to_cpu(*isuper++); |
| 1295 | |
| 1296 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1297 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | |
| 1299 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1300 | sb->sb_csum = disk_csum; |
| 1301 | return cpu_to_le32(csum); |
| 1302 | } |
| 1303 | |
| 1304 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1305 | { |
| 1306 | struct mdp_superblock_1 *sb; |
| 1307 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1308 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1310 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | |
| 1312 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1313 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | * It is always aligned to a 4K boundary and |
| 1315 | * depeding on minor_version, it can be: |
| 1316 | * 0: At least 8K, but less than 12K, from end of device |
| 1317 | * 1: At start of device |
| 1318 | * 2: 4K from start of device. |
| 1319 | */ |
| 1320 | switch(minor_version) { |
| 1321 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1322 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1323 | sb_start -= 8*2; |
| 1324 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | break; |
| 1326 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1327 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | break; |
| 1329 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1330 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | break; |
| 1332 | default: |
| 1333 | return -EINVAL; |
| 1334 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1335 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1337 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1338 | * and it is safe to read 4k, so we do that |
| 1339 | */ |
| 1340 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | if (ret) return ret; |
| 1342 | |
| 1343 | |
| 1344 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1345 | |
| 1346 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1347 | sb->major_version != cpu_to_le32(1) || |
| 1348 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1349 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1350 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | return -EINVAL; |
| 1352 | |
| 1353 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1354 | printk("md: invalid superblock checksum on %s\n", |
| 1355 | bdevname(rdev->bdev,b)); |
| 1356 | return -EINVAL; |
| 1357 | } |
| 1358 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1359 | printk("md: data_size too small on %s\n", |
| 1360 | bdevname(rdev->bdev,b)); |
| 1361 | return -EINVAL; |
| 1362 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | rdev->preferred_minor = 0xffff; |
| 1365 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1366 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1368 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1369 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1370 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1371 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1372 | |
| 1373 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1374 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1375 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1376 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1377 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1378 | rdev->desc_nr = -1; |
| 1379 | else |
| 1380 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1381 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1382 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1383 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1384 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | __u64 ev1, ev2; |
| 1386 | struct mdp_superblock_1 *refsb = |
| 1387 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1388 | |
| 1389 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1390 | sb->level != refsb->level || |
| 1391 | sb->layout != refsb->layout || |
| 1392 | sb->chunksize != refsb->chunksize) { |
| 1393 | printk(KERN_WARNING "md: %s has strangely different" |
| 1394 | " superblock to %s\n", |
| 1395 | bdevname(rdev->bdev,b), |
| 1396 | bdevname(refdev->bdev,b2)); |
| 1397 | return -EINVAL; |
| 1398 | } |
| 1399 | ev1 = le64_to_cpu(sb->events); |
| 1400 | ev2 = le64_to_cpu(refsb->events); |
| 1401 | |
| 1402 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1403 | ret = 1; |
| 1404 | else |
| 1405 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1407 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1408 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1409 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1411 | rdev->sectors = rdev->sb_start; |
| 1412 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1414 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1415 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1416 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1417 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1421 | { |
| 1422 | 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] | 1423 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1425 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1426 | clear_bit(Faulty, &rdev->flags); |
| 1427 | clear_bit(In_sync, &rdev->flags); |
| 1428 | clear_bit(WriteMostly, &rdev->flags); |
| 1429 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | if (mddev->raid_disks == 0) { |
| 1432 | mddev->major_version = 1; |
| 1433 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1434 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1435 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1437 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1438 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1439 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | mddev->layout = le32_to_cpu(sb->layout); |
| 1441 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1442 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1443 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1444 | mddev->bitmap_info.offset = 0; |
| 1445 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
| 1447 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1448 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1449 | |
| 1450 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1451 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1452 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1453 | mddev->bitmap_info.file == NULL ) |
| 1454 | mddev->bitmap_info.offset = |
| 1455 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1456 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1457 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1458 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1459 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1460 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1461 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1462 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1463 | } else { |
| 1464 | mddev->reshape_position = MaxSector; |
| 1465 | mddev->delta_disks = 0; |
| 1466 | mddev->new_level = mddev->level; |
| 1467 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1468 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1469 | } |
| 1470 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1471 | } else if (mddev->pers == NULL) { |
| 1472 | /* Insist of good event counter while assembling */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | ++ev1; |
| 1474 | if (ev1 < mddev->events) |
| 1475 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1476 | } else if (mddev->bitmap) { |
| 1477 | /* If adding to array with a bitmap, then we can accept an |
| 1478 | * older device, but not too old. |
| 1479 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1480 | if (ev1 < mddev->bitmap->events_cleared) |
| 1481 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1482 | } else { |
| 1483 | if (ev1 < mddev->events) |
| 1484 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1485 | return 0; |
| 1486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1488 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1489 | if (rdev->desc_nr < 0 || |
| 1490 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1491 | role = 0xffff; |
| 1492 | rdev->desc_nr = -1; |
| 1493 | } else |
| 1494 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | switch(role) { |
| 1496 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | break; |
| 1498 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1499 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | break; |
| 1501 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1502 | if ((le32_to_cpu(sb->feature_map) & |
| 1503 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1504 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1505 | else |
| 1506 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | rdev->raid_disk = role; |
| 1508 | break; |
| 1509 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1510 | if (sb->devflags & WriteMostly1) |
| 1511 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1512 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1513 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | return 0; |
| 1516 | } |
| 1517 | |
| 1518 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1519 | { |
| 1520 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | mdk_rdev_t *rdev2; |
| 1522 | int max_dev, i; |
| 1523 | /* make rdev->sb match mddev and rdev data. */ |
| 1524 | |
| 1525 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1526 | |
| 1527 | sb->feature_map = 0; |
| 1528 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1529 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1531 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1532 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1533 | |
| 1534 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1535 | sb->events = cpu_to_le64(mddev->events); |
| 1536 | if (mddev->in_sync) |
| 1537 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1538 | else |
| 1539 | sb->resync_offset = cpu_to_le64(0); |
| 1540 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1541 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1542 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1543 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1544 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1545 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1546 | sb->level = cpu_to_le32(mddev->level); |
| 1547 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1548 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1549 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1550 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1551 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1552 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1553 | |
| 1554 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1555 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1556 | sb->feature_map |= |
| 1557 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1558 | sb->recovery_offset = |
| 1559 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1562 | if (mddev->reshape_position != MaxSector) { |
| 1563 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1564 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1565 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1566 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1567 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1568 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1569 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1572 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | if (rdev2->desc_nr+1 > max_dev) |
| 1574 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1575 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1576 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1577 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1578 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1579 | rdev->sb_size = max_dev * 2 + 256; |
| 1580 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1581 | if (rdev->sb_size & bmask) |
| 1582 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | for (i=0; i<max_dev;i++) |
| 1585 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1586 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1587 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1589 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1591 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1593 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1594 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | else |
| 1596 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1597 | } |
| 1598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1600 | } |
| 1601 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1602 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1603 | super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1604 | { |
| 1605 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1606 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1607 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1608 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1609 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1610 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1611 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1612 | max_sectors -= rdev->data_offset; |
| 1613 | if (!num_sectors || num_sectors > max_sectors) |
| 1614 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1615 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1616 | /* minor version 0 with bitmap we can't move */ |
| 1617 | return 0; |
| 1618 | } else { |
| 1619 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1620 | sector_t sb_start; |
| 1621 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1622 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1623 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1624 | if (!num_sectors || num_sectors > max_sectors) |
| 1625 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1626 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1627 | } |
| 1628 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1629 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1630 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1631 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1632 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1633 | rdev->sb_page); |
| 1634 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1635 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1636 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1638 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | [0] = { |
| 1640 | .name = "0.90.0", |
| 1641 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1642 | .load_super = super_90_load, |
| 1643 | .validate_super = super_90_validate, |
| 1644 | .sync_super = super_90_sync, |
| 1645 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | }, |
| 1647 | [1] = { |
| 1648 | .name = "md-1", |
| 1649 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1650 | .load_super = super_1_load, |
| 1651 | .validate_super = super_1_validate, |
| 1652 | .sync_super = super_1_sync, |
| 1653 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | }, |
| 1655 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | |
| 1657 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1658 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1659 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1661 | rcu_read_lock(); |
| 1662 | rdev_for_each_rcu(rdev, mddev1) |
| 1663 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1664 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1665 | rdev2->bdev->bd_contains) { |
| 1666 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1667 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1668 | } |
| 1669 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | return 0; |
| 1671 | } |
| 1672 | |
| 1673 | static LIST_HEAD(pending_raid_disks); |
| 1674 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1675 | /* |
| 1676 | * Try to register data integrity profile for an mddev |
| 1677 | * |
| 1678 | * This is called when an array is started and after a disk has been kicked |
| 1679 | * from the array. It only succeeds if all working and active component devices |
| 1680 | * are integrity capable with matching profiles. |
| 1681 | */ |
| 1682 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1683 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1684 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1685 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1686 | if (list_empty(&mddev->disks)) |
| 1687 | return 0; /* nothing to do */ |
| 1688 | if (blk_get_integrity(mddev->gendisk)) |
| 1689 | return 0; /* already registered */ |
| 1690 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1691 | /* skip spares and non-functional disks */ |
| 1692 | if (test_bit(Faulty, &rdev->flags)) |
| 1693 | continue; |
| 1694 | if (rdev->raid_disk < 0) |
| 1695 | continue; |
| 1696 | /* |
| 1697 | * If at least one rdev is not integrity capable, we can not |
| 1698 | * enable data integrity for the md device. |
| 1699 | */ |
| 1700 | if (!bdev_get_integrity(rdev->bdev)) |
| 1701 | return -EINVAL; |
| 1702 | if (!reference) { |
| 1703 | /* Use the first rdev as the reference */ |
| 1704 | reference = rdev; |
| 1705 | continue; |
| 1706 | } |
| 1707 | /* does this rdev's profile match the reference profile? */ |
| 1708 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1709 | rdev->bdev->bd_disk) < 0) |
| 1710 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1711 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1712 | /* |
| 1713 | * All component devices are integrity capable and have matching |
| 1714 | * profiles, register the common profile for the md device. |
| 1715 | */ |
| 1716 | if (blk_integrity_register(mddev->gendisk, |
| 1717 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1718 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1719 | mdname(mddev)); |
| 1720 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1721 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1722 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1723 | mdname(mddev)); |
| 1724 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1725 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1726 | EXPORT_SYMBOL(md_integrity_register); |
| 1727 | |
| 1728 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1729 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1730 | { |
| 1731 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1732 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1733 | |
| 1734 | if (!bi_mddev) /* nothing to do */ |
| 1735 | return; |
| 1736 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1737 | return; |
| 1738 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1739 | rdev->bdev->bd_disk) >= 0) |
| 1740 | return; |
| 1741 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1742 | blk_integrity_unregister(mddev->gendisk); |
| 1743 | } |
| 1744 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1747 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1748 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1749 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1750 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1751 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
| 1753 | if (rdev->mddev) { |
| 1754 | MD_BUG(); |
| 1755 | return -EINVAL; |
| 1756 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1757 | |
| 1758 | /* prevent duplicates */ |
| 1759 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1760 | return -EEXIST; |
| 1761 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1762 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1763 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1764 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1765 | if (mddev->pers) { |
| 1766 | /* Cannot change size, so fail |
| 1767 | * If mddev->level <= 0, then we don't care |
| 1768 | * about aligning sizes (e.g. linear) |
| 1769 | */ |
| 1770 | if (mddev->level > 0) |
| 1771 | return -ENOSPC; |
| 1772 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1773 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
| 1776 | /* Verify rdev->desc_nr is unique. |
| 1777 | * If it is -1, assign a free number, else |
| 1778 | * check number is not in use |
| 1779 | */ |
| 1780 | if (rdev->desc_nr < 0) { |
| 1781 | int choice = 0; |
| 1782 | if (mddev->pers) choice = mddev->raid_disks; |
| 1783 | while (find_rdev_nr(mddev, choice)) |
| 1784 | choice++; |
| 1785 | rdev->desc_nr = choice; |
| 1786 | } else { |
| 1787 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1788 | return -EBUSY; |
| 1789 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1790 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1791 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1792 | mdname(mddev), mddev->max_disks); |
| 1793 | return -EBUSY; |
| 1794 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1795 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1796 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1797 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1800 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1801 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1802 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1803 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1804 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1805 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1806 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1807 | kobject_del(&rdev->kobj); |
| 1808 | goto fail; |
| 1809 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1810 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state"); |
| 1811 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1812 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1813 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1814 | |
| 1815 | /* May as well allow recovery to be retried once */ |
| 1816 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1819 | |
| 1820 | fail: |
| 1821 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1822 | b, mdname(mddev)); |
| 1823 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | } |
| 1825 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1826 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1827 | { |
| 1828 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1829 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1830 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1834 | { |
| 1835 | char b[BDEVNAME_SIZE]; |
| 1836 | if (!rdev->mddev) { |
| 1837 | MD_BUG(); |
| 1838 | return; |
| 1839 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1840 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1841 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1843 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1844 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1845 | sysfs_put(rdev->sysfs_state); |
| 1846 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1847 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1848 | * writing to 'remove' to "dev/state". We also need |
| 1849 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1850 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1851 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1852 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1853 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1854 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | * prevent the device from being mounted, repartitioned or |
| 1859 | * otherwise reused by a RAID array (or any other kernel |
| 1860 | * subsystem), by bd_claiming the device. |
| 1861 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1862 | 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] | 1863 | { |
| 1864 | int err = 0; |
| 1865 | struct block_device *bdev; |
| 1866 | char b[BDEVNAME_SIZE]; |
| 1867 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1868 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | if (IS_ERR(bdev)) { |
| 1870 | printk(KERN_ERR "md: could not open %s.\n", |
| 1871 | __bdevname(dev, b)); |
| 1872 | return PTR_ERR(bdev); |
| 1873 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1874 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | if (err) { |
| 1876 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1877 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1878 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | return err; |
| 1880 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1881 | if (!shared) |
| 1882 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | rdev->bdev = bdev; |
| 1884 | return err; |
| 1885 | } |
| 1886 | |
| 1887 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1888 | { |
| 1889 | struct block_device *bdev = rdev->bdev; |
| 1890 | rdev->bdev = NULL; |
| 1891 | if (!bdev) |
| 1892 | MD_BUG(); |
| 1893 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1894 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | void md_autodetect_dev(dev_t dev); |
| 1898 | |
| 1899 | static void export_rdev(mdk_rdev_t * rdev) |
| 1900 | { |
| 1901 | char b[BDEVNAME_SIZE]; |
| 1902 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1903 | bdevname(rdev->bdev,b)); |
| 1904 | if (rdev->mddev) |
| 1905 | MD_BUG(); |
| 1906 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1908 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1909 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | #endif |
| 1911 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1912 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1916 | { |
| 1917 | unbind_rdev_from_array(rdev); |
| 1918 | export_rdev(rdev); |
| 1919 | } |
| 1920 | |
| 1921 | static void export_array(mddev_t *mddev) |
| 1922 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1923 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1925 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | if (!rdev->mddev) { |
| 1927 | MD_BUG(); |
| 1928 | continue; |
| 1929 | } |
| 1930 | kick_rdev_from_array(rdev); |
| 1931 | } |
| 1932 | if (!list_empty(&mddev->disks)) |
| 1933 | MD_BUG(); |
| 1934 | mddev->raid_disks = 0; |
| 1935 | mddev->major_version = 0; |
| 1936 | } |
| 1937 | |
| 1938 | static void print_desc(mdp_disk_t *desc) |
| 1939 | { |
| 1940 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1941 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1942 | } |
| 1943 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1944 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | { |
| 1946 | int i; |
| 1947 | |
| 1948 | printk(KERN_INFO |
| 1949 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1950 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1951 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1952 | sb->ctime); |
| 1953 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1954 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1955 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1956 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1957 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1958 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1959 | sb->failed_disks, sb->spare_disks, |
| 1960 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1961 | |
| 1962 | printk(KERN_INFO); |
| 1963 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1964 | mdp_disk_t *desc; |
| 1965 | |
| 1966 | desc = sb->disks + i; |
| 1967 | if (desc->number || desc->major || desc->minor || |
| 1968 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1969 | printk(" D %2d: ", i); |
| 1970 | print_desc(desc); |
| 1971 | } |
| 1972 | } |
| 1973 | printk(KERN_INFO "md: THIS: "); |
| 1974 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | } |
| 1976 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1977 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1978 | { |
| 1979 | __u8 *uuid; |
| 1980 | |
| 1981 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1982 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1983 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1984 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1985 | le32_to_cpu(sb->major_version), |
| 1986 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1987 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1988 | sb->set_name, |
| 1989 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1990 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1991 | |
| 1992 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1993 | printk(KERN_INFO |
| 1994 | "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] | 1995 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1996 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1997 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 1998 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1999 | le32_to_cpu(sb->level), |
| 2000 | (unsigned long long)le64_to_cpu(sb->size), |
| 2001 | le32_to_cpu(sb->raid_disks), |
| 2002 | le32_to_cpu(sb->layout), |
| 2003 | le32_to_cpu(sb->chunksize), |
| 2004 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2005 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2006 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2007 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2008 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2009 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2010 | sb->devflags, |
| 2011 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2012 | (unsigned long long)le64_to_cpu(sb->events), |
| 2013 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2014 | le32_to_cpu(sb->sb_csum), |
| 2015 | le32_to_cpu(sb->max_dev) |
| 2016 | ); |
| 2017 | } |
| 2018 | |
| 2019 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | { |
| 2021 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2022 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2023 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2024 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2025 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2027 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2028 | switch (major_version) { |
| 2029 | case 0: |
| 2030 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2031 | break; |
| 2032 | case 1: |
| 2033 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2034 | break; |
| 2035 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | } else |
| 2037 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2038 | } |
| 2039 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2040 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2042 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | mdk_rdev_t *rdev; |
| 2044 | mddev_t *mddev; |
| 2045 | char b[BDEVNAME_SIZE]; |
| 2046 | |
| 2047 | printk("\n"); |
| 2048 | printk("md: **********************************\n"); |
| 2049 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2050 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2051 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2053 | if (mddev->bitmap) |
| 2054 | bitmap_print_sb(mddev->bitmap); |
| 2055 | else |
| 2056 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2057 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2059 | printk("\n"); |
| 2060 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2061 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2062 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | } |
| 2064 | printk("md: **********************************\n"); |
| 2065 | printk("\n"); |
| 2066 | } |
| 2067 | |
| 2068 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2069 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2071 | /* Update each superblock (in-memory image), but |
| 2072 | * if we are allowed to, skip spares which already |
| 2073 | * have the right event counter, or have one earlier |
| 2074 | * (which would mean they aren't being marked as dirty |
| 2075 | * with the rest of the array) |
| 2076 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2079 | /* First make sure individual recovery_offsets are correct */ |
| 2080 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2081 | if (rdev->raid_disk >= 0 && |
| 2082 | !test_bit(In_sync, &rdev->flags) && |
| 2083 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2084 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2085 | |
| 2086 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2087 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2088 | if (rdev->sb_events == mddev->events || |
| 2089 | (nospares && |
| 2090 | rdev->raid_disk < 0 && |
| 2091 | (rdev->sb_events&1)==0 && |
| 2092 | rdev->sb_events+1 == mddev->events)) { |
| 2093 | /* Don't update this superblock */ |
| 2094 | rdev->sb_loaded = 2; |
| 2095 | } else { |
| 2096 | super_types[mddev->major_version]. |
| 2097 | sync_super(mddev, rdev); |
| 2098 | rdev->sb_loaded = 1; |
| 2099 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | } |
| 2101 | } |
| 2102 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2103 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2106 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2107 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2109 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2110 | if (mddev->external) |
| 2111 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2113 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2114 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2115 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2116 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2117 | force_change = 1; |
| 2118 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2119 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2120 | * though if events isn't the right even/odd, we will have to do |
| 2121 | * spares after all |
| 2122 | */ |
| 2123 | nospares = 1; |
| 2124 | if (force_change) |
| 2125 | nospares = 0; |
| 2126 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2127 | /* If the array is degraded, then skipping spares is both |
| 2128 | * dangerous and fairly pointless. |
| 2129 | * Dangerous because a device that was removed from the array |
| 2130 | * might have a event_count that still looks up-to-date, |
| 2131 | * so it can be re-added without a resync. |
| 2132 | * Pointless because if there are any spares to skip, |
| 2133 | * then a recovery will happen and soon that array won't |
| 2134 | * be degraded any more and the spare can go back to sleep then. |
| 2135 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2136 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2137 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2138 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2139 | |
| 2140 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2141 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2142 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2143 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | 1031be7 | 2007-01-26 00:57:02 -0800 | [diff] [blame] | 2144 | && (mddev->events & 1) |
| 2145 | && mddev->events != 1) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2146 | mddev->events--; |
| 2147 | else { |
| 2148 | /* otherwise we have to go forward and ... */ |
| 2149 | mddev->events ++; |
| 2150 | if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */ |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2151 | /* .. if the array isn't clean, an 'even' event must also go |
| 2152 | * to spares. */ |
| 2153 | if ((mddev->events&1)==0) |
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 | } else { |
NeilBrown | 51d5668 | 2009-08-13 09:54:02 +1000 | [diff] [blame] | 2156 | /* otherwise an 'odd' event must go to spares */ |
| 2157 | if ((mddev->events&1)) |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2158 | nospares = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2159 | } |
| 2160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | |
| 2162 | if (!mddev->events) { |
| 2163 | /* |
| 2164 | * oops, this 64-bit counter should never wrap. |
| 2165 | * Either we are in around ~1 trillion A.C., assuming |
| 2166 | * 1 reboot per second, or we have a bug: |
| 2167 | */ |
| 2168 | MD_BUG(); |
| 2169 | mddev->events --; |
| 2170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | |
| 2172 | /* |
| 2173 | * do not write anything to disk if using |
| 2174 | * nonpersistent superblocks |
| 2175 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2176 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2177 | if (!mddev->external) |
| 2178 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2179 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2180 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2181 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2183 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2184 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2185 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | |
| 2187 | dprintk(KERN_INFO |
| 2188 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2189 | mdname(mddev),mddev->in_sync); |
| 2190 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2191 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2192 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | char b[BDEVNAME_SIZE]; |
| 2194 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2195 | if (rdev->sb_loaded != 1) |
| 2196 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2197 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | dprintk("(skipping faulty "); |
| 2199 | |
| 2200 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2201 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2202 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2203 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2204 | rdev->sb_page); |
| 2205 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2206 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2207 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2208 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | } else |
| 2211 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2212 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | /* only need to write one superblock... */ |
| 2214 | break; |
| 2215 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2216 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2217 | /* 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] | 2218 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2219 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2220 | if (mddev->in_sync != sync_req || |
| 2221 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2222 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2223 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2224 | goto repeat; |
| 2225 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2226 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2227 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2228 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2229 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2230 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2234 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2235 | * We want to accept with case. For this we use cmd_match. |
| 2236 | */ |
| 2237 | static int cmd_match(const char *cmd, const char *str) |
| 2238 | { |
| 2239 | /* See if cmd, written into a sysfs file, matches |
| 2240 | * str. They must either be the same, or cmd can |
| 2241 | * have a trailing newline |
| 2242 | */ |
| 2243 | while (*cmd && *str && *cmd == *str) { |
| 2244 | cmd++; |
| 2245 | str++; |
| 2246 | } |
| 2247 | if (*cmd == '\n') |
| 2248 | cmd++; |
| 2249 | if (*str || *cmd) |
| 2250 | return 0; |
| 2251 | return 1; |
| 2252 | } |
| 2253 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2254 | struct rdev_sysfs_entry { |
| 2255 | struct attribute attr; |
| 2256 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2257 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2258 | }; |
| 2259 | |
| 2260 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2261 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2262 | { |
| 2263 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2264 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2265 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2266 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2267 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2268 | sep = ","; |
| 2269 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2270 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2271 | len += sprintf(page+len, "%sin_sync",sep); |
| 2272 | sep = ","; |
| 2273 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2274 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2275 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2276 | sep = ","; |
| 2277 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2278 | if (test_bit(Blocked, &rdev->flags)) { |
| 2279 | len += sprintf(page+len, "%sblocked", sep); |
| 2280 | sep = ","; |
| 2281 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2282 | if (!test_bit(Faulty, &rdev->flags) && |
| 2283 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2284 | len += sprintf(page+len, "%sspare", sep); |
| 2285 | sep = ","; |
| 2286 | } |
| 2287 | return len+sprintf(page+len, "\n"); |
| 2288 | } |
| 2289 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2290 | static ssize_t |
| 2291 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2292 | { |
| 2293 | /* can write |
| 2294 | * faulty - simulates and error |
| 2295 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2296 | * writemostly - sets write_mostly |
| 2297 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2298 | * blocked - sets the Blocked flag |
| 2299 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2300 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2301 | */ |
| 2302 | int err = -EINVAL; |
| 2303 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2304 | md_error(rdev->mddev, rdev); |
| 2305 | err = 0; |
| 2306 | } else if (cmd_match(buf, "remove")) { |
| 2307 | if (rdev->raid_disk >= 0) |
| 2308 | err = -EBUSY; |
| 2309 | else { |
| 2310 | mddev_t *mddev = rdev->mddev; |
| 2311 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2312 | if (mddev->pers) |
| 2313 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2314 | md_new_event(mddev); |
| 2315 | err = 0; |
| 2316 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2317 | } else if (cmd_match(buf, "writemostly")) { |
| 2318 | set_bit(WriteMostly, &rdev->flags); |
| 2319 | err = 0; |
| 2320 | } else if (cmd_match(buf, "-writemostly")) { |
| 2321 | clear_bit(WriteMostly, &rdev->flags); |
| 2322 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2323 | } else if (cmd_match(buf, "blocked")) { |
| 2324 | set_bit(Blocked, &rdev->flags); |
| 2325 | err = 0; |
| 2326 | } else if (cmd_match(buf, "-blocked")) { |
| 2327 | clear_bit(Blocked, &rdev->flags); |
| 2328 | wake_up(&rdev->blocked_wait); |
| 2329 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2330 | md_wakeup_thread(rdev->mddev->thread); |
| 2331 | |
| 2332 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2333 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2334 | set_bit(In_sync, &rdev->flags); |
| 2335 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2336 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2337 | if (!err && rdev->sysfs_state) |
| 2338 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2339 | return err ? err : len; |
| 2340 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2341 | static struct rdev_sysfs_entry rdev_state = |
| 2342 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2343 | |
| 2344 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2345 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2346 | { |
| 2347 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2348 | } |
| 2349 | |
| 2350 | static ssize_t |
| 2351 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2352 | { |
| 2353 | char *e; |
| 2354 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2355 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2356 | atomic_set(&rdev->corrected_errors, n); |
| 2357 | return len; |
| 2358 | } |
| 2359 | return -EINVAL; |
| 2360 | } |
| 2361 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2362 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2363 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2364 | static ssize_t |
| 2365 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2366 | { |
| 2367 | if (rdev->raid_disk < 0) |
| 2368 | return sprintf(page, "none\n"); |
| 2369 | else |
| 2370 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2371 | } |
| 2372 | |
| 2373 | static ssize_t |
| 2374 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2375 | { |
| 2376 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2377 | int err; |
| 2378 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2379 | int slot = simple_strtoul(buf, &e, 10); |
| 2380 | if (strncmp(buf, "none", 4)==0) |
| 2381 | slot = -1; |
| 2382 | else if (e==buf || (*e && *e!= '\n')) |
| 2383 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2384 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2385 | /* Setting 'slot' on an active array requires also |
| 2386 | * updating the 'rd%d' link, and communicating |
| 2387 | * with the personality with ->hot_*_disk. |
| 2388 | * For now we only support removing |
| 2389 | * failed/spare devices. This normally happens automatically, |
| 2390 | * but not when the metadata is externally managed. |
| 2391 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2392 | if (rdev->raid_disk == -1) |
| 2393 | return -EEXIST; |
| 2394 | /* personality does all needed checks */ |
| 2395 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2396 | return -EINVAL; |
| 2397 | err = rdev->mddev->pers-> |
| 2398 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2399 | if (err) |
| 2400 | return err; |
| 2401 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2402 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2403 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2404 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2405 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2406 | } else if (rdev->mddev->pers) { |
| 2407 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2408 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2409 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2410 | */ |
| 2411 | |
| 2412 | if (rdev->raid_disk != -1) |
| 2413 | return -EBUSY; |
| 2414 | |
| 2415 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2416 | return -EINVAL; |
| 2417 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2418 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2419 | if (rdev2->raid_disk == slot) |
| 2420 | return -EEXIST; |
| 2421 | |
| 2422 | rdev->raid_disk = slot; |
| 2423 | if (test_bit(In_sync, &rdev->flags)) |
| 2424 | rdev->saved_raid_disk = slot; |
| 2425 | else |
| 2426 | rdev->saved_raid_disk = -1; |
| 2427 | err = rdev->mddev->pers-> |
| 2428 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2429 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2430 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2431 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2432 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2433 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2434 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2435 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2436 | printk(KERN_WARNING |
| 2437 | "md: cannot register " |
| 2438 | "%s for %s\n", |
| 2439 | nm, mdname(rdev->mddev)); |
| 2440 | |
| 2441 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2442 | } else { |
| 2443 | if (slot >= rdev->mddev->raid_disks) |
| 2444 | return -ENOSPC; |
| 2445 | rdev->raid_disk = slot; |
| 2446 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2447 | clear_bit(Faulty, &rdev->flags); |
| 2448 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2449 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2450 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2451 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2452 | return len; |
| 2453 | } |
| 2454 | |
| 2455 | |
| 2456 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2457 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2458 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2459 | static ssize_t |
| 2460 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2461 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2462 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | static ssize_t |
| 2466 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2467 | { |
| 2468 | char *e; |
| 2469 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2470 | if (e==buf || (*e && *e != '\n')) |
| 2471 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2472 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2473 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2474 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2475 | /* Must set offset before size, so overlap checks |
| 2476 | * can be sane */ |
| 2477 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2478 | rdev->data_offset = offset; |
| 2479 | return len; |
| 2480 | } |
| 2481 | |
| 2482 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2483 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2484 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2485 | static ssize_t |
| 2486 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2487 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2488 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2489 | } |
| 2490 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2491 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2492 | { |
| 2493 | /* check if two start/length pairs overlap */ |
| 2494 | if (s1+l1 <= s2) |
| 2495 | return 0; |
| 2496 | if (s2+l2 <= s1) |
| 2497 | return 0; |
| 2498 | return 1; |
| 2499 | } |
| 2500 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2501 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2502 | { |
| 2503 | unsigned long long blocks; |
| 2504 | sector_t new; |
| 2505 | |
| 2506 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2507 | return -EINVAL; |
| 2508 | |
| 2509 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2510 | return -EINVAL; /* sector conversion overflow */ |
| 2511 | |
| 2512 | new = blocks * 2; |
| 2513 | if (new != blocks * 2) |
| 2514 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2515 | |
| 2516 | *sectors = new; |
| 2517 | return 0; |
| 2518 | } |
| 2519 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2520 | static ssize_t |
| 2521 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2522 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2523 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2524 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2525 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2526 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2527 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2528 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2529 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2530 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2531 | sectors = super_types[my_mddev->major_version]. |
| 2532 | rdev_size_change(rdev, sectors); |
| 2533 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2534 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2535 | } else if (!sectors) |
| 2536 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2537 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2538 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2539 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2540 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2541 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2542 | rdev->sectors = sectors; |
| 2543 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2544 | /* need to check that all other rdevs with the same ->bdev |
| 2545 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2546 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2547 | * we have to change it back, we will have the lock again. |
| 2548 | */ |
| 2549 | mddev_t *mddev; |
| 2550 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2551 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2552 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2553 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2554 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2555 | mdk_rdev_t *rdev2; |
| 2556 | |
| 2557 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2558 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2559 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2560 | (rdev->bdev == rdev2->bdev && |
| 2561 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2562 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2563 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2564 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2565 | overlap = 1; |
| 2566 | break; |
| 2567 | } |
| 2568 | mddev_unlock(mddev); |
| 2569 | if (overlap) { |
| 2570 | mddev_put(mddev); |
| 2571 | break; |
| 2572 | } |
| 2573 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2574 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2575 | if (overlap) { |
| 2576 | /* Someone else could have slipped in a size |
| 2577 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2578 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2579 | * safe, and trust userspace not to race with |
| 2580 | * itself |
| 2581 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2582 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2583 | return -EBUSY; |
| 2584 | } |
| 2585 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2586 | return len; |
| 2587 | } |
| 2588 | |
| 2589 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2590 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2591 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2592 | |
| 2593 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2594 | { |
| 2595 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2596 | |
| 2597 | if (test_bit(In_sync, &rdev->flags) || |
| 2598 | recovery_start == MaxSector) |
| 2599 | return sprintf(page, "none\n"); |
| 2600 | |
| 2601 | return sprintf(page, "%llu\n", recovery_start); |
| 2602 | } |
| 2603 | |
| 2604 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2605 | { |
| 2606 | unsigned long long recovery_start; |
| 2607 | |
| 2608 | if (cmd_match(buf, "none")) |
| 2609 | recovery_start = MaxSector; |
| 2610 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2611 | return -EINVAL; |
| 2612 | |
| 2613 | if (rdev->mddev->pers && |
| 2614 | rdev->raid_disk >= 0) |
| 2615 | return -EBUSY; |
| 2616 | |
| 2617 | rdev->recovery_offset = recovery_start; |
| 2618 | if (recovery_start == MaxSector) |
| 2619 | set_bit(In_sync, &rdev->flags); |
| 2620 | else |
| 2621 | clear_bit(In_sync, &rdev->flags); |
| 2622 | return len; |
| 2623 | } |
| 2624 | |
| 2625 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2626 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2627 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2628 | static struct attribute *rdev_default_attrs[] = { |
| 2629 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2630 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2631 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2632 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2633 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2634 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2635 | NULL, |
| 2636 | }; |
| 2637 | static ssize_t |
| 2638 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2639 | { |
| 2640 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2641 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2642 | mddev_t *mddev = rdev->mddev; |
| 2643 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2644 | |
| 2645 | if (!entry->show) |
| 2646 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2647 | |
| 2648 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2649 | if (!rv) { |
| 2650 | if (rdev->mddev == NULL) |
| 2651 | rv = -EBUSY; |
| 2652 | else |
| 2653 | rv = entry->show(rdev, page); |
| 2654 | mddev_unlock(mddev); |
| 2655 | } |
| 2656 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | static ssize_t |
| 2660 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2661 | const char *page, size_t length) |
| 2662 | { |
| 2663 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2664 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2665 | ssize_t rv; |
| 2666 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2667 | |
| 2668 | if (!entry->store) |
| 2669 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2670 | if (!capable(CAP_SYS_ADMIN)) |
| 2671 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2672 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2673 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2674 | if (rdev->mddev == NULL) |
| 2675 | rv = -EBUSY; |
| 2676 | else |
| 2677 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2678 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2679 | } |
| 2680 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | static void rdev_free(struct kobject *ko) |
| 2684 | { |
| 2685 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2686 | kfree(rdev); |
| 2687 | } |
| 2688 | static struct sysfs_ops rdev_sysfs_ops = { |
| 2689 | .show = rdev_attr_show, |
| 2690 | .store = rdev_attr_store, |
| 2691 | }; |
| 2692 | static struct kobj_type rdev_ktype = { |
| 2693 | .release = rdev_free, |
| 2694 | .sysfs_ops = &rdev_sysfs_ops, |
| 2695 | .default_attrs = rdev_default_attrs, |
| 2696 | }; |
| 2697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | /* |
| 2699 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2700 | * |
| 2701 | * mark the device faulty if: |
| 2702 | * |
| 2703 | * - the device is nonexistent (zero size) |
| 2704 | * - the device has no valid superblock |
| 2705 | * |
| 2706 | * a faulty rdev _never_ has rdev->sb set. |
| 2707 | */ |
| 2708 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2709 | { |
| 2710 | char b[BDEVNAME_SIZE]; |
| 2711 | int err; |
| 2712 | mdk_rdev_t *rdev; |
| 2713 | sector_t size; |
| 2714 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2715 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | if (!rdev) { |
| 2717 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2718 | return ERR_PTR(-ENOMEM); |
| 2719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | |
| 2721 | if ((err = alloc_disk_sb(rdev))) |
| 2722 | goto abort_free; |
| 2723 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2724 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | if (err) |
| 2726 | goto abort_free; |
| 2727 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2728 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2731 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2732 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2733 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2735 | rdev->sb_events = 0; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2736 | rdev->last_read_error.tv_sec = 0; |
| 2737 | rdev->last_read_error.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2739 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2740 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2741 | |
| 2742 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2743 | if (!size) { |
| 2744 | printk(KERN_WARNING |
| 2745 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2746 | bdevname(rdev->bdev,b)); |
| 2747 | err = -EINVAL; |
| 2748 | goto abort_free; |
| 2749 | } |
| 2750 | |
| 2751 | if (super_format >= 0) { |
| 2752 | err = super_types[super_format]. |
| 2753 | load_super(rdev, NULL, super_minor); |
| 2754 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2755 | printk(KERN_WARNING |
| 2756 | "md: %s does not have a valid v%d.%d " |
| 2757 | "superblock, not importing!\n", |
| 2758 | bdevname(rdev->bdev,b), |
| 2759 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | goto abort_free; |
| 2761 | } |
| 2762 | if (err < 0) { |
| 2763 | printk(KERN_WARNING |
| 2764 | "md: could not read %s's sb, not importing!\n", |
| 2765 | bdevname(rdev->bdev,b)); |
| 2766 | goto abort_free; |
| 2767 | } |
| 2768 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2771 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | |
| 2773 | return rdev; |
| 2774 | |
| 2775 | abort_free: |
| 2776 | if (rdev->sb_page) { |
| 2777 | if (rdev->bdev) |
| 2778 | unlock_rdev(rdev); |
| 2779 | free_disk_sb(rdev); |
| 2780 | } |
| 2781 | kfree(rdev); |
| 2782 | return ERR_PTR(err); |
| 2783 | } |
| 2784 | |
| 2785 | /* |
| 2786 | * Check a full RAID array for plausibility |
| 2787 | */ |
| 2788 | |
| 2789 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2790 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | { |
| 2792 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2793 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | char b[BDEVNAME_SIZE]; |
| 2795 | |
| 2796 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2797 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | switch (super_types[mddev->major_version]. |
| 2799 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2800 | case 1: |
| 2801 | freshest = rdev; |
| 2802 | break; |
| 2803 | case 0: |
| 2804 | break; |
| 2805 | default: |
| 2806 | printk( KERN_ERR \ |
| 2807 | "md: fatal superblock inconsistency in %s" |
| 2808 | " -- removing from array\n", |
| 2809 | bdevname(rdev->bdev,b)); |
| 2810 | kick_rdev_from_array(rdev); |
| 2811 | } |
| 2812 | |
| 2813 | |
| 2814 | super_types[mddev->major_version]. |
| 2815 | validate_super(mddev, freshest); |
| 2816 | |
| 2817 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2818 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2819 | if (mddev->max_disks && |
| 2820 | (rdev->desc_nr >= mddev->max_disks || |
| 2821 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2822 | printk(KERN_WARNING |
| 2823 | "md: %s: %s: only %d devices permitted\n", |
| 2824 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2825 | mddev->max_disks); |
| 2826 | kick_rdev_from_array(rdev); |
| 2827 | continue; |
| 2828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | if (rdev != freshest) |
| 2830 | if (super_types[mddev->major_version]. |
| 2831 | validate_super(mddev, rdev)) { |
| 2832 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2833 | " from array!\n", |
| 2834 | bdevname(rdev->bdev,b)); |
| 2835 | kick_rdev_from_array(rdev); |
| 2836 | continue; |
| 2837 | } |
| 2838 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2839 | rdev->desc_nr = i++; |
| 2840 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2841 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2842 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2843 | rdev->raid_disk = -1; |
| 2844 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | } |
| 2846 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | } |
| 2848 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2849 | /* Read a fixed-point number. |
| 2850 | * Numbers in sysfs attributes should be in "standard" units where |
| 2851 | * possible, so time should be in seconds. |
| 2852 | * However we internally use a a much smaller unit such as |
| 2853 | * milliseconds or jiffies. |
| 2854 | * This function takes a decimal number with a possible fractional |
| 2855 | * component, and produces an integer which is the result of |
| 2856 | * multiplying that number by 10^'scale'. |
| 2857 | * all without any floating-point arithmetic. |
| 2858 | */ |
| 2859 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2860 | { |
| 2861 | unsigned long result = 0; |
| 2862 | long decimals = -1; |
| 2863 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2864 | if (*cp == '.') |
| 2865 | decimals = 0; |
| 2866 | else if (decimals < scale) { |
| 2867 | unsigned int value; |
| 2868 | value = *cp - '0'; |
| 2869 | result = result * 10 + value; |
| 2870 | if (decimals >= 0) |
| 2871 | decimals++; |
| 2872 | } |
| 2873 | cp++; |
| 2874 | } |
| 2875 | if (*cp == '\n') |
| 2876 | cp++; |
| 2877 | if (*cp) |
| 2878 | return -EINVAL; |
| 2879 | if (decimals < 0) |
| 2880 | decimals = 0; |
| 2881 | while (decimals < scale) { |
| 2882 | result *= 10; |
| 2883 | decimals ++; |
| 2884 | } |
| 2885 | *res = result; |
| 2886 | return 0; |
| 2887 | } |
| 2888 | |
| 2889 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2890 | static void md_safemode_timeout(unsigned long data); |
| 2891 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2892 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2893 | safe_delay_show(mddev_t *mddev, char *page) |
| 2894 | { |
| 2895 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2896 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2897 | } |
| 2898 | static ssize_t |
| 2899 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2900 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2901 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2902 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2903 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2904 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2905 | if (msec == 0) |
| 2906 | mddev->safemode_delay = 0; |
| 2907 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2908 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2909 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2910 | if (mddev->safemode_delay == 0) |
| 2911 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2912 | if (mddev->safemode_delay < old_delay) |
| 2913 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2914 | } |
| 2915 | return len; |
| 2916 | } |
| 2917 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2918 | __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] | 2919 | |
| 2920 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2921 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2922 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2923 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2924 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2925 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2926 | else if (mddev->clevel[0]) |
| 2927 | return sprintf(page, "%s\n", mddev->clevel); |
| 2928 | else if (mddev->level != LEVEL_NONE) |
| 2929 | return sprintf(page, "%d\n", mddev->level); |
| 2930 | else |
| 2931 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2932 | } |
| 2933 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2934 | static ssize_t |
| 2935 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2936 | { |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2937 | char level[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2938 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2939 | struct mdk_personality *pers; |
| 2940 | void *priv; |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 2941 | mdk_rdev_t *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2942 | |
| 2943 | if (mddev->pers == NULL) { |
| 2944 | if (len == 0) |
| 2945 | return 0; |
| 2946 | if (len >= sizeof(mddev->clevel)) |
| 2947 | return -ENOSPC; |
| 2948 | strncpy(mddev->clevel, buf, len); |
| 2949 | if (mddev->clevel[len-1] == '\n') |
| 2950 | len--; |
| 2951 | mddev->clevel[len] = 0; |
| 2952 | mddev->level = LEVEL_NONE; |
| 2953 | return rv; |
| 2954 | } |
| 2955 | |
| 2956 | /* request to change the personality. Need to ensure: |
| 2957 | * - array is not engaged in resync/recovery/reshape |
| 2958 | * - old personality can be suspended |
| 2959 | * - new personality will access other array. |
| 2960 | */ |
| 2961 | |
| 2962 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2963 | return -EBUSY; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2964 | |
| 2965 | if (!mddev->pers->quiesce) { |
| 2966 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 2967 | mdname(mddev), mddev->pers->name); |
| 2968 | return -EINVAL; |
| 2969 | } |
| 2970 | |
| 2971 | /* Now find the new personality */ |
| 2972 | if (len == 0 || len >= sizeof(level)) |
| 2973 | return -EINVAL; |
| 2974 | strncpy(level, buf, len); |
| 2975 | if (level[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2976 | len--; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2977 | level[len] = 0; |
| 2978 | |
| 2979 | request_module("md-%s", level); |
| 2980 | spin_lock(&pers_lock); |
| 2981 | pers = find_pers(LEVEL_NONE, level); |
| 2982 | if (!pers || !try_module_get(pers->owner)) { |
| 2983 | spin_unlock(&pers_lock); |
| 2984 | printk(KERN_WARNING "md: personality %s not loaded\n", level); |
| 2985 | return -EINVAL; |
| 2986 | } |
| 2987 | spin_unlock(&pers_lock); |
| 2988 | |
| 2989 | if (pers == mddev->pers) { |
| 2990 | /* Nothing to do! */ |
| 2991 | module_put(pers->owner); |
| 2992 | return rv; |
| 2993 | } |
| 2994 | if (!pers->takeover) { |
| 2995 | module_put(pers->owner); |
| 2996 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
| 2997 | mdname(mddev), level); |
| 2998 | return -EINVAL; |
| 2999 | } |
| 3000 | |
| 3001 | /* ->takeover must set new_* and/or delta_disks |
| 3002 | * if it succeeds, and may set them when it fails. |
| 3003 | */ |
| 3004 | priv = pers->takeover(mddev); |
| 3005 | if (IS_ERR(priv)) { |
| 3006 | mddev->new_level = mddev->level; |
| 3007 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3008 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3009 | mddev->raid_disks -= mddev->delta_disks; |
| 3010 | mddev->delta_disks = 0; |
| 3011 | module_put(pers->owner); |
| 3012 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
| 3013 | mdname(mddev), level); |
| 3014 | return PTR_ERR(priv); |
| 3015 | } |
| 3016 | |
| 3017 | /* Looks like we have a winner */ |
| 3018 | mddev_suspend(mddev); |
| 3019 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3020 | |
| 3021 | if (mddev->pers->sync_request == NULL && |
| 3022 | pers->sync_request != NULL) { |
| 3023 | /* need to add the md_redundancy_group */ |
| 3024 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3025 | printk(KERN_WARNING |
| 3026 | "md: cannot register extra attributes for %s\n", |
| 3027 | mdname(mddev)); |
| 3028 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3029 | } |
| 3030 | if (mddev->pers->sync_request != NULL && |
| 3031 | pers->sync_request == NULL) { |
| 3032 | /* need to remove the md_redundancy_group */ |
| 3033 | if (mddev->to_remove == NULL) |
| 3034 | mddev->to_remove = &md_redundancy_group; |
| 3035 | } |
| 3036 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3037 | if (mddev->pers->sync_request == NULL && |
| 3038 | mddev->external) { |
| 3039 | /* We are converting from a no-redundancy array |
| 3040 | * to a redundancy array and metadata is managed |
| 3041 | * externally so we need to be sure that writes |
| 3042 | * won't block due to a need to transition |
| 3043 | * clean->dirty |
| 3044 | * until external management is started. |
| 3045 | */ |
| 3046 | mddev->in_sync = 0; |
| 3047 | mddev->safemode_delay = 0; |
| 3048 | mddev->safemode = 0; |
| 3049 | } |
| 3050 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3051 | module_put(mddev->pers->owner); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3052 | /* Invalidate devices that are now superfluous */ |
| 3053 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3054 | if (rdev->raid_disk >= mddev->raid_disks) { |
| 3055 | rdev->raid_disk = -1; |
| 3056 | clear_bit(In_sync, &rdev->flags); |
| 3057 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3058 | mddev->pers = pers; |
| 3059 | mddev->private = priv; |
| 3060 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3061 | mddev->level = mddev->new_level; |
| 3062 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3063 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3064 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3065 | if (mddev->pers->sync_request == NULL) { |
| 3066 | /* this is now an array without redundancy, so |
| 3067 | * it must always be in_sync |
| 3068 | */ |
| 3069 | mddev->in_sync = 1; |
| 3070 | del_timer_sync(&mddev->safemode_timer); |
| 3071 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3072 | pers->run(mddev); |
| 3073 | mddev_resume(mddev); |
| 3074 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3075 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3076 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3077 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame^] | 3078 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3079 | return rv; |
| 3080 | } |
| 3081 | |
| 3082 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3083 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3084 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3085 | |
| 3086 | static ssize_t |
| 3087 | layout_show(mddev_t *mddev, char *page) |
| 3088 | { |
| 3089 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3090 | if (mddev->reshape_position != MaxSector && |
| 3091 | mddev->layout != mddev->new_layout) |
| 3092 | return sprintf(page, "%d (%d)\n", |
| 3093 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3094 | return sprintf(page, "%d\n", mddev->layout); |
| 3095 | } |
| 3096 | |
| 3097 | static ssize_t |
| 3098 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3099 | { |
| 3100 | char *e; |
| 3101 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3102 | |
| 3103 | if (!*buf || (*e && *e != '\n')) |
| 3104 | return -EINVAL; |
| 3105 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3106 | if (mddev->pers) { |
| 3107 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3108 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3109 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3110 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3111 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3112 | if (err) { |
| 3113 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3114 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3115 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3116 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3117 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3118 | if (mddev->reshape_position == MaxSector) |
| 3119 | mddev->layout = n; |
| 3120 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3121 | return len; |
| 3122 | } |
| 3123 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3124 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3125 | |
| 3126 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3127 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3128 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3129 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3130 | if (mddev->raid_disks == 0) |
| 3131 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3132 | if (mddev->reshape_position != MaxSector && |
| 3133 | mddev->delta_disks != 0) |
| 3134 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3135 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3136 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3137 | } |
| 3138 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3139 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3140 | |
| 3141 | static ssize_t |
| 3142 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3143 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3144 | char *e; |
| 3145 | int rv = 0; |
| 3146 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3147 | |
| 3148 | if (!*buf || (*e && *e != '\n')) |
| 3149 | return -EINVAL; |
| 3150 | |
| 3151 | if (mddev->pers) |
| 3152 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3153 | else if (mddev->reshape_position != MaxSector) { |
| 3154 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3155 | mddev->delta_disks = n - olddisks; |
| 3156 | mddev->raid_disks = n; |
| 3157 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3158 | mddev->raid_disks = n; |
| 3159 | return rv ? rv : len; |
| 3160 | } |
| 3161 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3162 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3163 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3164 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3165 | chunk_size_show(mddev_t *mddev, char *page) |
| 3166 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3167 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3168 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3169 | return sprintf(page, "%d (%d)\n", |
| 3170 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3171 | mddev->chunk_sectors << 9); |
| 3172 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | static ssize_t |
| 3176 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3177 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3178 | char *e; |
| 3179 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3180 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3181 | if (!*buf || (*e && *e != '\n')) |
| 3182 | return -EINVAL; |
| 3183 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3184 | if (mddev->pers) { |
| 3185 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3186 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3187 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3188 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3189 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3190 | if (err) { |
| 3191 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3192 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3193 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3194 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3195 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3196 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3197 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3198 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3199 | return len; |
| 3200 | } |
| 3201 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3202 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3203 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3204 | static ssize_t |
| 3205 | resync_start_show(mddev_t *mddev, char *page) |
| 3206 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3207 | if (mddev->recovery_cp == MaxSector) |
| 3208 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3209 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3210 | } |
| 3211 | |
| 3212 | static ssize_t |
| 3213 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3214 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3215 | char *e; |
| 3216 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3217 | |
| 3218 | if (mddev->pers) |
| 3219 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3220 | if (cmd_match(buf, "none")) |
| 3221 | n = MaxSector; |
| 3222 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3223 | return -EINVAL; |
| 3224 | |
| 3225 | mddev->recovery_cp = n; |
| 3226 | return len; |
| 3227 | } |
| 3228 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3229 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3230 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3231 | /* |
| 3232 | * The array state can be: |
| 3233 | * |
| 3234 | * clear |
| 3235 | * No devices, no size, no level |
| 3236 | * Equivalent to STOP_ARRAY ioctl |
| 3237 | * inactive |
| 3238 | * May have some settings, but array is not active |
| 3239 | * all IO results in error |
| 3240 | * When written, doesn't tear down array, but just stops it |
| 3241 | * suspended (not supported yet) |
| 3242 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3243 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3244 | * readonly |
| 3245 | * no resync can happen. no superblocks get written. |
| 3246 | * write requests fail |
| 3247 | * read-auto |
| 3248 | * like readonly, but behaves like 'clean' on a write request. |
| 3249 | * |
| 3250 | * clean - no pending writes, but otherwise active. |
| 3251 | * When written to inactive array, starts without resync |
| 3252 | * If a write request arrives then |
| 3253 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3254 | * if not known, block and switch to write-pending |
| 3255 | * If written to an active array that has pending writes, then fails. |
| 3256 | * active |
| 3257 | * fully active: IO and resync can be happening. |
| 3258 | * When written to inactive array, starts with resync |
| 3259 | * |
| 3260 | * write-pending |
| 3261 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3262 | * |
| 3263 | * active-idle |
| 3264 | * like active, but no writes have been seen for a while (100msec). |
| 3265 | * |
| 3266 | */ |
| 3267 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3268 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3269 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3270 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3271 | "write-pending", "active-idle", NULL }; |
| 3272 | |
| 3273 | static int match_word(const char *word, char **list) |
| 3274 | { |
| 3275 | int n; |
| 3276 | for (n=0; list[n]; n++) |
| 3277 | if (cmd_match(word, list[n])) |
| 3278 | break; |
| 3279 | return n; |
| 3280 | } |
| 3281 | |
| 3282 | static ssize_t |
| 3283 | array_state_show(mddev_t *mddev, char *page) |
| 3284 | { |
| 3285 | enum array_state st = inactive; |
| 3286 | |
| 3287 | if (mddev->pers) |
| 3288 | switch(mddev->ro) { |
| 3289 | case 1: |
| 3290 | st = readonly; |
| 3291 | break; |
| 3292 | case 2: |
| 3293 | st = read_auto; |
| 3294 | break; |
| 3295 | case 0: |
| 3296 | if (mddev->in_sync) |
| 3297 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3298 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3299 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3300 | else if (mddev->safemode) |
| 3301 | st = active_idle; |
| 3302 | else |
| 3303 | st = active; |
| 3304 | } |
| 3305 | else { |
| 3306 | if (list_empty(&mddev->disks) && |
| 3307 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3308 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3309 | st = clear; |
| 3310 | else |
| 3311 | st = inactive; |
| 3312 | } |
| 3313 | return sprintf(page, "%s\n", array_states[st]); |
| 3314 | } |
| 3315 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3316 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3317 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3318 | static int do_md_run(mddev_t * mddev); |
| 3319 | static int restart_array(mddev_t *mddev); |
| 3320 | |
| 3321 | static ssize_t |
| 3322 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3323 | { |
| 3324 | int err = -EINVAL; |
| 3325 | enum array_state st = match_word(buf, array_states); |
| 3326 | switch(st) { |
| 3327 | case bad_word: |
| 3328 | break; |
| 3329 | case clear: |
| 3330 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3331 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3332 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3333 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3334 | break; |
| 3335 | case inactive: |
| 3336 | /* stopping an active array */ |
| 3337 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3338 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3339 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3340 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3341 | } else |
| 3342 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3343 | break; |
| 3344 | case suspended: |
| 3345 | break; /* not supported yet */ |
| 3346 | case readonly: |
| 3347 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3348 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3349 | else { |
| 3350 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3351 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3352 | err = do_md_run(mddev); |
| 3353 | } |
| 3354 | break; |
| 3355 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3356 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3357 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3358 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3359 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3360 | err = restart_array(mddev); |
| 3361 | if (err == 0) { |
| 3362 | mddev->ro = 2; |
| 3363 | set_disk_ro(mddev->gendisk, 0); |
| 3364 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3365 | } else { |
| 3366 | mddev->ro = 2; |
| 3367 | err = do_md_run(mddev); |
| 3368 | } |
| 3369 | break; |
| 3370 | case clean: |
| 3371 | if (mddev->pers) { |
| 3372 | restart_array(mddev); |
| 3373 | spin_lock_irq(&mddev->write_lock); |
| 3374 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3375 | if (mddev->in_sync == 0) { |
| 3376 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3377 | if (mddev->safemode == 1) |
| 3378 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3379 | if (mddev->persistent) |
| 3380 | set_bit(MD_CHANGE_CLEAN, |
| 3381 | &mddev->flags); |
| 3382 | } |
| 3383 | err = 0; |
| 3384 | } else |
| 3385 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3386 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3387 | } else |
| 3388 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3389 | break; |
| 3390 | case active: |
| 3391 | if (mddev->pers) { |
| 3392 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3393 | if (mddev->external) |
| 3394 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3395 | wake_up(&mddev->sb_wait); |
| 3396 | err = 0; |
| 3397 | } else { |
| 3398 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3399 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3400 | err = do_md_run(mddev); |
| 3401 | } |
| 3402 | break; |
| 3403 | case write_pending: |
| 3404 | case active_idle: |
| 3405 | /* these cannot be set */ |
| 3406 | break; |
| 3407 | } |
| 3408 | if (err) |
| 3409 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3410 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3411 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3412 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3413 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3414 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3415 | static struct md_sysfs_entry md_array_state = |
| 3416 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3417 | |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3418 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3419 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3420 | return sprintf(page, "%d\n", |
| 3421 | atomic_read(&mddev->max_corr_read_errors)); |
| 3422 | } |
| 3423 | |
| 3424 | static ssize_t |
| 3425 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3426 | { |
| 3427 | char *e; |
| 3428 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3429 | |
| 3430 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3431 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3432 | return len; |
| 3433 | } |
| 3434 | return -EINVAL; |
| 3435 | } |
| 3436 | |
| 3437 | static struct md_sysfs_entry max_corr_read_errors = |
| 3438 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3439 | max_corrected_read_errors_store); |
| 3440 | |
| 3441 | static ssize_t |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3442 | null_show(mddev_t *mddev, char *page) |
| 3443 | { |
| 3444 | return -EINVAL; |
| 3445 | } |
| 3446 | |
| 3447 | static ssize_t |
| 3448 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3449 | { |
| 3450 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3451 | /* The new device is added to the array. |
| 3452 | * If the array has a persistent superblock, we read the |
| 3453 | * superblock to initialise info and check validity. |
| 3454 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3455 | * which mainly checks size. |
| 3456 | */ |
| 3457 | char *e; |
| 3458 | int major = simple_strtoul(buf, &e, 10); |
| 3459 | int minor; |
| 3460 | dev_t dev; |
| 3461 | mdk_rdev_t *rdev; |
| 3462 | int err; |
| 3463 | |
| 3464 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3465 | return -EINVAL; |
| 3466 | minor = simple_strtoul(e+1, &e, 10); |
| 3467 | if (*e && *e != '\n') |
| 3468 | return -EINVAL; |
| 3469 | dev = MKDEV(major, minor); |
| 3470 | if (major != MAJOR(dev) || |
| 3471 | minor != MINOR(dev)) |
| 3472 | return -EOVERFLOW; |
| 3473 | |
| 3474 | |
| 3475 | if (mddev->persistent) { |
| 3476 | rdev = md_import_device(dev, mddev->major_version, |
| 3477 | mddev->minor_version); |
| 3478 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3479 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3480 | mdk_rdev_t, same_set); |
| 3481 | err = super_types[mddev->major_version] |
| 3482 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3483 | if (err < 0) |
| 3484 | goto out; |
| 3485 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3486 | } else if (mddev->external) |
| 3487 | rdev = md_import_device(dev, -2, -1); |
| 3488 | else |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3489 | rdev = md_import_device(dev, -1, -1); |
| 3490 | |
| 3491 | if (IS_ERR(rdev)) |
| 3492 | return PTR_ERR(rdev); |
| 3493 | err = bind_rdev_to_array(rdev, mddev); |
| 3494 | out: |
| 3495 | if (err) |
| 3496 | export_rdev(rdev); |
| 3497 | return err ? err : len; |
| 3498 | } |
| 3499 | |
| 3500 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3501 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3502 | |
| 3503 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3504 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3505 | { |
| 3506 | char *end; |
| 3507 | unsigned long chunk, end_chunk; |
| 3508 | |
| 3509 | if (!mddev->bitmap) |
| 3510 | goto out; |
| 3511 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3512 | while (*buf) { |
| 3513 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3514 | if (buf == end) break; |
| 3515 | if (*end == '-') { /* range */ |
| 3516 | buf = end + 1; |
| 3517 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3518 | if (buf == end) break; |
| 3519 | } |
| 3520 | if (*end && !isspace(*end)) break; |
| 3521 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3522 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3523 | } |
| 3524 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3525 | out: |
| 3526 | return len; |
| 3527 | } |
| 3528 | |
| 3529 | static struct md_sysfs_entry md_bitmap = |
| 3530 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3531 | |
| 3532 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3533 | size_show(mddev_t *mddev, char *page) |
| 3534 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3535 | return sprintf(page, "%llu\n", |
| 3536 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3537 | } |
| 3538 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3539 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3540 | |
| 3541 | static ssize_t |
| 3542 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3543 | { |
| 3544 | /* If array is inactive, we can reduce the component size, but |
| 3545 | * not increase it (except from 0). |
| 3546 | * If array is active, we can try an on-line resize |
| 3547 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3548 | sector_t sectors; |
| 3549 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3550 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3551 | if (err < 0) |
| 3552 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3553 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3554 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3555 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3556 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3557 | if (mddev->dev_sectors == 0 || |
| 3558 | mddev->dev_sectors > sectors) |
| 3559 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3560 | else |
| 3561 | err = -ENOSPC; |
| 3562 | } |
| 3563 | return err ? err : len; |
| 3564 | } |
| 3565 | |
| 3566 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3567 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3568 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3569 | |
| 3570 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3571 | * This is one of |
| 3572 | * 'none' for arrays with no metadata (good luck...) |
| 3573 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3574 | * or N.M for internally known formats |
| 3575 | */ |
| 3576 | static ssize_t |
| 3577 | metadata_show(mddev_t *mddev, char *page) |
| 3578 | { |
| 3579 | if (mddev->persistent) |
| 3580 | return sprintf(page, "%d.%d\n", |
| 3581 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3582 | else if (mddev->external) |
| 3583 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3584 | else |
| 3585 | return sprintf(page, "none\n"); |
| 3586 | } |
| 3587 | |
| 3588 | static ssize_t |
| 3589 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3590 | { |
| 3591 | int major, minor; |
| 3592 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3593 | /* Changing the details of 'external' metadata is |
| 3594 | * always permitted. Otherwise there must be |
| 3595 | * no devices attached to the array. |
| 3596 | */ |
| 3597 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3598 | ; |
| 3599 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3600 | return -EBUSY; |
| 3601 | |
| 3602 | if (cmd_match(buf, "none")) { |
| 3603 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3604 | mddev->external = 0; |
| 3605 | mddev->major_version = 0; |
| 3606 | mddev->minor_version = 90; |
| 3607 | return len; |
| 3608 | } |
| 3609 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3610 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3611 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3612 | namelen = sizeof(mddev->metadata_type)-1; |
| 3613 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3614 | mddev->metadata_type[namelen] = 0; |
| 3615 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3616 | mddev->metadata_type[--namelen] = 0; |
| 3617 | mddev->persistent = 0; |
| 3618 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3619 | mddev->major_version = 0; |
| 3620 | mddev->minor_version = 90; |
| 3621 | return len; |
| 3622 | } |
| 3623 | major = simple_strtoul(buf, &e, 10); |
| 3624 | if (e==buf || *e != '.') |
| 3625 | return -EINVAL; |
| 3626 | buf = e+1; |
| 3627 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3628 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3629 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3630 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3631 | return -ENOENT; |
| 3632 | mddev->major_version = major; |
| 3633 | mddev->minor_version = minor; |
| 3634 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3635 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3636 | return len; |
| 3637 | } |
| 3638 | |
| 3639 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3640 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3641 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3642 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3643 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3644 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3645 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3646 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3647 | type = "frozen"; |
| 3648 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3649 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3650 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3651 | type = "reshape"; |
| 3652 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3653 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3654 | type = "resync"; |
| 3655 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3656 | type = "check"; |
| 3657 | else |
| 3658 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3659 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3660 | type = "recover"; |
| 3661 | } |
| 3662 | return sprintf(page, "%s\n", type); |
| 3663 | } |
| 3664 | |
| 3665 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3666 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3667 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3668 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3669 | return -EINVAL; |
| 3670 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3671 | if (cmd_match(page, "frozen")) |
| 3672 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3673 | else |
| 3674 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3675 | |
| 3676 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3677 | if (mddev->sync_thread) { |
| 3678 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3679 | md_unregister_thread(mddev->sync_thread); |
| 3680 | mddev->sync_thread = NULL; |
| 3681 | mddev->recovery = 0; |
| 3682 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3683 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3684 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3685 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3686 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3687 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3688 | else if (cmd_match(page, "recover")) { |
| 3689 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3690 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3691 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3692 | int err; |
| 3693 | if (mddev->pers->start_reshape == NULL) |
| 3694 | return -EINVAL; |
| 3695 | err = mddev->pers->start_reshape(mddev); |
| 3696 | if (err) |
| 3697 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3698 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3699 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3700 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3701 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3702 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3703 | return -EINVAL; |
| 3704 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3705 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3706 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3707 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3708 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3709 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3710 | return len; |
| 3711 | } |
| 3712 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3713 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3714 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3715 | { |
| 3716 | return sprintf(page, "%llu\n", |
| 3717 | (unsigned long long) mddev->resync_mismatches); |
| 3718 | } |
| 3719 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3720 | static struct md_sysfs_entry md_scan_mode = |
| 3721 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3722 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3723 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3724 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3725 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3726 | static ssize_t |
| 3727 | sync_min_show(mddev_t *mddev, char *page) |
| 3728 | { |
| 3729 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3730 | mddev->sync_speed_min ? "local": "system"); |
| 3731 | } |
| 3732 | |
| 3733 | static ssize_t |
| 3734 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3735 | { |
| 3736 | int min; |
| 3737 | char *e; |
| 3738 | if (strncmp(buf, "system", 6)==0) { |
| 3739 | mddev->sync_speed_min = 0; |
| 3740 | return len; |
| 3741 | } |
| 3742 | min = simple_strtoul(buf, &e, 10); |
| 3743 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3744 | return -EINVAL; |
| 3745 | mddev->sync_speed_min = min; |
| 3746 | return len; |
| 3747 | } |
| 3748 | |
| 3749 | static struct md_sysfs_entry md_sync_min = |
| 3750 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3751 | |
| 3752 | static ssize_t |
| 3753 | sync_max_show(mddev_t *mddev, char *page) |
| 3754 | { |
| 3755 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3756 | mddev->sync_speed_max ? "local": "system"); |
| 3757 | } |
| 3758 | |
| 3759 | static ssize_t |
| 3760 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3761 | { |
| 3762 | int max; |
| 3763 | char *e; |
| 3764 | if (strncmp(buf, "system", 6)==0) { |
| 3765 | mddev->sync_speed_max = 0; |
| 3766 | return len; |
| 3767 | } |
| 3768 | max = simple_strtoul(buf, &e, 10); |
| 3769 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3770 | return -EINVAL; |
| 3771 | mddev->sync_speed_max = max; |
| 3772 | return len; |
| 3773 | } |
| 3774 | |
| 3775 | static struct md_sysfs_entry md_sync_max = |
| 3776 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3777 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3778 | static ssize_t |
| 3779 | degraded_show(mddev_t *mddev, char *page) |
| 3780 | { |
| 3781 | return sprintf(page, "%d\n", mddev->degraded); |
| 3782 | } |
| 3783 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3784 | |
| 3785 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3786 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3787 | { |
| 3788 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3789 | } |
| 3790 | |
| 3791 | static ssize_t |
| 3792 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3793 | { |
| 3794 | long n; |
| 3795 | |
| 3796 | if (strict_strtol(buf, 10, &n)) |
| 3797 | return -EINVAL; |
| 3798 | |
| 3799 | if (n != 0 && n != 1) |
| 3800 | return -EINVAL; |
| 3801 | |
| 3802 | mddev->parallel_resync = n; |
| 3803 | |
| 3804 | if (mddev->sync_thread) |
| 3805 | wake_up(&resync_wait); |
| 3806 | |
| 3807 | return len; |
| 3808 | } |
| 3809 | |
| 3810 | /* force parallel resync, even with shared block devices */ |
| 3811 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3812 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3813 | sync_force_parallel_show, sync_force_parallel_store); |
| 3814 | |
| 3815 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3816 | sync_speed_show(mddev_t *mddev, char *page) |
| 3817 | { |
| 3818 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3819 | if (mddev->curr_resync == 0) |
| 3820 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3821 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3822 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3823 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3824 | db = resync - mddev->resync_mark_cnt; |
| 3825 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3826 | } |
| 3827 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3828 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3829 | |
| 3830 | static ssize_t |
| 3831 | sync_completed_show(mddev_t *mddev, char *page) |
| 3832 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3833 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3834 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3835 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3836 | return sprintf(page, "none\n"); |
| 3837 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3838 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3839 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3840 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3841 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3842 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3843 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3844 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3845 | } |
| 3846 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3847 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3848 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3849 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3850 | min_sync_show(mddev_t *mddev, char *page) |
| 3851 | { |
| 3852 | return sprintf(page, "%llu\n", |
| 3853 | (unsigned long long)mddev->resync_min); |
| 3854 | } |
| 3855 | static ssize_t |
| 3856 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3857 | { |
| 3858 | unsigned long long min; |
| 3859 | if (strict_strtoull(buf, 10, &min)) |
| 3860 | return -EINVAL; |
| 3861 | if (min > mddev->resync_max) |
| 3862 | return -EINVAL; |
| 3863 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3864 | return -EBUSY; |
| 3865 | |
| 3866 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3867 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3868 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3869 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3870 | return -EINVAL; |
| 3871 | } |
| 3872 | mddev->resync_min = min; |
| 3873 | |
| 3874 | return len; |
| 3875 | } |
| 3876 | |
| 3877 | static struct md_sysfs_entry md_min_sync = |
| 3878 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3879 | |
| 3880 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3881 | max_sync_show(mddev_t *mddev, char *page) |
| 3882 | { |
| 3883 | if (mddev->resync_max == MaxSector) |
| 3884 | return sprintf(page, "max\n"); |
| 3885 | else |
| 3886 | return sprintf(page, "%llu\n", |
| 3887 | (unsigned long long)mddev->resync_max); |
| 3888 | } |
| 3889 | static ssize_t |
| 3890 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3891 | { |
| 3892 | if (strncmp(buf, "max", 3) == 0) |
| 3893 | mddev->resync_max = MaxSector; |
| 3894 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3895 | unsigned long long max; |
| 3896 | if (strict_strtoull(buf, 10, &max)) |
| 3897 | return -EINVAL; |
| 3898 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3899 | return -EINVAL; |
| 3900 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3901 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3902 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3903 | return -EBUSY; |
| 3904 | |
| 3905 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3906 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3907 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3908 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3909 | return -EINVAL; |
| 3910 | } |
| 3911 | mddev->resync_max = max; |
| 3912 | } |
| 3913 | wake_up(&mddev->recovery_wait); |
| 3914 | return len; |
| 3915 | } |
| 3916 | |
| 3917 | static struct md_sysfs_entry md_max_sync = |
| 3918 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3919 | |
| 3920 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3921 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3922 | { |
| 3923 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3924 | } |
| 3925 | |
| 3926 | static ssize_t |
| 3927 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3928 | { |
| 3929 | char *e; |
| 3930 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3931 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3932 | if (mddev->pers == NULL || |
| 3933 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3934 | return -EINVAL; |
| 3935 | if (buf == e || (*e && *e != '\n')) |
| 3936 | return -EINVAL; |
| 3937 | if (new >= mddev->suspend_hi || |
| 3938 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3939 | mddev->suspend_lo = new; |
| 3940 | mddev->pers->quiesce(mddev, 2); |
| 3941 | return len; |
| 3942 | } else |
| 3943 | return -EINVAL; |
| 3944 | } |
| 3945 | static struct md_sysfs_entry md_suspend_lo = |
| 3946 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3947 | |
| 3948 | |
| 3949 | static ssize_t |
| 3950 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3951 | { |
| 3952 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3953 | } |
| 3954 | |
| 3955 | static ssize_t |
| 3956 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3957 | { |
| 3958 | char *e; |
| 3959 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3960 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3961 | if (mddev->pers == NULL || |
| 3962 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3963 | return -EINVAL; |
| 3964 | if (buf == e || (*e && *e != '\n')) |
| 3965 | return -EINVAL; |
| 3966 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3967 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3968 | mddev->suspend_hi = new; |
| 3969 | mddev->pers->quiesce(mddev, 1); |
| 3970 | mddev->pers->quiesce(mddev, 0); |
| 3971 | return len; |
| 3972 | } else |
| 3973 | return -EINVAL; |
| 3974 | } |
| 3975 | static struct md_sysfs_entry md_suspend_hi = |
| 3976 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3977 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3978 | static ssize_t |
| 3979 | reshape_position_show(mddev_t *mddev, char *page) |
| 3980 | { |
| 3981 | if (mddev->reshape_position != MaxSector) |
| 3982 | return sprintf(page, "%llu\n", |
| 3983 | (unsigned long long)mddev->reshape_position); |
| 3984 | strcpy(page, "none\n"); |
| 3985 | return 5; |
| 3986 | } |
| 3987 | |
| 3988 | static ssize_t |
| 3989 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3990 | { |
| 3991 | char *e; |
| 3992 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3993 | if (mddev->pers) |
| 3994 | return -EBUSY; |
| 3995 | if (buf == e || (*e && *e != '\n')) |
| 3996 | return -EINVAL; |
| 3997 | mddev->reshape_position = new; |
| 3998 | mddev->delta_disks = 0; |
| 3999 | mddev->new_level = mddev->level; |
| 4000 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4001 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4002 | return len; |
| 4003 | } |
| 4004 | |
| 4005 | static struct md_sysfs_entry md_reshape_position = |
| 4006 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4007 | reshape_position_store); |
| 4008 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4009 | static ssize_t |
| 4010 | array_size_show(mddev_t *mddev, char *page) |
| 4011 | { |
| 4012 | if (mddev->external_size) |
| 4013 | return sprintf(page, "%llu\n", |
| 4014 | (unsigned long long)mddev->array_sectors/2); |
| 4015 | else |
| 4016 | return sprintf(page, "default\n"); |
| 4017 | } |
| 4018 | |
| 4019 | static ssize_t |
| 4020 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4021 | { |
| 4022 | sector_t sectors; |
| 4023 | |
| 4024 | if (strncmp(buf, "default", 7) == 0) { |
| 4025 | if (mddev->pers) |
| 4026 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4027 | else |
| 4028 | sectors = mddev->array_sectors; |
| 4029 | |
| 4030 | mddev->external_size = 0; |
| 4031 | } else { |
| 4032 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4033 | return -EINVAL; |
| 4034 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4035 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4036 | |
| 4037 | mddev->external_size = 1; |
| 4038 | } |
| 4039 | |
| 4040 | mddev->array_sectors = sectors; |
| 4041 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4042 | if (mddev->pers) |
| 4043 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4044 | |
| 4045 | return len; |
| 4046 | } |
| 4047 | |
| 4048 | static struct md_sysfs_entry md_array_size = |
| 4049 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4050 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4051 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4052 | static struct attribute *md_default_attrs[] = { |
| 4053 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4054 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4055 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4056 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4057 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4058 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4059 | &md_metadata.attr, |
NeilBrown | 6d7ff73 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4060 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4061 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4062 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4063 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4064 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4065 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4066 | NULL, |
| 4067 | }; |
| 4068 | |
| 4069 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4070 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4071 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4072 | &md_sync_min.attr, |
| 4073 | &md_sync_max.attr, |
| 4074 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4075 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4076 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4077 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4078 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4079 | &md_suspend_lo.attr, |
| 4080 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4081 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4082 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4083 | NULL, |
| 4084 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4085 | static struct attribute_group md_redundancy_group = { |
| 4086 | .name = NULL, |
| 4087 | .attrs = md_redundancy_attrs, |
| 4088 | }; |
| 4089 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4090 | |
| 4091 | static ssize_t |
| 4092 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4093 | { |
| 4094 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4095 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4096 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4097 | |
| 4098 | if (!entry->show) |
| 4099 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4100 | rv = mddev_lock(mddev); |
| 4101 | if (!rv) { |
| 4102 | rv = entry->show(mddev, page); |
| 4103 | mddev_unlock(mddev); |
| 4104 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4105 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | static ssize_t |
| 4109 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4110 | const char *page, size_t length) |
| 4111 | { |
| 4112 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4113 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4114 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4115 | |
| 4116 | if (!entry->store) |
| 4117 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4118 | if (!capable(CAP_SYS_ADMIN)) |
| 4119 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4120 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4121 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4122 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4123 | if (!rv) { |
| 4124 | rv = entry->store(mddev, page, length); |
| 4125 | mddev_unlock(mddev); |
| 4126 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4127 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4128 | } |
| 4129 | |
| 4130 | static void md_free(struct kobject *ko) |
| 4131 | { |
| 4132 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4133 | |
| 4134 | if (mddev->sysfs_state) |
| 4135 | sysfs_put(mddev->sysfs_state); |
| 4136 | |
| 4137 | if (mddev->gendisk) { |
| 4138 | del_gendisk(mddev->gendisk); |
| 4139 | put_disk(mddev->gendisk); |
| 4140 | } |
| 4141 | if (mddev->queue) |
| 4142 | blk_cleanup_queue(mddev->queue); |
| 4143 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4144 | kfree(mddev); |
| 4145 | } |
| 4146 | |
| 4147 | static struct sysfs_ops md_sysfs_ops = { |
| 4148 | .show = md_attr_show, |
| 4149 | .store = md_attr_store, |
| 4150 | }; |
| 4151 | static struct kobj_type md_ktype = { |
| 4152 | .release = md_free, |
| 4153 | .sysfs_ops = &md_sysfs_ops, |
| 4154 | .default_attrs = md_default_attrs, |
| 4155 | }; |
| 4156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | int mdp_major = 0; |
| 4158 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4159 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4160 | { |
| 4161 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4162 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4163 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4164 | kobject_del(&mddev->kobj); |
| 4165 | kobject_put(&mddev->kobj); |
| 4166 | } |
| 4167 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4168 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4170 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | mddev_t *mddev = mddev_find(dev); |
| 4172 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4173 | int partitioned; |
| 4174 | int shift; |
| 4175 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4176 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | |
| 4178 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4179 | return -ENODEV; |
| 4180 | |
| 4181 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4182 | shift = partitioned ? MdpMinorShift : 0; |
| 4183 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4185 | /* wait for any previous instance if this device |
| 4186 | * to be completed removed (mddev_delayed_delete). |
| 4187 | */ |
| 4188 | flush_scheduled_work(); |
| 4189 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4190 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4191 | error = -EEXIST; |
| 4192 | if (mddev->gendisk) |
| 4193 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4194 | |
| 4195 | if (name) { |
| 4196 | /* Need to ensure that 'name' is not a duplicate. |
| 4197 | */ |
| 4198 | mddev_t *mddev2; |
| 4199 | spin_lock(&all_mddevs_lock); |
| 4200 | |
| 4201 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4202 | if (mddev2->gendisk && |
| 4203 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4204 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4205 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4206 | } |
| 4207 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4208 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4209 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4210 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4211 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4212 | if (!mddev->queue) |
| 4213 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4214 | mddev->queue->queuedata = mddev; |
| 4215 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4216 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4217 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4218 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4219 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | disk = alloc_disk(1 << shift); |
| 4222 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4223 | blk_cleanup_queue(mddev->queue); |
| 4224 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4225 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4227 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4228 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4229 | if (name) |
| 4230 | strcpy(disk->disk_name, name); |
| 4231 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4233 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | disk->fops = &md_fops; |
| 4236 | disk->private_data = mddev; |
| 4237 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4238 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4239 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4240 | * remove it now. |
| 4241 | */ |
| 4242 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | add_disk(disk); |
| 4244 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4245 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4246 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4247 | if (error) { |
| 4248 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4249 | * __must_check, we must do something with the result |
| 4250 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4251 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4252 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4253 | error = 0; |
| 4254 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4255 | if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
| 4256 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4257 | abort: |
| 4258 | mutex_unlock(&disks_mutex); |
| 4259 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4260 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4261 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 4262 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4263 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4264 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4268 | { |
| 4269 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | return NULL; |
| 4271 | } |
| 4272 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4273 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4274 | { |
| 4275 | /* val must be "md_*" where * is not all digits. |
| 4276 | * We allocate an array with a large free minor number, and |
| 4277 | * set the name to val. val must not already be an active name. |
| 4278 | */ |
| 4279 | int len = strlen(val); |
| 4280 | char buf[DISK_NAME_LEN]; |
| 4281 | |
| 4282 | while (len && val[len-1] == '\n') |
| 4283 | len--; |
| 4284 | if (len >= DISK_NAME_LEN) |
| 4285 | return -E2BIG; |
| 4286 | strlcpy(buf, val, len+1); |
| 4287 | if (strncmp(buf, "md_", 3) != 0) |
| 4288 | return -EINVAL; |
| 4289 | return md_alloc(0, buf); |
| 4290 | } |
| 4291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | static void md_safemode_timeout(unsigned long data) |
| 4293 | { |
| 4294 | mddev_t *mddev = (mddev_t *) data; |
| 4295 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4296 | if (!atomic_read(&mddev->writes_pending)) { |
| 4297 | mddev->safemode = 1; |
| 4298 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4299 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | md_wakeup_thread(mddev->thread); |
| 4302 | } |
| 4303 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4304 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4306 | static int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4308 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4310 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4312 | if (list_empty(&mddev->disks)) |
| 4313 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | |
| 4316 | if (mddev->pers) |
| 4317 | return -EBUSY; |
| 4318 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4319 | /* These two calls synchronise us with the |
| 4320 | * sysfs_remove_group calls in mddev_unlock, |
| 4321 | * so they must have completed. |
| 4322 | */ |
| 4323 | mutex_lock(&mddev->open_mutex); |
| 4324 | mutex_unlock(&mddev->open_mutex); |
| 4325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | /* |
| 4327 | * Analyze all RAID superblock(s) |
| 4328 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4329 | if (!mddev->raid_disks) { |
| 4330 | if (!mddev->persistent) |
| 4331 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4332 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4335 | if (mddev->level != LEVEL_NONE) |
| 4336 | request_module("md-level-%d", mddev->level); |
| 4337 | else if (mddev->clevel[0]) |
| 4338 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | |
| 4340 | /* |
| 4341 | * Drop all container device buffers, from now on |
| 4342 | * the only valid external interface is through the md |
| 4343 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4345 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4346 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | continue; |
| 4348 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4349 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4350 | |
| 4351 | /* perform some consistency tests on the device. |
| 4352 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4353 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4354 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4355 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4356 | if (mddev->dev_sectors && |
| 4357 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4358 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4359 | printk("md: %s: data overlaps metadata\n", |
| 4360 | mdname(mddev)); |
| 4361 | return -EINVAL; |
| 4362 | } |
| 4363 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4364 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4365 | > rdev->data_offset) { |
| 4366 | printk("md: %s: metadata overlaps data\n", |
| 4367 | mdname(mddev)); |
| 4368 | return -EINVAL; |
| 4369 | } |
| 4370 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4371 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | } |
| 4373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4374 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4375 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4376 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4378 | if (mddev->level != LEVEL_NONE) |
| 4379 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4380 | mddev->level); |
| 4381 | else |
| 4382 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4383 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | return -EINVAL; |
| 4385 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4386 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4388 | if (mddev->level != pers->level) { |
| 4389 | mddev->level = pers->level; |
| 4390 | mddev->new_level = pers->level; |
| 4391 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4392 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4393 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4394 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4395 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4396 | /* This personality cannot handle reshaping... */ |
| 4397 | mddev->pers = NULL; |
| 4398 | module_put(pers->owner); |
| 4399 | return -EINVAL; |
| 4400 | } |
| 4401 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4402 | if (pers->sync_request) { |
| 4403 | /* Warn if this is a potentially silly |
| 4404 | * configuration. |
| 4405 | */ |
| 4406 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4407 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4408 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4409 | |
| 4410 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4411 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4412 | if (rdev < rdev2 && |
| 4413 | rdev->bdev->bd_contains == |
| 4414 | rdev2->bdev->bd_contains) { |
| 4415 | printk(KERN_WARNING |
| 4416 | "%s: WARNING: %s appears to be" |
| 4417 | " on the same physical disk as" |
| 4418 | " %s.\n", |
| 4419 | mdname(mddev), |
| 4420 | bdevname(rdev->bdev,b), |
| 4421 | bdevname(rdev2->bdev,b2)); |
| 4422 | warned = 1; |
| 4423 | } |
| 4424 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4425 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4426 | if (warned) |
| 4427 | printk(KERN_WARNING |
| 4428 | "True protection against single-disk" |
| 4429 | " failure might be compromised.\n"); |
| 4430 | } |
| 4431 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4432 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4433 | /* may be over-ridden by personality */ |
| 4434 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4435 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4436 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4437 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | |
NeilBrown | 0f9552b | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4439 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4440 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4441 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4442 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4443 | if (err) |
| 4444 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4445 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4446 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4447 | " but 'external_size' not in effect?\n", __func__); |
| 4448 | printk(KERN_ERR |
| 4449 | "md: invalid array_size %llu > default size %llu\n", |
| 4450 | (unsigned long long)mddev->array_sectors / 2, |
| 4451 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4452 | err = -EINVAL; |
| 4453 | mddev->pers->stop(mddev); |
| 4454 | } |
| 4455 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4456 | err = bitmap_create(mddev); |
| 4457 | if (err) { |
| 4458 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4459 | mdname(mddev), err); |
| 4460 | mddev->pers->stop(mddev); |
| 4461 | } |
| 4462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | module_put(mddev->pers->owner); |
| 4465 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4466 | bitmap_destroy(mddev); |
| 4467 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4469 | if (mddev->pers->sync_request) { |
| 4470 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4471 | printk(KERN_WARNING |
| 4472 | "md: cannot register extra attributes for %s\n", |
| 4473 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4474 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4475 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4476 | mddev->ro = 0; |
| 4477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4479 | atomic_set(&mddev->max_corr_read_errors, |
| 4480 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | mddev->safemode = 0; |
| 4482 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4483 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4484 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4486 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4487 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4488 | if (rdev->raid_disk >= 0) { |
| 4489 | char nm[20]; |
| 4490 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4491 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4492 | printk("md: cannot register %s for %s\n", |
| 4493 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | |
| 4496 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4497 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4498 | if (mddev->flags) |
| 4499 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4501 | md_wakeup_thread(mddev->thread); |
| 4502 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4503 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4504 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4505 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4506 | if (mddev->sysfs_action) |
| 4507 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4508 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | return 0; |
| 4510 | } |
| 4511 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4512 | static int do_md_run(mddev_t *mddev) |
| 4513 | { |
| 4514 | int err; |
| 4515 | |
| 4516 | err = md_run(mddev); |
| 4517 | if (err) |
| 4518 | goto out; |
| 4519 | |
| 4520 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4521 | revalidate_disk(mddev->gendisk); |
| 4522 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4523 | out: |
| 4524 | return err; |
| 4525 | } |
| 4526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | static int restart_array(mddev_t *mddev) |
| 4528 | { |
| 4529 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4531 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4532 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4533 | return -ENXIO; |
| 4534 | if (!mddev->pers) |
| 4535 | return -EINVAL; |
| 4536 | if (!mddev->ro) |
| 4537 | return -EBUSY; |
| 4538 | mddev->safemode = 0; |
| 4539 | mddev->ro = 0; |
| 4540 | set_disk_ro(disk, 0); |
| 4541 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4542 | mdname(mddev)); |
| 4543 | /* Kick recovery or resync if necessary */ |
| 4544 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4545 | md_wakeup_thread(mddev->thread); |
| 4546 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4547 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4548 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4549 | } |
| 4550 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4551 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4552 | * to the file ourselves */ |
| 4553 | static int deny_bitmap_write_access(struct file * file) |
| 4554 | { |
| 4555 | struct inode *inode = file->f_mapping->host; |
| 4556 | |
| 4557 | spin_lock(&inode->i_lock); |
| 4558 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4559 | spin_unlock(&inode->i_lock); |
| 4560 | return -ETXTBSY; |
| 4561 | } |
| 4562 | atomic_set(&inode->i_writecount, -1); |
| 4563 | spin_unlock(&inode->i_lock); |
| 4564 | |
| 4565 | return 0; |
| 4566 | } |
| 4567 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4568 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4569 | { |
| 4570 | struct inode *inode = file->f_mapping->host; |
| 4571 | |
| 4572 | spin_lock(&inode->i_lock); |
| 4573 | atomic_set(&inode->i_writecount, 1); |
| 4574 | spin_unlock(&inode->i_lock); |
| 4575 | } |
| 4576 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4577 | static void md_clean(mddev_t *mddev) |
| 4578 | { |
| 4579 | mddev->array_sectors = 0; |
| 4580 | mddev->external_size = 0; |
| 4581 | mddev->dev_sectors = 0; |
| 4582 | mddev->raid_disks = 0; |
| 4583 | mddev->recovery_cp = 0; |
| 4584 | mddev->resync_min = 0; |
| 4585 | mddev->resync_max = MaxSector; |
| 4586 | mddev->reshape_position = MaxSector; |
| 4587 | mddev->external = 0; |
| 4588 | mddev->persistent = 0; |
| 4589 | mddev->level = LEVEL_NONE; |
| 4590 | mddev->clevel[0] = 0; |
| 4591 | mddev->flags = 0; |
| 4592 | mddev->ro = 0; |
| 4593 | mddev->metadata_type[0] = 0; |
| 4594 | mddev->chunk_sectors = 0; |
| 4595 | mddev->ctime = mddev->utime = 0; |
| 4596 | mddev->layout = 0; |
| 4597 | mddev->max_disks = 0; |
| 4598 | mddev->events = 0; |
| 4599 | mddev->delta_disks = 0; |
| 4600 | mddev->new_level = LEVEL_NONE; |
| 4601 | mddev->new_layout = 0; |
| 4602 | mddev->new_chunk_sectors = 0; |
| 4603 | mddev->curr_resync = 0; |
| 4604 | mddev->resync_mismatches = 0; |
| 4605 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4606 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4607 | mddev->recovery = 0; |
| 4608 | mddev->in_sync = 0; |
| 4609 | mddev->degraded = 0; |
| 4610 | mddev->barriers_work = 0; |
| 4611 | mddev->safemode = 0; |
| 4612 | mddev->bitmap_info.offset = 0; |
| 4613 | mddev->bitmap_info.default_offset = 0; |
| 4614 | mddev->bitmap_info.chunksize = 0; |
| 4615 | mddev->bitmap_info.daemon_sleep = 0; |
| 4616 | mddev->bitmap_info.max_write_behind = 0; |
| 4617 | } |
| 4618 | |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4619 | static void md_stop_writes(mddev_t *mddev) |
| 4620 | { |
| 4621 | if (mddev->sync_thread) { |
| 4622 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4623 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4624 | md_unregister_thread(mddev->sync_thread); |
| 4625 | mddev->sync_thread = NULL; |
| 4626 | } |
| 4627 | |
| 4628 | del_timer_sync(&mddev->safemode_timer); |
| 4629 | |
| 4630 | bitmap_flush(mddev); |
| 4631 | md_super_wait(mddev); |
| 4632 | |
| 4633 | if (!mddev->in_sync || mddev->flags) { |
| 4634 | /* mark array as shutdown cleanly */ |
| 4635 | mddev->in_sync = 1; |
| 4636 | md_update_sb(mddev, 1); |
| 4637 | } |
| 4638 | } |
| 4639 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4640 | static void md_stop(mddev_t *mddev) |
| 4641 | { |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4642 | md_stop_writes(mddev); |
| 4643 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4644 | mddev->pers->stop(mddev); |
| 4645 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4646 | mddev->to_remove = &md_redundancy_group; |
| 4647 | module_put(mddev->pers->owner); |
| 4648 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4649 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4650 | } |
| 4651 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4652 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4653 | { |
| 4654 | int err = 0; |
| 4655 | mutex_lock(&mddev->open_mutex); |
| 4656 | if (atomic_read(&mddev->openers) > is_open) { |
| 4657 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4658 | err = -EBUSY; |
| 4659 | goto out; |
| 4660 | } |
| 4661 | if (mddev->pers) { |
| 4662 | md_stop_writes(mddev); |
| 4663 | |
| 4664 | err = -ENXIO; |
| 4665 | if (mddev->ro==1) |
| 4666 | goto out; |
| 4667 | mddev->ro = 1; |
| 4668 | set_disk_ro(mddev->gendisk, 1); |
| 4669 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4670 | sysfs_notify_dirent(mddev->sysfs_state); |
| 4671 | err = 0; |
| 4672 | } |
| 4673 | out: |
| 4674 | mutex_unlock(&mddev->open_mutex); |
| 4675 | return err; |
| 4676 | } |
| 4677 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4678 | /* mode: |
| 4679 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4680 | * 2 - stop but do not disassemble array |
| 4681 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4682 | 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] | 4683 | { |
| 4684 | int err = 0; |
| 4685 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4686 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4688 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4689 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4690 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4691 | err = -EBUSY; |
| 4692 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4694 | if (mddev->ro) |
| 4695 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4696 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4697 | md_stop(mddev); |
| 4698 | mddev->queue->merge_bvec_fn = NULL; |
| 4699 | mddev->queue->unplug_fn = NULL; |
| 4700 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4701 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4702 | /* tell userspace to handle 'inactive' */ |
| 4703 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4704 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4705 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4706 | if (rdev->raid_disk >= 0) { |
| 4707 | char nm[20]; |
| 4708 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4709 | sysfs_remove_link(&mddev->kobj, nm); |
| 4710 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4711 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4712 | set_capacity(disk, 0); |
| 4713 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4714 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4715 | if (mddev->ro) |
| 4716 | mddev->ro = 0; |
| 4717 | |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4718 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4720 | mutex_unlock(&mddev->open_mutex); |
| 4721 | if (err) |
| 4722 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4723 | /* |
| 4724 | * Free resources if final stop |
| 4725 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4726 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4729 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4730 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4731 | if (mddev->bitmap_info.file) { |
| 4732 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4733 | fput(mddev->bitmap_info.file); |
| 4734 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4735 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4736 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4738 | export_array(mddev); |
| 4739 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4740 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4741 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4742 | if (mddev->hold_active == UNTIL_STOP) |
| 4743 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4744 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4745 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4747 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4748 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4749 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | return err; |
| 4751 | } |
| 4752 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4753 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | static void autorun_array(mddev_t *mddev) |
| 4755 | { |
| 4756 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | int err; |
| 4758 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4759 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4761 | |
| 4762 | printk(KERN_INFO "md: running: "); |
| 4763 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4764 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4765 | char b[BDEVNAME_SIZE]; |
| 4766 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4767 | } |
| 4768 | printk("\n"); |
| 4769 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4770 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | if (err) { |
| 4772 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4773 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | } |
| 4775 | } |
| 4776 | |
| 4777 | /* |
| 4778 | * lets try to run arrays based on all disks that have arrived |
| 4779 | * until now. (those are in pending_raid_disks) |
| 4780 | * |
| 4781 | * the method: pick the first pending disk, collect all disks with |
| 4782 | * the same UUID, remove all from the pending list and put them into |
| 4783 | * the 'same_array' list. Then order this list based on superblock |
| 4784 | * update time (freshest comes first), kick out 'old' disks and |
| 4785 | * compare superblocks. If everything's fine then run it. |
| 4786 | * |
| 4787 | * If "unit" is allocated, then bump its reference count |
| 4788 | */ |
| 4789 | static void autorun_devices(int part) |
| 4790 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4791 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | mddev_t *mddev; |
| 4793 | char b[BDEVNAME_SIZE]; |
| 4794 | |
| 4795 | printk(KERN_INFO "md: autorun ...\n"); |
| 4796 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4797 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4799 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | rdev0 = list_entry(pending_raid_disks.next, |
| 4801 | mdk_rdev_t, same_set); |
| 4802 | |
| 4803 | printk(KERN_INFO "md: considering %s ...\n", |
| 4804 | bdevname(rdev0->bdev,b)); |
| 4805 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4806 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4808 | printk(KERN_INFO "md: adding %s ...\n", |
| 4809 | bdevname(rdev->bdev,b)); |
| 4810 | list_move(&rdev->same_set, &candidates); |
| 4811 | } |
| 4812 | /* |
| 4813 | * now we have a set of devices, with all of them having |
| 4814 | * mostly sane superblocks. It's time to allocate the |
| 4815 | * mddev. |
| 4816 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4817 | if (part) { |
| 4818 | dev = MKDEV(mdp_major, |
| 4819 | rdev0->preferred_minor << MdpMinorShift); |
| 4820 | unit = MINOR(dev) >> MdpMinorShift; |
| 4821 | } else { |
| 4822 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4823 | unit = MINOR(dev); |
| 4824 | } |
| 4825 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4827 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4828 | break; |
| 4829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4830 | |
| 4831 | md_probe(dev, NULL, NULL); |
| 4832 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4833 | if (!mddev || !mddev->gendisk) { |
| 4834 | if (mddev) |
| 4835 | mddev_put(mddev); |
| 4836 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4837 | "md: cannot allocate memory for md drive.\n"); |
| 4838 | break; |
| 4839 | } |
| 4840 | if (mddev_lock(mddev)) |
| 4841 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4842 | mdname(mddev)); |
| 4843 | else if (mddev->raid_disks || mddev->major_version |
| 4844 | || !list_empty(&mddev->disks)) { |
| 4845 | printk(KERN_WARNING |
| 4846 | "md: %s already running, cannot run %s\n", |
| 4847 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4848 | mddev_unlock(mddev); |
| 4849 | } else { |
| 4850 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4851 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4852 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | list_del_init(&rdev->same_set); |
| 4854 | if (bind_rdev_to_array(rdev, mddev)) |
| 4855 | export_rdev(rdev); |
| 4856 | } |
| 4857 | autorun_array(mddev); |
| 4858 | mddev_unlock(mddev); |
| 4859 | } |
| 4860 | /* on success, candidates will be empty, on error |
| 4861 | * it won't... |
| 4862 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4863 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4864 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4865 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4866 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4867 | mddev_put(mddev); |
| 4868 | } |
| 4869 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4870 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4871 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4873 | static int get_version(void __user * arg) |
| 4874 | { |
| 4875 | mdu_version_t ver; |
| 4876 | |
| 4877 | ver.major = MD_MAJOR_VERSION; |
| 4878 | ver.minor = MD_MINOR_VERSION; |
| 4879 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4880 | |
| 4881 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4882 | return -EFAULT; |
| 4883 | |
| 4884 | return 0; |
| 4885 | } |
| 4886 | |
| 4887 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4888 | { |
| 4889 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4890 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4892 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4893 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4894 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4895 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4896 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4897 | failed++; |
| 4898 | else { |
| 4899 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4900 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4901 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4902 | else |
| 4903 | spare++; |
| 4904 | } |
| 4905 | } |
| 4906 | |
| 4907 | info.major_version = mddev->major_version; |
| 4908 | info.minor_version = mddev->minor_version; |
| 4909 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4910 | info.ctime = mddev->ctime; |
| 4911 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4912 | info.size = mddev->dev_sectors / 2; |
| 4913 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4914 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4915 | info.nr_disks = nr; |
| 4916 | info.raid_disks = mddev->raid_disks; |
| 4917 | info.md_minor = mddev->md_minor; |
| 4918 | info.not_persistent= !mddev->persistent; |
| 4919 | |
| 4920 | info.utime = mddev->utime; |
| 4921 | info.state = 0; |
| 4922 | if (mddev->in_sync) |
| 4923 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4924 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4925 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4926 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4927 | info.working_disks = working; |
| 4928 | info.failed_disks = failed; |
| 4929 | info.spare_disks = spare; |
| 4930 | |
| 4931 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4932 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4933 | |
| 4934 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4935 | return -EFAULT; |
| 4936 | |
| 4937 | return 0; |
| 4938 | } |
| 4939 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4940 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4941 | { |
| 4942 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4943 | char *ptr, *buf = NULL; |
| 4944 | int err = -ENOMEM; |
| 4945 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4946 | if (md_allow_write(mddev)) |
| 4947 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4948 | else |
| 4949 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4950 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4951 | if (!file) |
| 4952 | goto out; |
| 4953 | |
| 4954 | /* bitmap disabled, zero the first byte and copy out */ |
| 4955 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4956 | file->pathname[0] = '\0'; |
| 4957 | goto copy_out; |
| 4958 | } |
| 4959 | |
| 4960 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4961 | if (!buf) |
| 4962 | goto out; |
| 4963 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4964 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4965 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4966 | goto out; |
| 4967 | |
| 4968 | strcpy(file->pathname, ptr); |
| 4969 | |
| 4970 | copy_out: |
| 4971 | err = 0; |
| 4972 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4973 | err = -EFAULT; |
| 4974 | out: |
| 4975 | kfree(buf); |
| 4976 | kfree(file); |
| 4977 | return err; |
| 4978 | } |
| 4979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4980 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4981 | { |
| 4982 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4983 | mdk_rdev_t *rdev; |
| 4984 | |
| 4985 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4986 | return -EFAULT; |
| 4987 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4988 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4989 | if (rdev) { |
| 4990 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4991 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4992 | info.raid_disk = rdev->raid_disk; |
| 4993 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4994 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4995 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4996 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4998 | info.state |= (1<<MD_DISK_SYNC); |
| 4999 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5000 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5001 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5002 | } else { |
| 5003 | info.major = info.minor = 0; |
| 5004 | info.raid_disk = -1; |
| 5005 | info.state = (1<<MD_DISK_REMOVED); |
| 5006 | } |
| 5007 | |
| 5008 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5009 | return -EFAULT; |
| 5010 | |
| 5011 | return 0; |
| 5012 | } |
| 5013 | |
| 5014 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5015 | { |
| 5016 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5017 | mdk_rdev_t *rdev; |
| 5018 | dev_t dev = MKDEV(info->major,info->minor); |
| 5019 | |
| 5020 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5021 | return -EOVERFLOW; |
| 5022 | |
| 5023 | if (!mddev->raid_disks) { |
| 5024 | int err; |
| 5025 | /* expecting a device which has a superblock */ |
| 5026 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5027 | if (IS_ERR(rdev)) { |
| 5028 | printk(KERN_WARNING |
| 5029 | "md: md_import_device returned %ld\n", |
| 5030 | PTR_ERR(rdev)); |
| 5031 | return PTR_ERR(rdev); |
| 5032 | } |
| 5033 | if (!list_empty(&mddev->disks)) { |
| 5034 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5035 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5036 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5038 | if (err < 0) { |
| 5039 | printk(KERN_WARNING |
| 5040 | "md: %s has different UUID to %s\n", |
| 5041 | bdevname(rdev->bdev,b), |
| 5042 | bdevname(rdev0->bdev,b2)); |
| 5043 | export_rdev(rdev); |
| 5044 | return -EINVAL; |
| 5045 | } |
| 5046 | } |
| 5047 | err = bind_rdev_to_array(rdev, mddev); |
| 5048 | if (err) |
| 5049 | export_rdev(rdev); |
| 5050 | return err; |
| 5051 | } |
| 5052 | |
| 5053 | /* |
| 5054 | * add_new_disk can be used once the array is assembled |
| 5055 | * to add "hot spares". They must already have a superblock |
| 5056 | * written |
| 5057 | */ |
| 5058 | if (mddev->pers) { |
| 5059 | int err; |
| 5060 | if (!mddev->pers->hot_add_disk) { |
| 5061 | printk(KERN_WARNING |
| 5062 | "%s: personality does not support diskops!\n", |
| 5063 | mdname(mddev)); |
| 5064 | return -EINVAL; |
| 5065 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5066 | if (mddev->persistent) |
| 5067 | rdev = md_import_device(dev, mddev->major_version, |
| 5068 | mddev->minor_version); |
| 5069 | else |
| 5070 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | if (IS_ERR(rdev)) { |
| 5072 | printk(KERN_WARNING |
| 5073 | "md: md_import_device returned %ld\n", |
| 5074 | PTR_ERR(rdev)); |
| 5075 | return PTR_ERR(rdev); |
| 5076 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5077 | /* set save_raid_disk if appropriate */ |
| 5078 | if (!mddev->persistent) { |
| 5079 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5080 | info->raid_disk < mddev->raid_disks) |
| 5081 | rdev->raid_disk = info->raid_disk; |
| 5082 | else |
| 5083 | rdev->raid_disk = -1; |
| 5084 | } else |
| 5085 | super_types[mddev->major_version]. |
| 5086 | validate_super(mddev, rdev); |
| 5087 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5088 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5089 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5090 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5091 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5092 | else |
| 5093 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | rdev->raid_disk = -1; |
| 5096 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5097 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5098 | /* If there is hot_add_disk but no hot_remove_disk |
| 5099 | * then added disks for geometry changes, |
| 5100 | * and should be added immediately. |
| 5101 | */ |
| 5102 | super_types[mddev->major_version]. |
| 5103 | validate_super(mddev, rdev); |
| 5104 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5105 | if (err) |
| 5106 | unbind_rdev_from_array(rdev); |
| 5107 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | if (err) |
| 5109 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5110 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 5111 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5112 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5113 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5114 | if (mddev->degraded) |
| 5115 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5116 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5117 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5118 | return err; |
| 5119 | } |
| 5120 | |
| 5121 | /* otherwise, add_new_disk is only allowed |
| 5122 | * for major_version==0 superblocks |
| 5123 | */ |
| 5124 | if (mddev->major_version != 0) { |
| 5125 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5126 | mdname(mddev)); |
| 5127 | return -EINVAL; |
| 5128 | } |
| 5129 | |
| 5130 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5131 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5132 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5133 | if (IS_ERR(rdev)) { |
| 5134 | printk(KERN_WARNING |
| 5135 | "md: error, md_import_device() returned %ld\n", |
| 5136 | PTR_ERR(rdev)); |
| 5137 | return PTR_ERR(rdev); |
| 5138 | } |
| 5139 | rdev->desc_nr = info->number; |
| 5140 | if (info->raid_disk < mddev->raid_disks) |
| 5141 | rdev->raid_disk = info->raid_disk; |
| 5142 | else |
| 5143 | rdev->raid_disk = -1; |
| 5144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5145 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5146 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5147 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5148 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5149 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5150 | set_bit(WriteMostly, &rdev->flags); |
| 5151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | if (!mddev->persistent) { |
| 5153 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5154 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5155 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5156 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5157 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5158 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5159 | err = bind_rdev_to_array(rdev, mddev); |
| 5160 | if (err) { |
| 5161 | export_rdev(rdev); |
| 5162 | return err; |
| 5163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5164 | } |
| 5165 | |
| 5166 | return 0; |
| 5167 | } |
| 5168 | |
| 5169 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5170 | { |
| 5171 | char b[BDEVNAME_SIZE]; |
| 5172 | mdk_rdev_t *rdev; |
| 5173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | rdev = find_rdev(mddev, dev); |
| 5175 | if (!rdev) |
| 5176 | return -ENXIO; |
| 5177 | |
| 5178 | if (rdev->raid_disk >= 0) |
| 5179 | goto busy; |
| 5180 | |
| 5181 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5182 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5183 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | |
| 5185 | return 0; |
| 5186 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5187 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5188 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5189 | return -EBUSY; |
| 5190 | } |
| 5191 | |
| 5192 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5193 | { |
| 5194 | char b[BDEVNAME_SIZE]; |
| 5195 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | mdk_rdev_t *rdev; |
| 5197 | |
| 5198 | if (!mddev->pers) |
| 5199 | return -ENODEV; |
| 5200 | |
| 5201 | if (mddev->major_version != 0) { |
| 5202 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5203 | " version-0 superblocks.\n", |
| 5204 | mdname(mddev)); |
| 5205 | return -EINVAL; |
| 5206 | } |
| 5207 | if (!mddev->pers->hot_add_disk) { |
| 5208 | printk(KERN_WARNING |
| 5209 | "%s: personality does not support diskops!\n", |
| 5210 | mdname(mddev)); |
| 5211 | return -EINVAL; |
| 5212 | } |
| 5213 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5214 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | if (IS_ERR(rdev)) { |
| 5216 | printk(KERN_WARNING |
| 5217 | "md: error, md_import_device() returned %ld\n", |
| 5218 | PTR_ERR(rdev)); |
| 5219 | return -EINVAL; |
| 5220 | } |
| 5221 | |
| 5222 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5223 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5224 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5225 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5226 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5227 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5228 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5229 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | printk(KERN_WARNING |
| 5231 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5232 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5233 | err = -EINVAL; |
| 5234 | goto abort_export; |
| 5235 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5236 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5238 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5239 | err = bind_rdev_to_array(rdev, mddev); |
| 5240 | if (err) |
| 5241 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | |
| 5243 | /* |
| 5244 | * The rest should better be atomic, we can have disk failures |
| 5245 | * noticed in interrupt contexts ... |
| 5246 | */ |
| 5247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | rdev->raid_disk = -1; |
| 5249 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5250 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | |
| 5252 | /* |
| 5253 | * Kick recovery, maybe this spare has to be added to the |
| 5254 | * array immediately. |
| 5255 | */ |
| 5256 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5257 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5258 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | return 0; |
| 5260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | abort_export: |
| 5262 | export_rdev(rdev); |
| 5263 | return err; |
| 5264 | } |
| 5265 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5266 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5267 | { |
| 5268 | int err; |
| 5269 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5270 | if (mddev->pers) { |
| 5271 | if (!mddev->pers->quiesce) |
| 5272 | return -EBUSY; |
| 5273 | if (mddev->recovery || mddev->sync_thread) |
| 5274 | return -EBUSY; |
| 5275 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5276 | } |
| 5277 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5278 | |
| 5279 | if (fd >= 0) { |
| 5280 | if (mddev->bitmap) |
| 5281 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5282 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5283 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5284 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5285 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5286 | mdname(mddev)); |
| 5287 | return -EBADF; |
| 5288 | } |
| 5289 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5290 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5291 | if (err) { |
| 5292 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5293 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5294 | fput(mddev->bitmap_info.file); |
| 5295 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5296 | return err; |
| 5297 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5298 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5299 | } else if (mddev->bitmap == NULL) |
| 5300 | return -ENOENT; /* cannot remove what isn't there */ |
| 5301 | err = 0; |
| 5302 | if (mddev->pers) { |
| 5303 | mddev->pers->quiesce(mddev, 1); |
| 5304 | if (fd >= 0) |
| 5305 | err = bitmap_create(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5306 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5307 | bitmap_destroy(mddev); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5308 | fd = -1; /* make sure to put the file */ |
| 5309 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5310 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab32 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5311 | } |
| 5312 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5313 | if (mddev->bitmap_info.file) { |
| 5314 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5315 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5316 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5317 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5318 | } |
| 5319 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5320 | return err; |
| 5321 | } |
| 5322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5323 | /* |
| 5324 | * set_array_info is used two different ways |
| 5325 | * The original usage is when creating a new array. |
| 5326 | * In this usage, raid_disks is > 0 and it together with |
| 5327 | * level, size, not_persistent,layout,chunksize determine the |
| 5328 | * shape of the array. |
| 5329 | * This will always create an array with a type-0.90.0 superblock. |
| 5330 | * The newer usage is when assembling an array. |
| 5331 | * In this case raid_disks will be 0, and the major_version field is |
| 5332 | * use to determine which style super-blocks are to be found on the devices. |
| 5333 | * The minor and patch _version numbers are also kept incase the |
| 5334 | * super_block handler wishes to interpret them. |
| 5335 | */ |
| 5336 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5337 | { |
| 5338 | |
| 5339 | if (info->raid_disks == 0) { |
| 5340 | /* just setting version number for superblock loading */ |
| 5341 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5342 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | super_types[info->major_version].name == NULL) { |
| 5344 | /* maybe try to auto-load a module? */ |
| 5345 | printk(KERN_INFO |
| 5346 | "md: superblock version %d not known\n", |
| 5347 | info->major_version); |
| 5348 | return -EINVAL; |
| 5349 | } |
| 5350 | mddev->major_version = info->major_version; |
| 5351 | mddev->minor_version = info->minor_version; |
| 5352 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5353 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5354 | /* ensure mddev_put doesn't delete this now that there |
| 5355 | * is some minimal configuration. |
| 5356 | */ |
| 5357 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | return 0; |
| 5359 | } |
| 5360 | mddev->major_version = MD_MAJOR_VERSION; |
| 5361 | mddev->minor_version = MD_MINOR_VERSION; |
| 5362 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5363 | mddev->ctime = get_seconds(); |
| 5364 | |
| 5365 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5366 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5367 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5368 | mddev->raid_disks = info->raid_disks; |
| 5369 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5370 | * openned |
| 5371 | */ |
| 5372 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5373 | mddev->recovery_cp = MaxSector; |
| 5374 | else |
| 5375 | mddev->recovery_cp = 0; |
| 5376 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5377 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5378 | |
| 5379 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5380 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5381 | |
| 5382 | mddev->max_disks = MD_SB_DISKS; |
| 5383 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5384 | if (mddev->persistent) |
| 5385 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5386 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5388 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5389 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5390 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5391 | mddev->reshape_position = MaxSector; |
| 5392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | /* |
| 5394 | * Generate a 128 bit UUID |
| 5395 | */ |
| 5396 | get_random_bytes(mddev->uuid, 16); |
| 5397 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5398 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5399 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5400 | mddev->new_layout = mddev->layout; |
| 5401 | mddev->delta_disks = 0; |
| 5402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5403 | return 0; |
| 5404 | } |
| 5405 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5406 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5407 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5408 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5409 | |
| 5410 | if (mddev->external_size) |
| 5411 | return; |
| 5412 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5413 | mddev->array_sectors = array_sectors; |
| 5414 | } |
| 5415 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5416 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5417 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5418 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5419 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5420 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5421 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5422 | |
| 5423 | if (mddev->pers->resize == NULL) |
| 5424 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5425 | /* The "num_sectors" is the number of sectors of each device that |
| 5426 | * is used. This can only make sense for arrays with redundancy. |
| 5427 | * linear and raid0 always use whatever space is available. We can only |
| 5428 | * consider changing this number if no resync or reconstruction is |
| 5429 | * 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] | 5430 | * 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] | 5431 | * of each device. If num_sectors is zero, we find the largest size |
| 5432 | * that fits. |
| 5433 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5434 | */ |
| 5435 | if (mddev->sync_thread) |
| 5436 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5437 | if (mddev->bitmap) |
| 5438 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5439 | * grow, and re-add. |
| 5440 | */ |
| 5441 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5442 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5443 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5444 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5445 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5446 | num_sectors = avail; |
| 5447 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5448 | return -ENOSPC; |
| 5449 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5450 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5451 | if (!rv) |
| 5452 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5453 | return rv; |
| 5454 | } |
| 5455 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5456 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5457 | { |
| 5458 | int rv; |
| 5459 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5460 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5461 | return -EINVAL; |
| 5462 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5463 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5464 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5465 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5466 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5467 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5468 | |
| 5469 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5470 | return rv; |
| 5471 | } |
| 5472 | |
| 5473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | /* |
| 5475 | * update_array_info is used to change the configuration of an |
| 5476 | * on-line array. |
| 5477 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5478 | * fields in the info are checked against the array. |
| 5479 | * Any differences that cannot be handled will cause an error. |
| 5480 | * Normally, only one change can be managed at a time. |
| 5481 | */ |
| 5482 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5483 | { |
| 5484 | int rv = 0; |
| 5485 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5486 | int state = 0; |
| 5487 | |
| 5488 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5489 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5490 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | |
| 5492 | if (mddev->major_version != info->major_version || |
| 5493 | mddev->minor_version != info->minor_version || |
| 5494 | /* mddev->patch_version != info->patch_version || */ |
| 5495 | mddev->ctime != info->ctime || |
| 5496 | mddev->level != info->level || |
| 5497 | /* mddev->layout != info->layout || */ |
| 5498 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5499 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5500 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5501 | ((state^info->state) & 0xfffffe00) |
| 5502 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | return -EINVAL; |
| 5504 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5505 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5506 | cnt++; |
| 5507 | if (mddev->raid_disks != info->raid_disks) |
| 5508 | cnt++; |
| 5509 | if (mddev->layout != info->layout) |
| 5510 | cnt++; |
| 5511 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5512 | cnt++; |
| 5513 | if (cnt == 0) |
| 5514 | return 0; |
| 5515 | if (cnt > 1) |
| 5516 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5517 | |
| 5518 | if (mddev->layout != info->layout) { |
| 5519 | /* Change layout |
| 5520 | * we don't need to do anything at the md level, the |
| 5521 | * personality will take care of it all. |
| 5522 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5523 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5525 | else { |
| 5526 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5527 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5528 | if (rv) |
| 5529 | mddev->new_layout = mddev->layout; |
| 5530 | return rv; |
| 5531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5533 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5534 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5536 | if (mddev->raid_disks != info->raid_disks) |
| 5537 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5538 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5539 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5540 | if (mddev->pers->quiesce == NULL) |
| 5541 | return -EINVAL; |
| 5542 | if (mddev->recovery || mddev->sync_thread) |
| 5543 | return -EBUSY; |
| 5544 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5545 | /* add the bitmap */ |
| 5546 | if (mddev->bitmap) |
| 5547 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5548 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5549 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5550 | mddev->bitmap_info.offset = |
| 5551 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5552 | mddev->pers->quiesce(mddev, 1); |
| 5553 | rv = bitmap_create(mddev); |
| 5554 | if (rv) |
| 5555 | bitmap_destroy(mddev); |
| 5556 | mddev->pers->quiesce(mddev, 0); |
| 5557 | } else { |
| 5558 | /* remove the bitmap */ |
| 5559 | if (!mddev->bitmap) |
| 5560 | return -ENOENT; |
| 5561 | if (mddev->bitmap->file) |
| 5562 | return -EINVAL; |
| 5563 | mddev->pers->quiesce(mddev, 1); |
| 5564 | bitmap_destroy(mddev); |
| 5565 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5566 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5567 | } |
| 5568 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5569 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | return rv; |
| 5571 | } |
| 5572 | |
| 5573 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5574 | { |
| 5575 | mdk_rdev_t *rdev; |
| 5576 | |
| 5577 | if (mddev->pers == NULL) |
| 5578 | return -ENODEV; |
| 5579 | |
| 5580 | rdev = find_rdev(mddev, dev); |
| 5581 | if (!rdev) |
| 5582 | return -ENODEV; |
| 5583 | |
| 5584 | md_error(mddev, rdev); |
| 5585 | return 0; |
| 5586 | } |
| 5587 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5588 | /* |
| 5589 | * We have a problem here : there is no easy way to give a CHS |
| 5590 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5591 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5592 | * dosfs just mad... ;-) |
| 5593 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5594 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5595 | { |
| 5596 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5597 | |
| 5598 | geo->heads = 2; |
| 5599 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5600 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5601 | return 0; |
| 5602 | } |
| 5603 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5604 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | unsigned int cmd, unsigned long arg) |
| 5606 | { |
| 5607 | int err = 0; |
| 5608 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5610 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5611 | |
| 5612 | if (!capable(CAP_SYS_ADMIN)) |
| 5613 | return -EACCES; |
| 5614 | |
| 5615 | /* |
| 5616 | * Commands dealing with the RAID driver but not any |
| 5617 | * particular array: |
| 5618 | */ |
| 5619 | switch (cmd) |
| 5620 | { |
| 5621 | case RAID_VERSION: |
| 5622 | err = get_version(argp); |
| 5623 | goto done; |
| 5624 | |
| 5625 | case PRINT_RAID_DEBUG: |
| 5626 | err = 0; |
| 5627 | md_print_devices(); |
| 5628 | goto done; |
| 5629 | |
| 5630 | #ifndef MODULE |
| 5631 | case RAID_AUTORUN: |
| 5632 | err = 0; |
| 5633 | autostart_arrays(arg); |
| 5634 | goto done; |
| 5635 | #endif |
| 5636 | default:; |
| 5637 | } |
| 5638 | |
| 5639 | /* |
| 5640 | * Commands creating/starting a new array: |
| 5641 | */ |
| 5642 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5643 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | |
| 5645 | if (!mddev) { |
| 5646 | BUG(); |
| 5647 | goto abort; |
| 5648 | } |
| 5649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | err = mddev_lock(mddev); |
| 5651 | if (err) { |
| 5652 | printk(KERN_INFO |
| 5653 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5654 | err, cmd); |
| 5655 | goto abort; |
| 5656 | } |
| 5657 | |
| 5658 | switch (cmd) |
| 5659 | { |
| 5660 | case SET_ARRAY_INFO: |
| 5661 | { |
| 5662 | mdu_array_info_t info; |
| 5663 | if (!arg) |
| 5664 | memset(&info, 0, sizeof(info)); |
| 5665 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5666 | err = -EFAULT; |
| 5667 | goto abort_unlock; |
| 5668 | } |
| 5669 | if (mddev->pers) { |
| 5670 | err = update_array_info(mddev, &info); |
| 5671 | if (err) { |
| 5672 | printk(KERN_WARNING "md: couldn't update" |
| 5673 | " array info. %d\n", err); |
| 5674 | goto abort_unlock; |
| 5675 | } |
| 5676 | goto done_unlock; |
| 5677 | } |
| 5678 | if (!list_empty(&mddev->disks)) { |
| 5679 | printk(KERN_WARNING |
| 5680 | "md: array %s already has disks!\n", |
| 5681 | mdname(mddev)); |
| 5682 | err = -EBUSY; |
| 5683 | goto abort_unlock; |
| 5684 | } |
| 5685 | if (mddev->raid_disks) { |
| 5686 | printk(KERN_WARNING |
| 5687 | "md: array %s already initialised!\n", |
| 5688 | mdname(mddev)); |
| 5689 | err = -EBUSY; |
| 5690 | goto abort_unlock; |
| 5691 | } |
| 5692 | err = set_array_info(mddev, &info); |
| 5693 | if (err) { |
| 5694 | printk(KERN_WARNING "md: couldn't set" |
| 5695 | " array info. %d\n", err); |
| 5696 | goto abort_unlock; |
| 5697 | } |
| 5698 | } |
| 5699 | goto done_unlock; |
| 5700 | |
| 5701 | default:; |
| 5702 | } |
| 5703 | |
| 5704 | /* |
| 5705 | * Commands querying/configuring an existing array: |
| 5706 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5707 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5708 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5709 | if ((!mddev->raid_disks && !mddev->external) |
| 5710 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5711 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5712 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | err = -ENODEV; |
| 5714 | goto abort_unlock; |
| 5715 | } |
| 5716 | |
| 5717 | /* |
| 5718 | * Commands even a read-only array can execute: |
| 5719 | */ |
| 5720 | switch (cmd) |
| 5721 | { |
| 5722 | case GET_ARRAY_INFO: |
| 5723 | err = get_array_info(mddev, argp); |
| 5724 | goto done_unlock; |
| 5725 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5726 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5727 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5728 | goto done_unlock; |
| 5729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | case GET_DISK_INFO: |
| 5731 | err = get_disk_info(mddev, argp); |
| 5732 | goto done_unlock; |
| 5733 | |
| 5734 | case RESTART_ARRAY_RW: |
| 5735 | err = restart_array(mddev); |
| 5736 | goto done_unlock; |
| 5737 | |
| 5738 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5739 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | goto done_unlock; |
| 5741 | |
| 5742 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5743 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | goto done_unlock; |
| 5745 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5746 | case BLKROSET: |
| 5747 | if (get_user(ro, (int __user *)(arg))) { |
| 5748 | err = -EFAULT; |
| 5749 | goto done_unlock; |
| 5750 | } |
| 5751 | err = -EINVAL; |
| 5752 | |
| 5753 | /* if the bdev is going readonly the value of mddev->ro |
| 5754 | * does not matter, no writes are coming |
| 5755 | */ |
| 5756 | if (ro) |
| 5757 | goto done_unlock; |
| 5758 | |
| 5759 | /* are we are already prepared for writes? */ |
| 5760 | if (mddev->ro != 1) |
| 5761 | goto done_unlock; |
| 5762 | |
| 5763 | /* transitioning to readauto need only happen for |
| 5764 | * arrays that call md_write_start |
| 5765 | */ |
| 5766 | if (mddev->pers) { |
| 5767 | err = restart_array(mddev); |
| 5768 | if (err == 0) { |
| 5769 | mddev->ro = 2; |
| 5770 | set_disk_ro(mddev->gendisk, 0); |
| 5771 | } |
| 5772 | } |
| 5773 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | } |
| 5775 | |
| 5776 | /* |
| 5777 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5778 | * superblock, so we do not allow them on read-only arrays. |
| 5779 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5780 | * here and hit the 'default' below, so only disallow |
| 5781 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5783 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5784 | if (mddev->ro == 2) { |
| 5785 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5786 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5787 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5788 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5789 | } else { |
| 5790 | err = -EROFS; |
| 5791 | goto abort_unlock; |
| 5792 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5793 | } |
| 5794 | |
| 5795 | switch (cmd) |
| 5796 | { |
| 5797 | case ADD_NEW_DISK: |
| 5798 | { |
| 5799 | mdu_disk_info_t info; |
| 5800 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5801 | err = -EFAULT; |
| 5802 | else |
| 5803 | err = add_new_disk(mddev, &info); |
| 5804 | goto done_unlock; |
| 5805 | } |
| 5806 | |
| 5807 | case HOT_REMOVE_DISK: |
| 5808 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5809 | goto done_unlock; |
| 5810 | |
| 5811 | case HOT_ADD_DISK: |
| 5812 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5813 | goto done_unlock; |
| 5814 | |
| 5815 | case SET_DISK_FAULTY: |
| 5816 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5817 | goto done_unlock; |
| 5818 | |
| 5819 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5820 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | goto done_unlock; |
| 5822 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5823 | case SET_BITMAP_FILE: |
| 5824 | err = set_bitmap_file(mddev, (int)arg); |
| 5825 | goto done_unlock; |
| 5826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | err = -EINVAL; |
| 5829 | goto abort_unlock; |
| 5830 | } |
| 5831 | |
| 5832 | done_unlock: |
| 5833 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5834 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5835 | err != -EINVAL) |
| 5836 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | mddev_unlock(mddev); |
| 5838 | |
| 5839 | return err; |
| 5840 | done: |
| 5841 | if (err) |
| 5842 | MD_BUG(); |
| 5843 | abort: |
| 5844 | return err; |
| 5845 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5846 | #ifdef CONFIG_COMPAT |
| 5847 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5848 | unsigned int cmd, unsigned long arg) |
| 5849 | { |
| 5850 | switch (cmd) { |
| 5851 | case HOT_REMOVE_DISK: |
| 5852 | case HOT_ADD_DISK: |
| 5853 | case SET_DISK_FAULTY: |
| 5854 | case SET_BITMAP_FILE: |
| 5855 | /* These take in integer arg, do not convert */ |
| 5856 | break; |
| 5857 | default: |
| 5858 | arg = (unsigned long)compat_ptr(arg); |
| 5859 | break; |
| 5860 | } |
| 5861 | |
| 5862 | return md_ioctl(bdev, mode, cmd, arg); |
| 5863 | } |
| 5864 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5866 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5867 | { |
| 5868 | /* |
| 5869 | * Succeed if we can lock the mddev, which confirms that |
| 5870 | * it isn't being stopped right now. |
| 5871 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5872 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | int err; |
| 5874 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5875 | if (mddev->gendisk != bdev->bd_disk) { |
| 5876 | /* we are racing with mddev_put which is discarding this |
| 5877 | * bd_disk. |
| 5878 | */ |
| 5879 | mddev_put(mddev); |
| 5880 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5881 | flush_scheduled_work(); |
| 5882 | /* Then retry the open from the top */ |
| 5883 | return -ERESTARTSYS; |
| 5884 | } |
| 5885 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5886 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5887 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5888 | goto out; |
| 5889 | |
| 5890 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5891 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5892 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5894 | out: |
| 5895 | return err; |
| 5896 | } |
| 5897 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5898 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5900 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5901 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5902 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5903 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | mddev_put(mddev); |
| 5905 | |
| 5906 | return 0; |
| 5907 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5908 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5909 | { |
| 5910 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5911 | .open = md_open, |
| 5912 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5913 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5914 | #ifdef CONFIG_COMPAT |
| 5915 | .compat_ioctl = md_compat_ioctl, |
| 5916 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5917 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5918 | }; |
| 5919 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5920 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | { |
| 5922 | mdk_thread_t *thread = arg; |
| 5923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | /* |
| 5925 | * md_thread is a 'system-thread', it's priority should be very |
| 5926 | * high. We avoid resource deadlocks individually in each |
| 5927 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5928 | * the very same RT priority as kswapd, thus we will never get |
| 5929 | * into a priority inversion deadlock. |
| 5930 | * |
| 5931 | * we definitely have to have equal or higher priority than |
| 5932 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5933 | * many dirty RAID5 blocks. |
| 5934 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5935 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5936 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5937 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5939 | /* We need to wait INTERRUPTIBLE so that |
| 5940 | * we don't add to the load-average. |
| 5941 | * That means we need to be sure no signals are |
| 5942 | * pending |
| 5943 | */ |
| 5944 | if (signal_pending(current)) |
| 5945 | flush_signals(current); |
| 5946 | |
| 5947 | wait_event_interruptible_timeout |
| 5948 | (thread->wqueue, |
| 5949 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5950 | || kthread_should_stop(), |
| 5951 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | |
| 5953 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5954 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5955 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5958 | return 0; |
| 5959 | } |
| 5960 | |
| 5961 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5962 | { |
| 5963 | if (thread) { |
| 5964 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5965 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5966 | wake_up(&thread->wqueue); |
| 5967 | } |
| 5968 | } |
| 5969 | |
| 5970 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5971 | const char *name) |
| 5972 | { |
| 5973 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5974 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5975 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5976 | if (!thread) |
| 5977 | return NULL; |
| 5978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5979 | init_waitqueue_head(&thread->wqueue); |
| 5980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5981 | thread->run = run; |
| 5982 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5983 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5984 | thread->tsk = kthread_run(md_thread, thread, |
| 5985 | "%s_%s", |
| 5986 | mdname(thread->mddev), |
| 5987 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5988 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | kfree(thread); |
| 5990 | return NULL; |
| 5991 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5992 | return thread; |
| 5993 | } |
| 5994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | void md_unregister_thread(mdk_thread_t *thread) |
| 5996 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5997 | if (!thread) |
| 5998 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5999 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6000 | |
| 6001 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | kfree(thread); |
| 6003 | } |
| 6004 | |
| 6005 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6006 | { |
| 6007 | if (!mddev) { |
| 6008 | MD_BUG(); |
| 6009 | return; |
| 6010 | } |
| 6011 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6012 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6013 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6014 | |
| 6015 | if (mddev->external) |
| 6016 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6017 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6018 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6019 | mdname(mddev), |
| 6020 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6021 | __builtin_return_address(0),__builtin_return_address(1), |
| 6022 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6023 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6024 | if (!mddev->pers) |
| 6025 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | if (!mddev->pers->error_handler) |
| 6027 | return; |
| 6028 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6029 | if (mddev->degraded) |
| 6030 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c0cc75f | 2010-03-22 10:28:51 +1100 | [diff] [blame] | 6031 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6033 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6034 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6035 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6036 | } |
| 6037 | |
| 6038 | /* seq_file implementation /proc/mdstat */ |
| 6039 | |
| 6040 | static void status_unused(struct seq_file *seq) |
| 6041 | { |
| 6042 | int i = 0; |
| 6043 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6044 | |
| 6045 | seq_printf(seq, "unused devices: "); |
| 6046 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6047 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | char b[BDEVNAME_SIZE]; |
| 6049 | i++; |
| 6050 | seq_printf(seq, "%s ", |
| 6051 | bdevname(rdev->bdev,b)); |
| 6052 | } |
| 6053 | if (!i) |
| 6054 | seq_printf(seq, "<none>"); |
| 6055 | |
| 6056 | seq_printf(seq, "\n"); |
| 6057 | } |
| 6058 | |
| 6059 | |
| 6060 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6061 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6062 | sector_t max_sectors, resync, res; |
| 6063 | unsigned long dt, db; |
| 6064 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6065 | int scale; |
| 6066 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6068 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6069 | |
| 6070 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6071 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6073 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | |
| 6075 | /* |
| 6076 | * Should not happen. |
| 6077 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6078 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | MD_BUG(); |
| 6080 | return; |
| 6081 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6082 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6083 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6084 | * u32, as those are the requirements for sector_div. |
| 6085 | * Thus 'scale' must be at least 10 |
| 6086 | */ |
| 6087 | scale = 10; |
| 6088 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6089 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6090 | scale++; |
| 6091 | } |
| 6092 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6093 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6094 | |
| 6095 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6097 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | seq_printf(seq, "["); |
| 6099 | for (i = 0; i < x; i++) |
| 6100 | seq_printf(seq, "="); |
| 6101 | seq_printf(seq, ">"); |
| 6102 | for (i = 0; i < y; i++) |
| 6103 | seq_printf(seq, "."); |
| 6104 | seq_printf(seq, "] "); |
| 6105 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6106 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6107 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6108 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6109 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6110 | "check" : |
| 6111 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6112 | "resync" : "recovery"))), |
| 6113 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6114 | (unsigned long long) resync/2, |
| 6115 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | |
| 6117 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6118 | * dt: time from mark until now |
| 6119 | * db: blocks written from mark until now |
| 6120 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6121 | * |
| 6122 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6123 | * So we divide before multiply in case it is 32bit and close |
| 6124 | * to the limit. |
| 6125 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6126 | * near the end of resync when the number of remaining sectors |
| 6127 | * is close to 'db'. |
| 6128 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6129 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | */ |
| 6131 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6132 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6133 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6134 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6135 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6136 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6137 | sector_div(rt, db/32+1); |
| 6138 | rt *= dt; |
| 6139 | rt >>= 5; |
| 6140 | |
| 6141 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6142 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6143 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6144 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | } |
| 6146 | |
| 6147 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6148 | { |
| 6149 | struct list_head *tmp; |
| 6150 | loff_t l = *pos; |
| 6151 | mddev_t *mddev; |
| 6152 | |
| 6153 | if (l >= 0x10000) |
| 6154 | return NULL; |
| 6155 | if (!l--) |
| 6156 | /* header */ |
| 6157 | return (void*)1; |
| 6158 | |
| 6159 | spin_lock(&all_mddevs_lock); |
| 6160 | list_for_each(tmp,&all_mddevs) |
| 6161 | if (!l--) { |
| 6162 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6163 | mddev_get(mddev); |
| 6164 | spin_unlock(&all_mddevs_lock); |
| 6165 | return mddev; |
| 6166 | } |
| 6167 | spin_unlock(&all_mddevs_lock); |
| 6168 | if (!l--) |
| 6169 | return (void*)2;/* tail */ |
| 6170 | return NULL; |
| 6171 | } |
| 6172 | |
| 6173 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6174 | { |
| 6175 | struct list_head *tmp; |
| 6176 | mddev_t *next_mddev, *mddev = v; |
| 6177 | |
| 6178 | ++*pos; |
| 6179 | if (v == (void*)2) |
| 6180 | return NULL; |
| 6181 | |
| 6182 | spin_lock(&all_mddevs_lock); |
| 6183 | if (v == (void*)1) |
| 6184 | tmp = all_mddevs.next; |
| 6185 | else |
| 6186 | tmp = mddev->all_mddevs.next; |
| 6187 | if (tmp != &all_mddevs) |
| 6188 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6189 | else { |
| 6190 | next_mddev = (void*)2; |
| 6191 | *pos = 0x10000; |
| 6192 | } |
| 6193 | spin_unlock(&all_mddevs_lock); |
| 6194 | |
| 6195 | if (v != (void*)1) |
| 6196 | mddev_put(mddev); |
| 6197 | return next_mddev; |
| 6198 | |
| 6199 | } |
| 6200 | |
| 6201 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6202 | { |
| 6203 | mddev_t *mddev = v; |
| 6204 | |
| 6205 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6206 | mddev_put(mddev); |
| 6207 | } |
| 6208 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6209 | struct mdstat_info { |
| 6210 | int event; |
| 6211 | }; |
| 6212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6214 | { |
| 6215 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6216 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6217 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6218 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6219 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | |
| 6221 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6222 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6223 | seq_printf(seq, "Personalities : "); |
| 6224 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6225 | list_for_each_entry(pers, &pers_list, list) |
| 6226 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | |
| 6228 | spin_unlock(&pers_lock); |
| 6229 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6230 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | return 0; |
| 6232 | } |
| 6233 | if (v == (void*)2) { |
| 6234 | status_unused(seq); |
| 6235 | return 0; |
| 6236 | } |
| 6237 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6238 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6239 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6242 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6243 | mddev->pers ? "" : "in"); |
| 6244 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6245 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6247 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6248 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | seq_printf(seq, " %s", mddev->pers->name); |
| 6250 | } |
| 6251 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6252 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6253 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | char b[BDEVNAME_SIZE]; |
| 6255 | seq_printf(seq, " %s[%d]", |
| 6256 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6257 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6258 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6259 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6260 | seq_printf(seq, "(F)"); |
| 6261 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6262 | } else if (rdev->raid_disk < 0) |
| 6263 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6264 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6265 | } |
| 6266 | |
| 6267 | if (!list_empty(&mddev->disks)) { |
| 6268 | if (mddev->pers) |
| 6269 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6270 | (unsigned long long) |
| 6271 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6272 | else |
| 6273 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6274 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6275 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6276 | if (mddev->persistent) { |
| 6277 | if (mddev->major_version != 0 || |
| 6278 | mddev->minor_version != 90) { |
| 6279 | seq_printf(seq," super %d.%d", |
| 6280 | mddev->major_version, |
| 6281 | mddev->minor_version); |
| 6282 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6283 | } else if (mddev->external) |
| 6284 | seq_printf(seq, " super external:%s", |
| 6285 | mddev->metadata_type); |
| 6286 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6287 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6288 | |
| 6289 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6290 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6291 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6292 | if (mddev->pers->sync_request) { |
| 6293 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6294 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6295 | seq_printf(seq, "\n "); |
| 6296 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6297 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6298 | else if (mddev->recovery_cp < MaxSector) |
| 6299 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6300 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6301 | } else |
| 6302 | seq_printf(seq, "\n "); |
| 6303 | |
| 6304 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6305 | unsigned long chunk_kb; |
| 6306 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6307 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6308 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6309 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6310 | "%lu%s chunk", |
| 6311 | bitmap->pages - bitmap->missing_pages, |
| 6312 | bitmap->pages, |
| 6313 | (bitmap->pages - bitmap->missing_pages) |
| 6314 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6315 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6316 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6317 | if (bitmap->file) { |
| 6318 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6319 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6320 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6321 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6322 | seq_printf(seq, "\n"); |
| 6323 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | seq_printf(seq, "\n"); |
| 6327 | } |
| 6328 | mddev_unlock(mddev); |
| 6329 | |
| 6330 | return 0; |
| 6331 | } |
| 6332 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6333 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6334 | .start = md_seq_start, |
| 6335 | .next = md_seq_next, |
| 6336 | .stop = md_seq_stop, |
| 6337 | .show = md_seq_show, |
| 6338 | }; |
| 6339 | |
| 6340 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6341 | { |
| 6342 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6343 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6344 | if (mi == NULL) |
| 6345 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6346 | |
| 6347 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6348 | if (error) |
| 6349 | kfree(mi); |
| 6350 | else { |
| 6351 | struct seq_file *p = file->private_data; |
| 6352 | p->private = mi; |
| 6353 | mi->event = atomic_read(&md_event_count); |
| 6354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6355 | return error; |
| 6356 | } |
| 6357 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6358 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6359 | { |
| 6360 | struct seq_file *m = filp->private_data; |
| 6361 | struct mdstat_info *mi = m->private; |
| 6362 | int mask; |
| 6363 | |
| 6364 | poll_wait(filp, &md_event_waiters, wait); |
| 6365 | |
| 6366 | /* always allow read */ |
| 6367 | mask = POLLIN | POLLRDNORM; |
| 6368 | |
| 6369 | if (mi->event != atomic_read(&md_event_count)) |
| 6370 | mask |= POLLERR | POLLPRI; |
| 6371 | return mask; |
| 6372 | } |
| 6373 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6374 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6375 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6376 | .open = md_seq_open, |
| 6377 | .read = seq_read, |
| 6378 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6379 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6380 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | }; |
| 6382 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6383 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6384 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6386 | list_add_tail(&p->list, &pers_list); |
| 6387 | 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] | 6388 | spin_unlock(&pers_lock); |
| 6389 | return 0; |
| 6390 | } |
| 6391 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6392 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6394 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6395 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6396 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6397 | spin_unlock(&pers_lock); |
| 6398 | return 0; |
| 6399 | } |
| 6400 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6401 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | { |
| 6403 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6404 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6405 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | |
| 6407 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6408 | rcu_read_lock(); |
| 6409 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6410 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6411 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6412 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6413 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6414 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6415 | * as sync_io is counted when a request starts, and |
| 6416 | * disk_stats is counted when it completes. |
| 6417 | * So resync activity will cause curr_events to be smaller than |
| 6418 | * when there was no such activity. |
| 6419 | * non-sync IO will cause disk_stat to increase without |
| 6420 | * increasing sync_io so curr_events will (eventually) |
| 6421 | * be larger than it was before. Once it becomes |
| 6422 | * substantially larger, the test below will cause |
| 6423 | * the array to appear non-idle, and resync will slow |
| 6424 | * down. |
| 6425 | * If there is a lot of outstanding resync activity when |
| 6426 | * we set last_event to curr_events, then all that activity |
| 6427 | * completing might cause the array to appear non-idle |
| 6428 | * and resync will be slowed down even though there might |
| 6429 | * not have been non-resync activity. This will only |
| 6430 | * happen once though. 'last_events' will soon reflect |
| 6431 | * the state where there is little or no outstanding |
| 6432 | * resync requests, and further resync activity will |
| 6433 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6434 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6436 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | rdev->last_events = curr_events; |
| 6438 | idle = 0; |
| 6439 | } |
| 6440 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6441 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6442 | return idle; |
| 6443 | } |
| 6444 | |
| 6445 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6446 | { |
| 6447 | /* another "blocks" (512byte) blocks have been synced */ |
| 6448 | atomic_sub(blocks, &mddev->recovery_active); |
| 6449 | wake_up(&mddev->recovery_wait); |
| 6450 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6451 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6452 | md_wakeup_thread(mddev->thread); |
| 6453 | // stop recovery, signal do_sync .... |
| 6454 | } |
| 6455 | } |
| 6456 | |
| 6457 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6458 | /* md_write_start(mddev, bi) |
| 6459 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6460 | * in superblock) before writing, schedule a superblock update |
| 6461 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6462 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6463 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6464 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6465 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6466 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6467 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6468 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6469 | BUG_ON(mddev->ro == 1); |
| 6470 | if (mddev->ro == 2) { |
| 6471 | /* need to switch to read/write */ |
| 6472 | mddev->ro = 0; |
| 6473 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6474 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6475 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6476 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6477 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6478 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6479 | if (mddev->safemode == 1) |
| 6480 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6481 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6482 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6483 | if (mddev->in_sync) { |
| 6484 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6485 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6486 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6487 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6488 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6489 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6490 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6491 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6492 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6493 | wait_event(mddev->sb_wait, |
| 6494 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6495 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | } |
| 6497 | |
| 6498 | void md_write_end(mddev_t *mddev) |
| 6499 | { |
| 6500 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6501 | if (mddev->safemode == 2) |
| 6502 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6503 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6505 | } |
| 6506 | } |
| 6507 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6508 | /* md_allow_write(mddev) |
| 6509 | * Calling this ensures that the array is marked 'active' so that writes |
| 6510 | * may proceed without blocking. It is important to call this before |
| 6511 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6512 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6513 | * |
| 6514 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6515 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6516 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6517 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6518 | { |
| 6519 | if (!mddev->pers) |
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 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6522 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6523 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6524 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6525 | |
| 6526 | spin_lock_irq(&mddev->write_lock); |
| 6527 | if (mddev->in_sync) { |
| 6528 | mddev->in_sync = 0; |
| 6529 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6530 | if (mddev->safemode_delay && |
| 6531 | mddev->safemode == 0) |
| 6532 | mddev->safemode = 1; |
| 6533 | spin_unlock_irq(&mddev->write_lock); |
| 6534 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6535 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6536 | } else |
| 6537 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6538 | |
| 6539 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6540 | return -EAGAIN; |
| 6541 | else |
| 6542 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6543 | } |
| 6544 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6546 | #define SYNC_MARKS 10 |
| 6547 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6548 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6549 | { |
| 6550 | mddev_t *mddev2; |
| 6551 | unsigned int currspeed = 0, |
| 6552 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6553 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6554 | unsigned long mark[SYNC_MARKS]; |
| 6555 | sector_t mark_cnt[SYNC_MARKS]; |
| 6556 | int last_mark,m; |
| 6557 | struct list_head *tmp; |
| 6558 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6559 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6560 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6561 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6562 | |
| 6563 | /* just incase thread restarts... */ |
| 6564 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6565 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6566 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6567 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6569 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6570 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6571 | desc = "data-check"; |
| 6572 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6573 | desc = "requested-resync"; |
| 6574 | else |
| 6575 | desc = "resync"; |
| 6576 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6577 | desc = "reshape"; |
| 6578 | else |
| 6579 | desc = "recovery"; |
| 6580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6581 | /* we overload curr_resync somewhat here. |
| 6582 | * 0 == not engaged in resync at all |
| 6583 | * 2 == checking that there is no conflict with another sync |
| 6584 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6585 | * commense |
| 6586 | * other == active in resync - this many blocks |
| 6587 | * |
| 6588 | * Before starting a resync we must have set curr_resync to |
| 6589 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6590 | * less than ours. When we find one that is the same or higher |
| 6591 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6592 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6593 | * This will mean we have to start checking from the beginning again. |
| 6594 | * |
| 6595 | */ |
| 6596 | |
| 6597 | do { |
| 6598 | mddev->curr_resync = 2; |
| 6599 | |
| 6600 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6601 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6602 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6603 | |
| 6604 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6606 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 | if (mddev2 == mddev) |
| 6608 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6609 | if (!mddev->parallel_resync |
| 6610 | && mddev2->curr_resync |
| 6611 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6612 | DEFINE_WAIT(wq); |
| 6613 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6614 | /* arbitrarily yield */ |
| 6615 | mddev->curr_resync = 1; |
| 6616 | wake_up(&resync_wait); |
| 6617 | } |
| 6618 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6619 | /* no need to wait here, we can wait the next |
| 6620 | * time 'round when curr_resync == 2 |
| 6621 | */ |
| 6622 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6623 | /* We need to wait 'interruptible' so as not to |
| 6624 | * contribute to the load average, and not to |
| 6625 | * be caught by 'softlockup' |
| 6626 | */ |
| 6627 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6628 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6629 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6630 | printk(KERN_INFO "md: delaying %s of %s" |
| 6631 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6632 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6633 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6634 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6635 | if (signal_pending(current)) |
| 6636 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6637 | schedule(); |
| 6638 | finish_wait(&resync_wait, &wq); |
| 6639 | goto try_again; |
| 6640 | } |
| 6641 | finish_wait(&resync_wait, &wq); |
| 6642 | } |
| 6643 | } |
| 6644 | } while (mddev->curr_resync < 2); |
| 6645 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6646 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6647 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6649 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | */ |
| 6651 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6652 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6653 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6654 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6655 | j = mddev->resync_min; |
| 6656 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6657 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6658 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6659 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6660 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6661 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6663 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6664 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6665 | rcu_read_lock(); |
| 6666 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6667 | if (rdev->raid_disk >= 0 && |
| 6668 | !test_bit(Faulty, &rdev->flags) && |
| 6669 | !test_bit(In_sync, &rdev->flags) && |
| 6670 | rdev->recovery_offset < j) |
| 6671 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6672 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6674 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6675 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6676 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6677 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6678 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6679 | "(but not more than %d KB/sec) for %s.\n", |
| 6680 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6681 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6682 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6683 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6684 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6685 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6686 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6687 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6688 | } |
| 6689 | last_mark = 0; |
| 6690 | mddev->resync_mark = mark[last_mark]; |
| 6691 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6692 | |
| 6693 | /* |
| 6694 | * Tune reconstruction: |
| 6695 | */ |
| 6696 | window = 32*(PAGE_SIZE/512); |
| 6697 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6698 | window/2,(unsigned long long) max_sectors/2); |
| 6699 | |
| 6700 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6701 | last_check = 0; |
| 6702 | |
| 6703 | if (j>2) { |
| 6704 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6705 | "md: resuming %s of %s from checkpoint.\n", |
| 6706 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6707 | mddev->curr_resync = j; |
| 6708 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6709 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6710 | |
| 6711 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6712 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6714 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6715 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6716 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6717 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6718 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6719 | > (max_sectors >> 4)) || |
| 6720 | (j - mddev->curr_resync_completed)*2 |
| 6721 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6722 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6723 | /* time to update curr_resync_completed */ |
| 6724 | blk_unplug(mddev->queue); |
| 6725 | wait_event(mddev->recovery_wait, |
| 6726 | atomic_read(&mddev->recovery_active) == 0); |
| 6727 | mddev->curr_resync_completed = |
| 6728 | mddev->curr_resync; |
| 6729 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6730 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6731 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6732 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6733 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6734 | /* As this condition is controlled by user-space, |
| 6735 | * we can block indefinitely, so use '_interruptible' |
| 6736 | * to avoid triggering warnings. |
| 6737 | */ |
| 6738 | flush_signals(current); /* just in case */ |
| 6739 | wait_event_interruptible(mddev->recovery_wait, |
| 6740 | mddev->resync_max > j |
| 6741 | || kthread_should_stop()); |
| 6742 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6743 | |
| 6744 | if (kthread_should_stop()) |
| 6745 | goto interrupted; |
| 6746 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6747 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6748 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6749 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6750 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6751 | goto out; |
| 6752 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6753 | |
| 6754 | if (!skipped) { /* actual IO requested */ |
| 6755 | io_sectors += sectors; |
| 6756 | atomic_add(sectors, &mddev->recovery_active); |
| 6757 | } |
| 6758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6759 | j += sectors; |
| 6760 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6761 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6762 | if (last_check == 0) |
| 6763 | /* this is the earliers that rebuilt will be |
| 6764 | * visible in /proc/mdstat |
| 6765 | */ |
| 6766 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6767 | |
| 6768 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | continue; |
| 6770 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6771 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6772 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6773 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6774 | break; |
| 6775 | |
| 6776 | repeat: |
| 6777 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6778 | /* step marks */ |
| 6779 | int next = (last_mark+1) % SYNC_MARKS; |
| 6780 | |
| 6781 | mddev->resync_mark = mark[next]; |
| 6782 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6783 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6784 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6785 | last_mark = next; |
| 6786 | } |
| 6787 | |
| 6788 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6789 | if (kthread_should_stop()) |
| 6790 | goto interrupted; |
| 6791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6792 | |
| 6793 | /* |
| 6794 | * this loop exits only if either when we are slower than |
| 6795 | * the 'hard' speed limit, or the system was IO-idle for |
| 6796 | * a jiffy. |
| 6797 | * the system might be non-idle CPU-wise, but we only care |
| 6798 | * about not overloading the IO subsystem. (things like an |
| 6799 | * e2fsck being done on the RAID array should execute fast) |
| 6800 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6801 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | cond_resched(); |
| 6803 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6804 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6805 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6807 | if (currspeed > speed_min(mddev)) { |
| 6808 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6809 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6810 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6811 | goto repeat; |
| 6812 | } |
| 6813 | } |
| 6814 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6815 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | /* |
| 6817 | * this also signals 'finished resyncing' to md_stop |
| 6818 | */ |
| 6819 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6820 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6821 | |
| 6822 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6823 | |
| 6824 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6825 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6826 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6827 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6828 | mddev->curr_resync > 2) { |
| 6829 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6830 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6831 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6832 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6833 | "md: checkpointing %s of %s.\n", |
| 6834 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6835 | mddev->recovery_cp = mddev->curr_resync; |
| 6836 | } |
| 6837 | } else |
| 6838 | mddev->recovery_cp = MaxSector; |
| 6839 | } else { |
| 6840 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6841 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6842 | rcu_read_lock(); |
| 6843 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6844 | if (rdev->raid_disk >= 0 && |
| 6845 | !test_bit(Faulty, &rdev->flags) && |
| 6846 | !test_bit(In_sync, &rdev->flags) && |
| 6847 | rdev->recovery_offset < mddev->curr_resync) |
| 6848 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6849 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6850 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6851 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6852 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6854 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6855 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6856 | /* We completed so min/max setting can be forgotten if used. */ |
| 6857 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6858 | mddev->resync_min = 0; |
| 6859 | mddev->resync_max = MaxSector; |
| 6860 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6861 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6863 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6864 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6865 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6866 | wake_up(&resync_wait); |
| 6867 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6868 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6869 | return; |
| 6870 | |
| 6871 | interrupted: |
| 6872 | /* |
| 6873 | * got a signal, exit. |
| 6874 | */ |
| 6875 | printk(KERN_INFO |
| 6876 | "md: md_do_sync() got signal ... exiting\n"); |
| 6877 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6878 | goto out; |
| 6879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6880 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6881 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6882 | |
| 6883 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6884 | static int remove_and_add_spares(mddev_t *mddev) |
| 6885 | { |
| 6886 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6887 | int spares = 0; |
| 6888 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6889 | mddev->curr_resync_completed = 0; |
| 6890 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6891 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6892 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6893 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6894 | (test_bit(Faulty, &rdev->flags) || |
| 6895 | ! test_bit(In_sync, &rdev->flags)) && |
| 6896 | atomic_read(&rdev->nr_pending)==0) { |
| 6897 | if (mddev->pers->hot_remove_disk( |
| 6898 | mddev, rdev->raid_disk)==0) { |
| 6899 | char nm[20]; |
| 6900 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6901 | sysfs_remove_link(&mddev->kobj, nm); |
| 6902 | rdev->raid_disk = -1; |
| 6903 | } |
| 6904 | } |
| 6905 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6906 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6907 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6908 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6909 | !test_bit(In_sync, &rdev->flags) && |
| 6910 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6911 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6912 | if (rdev->raid_disk < 0 |
| 6913 | && !test_bit(Faulty, &rdev->flags)) { |
| 6914 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6915 | if (mddev->pers-> |
| 6916 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6917 | char nm[20]; |
| 6918 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6919 | if (sysfs_create_link(&mddev->kobj, |
| 6920 | &rdev->kobj, nm)) |
| 6921 | printk(KERN_WARNING |
| 6922 | "md: cannot register " |
| 6923 | "%s for %s\n", |
| 6924 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6925 | spares++; |
| 6926 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 6927 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6928 | } else |
| 6929 | break; |
| 6930 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6931 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6932 | } |
| 6933 | return spares; |
| 6934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6935 | /* |
| 6936 | * This routine is regularly called by all per-raid-array threads to |
| 6937 | * deal with generic issues like resync and super-block update. |
| 6938 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6939 | * need this as they never do any recovery or update the superblock. |
| 6940 | * |
| 6941 | * It does not do any resync itself, but rather "forks" off other threads |
| 6942 | * to do that as needed. |
| 6943 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6944 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6945 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | * and wakeups up this thread which will reap the thread and finish up. |
| 6947 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6948 | * |
| 6949 | * The overall approach is: |
| 6950 | * 1/ if the superblock needs updating, update it. |
| 6951 | * 2/ If a recovery thread is running, don't do anything else. |
| 6952 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6953 | * 4/ If there are any faulty devices, remove them. |
| 6954 | * 5/ If array is degraded, try to add spares devices |
| 6955 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6956 | */ |
| 6957 | void md_check_recovery(mddev_t *mddev) |
| 6958 | { |
| 6959 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | |
| 6961 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6962 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6963 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | |
| 6965 | if (mddev->ro) |
| 6966 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6967 | |
| 6968 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6969 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6970 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6971 | mdname(mddev)); |
| 6972 | mddev->safemode = 2; |
| 6973 | } |
| 6974 | flush_signals(current); |
| 6975 | } |
| 6976 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6977 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6978 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6979 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6980 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6981 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6982 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6983 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6984 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6985 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | )) |
| 6987 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6988 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6989 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6990 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6991 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6992 | if (mddev->ro) { |
| 6993 | /* Only thing we do on a ro array is remove |
| 6994 | * failed devices. |
| 6995 | */ |
| 6996 | remove_and_add_spares(mddev); |
| 6997 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6998 | goto unlock; |
| 6999 | } |
| 7000 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7001 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7002 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7003 | spin_lock_irq(&mddev->write_lock); |
| 7004 | if (mddev->safemode && |
| 7005 | !atomic_read(&mddev->writes_pending) && |
| 7006 | !mddev->in_sync && |
| 7007 | mddev->recovery_cp == MaxSector) { |
| 7008 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7009 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7010 | if (mddev->persistent) |
| 7011 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 7012 | } |
| 7013 | if (mddev->safemode == 1) |
| 7014 | mddev->safemode = 0; |
| 7015 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7016 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 7017 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7018 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7019 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7020 | if (mddev->flags) |
| 7021 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7024 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7025 | /* resync/recovery still happening */ |
| 7026 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7027 | goto unlock; |
| 7028 | } |
| 7029 | if (mddev->sync_thread) { |
| 7030 | /* resync has finished, collect result */ |
| 7031 | md_unregister_thread(mddev->sync_thread); |
| 7032 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7033 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7034 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7035 | /* success...*/ |
| 7036 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 7037 | if (mddev->pers->spare_active(mddev)) |
| 7038 | sysfs_notify(&mddev->kobj, NULL, |
| 7039 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7040 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7041 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7042 | mddev->pers->finish_reshape) |
| 7043 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7044 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7045 | |
| 7046 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7047 | * information must be scrapped |
| 7048 | */ |
| 7049 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7050 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7051 | rdev->saved_raid_disk = -1; |
| 7052 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7053 | mddev->recovery = 0; |
| 7054 | /* flag recovery needed just to double check */ |
| 7055 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7056 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7057 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7058 | goto unlock; |
| 7059 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7060 | /* Set RUNNING before clearing NEEDED to avoid |
| 7061 | * any transients in the value of "sync_action". |
| 7062 | */ |
| 7063 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7064 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7065 | /* Clear some bits that don't mean anything, but |
| 7066 | * might be left set |
| 7067 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7068 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7069 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7070 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7071 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7072 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7073 | /* no recovery is running. |
| 7074 | * remove any failed drives, then |
| 7075 | * add spares if possible. |
| 7076 | * Spare are also removed and re-added, to allow |
| 7077 | * the personality to fail the re-add. |
| 7078 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7079 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7080 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7081 | if (mddev->pers->check_reshape == NULL || |
| 7082 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7083 | /* Cannot proceed */ |
| 7084 | goto unlock; |
| 7085 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7086 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7087 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7088 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7089 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7090 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7091 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7092 | } else if (mddev->recovery_cp < MaxSector) { |
| 7093 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7094 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7095 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7096 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7097 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7099 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7100 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7101 | /* We are adding a device or devices to an array |
| 7102 | * which has the bitmap stored on all devices. |
| 7103 | * So make sure all bitmap pages get written |
| 7104 | */ |
| 7105 | bitmap_write_all(mddev->bitmap); |
| 7106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7108 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7109 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7110 | if (!mddev->sync_thread) { |
| 7111 | printk(KERN_ERR "%s: could not start resync" |
| 7112 | " thread...\n", |
| 7113 | mdname(mddev)); |
| 7114 | /* leave the spares where they are, it shouldn't hurt */ |
| 7115 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7116 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7117 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7118 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7119 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | } |
| 7121 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7122 | if (!mddev->sync_thread) { |
| 7123 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7124 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7125 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7126 | if (mddev->sysfs_action) |
| 7127 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7129 | mddev_unlock(mddev); |
| 7130 | } |
| 7131 | } |
| 7132 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7133 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7134 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 7135 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7136 | wait_event_timeout(rdev->blocked_wait, |
| 7137 | !test_bit(Blocked, &rdev->flags), |
| 7138 | msecs_to_jiffies(5000)); |
| 7139 | rdev_dec_pending(rdev, mddev); |
| 7140 | } |
| 7141 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7142 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7143 | static int md_notify_reboot(struct notifier_block *this, |
| 7144 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | { |
| 7146 | struct list_head *tmp; |
| 7147 | mddev_t *mddev; |
| 7148 | |
| 7149 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7150 | |
| 7151 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7152 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7153 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7154 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7155 | /* Force a switch to readonly even array |
| 7156 | * appears to still be in use. Hence |
| 7157 | * the '100'. |
| 7158 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7159 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7160 | mddev_unlock(mddev); |
| 7161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7162 | /* |
| 7163 | * certain more exotic SCSI devices are known to be |
| 7164 | * volatile wrt too early system reboots. While the |
| 7165 | * right place to handle this issue is the given |
| 7166 | * driver, we do want to have a safe RAID driver ... |
| 7167 | */ |
| 7168 | mdelay(1000*1); |
| 7169 | } |
| 7170 | return NOTIFY_DONE; |
| 7171 | } |
| 7172 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7173 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7174 | .notifier_call = md_notify_reboot, |
| 7175 | .next = NULL, |
| 7176 | .priority = INT_MAX, /* before any real devices */ |
| 7177 | }; |
| 7178 | |
| 7179 | static void md_geninit(void) |
| 7180 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7181 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7182 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7183 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7184 | } |
| 7185 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7186 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7187 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7188 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7189 | return -1; |
| 7190 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7191 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | return -1; |
| 7193 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7194 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7195 | md_probe, NULL, NULL); |
| 7196 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7197 | md_probe, NULL, NULL); |
| 7198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7199 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7200 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7201 | |
| 7202 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7203 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | } |
| 7205 | |
| 7206 | |
| 7207 | #ifndef MODULE |
| 7208 | |
| 7209 | /* |
| 7210 | * Searches all registered partitions for autorun RAID arrays |
| 7211 | * at boot time. |
| 7212 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7213 | |
| 7214 | static LIST_HEAD(all_detected_devices); |
| 7215 | struct detected_devices_node { |
| 7216 | struct list_head list; |
| 7217 | dev_t dev; |
| 7218 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7219 | |
| 7220 | void md_autodetect_dev(dev_t dev) |
| 7221 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7222 | struct detected_devices_node *node_detected_dev; |
| 7223 | |
| 7224 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7225 | if (node_detected_dev) { |
| 7226 | node_detected_dev->dev = dev; |
| 7227 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7228 | } else { |
| 7229 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7230 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7232 | } |
| 7233 | |
| 7234 | |
| 7235 | static void autostart_arrays(int part) |
| 7236 | { |
| 7237 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7238 | struct detected_devices_node *node_detected_dev; |
| 7239 | dev_t dev; |
| 7240 | int i_scanned, i_passed; |
| 7241 | |
| 7242 | i_scanned = 0; |
| 7243 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7244 | |
| 7245 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7246 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7247 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7248 | i_scanned++; |
| 7249 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7250 | struct detected_devices_node, list); |
| 7251 | list_del(&node_detected_dev->list); |
| 7252 | dev = node_detected_dev->dev; |
| 7253 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7254 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7255 | if (IS_ERR(rdev)) |
| 7256 | continue; |
| 7257 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7258 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7259 | MD_BUG(); |
| 7260 | continue; |
| 7261 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7262 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7263 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7264 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7265 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7266 | |
| 7267 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7268 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7269 | |
| 7270 | autorun_devices(part); |
| 7271 | } |
| 7272 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7273 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7274 | |
| 7275 | static __exit void md_exit(void) |
| 7276 | { |
| 7277 | mddev_t *mddev; |
| 7278 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7279 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7280 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7281 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7282 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7283 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7284 | unregister_blkdev(mdp_major, "mdp"); |
| 7285 | unregister_reboot_notifier(&md_notifier); |
| 7286 | unregister_sysctl_table(raid_table_header); |
| 7287 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7288 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7289 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7290 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7291 | } |
| 7292 | } |
| 7293 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7294 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7295 | module_exit(md_exit) |
| 7296 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7297 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7298 | { |
| 7299 | return sprintf(buffer, "%d", start_readonly); |
| 7300 | } |
| 7301 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7302 | { |
| 7303 | char *e; |
| 7304 | int num = simple_strtoul(val, &e, 10); |
| 7305 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7306 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7307 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7308 | } |
| 7309 | return -EINVAL; |
| 7310 | } |
| 7311 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7312 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7313 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7314 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7315 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7317 | EXPORT_SYMBOL(register_md_personality); |
| 7318 | EXPORT_SYMBOL(unregister_md_personality); |
| 7319 | EXPORT_SYMBOL(md_error); |
| 7320 | EXPORT_SYMBOL(md_done_sync); |
| 7321 | EXPORT_SYMBOL(md_write_start); |
| 7322 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7323 | EXPORT_SYMBOL(md_register_thread); |
| 7324 | EXPORT_SYMBOL(md_unregister_thread); |
| 7325 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7326 | EXPORT_SYMBOL(md_check_recovery); |
| 7327 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7328 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7329 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7330 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |