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 | 850b2b42 | 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 | 850b2b42 | 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 | 850b2b42 | 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 | 850b2b42 | 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 | 850b2b42 | 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 | 850b2b42 | 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 | 850b2b42 | 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"); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3078 | return rv; |
| 3079 | } |
| 3080 | |
| 3081 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3082 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3083 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3084 | |
| 3085 | static ssize_t |
| 3086 | layout_show(mddev_t *mddev, char *page) |
| 3087 | { |
| 3088 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3089 | if (mddev->reshape_position != MaxSector && |
| 3090 | mddev->layout != mddev->new_layout) |
| 3091 | return sprintf(page, "%d (%d)\n", |
| 3092 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3093 | return sprintf(page, "%d\n", mddev->layout); |
| 3094 | } |
| 3095 | |
| 3096 | static ssize_t |
| 3097 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3098 | { |
| 3099 | char *e; |
| 3100 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3101 | |
| 3102 | if (!*buf || (*e && *e != '\n')) |
| 3103 | return -EINVAL; |
| 3104 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3105 | if (mddev->pers) { |
| 3106 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3107 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3108 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3109 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3110 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3111 | if (err) { |
| 3112 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3113 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3114 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3115 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3116 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3117 | if (mddev->reshape_position == MaxSector) |
| 3118 | mddev->layout = n; |
| 3119 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3120 | return len; |
| 3121 | } |
| 3122 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3123 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3124 | |
| 3125 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3126 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3127 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3128 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3129 | if (mddev->raid_disks == 0) |
| 3130 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3131 | if (mddev->reshape_position != MaxSector && |
| 3132 | mddev->delta_disks != 0) |
| 3133 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3134 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3135 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3136 | } |
| 3137 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3138 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3139 | |
| 3140 | static ssize_t |
| 3141 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3142 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3143 | char *e; |
| 3144 | int rv = 0; |
| 3145 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3146 | |
| 3147 | if (!*buf || (*e && *e != '\n')) |
| 3148 | return -EINVAL; |
| 3149 | |
| 3150 | if (mddev->pers) |
| 3151 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3152 | else if (mddev->reshape_position != MaxSector) { |
| 3153 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3154 | mddev->delta_disks = n - olddisks; |
| 3155 | mddev->raid_disks = n; |
| 3156 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3157 | mddev->raid_disks = n; |
| 3158 | return rv ? rv : len; |
| 3159 | } |
| 3160 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3161 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3162 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3163 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3164 | chunk_size_show(mddev_t *mddev, char *page) |
| 3165 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3166 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3167 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3168 | return sprintf(page, "%d (%d)\n", |
| 3169 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3170 | mddev->chunk_sectors << 9); |
| 3171 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | static ssize_t |
| 3175 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3176 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3177 | char *e; |
| 3178 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3179 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3180 | if (!*buf || (*e && *e != '\n')) |
| 3181 | return -EINVAL; |
| 3182 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3183 | if (mddev->pers) { |
| 3184 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3185 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3186 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3187 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3188 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3189 | if (err) { |
| 3190 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3191 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3192 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3193 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3194 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3195 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3196 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3197 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3198 | return len; |
| 3199 | } |
| 3200 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3201 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3202 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3203 | static ssize_t |
| 3204 | resync_start_show(mddev_t *mddev, char *page) |
| 3205 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3206 | if (mddev->recovery_cp == MaxSector) |
| 3207 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3208 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3209 | } |
| 3210 | |
| 3211 | static ssize_t |
| 3212 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3213 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3214 | char *e; |
| 3215 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3216 | |
| 3217 | if (mddev->pers) |
| 3218 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3219 | if (cmd_match(buf, "none")) |
| 3220 | n = MaxSector; |
| 3221 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3222 | return -EINVAL; |
| 3223 | |
| 3224 | mddev->recovery_cp = n; |
| 3225 | return len; |
| 3226 | } |
| 3227 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3228 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3229 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3230 | /* |
| 3231 | * The array state can be: |
| 3232 | * |
| 3233 | * clear |
| 3234 | * No devices, no size, no level |
| 3235 | * Equivalent to STOP_ARRAY ioctl |
| 3236 | * inactive |
| 3237 | * May have some settings, but array is not active |
| 3238 | * all IO results in error |
| 3239 | * When written, doesn't tear down array, but just stops it |
| 3240 | * suspended (not supported yet) |
| 3241 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3242 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3243 | * readonly |
| 3244 | * no resync can happen. no superblocks get written. |
| 3245 | * write requests fail |
| 3246 | * read-auto |
| 3247 | * like readonly, but behaves like 'clean' on a write request. |
| 3248 | * |
| 3249 | * clean - no pending writes, but otherwise active. |
| 3250 | * When written to inactive array, starts without resync |
| 3251 | * If a write request arrives then |
| 3252 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3253 | * if not known, block and switch to write-pending |
| 3254 | * If written to an active array that has pending writes, then fails. |
| 3255 | * active |
| 3256 | * fully active: IO and resync can be happening. |
| 3257 | * When written to inactive array, starts with resync |
| 3258 | * |
| 3259 | * write-pending |
| 3260 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3261 | * |
| 3262 | * active-idle |
| 3263 | * like active, but no writes have been seen for a while (100msec). |
| 3264 | * |
| 3265 | */ |
| 3266 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3267 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3268 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3269 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3270 | "write-pending", "active-idle", NULL }; |
| 3271 | |
| 3272 | static int match_word(const char *word, char **list) |
| 3273 | { |
| 3274 | int n; |
| 3275 | for (n=0; list[n]; n++) |
| 3276 | if (cmd_match(word, list[n])) |
| 3277 | break; |
| 3278 | return n; |
| 3279 | } |
| 3280 | |
| 3281 | static ssize_t |
| 3282 | array_state_show(mddev_t *mddev, char *page) |
| 3283 | { |
| 3284 | enum array_state st = inactive; |
| 3285 | |
| 3286 | if (mddev->pers) |
| 3287 | switch(mddev->ro) { |
| 3288 | case 1: |
| 3289 | st = readonly; |
| 3290 | break; |
| 3291 | case 2: |
| 3292 | st = read_auto; |
| 3293 | break; |
| 3294 | case 0: |
| 3295 | if (mddev->in_sync) |
| 3296 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3297 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3298 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3299 | else if (mddev->safemode) |
| 3300 | st = active_idle; |
| 3301 | else |
| 3302 | st = active; |
| 3303 | } |
| 3304 | else { |
| 3305 | if (list_empty(&mddev->disks) && |
| 3306 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3307 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3308 | st = clear; |
| 3309 | else |
| 3310 | st = inactive; |
| 3311 | } |
| 3312 | return sprintf(page, "%s\n", array_states[st]); |
| 3313 | } |
| 3314 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3315 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3316 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3317 | static int do_md_run(mddev_t * mddev); |
| 3318 | static int restart_array(mddev_t *mddev); |
| 3319 | |
| 3320 | static ssize_t |
| 3321 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3322 | { |
| 3323 | int err = -EINVAL; |
| 3324 | enum array_state st = match_word(buf, array_states); |
| 3325 | switch(st) { |
| 3326 | case bad_word: |
| 3327 | break; |
| 3328 | case clear: |
| 3329 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3330 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3331 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3332 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3333 | break; |
| 3334 | case inactive: |
| 3335 | /* stopping an active array */ |
| 3336 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3337 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3338 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3339 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3340 | } else |
| 3341 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3342 | break; |
| 3343 | case suspended: |
| 3344 | break; /* not supported yet */ |
| 3345 | case readonly: |
| 3346 | if (mddev->pers) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3347 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3348 | else { |
| 3349 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3350 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3351 | err = do_md_run(mddev); |
| 3352 | } |
| 3353 | break; |
| 3354 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3355 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3356 | if (mddev->ro == 0) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3357 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3358 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3359 | err = restart_array(mddev); |
| 3360 | if (err == 0) { |
| 3361 | mddev->ro = 2; |
| 3362 | set_disk_ro(mddev->gendisk, 0); |
| 3363 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3364 | } else { |
| 3365 | mddev->ro = 2; |
| 3366 | err = do_md_run(mddev); |
| 3367 | } |
| 3368 | break; |
| 3369 | case clean: |
| 3370 | if (mddev->pers) { |
| 3371 | restart_array(mddev); |
| 3372 | spin_lock_irq(&mddev->write_lock); |
| 3373 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3374 | if (mddev->in_sync == 0) { |
| 3375 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3376 | if (mddev->safemode == 1) |
| 3377 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3378 | if (mddev->persistent) |
| 3379 | set_bit(MD_CHANGE_CLEAN, |
| 3380 | &mddev->flags); |
| 3381 | } |
| 3382 | err = 0; |
| 3383 | } else |
| 3384 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3385 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3386 | } else |
| 3387 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3388 | break; |
| 3389 | case active: |
| 3390 | if (mddev->pers) { |
| 3391 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3392 | if (mddev->external) |
| 3393 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3394 | wake_up(&mddev->sb_wait); |
| 3395 | err = 0; |
| 3396 | } else { |
| 3397 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3398 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3399 | err = do_md_run(mddev); |
| 3400 | } |
| 3401 | break; |
| 3402 | case write_pending: |
| 3403 | case active_idle: |
| 3404 | /* these cannot be set */ |
| 3405 | break; |
| 3406 | } |
| 3407 | if (err) |
| 3408 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3409 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3410 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3411 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3412 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3413 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3414 | static struct md_sysfs_entry md_array_state = |
| 3415 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3416 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3417 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3418 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3419 | return sprintf(page, "%d\n", |
| 3420 | atomic_read(&mddev->max_corr_read_errors)); |
| 3421 | } |
| 3422 | |
| 3423 | static ssize_t |
| 3424 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3425 | { |
| 3426 | char *e; |
| 3427 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3428 | |
| 3429 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3430 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3431 | return len; |
| 3432 | } |
| 3433 | return -EINVAL; |
| 3434 | } |
| 3435 | |
| 3436 | static struct md_sysfs_entry max_corr_read_errors = |
| 3437 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3438 | max_corrected_read_errors_store); |
| 3439 | |
| 3440 | static ssize_t |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3441 | null_show(mddev_t *mddev, char *page) |
| 3442 | { |
| 3443 | return -EINVAL; |
| 3444 | } |
| 3445 | |
| 3446 | static ssize_t |
| 3447 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3448 | { |
| 3449 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3450 | /* The new device is added to the array. |
| 3451 | * If the array has a persistent superblock, we read the |
| 3452 | * superblock to initialise info and check validity. |
| 3453 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3454 | * which mainly checks size. |
| 3455 | */ |
| 3456 | char *e; |
| 3457 | int major = simple_strtoul(buf, &e, 10); |
| 3458 | int minor; |
| 3459 | dev_t dev; |
| 3460 | mdk_rdev_t *rdev; |
| 3461 | int err; |
| 3462 | |
| 3463 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3464 | return -EINVAL; |
| 3465 | minor = simple_strtoul(e+1, &e, 10); |
| 3466 | if (*e && *e != '\n') |
| 3467 | return -EINVAL; |
| 3468 | dev = MKDEV(major, minor); |
| 3469 | if (major != MAJOR(dev) || |
| 3470 | minor != MINOR(dev)) |
| 3471 | return -EOVERFLOW; |
| 3472 | |
| 3473 | |
| 3474 | if (mddev->persistent) { |
| 3475 | rdev = md_import_device(dev, mddev->major_version, |
| 3476 | mddev->minor_version); |
| 3477 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3478 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3479 | mdk_rdev_t, same_set); |
| 3480 | err = super_types[mddev->major_version] |
| 3481 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3482 | if (err < 0) |
| 3483 | goto out; |
| 3484 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3485 | } else if (mddev->external) |
| 3486 | rdev = md_import_device(dev, -2, -1); |
| 3487 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3488 | rdev = md_import_device(dev, -1, -1); |
| 3489 | |
| 3490 | if (IS_ERR(rdev)) |
| 3491 | return PTR_ERR(rdev); |
| 3492 | err = bind_rdev_to_array(rdev, mddev); |
| 3493 | out: |
| 3494 | if (err) |
| 3495 | export_rdev(rdev); |
| 3496 | return err ? err : len; |
| 3497 | } |
| 3498 | |
| 3499 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3500 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3501 | |
| 3502 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3503 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3504 | { |
| 3505 | char *end; |
| 3506 | unsigned long chunk, end_chunk; |
| 3507 | |
| 3508 | if (!mddev->bitmap) |
| 3509 | goto out; |
| 3510 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3511 | while (*buf) { |
| 3512 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3513 | if (buf == end) break; |
| 3514 | if (*end == '-') { /* range */ |
| 3515 | buf = end + 1; |
| 3516 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3517 | if (buf == end) break; |
| 3518 | } |
| 3519 | if (*end && !isspace(*end)) break; |
| 3520 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3521 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3522 | } |
| 3523 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3524 | out: |
| 3525 | return len; |
| 3526 | } |
| 3527 | |
| 3528 | static struct md_sysfs_entry md_bitmap = |
| 3529 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3530 | |
| 3531 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3532 | size_show(mddev_t *mddev, char *page) |
| 3533 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3534 | return sprintf(page, "%llu\n", |
| 3535 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3536 | } |
| 3537 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3538 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3539 | |
| 3540 | static ssize_t |
| 3541 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3542 | { |
| 3543 | /* If array is inactive, we can reduce the component size, but |
| 3544 | * not increase it (except from 0). |
| 3545 | * If array is active, we can try an on-line resize |
| 3546 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3547 | sector_t sectors; |
| 3548 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3549 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3550 | if (err < 0) |
| 3551 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3552 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3553 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3554 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3555 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3556 | if (mddev->dev_sectors == 0 || |
| 3557 | mddev->dev_sectors > sectors) |
| 3558 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3559 | else |
| 3560 | err = -ENOSPC; |
| 3561 | } |
| 3562 | return err ? err : len; |
| 3563 | } |
| 3564 | |
| 3565 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3566 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3567 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3568 | |
| 3569 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3570 | * This is one of |
| 3571 | * 'none' for arrays with no metadata (good luck...) |
| 3572 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3573 | * or N.M for internally known formats |
| 3574 | */ |
| 3575 | static ssize_t |
| 3576 | metadata_show(mddev_t *mddev, char *page) |
| 3577 | { |
| 3578 | if (mddev->persistent) |
| 3579 | return sprintf(page, "%d.%d\n", |
| 3580 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3581 | else if (mddev->external) |
| 3582 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3583 | else |
| 3584 | return sprintf(page, "none\n"); |
| 3585 | } |
| 3586 | |
| 3587 | static ssize_t |
| 3588 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3589 | { |
| 3590 | int major, minor; |
| 3591 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3592 | /* Changing the details of 'external' metadata is |
| 3593 | * always permitted. Otherwise there must be |
| 3594 | * no devices attached to the array. |
| 3595 | */ |
| 3596 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3597 | ; |
| 3598 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3599 | return -EBUSY; |
| 3600 | |
| 3601 | if (cmd_match(buf, "none")) { |
| 3602 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3603 | mddev->external = 0; |
| 3604 | mddev->major_version = 0; |
| 3605 | mddev->minor_version = 90; |
| 3606 | return len; |
| 3607 | } |
| 3608 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3609 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3610 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3611 | namelen = sizeof(mddev->metadata_type)-1; |
| 3612 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3613 | mddev->metadata_type[namelen] = 0; |
| 3614 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3615 | mddev->metadata_type[--namelen] = 0; |
| 3616 | mddev->persistent = 0; |
| 3617 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3618 | mddev->major_version = 0; |
| 3619 | mddev->minor_version = 90; |
| 3620 | return len; |
| 3621 | } |
| 3622 | major = simple_strtoul(buf, &e, 10); |
| 3623 | if (e==buf || *e != '.') |
| 3624 | return -EINVAL; |
| 3625 | buf = e+1; |
| 3626 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3627 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3628 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3629 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3630 | return -ENOENT; |
| 3631 | mddev->major_version = major; |
| 3632 | mddev->minor_version = minor; |
| 3633 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3634 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3635 | return len; |
| 3636 | } |
| 3637 | |
| 3638 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3639 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3640 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3641 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3642 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3643 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3644 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3645 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3646 | type = "frozen"; |
| 3647 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3648 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3649 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3650 | type = "reshape"; |
| 3651 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3652 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3653 | type = "resync"; |
| 3654 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3655 | type = "check"; |
| 3656 | else |
| 3657 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3658 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3659 | type = "recover"; |
| 3660 | } |
| 3661 | return sprintf(page, "%s\n", type); |
| 3662 | } |
| 3663 | |
| 3664 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3665 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3666 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3667 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3668 | return -EINVAL; |
| 3669 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3670 | if (cmd_match(page, "frozen")) |
| 3671 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3672 | else |
| 3673 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3674 | |
| 3675 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3676 | if (mddev->sync_thread) { |
| 3677 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3678 | md_unregister_thread(mddev->sync_thread); |
| 3679 | mddev->sync_thread = NULL; |
| 3680 | mddev->recovery = 0; |
| 3681 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3682 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3683 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3684 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3685 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3686 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3687 | else if (cmd_match(page, "recover")) { |
| 3688 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3689 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3690 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3691 | int err; |
| 3692 | if (mddev->pers->start_reshape == NULL) |
| 3693 | return -EINVAL; |
| 3694 | err = mddev->pers->start_reshape(mddev); |
| 3695 | if (err) |
| 3696 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3697 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3698 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3699 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3700 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3701 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3702 | return -EINVAL; |
| 3703 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3704 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3705 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3706 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3707 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3708 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3709 | return len; |
| 3710 | } |
| 3711 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3712 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3713 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3714 | { |
| 3715 | return sprintf(page, "%llu\n", |
| 3716 | (unsigned long long) mddev->resync_mismatches); |
| 3717 | } |
| 3718 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3719 | static struct md_sysfs_entry md_scan_mode = |
| 3720 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3721 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3722 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3723 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3724 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3725 | static ssize_t |
| 3726 | sync_min_show(mddev_t *mddev, char *page) |
| 3727 | { |
| 3728 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3729 | mddev->sync_speed_min ? "local": "system"); |
| 3730 | } |
| 3731 | |
| 3732 | static ssize_t |
| 3733 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3734 | { |
| 3735 | int min; |
| 3736 | char *e; |
| 3737 | if (strncmp(buf, "system", 6)==0) { |
| 3738 | mddev->sync_speed_min = 0; |
| 3739 | return len; |
| 3740 | } |
| 3741 | min = simple_strtoul(buf, &e, 10); |
| 3742 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3743 | return -EINVAL; |
| 3744 | mddev->sync_speed_min = min; |
| 3745 | return len; |
| 3746 | } |
| 3747 | |
| 3748 | static struct md_sysfs_entry md_sync_min = |
| 3749 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3750 | |
| 3751 | static ssize_t |
| 3752 | sync_max_show(mddev_t *mddev, char *page) |
| 3753 | { |
| 3754 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3755 | mddev->sync_speed_max ? "local": "system"); |
| 3756 | } |
| 3757 | |
| 3758 | static ssize_t |
| 3759 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3760 | { |
| 3761 | int max; |
| 3762 | char *e; |
| 3763 | if (strncmp(buf, "system", 6)==0) { |
| 3764 | mddev->sync_speed_max = 0; |
| 3765 | return len; |
| 3766 | } |
| 3767 | max = simple_strtoul(buf, &e, 10); |
| 3768 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3769 | return -EINVAL; |
| 3770 | mddev->sync_speed_max = max; |
| 3771 | return len; |
| 3772 | } |
| 3773 | |
| 3774 | static struct md_sysfs_entry md_sync_max = |
| 3775 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3776 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3777 | static ssize_t |
| 3778 | degraded_show(mddev_t *mddev, char *page) |
| 3779 | { |
| 3780 | return sprintf(page, "%d\n", mddev->degraded); |
| 3781 | } |
| 3782 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3783 | |
| 3784 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3785 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3786 | { |
| 3787 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3788 | } |
| 3789 | |
| 3790 | static ssize_t |
| 3791 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3792 | { |
| 3793 | long n; |
| 3794 | |
| 3795 | if (strict_strtol(buf, 10, &n)) |
| 3796 | return -EINVAL; |
| 3797 | |
| 3798 | if (n != 0 && n != 1) |
| 3799 | return -EINVAL; |
| 3800 | |
| 3801 | mddev->parallel_resync = n; |
| 3802 | |
| 3803 | if (mddev->sync_thread) |
| 3804 | wake_up(&resync_wait); |
| 3805 | |
| 3806 | return len; |
| 3807 | } |
| 3808 | |
| 3809 | /* force parallel resync, even with shared block devices */ |
| 3810 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3811 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3812 | sync_force_parallel_show, sync_force_parallel_store); |
| 3813 | |
| 3814 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3815 | sync_speed_show(mddev_t *mddev, char *page) |
| 3816 | { |
| 3817 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3818 | if (mddev->curr_resync == 0) |
| 3819 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3820 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3821 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3822 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3823 | db = resync - mddev->resync_mark_cnt; |
| 3824 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3825 | } |
| 3826 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3827 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3828 | |
| 3829 | static ssize_t |
| 3830 | sync_completed_show(mddev_t *mddev, char *page) |
| 3831 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3832 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3833 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3834 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3835 | return sprintf(page, "none\n"); |
| 3836 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3837 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3838 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3839 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3840 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3841 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3842 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3843 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3844 | } |
| 3845 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3846 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3847 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3848 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3849 | min_sync_show(mddev_t *mddev, char *page) |
| 3850 | { |
| 3851 | return sprintf(page, "%llu\n", |
| 3852 | (unsigned long long)mddev->resync_min); |
| 3853 | } |
| 3854 | static ssize_t |
| 3855 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3856 | { |
| 3857 | unsigned long long min; |
| 3858 | if (strict_strtoull(buf, 10, &min)) |
| 3859 | return -EINVAL; |
| 3860 | if (min > mddev->resync_max) |
| 3861 | return -EINVAL; |
| 3862 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3863 | return -EBUSY; |
| 3864 | |
| 3865 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3866 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3867 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3868 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3869 | return -EINVAL; |
| 3870 | } |
| 3871 | mddev->resync_min = min; |
| 3872 | |
| 3873 | return len; |
| 3874 | } |
| 3875 | |
| 3876 | static struct md_sysfs_entry md_min_sync = |
| 3877 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3878 | |
| 3879 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3880 | max_sync_show(mddev_t *mddev, char *page) |
| 3881 | { |
| 3882 | if (mddev->resync_max == MaxSector) |
| 3883 | return sprintf(page, "max\n"); |
| 3884 | else |
| 3885 | return sprintf(page, "%llu\n", |
| 3886 | (unsigned long long)mddev->resync_max); |
| 3887 | } |
| 3888 | static ssize_t |
| 3889 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3890 | { |
| 3891 | if (strncmp(buf, "max", 3) == 0) |
| 3892 | mddev->resync_max = MaxSector; |
| 3893 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3894 | unsigned long long max; |
| 3895 | if (strict_strtoull(buf, 10, &max)) |
| 3896 | return -EINVAL; |
| 3897 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3898 | return -EINVAL; |
| 3899 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3900 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3901 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3902 | return -EBUSY; |
| 3903 | |
| 3904 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3905 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3906 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3907 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3908 | return -EINVAL; |
| 3909 | } |
| 3910 | mddev->resync_max = max; |
| 3911 | } |
| 3912 | wake_up(&mddev->recovery_wait); |
| 3913 | return len; |
| 3914 | } |
| 3915 | |
| 3916 | static struct md_sysfs_entry md_max_sync = |
| 3917 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3918 | |
| 3919 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3920 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3921 | { |
| 3922 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3923 | } |
| 3924 | |
| 3925 | static ssize_t |
| 3926 | suspend_lo_store(mddev_t *mddev, const char *buf, size_t len) |
| 3927 | { |
| 3928 | char *e; |
| 3929 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3930 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3931 | if (mddev->pers == NULL || |
| 3932 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3933 | return -EINVAL; |
| 3934 | if (buf == e || (*e && *e != '\n')) |
| 3935 | return -EINVAL; |
| 3936 | if (new >= mddev->suspend_hi || |
| 3937 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3938 | mddev->suspend_lo = new; |
| 3939 | mddev->pers->quiesce(mddev, 2); |
| 3940 | return len; |
| 3941 | } else |
| 3942 | return -EINVAL; |
| 3943 | } |
| 3944 | static struct md_sysfs_entry md_suspend_lo = |
| 3945 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3946 | |
| 3947 | |
| 3948 | static ssize_t |
| 3949 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3950 | { |
| 3951 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3952 | } |
| 3953 | |
| 3954 | static ssize_t |
| 3955 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3956 | { |
| 3957 | char *e; |
| 3958 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3959 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3960 | if (mddev->pers == NULL || |
| 3961 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3962 | return -EINVAL; |
| 3963 | if (buf == e || (*e && *e != '\n')) |
| 3964 | return -EINVAL; |
| 3965 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3966 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3967 | mddev->suspend_hi = new; |
| 3968 | mddev->pers->quiesce(mddev, 1); |
| 3969 | mddev->pers->quiesce(mddev, 0); |
| 3970 | return len; |
| 3971 | } else |
| 3972 | return -EINVAL; |
| 3973 | } |
| 3974 | static struct md_sysfs_entry md_suspend_hi = |
| 3975 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 3976 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3977 | static ssize_t |
| 3978 | reshape_position_show(mddev_t *mddev, char *page) |
| 3979 | { |
| 3980 | if (mddev->reshape_position != MaxSector) |
| 3981 | return sprintf(page, "%llu\n", |
| 3982 | (unsigned long long)mddev->reshape_position); |
| 3983 | strcpy(page, "none\n"); |
| 3984 | return 5; |
| 3985 | } |
| 3986 | |
| 3987 | static ssize_t |
| 3988 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 3989 | { |
| 3990 | char *e; |
| 3991 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3992 | if (mddev->pers) |
| 3993 | return -EBUSY; |
| 3994 | if (buf == e || (*e && *e != '\n')) |
| 3995 | return -EINVAL; |
| 3996 | mddev->reshape_position = new; |
| 3997 | mddev->delta_disks = 0; |
| 3998 | mddev->new_level = mddev->level; |
| 3999 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4000 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4001 | return len; |
| 4002 | } |
| 4003 | |
| 4004 | static struct md_sysfs_entry md_reshape_position = |
| 4005 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4006 | reshape_position_store); |
| 4007 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4008 | static ssize_t |
| 4009 | array_size_show(mddev_t *mddev, char *page) |
| 4010 | { |
| 4011 | if (mddev->external_size) |
| 4012 | return sprintf(page, "%llu\n", |
| 4013 | (unsigned long long)mddev->array_sectors/2); |
| 4014 | else |
| 4015 | return sprintf(page, "default\n"); |
| 4016 | } |
| 4017 | |
| 4018 | static ssize_t |
| 4019 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4020 | { |
| 4021 | sector_t sectors; |
| 4022 | |
| 4023 | if (strncmp(buf, "default", 7) == 0) { |
| 4024 | if (mddev->pers) |
| 4025 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4026 | else |
| 4027 | sectors = mddev->array_sectors; |
| 4028 | |
| 4029 | mddev->external_size = 0; |
| 4030 | } else { |
| 4031 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4032 | return -EINVAL; |
| 4033 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4034 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4035 | |
| 4036 | mddev->external_size = 1; |
| 4037 | } |
| 4038 | |
| 4039 | mddev->array_sectors = sectors; |
| 4040 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4041 | if (mddev->pers) |
| 4042 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4043 | |
| 4044 | return len; |
| 4045 | } |
| 4046 | |
| 4047 | static struct md_sysfs_entry md_array_size = |
| 4048 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4049 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4050 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4051 | static struct attribute *md_default_attrs[] = { |
| 4052 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4053 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4054 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4055 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4056 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4057 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4058 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4059 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4060 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4061 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4062 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4063 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4064 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4065 | NULL, |
| 4066 | }; |
| 4067 | |
| 4068 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4069 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4070 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4071 | &md_sync_min.attr, |
| 4072 | &md_sync_max.attr, |
| 4073 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4074 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4075 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4076 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4077 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4078 | &md_suspend_lo.attr, |
| 4079 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4080 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4081 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4082 | NULL, |
| 4083 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4084 | static struct attribute_group md_redundancy_group = { |
| 4085 | .name = NULL, |
| 4086 | .attrs = md_redundancy_attrs, |
| 4087 | }; |
| 4088 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4089 | |
| 4090 | static ssize_t |
| 4091 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4092 | { |
| 4093 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4094 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4095 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4096 | |
| 4097 | if (!entry->show) |
| 4098 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4099 | rv = mddev_lock(mddev); |
| 4100 | if (!rv) { |
| 4101 | rv = entry->show(mddev, page); |
| 4102 | mddev_unlock(mddev); |
| 4103 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4104 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | static ssize_t |
| 4108 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4109 | const char *page, size_t length) |
| 4110 | { |
| 4111 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4112 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4113 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4114 | |
| 4115 | if (!entry->store) |
| 4116 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4117 | if (!capable(CAP_SYS_ADMIN)) |
| 4118 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4119 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4120 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4121 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4122 | if (!rv) { |
| 4123 | rv = entry->store(mddev, page, length); |
| 4124 | mddev_unlock(mddev); |
| 4125 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4126 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4127 | } |
| 4128 | |
| 4129 | static void md_free(struct kobject *ko) |
| 4130 | { |
| 4131 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4132 | |
| 4133 | if (mddev->sysfs_state) |
| 4134 | sysfs_put(mddev->sysfs_state); |
| 4135 | |
| 4136 | if (mddev->gendisk) { |
| 4137 | del_gendisk(mddev->gendisk); |
| 4138 | put_disk(mddev->gendisk); |
| 4139 | } |
| 4140 | if (mddev->queue) |
| 4141 | blk_cleanup_queue(mddev->queue); |
| 4142 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4143 | kfree(mddev); |
| 4144 | } |
| 4145 | |
| 4146 | static struct sysfs_ops md_sysfs_ops = { |
| 4147 | .show = md_attr_show, |
| 4148 | .store = md_attr_store, |
| 4149 | }; |
| 4150 | static struct kobj_type md_ktype = { |
| 4151 | .release = md_free, |
| 4152 | .sysfs_ops = &md_sysfs_ops, |
| 4153 | .default_attrs = md_default_attrs, |
| 4154 | }; |
| 4155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | int mdp_major = 0; |
| 4157 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4158 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4159 | { |
| 4160 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4161 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4162 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4163 | kobject_del(&mddev->kobj); |
| 4164 | kobject_put(&mddev->kobj); |
| 4165 | } |
| 4166 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4167 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4168 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4169 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | mddev_t *mddev = mddev_find(dev); |
| 4171 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4172 | int partitioned; |
| 4173 | int shift; |
| 4174 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4175 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | |
| 4177 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4178 | return -ENODEV; |
| 4179 | |
| 4180 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4181 | shift = partitioned ? MdpMinorShift : 0; |
| 4182 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4184 | /* wait for any previous instance if this device |
| 4185 | * to be completed removed (mddev_delayed_delete). |
| 4186 | */ |
| 4187 | flush_scheduled_work(); |
| 4188 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4189 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4190 | error = -EEXIST; |
| 4191 | if (mddev->gendisk) |
| 4192 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4193 | |
| 4194 | if (name) { |
| 4195 | /* Need to ensure that 'name' is not a duplicate. |
| 4196 | */ |
| 4197 | mddev_t *mddev2; |
| 4198 | spin_lock(&all_mddevs_lock); |
| 4199 | |
| 4200 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4201 | if (mddev2->gendisk && |
| 4202 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4203 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4204 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4205 | } |
| 4206 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4208 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4209 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4210 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4211 | if (!mddev->queue) |
| 4212 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4213 | mddev->queue->queuedata = mddev; |
| 4214 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4215 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4216 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4217 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4218 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | disk = alloc_disk(1 << shift); |
| 4221 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4222 | blk_cleanup_queue(mddev->queue); |
| 4223 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4224 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4226 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4228 | if (name) |
| 4229 | strcpy(disk->disk_name, name); |
| 4230 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4232 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | disk->fops = &md_fops; |
| 4235 | disk->private_data = mddev; |
| 4236 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4237 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4238 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4239 | * remove it now. |
| 4240 | */ |
| 4241 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | add_disk(disk); |
| 4243 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4244 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4245 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4246 | if (error) { |
| 4247 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4248 | * __must_check, we must do something with the result |
| 4249 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4250 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4251 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4252 | error = 0; |
| 4253 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4254 | if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
| 4255 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4256 | abort: |
| 4257 | mutex_unlock(&disks_mutex); |
| 4258 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4259 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4260 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state"); |
| 4261 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4262 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4263 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4267 | { |
| 4268 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | return NULL; |
| 4270 | } |
| 4271 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4272 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4273 | { |
| 4274 | /* val must be "md_*" where * is not all digits. |
| 4275 | * We allocate an array with a large free minor number, and |
| 4276 | * set the name to val. val must not already be an active name. |
| 4277 | */ |
| 4278 | int len = strlen(val); |
| 4279 | char buf[DISK_NAME_LEN]; |
| 4280 | |
| 4281 | while (len && val[len-1] == '\n') |
| 4282 | len--; |
| 4283 | if (len >= DISK_NAME_LEN) |
| 4284 | return -E2BIG; |
| 4285 | strlcpy(buf, val, len+1); |
| 4286 | if (strncmp(buf, "md_", 3) != 0) |
| 4287 | return -EINVAL; |
| 4288 | return md_alloc(0, buf); |
| 4289 | } |
| 4290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | static void md_safemode_timeout(unsigned long data) |
| 4292 | { |
| 4293 | mddev_t *mddev = (mddev_t *) data; |
| 4294 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4295 | if (!atomic_read(&mddev->writes_pending)) { |
| 4296 | mddev->safemode = 1; |
| 4297 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4298 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | md_wakeup_thread(mddev->thread); |
| 4301 | } |
| 4302 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4303 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4305 | static int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4307 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4309 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4311 | if (list_empty(&mddev->disks)) |
| 4312 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | |
| 4315 | if (mddev->pers) |
| 4316 | return -EBUSY; |
| 4317 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4318 | /* These two calls synchronise us with the |
| 4319 | * sysfs_remove_group calls in mddev_unlock, |
| 4320 | * so they must have completed. |
| 4321 | */ |
| 4322 | mutex_lock(&mddev->open_mutex); |
| 4323 | mutex_unlock(&mddev->open_mutex); |
| 4324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | /* |
| 4326 | * Analyze all RAID superblock(s) |
| 4327 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4328 | if (!mddev->raid_disks) { |
| 4329 | if (!mddev->persistent) |
| 4330 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4331 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4334 | if (mddev->level != LEVEL_NONE) |
| 4335 | request_module("md-level-%d", mddev->level); |
| 4336 | else if (mddev->clevel[0]) |
| 4337 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | |
| 4339 | /* |
| 4340 | * Drop all container device buffers, from now on |
| 4341 | * the only valid external interface is through the md |
| 4342 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4344 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4345 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4346 | continue; |
| 4347 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4348 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4349 | |
| 4350 | /* perform some consistency tests on the device. |
| 4351 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4352 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4353 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4354 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4355 | if (mddev->dev_sectors && |
| 4356 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4357 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4358 | printk("md: %s: data overlaps metadata\n", |
| 4359 | mdname(mddev)); |
| 4360 | return -EINVAL; |
| 4361 | } |
| 4362 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4363 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4364 | > rdev->data_offset) { |
| 4365 | printk("md: %s: metadata overlaps data\n", |
| 4366 | mdname(mddev)); |
| 4367 | return -EINVAL; |
| 4368 | } |
| 4369 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4370 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | } |
| 4372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4374 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4375 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4377 | if (mddev->level != LEVEL_NONE) |
| 4378 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4379 | mddev->level); |
| 4380 | else |
| 4381 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4382 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | return -EINVAL; |
| 4384 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4385 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4387 | if (mddev->level != pers->level) { |
| 4388 | mddev->level = pers->level; |
| 4389 | mddev->new_level = pers->level; |
| 4390 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4391 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4393 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4394 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4395 | /* This personality cannot handle reshaping... */ |
| 4396 | mddev->pers = NULL; |
| 4397 | module_put(pers->owner); |
| 4398 | return -EINVAL; |
| 4399 | } |
| 4400 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4401 | if (pers->sync_request) { |
| 4402 | /* Warn if this is a potentially silly |
| 4403 | * configuration. |
| 4404 | */ |
| 4405 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4406 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4407 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4408 | |
| 4409 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4410 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4411 | if (rdev < rdev2 && |
| 4412 | rdev->bdev->bd_contains == |
| 4413 | rdev2->bdev->bd_contains) { |
| 4414 | printk(KERN_WARNING |
| 4415 | "%s: WARNING: %s appears to be" |
| 4416 | " on the same physical disk as" |
| 4417 | " %s.\n", |
| 4418 | mdname(mddev), |
| 4419 | bdevname(rdev->bdev,b), |
| 4420 | bdevname(rdev2->bdev,b2)); |
| 4421 | warned = 1; |
| 4422 | } |
| 4423 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4424 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4425 | if (warned) |
| 4426 | printk(KERN_WARNING |
| 4427 | "True protection against single-disk" |
| 4428 | " failure might be compromised.\n"); |
| 4429 | } |
| 4430 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4431 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4432 | /* may be over-ridden by personality */ |
| 4433 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4434 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4435 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4436 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4437 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4438 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4439 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4440 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4441 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4442 | if (err) |
| 4443 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4444 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4445 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4446 | " but 'external_size' not in effect?\n", __func__); |
| 4447 | printk(KERN_ERR |
| 4448 | "md: invalid array_size %llu > default size %llu\n", |
| 4449 | (unsigned long long)mddev->array_sectors / 2, |
| 4450 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4451 | err = -EINVAL; |
| 4452 | mddev->pers->stop(mddev); |
| 4453 | } |
| 4454 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4455 | err = bitmap_create(mddev); |
| 4456 | if (err) { |
| 4457 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4458 | mdname(mddev), err); |
| 4459 | mddev->pers->stop(mddev); |
| 4460 | } |
| 4461 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4462 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | module_put(mddev->pers->owner); |
| 4464 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4465 | bitmap_destroy(mddev); |
| 4466 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4468 | if (mddev->pers->sync_request) { |
| 4469 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4470 | printk(KERN_WARNING |
| 4471 | "md: cannot register extra attributes for %s\n", |
| 4472 | mdname(mddev)); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4473 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4474 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4475 | mddev->ro = 0; |
| 4476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4477 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4478 | atomic_set(&mddev->max_corr_read_errors, |
| 4479 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | mddev->safemode = 0; |
| 4481 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4482 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4483 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4485 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4486 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4487 | if (rdev->raid_disk >= 0) { |
| 4488 | char nm[20]; |
| 4489 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4490 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4491 | printk("md: cannot register %s for %s\n", |
| 4492 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4493 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4494 | |
| 4495 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4496 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4497 | if (mddev->flags) |
| 4498 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4500 | md_wakeup_thread(mddev->thread); |
| 4501 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4502 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4503 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4504 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4505 | if (mddev->sysfs_action) |
| 4506 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4507 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4508 | return 0; |
| 4509 | } |
| 4510 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4511 | static int do_md_run(mddev_t *mddev) |
| 4512 | { |
| 4513 | int err; |
| 4514 | |
| 4515 | err = md_run(mddev); |
| 4516 | if (err) |
| 4517 | goto out; |
| 4518 | |
| 4519 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4520 | revalidate_disk(mddev->gendisk); |
| 4521 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4522 | out: |
| 4523 | return err; |
| 4524 | } |
| 4525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | static int restart_array(mddev_t *mddev) |
| 4527 | { |
| 4528 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4530 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4532 | return -ENXIO; |
| 4533 | if (!mddev->pers) |
| 4534 | return -EINVAL; |
| 4535 | if (!mddev->ro) |
| 4536 | return -EBUSY; |
| 4537 | mddev->safemode = 0; |
| 4538 | mddev->ro = 0; |
| 4539 | set_disk_ro(disk, 0); |
| 4540 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4541 | mdname(mddev)); |
| 4542 | /* Kick recovery or resync if necessary */ |
| 4543 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4544 | md_wakeup_thread(mddev->thread); |
| 4545 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4546 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4547 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | } |
| 4549 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4550 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4551 | * to the file ourselves */ |
| 4552 | static int deny_bitmap_write_access(struct file * file) |
| 4553 | { |
| 4554 | struct inode *inode = file->f_mapping->host; |
| 4555 | |
| 4556 | spin_lock(&inode->i_lock); |
| 4557 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4558 | spin_unlock(&inode->i_lock); |
| 4559 | return -ETXTBSY; |
| 4560 | } |
| 4561 | atomic_set(&inode->i_writecount, -1); |
| 4562 | spin_unlock(&inode->i_lock); |
| 4563 | |
| 4564 | return 0; |
| 4565 | } |
| 4566 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4567 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4568 | { |
| 4569 | struct inode *inode = file->f_mapping->host; |
| 4570 | |
| 4571 | spin_lock(&inode->i_lock); |
| 4572 | atomic_set(&inode->i_writecount, 1); |
| 4573 | spin_unlock(&inode->i_lock); |
| 4574 | } |
| 4575 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4576 | static void md_clean(mddev_t *mddev) |
| 4577 | { |
| 4578 | mddev->array_sectors = 0; |
| 4579 | mddev->external_size = 0; |
| 4580 | mddev->dev_sectors = 0; |
| 4581 | mddev->raid_disks = 0; |
| 4582 | mddev->recovery_cp = 0; |
| 4583 | mddev->resync_min = 0; |
| 4584 | mddev->resync_max = MaxSector; |
| 4585 | mddev->reshape_position = MaxSector; |
| 4586 | mddev->external = 0; |
| 4587 | mddev->persistent = 0; |
| 4588 | mddev->level = LEVEL_NONE; |
| 4589 | mddev->clevel[0] = 0; |
| 4590 | mddev->flags = 0; |
| 4591 | mddev->ro = 0; |
| 4592 | mddev->metadata_type[0] = 0; |
| 4593 | mddev->chunk_sectors = 0; |
| 4594 | mddev->ctime = mddev->utime = 0; |
| 4595 | mddev->layout = 0; |
| 4596 | mddev->max_disks = 0; |
| 4597 | mddev->events = 0; |
| 4598 | mddev->delta_disks = 0; |
| 4599 | mddev->new_level = LEVEL_NONE; |
| 4600 | mddev->new_layout = 0; |
| 4601 | mddev->new_chunk_sectors = 0; |
| 4602 | mddev->curr_resync = 0; |
| 4603 | mddev->resync_mismatches = 0; |
| 4604 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4605 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4606 | mddev->recovery = 0; |
| 4607 | mddev->in_sync = 0; |
| 4608 | mddev->degraded = 0; |
| 4609 | mddev->barriers_work = 0; |
| 4610 | mddev->safemode = 0; |
| 4611 | mddev->bitmap_info.offset = 0; |
| 4612 | mddev->bitmap_info.default_offset = 0; |
| 4613 | mddev->bitmap_info.chunksize = 0; |
| 4614 | mddev->bitmap_info.daemon_sleep = 0; |
| 4615 | mddev->bitmap_info.max_write_behind = 0; |
| 4616 | } |
| 4617 | |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4618 | static void md_stop_writes(mddev_t *mddev) |
| 4619 | { |
| 4620 | if (mddev->sync_thread) { |
| 4621 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4622 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4623 | md_unregister_thread(mddev->sync_thread); |
| 4624 | mddev->sync_thread = NULL; |
| 4625 | } |
| 4626 | |
| 4627 | del_timer_sync(&mddev->safemode_timer); |
| 4628 | |
| 4629 | bitmap_flush(mddev); |
| 4630 | md_super_wait(mddev); |
| 4631 | |
| 4632 | if (!mddev->in_sync || mddev->flags) { |
| 4633 | /* mark array as shutdown cleanly */ |
| 4634 | mddev->in_sync = 1; |
| 4635 | md_update_sb(mddev, 1); |
| 4636 | } |
| 4637 | } |
| 4638 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4639 | static void md_stop(mddev_t *mddev) |
| 4640 | { |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4641 | md_stop_writes(mddev); |
| 4642 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4643 | mddev->pers->stop(mddev); |
| 4644 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4645 | mddev->to_remove = &md_redundancy_group; |
| 4646 | module_put(mddev->pers->owner); |
| 4647 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4648 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4649 | } |
| 4650 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4651 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4652 | { |
| 4653 | int err = 0; |
| 4654 | mutex_lock(&mddev->open_mutex); |
| 4655 | if (atomic_read(&mddev->openers) > is_open) { |
| 4656 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4657 | err = -EBUSY; |
| 4658 | goto out; |
| 4659 | } |
| 4660 | if (mddev->pers) { |
| 4661 | md_stop_writes(mddev); |
| 4662 | |
| 4663 | err = -ENXIO; |
| 4664 | if (mddev->ro==1) |
| 4665 | goto out; |
| 4666 | mddev->ro = 1; |
| 4667 | set_disk_ro(mddev->gendisk, 1); |
| 4668 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4669 | sysfs_notify_dirent(mddev->sysfs_state); |
| 4670 | err = 0; |
| 4671 | } |
| 4672 | out: |
| 4673 | mutex_unlock(&mddev->open_mutex); |
| 4674 | return err; |
| 4675 | } |
| 4676 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4677 | /* mode: |
| 4678 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4679 | * 2 - stop but do not disassemble array |
| 4680 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4681 | 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] | 4682 | { |
| 4683 | int err = 0; |
| 4684 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4685 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4687 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4688 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4689 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4690 | err = -EBUSY; |
| 4691 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4693 | if (mddev->ro) |
| 4694 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4695 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4696 | md_stop(mddev); |
| 4697 | mddev->queue->merge_bvec_fn = NULL; |
| 4698 | mddev->queue->unplug_fn = NULL; |
| 4699 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4700 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4701 | /* tell userspace to handle 'inactive' */ |
| 4702 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4703 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4704 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4705 | if (rdev->raid_disk >= 0) { |
| 4706 | char nm[20]; |
| 4707 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4708 | sysfs_remove_link(&mddev->kobj, nm); |
| 4709 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4710 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4711 | set_capacity(disk, 0); |
| 4712 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4713 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4714 | if (mddev->ro) |
| 4715 | mddev->ro = 0; |
| 4716 | |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4717 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4718 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4719 | mutex_unlock(&mddev->open_mutex); |
| 4720 | if (err) |
| 4721 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | /* |
| 4723 | * Free resources if final stop |
| 4724 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4725 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4728 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4729 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4730 | if (mddev->bitmap_info.file) { |
| 4731 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4732 | fput(mddev->bitmap_info.file); |
| 4733 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4734 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4735 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | export_array(mddev); |
| 4738 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4739 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4740 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4741 | if (mddev->hold_active == UNTIL_STOP) |
| 4742 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4743 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4745 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4746 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4747 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4748 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | return err; |
| 4750 | } |
| 4751 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4752 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | static void autorun_array(mddev_t *mddev) |
| 4754 | { |
| 4755 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | int err; |
| 4757 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4758 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4759 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | |
| 4761 | printk(KERN_INFO "md: running: "); |
| 4762 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4763 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4764 | char b[BDEVNAME_SIZE]; |
| 4765 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4766 | } |
| 4767 | printk("\n"); |
| 4768 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4769 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | if (err) { |
| 4771 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4772 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | /* |
| 4777 | * lets try to run arrays based on all disks that have arrived |
| 4778 | * until now. (those are in pending_raid_disks) |
| 4779 | * |
| 4780 | * the method: pick the first pending disk, collect all disks with |
| 4781 | * the same UUID, remove all from the pending list and put them into |
| 4782 | * the 'same_array' list. Then order this list based on superblock |
| 4783 | * update time (freshest comes first), kick out 'old' disks and |
| 4784 | * compare superblocks. If everything's fine then run it. |
| 4785 | * |
| 4786 | * If "unit" is allocated, then bump its reference count |
| 4787 | */ |
| 4788 | static void autorun_devices(int part) |
| 4789 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4790 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4791 | mddev_t *mddev; |
| 4792 | char b[BDEVNAME_SIZE]; |
| 4793 | |
| 4794 | printk(KERN_INFO "md: autorun ...\n"); |
| 4795 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4796 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4798 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4799 | rdev0 = list_entry(pending_raid_disks.next, |
| 4800 | mdk_rdev_t, same_set); |
| 4801 | |
| 4802 | printk(KERN_INFO "md: considering %s ...\n", |
| 4803 | bdevname(rdev0->bdev,b)); |
| 4804 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4805 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4807 | printk(KERN_INFO "md: adding %s ...\n", |
| 4808 | bdevname(rdev->bdev,b)); |
| 4809 | list_move(&rdev->same_set, &candidates); |
| 4810 | } |
| 4811 | /* |
| 4812 | * now we have a set of devices, with all of them having |
| 4813 | * mostly sane superblocks. It's time to allocate the |
| 4814 | * mddev. |
| 4815 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4816 | if (part) { |
| 4817 | dev = MKDEV(mdp_major, |
| 4818 | rdev0->preferred_minor << MdpMinorShift); |
| 4819 | unit = MINOR(dev) >> MdpMinorShift; |
| 4820 | } else { |
| 4821 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4822 | unit = MINOR(dev); |
| 4823 | } |
| 4824 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4826 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4827 | break; |
| 4828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | |
| 4830 | md_probe(dev, NULL, NULL); |
| 4831 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4832 | if (!mddev || !mddev->gendisk) { |
| 4833 | if (mddev) |
| 4834 | mddev_put(mddev); |
| 4835 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | "md: cannot allocate memory for md drive.\n"); |
| 4837 | break; |
| 4838 | } |
| 4839 | if (mddev_lock(mddev)) |
| 4840 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4841 | mdname(mddev)); |
| 4842 | else if (mddev->raid_disks || mddev->major_version |
| 4843 | || !list_empty(&mddev->disks)) { |
| 4844 | printk(KERN_WARNING |
| 4845 | "md: %s already running, cannot run %s\n", |
| 4846 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4847 | mddev_unlock(mddev); |
| 4848 | } else { |
| 4849 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4850 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4851 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | list_del_init(&rdev->same_set); |
| 4853 | if (bind_rdev_to_array(rdev, mddev)) |
| 4854 | export_rdev(rdev); |
| 4855 | } |
| 4856 | autorun_array(mddev); |
| 4857 | mddev_unlock(mddev); |
| 4858 | } |
| 4859 | /* on success, candidates will be empty, on error |
| 4860 | * it won't... |
| 4861 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4862 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4863 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | mddev_put(mddev); |
| 4867 | } |
| 4868 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4869 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4870 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | static int get_version(void __user * arg) |
| 4873 | { |
| 4874 | mdu_version_t ver; |
| 4875 | |
| 4876 | ver.major = MD_MAJOR_VERSION; |
| 4877 | ver.minor = MD_MINOR_VERSION; |
| 4878 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4879 | |
| 4880 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4881 | return -EFAULT; |
| 4882 | |
| 4883 | return 0; |
| 4884 | } |
| 4885 | |
| 4886 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4887 | { |
| 4888 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4889 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4890 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4891 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4892 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4893 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4895 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | failed++; |
| 4897 | else { |
| 4898 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4899 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4900 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | else |
| 4902 | spare++; |
| 4903 | } |
| 4904 | } |
| 4905 | |
| 4906 | info.major_version = mddev->major_version; |
| 4907 | info.minor_version = mddev->minor_version; |
| 4908 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4909 | info.ctime = mddev->ctime; |
| 4910 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4911 | info.size = mddev->dev_sectors / 2; |
| 4912 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4913 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | info.nr_disks = nr; |
| 4915 | info.raid_disks = mddev->raid_disks; |
| 4916 | info.md_minor = mddev->md_minor; |
| 4917 | info.not_persistent= !mddev->persistent; |
| 4918 | |
| 4919 | info.utime = mddev->utime; |
| 4920 | info.state = 0; |
| 4921 | if (mddev->in_sync) |
| 4922 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4923 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4924 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4925 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | info.working_disks = working; |
| 4927 | info.failed_disks = failed; |
| 4928 | info.spare_disks = spare; |
| 4929 | |
| 4930 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4931 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4932 | |
| 4933 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4934 | return -EFAULT; |
| 4935 | |
| 4936 | return 0; |
| 4937 | } |
| 4938 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4939 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4940 | { |
| 4941 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4942 | char *ptr, *buf = NULL; |
| 4943 | int err = -ENOMEM; |
| 4944 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4945 | if (md_allow_write(mddev)) |
| 4946 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4947 | else |
| 4948 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4949 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4950 | if (!file) |
| 4951 | goto out; |
| 4952 | |
| 4953 | /* bitmap disabled, zero the first byte and copy out */ |
| 4954 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4955 | file->pathname[0] = '\0'; |
| 4956 | goto copy_out; |
| 4957 | } |
| 4958 | |
| 4959 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4960 | if (!buf) |
| 4961 | goto out; |
| 4962 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4963 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4964 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4965 | goto out; |
| 4966 | |
| 4967 | strcpy(file->pathname, ptr); |
| 4968 | |
| 4969 | copy_out: |
| 4970 | err = 0; |
| 4971 | if (copy_to_user(arg, file, sizeof(*file))) |
| 4972 | err = -EFAULT; |
| 4973 | out: |
| 4974 | kfree(buf); |
| 4975 | kfree(file); |
| 4976 | return err; |
| 4977 | } |
| 4978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 4980 | { |
| 4981 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4982 | mdk_rdev_t *rdev; |
| 4983 | |
| 4984 | if (copy_from_user(&info, arg, sizeof(info))) |
| 4985 | return -EFAULT; |
| 4986 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4987 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | if (rdev) { |
| 4989 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 4990 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 4991 | info.raid_disk = rdev->raid_disk; |
| 4992 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4993 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4994 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4995 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4996 | info.state |= (1<<MD_DISK_ACTIVE); |
| 4997 | info.state |= (1<<MD_DISK_SYNC); |
| 4998 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4999 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5000 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5001 | } else { |
| 5002 | info.major = info.minor = 0; |
| 5003 | info.raid_disk = -1; |
| 5004 | info.state = (1<<MD_DISK_REMOVED); |
| 5005 | } |
| 5006 | |
| 5007 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5008 | return -EFAULT; |
| 5009 | |
| 5010 | return 0; |
| 5011 | } |
| 5012 | |
| 5013 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5014 | { |
| 5015 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5016 | mdk_rdev_t *rdev; |
| 5017 | dev_t dev = MKDEV(info->major,info->minor); |
| 5018 | |
| 5019 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5020 | return -EOVERFLOW; |
| 5021 | |
| 5022 | if (!mddev->raid_disks) { |
| 5023 | int err; |
| 5024 | /* expecting a device which has a superblock */ |
| 5025 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5026 | if (IS_ERR(rdev)) { |
| 5027 | printk(KERN_WARNING |
| 5028 | "md: md_import_device returned %ld\n", |
| 5029 | PTR_ERR(rdev)); |
| 5030 | return PTR_ERR(rdev); |
| 5031 | } |
| 5032 | if (!list_empty(&mddev->disks)) { |
| 5033 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5034 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5035 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5037 | if (err < 0) { |
| 5038 | printk(KERN_WARNING |
| 5039 | "md: %s has different UUID to %s\n", |
| 5040 | bdevname(rdev->bdev,b), |
| 5041 | bdevname(rdev0->bdev,b2)); |
| 5042 | export_rdev(rdev); |
| 5043 | return -EINVAL; |
| 5044 | } |
| 5045 | } |
| 5046 | err = bind_rdev_to_array(rdev, mddev); |
| 5047 | if (err) |
| 5048 | export_rdev(rdev); |
| 5049 | return err; |
| 5050 | } |
| 5051 | |
| 5052 | /* |
| 5053 | * add_new_disk can be used once the array is assembled |
| 5054 | * to add "hot spares". They must already have a superblock |
| 5055 | * written |
| 5056 | */ |
| 5057 | if (mddev->pers) { |
| 5058 | int err; |
| 5059 | if (!mddev->pers->hot_add_disk) { |
| 5060 | printk(KERN_WARNING |
| 5061 | "%s: personality does not support diskops!\n", |
| 5062 | mdname(mddev)); |
| 5063 | return -EINVAL; |
| 5064 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5065 | if (mddev->persistent) |
| 5066 | rdev = md_import_device(dev, mddev->major_version, |
| 5067 | mddev->minor_version); |
| 5068 | else |
| 5069 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | if (IS_ERR(rdev)) { |
| 5071 | printk(KERN_WARNING |
| 5072 | "md: md_import_device returned %ld\n", |
| 5073 | PTR_ERR(rdev)); |
| 5074 | return PTR_ERR(rdev); |
| 5075 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5076 | /* set save_raid_disk if appropriate */ |
| 5077 | if (!mddev->persistent) { |
| 5078 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5079 | info->raid_disk < mddev->raid_disks) |
| 5080 | rdev->raid_disk = info->raid_disk; |
| 5081 | else |
| 5082 | rdev->raid_disk = -1; |
| 5083 | } else |
| 5084 | super_types[mddev->major_version]. |
| 5085 | validate_super(mddev, rdev); |
| 5086 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5087 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5088 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5089 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5090 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5091 | else |
| 5092 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | rdev->raid_disk = -1; |
| 5095 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5096 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5097 | /* If there is hot_add_disk but no hot_remove_disk |
| 5098 | * then added disks for geometry changes, |
| 5099 | * and should be added immediately. |
| 5100 | */ |
| 5101 | super_types[mddev->major_version]. |
| 5102 | validate_super(mddev, rdev); |
| 5103 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5104 | if (err) |
| 5105 | unbind_rdev_from_array(rdev); |
| 5106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | if (err) |
| 5108 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5109 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 5110 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5111 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5112 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5113 | if (mddev->degraded) |
| 5114 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5115 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5116 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | return err; |
| 5118 | } |
| 5119 | |
| 5120 | /* otherwise, add_new_disk is only allowed |
| 5121 | * for major_version==0 superblocks |
| 5122 | */ |
| 5123 | if (mddev->major_version != 0) { |
| 5124 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5125 | mdname(mddev)); |
| 5126 | return -EINVAL; |
| 5127 | } |
| 5128 | |
| 5129 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5130 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5131 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | if (IS_ERR(rdev)) { |
| 5133 | printk(KERN_WARNING |
| 5134 | "md: error, md_import_device() returned %ld\n", |
| 5135 | PTR_ERR(rdev)); |
| 5136 | return PTR_ERR(rdev); |
| 5137 | } |
| 5138 | rdev->desc_nr = info->number; |
| 5139 | if (info->raid_disk < mddev->raid_disks) |
| 5140 | rdev->raid_disk = info->raid_disk; |
| 5141 | else |
| 5142 | rdev->raid_disk = -1; |
| 5143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5145 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5146 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5148 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5149 | set_bit(WriteMostly, &rdev->flags); |
| 5150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5151 | if (!mddev->persistent) { |
| 5152 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5153 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5155 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5156 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5158 | err = bind_rdev_to_array(rdev, mddev); |
| 5159 | if (err) { |
| 5160 | export_rdev(rdev); |
| 5161 | return err; |
| 5162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | } |
| 5164 | |
| 5165 | return 0; |
| 5166 | } |
| 5167 | |
| 5168 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5169 | { |
| 5170 | char b[BDEVNAME_SIZE]; |
| 5171 | mdk_rdev_t *rdev; |
| 5172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5173 | rdev = find_rdev(mddev, dev); |
| 5174 | if (!rdev) |
| 5175 | return -ENXIO; |
| 5176 | |
| 5177 | if (rdev->raid_disk >= 0) |
| 5178 | goto busy; |
| 5179 | |
| 5180 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5181 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5182 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | |
| 5184 | return 0; |
| 5185 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5186 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5188 | return -EBUSY; |
| 5189 | } |
| 5190 | |
| 5191 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5192 | { |
| 5193 | char b[BDEVNAME_SIZE]; |
| 5194 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | mdk_rdev_t *rdev; |
| 5196 | |
| 5197 | if (!mddev->pers) |
| 5198 | return -ENODEV; |
| 5199 | |
| 5200 | if (mddev->major_version != 0) { |
| 5201 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5202 | " version-0 superblocks.\n", |
| 5203 | mdname(mddev)); |
| 5204 | return -EINVAL; |
| 5205 | } |
| 5206 | if (!mddev->pers->hot_add_disk) { |
| 5207 | printk(KERN_WARNING |
| 5208 | "%s: personality does not support diskops!\n", |
| 5209 | mdname(mddev)); |
| 5210 | return -EINVAL; |
| 5211 | } |
| 5212 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5213 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5214 | if (IS_ERR(rdev)) { |
| 5215 | printk(KERN_WARNING |
| 5216 | "md: error, md_import_device() returned %ld\n", |
| 5217 | PTR_ERR(rdev)); |
| 5218 | return -EINVAL; |
| 5219 | } |
| 5220 | |
| 5221 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5222 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5223 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5224 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5226 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5227 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5228 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5229 | printk(KERN_WARNING |
| 5230 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5231 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5232 | err = -EINVAL; |
| 5233 | goto abort_export; |
| 5234 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5235 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5237 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5238 | err = bind_rdev_to_array(rdev, mddev); |
| 5239 | if (err) |
| 5240 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | |
| 5242 | /* |
| 5243 | * The rest should better be atomic, we can have disk failures |
| 5244 | * noticed in interrupt contexts ... |
| 5245 | */ |
| 5246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5247 | rdev->raid_disk = -1; |
| 5248 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5249 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | |
| 5251 | /* |
| 5252 | * Kick recovery, maybe this spare has to be added to the |
| 5253 | * array immediately. |
| 5254 | */ |
| 5255 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5256 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5257 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | return 0; |
| 5259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5260 | abort_export: |
| 5261 | export_rdev(rdev); |
| 5262 | return err; |
| 5263 | } |
| 5264 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5265 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5266 | { |
| 5267 | int err; |
| 5268 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5269 | if (mddev->pers) { |
| 5270 | if (!mddev->pers->quiesce) |
| 5271 | return -EBUSY; |
| 5272 | if (mddev->recovery || mddev->sync_thread) |
| 5273 | return -EBUSY; |
| 5274 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5275 | } |
| 5276 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5277 | |
| 5278 | if (fd >= 0) { |
| 5279 | if (mddev->bitmap) |
| 5280 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5281 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5282 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5283 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5284 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5285 | mdname(mddev)); |
| 5286 | return -EBADF; |
| 5287 | } |
| 5288 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5289 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5290 | if (err) { |
| 5291 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5292 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5293 | fput(mddev->bitmap_info.file); |
| 5294 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5295 | return err; |
| 5296 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5297 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5298 | } else if (mddev->bitmap == NULL) |
| 5299 | return -ENOENT; /* cannot remove what isn't there */ |
| 5300 | err = 0; |
| 5301 | if (mddev->pers) { |
| 5302 | mddev->pers->quiesce(mddev, 1); |
| 5303 | if (fd >= 0) |
| 5304 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5305 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5306 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5307 | fd = -1; /* make sure to put the file */ |
| 5308 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5309 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5310 | } |
| 5311 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5312 | if (mddev->bitmap_info.file) { |
| 5313 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5314 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5315 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5316 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5317 | } |
| 5318 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5319 | return err; |
| 5320 | } |
| 5321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | /* |
| 5323 | * set_array_info is used two different ways |
| 5324 | * The original usage is when creating a new array. |
| 5325 | * In this usage, raid_disks is > 0 and it together with |
| 5326 | * level, size, not_persistent,layout,chunksize determine the |
| 5327 | * shape of the array. |
| 5328 | * This will always create an array with a type-0.90.0 superblock. |
| 5329 | * The newer usage is when assembling an array. |
| 5330 | * In this case raid_disks will be 0, and the major_version field is |
| 5331 | * use to determine which style super-blocks are to be found on the devices. |
| 5332 | * The minor and patch _version numbers are also kept incase the |
| 5333 | * super_block handler wishes to interpret them. |
| 5334 | */ |
| 5335 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5336 | { |
| 5337 | |
| 5338 | if (info->raid_disks == 0) { |
| 5339 | /* just setting version number for superblock loading */ |
| 5340 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5341 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | super_types[info->major_version].name == NULL) { |
| 5343 | /* maybe try to auto-load a module? */ |
| 5344 | printk(KERN_INFO |
| 5345 | "md: superblock version %d not known\n", |
| 5346 | info->major_version); |
| 5347 | return -EINVAL; |
| 5348 | } |
| 5349 | mddev->major_version = info->major_version; |
| 5350 | mddev->minor_version = info->minor_version; |
| 5351 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5352 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5353 | /* ensure mddev_put doesn't delete this now that there |
| 5354 | * is some minimal configuration. |
| 5355 | */ |
| 5356 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | return 0; |
| 5358 | } |
| 5359 | mddev->major_version = MD_MAJOR_VERSION; |
| 5360 | mddev->minor_version = MD_MINOR_VERSION; |
| 5361 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5362 | mddev->ctime = get_seconds(); |
| 5363 | |
| 5364 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5365 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5366 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5367 | mddev->raid_disks = info->raid_disks; |
| 5368 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5369 | * openned |
| 5370 | */ |
| 5371 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5372 | mddev->recovery_cp = MaxSector; |
| 5373 | else |
| 5374 | mddev->recovery_cp = 0; |
| 5375 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5376 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5377 | |
| 5378 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5379 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | |
| 5381 | mddev->max_disks = MD_SB_DISKS; |
| 5382 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5383 | if (mddev->persistent) |
| 5384 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5385 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5387 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5388 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5389 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5390 | mddev->reshape_position = MaxSector; |
| 5391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5392 | /* |
| 5393 | * Generate a 128 bit UUID |
| 5394 | */ |
| 5395 | get_random_bytes(mddev->uuid, 16); |
| 5396 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5397 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5398 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5399 | mddev->new_layout = mddev->layout; |
| 5400 | mddev->delta_disks = 0; |
| 5401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | return 0; |
| 5403 | } |
| 5404 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5405 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5406 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5407 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5408 | |
| 5409 | if (mddev->external_size) |
| 5410 | return; |
| 5411 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5412 | mddev->array_sectors = array_sectors; |
| 5413 | } |
| 5414 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5415 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5416 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5417 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5418 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5419 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5420 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5421 | |
| 5422 | if (mddev->pers->resize == NULL) |
| 5423 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5424 | /* The "num_sectors" is the number of sectors of each device that |
| 5425 | * is used. This can only make sense for arrays with redundancy. |
| 5426 | * linear and raid0 always use whatever space is available. We can only |
| 5427 | * consider changing this number if no resync or reconstruction is |
| 5428 | * 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] | 5429 | * 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] | 5430 | * of each device. If num_sectors is zero, we find the largest size |
| 5431 | * that fits. |
| 5432 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5433 | */ |
| 5434 | if (mddev->sync_thread) |
| 5435 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5436 | if (mddev->bitmap) |
| 5437 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5438 | * grow, and re-add. |
| 5439 | */ |
| 5440 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5441 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5442 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5443 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5444 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5445 | num_sectors = avail; |
| 5446 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5447 | return -ENOSPC; |
| 5448 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5449 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5450 | if (!rv) |
| 5451 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5452 | return rv; |
| 5453 | } |
| 5454 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5455 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5456 | { |
| 5457 | int rv; |
| 5458 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5459 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5460 | return -EINVAL; |
| 5461 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5462 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5463 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5464 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5465 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5466 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5467 | |
| 5468 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5469 | return rv; |
| 5470 | } |
| 5471 | |
| 5472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | /* |
| 5474 | * update_array_info is used to change the configuration of an |
| 5475 | * on-line array. |
| 5476 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5477 | * fields in the info are checked against the array. |
| 5478 | * Any differences that cannot be handled will cause an error. |
| 5479 | * Normally, only one change can be managed at a time. |
| 5480 | */ |
| 5481 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5482 | { |
| 5483 | int rv = 0; |
| 5484 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5485 | int state = 0; |
| 5486 | |
| 5487 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5488 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5489 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5490 | |
| 5491 | if (mddev->major_version != info->major_version || |
| 5492 | mddev->minor_version != info->minor_version || |
| 5493 | /* mddev->patch_version != info->patch_version || */ |
| 5494 | mddev->ctime != info->ctime || |
| 5495 | mddev->level != info->level || |
| 5496 | /* mddev->layout != info->layout || */ |
| 5497 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5498 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5499 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5500 | ((state^info->state) & 0xfffffe00) |
| 5501 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | return -EINVAL; |
| 5503 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5504 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5505 | cnt++; |
| 5506 | if (mddev->raid_disks != info->raid_disks) |
| 5507 | cnt++; |
| 5508 | if (mddev->layout != info->layout) |
| 5509 | cnt++; |
| 5510 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5511 | cnt++; |
| 5512 | if (cnt == 0) |
| 5513 | return 0; |
| 5514 | if (cnt > 1) |
| 5515 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5516 | |
| 5517 | if (mddev->layout != info->layout) { |
| 5518 | /* Change layout |
| 5519 | * we don't need to do anything at the md level, the |
| 5520 | * personality will take care of it all. |
| 5521 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5522 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5524 | else { |
| 5525 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5526 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5527 | if (rv) |
| 5528 | mddev->new_layout = mddev->layout; |
| 5529 | return rv; |
| 5530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5531 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5532 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5533 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5534 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5535 | if (mddev->raid_disks != info->raid_disks) |
| 5536 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5537 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5538 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5539 | if (mddev->pers->quiesce == NULL) |
| 5540 | return -EINVAL; |
| 5541 | if (mddev->recovery || mddev->sync_thread) |
| 5542 | return -EBUSY; |
| 5543 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5544 | /* add the bitmap */ |
| 5545 | if (mddev->bitmap) |
| 5546 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5547 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5548 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5549 | mddev->bitmap_info.offset = |
| 5550 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5551 | mddev->pers->quiesce(mddev, 1); |
| 5552 | rv = bitmap_create(mddev); |
| 5553 | if (rv) |
| 5554 | bitmap_destroy(mddev); |
| 5555 | mddev->pers->quiesce(mddev, 0); |
| 5556 | } else { |
| 5557 | /* remove the bitmap */ |
| 5558 | if (!mddev->bitmap) |
| 5559 | return -ENOENT; |
| 5560 | if (mddev->bitmap->file) |
| 5561 | return -EINVAL; |
| 5562 | mddev->pers->quiesce(mddev, 1); |
| 5563 | bitmap_destroy(mddev); |
| 5564 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5565 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5566 | } |
| 5567 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5568 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5569 | return rv; |
| 5570 | } |
| 5571 | |
| 5572 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5573 | { |
| 5574 | mdk_rdev_t *rdev; |
| 5575 | |
| 5576 | if (mddev->pers == NULL) |
| 5577 | return -ENODEV; |
| 5578 | |
| 5579 | rdev = find_rdev(mddev, dev); |
| 5580 | if (!rdev) |
| 5581 | return -ENODEV; |
| 5582 | |
| 5583 | md_error(mddev, rdev); |
| 5584 | return 0; |
| 5585 | } |
| 5586 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5587 | /* |
| 5588 | * We have a problem here : there is no easy way to give a CHS |
| 5589 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5590 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5591 | * dosfs just mad... ;-) |
| 5592 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5593 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5594 | { |
| 5595 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5596 | |
| 5597 | geo->heads = 2; |
| 5598 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5599 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5600 | return 0; |
| 5601 | } |
| 5602 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5603 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5604 | unsigned int cmd, unsigned long arg) |
| 5605 | { |
| 5606 | int err = 0; |
| 5607 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5609 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | |
| 5611 | if (!capable(CAP_SYS_ADMIN)) |
| 5612 | return -EACCES; |
| 5613 | |
| 5614 | /* |
| 5615 | * Commands dealing with the RAID driver but not any |
| 5616 | * particular array: |
| 5617 | */ |
| 5618 | switch (cmd) |
| 5619 | { |
| 5620 | case RAID_VERSION: |
| 5621 | err = get_version(argp); |
| 5622 | goto done; |
| 5623 | |
| 5624 | case PRINT_RAID_DEBUG: |
| 5625 | err = 0; |
| 5626 | md_print_devices(); |
| 5627 | goto done; |
| 5628 | |
| 5629 | #ifndef MODULE |
| 5630 | case RAID_AUTORUN: |
| 5631 | err = 0; |
| 5632 | autostart_arrays(arg); |
| 5633 | goto done; |
| 5634 | #endif |
| 5635 | default:; |
| 5636 | } |
| 5637 | |
| 5638 | /* |
| 5639 | * Commands creating/starting a new array: |
| 5640 | */ |
| 5641 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5642 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5643 | |
| 5644 | if (!mddev) { |
| 5645 | BUG(); |
| 5646 | goto abort; |
| 5647 | } |
| 5648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | err = mddev_lock(mddev); |
| 5650 | if (err) { |
| 5651 | printk(KERN_INFO |
| 5652 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5653 | err, cmd); |
| 5654 | goto abort; |
| 5655 | } |
| 5656 | |
| 5657 | switch (cmd) |
| 5658 | { |
| 5659 | case SET_ARRAY_INFO: |
| 5660 | { |
| 5661 | mdu_array_info_t info; |
| 5662 | if (!arg) |
| 5663 | memset(&info, 0, sizeof(info)); |
| 5664 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5665 | err = -EFAULT; |
| 5666 | goto abort_unlock; |
| 5667 | } |
| 5668 | if (mddev->pers) { |
| 5669 | err = update_array_info(mddev, &info); |
| 5670 | if (err) { |
| 5671 | printk(KERN_WARNING "md: couldn't update" |
| 5672 | " array info. %d\n", err); |
| 5673 | goto abort_unlock; |
| 5674 | } |
| 5675 | goto done_unlock; |
| 5676 | } |
| 5677 | if (!list_empty(&mddev->disks)) { |
| 5678 | printk(KERN_WARNING |
| 5679 | "md: array %s already has disks!\n", |
| 5680 | mdname(mddev)); |
| 5681 | err = -EBUSY; |
| 5682 | goto abort_unlock; |
| 5683 | } |
| 5684 | if (mddev->raid_disks) { |
| 5685 | printk(KERN_WARNING |
| 5686 | "md: array %s already initialised!\n", |
| 5687 | mdname(mddev)); |
| 5688 | err = -EBUSY; |
| 5689 | goto abort_unlock; |
| 5690 | } |
| 5691 | err = set_array_info(mddev, &info); |
| 5692 | if (err) { |
| 5693 | printk(KERN_WARNING "md: couldn't set" |
| 5694 | " array info. %d\n", err); |
| 5695 | goto abort_unlock; |
| 5696 | } |
| 5697 | } |
| 5698 | goto done_unlock; |
| 5699 | |
| 5700 | default:; |
| 5701 | } |
| 5702 | |
| 5703 | /* |
| 5704 | * Commands querying/configuring an existing array: |
| 5705 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5706 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5707 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5708 | if ((!mddev->raid_disks && !mddev->external) |
| 5709 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5710 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5711 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | err = -ENODEV; |
| 5713 | goto abort_unlock; |
| 5714 | } |
| 5715 | |
| 5716 | /* |
| 5717 | * Commands even a read-only array can execute: |
| 5718 | */ |
| 5719 | switch (cmd) |
| 5720 | { |
| 5721 | case GET_ARRAY_INFO: |
| 5722 | err = get_array_info(mddev, argp); |
| 5723 | goto done_unlock; |
| 5724 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5725 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5726 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5727 | goto done_unlock; |
| 5728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | case GET_DISK_INFO: |
| 5730 | err = get_disk_info(mddev, argp); |
| 5731 | goto done_unlock; |
| 5732 | |
| 5733 | case RESTART_ARRAY_RW: |
| 5734 | err = restart_array(mddev); |
| 5735 | goto done_unlock; |
| 5736 | |
| 5737 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5738 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5739 | goto done_unlock; |
| 5740 | |
| 5741 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5742 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | goto done_unlock; |
| 5744 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5745 | case BLKROSET: |
| 5746 | if (get_user(ro, (int __user *)(arg))) { |
| 5747 | err = -EFAULT; |
| 5748 | goto done_unlock; |
| 5749 | } |
| 5750 | err = -EINVAL; |
| 5751 | |
| 5752 | /* if the bdev is going readonly the value of mddev->ro |
| 5753 | * does not matter, no writes are coming |
| 5754 | */ |
| 5755 | if (ro) |
| 5756 | goto done_unlock; |
| 5757 | |
| 5758 | /* are we are already prepared for writes? */ |
| 5759 | if (mddev->ro != 1) |
| 5760 | goto done_unlock; |
| 5761 | |
| 5762 | /* transitioning to readauto need only happen for |
| 5763 | * arrays that call md_write_start |
| 5764 | */ |
| 5765 | if (mddev->pers) { |
| 5766 | err = restart_array(mddev); |
| 5767 | if (err == 0) { |
| 5768 | mddev->ro = 2; |
| 5769 | set_disk_ro(mddev->gendisk, 0); |
| 5770 | } |
| 5771 | } |
| 5772 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | /* |
| 5776 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5777 | * superblock, so we do not allow them on read-only arrays. |
| 5778 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5779 | * here and hit the 'default' below, so only disallow |
| 5780 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5782 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5783 | if (mddev->ro == 2) { |
| 5784 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5785 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5786 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5787 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5788 | } else { |
| 5789 | err = -EROFS; |
| 5790 | goto abort_unlock; |
| 5791 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | } |
| 5793 | |
| 5794 | switch (cmd) |
| 5795 | { |
| 5796 | case ADD_NEW_DISK: |
| 5797 | { |
| 5798 | mdu_disk_info_t info; |
| 5799 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5800 | err = -EFAULT; |
| 5801 | else |
| 5802 | err = add_new_disk(mddev, &info); |
| 5803 | goto done_unlock; |
| 5804 | } |
| 5805 | |
| 5806 | case HOT_REMOVE_DISK: |
| 5807 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5808 | goto done_unlock; |
| 5809 | |
| 5810 | case HOT_ADD_DISK: |
| 5811 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5812 | goto done_unlock; |
| 5813 | |
| 5814 | case SET_DISK_FAULTY: |
| 5815 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5816 | goto done_unlock; |
| 5817 | |
| 5818 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5819 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5820 | goto done_unlock; |
| 5821 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5822 | case SET_BITMAP_FILE: |
| 5823 | err = set_bitmap_file(mddev, (int)arg); |
| 5824 | goto done_unlock; |
| 5825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5826 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | err = -EINVAL; |
| 5828 | goto abort_unlock; |
| 5829 | } |
| 5830 | |
| 5831 | done_unlock: |
| 5832 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5833 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5834 | err != -EINVAL) |
| 5835 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5836 | mddev_unlock(mddev); |
| 5837 | |
| 5838 | return err; |
| 5839 | done: |
| 5840 | if (err) |
| 5841 | MD_BUG(); |
| 5842 | abort: |
| 5843 | return err; |
| 5844 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5845 | #ifdef CONFIG_COMPAT |
| 5846 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5847 | unsigned int cmd, unsigned long arg) |
| 5848 | { |
| 5849 | switch (cmd) { |
| 5850 | case HOT_REMOVE_DISK: |
| 5851 | case HOT_ADD_DISK: |
| 5852 | case SET_DISK_FAULTY: |
| 5853 | case SET_BITMAP_FILE: |
| 5854 | /* These take in integer arg, do not convert */ |
| 5855 | break; |
| 5856 | default: |
| 5857 | arg = (unsigned long)compat_ptr(arg); |
| 5858 | break; |
| 5859 | } |
| 5860 | |
| 5861 | return md_ioctl(bdev, mode, cmd, arg); |
| 5862 | } |
| 5863 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5864 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5865 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | { |
| 5867 | /* |
| 5868 | * Succeed if we can lock the mddev, which confirms that |
| 5869 | * it isn't being stopped right now. |
| 5870 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5871 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5872 | int err; |
| 5873 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5874 | if (mddev->gendisk != bdev->bd_disk) { |
| 5875 | /* we are racing with mddev_put which is discarding this |
| 5876 | * bd_disk. |
| 5877 | */ |
| 5878 | mddev_put(mddev); |
| 5879 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5880 | flush_scheduled_work(); |
| 5881 | /* Then retry the open from the top */ |
| 5882 | return -ERESTARTSYS; |
| 5883 | } |
| 5884 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5885 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5886 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | goto out; |
| 5888 | |
| 5889 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5890 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5891 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5893 | out: |
| 5894 | return err; |
| 5895 | } |
| 5896 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5897 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5899 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5900 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5901 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5902 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5903 | mddev_put(mddev); |
| 5904 | |
| 5905 | return 0; |
| 5906 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5907 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5908 | { |
| 5909 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5910 | .open = md_open, |
| 5911 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5912 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5913 | #ifdef CONFIG_COMPAT |
| 5914 | .compat_ioctl = md_compat_ioctl, |
| 5915 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5916 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | }; |
| 5918 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5919 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | { |
| 5921 | mdk_thread_t *thread = arg; |
| 5922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | /* |
| 5924 | * md_thread is a 'system-thread', it's priority should be very |
| 5925 | * high. We avoid resource deadlocks individually in each |
| 5926 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5927 | * the very same RT priority as kswapd, thus we will never get |
| 5928 | * into a priority inversion deadlock. |
| 5929 | * |
| 5930 | * we definitely have to have equal or higher priority than |
| 5931 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5932 | * many dirty RAID5 blocks. |
| 5933 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5935 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5936 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5938 | /* We need to wait INTERRUPTIBLE so that |
| 5939 | * we don't add to the load-average. |
| 5940 | * That means we need to be sure no signals are |
| 5941 | * pending |
| 5942 | */ |
| 5943 | if (signal_pending(current)) |
| 5944 | flush_signals(current); |
| 5945 | |
| 5946 | wait_event_interruptible_timeout |
| 5947 | (thread->wqueue, |
| 5948 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5949 | || kthread_should_stop(), |
| 5950 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | |
| 5952 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5953 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5954 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5957 | return 0; |
| 5958 | } |
| 5959 | |
| 5960 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5961 | { |
| 5962 | if (thread) { |
| 5963 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5964 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5965 | wake_up(&thread->wqueue); |
| 5966 | } |
| 5967 | } |
| 5968 | |
| 5969 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 5970 | const char *name) |
| 5971 | { |
| 5972 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5973 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5974 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | if (!thread) |
| 5976 | return NULL; |
| 5977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5978 | init_waitqueue_head(&thread->wqueue); |
| 5979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | thread->run = run; |
| 5981 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5982 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5983 | thread->tsk = kthread_run(md_thread, thread, |
| 5984 | "%s_%s", |
| 5985 | mdname(thread->mddev), |
| 5986 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5987 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | kfree(thread); |
| 5989 | return NULL; |
| 5990 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | return thread; |
| 5992 | } |
| 5993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5994 | void md_unregister_thread(mdk_thread_t *thread) |
| 5995 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5996 | if (!thread) |
| 5997 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 5998 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5999 | |
| 6000 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | kfree(thread); |
| 6002 | } |
| 6003 | |
| 6004 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6005 | { |
| 6006 | if (!mddev) { |
| 6007 | MD_BUG(); |
| 6008 | return; |
| 6009 | } |
| 6010 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6011 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6012 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6013 | |
| 6014 | if (mddev->external) |
| 6015 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6016 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6018 | mdname(mddev), |
| 6019 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6020 | __builtin_return_address(0),__builtin_return_address(1), |
| 6021 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6022 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6023 | if (!mddev->pers) |
| 6024 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | if (!mddev->pers->error_handler) |
| 6026 | return; |
| 6027 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6028 | if (mddev->degraded) |
| 6029 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c0cc75f | 2010-03-22 10:28:51 +1100 | [diff] [blame] | 6030 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6031 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6032 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6033 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6034 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6035 | } |
| 6036 | |
| 6037 | /* seq_file implementation /proc/mdstat */ |
| 6038 | |
| 6039 | static void status_unused(struct seq_file *seq) |
| 6040 | { |
| 6041 | int i = 0; |
| 6042 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | |
| 6044 | seq_printf(seq, "unused devices: "); |
| 6045 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6046 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | char b[BDEVNAME_SIZE]; |
| 6048 | i++; |
| 6049 | seq_printf(seq, "%s ", |
| 6050 | bdevname(rdev->bdev,b)); |
| 6051 | } |
| 6052 | if (!i) |
| 6053 | seq_printf(seq, "<none>"); |
| 6054 | |
| 6055 | seq_printf(seq, "\n"); |
| 6056 | } |
| 6057 | |
| 6058 | |
| 6059 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6060 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6061 | sector_t max_sectors, resync, res; |
| 6062 | unsigned long dt, db; |
| 6063 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6064 | int scale; |
| 6065 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6067 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | |
| 6069 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6070 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6072 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6073 | |
| 6074 | /* |
| 6075 | * Should not happen. |
| 6076 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6077 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | MD_BUG(); |
| 6079 | return; |
| 6080 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6081 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6082 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6083 | * u32, as those are the requirements for sector_div. |
| 6084 | * Thus 'scale' must be at least 10 |
| 6085 | */ |
| 6086 | scale = 10; |
| 6087 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6088 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6089 | scale++; |
| 6090 | } |
| 6091 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6092 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6093 | |
| 6094 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6096 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | seq_printf(seq, "["); |
| 6098 | for (i = 0; i < x; i++) |
| 6099 | seq_printf(seq, "="); |
| 6100 | seq_printf(seq, ">"); |
| 6101 | for (i = 0; i < y; i++) |
| 6102 | seq_printf(seq, "."); |
| 6103 | seq_printf(seq, "] "); |
| 6104 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6105 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6106 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6107 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6108 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6109 | "check" : |
| 6110 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6111 | "resync" : "recovery"))), |
| 6112 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6113 | (unsigned long long) resync/2, |
| 6114 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | |
| 6116 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | * dt: time from mark until now |
| 6118 | * db: blocks written from mark until now |
| 6119 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6120 | * |
| 6121 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6122 | * So we divide before multiply in case it is 32bit and close |
| 6123 | * to the limit. |
| 6124 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6125 | * near the end of resync when the number of remaining sectors |
| 6126 | * is close to 'db'. |
| 6127 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6128 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6129 | */ |
| 6130 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6131 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6132 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6133 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6134 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6135 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6136 | sector_div(rt, db/32+1); |
| 6137 | rt *= dt; |
| 6138 | rt >>= 5; |
| 6139 | |
| 6140 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6141 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6143 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6144 | } |
| 6145 | |
| 6146 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6147 | { |
| 6148 | struct list_head *tmp; |
| 6149 | loff_t l = *pos; |
| 6150 | mddev_t *mddev; |
| 6151 | |
| 6152 | if (l >= 0x10000) |
| 6153 | return NULL; |
| 6154 | if (!l--) |
| 6155 | /* header */ |
| 6156 | return (void*)1; |
| 6157 | |
| 6158 | spin_lock(&all_mddevs_lock); |
| 6159 | list_for_each(tmp,&all_mddevs) |
| 6160 | if (!l--) { |
| 6161 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6162 | mddev_get(mddev); |
| 6163 | spin_unlock(&all_mddevs_lock); |
| 6164 | return mddev; |
| 6165 | } |
| 6166 | spin_unlock(&all_mddevs_lock); |
| 6167 | if (!l--) |
| 6168 | return (void*)2;/* tail */ |
| 6169 | return NULL; |
| 6170 | } |
| 6171 | |
| 6172 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6173 | { |
| 6174 | struct list_head *tmp; |
| 6175 | mddev_t *next_mddev, *mddev = v; |
| 6176 | |
| 6177 | ++*pos; |
| 6178 | if (v == (void*)2) |
| 6179 | return NULL; |
| 6180 | |
| 6181 | spin_lock(&all_mddevs_lock); |
| 6182 | if (v == (void*)1) |
| 6183 | tmp = all_mddevs.next; |
| 6184 | else |
| 6185 | tmp = mddev->all_mddevs.next; |
| 6186 | if (tmp != &all_mddevs) |
| 6187 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6188 | else { |
| 6189 | next_mddev = (void*)2; |
| 6190 | *pos = 0x10000; |
| 6191 | } |
| 6192 | spin_unlock(&all_mddevs_lock); |
| 6193 | |
| 6194 | if (v != (void*)1) |
| 6195 | mddev_put(mddev); |
| 6196 | return next_mddev; |
| 6197 | |
| 6198 | } |
| 6199 | |
| 6200 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6201 | { |
| 6202 | mddev_t *mddev = v; |
| 6203 | |
| 6204 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6205 | mddev_put(mddev); |
| 6206 | } |
| 6207 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6208 | struct mdstat_info { |
| 6209 | int event; |
| 6210 | }; |
| 6211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6212 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6213 | { |
| 6214 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6215 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6216 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6217 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6218 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6219 | |
| 6220 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6221 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6222 | seq_printf(seq, "Personalities : "); |
| 6223 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6224 | list_for_each_entry(pers, &pers_list, list) |
| 6225 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | |
| 6227 | spin_unlock(&pers_lock); |
| 6228 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6229 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6230 | return 0; |
| 6231 | } |
| 6232 | if (v == (void*)2) { |
| 6233 | status_unused(seq); |
| 6234 | return 0; |
| 6235 | } |
| 6236 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6237 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6238 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6241 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6242 | mddev->pers ? "" : "in"); |
| 6243 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6244 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6246 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6247 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | seq_printf(seq, " %s", mddev->pers->name); |
| 6249 | } |
| 6250 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6251 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6252 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6253 | char b[BDEVNAME_SIZE]; |
| 6254 | seq_printf(seq, " %s[%d]", |
| 6255 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6256 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6257 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6258 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | seq_printf(seq, "(F)"); |
| 6260 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6261 | } else if (rdev->raid_disk < 0) |
| 6262 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6263 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | } |
| 6265 | |
| 6266 | if (!list_empty(&mddev->disks)) { |
| 6267 | if (mddev->pers) |
| 6268 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6269 | (unsigned long long) |
| 6270 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | else |
| 6272 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6273 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6274 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6275 | if (mddev->persistent) { |
| 6276 | if (mddev->major_version != 0 || |
| 6277 | mddev->minor_version != 90) { |
| 6278 | seq_printf(seq," super %d.%d", |
| 6279 | mddev->major_version, |
| 6280 | mddev->minor_version); |
| 6281 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6282 | } else if (mddev->external) |
| 6283 | seq_printf(seq, " super external:%s", |
| 6284 | mddev->metadata_type); |
| 6285 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6286 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6287 | |
| 6288 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6289 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6291 | if (mddev->pers->sync_request) { |
| 6292 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6293 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6294 | seq_printf(seq, "\n "); |
| 6295 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6296 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6297 | else if (mddev->recovery_cp < MaxSector) |
| 6298 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6299 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6300 | } else |
| 6301 | seq_printf(seq, "\n "); |
| 6302 | |
| 6303 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6304 | unsigned long chunk_kb; |
| 6305 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6306 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6307 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6308 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6309 | "%lu%s chunk", |
| 6310 | bitmap->pages - bitmap->missing_pages, |
| 6311 | bitmap->pages, |
| 6312 | (bitmap->pages - bitmap->missing_pages) |
| 6313 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6314 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6315 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6316 | if (bitmap->file) { |
| 6317 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6318 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6319 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6320 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6321 | seq_printf(seq, "\n"); |
| 6322 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6323 | } |
| 6324 | |
| 6325 | seq_printf(seq, "\n"); |
| 6326 | } |
| 6327 | mddev_unlock(mddev); |
| 6328 | |
| 6329 | return 0; |
| 6330 | } |
| 6331 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6332 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | .start = md_seq_start, |
| 6334 | .next = md_seq_next, |
| 6335 | .stop = md_seq_stop, |
| 6336 | .show = md_seq_show, |
| 6337 | }; |
| 6338 | |
| 6339 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6340 | { |
| 6341 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6342 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6343 | if (mi == NULL) |
| 6344 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6345 | |
| 6346 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6347 | if (error) |
| 6348 | kfree(mi); |
| 6349 | else { |
| 6350 | struct seq_file *p = file->private_data; |
| 6351 | p->private = mi; |
| 6352 | mi->event = atomic_read(&md_event_count); |
| 6353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6354 | return error; |
| 6355 | } |
| 6356 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6357 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6358 | { |
| 6359 | struct seq_file *m = filp->private_data; |
| 6360 | struct mdstat_info *mi = m->private; |
| 6361 | int mask; |
| 6362 | |
| 6363 | poll_wait(filp, &md_event_waiters, wait); |
| 6364 | |
| 6365 | /* always allow read */ |
| 6366 | mask = POLLIN | POLLRDNORM; |
| 6367 | |
| 6368 | if (mi->event != atomic_read(&md_event_count)) |
| 6369 | mask |= POLLERR | POLLPRI; |
| 6370 | return mask; |
| 6371 | } |
| 6372 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6373 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6374 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | .open = md_seq_open, |
| 6376 | .read = seq_read, |
| 6377 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6378 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6379 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | }; |
| 6381 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6382 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6384 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6385 | list_add_tail(&p->list, &pers_list); |
| 6386 | 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] | 6387 | spin_unlock(&pers_lock); |
| 6388 | return 0; |
| 6389 | } |
| 6390 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6391 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6392 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6393 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6394 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6395 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6396 | spin_unlock(&pers_lock); |
| 6397 | return 0; |
| 6398 | } |
| 6399 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6400 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | { |
| 6402 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6403 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6404 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6405 | |
| 6406 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6407 | rcu_read_lock(); |
| 6408 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6409 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6410 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6411 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6412 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6413 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6414 | * as sync_io is counted when a request starts, and |
| 6415 | * disk_stats is counted when it completes. |
| 6416 | * So resync activity will cause curr_events to be smaller than |
| 6417 | * when there was no such activity. |
| 6418 | * non-sync IO will cause disk_stat to increase without |
| 6419 | * increasing sync_io so curr_events will (eventually) |
| 6420 | * be larger than it was before. Once it becomes |
| 6421 | * substantially larger, the test below will cause |
| 6422 | * the array to appear non-idle, and resync will slow |
| 6423 | * down. |
| 6424 | * If there is a lot of outstanding resync activity when |
| 6425 | * we set last_event to curr_events, then all that activity |
| 6426 | * completing might cause the array to appear non-idle |
| 6427 | * and resync will be slowed down even though there might |
| 6428 | * not have been non-resync activity. This will only |
| 6429 | * happen once though. 'last_events' will soon reflect |
| 6430 | * the state where there is little or no outstanding |
| 6431 | * resync requests, and further resync activity will |
| 6432 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6433 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6435 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | rdev->last_events = curr_events; |
| 6437 | idle = 0; |
| 6438 | } |
| 6439 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6440 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6441 | return idle; |
| 6442 | } |
| 6443 | |
| 6444 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6445 | { |
| 6446 | /* another "blocks" (512byte) blocks have been synced */ |
| 6447 | atomic_sub(blocks, &mddev->recovery_active); |
| 6448 | wake_up(&mddev->recovery_wait); |
| 6449 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6450 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6451 | md_wakeup_thread(mddev->thread); |
| 6452 | // stop recovery, signal do_sync .... |
| 6453 | } |
| 6454 | } |
| 6455 | |
| 6456 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6457 | /* md_write_start(mddev, bi) |
| 6458 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6459 | * in superblock) before writing, schedule a superblock update |
| 6460 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6461 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6462 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6464 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6465 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6466 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6467 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6468 | BUG_ON(mddev->ro == 1); |
| 6469 | if (mddev->ro == 2) { |
| 6470 | /* need to switch to read/write */ |
| 6471 | mddev->ro = 0; |
| 6472 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6473 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6474 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6475 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6476 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6477 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6478 | if (mddev->safemode == 1) |
| 6479 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6480 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6481 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6482 | if (mddev->in_sync) { |
| 6483 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6484 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6485 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6486 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6487 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6488 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6489 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6490 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6491 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6492 | wait_event(mddev->sb_wait, |
| 6493 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6494 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | } |
| 6496 | |
| 6497 | void md_write_end(mddev_t *mddev) |
| 6498 | { |
| 6499 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6500 | if (mddev->safemode == 2) |
| 6501 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6502 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6504 | } |
| 6505 | } |
| 6506 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6507 | /* md_allow_write(mddev) |
| 6508 | * Calling this ensures that the array is marked 'active' so that writes |
| 6509 | * may proceed without blocking. It is important to call this before |
| 6510 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6511 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6512 | * |
| 6513 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6514 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6515 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6516 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6517 | { |
| 6518 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6519 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6520 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6521 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6522 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6523 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6524 | |
| 6525 | spin_lock_irq(&mddev->write_lock); |
| 6526 | if (mddev->in_sync) { |
| 6527 | mddev->in_sync = 0; |
| 6528 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6529 | if (mddev->safemode_delay && |
| 6530 | mddev->safemode == 0) |
| 6531 | mddev->safemode = 1; |
| 6532 | spin_unlock_irq(&mddev->write_lock); |
| 6533 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6534 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6535 | } else |
| 6536 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6537 | |
| 6538 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6539 | return -EAGAIN; |
| 6540 | else |
| 6541 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6542 | } |
| 6543 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6544 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6545 | #define SYNC_MARKS 10 |
| 6546 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6547 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | { |
| 6549 | mddev_t *mddev2; |
| 6550 | unsigned int currspeed = 0, |
| 6551 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6552 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | unsigned long mark[SYNC_MARKS]; |
| 6554 | sector_t mark_cnt[SYNC_MARKS]; |
| 6555 | int last_mark,m; |
| 6556 | struct list_head *tmp; |
| 6557 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6558 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6559 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6560 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | |
| 6562 | /* just incase thread restarts... */ |
| 6563 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6564 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6565 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6566 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6567 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6568 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6569 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6570 | desc = "data-check"; |
| 6571 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6572 | desc = "requested-resync"; |
| 6573 | else |
| 6574 | desc = "resync"; |
| 6575 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6576 | desc = "reshape"; |
| 6577 | else |
| 6578 | desc = "recovery"; |
| 6579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6580 | /* we overload curr_resync somewhat here. |
| 6581 | * 0 == not engaged in resync at all |
| 6582 | * 2 == checking that there is no conflict with another sync |
| 6583 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6584 | * commense |
| 6585 | * other == active in resync - this many blocks |
| 6586 | * |
| 6587 | * Before starting a resync we must have set curr_resync to |
| 6588 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6589 | * less than ours. When we find one that is the same or higher |
| 6590 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6591 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6592 | * This will mean we have to start checking from the beginning again. |
| 6593 | * |
| 6594 | */ |
| 6595 | |
| 6596 | do { |
| 6597 | mddev->curr_resync = 2; |
| 6598 | |
| 6599 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6600 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6601 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6602 | |
| 6603 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6604 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6605 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6606 | if (mddev2 == mddev) |
| 6607 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6608 | if (!mddev->parallel_resync |
| 6609 | && mddev2->curr_resync |
| 6610 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | DEFINE_WAIT(wq); |
| 6612 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6613 | /* arbitrarily yield */ |
| 6614 | mddev->curr_resync = 1; |
| 6615 | wake_up(&resync_wait); |
| 6616 | } |
| 6617 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6618 | /* no need to wait here, we can wait the next |
| 6619 | * time 'round when curr_resync == 2 |
| 6620 | */ |
| 6621 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6622 | /* We need to wait 'interruptible' so as not to |
| 6623 | * contribute to the load average, and not to |
| 6624 | * be caught by 'softlockup' |
| 6625 | */ |
| 6626 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6627 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6628 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6629 | printk(KERN_INFO "md: delaying %s of %s" |
| 6630 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6631 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6632 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6634 | if (signal_pending(current)) |
| 6635 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6636 | schedule(); |
| 6637 | finish_wait(&resync_wait, &wq); |
| 6638 | goto try_again; |
| 6639 | } |
| 6640 | finish_wait(&resync_wait, &wq); |
| 6641 | } |
| 6642 | } |
| 6643 | } while (mddev->curr_resync < 2); |
| 6644 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6645 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6646 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6648 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6649 | */ |
| 6650 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6651 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6652 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6653 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6654 | j = mddev->resync_min; |
| 6655 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6656 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6657 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6658 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6659 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6660 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6661 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6662 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6663 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6664 | rcu_read_lock(); |
| 6665 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6666 | if (rdev->raid_disk >= 0 && |
| 6667 | !test_bit(Faulty, &rdev->flags) && |
| 6668 | !test_bit(In_sync, &rdev->flags) && |
| 6669 | rdev->recovery_offset < j) |
| 6670 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6671 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6673 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6674 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6675 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6676 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6677 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6678 | "(but not more than %d KB/sec) for %s.\n", |
| 6679 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6680 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6681 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6682 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6683 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6684 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6685 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6686 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6687 | } |
| 6688 | last_mark = 0; |
| 6689 | mddev->resync_mark = mark[last_mark]; |
| 6690 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6691 | |
| 6692 | /* |
| 6693 | * Tune reconstruction: |
| 6694 | */ |
| 6695 | window = 32*(PAGE_SIZE/512); |
| 6696 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6697 | window/2,(unsigned long long) max_sectors/2); |
| 6698 | |
| 6699 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6700 | last_check = 0; |
| 6701 | |
| 6702 | if (j>2) { |
| 6703 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6704 | "md: resuming %s of %s from checkpoint.\n", |
| 6705 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | mddev->curr_resync = j; |
| 6707 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6708 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | |
| 6710 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6711 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6712 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6713 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6714 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6715 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6716 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6717 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6718 | > (max_sectors >> 4)) || |
| 6719 | (j - mddev->curr_resync_completed)*2 |
| 6720 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6721 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6722 | /* time to update curr_resync_completed */ |
| 6723 | blk_unplug(mddev->queue); |
| 6724 | wait_event(mddev->recovery_wait, |
| 6725 | atomic_read(&mddev->recovery_active) == 0); |
| 6726 | mddev->curr_resync_completed = |
| 6727 | mddev->curr_resync; |
| 6728 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6729 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6730 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6731 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6732 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6733 | /* As this condition is controlled by user-space, |
| 6734 | * we can block indefinitely, so use '_interruptible' |
| 6735 | * to avoid triggering warnings. |
| 6736 | */ |
| 6737 | flush_signals(current); /* just in case */ |
| 6738 | wait_event_interruptible(mddev->recovery_wait, |
| 6739 | mddev->resync_max > j |
| 6740 | || kthread_should_stop()); |
| 6741 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6742 | |
| 6743 | if (kthread_should_stop()) |
| 6744 | goto interrupted; |
| 6745 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6746 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6747 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6748 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6749 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | goto out; |
| 6751 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6752 | |
| 6753 | if (!skipped) { /* actual IO requested */ |
| 6754 | io_sectors += sectors; |
| 6755 | atomic_add(sectors, &mddev->recovery_active); |
| 6756 | } |
| 6757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6758 | j += sectors; |
| 6759 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6760 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6761 | if (last_check == 0) |
| 6762 | /* this is the earliers that rebuilt will be |
| 6763 | * visible in /proc/mdstat |
| 6764 | */ |
| 6765 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6766 | |
| 6767 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6768 | continue; |
| 6769 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6770 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6771 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6772 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | break; |
| 6774 | |
| 6775 | repeat: |
| 6776 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6777 | /* step marks */ |
| 6778 | int next = (last_mark+1) % SYNC_MARKS; |
| 6779 | |
| 6780 | mddev->resync_mark = mark[next]; |
| 6781 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6782 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6783 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | last_mark = next; |
| 6785 | } |
| 6786 | |
| 6787 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6788 | if (kthread_should_stop()) |
| 6789 | goto interrupted; |
| 6790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6791 | |
| 6792 | /* |
| 6793 | * this loop exits only if either when we are slower than |
| 6794 | * the 'hard' speed limit, or the system was IO-idle for |
| 6795 | * a jiffy. |
| 6796 | * the system might be non-idle CPU-wise, but we only care |
| 6797 | * about not overloading the IO subsystem. (things like an |
| 6798 | * e2fsck being done on the RAID array should execute fast) |
| 6799 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6800 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | cond_resched(); |
| 6802 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6803 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6804 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6806 | if (currspeed > speed_min(mddev)) { |
| 6807 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6808 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6809 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6810 | goto repeat; |
| 6811 | } |
| 6812 | } |
| 6813 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6814 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6815 | /* |
| 6816 | * this also signals 'finished resyncing' to md_stop |
| 6817 | */ |
| 6818 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6819 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6820 | |
| 6821 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6822 | |
| 6823 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6824 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6826 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6827 | mddev->curr_resync > 2) { |
| 6828 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6829 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6830 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6831 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6832 | "md: checkpointing %s of %s.\n", |
| 6833 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6834 | mddev->recovery_cp = mddev->curr_resync; |
| 6835 | } |
| 6836 | } else |
| 6837 | mddev->recovery_cp = MaxSector; |
| 6838 | } else { |
| 6839 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6840 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6841 | rcu_read_lock(); |
| 6842 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6843 | if (rdev->raid_disk >= 0 && |
| 6844 | !test_bit(Faulty, &rdev->flags) && |
| 6845 | !test_bit(In_sync, &rdev->flags) && |
| 6846 | rdev->recovery_offset < mddev->curr_resync) |
| 6847 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6848 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6851 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6853 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6854 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6855 | /* We completed so min/max setting can be forgotten if used. */ |
| 6856 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6857 | mddev->resync_min = 0; |
| 6858 | mddev->resync_max = MaxSector; |
| 6859 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6860 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6862 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6863 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6864 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | wake_up(&resync_wait); |
| 6866 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6867 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6868 | return; |
| 6869 | |
| 6870 | interrupted: |
| 6871 | /* |
| 6872 | * got a signal, exit. |
| 6873 | */ |
| 6874 | printk(KERN_INFO |
| 6875 | "md: md_do_sync() got signal ... exiting\n"); |
| 6876 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6877 | goto out; |
| 6878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6879 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6880 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6881 | |
| 6882 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6883 | static int remove_and_add_spares(mddev_t *mddev) |
| 6884 | { |
| 6885 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6886 | int spares = 0; |
| 6887 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6888 | mddev->curr_resync_completed = 0; |
| 6889 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6890 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6891 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6892 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6893 | (test_bit(Faulty, &rdev->flags) || |
| 6894 | ! test_bit(In_sync, &rdev->flags)) && |
| 6895 | atomic_read(&rdev->nr_pending)==0) { |
| 6896 | if (mddev->pers->hot_remove_disk( |
| 6897 | mddev, rdev->raid_disk)==0) { |
| 6898 | char nm[20]; |
| 6899 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6900 | sysfs_remove_link(&mddev->kobj, nm); |
| 6901 | rdev->raid_disk = -1; |
| 6902 | } |
| 6903 | } |
| 6904 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6905 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6906 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6907 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6908 | !test_bit(In_sync, &rdev->flags) && |
| 6909 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6910 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6911 | if (rdev->raid_disk < 0 |
| 6912 | && !test_bit(Faulty, &rdev->flags)) { |
| 6913 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6914 | if (mddev->pers-> |
| 6915 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6916 | char nm[20]; |
| 6917 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6918 | if (sysfs_create_link(&mddev->kobj, |
| 6919 | &rdev->kobj, nm)) |
| 6920 | printk(KERN_WARNING |
| 6921 | "md: cannot register " |
| 6922 | "%s for %s\n", |
| 6923 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6924 | spares++; |
| 6925 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 6926 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6927 | } else |
| 6928 | break; |
| 6929 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6930 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6931 | } |
| 6932 | return spares; |
| 6933 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6934 | /* |
| 6935 | * This routine is regularly called by all per-raid-array threads to |
| 6936 | * deal with generic issues like resync and super-block update. |
| 6937 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6938 | * need this as they never do any recovery or update the superblock. |
| 6939 | * |
| 6940 | * It does not do any resync itself, but rather "forks" off other threads |
| 6941 | * to do that as needed. |
| 6942 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6943 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6944 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6945 | * and wakeups up this thread which will reap the thread and finish up. |
| 6946 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6947 | * |
| 6948 | * The overall approach is: |
| 6949 | * 1/ if the superblock needs updating, update it. |
| 6950 | * 2/ If a recovery thread is running, don't do anything else. |
| 6951 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6952 | * 4/ If there are any faulty devices, remove them. |
| 6953 | * 5/ If array is degraded, try to add spares devices |
| 6954 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6955 | */ |
| 6956 | void md_check_recovery(mddev_t *mddev) |
| 6957 | { |
| 6958 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | |
| 6960 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6961 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6962 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | |
| 6964 | if (mddev->ro) |
| 6965 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6966 | |
| 6967 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6968 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6969 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 6970 | mdname(mddev)); |
| 6971 | mddev->safemode = 2; |
| 6972 | } |
| 6973 | flush_signals(current); |
| 6974 | } |
| 6975 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6976 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 6977 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6979 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6981 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6982 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6983 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 6984 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6985 | )) |
| 6986 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6987 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 6988 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6989 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6990 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 6991 | if (mddev->ro) { |
| 6992 | /* Only thing we do on a ro array is remove |
| 6993 | * failed devices. |
| 6994 | */ |
| 6995 | remove_and_add_spares(mddev); |
| 6996 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6997 | goto unlock; |
| 6998 | } |
| 6999 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7000 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7001 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7002 | spin_lock_irq(&mddev->write_lock); |
| 7003 | if (mddev->safemode && |
| 7004 | !atomic_read(&mddev->writes_pending) && |
| 7005 | !mddev->in_sync && |
| 7006 | mddev->recovery_cp == MaxSector) { |
| 7007 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7008 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7009 | if (mddev->persistent) |
| 7010 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 7011 | } |
| 7012 | if (mddev->safemode == 1) |
| 7013 | mddev->safemode = 0; |
| 7014 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7015 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 7016 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7017 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7018 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7019 | if (mddev->flags) |
| 7020 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7022 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7023 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7024 | /* resync/recovery still happening */ |
| 7025 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7026 | goto unlock; |
| 7027 | } |
| 7028 | if (mddev->sync_thread) { |
| 7029 | /* resync has finished, collect result */ |
| 7030 | md_unregister_thread(mddev->sync_thread); |
| 7031 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7032 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7033 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7034 | /* success...*/ |
| 7035 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 7036 | if (mddev->pers->spare_active(mddev)) |
| 7037 | sysfs_notify(&mddev->kobj, NULL, |
| 7038 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7039 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7040 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7041 | mddev->pers->finish_reshape) |
| 7042 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7043 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7044 | |
| 7045 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7046 | * information must be scrapped |
| 7047 | */ |
| 7048 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7049 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7050 | rdev->saved_raid_disk = -1; |
| 7051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7052 | mddev->recovery = 0; |
| 7053 | /* flag recovery needed just to double check */ |
| 7054 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7055 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7056 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7057 | goto unlock; |
| 7058 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7059 | /* Set RUNNING before clearing NEEDED to avoid |
| 7060 | * any transients in the value of "sync_action". |
| 7061 | */ |
| 7062 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7063 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7064 | /* Clear some bits that don't mean anything, but |
| 7065 | * might be left set |
| 7066 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7067 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7068 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7069 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7070 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7071 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7072 | /* no recovery is running. |
| 7073 | * remove any failed drives, then |
| 7074 | * add spares if possible. |
| 7075 | * Spare are also removed and re-added, to allow |
| 7076 | * the personality to fail the re-add. |
| 7077 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7078 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7079 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7080 | if (mddev->pers->check_reshape == NULL || |
| 7081 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7082 | /* Cannot proceed */ |
| 7083 | goto unlock; |
| 7084 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7085 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7086 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7087 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7088 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7089 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7090 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7091 | } else if (mddev->recovery_cp < MaxSector) { |
| 7092 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7093 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7094 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7095 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7098 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7099 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7100 | /* We are adding a device or devices to an array |
| 7101 | * which has the bitmap stored on all devices. |
| 7102 | * So make sure all bitmap pages get written |
| 7103 | */ |
| 7104 | bitmap_write_all(mddev->bitmap); |
| 7105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7106 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7107 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7108 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7109 | if (!mddev->sync_thread) { |
| 7110 | printk(KERN_ERR "%s: could not start resync" |
| 7111 | " thread...\n", |
| 7112 | mdname(mddev)); |
| 7113 | /* leave the spares where they are, it shouldn't hurt */ |
| 7114 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7115 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7116 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7117 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7118 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | } |
| 7120 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7121 | if (!mddev->sync_thread) { |
| 7122 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7123 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7124 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7125 | if (mddev->sysfs_action) |
| 7126 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7128 | mddev_unlock(mddev); |
| 7129 | } |
| 7130 | } |
| 7131 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7132 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7133 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 7134 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7135 | wait_event_timeout(rdev->blocked_wait, |
| 7136 | !test_bit(Blocked, &rdev->flags), |
| 7137 | msecs_to_jiffies(5000)); |
| 7138 | rdev_dec_pending(rdev, mddev); |
| 7139 | } |
| 7140 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7141 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7142 | static int md_notify_reboot(struct notifier_block *this, |
| 7143 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7144 | { |
| 7145 | struct list_head *tmp; |
| 7146 | mddev_t *mddev; |
| 7147 | |
| 7148 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7149 | |
| 7150 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7151 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7152 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7153 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7154 | /* Force a switch to readonly even array |
| 7155 | * appears to still be in use. Hence |
| 7156 | * the '100'. |
| 7157 | */ |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7158 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7159 | mddev_unlock(mddev); |
| 7160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7161 | /* |
| 7162 | * certain more exotic SCSI devices are known to be |
| 7163 | * volatile wrt too early system reboots. While the |
| 7164 | * right place to handle this issue is the given |
| 7165 | * driver, we do want to have a safe RAID driver ... |
| 7166 | */ |
| 7167 | mdelay(1000*1); |
| 7168 | } |
| 7169 | return NOTIFY_DONE; |
| 7170 | } |
| 7171 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7172 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7173 | .notifier_call = md_notify_reboot, |
| 7174 | .next = NULL, |
| 7175 | .priority = INT_MAX, /* before any real devices */ |
| 7176 | }; |
| 7177 | |
| 7178 | static void md_geninit(void) |
| 7179 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7180 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7181 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7182 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7183 | } |
| 7184 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7185 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7186 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7187 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7188 | return -1; |
| 7189 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7190 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7191 | return -1; |
| 7192 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7193 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7194 | md_probe, NULL, NULL); |
| 7195 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7196 | md_probe, NULL, NULL); |
| 7197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7198 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7199 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7200 | |
| 7201 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7202 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7203 | } |
| 7204 | |
| 7205 | |
| 7206 | #ifndef MODULE |
| 7207 | |
| 7208 | /* |
| 7209 | * Searches all registered partitions for autorun RAID arrays |
| 7210 | * at boot time. |
| 7211 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7212 | |
| 7213 | static LIST_HEAD(all_detected_devices); |
| 7214 | struct detected_devices_node { |
| 7215 | struct list_head list; |
| 7216 | dev_t dev; |
| 7217 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7218 | |
| 7219 | void md_autodetect_dev(dev_t dev) |
| 7220 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7221 | struct detected_devices_node *node_detected_dev; |
| 7222 | |
| 7223 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7224 | if (node_detected_dev) { |
| 7225 | node_detected_dev->dev = dev; |
| 7226 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7227 | } else { |
| 7228 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7229 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7231 | } |
| 7232 | |
| 7233 | |
| 7234 | static void autostart_arrays(int part) |
| 7235 | { |
| 7236 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7237 | struct detected_devices_node *node_detected_dev; |
| 7238 | dev_t dev; |
| 7239 | int i_scanned, i_passed; |
| 7240 | |
| 7241 | i_scanned = 0; |
| 7242 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7243 | |
| 7244 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7245 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7246 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7247 | i_scanned++; |
| 7248 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7249 | struct detected_devices_node, list); |
| 7250 | list_del(&node_detected_dev->list); |
| 7251 | dev = node_detected_dev->dev; |
| 7252 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7253 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7254 | if (IS_ERR(rdev)) |
| 7255 | continue; |
| 7256 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7257 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7258 | MD_BUG(); |
| 7259 | continue; |
| 7260 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7261 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7262 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7263 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7264 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7265 | |
| 7266 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7267 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7268 | |
| 7269 | autorun_devices(part); |
| 7270 | } |
| 7271 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7272 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7273 | |
| 7274 | static __exit void md_exit(void) |
| 7275 | { |
| 7276 | mddev_t *mddev; |
| 7277 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7278 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7279 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7280 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7282 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7283 | unregister_blkdev(mdp_major, "mdp"); |
| 7284 | unregister_reboot_notifier(&md_notifier); |
| 7285 | unregister_sysctl_table(raid_table_header); |
| 7286 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7287 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7288 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7289 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7290 | } |
| 7291 | } |
| 7292 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7293 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7294 | module_exit(md_exit) |
| 7295 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7296 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7297 | { |
| 7298 | return sprintf(buffer, "%d", start_readonly); |
| 7299 | } |
| 7300 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7301 | { |
| 7302 | char *e; |
| 7303 | int num = simple_strtoul(val, &e, 10); |
| 7304 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7305 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7306 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7307 | } |
| 7308 | return -EINVAL; |
| 7309 | } |
| 7310 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7311 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7312 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7313 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7314 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7316 | EXPORT_SYMBOL(register_md_personality); |
| 7317 | EXPORT_SYMBOL(unregister_md_personality); |
| 7318 | EXPORT_SYMBOL(md_error); |
| 7319 | EXPORT_SYMBOL(md_done_sync); |
| 7320 | EXPORT_SYMBOL(md_write_start); |
| 7321 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7322 | EXPORT_SYMBOL(md_register_thread); |
| 7323 | EXPORT_SYMBOL(md_unregister_thread); |
| 7324 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7325 | EXPORT_SYMBOL(md_check_recovery); |
| 7326 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7327 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7328 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7329 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |