Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
| 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
| 4 | |
| 5 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 6 | |
| 7 | Changes: |
| 8 | |
| 9 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 10 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 11 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 12 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 13 | - kmod support by: Cyrus Durgin |
| 14 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 15 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 16 | |
| 17 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 18 | RAID code (such as request based resynchronization): |
| 19 | |
| 20 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 21 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 22 | - persistent bitmap code |
| 23 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | This program is free software; you can redistribute it and/or modify |
| 26 | it under the terms of the GNU General Public License as published by |
| 27 | the Free Software Foundation; either version 2, or (at your option) |
| 28 | any later version. |
| 29 | |
| 30 | You should have received a copy of the GNU General Public License |
| 31 | (for example /usr/src/linux/COPYING); if not, write to the Free |
| 32 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 33 | */ |
| 34 | |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 35 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 36 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/buffer_head.h> /* for invalidate_bdev */ |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 40 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 41 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 42 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 43 | #include <linux/hdreg.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/random.h> |
| 46 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 47 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 48 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 49 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 50 | #include <linux/raid/md_p.h> |
| 51 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 52 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 53 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 54 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | #define DEBUG 0 |
| 57 | #define dprintk(x...) ((void)(DEBUG && printk(x))) |
| 58 | |
| 59 | |
| 60 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 61 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | #endif |
| 63 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 64 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(pers_lock); |
| 66 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 67 | static void md_print_devices(void); |
| 68 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 69 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
| 70 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 71 | #define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); } |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 74 | * Default number of read corrections we'll attempt on an rdev |
| 75 | * before ejecting it from the array. We divide the read error |
| 76 | * count by 2 for every hour elapsed between read errors. |
| 77 | */ |
| 78 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 79 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 81 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 82 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 83 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 85 | * speed limit - in case reconstruction slows down your system despite |
| 86 | * idle IO detection. |
| 87 | * |
| 88 | * you can change it via /proc/sys/dev/raid/speed_limit_min and _max. |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 89 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | */ |
| 91 | |
| 92 | static int sysctl_speed_limit_min = 1000; |
| 93 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 94 | static inline int speed_min(mddev_t *mddev) |
| 95 | { |
| 96 | return mddev->sync_speed_min ? |
| 97 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 98 | } |
| 99 | |
| 100 | static inline int speed_max(mddev_t *mddev) |
| 101 | { |
| 102 | return mddev->sync_speed_max ? |
| 103 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 104 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | static struct ctl_table_header *raid_table_header; |
| 107 | |
| 108 | static ctl_table raid_table[] = { |
| 109 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | .procname = "speed_limit_min", |
| 111 | .data = &sysctl_speed_limit_min, |
| 112 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 113 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 114 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | }, |
| 116 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .procname = "speed_limit_max", |
| 118 | .data = &sysctl_speed_limit_max, |
| 119 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 120 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 121 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 123 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | static ctl_table raid_dir_table[] = { |
| 127 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | .procname = "raid", |
| 129 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 130 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .child = raid_table, |
| 132 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 133 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | static ctl_table raid_root_table[] = { |
| 137 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .procname = "dev", |
| 139 | .maxlen = 0, |
| 140 | .mode = 0555, |
| 141 | .child = raid_dir_table, |
| 142 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 143 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 146 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 148 | static int start_readonly; |
| 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 151 | * We have a system wide 'event count' that is incremented |
| 152 | * on any 'interesting' event, and readers of /proc/mdstat |
| 153 | * can use 'poll' or 'select' to find out when the event |
| 154 | * count increases. |
| 155 | * |
| 156 | * Events are: |
| 157 | * start array, stop array, error, add device, remove device, |
| 158 | * start build, activate spare |
| 159 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 160 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 161 | static atomic_t md_event_count; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 162 | void md_new_event(mddev_t *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 163 | { |
| 164 | atomic_inc(&md_event_count); |
| 165 | wake_up(&md_event_waiters); |
| 166 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 167 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 168 | |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 169 | /* Alternate version that can be called from interrupts |
| 170 | * when calling sysfs_notify isn't needed. |
| 171 | */ |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 172 | static void md_new_event_inintr(mddev_t *mddev) |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 173 | { |
| 174 | atomic_inc(&md_event_count); |
| 175 | wake_up(&md_event_waiters); |
| 176 | } |
| 177 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 178 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | * Enables to iterate over all existing md arrays |
| 180 | * all_mddevs_lock protects this list. |
| 181 | */ |
| 182 | static LIST_HEAD(all_mddevs); |
| 183 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 184 | |
| 185 | |
| 186 | /* |
| 187 | * iterates through all used mddevs in the system. |
| 188 | * We take care to grab the all_mddevs_lock whenever navigating |
| 189 | * the list, and to always hold a refcount when unlocked. |
| 190 | * Any code which breaks out of this loop while own |
| 191 | * a reference to the current mddev and must mddev_put it. |
| 192 | */ |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 193 | #define for_each_mddev(mddev,tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | \ |
| 195 | for (({ spin_lock(&all_mddevs_lock); \ |
| 196 | tmp = all_mddevs.next; \ |
| 197 | mddev = NULL;}); \ |
| 198 | ({ if (tmp != &all_mddevs) \ |
| 199 | mddev_get(list_entry(tmp, mddev_t, all_mddevs));\ |
| 200 | spin_unlock(&all_mddevs_lock); \ |
| 201 | if (mddev) mddev_put(mddev); \ |
| 202 | mddev = list_entry(tmp, mddev_t, all_mddevs); \ |
| 203 | tmp != &all_mddevs;}); \ |
| 204 | ({ spin_lock(&all_mddevs_lock); \ |
| 205 | tmp = tmp->next;}) \ |
| 206 | ) |
| 207 | |
| 208 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 209 | /* Rather than calling directly into the personality make_request function, |
| 210 | * IO requests come here first so that we can check if the device is |
| 211 | * being suspended pending a reconfiguration. |
| 212 | * We hold a refcount over the call to ->make_request. By the time that |
| 213 | * call has finished, the bio has been linked into some internal structure |
| 214 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 215 | */ |
| 216 | static int md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 218 | const int rw = bio_data_dir(bio); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 219 | mddev_t *mddev = q->queuedata; |
| 220 | int rv; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 221 | int cpu; |
| 222 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 223 | if (mddev == NULL || mddev->pers == NULL) { |
| 224 | bio_io_error(bio); |
| 225 | return 0; |
| 226 | } |
| 227 | rcu_read_lock(); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 228 | if (mddev->suspended || mddev->barrier) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 229 | DEFINE_WAIT(__wait); |
| 230 | for (;;) { |
| 231 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 232 | TASK_UNINTERRUPTIBLE); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 233 | if (!mddev->suspended && !mddev->barrier) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 234 | break; |
| 235 | rcu_read_unlock(); |
| 236 | schedule(); |
| 237 | rcu_read_lock(); |
| 238 | } |
| 239 | finish_wait(&mddev->sb_wait, &__wait); |
| 240 | } |
| 241 | atomic_inc(&mddev->active_io); |
| 242 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 243 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 244 | rv = mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 245 | |
| 246 | cpu = part_stat_lock(); |
| 247 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 248 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], |
| 249 | bio_sectors(bio)); |
| 250 | part_stat_unlock(); |
| 251 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 252 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 253 | wake_up(&mddev->sb_wait); |
| 254 | |
| 255 | return rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 258 | /* mddev_suspend makes sure no new requests are submitted |
| 259 | * to the device, and that any requests that have been submitted |
| 260 | * are completely handled. |
| 261 | * Once ->stop is called and completes, the module will be completely |
| 262 | * unused. |
| 263 | */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 264 | static void mddev_suspend(mddev_t *mddev) |
| 265 | { |
| 266 | BUG_ON(mddev->suspended); |
| 267 | mddev->suspended = 1; |
| 268 | synchronize_rcu(); |
| 269 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 270 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static void mddev_resume(mddev_t *mddev) |
| 274 | { |
| 275 | mddev->suspended = 0; |
| 276 | wake_up(&mddev->sb_wait); |
| 277 | mddev->pers->quiesce(mddev, 0); |
| 278 | } |
| 279 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 280 | int mddev_congested(mddev_t *mddev, int bits) |
| 281 | { |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 282 | if (mddev->barrier) |
| 283 | return 1; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 284 | return mddev->suspended; |
| 285 | } |
| 286 | EXPORT_SYMBOL(mddev_congested); |
| 287 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 288 | /* |
| 289 | * Generic barrier handling for md |
| 290 | */ |
| 291 | |
| 292 | #define POST_REQUEST_BARRIER ((void*)1) |
| 293 | |
| 294 | static void md_end_barrier(struct bio *bio, int err) |
| 295 | { |
| 296 | mdk_rdev_t *rdev = bio->bi_private; |
| 297 | mddev_t *mddev = rdev->mddev; |
| 298 | if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER) |
| 299 | set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags); |
| 300 | |
| 301 | rdev_dec_pending(rdev, mddev); |
| 302 | |
| 303 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 304 | if (mddev->barrier == POST_REQUEST_BARRIER) { |
| 305 | /* This was a post-request barrier */ |
| 306 | mddev->barrier = NULL; |
| 307 | wake_up(&mddev->sb_wait); |
| 308 | } else |
| 309 | /* The pre-request barrier has finished */ |
| 310 | schedule_work(&mddev->barrier_work); |
| 311 | } |
| 312 | bio_put(bio); |
| 313 | } |
| 314 | |
| 315 | static void submit_barriers(mddev_t *mddev) |
| 316 | { |
| 317 | mdk_rdev_t *rdev; |
| 318 | |
| 319 | rcu_read_lock(); |
| 320 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
| 321 | if (rdev->raid_disk >= 0 && |
| 322 | !test_bit(Faulty, &rdev->flags)) { |
| 323 | /* Take two references, one is dropped |
| 324 | * when request finishes, one after |
| 325 | * we reclaim rcu_read_lock |
| 326 | */ |
| 327 | struct bio *bi; |
| 328 | atomic_inc(&rdev->nr_pending); |
| 329 | atomic_inc(&rdev->nr_pending); |
| 330 | rcu_read_unlock(); |
| 331 | bi = bio_alloc(GFP_KERNEL, 0); |
| 332 | bi->bi_end_io = md_end_barrier; |
| 333 | bi->bi_private = rdev; |
| 334 | bi->bi_bdev = rdev->bdev; |
| 335 | atomic_inc(&mddev->flush_pending); |
| 336 | submit_bio(WRITE_BARRIER, bi); |
| 337 | rcu_read_lock(); |
| 338 | rdev_dec_pending(rdev, mddev); |
| 339 | } |
| 340 | rcu_read_unlock(); |
| 341 | } |
| 342 | |
| 343 | static void md_submit_barrier(struct work_struct *ws) |
| 344 | { |
| 345 | mddev_t *mddev = container_of(ws, mddev_t, barrier_work); |
| 346 | struct bio *bio = mddev->barrier; |
| 347 | |
| 348 | atomic_set(&mddev->flush_pending, 1); |
| 349 | |
| 350 | if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags)) |
| 351 | bio_endio(bio, -EOPNOTSUPP); |
| 352 | else if (bio->bi_size == 0) |
| 353 | /* an empty barrier - all done */ |
| 354 | bio_endio(bio, 0); |
| 355 | else { |
| 356 | bio->bi_rw &= ~(1<<BIO_RW_BARRIER); |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 357 | if (mddev->pers->make_request(mddev, bio)) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 358 | generic_make_request(bio); |
| 359 | mddev->barrier = POST_REQUEST_BARRIER; |
| 360 | submit_barriers(mddev); |
| 361 | } |
| 362 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 363 | mddev->barrier = NULL; |
| 364 | wake_up(&mddev->sb_wait); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | void md_barrier_request(mddev_t *mddev, struct bio *bio) |
| 369 | { |
| 370 | spin_lock_irq(&mddev->write_lock); |
| 371 | wait_event_lock_irq(mddev->sb_wait, |
| 372 | !mddev->barrier, |
| 373 | mddev->write_lock, /*nothing*/); |
| 374 | mddev->barrier = bio; |
| 375 | spin_unlock_irq(&mddev->write_lock); |
| 376 | |
| 377 | atomic_set(&mddev->flush_pending, 1); |
| 378 | INIT_WORK(&mddev->barrier_work, md_submit_barrier); |
| 379 | |
| 380 | submit_barriers(mddev); |
| 381 | |
| 382 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 383 | schedule_work(&mddev->barrier_work); |
| 384 | } |
| 385 | EXPORT_SYMBOL(md_barrier_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | static inline mddev_t *mddev_get(mddev_t *mddev) |
| 388 | { |
| 389 | atomic_inc(&mddev->active); |
| 390 | return mddev; |
| 391 | } |
| 392 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 393 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | static void mddev_put(mddev_t *mddev) |
| 396 | { |
| 397 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 398 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 399 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 400 | mddev->ctime == 0 && !mddev->hold_active) { |
| 401 | /* Array is not configured at all, and not held active, |
| 402 | * so destroy it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | list_del(&mddev->all_mddevs); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 404 | if (mddev->gendisk) { |
| 405 | /* we did a probe so need to clean up. |
| 406 | * Call schedule_work inside the spinlock |
| 407 | * so that flush_scheduled_work() after |
| 408 | * mddev_find will succeed in waiting for the |
| 409 | * work to be done. |
| 410 | */ |
| 411 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 412 | schedule_work(&mddev->del_work); |
| 413 | } else |
| 414 | kfree(mddev); |
| 415 | } |
| 416 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 419 | static void mddev_init(mddev_t *mddev) |
| 420 | { |
| 421 | mutex_init(&mddev->open_mutex); |
| 422 | mutex_init(&mddev->reconfig_mutex); |
| 423 | mutex_init(&mddev->bitmap_info.mutex); |
| 424 | INIT_LIST_HEAD(&mddev->disks); |
| 425 | INIT_LIST_HEAD(&mddev->all_mddevs); |
| 426 | init_timer(&mddev->safemode_timer); |
| 427 | atomic_set(&mddev->active, 1); |
| 428 | atomic_set(&mddev->openers, 0); |
| 429 | atomic_set(&mddev->active_io, 0); |
| 430 | spin_lock_init(&mddev->write_lock); |
| 431 | atomic_set(&mddev->flush_pending, 0); |
| 432 | init_waitqueue_head(&mddev->sb_wait); |
| 433 | init_waitqueue_head(&mddev->recovery_wait); |
| 434 | mddev->reshape_position = MaxSector; |
| 435 | mddev->resync_min = 0; |
| 436 | mddev->resync_max = MaxSector; |
| 437 | mddev->level = LEVEL_NONE; |
| 438 | } |
| 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | static mddev_t * mddev_find(dev_t unit) |
| 441 | { |
| 442 | mddev_t *mddev, *new = NULL; |
| 443 | |
| 444 | retry: |
| 445 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 447 | if (unit) { |
| 448 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 449 | if (mddev->unit == unit) { |
| 450 | mddev_get(mddev); |
| 451 | spin_unlock(&all_mddevs_lock); |
| 452 | kfree(new); |
| 453 | return mddev; |
| 454 | } |
| 455 | |
| 456 | if (new) { |
| 457 | list_add(&new->all_mddevs, &all_mddevs); |
| 458 | spin_unlock(&all_mddevs_lock); |
| 459 | new->hold_active = UNTIL_IOCTL; |
| 460 | return new; |
| 461 | } |
| 462 | } else if (new) { |
| 463 | /* find an unused unit number */ |
| 464 | static int next_minor = 512; |
| 465 | int start = next_minor; |
| 466 | int is_free = 0; |
| 467 | int dev = 0; |
| 468 | while (!is_free) { |
| 469 | dev = MKDEV(MD_MAJOR, next_minor); |
| 470 | next_minor++; |
| 471 | if (next_minor > MINORMASK) |
| 472 | next_minor = 0; |
| 473 | if (next_minor == start) { |
| 474 | /* Oh dear, all in use. */ |
| 475 | spin_unlock(&all_mddevs_lock); |
| 476 | kfree(new); |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | is_free = 1; |
| 481 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 482 | if (mddev->unit == dev) { |
| 483 | is_free = 0; |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | new->unit = dev; |
| 488 | new->md_minor = MINOR(dev); |
| 489 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | list_add(&new->all_mddevs, &all_mddevs); |
| 491 | spin_unlock(&all_mddevs_lock); |
| 492 | return new; |
| 493 | } |
| 494 | spin_unlock(&all_mddevs_lock); |
| 495 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 496 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | if (!new) |
| 498 | return NULL; |
| 499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | new->unit = unit; |
| 501 | if (MAJOR(unit) == MD_MAJOR) |
| 502 | new->md_minor = MINOR(unit); |
| 503 | else |
| 504 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 505 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 506 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | goto retry; |
| 509 | } |
| 510 | |
| 511 | static inline int mddev_lock(mddev_t * mddev) |
| 512 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 513 | return mutex_lock_interruptible(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 516 | static inline int mddev_is_locked(mddev_t *mddev) |
| 517 | { |
| 518 | return mutex_is_locked(&mddev->reconfig_mutex); |
| 519 | } |
| 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | static inline int mddev_trylock(mddev_t * mddev) |
| 522 | { |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 523 | return mutex_trylock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
| 525 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 526 | static struct attribute_group md_redundancy_group; |
| 527 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 528 | static void mddev_unlock(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 530 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 531 | /* These cannot be removed under reconfig_mutex as |
| 532 | * an access to the files will try to take reconfig_mutex |
| 533 | * while holding the file unremovable, which leads to |
| 534 | * a deadlock. |
| 535 | * So hold open_mutex instead - we are allowed to take |
| 536 | * it while holding reconfig_mutex, and md_run can |
| 537 | * use it to wait for the remove to complete. |
| 538 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 539 | struct attribute_group *to_remove = mddev->to_remove; |
| 540 | mddev->to_remove = NULL; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 541 | mutex_lock(&mddev->open_mutex); |
| 542 | mutex_unlock(&mddev->reconfig_mutex); |
| 543 | |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 544 | if (to_remove != &md_redundancy_group) |
| 545 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 546 | if (mddev->pers == NULL || |
| 547 | mddev->pers->sync_request == NULL) { |
| 548 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 549 | if (mddev->sysfs_action) |
| 550 | sysfs_put(mddev->sysfs_action); |
| 551 | mddev->sysfs_action = NULL; |
| 552 | } |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 553 | mutex_unlock(&mddev->open_mutex); |
| 554 | } else |
| 555 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 557 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 560 | static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 562 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 564 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | if (rdev->desc_nr == nr) |
| 566 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | return NULL; |
| 569 | } |
| 570 | |
| 571 | static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev) |
| 572 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | mdk_rdev_t *rdev; |
| 574 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 575 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | if (rdev->bdev->bd_dev == dev) |
| 577 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | return NULL; |
| 580 | } |
| 581 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 582 | static struct mdk_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 583 | { |
| 584 | struct mdk_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 585 | list_for_each_entry(pers, &pers_list, list) { |
| 586 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 587 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 588 | if (strcmp(pers->name, clevel)==0) |
| 589 | return pers; |
| 590 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 591 | return NULL; |
| 592 | } |
| 593 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 594 | /* return the offset of the super block in 512byte sectors */ |
Jesper Juhl | 77933d7 | 2005-07-27 11:46:09 -0700 | [diff] [blame] | 595 | static inline sector_t calc_dev_sboffset(struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | { |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 597 | sector_t num_sectors = bdev->bd_inode->i_size / 512; |
| 598 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | static int alloc_disk_sb(mdk_rdev_t * rdev) |
| 602 | { |
| 603 | if (rdev->sb_page) |
| 604 | MD_BUG(); |
| 605 | |
| 606 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 607 | if (!rdev->sb_page) { |
| 608 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 609 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static void free_disk_sb(mdk_rdev_t * rdev) |
| 616 | { |
| 617 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 618 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | rdev->sb_loaded = 0; |
| 620 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 621 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 622 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 627 | static void super_written(struct bio *bio, int error) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 628 | { |
| 629 | mdk_rdev_t *rdev = bio->bi_private; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 630 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 631 | |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 632 | if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 633 | printk("md: super_written gets error=%d, uptodate=%d\n", |
| 634 | error, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
| 635 | WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 636 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 637 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 638 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 639 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 640 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 641 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 642 | } |
| 643 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 644 | static void super_written_barrier(struct bio *bio, int error) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 645 | { |
| 646 | struct bio *bio2 = bio->bi_private; |
| 647 | mdk_rdev_t *rdev = bio2->bi_private; |
| 648 | mddev_t *mddev = rdev->mddev; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 649 | |
| 650 | if (!test_bit(BIO_UPTODATE, &bio->bi_flags) && |
| 651 | error == -EOPNOTSUPP) { |
| 652 | unsigned long flags; |
| 653 | /* barriers don't appear to be supported :-( */ |
| 654 | set_bit(BarriersNotsupp, &rdev->flags); |
| 655 | mddev->barriers_work = 0; |
| 656 | spin_lock_irqsave(&mddev->write_lock, flags); |
| 657 | bio2->bi_next = mddev->biolist; |
| 658 | mddev->biolist = bio2; |
| 659 | spin_unlock_irqrestore(&mddev->write_lock, flags); |
| 660 | wake_up(&mddev->sb_wait); |
| 661 | bio_put(bio); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 662 | } else { |
| 663 | bio_put(bio2); |
| 664 | bio->bi_private = rdev; |
| 665 | super_written(bio, error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 666 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 667 | } |
| 668 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 669 | void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev, |
| 670 | sector_t sector, int size, struct page *page) |
| 671 | { |
| 672 | /* write first size bytes of page to sector of rdev |
| 673 | * Increment mddev->pending_writes before returning |
| 674 | * and decrement it on completion, waking up sb_wait |
| 675 | * if zero is reached. |
| 676 | * If an error occurred, call md_error |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 677 | * |
| 678 | * As we might need to resubmit the request if BIO_RW_BARRIER |
| 679 | * causes ENOTSUPP, we allocate a spare bio... |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 680 | */ |
| 681 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 682 | int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 683 | |
| 684 | bio->bi_bdev = rdev->bdev; |
| 685 | bio->bi_sector = sector; |
| 686 | bio_add_page(bio, page, size, 0); |
| 687 | bio->bi_private = rdev; |
| 688 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 689 | bio->bi_rw = rw; |
| 690 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 691 | atomic_inc(&mddev->pending_writes); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 692 | if (!test_bit(BarriersNotsupp, &rdev->flags)) { |
| 693 | struct bio *rbio; |
| 694 | rw |= (1<<BIO_RW_BARRIER); |
| 695 | rbio = bio_clone(bio, GFP_NOIO); |
| 696 | rbio->bi_private = bio; |
| 697 | rbio->bi_end_io = super_written_barrier; |
| 698 | submit_bio(rw, rbio); |
| 699 | } else |
| 700 | submit_bio(rw, bio); |
| 701 | } |
| 702 | |
| 703 | void md_super_wait(mddev_t *mddev) |
| 704 | { |
| 705 | /* wait for all superblock writes that were scheduled to complete. |
| 706 | * if any had to be retried (due to BARRIER problems), retry them |
| 707 | */ |
| 708 | DEFINE_WAIT(wq); |
| 709 | for(;;) { |
| 710 | prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE); |
| 711 | if (atomic_read(&mddev->pending_writes)==0) |
| 712 | break; |
| 713 | while (mddev->biolist) { |
| 714 | struct bio *bio; |
| 715 | spin_lock_irq(&mddev->write_lock); |
| 716 | bio = mddev->biolist; |
| 717 | mddev->biolist = bio->bi_next ; |
| 718 | bio->bi_next = NULL; |
| 719 | spin_unlock_irq(&mddev->write_lock); |
| 720 | submit_bio(bio->bi_rw, bio); |
| 721 | } |
| 722 | schedule(); |
| 723 | } |
| 724 | finish_wait(&mddev->sb_wait, &wq); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 725 | } |
| 726 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 727 | static void bi_complete(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | complete((struct completion*)bio->bi_private); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
| 731 | |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 732 | 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] | 733 | struct page *page, int rw) |
| 734 | { |
Neil Brown | baaa2c5 | 2005-04-16 15:23:54 -0700 | [diff] [blame] | 735 | struct bio *bio = bio_alloc(GFP_NOIO, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | struct completion event; |
| 737 | int ret; |
| 738 | |
Jens Axboe | 93dbb39 | 2009-02-16 10:25:40 +0100 | [diff] [blame] | 739 | rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
| 741 | bio->bi_bdev = bdev; |
| 742 | bio->bi_sector = sector; |
| 743 | bio_add_page(bio, page, size, 0); |
| 744 | init_completion(&event); |
| 745 | bio->bi_private = &event; |
| 746 | bio->bi_end_io = bi_complete; |
| 747 | submit_bio(rw, bio); |
| 748 | wait_for_completion(&event); |
| 749 | |
| 750 | ret = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 751 | bio_put(bio); |
| 752 | return ret; |
| 753 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 754 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 756 | static int read_disk_sb(mdk_rdev_t * rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | { |
| 758 | char b[BDEVNAME_SIZE]; |
| 759 | if (!rdev->sb_page) { |
| 760 | MD_BUG(); |
| 761 | return -EINVAL; |
| 762 | } |
| 763 | if (rdev->sb_loaded) |
| 764 | return 0; |
| 765 | |
| 766 | |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 767 | 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] | 768 | goto fail; |
| 769 | rdev->sb_loaded = 1; |
| 770 | return 0; |
| 771 | |
| 772 | fail: |
| 773 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 774 | bdevname(rdev->bdev,b)); |
| 775 | return -EINVAL; |
| 776 | } |
| 777 | |
| 778 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 779 | { |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 780 | return sb1->set_uuid0 == sb2->set_uuid0 && |
| 781 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 782 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 783 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 787 | { |
| 788 | int ret; |
| 789 | mdp_super_t *tmp1, *tmp2; |
| 790 | |
| 791 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 792 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 793 | |
| 794 | if (!tmp1 || !tmp2) { |
| 795 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 796 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | goto abort; |
| 798 | } |
| 799 | |
| 800 | *tmp1 = *sb1; |
| 801 | *tmp2 = *sb2; |
| 802 | |
| 803 | /* |
| 804 | * nr_disks is not constant |
| 805 | */ |
| 806 | tmp1->nr_disks = 0; |
| 807 | tmp2->nr_disks = 0; |
| 808 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 809 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 811 | kfree(tmp1); |
| 812 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | return ret; |
| 814 | } |
| 815 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 816 | |
| 817 | static u32 md_csum_fold(u32 csum) |
| 818 | { |
| 819 | csum = (csum & 0xffff) + (csum >> 16); |
| 820 | return (csum & 0xffff) + (csum >> 16); |
| 821 | } |
| 822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | static unsigned int calc_sb_csum(mdp_super_t * sb) |
| 824 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 825 | u64 newcsum = 0; |
| 826 | u32 *sb32 = (u32*)sb; |
| 827 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | unsigned int disk_csum, csum; |
| 829 | |
| 830 | disk_csum = sb->sb_csum; |
| 831 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 832 | |
| 833 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 834 | newcsum += sb32[i]; |
| 835 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 836 | |
| 837 | |
| 838 | #ifdef CONFIG_ALPHA |
| 839 | /* This used to use csum_partial, which was wrong for several |
| 840 | * reasons including that different results are returned on |
| 841 | * different architectures. It isn't critical that we get exactly |
| 842 | * the same return value as before (we always csum_fold before |
| 843 | * testing, and that removes any differences). However as we |
| 844 | * know that csum_partial always returned a 16bit value on |
| 845 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 846 | */ |
| 847 | sb->sb_csum = md_csum_fold(disk_csum); |
| 848 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 850 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | return csum; |
| 852 | } |
| 853 | |
| 854 | |
| 855 | /* |
| 856 | * Handle superblock details. |
| 857 | * We want to be able to handle multiple superblock formats |
| 858 | * so we have a common interface to them all, and an array of |
| 859 | * different handlers. |
| 860 | * We rely on user-space to write the initial superblock, and support |
| 861 | * reading and updating of superblocks. |
| 862 | * Interface methods are: |
| 863 | * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version) |
| 864 | * loads and validates a superblock on dev. |
| 865 | * if refdev != NULL, compare superblocks on both devices |
| 866 | * Return: |
| 867 | * 0 - dev has a superblock that is compatible with refdev |
| 868 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 869 | * so dev should be used as the refdev in future |
| 870 | * -EINVAL superblock incompatible or invalid |
| 871 | * -othererror e.g. -EIO |
| 872 | * |
| 873 | * int validate_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 874 | * Verify that dev is acceptable into mddev. |
| 875 | * The first time, mddev->raid_disks will be 0, and data from |
| 876 | * dev should be merged in. Subsequent calls check that dev |
| 877 | * is new enough. Return 0 or -EINVAL |
| 878 | * |
| 879 | * void sync_super(mddev_t *mddev, mdk_rdev_t *dev) |
| 880 | * Update the superblock for rdev with data in mddev |
| 881 | * This does not write to disc. |
| 882 | * |
| 883 | */ |
| 884 | |
| 885 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 886 | char *name; |
| 887 | struct module *owner; |
| 888 | int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, |
| 889 | int minor_version); |
| 890 | int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 891 | void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev); |
| 892 | unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 893 | sector_t num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | }; |
| 895 | |
| 896 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 897 | * Check that the given mddev has no bitmap. |
| 898 | * |
| 899 | * This function is called from the run method of all personalities that do not |
| 900 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 901 | * has a bitmap. Otherwise, it returns 0. |
| 902 | * |
| 903 | */ |
| 904 | int md_check_no_bitmap(mddev_t *mddev) |
| 905 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 906 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 907 | return 0; |
| 908 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 909 | mdname(mddev), mddev->pers->name); |
| 910 | return 1; |
| 911 | } |
| 912 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 913 | |
| 914 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | * load_super for 0.90.0 |
| 916 | */ |
| 917 | static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 918 | { |
| 919 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 920 | mdp_super_t *sb; |
| 921 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | |
| 923 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 924 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | * it's at the end of the disk. |
| 926 | * |
| 927 | * It also happens to be a multiple of 4Kb. |
| 928 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 929 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 931 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | if (ret) return ret; |
| 933 | |
| 934 | ret = -EINVAL; |
| 935 | |
| 936 | bdevname(rdev->bdev, b); |
| 937 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 938 | |
| 939 | if (sb->md_magic != MD_SB_MAGIC) { |
| 940 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 941 | b); |
| 942 | goto abort; |
| 943 | } |
| 944 | |
| 945 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 946 | sb->minor_version < 90 || |
| 947 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 949 | sb->major_version, sb->minor_version, |
| 950 | b); |
| 951 | goto abort; |
| 952 | } |
| 953 | |
| 954 | if (sb->raid_disks <= 0) |
| 955 | goto abort; |
| 956 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 957 | 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] | 958 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 959 | b); |
| 960 | goto abort; |
| 961 | } |
| 962 | |
| 963 | rdev->preferred_minor = sb->md_minor; |
| 964 | rdev->data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 965 | rdev->sb_size = MD_SB_BYTES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | |
| 967 | if (sb->level == LEVEL_MULTIPATH) |
| 968 | rdev->desc_nr = -1; |
| 969 | else |
| 970 | rdev->desc_nr = sb->this_disk.number; |
| 971 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 972 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 974 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | __u64 ev1, ev2; |
| 976 | mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page); |
| 977 | if (!uuid_equal(refsb, sb)) { |
| 978 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 979 | b, bdevname(refdev->bdev,b2)); |
| 980 | goto abort; |
| 981 | } |
| 982 | if (!sb_equal(refsb, sb)) { |
| 983 | printk(KERN_WARNING "md: %s has same UUID" |
| 984 | " but different superblock to %s\n", |
| 985 | b, bdevname(refdev->bdev, b2)); |
| 986 | goto abort; |
| 987 | } |
| 988 | ev1 = md_event(sb); |
| 989 | ev2 = md_event(refsb); |
| 990 | if (ev1 > ev2) |
| 991 | ret = 1; |
| 992 | else |
| 993 | ret = 0; |
| 994 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 995 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 997 | if (rdev->sectors < sb->size * 2 && sb->level > 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 998 | /* "this cannot possibly happen" ... */ |
| 999 | ret = -EINVAL; |
| 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | abort: |
| 1002 | return ret; |
| 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * validate_super for 0.90.0 |
| 1007 | */ |
| 1008 | static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1009 | { |
| 1010 | mdp_disk_t *desc; |
| 1011 | mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1012 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1014 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1015 | clear_bit(Faulty, &rdev->flags); |
| 1016 | clear_bit(In_sync, &rdev->flags); |
| 1017 | clear_bit(WriteMostly, &rdev->flags); |
| 1018 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | if (mddev->raid_disks == 0) { |
| 1021 | mddev->major_version = 0; |
| 1022 | mddev->minor_version = sb->minor_version; |
| 1023 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1024 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1025 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | mddev->ctime = sb->ctime; |
| 1027 | mddev->utime = sb->utime; |
| 1028 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1029 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | mddev->layout = sb->layout; |
| 1031 | mddev->raid_disks = sb->raid_disks; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1032 | mddev->dev_sectors = sb->size * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1033 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1034 | mddev->bitmap_info.offset = 0; |
| 1035 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1037 | if (mddev->minor_version >= 91) { |
| 1038 | mddev->reshape_position = sb->reshape_position; |
| 1039 | mddev->delta_disks = sb->delta_disks; |
| 1040 | mddev->new_level = sb->new_level; |
| 1041 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1042 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1043 | } else { |
| 1044 | mddev->reshape_position = MaxSector; |
| 1045 | mddev->delta_disks = 0; |
| 1046 | mddev->new_level = mddev->level; |
| 1047 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1048 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1049 | } |
| 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1052 | mddev->recovery_cp = MaxSector; |
| 1053 | else { |
| 1054 | if (sb->events_hi == sb->cp_events_hi && |
| 1055 | sb->events_lo == sb->cp_events_lo) { |
| 1056 | mddev->recovery_cp = sb->recovery_cp; |
| 1057 | } else |
| 1058 | mddev->recovery_cp = 0; |
| 1059 | } |
| 1060 | |
| 1061 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1062 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1063 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1064 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1065 | |
| 1066 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1067 | |
| 1068 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1069 | mddev->bitmap_info.file == NULL) |
| 1070 | mddev->bitmap_info.offset = |
| 1071 | mddev->bitmap_info.default_offset; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1072 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1073 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1074 | /* Insist on good event counter while assembling, except |
| 1075 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1077 | if (sb->disks[rdev->desc_nr].state & ( |
| 1078 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
| 1079 | if (ev1 < mddev->events) |
| 1080 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1081 | } else if (mddev->bitmap) { |
| 1082 | /* if adding to array with a bitmap, then we can accept an |
| 1083 | * older device ... but not too old. |
| 1084 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1085 | if (ev1 < mddev->bitmap->events_cleared) |
| 1086 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1087 | } else { |
| 1088 | if (ev1 < mddev->events) |
| 1089 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1090 | return 0; |
| 1091 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | desc = sb->disks + rdev->desc_nr; |
| 1095 | |
| 1096 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1097 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1098 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1099 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1100 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1102 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1103 | /* active but not in sync implies recovery up to |
| 1104 | * reshape position. We don't know exactly where |
| 1105 | * that is, so set to zero for now */ |
| 1106 | if (mddev->minor_version >= 91) { |
| 1107 | rdev->recovery_offset = 0; |
| 1108 | rdev->raid_disk = desc->raid_disk; |
| 1109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1111 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1112 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1113 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1114 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | * sync_super for 0.90.0 |
| 1120 | */ |
| 1121 | static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1122 | { |
| 1123 | mdp_super_t *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | mdk_rdev_t *rdev2; |
| 1125 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
| 1128 | /* make rdev->sb match mddev data.. |
| 1129 | * |
| 1130 | * 1/ zero out disks |
| 1131 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1132 | * 3/ any empty disks < next_spare become removed |
| 1133 | * |
| 1134 | * disks[0] gets initialised to REMOVED because |
| 1135 | * we cannot be sure from other fields if it has |
| 1136 | * been initialised or not. |
| 1137 | */ |
| 1138 | int i; |
| 1139 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1140 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1141 | rdev->sb_size = MD_SB_BYTES; |
| 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | sb = (mdp_super_t*)page_address(rdev->sb_page); |
| 1144 | |
| 1145 | memset(sb, 0, sizeof(*sb)); |
| 1146 | |
| 1147 | sb->md_magic = MD_SB_MAGIC; |
| 1148 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | sb->patch_version = mddev->patch_version; |
| 1150 | sb->gvalid_words = 0; /* ignored */ |
| 1151 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1152 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1153 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1154 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1155 | |
| 1156 | sb->ctime = mddev->ctime; |
| 1157 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1158 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | sb->raid_disks = mddev->raid_disks; |
| 1160 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1161 | sb->not_persistent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | sb->utime = mddev->utime; |
| 1163 | sb->state = 0; |
| 1164 | sb->events_hi = (mddev->events>>32); |
| 1165 | sb->events_lo = (u32)mddev->events; |
| 1166 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1167 | if (mddev->reshape_position == MaxSector) |
| 1168 | sb->minor_version = 90; |
| 1169 | else { |
| 1170 | sb->minor_version = 91; |
| 1171 | sb->reshape_position = mddev->reshape_position; |
| 1172 | sb->new_level = mddev->new_level; |
| 1173 | sb->delta_disks = mddev->delta_disks; |
| 1174 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1175 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1176 | } |
| 1177 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | if (mddev->in_sync) |
| 1179 | { |
| 1180 | sb->recovery_cp = mddev->recovery_cp; |
| 1181 | sb->cp_events_hi = (mddev->events>>32); |
| 1182 | sb->cp_events_lo = (u32)mddev->events; |
| 1183 | if (mddev->recovery_cp == MaxSector) |
| 1184 | sb->state = (1<< MD_SB_CLEAN); |
| 1185 | } else |
| 1186 | sb->recovery_cp = 0; |
| 1187 | |
| 1188 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1189 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1191 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1192 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1195 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1197 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1198 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1199 | |
| 1200 | if (rdev2->raid_disk >= 0 && |
| 1201 | sb->minor_version >= 91) |
| 1202 | /* we have nowhere to store the recovery_offset, |
| 1203 | * but if it is not below the reshape_position, |
| 1204 | * we can piggy-back on that. |
| 1205 | */ |
| 1206 | is_active = 1; |
| 1207 | if (rdev2->raid_disk < 0 || |
| 1208 | test_bit(Faulty, &rdev2->flags)) |
| 1209 | is_active = 0; |
| 1210 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1211 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1213 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1214 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | d = &sb->disks[rdev2->desc_nr]; |
| 1216 | nr_disks++; |
| 1217 | d->number = rdev2->desc_nr; |
| 1218 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1219 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1220 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | d->raid_disk = rdev2->raid_disk; |
| 1222 | else |
| 1223 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1224 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1226 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1228 | if (test_bit(In_sync, &rdev2->flags)) |
| 1229 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | active++; |
| 1231 | working++; |
| 1232 | } else { |
| 1233 | d->state = 0; |
| 1234 | spare++; |
| 1235 | working++; |
| 1236 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1237 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1238 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1241 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1242 | mdp_disk_t *d = &sb->disks[i]; |
| 1243 | if (d->state == 0 && d->number == 0) { |
| 1244 | d->number = i; |
| 1245 | d->raid_disk = i; |
| 1246 | d->state = (1<<MD_DISK_REMOVED); |
| 1247 | d->state |= (1<<MD_DISK_FAULTY); |
| 1248 | failed++; |
| 1249 | } |
| 1250 | } |
| 1251 | sb->nr_disks = nr_disks; |
| 1252 | sb->active_disks = active; |
| 1253 | sb->working_disks = working; |
| 1254 | sb->failed_disks = failed; |
| 1255 | sb->spare_disks = spare; |
| 1256 | |
| 1257 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1258 | sb->sb_csum = calc_sb_csum(sb); |
| 1259 | } |
| 1260 | |
| 1261 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1262 | * rdev_size_change for 0.90.0 |
| 1263 | */ |
| 1264 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1265 | 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] | 1266 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1267 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1268 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1269 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1270 | return 0; /* can't move bitmap */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1271 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1272 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1273 | num_sectors = rdev->sb_start; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1274 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1275 | rdev->sb_page); |
| 1276 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1277 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | |
| 1281 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | * version 1 superblock |
| 1283 | */ |
| 1284 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1285 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1287 | __le32 disk_csum; |
| 1288 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | unsigned long long newcsum; |
| 1290 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1291 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | int i; |
| 1293 | |
| 1294 | disk_csum = sb->sb_csum; |
| 1295 | sb->sb_csum = 0; |
| 1296 | newcsum = 0; |
| 1297 | for (i=0; size>=4; size -= 4 ) |
| 1298 | newcsum += le32_to_cpu(*isuper++); |
| 1299 | |
| 1300 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1301 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | |
| 1303 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1304 | sb->sb_csum = disk_csum; |
| 1305 | return cpu_to_le32(csum); |
| 1306 | } |
| 1307 | |
| 1308 | static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version) |
| 1309 | { |
| 1310 | struct mdp_superblock_1 *sb; |
| 1311 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1312 | sector_t sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1314 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | |
| 1316 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1317 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | * It is always aligned to a 4K boundary and |
| 1319 | * depeding on minor_version, it can be: |
| 1320 | * 0: At least 8K, but less than 12K, from end of device |
| 1321 | * 1: At start of device |
| 1322 | * 2: 4K from start of device. |
| 1323 | */ |
| 1324 | switch(minor_version) { |
| 1325 | case 0: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1326 | sb_start = rdev->bdev->bd_inode->i_size >> 9; |
| 1327 | sb_start -= 8*2; |
| 1328 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | break; |
| 1330 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1331 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | break; |
| 1333 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1334 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | break; |
| 1336 | default: |
| 1337 | return -EINVAL; |
| 1338 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1339 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1341 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1342 | * and it is safe to read 4k, so we do that |
| 1343 | */ |
| 1344 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | if (ret) return ret; |
| 1346 | |
| 1347 | |
| 1348 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1349 | |
| 1350 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1351 | sb->major_version != cpu_to_le32(1) || |
| 1352 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1353 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1354 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | return -EINVAL; |
| 1356 | |
| 1357 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1358 | printk("md: invalid superblock checksum on %s\n", |
| 1359 | bdevname(rdev->bdev,b)); |
| 1360 | return -EINVAL; |
| 1361 | } |
| 1362 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1363 | printk("md: data_size too small on %s\n", |
| 1364 | bdevname(rdev->bdev,b)); |
| 1365 | return -EINVAL; |
| 1366 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | rdev->preferred_minor = 0xffff; |
| 1369 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1370 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1372 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1373 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1374 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1375 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1376 | |
| 1377 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1378 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1379 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1380 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1381 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1382 | rdev->desc_nr = -1; |
| 1383 | else |
| 1384 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1385 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1386 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1387 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1388 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | __u64 ev1, ev2; |
| 1390 | struct mdp_superblock_1 *refsb = |
| 1391 | (struct mdp_superblock_1*)page_address(refdev->sb_page); |
| 1392 | |
| 1393 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1394 | sb->level != refsb->level || |
| 1395 | sb->layout != refsb->layout || |
| 1396 | sb->chunksize != refsb->chunksize) { |
| 1397 | printk(KERN_WARNING "md: %s has strangely different" |
| 1398 | " superblock to %s\n", |
| 1399 | bdevname(rdev->bdev,b), |
| 1400 | bdevname(refdev->bdev,b2)); |
| 1401 | return -EINVAL; |
| 1402 | } |
| 1403 | ev1 = le64_to_cpu(sb->events); |
| 1404 | ev2 = le64_to_cpu(refsb->events); |
| 1405 | |
| 1406 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1407 | ret = 1; |
| 1408 | else |
| 1409 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | } |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1411 | if (minor_version) |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1412 | rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 1413 | le64_to_cpu(sb->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1415 | rdev->sectors = rdev->sb_start; |
| 1416 | if (rdev->sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1418 | rdev->sectors = le64_to_cpu(sb->data_size); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1419 | if (le64_to_cpu(sb->size) > rdev->sectors) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1420 | return -EINVAL; |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1421 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1425 | { |
| 1426 | 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] | 1427 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1429 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1430 | clear_bit(Faulty, &rdev->flags); |
| 1431 | clear_bit(In_sync, &rdev->flags); |
| 1432 | clear_bit(WriteMostly, &rdev->flags); |
| 1433 | clear_bit(BarriersNotsupp, &rdev->flags); |
| 1434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | if (mddev->raid_disks == 0) { |
| 1436 | mddev->major_version = 1; |
| 1437 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1438 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1439 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1); |
| 1441 | mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1); |
| 1442 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1443 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | mddev->layout = le32_to_cpu(sb->layout); |
| 1445 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1446 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1447 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1448 | mddev->bitmap_info.offset = 0; |
| 1449 | mddev->bitmap_info.default_offset = 1024 >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | |
| 1451 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1452 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1453 | |
| 1454 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1455 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1456 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1457 | mddev->bitmap_info.file == NULL ) |
| 1458 | mddev->bitmap_info.offset = |
| 1459 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1460 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1461 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1462 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1463 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1464 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1465 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1466 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1467 | } else { |
| 1468 | mddev->reshape_position = MaxSector; |
| 1469 | mddev->delta_disks = 0; |
| 1470 | mddev->new_level = mddev->level; |
| 1471 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1472 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1473 | } |
| 1474 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1475 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1476 | /* Insist of good event counter while assembling, except for |
| 1477 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1479 | if (rdev->desc_nr >= 0 && |
| 1480 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1481 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe) |
| 1482 | if (ev1 < mddev->events) |
| 1483 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1484 | } else if (mddev->bitmap) { |
| 1485 | /* If adding to array with a bitmap, then we can accept an |
| 1486 | * older device, but not too old. |
| 1487 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1488 | if (ev1 < mddev->bitmap->events_cleared) |
| 1489 | return 0; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1490 | } else { |
| 1491 | if (ev1 < mddev->events) |
| 1492 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1493 | return 0; |
| 1494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1496 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1497 | if (rdev->desc_nr < 0 || |
| 1498 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
| 1499 | role = 0xffff; |
| 1500 | rdev->desc_nr = -1; |
| 1501 | } else |
| 1502 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | switch(role) { |
| 1504 | case 0xffff: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | break; |
| 1506 | case 0xfffe: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1507 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | break; |
| 1509 | default: |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1510 | if ((le32_to_cpu(sb->feature_map) & |
| 1511 | MD_FEATURE_RECOVERY_OFFSET)) |
| 1512 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 1513 | else |
| 1514 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | rdev->raid_disk = role; |
| 1516 | break; |
| 1517 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1518 | if (sb->devflags & WriteMostly1) |
| 1519 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1520 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1521 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1527 | { |
| 1528 | struct mdp_superblock_1 *sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | mdk_rdev_t *rdev2; |
| 1530 | int max_dev, i; |
| 1531 | /* make rdev->sb match mddev and rdev data. */ |
| 1532 | |
| 1533 | sb = (struct mdp_superblock_1*)page_address(rdev->sb_page); |
| 1534 | |
| 1535 | sb->feature_map = 0; |
| 1536 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1537 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | memset(sb->pad1, 0, sizeof(sb->pad1)); |
| 1539 | memset(sb->pad2, 0, sizeof(sb->pad2)); |
| 1540 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1541 | |
| 1542 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1543 | sb->events = cpu_to_le64(mddev->events); |
| 1544 | if (mddev->in_sync) |
| 1545 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
| 1546 | else |
| 1547 | sb->resync_offset = cpu_to_le64(0); |
| 1548 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1549 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1550 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1551 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1552 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1553 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1554 | sb->level = cpu_to_le32(mddev->level); |
| 1555 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1556 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1557 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1558 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1559 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1560 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1561 | |
| 1562 | if (rdev->raid_disk >= 0 && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1563 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1564 | sb->feature_map |= |
| 1565 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1566 | sb->recovery_offset = |
| 1567 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1570 | if (mddev->reshape_position != MaxSector) { |
| 1571 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1572 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1573 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1574 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1575 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1576 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1577 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | max_dev = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1580 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | if (rdev2->desc_nr+1 > max_dev) |
| 1582 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1583 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1584 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1585 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1586 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1587 | rdev->sb_size = max_dev * 2 + 256; |
| 1588 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1589 | if (rdev->sb_size & bmask) |
| 1590 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1591 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | for (i=0; i<max_dev;i++) |
| 1593 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
| 1594 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1595 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1597 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | sb->dev_roles[i] = cpu_to_le16(0xfffe); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1599 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1601 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1602 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | else |
| 1604 | sb->dev_roles[i] = cpu_to_le16(0xffff); |
| 1605 | } |
| 1606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1608 | } |
| 1609 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1610 | static unsigned long long |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1611 | 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] | 1612 | { |
| 1613 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1614 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1615 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1616 | return 0; /* component must fit device */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1617 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1618 | /* minor versions 1 and 2; superblock before data */ |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1619 | max_sectors = rdev->bdev->bd_inode->i_size >> 9; |
| 1620 | max_sectors -= rdev->data_offset; |
| 1621 | if (!num_sectors || num_sectors > max_sectors) |
| 1622 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1623 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1624 | /* minor version 0 with bitmap we can't move */ |
| 1625 | return 0; |
| 1626 | } else { |
| 1627 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1628 | sector_t sb_start; |
| 1629 | sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2; |
| 1630 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1631 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1632 | if (!num_sectors || num_sectors > max_sectors) |
| 1633 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1634 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1635 | } |
| 1636 | sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1637 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1638 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1639 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1640 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1641 | rdev->sb_page); |
| 1642 | md_super_wait(rdev->mddev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1643 | return num_sectors / 2; /* kB for sysfs */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1646 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | [0] = { |
| 1648 | .name = "0.90.0", |
| 1649 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1650 | .load_super = super_90_load, |
| 1651 | .validate_super = super_90_validate, |
| 1652 | .sync_super = super_90_sync, |
| 1653 | .rdev_size_change = super_90_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | }, |
| 1655 | [1] = { |
| 1656 | .name = "md-1", |
| 1657 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1658 | .load_super = super_1_load, |
| 1659 | .validate_super = super_1_validate, |
| 1660 | .sync_super = super_1_sync, |
| 1661 | .rdev_size_change = super_1_rdev_size_change, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | }, |
| 1663 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | |
| 1665 | static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2) |
| 1666 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1667 | mdk_rdev_t *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1669 | rcu_read_lock(); |
| 1670 | rdev_for_each_rcu(rdev, mddev1) |
| 1671 | rdev_for_each_rcu(rdev2, mddev2) |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1672 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1673 | rdev2->bdev->bd_contains) { |
| 1674 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1675 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1676 | } |
| 1677 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | return 0; |
| 1679 | } |
| 1680 | |
| 1681 | static LIST_HEAD(pending_raid_disks); |
| 1682 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1683 | /* |
| 1684 | * Try to register data integrity profile for an mddev |
| 1685 | * |
| 1686 | * This is called when an array is started and after a disk has been kicked |
| 1687 | * from the array. It only succeeds if all working and active component devices |
| 1688 | * are integrity capable with matching profiles. |
| 1689 | */ |
| 1690 | int md_integrity_register(mddev_t *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1691 | { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1692 | mdk_rdev_t *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1693 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1694 | if (list_empty(&mddev->disks)) |
| 1695 | return 0; /* nothing to do */ |
| 1696 | if (blk_get_integrity(mddev->gendisk)) |
| 1697 | return 0; /* already registered */ |
| 1698 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 1699 | /* skip spares and non-functional disks */ |
| 1700 | if (test_bit(Faulty, &rdev->flags)) |
| 1701 | continue; |
| 1702 | if (rdev->raid_disk < 0) |
| 1703 | continue; |
| 1704 | /* |
| 1705 | * If at least one rdev is not integrity capable, we can not |
| 1706 | * enable data integrity for the md device. |
| 1707 | */ |
| 1708 | if (!bdev_get_integrity(rdev->bdev)) |
| 1709 | return -EINVAL; |
| 1710 | if (!reference) { |
| 1711 | /* Use the first rdev as the reference */ |
| 1712 | reference = rdev; |
| 1713 | continue; |
| 1714 | } |
| 1715 | /* does this rdev's profile match the reference profile? */ |
| 1716 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1717 | rdev->bdev->bd_disk) < 0) |
| 1718 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1719 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1720 | /* |
| 1721 | * All component devices are integrity capable and have matching |
| 1722 | * profiles, register the common profile for the md device. |
| 1723 | */ |
| 1724 | if (blk_integrity_register(mddev->gendisk, |
| 1725 | bdev_get_integrity(reference->bdev)) != 0) { |
| 1726 | printk(KERN_ERR "md: failed to register integrity for %s\n", |
| 1727 | mdname(mddev)); |
| 1728 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1729 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1730 | printk(KERN_NOTICE "md: data integrity on %s enabled\n", |
| 1731 | mdname(mddev)); |
| 1732 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1733 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1734 | EXPORT_SYMBOL(md_integrity_register); |
| 1735 | |
| 1736 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
| 1737 | void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 1738 | { |
| 1739 | struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev); |
| 1740 | struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk); |
| 1741 | |
| 1742 | if (!bi_mddev) /* nothing to do */ |
| 1743 | return; |
| 1744 | if (rdev->raid_disk < 0) /* skip spares */ |
| 1745 | return; |
| 1746 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 1747 | rdev->bdev->bd_disk) >= 0) |
| 1748 | return; |
| 1749 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 1750 | blk_integrity_unregister(mddev->gendisk); |
| 1751 | } |
| 1752 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev) |
| 1755 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1756 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 1757 | struct kobject *ko; |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1758 | char *s; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1759 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | |
| 1761 | if (rdev->mddev) { |
| 1762 | MD_BUG(); |
| 1763 | return -EINVAL; |
| 1764 | } |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1765 | |
| 1766 | /* prevent duplicates */ |
| 1767 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 1768 | return -EEXIST; |
| 1769 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1770 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
| 1771 | if (rdev->sectors && (mddev->dev_sectors == 0 || |
| 1772 | rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1773 | if (mddev->pers) { |
| 1774 | /* Cannot change size, so fail |
| 1775 | * If mddev->level <= 0, then we don't care |
| 1776 | * about aligning sizes (e.g. linear) |
| 1777 | */ |
| 1778 | if (mddev->level > 0) |
| 1779 | return -ENOSPC; |
| 1780 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1781 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | |
| 1784 | /* Verify rdev->desc_nr is unique. |
| 1785 | * If it is -1, assign a free number, else |
| 1786 | * check number is not in use |
| 1787 | */ |
| 1788 | if (rdev->desc_nr < 0) { |
| 1789 | int choice = 0; |
| 1790 | if (mddev->pers) choice = mddev->raid_disks; |
| 1791 | while (find_rdev_nr(mddev, choice)) |
| 1792 | choice++; |
| 1793 | rdev->desc_nr = choice; |
| 1794 | } else { |
| 1795 | if (find_rdev_nr(mddev, rdev->desc_nr)) |
| 1796 | return -EBUSY; |
| 1797 | } |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 1798 | if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
| 1799 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 1800 | mdname(mddev), mddev->max_disks); |
| 1801 | return -EBUSY; |
| 1802 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1803 | bdevname(rdev->bdev,b); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1804 | while ( (s=strchr(b, '/')) != NULL) |
Neil Brown | 1edf80d | 2006-01-12 01:05:23 -0800 | [diff] [blame] | 1805 | *s = '!'; |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1808 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1809 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 1810 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1811 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1812 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 1813 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1814 | if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) { |
| 1815 | kobject_del(&rdev->kobj); |
| 1816 | goto fail; |
| 1817 | } |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 1818 | rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, NULL, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1819 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1820 | list_add_rcu(&rdev->same_set, &mddev->disks); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1821 | bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 1822 | |
| 1823 | /* May as well allow recovery to be retried once */ |
| 1824 | mddev->recovery_disabled = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 1827 | |
| 1828 | fail: |
| 1829 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 1830 | b, mdname(mddev)); |
| 1831 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1834 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1835 | { |
| 1836 | mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work); |
| 1837 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1838 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | static void unbind_rdev_from_array(mdk_rdev_t * rdev) |
| 1842 | { |
| 1843 | char b[BDEVNAME_SIZE]; |
| 1844 | if (!rdev->mddev) { |
| 1845 | MD_BUG(); |
| 1846 | return; |
| 1847 | } |
Jun'ichi Nomura | 5463c79 | 2006-03-27 01:17:58 -0800 | [diff] [blame] | 1848 | bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1849 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 1851 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1852 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 1853 | sysfs_put(rdev->sysfs_state); |
| 1854 | rdev->sysfs_state = NULL; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1855 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1856 | * writing to 'remove' to "dev/state". We also need |
| 1857 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1858 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1859 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 1860 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 1861 | kobject_get(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 1862 | schedule_work(&rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * prevent the device from being mounted, repartitioned or |
| 1867 | * otherwise reused by a RAID array (or any other kernel |
| 1868 | * subsystem), by bd_claiming the device. |
| 1869 | */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1870 | 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] | 1871 | { |
| 1872 | int err = 0; |
| 1873 | struct block_device *bdev; |
| 1874 | char b[BDEVNAME_SIZE]; |
| 1875 | |
Peter Zijlstra | 2e7b651 | 2006-12-08 02:36:13 -0800 | [diff] [blame] | 1876 | bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | if (IS_ERR(bdev)) { |
| 1878 | printk(KERN_ERR "md: could not open %s.\n", |
| 1879 | __bdevname(dev, b)); |
| 1880 | return PTR_ERR(bdev); |
| 1881 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1882 | err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | if (err) { |
| 1884 | printk(KERN_ERR "md: could not bd_claim %s.\n", |
| 1885 | bdevname(bdev, b)); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1886 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | return err; |
| 1888 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1889 | if (!shared) |
| 1890 | set_bit(AllReserved, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | rdev->bdev = bdev; |
| 1892 | return err; |
| 1893 | } |
| 1894 | |
| 1895 | static void unlock_rdev(mdk_rdev_t *rdev) |
| 1896 | { |
| 1897 | struct block_device *bdev = rdev->bdev; |
| 1898 | rdev->bdev = NULL; |
| 1899 | if (!bdev) |
| 1900 | MD_BUG(); |
| 1901 | bd_release(bdev); |
Al Viro | 9a1c354 | 2008-02-22 20:40:24 -0500 | [diff] [blame] | 1902 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | void md_autodetect_dev(dev_t dev); |
| 1906 | |
| 1907 | static void export_rdev(mdk_rdev_t * rdev) |
| 1908 | { |
| 1909 | char b[BDEVNAME_SIZE]; |
| 1910 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 1911 | bdevname(rdev->bdev,b)); |
| 1912 | if (rdev->mddev) |
| 1913 | MD_BUG(); |
| 1914 | free_disk_sb(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1916 | if (test_bit(AutoDetected, &rdev->flags)) |
| 1917 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | #endif |
| 1919 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1920 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | static void kick_rdev_from_array(mdk_rdev_t * rdev) |
| 1924 | { |
| 1925 | unbind_rdev_from_array(rdev); |
| 1926 | export_rdev(rdev); |
| 1927 | } |
| 1928 | |
| 1929 | static void export_array(mddev_t *mddev) |
| 1930 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1931 | mdk_rdev_t *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 1933 | rdev_for_each(rdev, tmp, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | if (!rdev->mddev) { |
| 1935 | MD_BUG(); |
| 1936 | continue; |
| 1937 | } |
| 1938 | kick_rdev_from_array(rdev); |
| 1939 | } |
| 1940 | if (!list_empty(&mddev->disks)) |
| 1941 | MD_BUG(); |
| 1942 | mddev->raid_disks = 0; |
| 1943 | mddev->major_version = 0; |
| 1944 | } |
| 1945 | |
| 1946 | static void print_desc(mdp_disk_t *desc) |
| 1947 | { |
| 1948 | printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number, |
| 1949 | desc->major,desc->minor,desc->raid_disk,desc->state); |
| 1950 | } |
| 1951 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1952 | static void print_sb_90(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | { |
| 1954 | int i; |
| 1955 | |
| 1956 | printk(KERN_INFO |
| 1957 | "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n", |
| 1958 | sb->major_version, sb->minor_version, sb->patch_version, |
| 1959 | sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3, |
| 1960 | sb->ctime); |
| 1961 | printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n", |
| 1962 | sb->level, sb->size, sb->nr_disks, sb->raid_disks, |
| 1963 | sb->md_minor, sb->layout, sb->chunk_size); |
| 1964 | printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d" |
| 1965 | " FD:%d SD:%d CSUM:%08x E:%08lx\n", |
| 1966 | sb->utime, sb->state, sb->active_disks, sb->working_disks, |
| 1967 | sb->failed_disks, sb->spare_disks, |
| 1968 | sb->sb_csum, (unsigned long)sb->events_lo); |
| 1969 | |
| 1970 | printk(KERN_INFO); |
| 1971 | for (i = 0; i < MD_SB_DISKS; i++) { |
| 1972 | mdp_disk_t *desc; |
| 1973 | |
| 1974 | desc = sb->disks + i; |
| 1975 | if (desc->number || desc->major || desc->minor || |
| 1976 | desc->raid_disk || (desc->state && (desc->state != 4))) { |
| 1977 | printk(" D %2d: ", i); |
| 1978 | print_desc(desc); |
| 1979 | } |
| 1980 | } |
| 1981 | printk(KERN_INFO "md: THIS: "); |
| 1982 | print_desc(&sb->this_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1985 | static void print_sb_1(struct mdp_superblock_1 *sb) |
| 1986 | { |
| 1987 | __u8 *uuid; |
| 1988 | |
| 1989 | uuid = sb->set_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1990 | printk(KERN_INFO |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1991 | "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 1992 | "md: Name: \"%s\" CT:%llu\n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1993 | le32_to_cpu(sb->major_version), |
| 1994 | le32_to_cpu(sb->feature_map), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 1995 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 1996 | sb->set_name, |
| 1997 | (unsigned long long)le64_to_cpu(sb->ctime) |
| 1998 | & MD_SUPERBLOCK_1_TIME_SEC_MASK); |
| 1999 | |
| 2000 | uuid = sb->device_uuid; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2001 | printk(KERN_INFO |
| 2002 | "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] | 2003 | " RO:%llu\n" |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2004 | "md: Dev:%08x UUID: %pU\n" |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2005 | "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n" |
| 2006 | "md: (MaxDev:%u) \n", |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2007 | le32_to_cpu(sb->level), |
| 2008 | (unsigned long long)le64_to_cpu(sb->size), |
| 2009 | le32_to_cpu(sb->raid_disks), |
| 2010 | le32_to_cpu(sb->layout), |
| 2011 | le32_to_cpu(sb->chunksize), |
| 2012 | (unsigned long long)le64_to_cpu(sb->data_offset), |
| 2013 | (unsigned long long)le64_to_cpu(sb->data_size), |
| 2014 | (unsigned long long)le64_to_cpu(sb->super_offset), |
| 2015 | (unsigned long long)le64_to_cpu(sb->recovery_offset), |
| 2016 | le32_to_cpu(sb->dev_number), |
Joe Perches | 7b75c2f | 2009-12-14 18:01:12 -0800 | [diff] [blame] | 2017 | uuid, |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2018 | sb->devflags, |
| 2019 | (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK, |
| 2020 | (unsigned long long)le64_to_cpu(sb->events), |
| 2021 | (unsigned long long)le64_to_cpu(sb->resync_offset), |
| 2022 | le32_to_cpu(sb->sb_csum), |
| 2023 | le32_to_cpu(sb->max_dev) |
| 2024 | ); |
| 2025 | } |
| 2026 | |
| 2027 | static void print_rdev(mdk_rdev_t *rdev, int major_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | { |
| 2029 | char b[BDEVNAME_SIZE]; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2030 | printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n", |
| 2031 | bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors, |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2032 | test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags), |
| 2033 | rdev->desc_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | if (rdev->sb_loaded) { |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2035 | printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version); |
| 2036 | switch (major_version) { |
| 2037 | case 0: |
| 2038 | print_sb_90((mdp_super_t*)page_address(rdev->sb_page)); |
| 2039 | break; |
| 2040 | case 1: |
| 2041 | print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page)); |
| 2042 | break; |
| 2043 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2044 | } else |
| 2045 | printk(KERN_INFO "md: no rdev superblock!\n"); |
| 2046 | } |
| 2047 | |
Adrian Bunk | 5e56341 | 2006-06-26 00:27:42 -0700 | [diff] [blame] | 2048 | static void md_print_devices(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2050 | struct list_head *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | mdk_rdev_t *rdev; |
| 2052 | mddev_t *mddev; |
| 2053 | char b[BDEVNAME_SIZE]; |
| 2054 | |
| 2055 | printk("\n"); |
| 2056 | printk("md: **********************************\n"); |
| 2057 | printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n"); |
| 2058 | printk("md: **********************************\n"); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2059 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 2061 | if (mddev->bitmap) |
| 2062 | bitmap_print_sb(mddev->bitmap); |
| 2063 | else |
| 2064 | printk("%s: ", mdname(mddev)); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2065 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 2067 | printk("\n"); |
| 2068 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2069 | list_for_each_entry(rdev, &mddev->disks, same_set) |
Cheng Renquan | cd2ac93 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2070 | print_rdev(rdev, mddev->major_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | } |
| 2072 | printk("md: **********************************\n"); |
| 2073 | printk("\n"); |
| 2074 | } |
| 2075 | |
| 2076 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2077 | static void sync_sbs(mddev_t * mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2079 | /* Update each superblock (in-memory image), but |
| 2080 | * if we are allowed to, skip spares which already |
| 2081 | * have the right event counter, or have one earlier |
| 2082 | * (which would mean they aren't being marked as dirty |
| 2083 | * with the rest of the array) |
| 2084 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | |
NeilBrown | 5e86510 | 2009-11-12 12:08:04 +1100 | [diff] [blame] | 2087 | /* First make sure individual recovery_offsets are correct */ |
| 2088 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 2089 | if (rdev->raid_disk >= 0 && |
| 2090 | !test_bit(In_sync, &rdev->flags) && |
| 2091 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2092 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2093 | |
| 2094 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2095 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2096 | if (rdev->sb_events == mddev->events || |
| 2097 | (nospares && |
| 2098 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2099 | rdev->sb_events+1 == mddev->events)) { |
| 2100 | /* Don't update this superblock */ |
| 2101 | rdev->sb_loaded = 2; |
| 2102 | } else { |
| 2103 | super_types[mddev->major_version]. |
| 2104 | sync_super(mddev, rdev); |
| 2105 | rdev->sb_loaded = 1; |
| 2106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | } |
| 2108 | } |
| 2109 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2110 | static void md_update_sb(mddev_t * mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | mdk_rdev_t *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2113 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2114 | int nospares = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | |
NeilBrown | 1b57f13 | 2009-06-18 08:48:19 +1000 | [diff] [blame] | 2116 | mddev->utime = get_seconds(); |
NeilBrown | 8377bc8 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2117 | if (mddev->external) |
| 2118 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | repeat: |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2120 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2121 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2122 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2123 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2124 | force_change = 1; |
| 2125 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2126 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2127 | * though if events isn't the right even/odd, we will have to do |
| 2128 | * spares after all |
| 2129 | */ |
| 2130 | nospares = 1; |
| 2131 | if (force_change) |
| 2132 | nospares = 0; |
| 2133 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2134 | /* If the array is degraded, then skipping spares is both |
| 2135 | * dangerous and fairly pointless. |
| 2136 | * Dangerous because a device that was removed from the array |
| 2137 | * might have a event_count that still looks up-to-date, |
| 2138 | * so it can be re-added without a resync. |
| 2139 | * Pointless because if there are any spares to skip, |
| 2140 | * then a recovery will happen and soon that array won't |
| 2141 | * be degraded any more and the spare can go back to sleep then. |
| 2142 | */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2143 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2144 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2145 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2146 | |
| 2147 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2148 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2149 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2150 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2151 | && mddev->can_decrease_events |
| 2152 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2153 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2154 | mddev->can_decrease_events = 0; |
| 2155 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2156 | /* otherwise we have to go forward and ... */ |
| 2157 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2158 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | |
| 2161 | if (!mddev->events) { |
| 2162 | /* |
| 2163 | * oops, this 64-bit counter should never wrap. |
| 2164 | * Either we are in around ~1 trillion A.C., assuming |
| 2165 | * 1 reboot per second, or we have a bug: |
| 2166 | */ |
| 2167 | MD_BUG(); |
| 2168 | mddev->events --; |
| 2169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | |
| 2171 | /* |
| 2172 | * do not write anything to disk if using |
| 2173 | * nonpersistent superblocks |
| 2174 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2175 | if (!mddev->persistent) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2176 | if (!mddev->external) |
| 2177 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2178 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2179 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2180 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2182 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2183 | sync_sbs(mddev, nospares); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2184 | spin_unlock_irq(&mddev->write_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | |
| 2186 | dprintk(KERN_INFO |
| 2187 | "md: updating %s RAID superblock on device (in sync %d)\n", |
| 2188 | mdname(mddev),mddev->in_sync); |
| 2189 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2190 | bitmap_update_sb(mddev->bitmap); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2191 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | char b[BDEVNAME_SIZE]; |
| 2193 | dprintk(KERN_INFO "md: "); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2194 | if (rdev->sb_loaded != 1) |
| 2195 | continue; /* no noise on spare devices */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2196 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | dprintk("(skipping faulty "); |
| 2198 | |
| 2199 | dprintk("%s ", bdevname(rdev->bdev,b)); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2200 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2201 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2202 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2203 | rdev->sb_page); |
| 2204 | dprintk(KERN_INFO "(write) %s's sb offset: %llu\n", |
| 2205 | bdevname(rdev->bdev,b), |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2206 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2207 | rdev->sb_events = mddev->events; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | } else |
| 2210 | dprintk(")\n"); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2211 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | /* only need to write one superblock... */ |
| 2213 | break; |
| 2214 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2215 | md_super_wait(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2216 | /* 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] | 2217 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2218 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2219 | if (mddev->in_sync != sync_req || |
| 2220 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2221 | /* have to write it out again */ |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2222 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2223 | goto repeat; |
| 2224 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2225 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2226 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2227 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2228 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2229 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | } |
| 2232 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2233 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2234 | * We want to accept with case. For this we use cmd_match. |
| 2235 | */ |
| 2236 | static int cmd_match(const char *cmd, const char *str) |
| 2237 | { |
| 2238 | /* See if cmd, written into a sysfs file, matches |
| 2239 | * str. They must either be the same, or cmd can |
| 2240 | * have a trailing newline |
| 2241 | */ |
| 2242 | while (*cmd && *str && *cmd == *str) { |
| 2243 | cmd++; |
| 2244 | str++; |
| 2245 | } |
| 2246 | if (*cmd == '\n') |
| 2247 | cmd++; |
| 2248 | if (*str || *cmd) |
| 2249 | return 0; |
| 2250 | return 1; |
| 2251 | } |
| 2252 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2253 | struct rdev_sysfs_entry { |
| 2254 | struct attribute attr; |
| 2255 | ssize_t (*show)(mdk_rdev_t *, char *); |
| 2256 | ssize_t (*store)(mdk_rdev_t *, const char *, size_t); |
| 2257 | }; |
| 2258 | |
| 2259 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2260 | state_show(mdk_rdev_t *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2261 | { |
| 2262 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2263 | size_t len = 0; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2264 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2265 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2266 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2267 | sep = ","; |
| 2268 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2269 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2270 | len += sprintf(page+len, "%sin_sync",sep); |
| 2271 | sep = ","; |
| 2272 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2273 | if (test_bit(WriteMostly, &rdev->flags)) { |
| 2274 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2275 | sep = ","; |
| 2276 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2277 | if (test_bit(Blocked, &rdev->flags)) { |
| 2278 | len += sprintf(page+len, "%sblocked", sep); |
| 2279 | sep = ","; |
| 2280 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2281 | if (!test_bit(Faulty, &rdev->flags) && |
| 2282 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2283 | len += sprintf(page+len, "%sspare", sep); |
| 2284 | sep = ","; |
| 2285 | } |
| 2286 | return len+sprintf(page+len, "\n"); |
| 2287 | } |
| 2288 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2289 | static ssize_t |
| 2290 | state_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2291 | { |
| 2292 | /* can write |
| 2293 | * faulty - simulates and error |
| 2294 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2295 | * writemostly - sets write_mostly |
| 2296 | * -writemostly - clears write_mostly |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2297 | * blocked - sets the Blocked flag |
| 2298 | * -blocked - clears the Blocked flag |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2299 | * insync - sets Insync providing device isn't active |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2300 | */ |
| 2301 | int err = -EINVAL; |
| 2302 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2303 | md_error(rdev->mddev, rdev); |
| 2304 | err = 0; |
| 2305 | } else if (cmd_match(buf, "remove")) { |
| 2306 | if (rdev->raid_disk >= 0) |
| 2307 | err = -EBUSY; |
| 2308 | else { |
| 2309 | mddev_t *mddev = rdev->mddev; |
| 2310 | kick_rdev_from_array(rdev); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2311 | if (mddev->pers) |
| 2312 | md_update_sb(mddev, 1); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2313 | md_new_event(mddev); |
| 2314 | err = 0; |
| 2315 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2316 | } else if (cmd_match(buf, "writemostly")) { |
| 2317 | set_bit(WriteMostly, &rdev->flags); |
| 2318 | err = 0; |
| 2319 | } else if (cmd_match(buf, "-writemostly")) { |
| 2320 | clear_bit(WriteMostly, &rdev->flags); |
| 2321 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2322 | } else if (cmd_match(buf, "blocked")) { |
| 2323 | set_bit(Blocked, &rdev->flags); |
| 2324 | err = 0; |
| 2325 | } else if (cmd_match(buf, "-blocked")) { |
| 2326 | clear_bit(Blocked, &rdev->flags); |
| 2327 | wake_up(&rdev->blocked_wait); |
| 2328 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2329 | md_wakeup_thread(rdev->mddev->thread); |
| 2330 | |
| 2331 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2332 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2333 | set_bit(In_sync, &rdev->flags); |
| 2334 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2335 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2336 | if (!err && rdev->sysfs_state) |
| 2337 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2338 | return err ? err : len; |
| 2339 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2340 | static struct rdev_sysfs_entry rdev_state = |
| 2341 | __ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2342 | |
| 2343 | static ssize_t |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2344 | errors_show(mdk_rdev_t *rdev, char *page) |
| 2345 | { |
| 2346 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2347 | } |
| 2348 | |
| 2349 | static ssize_t |
| 2350 | errors_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2351 | { |
| 2352 | char *e; |
| 2353 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 2354 | if (*buf && (*e == 0 || *e == '\n')) { |
| 2355 | atomic_set(&rdev->corrected_errors, n); |
| 2356 | return len; |
| 2357 | } |
| 2358 | return -EINVAL; |
| 2359 | } |
| 2360 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2361 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2362 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2363 | static ssize_t |
| 2364 | slot_show(mdk_rdev_t *rdev, char *page) |
| 2365 | { |
| 2366 | if (rdev->raid_disk < 0) |
| 2367 | return sprintf(page, "none\n"); |
| 2368 | else |
| 2369 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2370 | } |
| 2371 | |
| 2372 | static ssize_t |
| 2373 | slot_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2374 | { |
| 2375 | char *e; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2376 | int err; |
| 2377 | char nm[20]; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2378 | int slot = simple_strtoul(buf, &e, 10); |
| 2379 | if (strncmp(buf, "none", 4)==0) |
| 2380 | slot = -1; |
| 2381 | else if (e==buf || (*e && *e!= '\n')) |
| 2382 | return -EINVAL; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2383 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2384 | /* Setting 'slot' on an active array requires also |
| 2385 | * updating the 'rd%d' link, and communicating |
| 2386 | * with the personality with ->hot_*_disk. |
| 2387 | * For now we only support removing |
| 2388 | * failed/spare devices. This normally happens automatically, |
| 2389 | * but not when the metadata is externally managed. |
| 2390 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2391 | if (rdev->raid_disk == -1) |
| 2392 | return -EEXIST; |
| 2393 | /* personality does all needed checks */ |
| 2394 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2395 | return -EINVAL; |
| 2396 | err = rdev->mddev->pers-> |
| 2397 | hot_remove_disk(rdev->mddev, rdev->raid_disk); |
| 2398 | if (err) |
| 2399 | return err; |
| 2400 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2401 | sysfs_remove_link(&rdev->mddev->kobj, nm); |
Maciej Trela | b710310 | 2010-04-14 16:58:16 +1000 | [diff] [blame] | 2402 | rdev->raid_disk = -1; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2403 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2404 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2405 | } else if (rdev->mddev->pers) { |
| 2406 | mdk_rdev_t *rdev2; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2407 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2408 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2409 | */ |
| 2410 | |
| 2411 | if (rdev->raid_disk != -1) |
| 2412 | return -EBUSY; |
| 2413 | |
| 2414 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2415 | return -EINVAL; |
| 2416 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2417 | list_for_each_entry(rdev2, &rdev->mddev->disks, same_set) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2418 | if (rdev2->raid_disk == slot) |
| 2419 | return -EEXIST; |
| 2420 | |
| 2421 | rdev->raid_disk = slot; |
| 2422 | if (test_bit(In_sync, &rdev->flags)) |
| 2423 | rdev->saved_raid_disk = slot; |
| 2424 | else |
| 2425 | rdev->saved_raid_disk = -1; |
| 2426 | err = rdev->mddev->pers-> |
| 2427 | hot_add_disk(rdev->mddev, rdev); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 2428 | if (err) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2429 | rdev->raid_disk = -1; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2430 | return err; |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 2431 | } else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2432 | sysfs_notify_dirent(rdev->sysfs_state); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2433 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 2434 | if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm)) |
| 2435 | printk(KERN_WARNING |
| 2436 | "md: cannot register " |
| 2437 | "%s for %s\n", |
| 2438 | nm, mdname(rdev->mddev)); |
| 2439 | |
| 2440 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2441 | } else { |
| 2442 | if (slot >= rdev->mddev->raid_disks) |
| 2443 | return -ENOSPC; |
| 2444 | rdev->raid_disk = slot; |
| 2445 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2446 | clear_bit(Faulty, &rdev->flags); |
| 2447 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2448 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2449 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2450 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2451 | return len; |
| 2452 | } |
| 2453 | |
| 2454 | |
| 2455 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2456 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2457 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2458 | static ssize_t |
| 2459 | offset_show(mdk_rdev_t *rdev, char *page) |
| 2460 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2461 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | static ssize_t |
| 2465 | offset_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2466 | { |
| 2467 | char *e; |
| 2468 | unsigned long long offset = simple_strtoull(buf, &e, 10); |
| 2469 | if (e==buf || (*e && *e != '\n')) |
| 2470 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2471 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2472 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2473 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2474 | /* Must set offset before size, so overlap checks |
| 2475 | * can be sane */ |
| 2476 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2477 | rdev->data_offset = offset; |
| 2478 | return len; |
| 2479 | } |
| 2480 | |
| 2481 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2482 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2483 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2484 | static ssize_t |
| 2485 | rdev_size_show(mdk_rdev_t *rdev, char *page) |
| 2486 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2487 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2488 | } |
| 2489 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2490 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2491 | { |
| 2492 | /* check if two start/length pairs overlap */ |
| 2493 | if (s1+l1 <= s2) |
| 2494 | return 0; |
| 2495 | if (s2+l2 <= s1) |
| 2496 | return 0; |
| 2497 | return 1; |
| 2498 | } |
| 2499 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2500 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2501 | { |
| 2502 | unsigned long long blocks; |
| 2503 | sector_t new; |
| 2504 | |
| 2505 | if (strict_strtoull(buf, 10, &blocks) < 0) |
| 2506 | return -EINVAL; |
| 2507 | |
| 2508 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2509 | return -EINVAL; /* sector conversion overflow */ |
| 2510 | |
| 2511 | new = blocks * 2; |
| 2512 | if (new != blocks * 2) |
| 2513 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2514 | |
| 2515 | *sectors = new; |
| 2516 | return 0; |
| 2517 | } |
| 2518 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2519 | static ssize_t |
| 2520 | rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2521 | { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2522 | mddev_t *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2523 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2524 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2525 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2526 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2527 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2528 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2529 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2530 | sectors = super_types[my_mddev->major_version]. |
| 2531 | rdev_size_change(rdev, sectors); |
| 2532 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2533 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2534 | } else if (!sectors) |
| 2535 | sectors = (rdev->bdev->bd_inode->i_size >> 9) - |
| 2536 | rdev->data_offset; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2537 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2538 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2539 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2540 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2541 | rdev->sectors = sectors; |
| 2542 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2543 | /* need to check that all other rdevs with the same ->bdev |
| 2544 | * do not overlap. We need to unlock the mddev to avoid |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2545 | * a deadlock. We have already changed rdev->sectors, and if |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2546 | * we have to change it back, we will have the lock again. |
| 2547 | */ |
| 2548 | mddev_t *mddev; |
| 2549 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2550 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2551 | |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2552 | mddev_unlock(my_mddev); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2553 | for_each_mddev(mddev, tmp) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2554 | mdk_rdev_t *rdev2; |
| 2555 | |
| 2556 | mddev_lock(mddev); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2557 | list_for_each_entry(rdev2, &mddev->disks, same_set) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2558 | if (test_bit(AllReserved, &rdev2->flags) || |
| 2559 | (rdev->bdev == rdev2->bdev && |
| 2560 | rdev != rdev2 && |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2561 | overlaps(rdev->data_offset, rdev->sectors, |
Andre Noll | d07bd3b | 2008-07-21 14:42:07 +1000 | [diff] [blame] | 2562 | rdev2->data_offset, |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2563 | rdev2->sectors))) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2564 | overlap = 1; |
| 2565 | break; |
| 2566 | } |
| 2567 | mddev_unlock(mddev); |
| 2568 | if (overlap) { |
| 2569 | mddev_put(mddev); |
| 2570 | break; |
| 2571 | } |
| 2572 | } |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2573 | mddev_lock(my_mddev); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2574 | if (overlap) { |
| 2575 | /* Someone else could have slipped in a size |
| 2576 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2577 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2578 | * safe, and trust userspace not to race with |
| 2579 | * itself |
| 2580 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2581 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2582 | return -EBUSY; |
| 2583 | } |
| 2584 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2585 | return len; |
| 2586 | } |
| 2587 | |
| 2588 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2589 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2590 | |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2591 | |
| 2592 | static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page) |
| 2593 | { |
| 2594 | unsigned long long recovery_start = rdev->recovery_offset; |
| 2595 | |
| 2596 | if (test_bit(In_sync, &rdev->flags) || |
| 2597 | recovery_start == MaxSector) |
| 2598 | return sprintf(page, "none\n"); |
| 2599 | |
| 2600 | return sprintf(page, "%llu\n", recovery_start); |
| 2601 | } |
| 2602 | |
| 2603 | static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len) |
| 2604 | { |
| 2605 | unsigned long long recovery_start; |
| 2606 | |
| 2607 | if (cmd_match(buf, "none")) |
| 2608 | recovery_start = MaxSector; |
| 2609 | else if (strict_strtoull(buf, 10, &recovery_start)) |
| 2610 | return -EINVAL; |
| 2611 | |
| 2612 | if (rdev->mddev->pers && |
| 2613 | rdev->raid_disk >= 0) |
| 2614 | return -EBUSY; |
| 2615 | |
| 2616 | rdev->recovery_offset = recovery_start; |
| 2617 | if (recovery_start == MaxSector) |
| 2618 | set_bit(In_sync, &rdev->flags); |
| 2619 | else |
| 2620 | clear_bit(In_sync, &rdev->flags); |
| 2621 | return len; |
| 2622 | } |
| 2623 | |
| 2624 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 2625 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 2626 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2627 | static struct attribute *rdev_default_attrs[] = { |
| 2628 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2629 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2630 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2631 | &rdev_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2632 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 2633 | &rdev_recovery_start.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2634 | NULL, |
| 2635 | }; |
| 2636 | static ssize_t |
| 2637 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 2638 | { |
| 2639 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2640 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2641 | mddev_t *mddev = rdev->mddev; |
| 2642 | ssize_t rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2643 | |
| 2644 | if (!entry->show) |
| 2645 | return -EIO; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2646 | |
| 2647 | rv = mddev ? mddev_lock(mddev) : -EBUSY; |
| 2648 | if (!rv) { |
| 2649 | if (rdev->mddev == NULL) |
| 2650 | rv = -EBUSY; |
| 2651 | else |
| 2652 | rv = entry->show(rdev, page); |
| 2653 | mddev_unlock(mddev); |
| 2654 | } |
| 2655 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | static ssize_t |
| 2659 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 2660 | const char *page, size_t length) |
| 2661 | { |
| 2662 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
| 2663 | mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2664 | ssize_t rv; |
| 2665 | mddev_t *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2666 | |
| 2667 | if (!entry->store) |
| 2668 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 2669 | if (!capable(CAP_SYS_ADMIN)) |
| 2670 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2671 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2672 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2673 | if (rdev->mddev == NULL) |
| 2674 | rv = -EBUSY; |
| 2675 | else |
| 2676 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 2677 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 2678 | } |
| 2679 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2680 | } |
| 2681 | |
| 2682 | static void rdev_free(struct kobject *ko) |
| 2683 | { |
| 2684 | mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj); |
| 2685 | kfree(rdev); |
| 2686 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 2687 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2688 | .show = rdev_attr_show, |
| 2689 | .store = rdev_attr_store, |
| 2690 | }; |
| 2691 | static struct kobj_type rdev_ktype = { |
| 2692 | .release = rdev_free, |
| 2693 | .sysfs_ops = &rdev_sysfs_ops, |
| 2694 | .default_attrs = rdev_default_attrs, |
| 2695 | }; |
| 2696 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | /* |
| 2698 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 2699 | * |
| 2700 | * mark the device faulty if: |
| 2701 | * |
| 2702 | * - the device is nonexistent (zero size) |
| 2703 | * - the device has no valid superblock |
| 2704 | * |
| 2705 | * a faulty rdev _never_ has rdev->sb set. |
| 2706 | */ |
| 2707 | static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor) |
| 2708 | { |
| 2709 | char b[BDEVNAME_SIZE]; |
| 2710 | int err; |
| 2711 | mdk_rdev_t *rdev; |
| 2712 | sector_t size; |
| 2713 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 2714 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | if (!rdev) { |
| 2716 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 2717 | return ERR_PTR(-ENOMEM); |
| 2718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | |
| 2720 | if ((err = alloc_disk_sb(rdev))) |
| 2721 | goto abort_free; |
| 2722 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2723 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2724 | if (err) |
| 2725 | goto abort_free; |
| 2726 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2727 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | rdev->desc_nr = -1; |
NeilBrown | 2b6e845 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 2730 | rdev->saved_raid_disk = -1; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 2731 | rdev->raid_disk = -1; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2732 | rdev->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | rdev->data_offset = 0; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2734 | rdev->sb_events = 0; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2735 | rdev->last_read_error.tv_sec = 0; |
| 2736 | rdev->last_read_error.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | atomic_set(&rdev->nr_pending, 0); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2738 | atomic_set(&rdev->read_errors, 0); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2739 | atomic_set(&rdev->corrected_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | |
| 2741 | size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS; |
| 2742 | if (!size) { |
| 2743 | printk(KERN_WARNING |
| 2744 | "md: %s has zero or unknown size, marking faulty!\n", |
| 2745 | bdevname(rdev->bdev,b)); |
| 2746 | err = -EINVAL; |
| 2747 | goto abort_free; |
| 2748 | } |
| 2749 | |
| 2750 | if (super_format >= 0) { |
| 2751 | err = super_types[super_format]. |
| 2752 | load_super(rdev, NULL, super_minor); |
| 2753 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 2754 | printk(KERN_WARNING |
| 2755 | "md: %s does not have a valid v%d.%d " |
| 2756 | "superblock, not importing!\n", |
| 2757 | bdevname(rdev->bdev,b), |
| 2758 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | goto abort_free; |
| 2760 | } |
| 2761 | if (err < 0) { |
| 2762 | printk(KERN_WARNING |
| 2763 | "md: could not read %s's sb, not importing!\n", |
| 2764 | bdevname(rdev->bdev,b)); |
| 2765 | goto abort_free; |
| 2766 | } |
| 2767 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | INIT_LIST_HEAD(&rdev->same_set); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2770 | init_waitqueue_head(&rdev->blocked_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | |
| 2772 | return rdev; |
| 2773 | |
| 2774 | abort_free: |
| 2775 | if (rdev->sb_page) { |
| 2776 | if (rdev->bdev) |
| 2777 | unlock_rdev(rdev); |
| 2778 | free_disk_sb(rdev); |
| 2779 | } |
| 2780 | kfree(rdev); |
| 2781 | return ERR_PTR(err); |
| 2782 | } |
| 2783 | |
| 2784 | /* |
| 2785 | * Check a full RAID array for plausibility |
| 2786 | */ |
| 2787 | |
| 2788 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 2789 | static void analyze_sbs(mddev_t * mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | { |
| 2791 | int i; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2792 | mdk_rdev_t *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | char b[BDEVNAME_SIZE]; |
| 2794 | |
| 2795 | freshest = NULL; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2796 | rdev_for_each(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | switch (super_types[mddev->major_version]. |
| 2798 | load_super(rdev, freshest, mddev->minor_version)) { |
| 2799 | case 1: |
| 2800 | freshest = rdev; |
| 2801 | break; |
| 2802 | case 0: |
| 2803 | break; |
| 2804 | default: |
| 2805 | printk( KERN_ERR \ |
| 2806 | "md: fatal superblock inconsistency in %s" |
| 2807 | " -- removing from array\n", |
| 2808 | bdevname(rdev->bdev,b)); |
| 2809 | kick_rdev_from_array(rdev); |
| 2810 | } |
| 2811 | |
| 2812 | |
| 2813 | super_types[mddev->major_version]. |
| 2814 | validate_super(mddev, freshest); |
| 2815 | |
| 2816 | i = 0; |
NeilBrown | d089c6a | 2008-02-06 01:39:59 -0800 | [diff] [blame] | 2817 | rdev_for_each(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 2818 | if (mddev->max_disks && |
| 2819 | (rdev->desc_nr >= mddev->max_disks || |
| 2820 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2821 | printk(KERN_WARNING |
| 2822 | "md: %s: %s: only %d devices permitted\n", |
| 2823 | mdname(mddev), bdevname(rdev->bdev, b), |
| 2824 | mddev->max_disks); |
| 2825 | kick_rdev_from_array(rdev); |
| 2826 | continue; |
| 2827 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | if (rdev != freshest) |
| 2829 | if (super_types[mddev->major_version]. |
| 2830 | validate_super(mddev, rdev)) { |
| 2831 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 2832 | " from array!\n", |
| 2833 | bdevname(rdev->bdev,b)); |
| 2834 | kick_rdev_from_array(rdev); |
| 2835 | continue; |
| 2836 | } |
| 2837 | if (mddev->level == LEVEL_MULTIPATH) { |
| 2838 | rdev->desc_nr = i++; |
| 2839 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2840 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2841 | } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2842 | rdev->raid_disk = -1; |
| 2843 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | } |
| 2845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2848 | /* Read a fixed-point number. |
| 2849 | * Numbers in sysfs attributes should be in "standard" units where |
| 2850 | * possible, so time should be in seconds. |
| 2851 | * However we internally use a a much smaller unit such as |
| 2852 | * milliseconds or jiffies. |
| 2853 | * This function takes a decimal number with a possible fractional |
| 2854 | * component, and produces an integer which is the result of |
| 2855 | * multiplying that number by 10^'scale'. |
| 2856 | * all without any floating-point arithmetic. |
| 2857 | */ |
| 2858 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 2859 | { |
| 2860 | unsigned long result = 0; |
| 2861 | long decimals = -1; |
| 2862 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 2863 | if (*cp == '.') |
| 2864 | decimals = 0; |
| 2865 | else if (decimals < scale) { |
| 2866 | unsigned int value; |
| 2867 | value = *cp - '0'; |
| 2868 | result = result * 10 + value; |
| 2869 | if (decimals >= 0) |
| 2870 | decimals++; |
| 2871 | } |
| 2872 | cp++; |
| 2873 | } |
| 2874 | if (*cp == '\n') |
| 2875 | cp++; |
| 2876 | if (*cp) |
| 2877 | return -EINVAL; |
| 2878 | if (decimals < 0) |
| 2879 | decimals = 0; |
| 2880 | while (decimals < scale) { |
| 2881 | result *= 10; |
| 2882 | decimals ++; |
| 2883 | } |
| 2884 | *res = result; |
| 2885 | return 0; |
| 2886 | } |
| 2887 | |
| 2888 | |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2889 | static void md_safemode_timeout(unsigned long data); |
| 2890 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2891 | static ssize_t |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2892 | safe_delay_show(mddev_t *mddev, char *page) |
| 2893 | { |
| 2894 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 2895 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 2896 | } |
| 2897 | static ssize_t |
| 2898 | safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len) |
| 2899 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2900 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 2901 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 2902 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2903 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2904 | if (msec == 0) |
| 2905 | mddev->safemode_delay = 0; |
| 2906 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2907 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2908 | mddev->safemode_delay = (msec*HZ)/1000; |
| 2909 | if (mddev->safemode_delay == 0) |
| 2910 | mddev->safemode_delay = 1; |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 2911 | if (mddev->safemode_delay < old_delay) |
| 2912 | md_safemode_timeout((unsigned long)mddev); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 2913 | } |
| 2914 | return len; |
| 2915 | } |
| 2916 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2917 | __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] | 2918 | |
| 2919 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 2920 | level_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2921 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2922 | struct mdk_personality *p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2923 | if (p) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2924 | return sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2925 | else if (mddev->clevel[0]) |
| 2926 | return sprintf(page, "%s\n", mddev->clevel); |
| 2927 | else if (mddev->level != LEVEL_NONE) |
| 2928 | return sprintf(page, "%d\n", mddev->level); |
| 2929 | else |
| 2930 | return 0; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 2931 | } |
| 2932 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2933 | static ssize_t |
| 2934 | level_store(mddev_t *mddev, const char *buf, size_t len) |
| 2935 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2936 | char clevel[16]; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2937 | ssize_t rv = len; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2938 | struct mdk_personality *pers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2939 | long level; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 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 */ |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2972 | if (len == 0 || len >= sizeof(clevel)) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2973 | return -EINVAL; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2974 | strncpy(clevel, buf, len); |
| 2975 | if (clevel[len-1] == '\n') |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2976 | len--; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2977 | clevel[len] = 0; |
| 2978 | if (strict_strtol(clevel, 10, &level)) |
| 2979 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2980 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2981 | if (request_module("md-%s", clevel) != 0) |
| 2982 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2983 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2984 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2985 | if (!pers || !try_module_get(pers->owner)) { |
| 2986 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 2987 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2988 | return -EINVAL; |
| 2989 | } |
| 2990 | spin_unlock(&pers_lock); |
| 2991 | |
| 2992 | if (pers == mddev->pers) { |
| 2993 | /* Nothing to do! */ |
| 2994 | module_put(pers->owner); |
| 2995 | return rv; |
| 2996 | } |
| 2997 | if (!pers->takeover) { |
| 2998 | module_put(pers->owner); |
| 2999 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3000 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3001 | return -EINVAL; |
| 3002 | } |
| 3003 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame^] | 3004 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 3005 | rdev->new_raid_disk = rdev->raid_disk; |
| 3006 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3007 | /* ->takeover must set new_* and/or delta_disks |
| 3008 | * if it succeeds, and may set them when it fails. |
| 3009 | */ |
| 3010 | priv = pers->takeover(mddev); |
| 3011 | if (IS_ERR(priv)) { |
| 3012 | mddev->new_level = mddev->level; |
| 3013 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3014 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3015 | mddev->raid_disks -= mddev->delta_disks; |
| 3016 | mddev->delta_disks = 0; |
| 3017 | module_put(pers->owner); |
| 3018 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3019 | mdname(mddev), clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3020 | return PTR_ERR(priv); |
| 3021 | } |
| 3022 | |
| 3023 | /* Looks like we have a winner */ |
| 3024 | mddev_suspend(mddev); |
| 3025 | mddev->pers->stop(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3026 | |
| 3027 | if (mddev->pers->sync_request == NULL && |
| 3028 | pers->sync_request != NULL) { |
| 3029 | /* need to add the md_redundancy_group */ |
| 3030 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3031 | printk(KERN_WARNING |
| 3032 | "md: cannot register extra attributes for %s\n", |
| 3033 | mdname(mddev)); |
NeilBrown | 19fdb9e | 2010-05-22 08:31:36 +1000 | [diff] [blame] | 3034 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 3035 | } |
| 3036 | if (mddev->pers->sync_request != NULL && |
| 3037 | pers->sync_request == NULL) { |
| 3038 | /* need to remove the md_redundancy_group */ |
| 3039 | if (mddev->to_remove == NULL) |
| 3040 | mddev->to_remove = &md_redundancy_group; |
| 3041 | } |
| 3042 | |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3043 | if (mddev->pers->sync_request == NULL && |
| 3044 | mddev->external) { |
| 3045 | /* We are converting from a no-redundancy array |
| 3046 | * to a redundancy array and metadata is managed |
| 3047 | * externally so we need to be sure that writes |
| 3048 | * won't block due to a need to transition |
| 3049 | * clean->dirty |
| 3050 | * until external management is started. |
| 3051 | */ |
| 3052 | mddev->in_sync = 0; |
| 3053 | mddev->safemode_delay = 0; |
| 3054 | mddev->safemode = 0; |
| 3055 | } |
| 3056 | |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame^] | 3057 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3058 | char nm[20]; |
| 3059 | if (rdev->raid_disk < 0) |
| 3060 | continue; |
| 3061 | if (rdev->new_raid_disk > mddev->raid_disks) |
| 3062 | rdev->new_raid_disk = -1; |
| 3063 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3064 | continue; |
| 3065 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3066 | sysfs_remove_link(&mddev->kobj, nm); |
| 3067 | } |
| 3068 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
| 3069 | if (rdev->raid_disk < 0) |
| 3070 | continue; |
| 3071 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3072 | continue; |
| 3073 | rdev->raid_disk = rdev->new_raid_disk; |
| 3074 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3075 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame^] | 3076 | else { |
| 3077 | char nm[20]; |
| 3078 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 3079 | if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 3080 | printk("md: cannot register %s for %s after level change\n", |
| 3081 | nm, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3082 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame^] | 3083 | } |
| 3084 | |
| 3085 | module_put(mddev->pers->owner); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3086 | mddev->pers = pers; |
| 3087 | mddev->private = priv; |
| 3088 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3089 | mddev->level = mddev->new_level; |
| 3090 | mddev->layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3091 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3092 | mddev->delta_disks = 0; |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3093 | if (mddev->pers->sync_request == NULL) { |
| 3094 | /* this is now an array without redundancy, so |
| 3095 | * it must always be in_sync |
| 3096 | */ |
| 3097 | mddev->in_sync = 1; |
| 3098 | del_timer_sync(&mddev->safemode_timer); |
| 3099 | } |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3100 | pers->run(mddev); |
| 3101 | mddev_resume(mddev); |
| 3102 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 3103 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3104 | md_wakeup_thread(mddev->thread); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3105 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3106 | md_new_event(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3107 | return rv; |
| 3108 | } |
| 3109 | |
| 3110 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3111 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3112 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3113 | |
| 3114 | static ssize_t |
| 3115 | layout_show(mddev_t *mddev, char *page) |
| 3116 | { |
| 3117 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3118 | if (mddev->reshape_position != MaxSector && |
| 3119 | mddev->layout != mddev->new_layout) |
| 3120 | return sprintf(page, "%d (%d)\n", |
| 3121 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3122 | return sprintf(page, "%d\n", mddev->layout); |
| 3123 | } |
| 3124 | |
| 3125 | static ssize_t |
| 3126 | layout_store(mddev_t *mddev, const char *buf, size_t len) |
| 3127 | { |
| 3128 | char *e; |
| 3129 | unsigned long n = simple_strtoul(buf, &e, 10); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3130 | |
| 3131 | if (!*buf || (*e && *e != '\n')) |
| 3132 | return -EINVAL; |
| 3133 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3134 | if (mddev->pers) { |
| 3135 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3136 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3137 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3138 | mddev->new_layout = n; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3139 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3140 | if (err) { |
| 3141 | mddev->new_layout = mddev->layout; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3142 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3143 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3144 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3145 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3146 | if (mddev->reshape_position == MaxSector) |
| 3147 | mddev->layout = n; |
| 3148 | } |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3149 | return len; |
| 3150 | } |
| 3151 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3152 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3153 | |
| 3154 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3155 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3156 | raid_disks_show(mddev_t *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3157 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3158 | if (mddev->raid_disks == 0) |
| 3159 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3160 | if (mddev->reshape_position != MaxSector && |
| 3161 | mddev->delta_disks != 0) |
| 3162 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3163 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3164 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3165 | } |
| 3166 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3167 | static int update_raid_disks(mddev_t *mddev, int raid_disks); |
| 3168 | |
| 3169 | static ssize_t |
| 3170 | raid_disks_store(mddev_t *mddev, const char *buf, size_t len) |
| 3171 | { |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3172 | char *e; |
| 3173 | int rv = 0; |
| 3174 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3175 | |
| 3176 | if (!*buf || (*e && *e != '\n')) |
| 3177 | return -EINVAL; |
| 3178 | |
| 3179 | if (mddev->pers) |
| 3180 | rv = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3181 | else if (mddev->reshape_position != MaxSector) { |
| 3182 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
| 3183 | mddev->delta_disks = n - olddisks; |
| 3184 | mddev->raid_disks = n; |
| 3185 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3186 | mddev->raid_disks = n; |
| 3187 | return rv ? rv : len; |
| 3188 | } |
| 3189 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3190 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3191 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3192 | static ssize_t |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3193 | chunk_size_show(mddev_t *mddev, char *page) |
| 3194 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3195 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3196 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3197 | return sprintf(page, "%d (%d)\n", |
| 3198 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3199 | mddev->chunk_sectors << 9); |
| 3200 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | static ssize_t |
| 3204 | chunk_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3205 | { |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3206 | char *e; |
| 3207 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3208 | |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3209 | if (!*buf || (*e && *e != '\n')) |
| 3210 | return -EINVAL; |
| 3211 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3212 | if (mddev->pers) { |
| 3213 | int err; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3214 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3215 | return -EBUSY; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3216 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3217 | err = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3218 | if (err) { |
| 3219 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3220 | return err; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3221 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3222 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3223 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3224 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3225 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3226 | } |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3227 | return len; |
| 3228 | } |
| 3229 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3230 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3231 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3232 | static ssize_t |
| 3233 | resync_start_show(mddev_t *mddev, char *page) |
| 3234 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3235 | if (mddev->recovery_cp == MaxSector) |
| 3236 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3237 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3238 | } |
| 3239 | |
| 3240 | static ssize_t |
| 3241 | resync_start_store(mddev_t *mddev, const char *buf, size_t len) |
| 3242 | { |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3243 | char *e; |
| 3244 | unsigned long long n = simple_strtoull(buf, &e, 10); |
| 3245 | |
| 3246 | if (mddev->pers) |
| 3247 | return -EBUSY; |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3248 | if (cmd_match(buf, "none")) |
| 3249 | n = MaxSector; |
| 3250 | else if (!*buf || (*e && *e != '\n')) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3251 | return -EINVAL; |
| 3252 | |
| 3253 | mddev->recovery_cp = n; |
| 3254 | return len; |
| 3255 | } |
| 3256 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3257 | __ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3258 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3259 | /* |
| 3260 | * The array state can be: |
| 3261 | * |
| 3262 | * clear |
| 3263 | * No devices, no size, no level |
| 3264 | * Equivalent to STOP_ARRAY ioctl |
| 3265 | * inactive |
| 3266 | * May have some settings, but array is not active |
| 3267 | * all IO results in error |
| 3268 | * When written, doesn't tear down array, but just stops it |
| 3269 | * suspended (not supported yet) |
| 3270 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3271 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3272 | * readonly |
| 3273 | * no resync can happen. no superblocks get written. |
| 3274 | * write requests fail |
| 3275 | * read-auto |
| 3276 | * like readonly, but behaves like 'clean' on a write request. |
| 3277 | * |
| 3278 | * clean - no pending writes, but otherwise active. |
| 3279 | * When written to inactive array, starts without resync |
| 3280 | * If a write request arrives then |
| 3281 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3282 | * if not known, block and switch to write-pending |
| 3283 | * If written to an active array that has pending writes, then fails. |
| 3284 | * active |
| 3285 | * fully active: IO and resync can be happening. |
| 3286 | * When written to inactive array, starts with resync |
| 3287 | * |
| 3288 | * write-pending |
| 3289 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3290 | * |
| 3291 | * active-idle |
| 3292 | * like active, but no writes have been seen for a while (100msec). |
| 3293 | * |
| 3294 | */ |
| 3295 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3296 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3297 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3298 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3299 | "write-pending", "active-idle", NULL }; |
| 3300 | |
| 3301 | static int match_word(const char *word, char **list) |
| 3302 | { |
| 3303 | int n; |
| 3304 | for (n=0; list[n]; n++) |
| 3305 | if (cmd_match(word, list[n])) |
| 3306 | break; |
| 3307 | return n; |
| 3308 | } |
| 3309 | |
| 3310 | static ssize_t |
| 3311 | array_state_show(mddev_t *mddev, char *page) |
| 3312 | { |
| 3313 | enum array_state st = inactive; |
| 3314 | |
| 3315 | if (mddev->pers) |
| 3316 | switch(mddev->ro) { |
| 3317 | case 1: |
| 3318 | st = readonly; |
| 3319 | break; |
| 3320 | case 2: |
| 3321 | st = read_auto; |
| 3322 | break; |
| 3323 | case 0: |
| 3324 | if (mddev->in_sync) |
| 3325 | st = clean; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3326 | else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 3327 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3328 | else if (mddev->safemode) |
| 3329 | st = active_idle; |
| 3330 | else |
| 3331 | st = active; |
| 3332 | } |
| 3333 | else { |
| 3334 | if (list_empty(&mddev->disks) && |
| 3335 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3336 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3337 | st = clear; |
| 3338 | else |
| 3339 | st = inactive; |
| 3340 | } |
| 3341 | return sprintf(page, "%s\n", array_states[st]); |
| 3342 | } |
| 3343 | |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3344 | static int do_md_stop(mddev_t * mddev, int ro, int is_open); |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3345 | static int md_set_readonly(mddev_t * mddev, int is_open); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3346 | static int do_md_run(mddev_t * mddev); |
| 3347 | static int restart_array(mddev_t *mddev); |
| 3348 | |
| 3349 | static ssize_t |
| 3350 | array_state_store(mddev_t *mddev, const char *buf, size_t len) |
| 3351 | { |
| 3352 | int err = -EINVAL; |
| 3353 | enum array_state st = match_word(buf, array_states); |
| 3354 | switch(st) { |
| 3355 | case bad_word: |
| 3356 | break; |
| 3357 | case clear: |
| 3358 | /* stopping an active array */ |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3359 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3360 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3361 | err = do_md_stop(mddev, 0, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3362 | break; |
| 3363 | case inactive: |
| 3364 | /* stopping an active array */ |
| 3365 | if (mddev->pers) { |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 3366 | if (atomic_read(&mddev->openers) > 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3367 | return -EBUSY; |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3368 | err = do_md_stop(mddev, 2, 0); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3369 | } else |
| 3370 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3371 | break; |
| 3372 | case suspended: |
| 3373 | break; /* not supported yet */ |
| 3374 | case readonly: |
| 3375 | if (mddev->pers) |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3376 | err = md_set_readonly(mddev, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3377 | else { |
| 3378 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3379 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3380 | err = do_md_run(mddev); |
| 3381 | } |
| 3382 | break; |
| 3383 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3384 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3385 | if (mddev->ro == 0) |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 3386 | err = md_set_readonly(mddev, 0); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3387 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3388 | err = restart_array(mddev); |
| 3389 | if (err == 0) { |
| 3390 | mddev->ro = 2; |
| 3391 | set_disk_ro(mddev->gendisk, 0); |
| 3392 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3393 | } else { |
| 3394 | mddev->ro = 2; |
| 3395 | err = do_md_run(mddev); |
| 3396 | } |
| 3397 | break; |
| 3398 | case clean: |
| 3399 | if (mddev->pers) { |
| 3400 | restart_array(mddev); |
| 3401 | spin_lock_irq(&mddev->write_lock); |
| 3402 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3403 | if (mddev->in_sync == 0) { |
| 3404 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3405 | if (mddev->safemode == 1) |
| 3406 | mddev->safemode = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3407 | if (mddev->persistent) |
| 3408 | set_bit(MD_CHANGE_CLEAN, |
| 3409 | &mddev->flags); |
| 3410 | } |
| 3411 | err = 0; |
| 3412 | } else |
| 3413 | err = -EBUSY; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3414 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3415 | } else |
| 3416 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3417 | break; |
| 3418 | case active: |
| 3419 | if (mddev->pers) { |
| 3420 | restart_array(mddev); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3421 | if (mddev->external) |
| 3422 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3423 | wake_up(&mddev->sb_wait); |
| 3424 | err = 0; |
| 3425 | } else { |
| 3426 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3427 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3428 | err = do_md_run(mddev); |
| 3429 | } |
| 3430 | break; |
| 3431 | case write_pending: |
| 3432 | case active_idle: |
| 3433 | /* these cannot be set */ |
| 3434 | break; |
| 3435 | } |
| 3436 | if (err) |
| 3437 | return err; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3438 | else { |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 3439 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3440 | return len; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 3441 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3442 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3443 | static struct md_sysfs_entry md_array_state = |
| 3444 | __ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3445 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3446 | static ssize_t |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3447 | max_corrected_read_errors_show(mddev_t *mddev, char *page) { |
| 3448 | return sprintf(page, "%d\n", |
| 3449 | atomic_read(&mddev->max_corr_read_errors)); |
| 3450 | } |
| 3451 | |
| 3452 | static ssize_t |
| 3453 | max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len) |
| 3454 | { |
| 3455 | char *e; |
| 3456 | unsigned long n = simple_strtoul(buf, &e, 10); |
| 3457 | |
| 3458 | if (*buf && (*e == 0 || *e == '\n')) { |
| 3459 | atomic_set(&mddev->max_corr_read_errors, n); |
| 3460 | return len; |
| 3461 | } |
| 3462 | return -EINVAL; |
| 3463 | } |
| 3464 | |
| 3465 | static struct md_sysfs_entry max_corr_read_errors = |
| 3466 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 3467 | max_corrected_read_errors_store); |
| 3468 | |
| 3469 | static ssize_t |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3470 | null_show(mddev_t *mddev, char *page) |
| 3471 | { |
| 3472 | return -EINVAL; |
| 3473 | } |
| 3474 | |
| 3475 | static ssize_t |
| 3476 | new_dev_store(mddev_t *mddev, const char *buf, size_t len) |
| 3477 | { |
| 3478 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 3479 | /* The new device is added to the array. |
| 3480 | * If the array has a persistent superblock, we read the |
| 3481 | * superblock to initialise info and check validity. |
| 3482 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 3483 | * which mainly checks size. |
| 3484 | */ |
| 3485 | char *e; |
| 3486 | int major = simple_strtoul(buf, &e, 10); |
| 3487 | int minor; |
| 3488 | dev_t dev; |
| 3489 | mdk_rdev_t *rdev; |
| 3490 | int err; |
| 3491 | |
| 3492 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 3493 | return -EINVAL; |
| 3494 | minor = simple_strtoul(e+1, &e, 10); |
| 3495 | if (*e && *e != '\n') |
| 3496 | return -EINVAL; |
| 3497 | dev = MKDEV(major, minor); |
| 3498 | if (major != MAJOR(dev) || |
| 3499 | minor != MINOR(dev)) |
| 3500 | return -EOVERFLOW; |
| 3501 | |
| 3502 | |
| 3503 | if (mddev->persistent) { |
| 3504 | rdev = md_import_device(dev, mddev->major_version, |
| 3505 | mddev->minor_version); |
| 3506 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
| 3507 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 3508 | mdk_rdev_t, same_set); |
| 3509 | err = super_types[mddev->major_version] |
| 3510 | .load_super(rdev, rdev0, mddev->minor_version); |
| 3511 | if (err < 0) |
| 3512 | goto out; |
| 3513 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3514 | } else if (mddev->external) |
| 3515 | rdev = md_import_device(dev, -2, -1); |
| 3516 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 3517 | rdev = md_import_device(dev, -1, -1); |
| 3518 | |
| 3519 | if (IS_ERR(rdev)) |
| 3520 | return PTR_ERR(rdev); |
| 3521 | err = bind_rdev_to_array(rdev, mddev); |
| 3522 | out: |
| 3523 | if (err) |
| 3524 | export_rdev(rdev); |
| 3525 | return err ? err : len; |
| 3526 | } |
| 3527 | |
| 3528 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3529 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3530 | |
| 3531 | static ssize_t |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3532 | bitmap_store(mddev_t *mddev, const char *buf, size_t len) |
| 3533 | { |
| 3534 | char *end; |
| 3535 | unsigned long chunk, end_chunk; |
| 3536 | |
| 3537 | if (!mddev->bitmap) |
| 3538 | goto out; |
| 3539 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 3540 | while (*buf) { |
| 3541 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 3542 | if (buf == end) break; |
| 3543 | if (*end == '-') { /* range */ |
| 3544 | buf = end + 1; |
| 3545 | end_chunk = simple_strtoul(buf, &end, 0); |
| 3546 | if (buf == end) break; |
| 3547 | } |
| 3548 | if (*end && !isspace(*end)) break; |
| 3549 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 3550 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 3551 | } |
| 3552 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 3553 | out: |
| 3554 | return len; |
| 3555 | } |
| 3556 | |
| 3557 | static struct md_sysfs_entry md_bitmap = |
| 3558 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 3559 | |
| 3560 | static ssize_t |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3561 | size_show(mddev_t *mddev, char *page) |
| 3562 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3563 | return sprintf(page, "%llu\n", |
| 3564 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3565 | } |
| 3566 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 3567 | static int update_size(mddev_t *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3568 | |
| 3569 | static ssize_t |
| 3570 | size_store(mddev_t *mddev, const char *buf, size_t len) |
| 3571 | { |
| 3572 | /* If array is inactive, we can reduce the component size, but |
| 3573 | * not increase it (except from 0). |
| 3574 | * If array is active, we can try an on-line resize |
| 3575 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3576 | sector_t sectors; |
| 3577 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3578 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3579 | if (err < 0) |
| 3580 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3581 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3582 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 3583 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3584 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3585 | if (mddev->dev_sectors == 0 || |
| 3586 | mddev->dev_sectors > sectors) |
| 3587 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3588 | else |
| 3589 | err = -ENOSPC; |
| 3590 | } |
| 3591 | return err ? err : len; |
| 3592 | } |
| 3593 | |
| 3594 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3595 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3596 | |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3597 | |
| 3598 | /* Metdata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3599 | * This is one of |
| 3600 | * 'none' for arrays with no metadata (good luck...) |
| 3601 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3602 | * or N.M for internally known formats |
| 3603 | */ |
| 3604 | static ssize_t |
| 3605 | metadata_show(mddev_t *mddev, char *page) |
| 3606 | { |
| 3607 | if (mddev->persistent) |
| 3608 | return sprintf(page, "%d.%d\n", |
| 3609 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3610 | else if (mddev->external) |
| 3611 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3612 | else |
| 3613 | return sprintf(page, "none\n"); |
| 3614 | } |
| 3615 | |
| 3616 | static ssize_t |
| 3617 | metadata_store(mddev_t *mddev, const char *buf, size_t len) |
| 3618 | { |
| 3619 | int major, minor; |
| 3620 | char *e; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3621 | /* Changing the details of 'external' metadata is |
| 3622 | * always permitted. Otherwise there must be |
| 3623 | * no devices attached to the array. |
| 3624 | */ |
| 3625 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 3626 | ; |
| 3627 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3628 | return -EBUSY; |
| 3629 | |
| 3630 | if (cmd_match(buf, "none")) { |
| 3631 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3632 | mddev->external = 0; |
| 3633 | mddev->major_version = 0; |
| 3634 | mddev->minor_version = 90; |
| 3635 | return len; |
| 3636 | } |
| 3637 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 3638 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3639 | if (namelen >= sizeof(mddev->metadata_type)) |
| 3640 | namelen = sizeof(mddev->metadata_type)-1; |
| 3641 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 3642 | mddev->metadata_type[namelen] = 0; |
| 3643 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 3644 | mddev->metadata_type[--namelen] = 0; |
| 3645 | mddev->persistent = 0; |
| 3646 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3647 | mddev->major_version = 0; |
| 3648 | mddev->minor_version = 90; |
| 3649 | return len; |
| 3650 | } |
| 3651 | major = simple_strtoul(buf, &e, 10); |
| 3652 | if (e==buf || *e != '.') |
| 3653 | return -EINVAL; |
| 3654 | buf = e+1; |
| 3655 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 3656 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3657 | return -EINVAL; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 3658 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3659 | return -ENOENT; |
| 3660 | mddev->major_version = major; |
| 3661 | mddev->minor_version = minor; |
| 3662 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3663 | mddev->external = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3664 | return len; |
| 3665 | } |
| 3666 | |
| 3667 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3668 | __ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 3669 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 3670 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3671 | action_show(mddev_t *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3672 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3673 | char *type = "idle"; |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3674 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 3675 | type = "frozen"; |
| 3676 | else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 2b12ab6 | 2007-10-16 23:30:53 -0700 | [diff] [blame] | 3677 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) { |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3678 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3679 | type = "reshape"; |
| 3680 | else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3681 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 3682 | type = "resync"; |
| 3683 | else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 3684 | type = "check"; |
| 3685 | else |
| 3686 | type = "repair"; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3687 | } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3688 | type = "recover"; |
| 3689 | } |
| 3690 | return sprintf(page, "%s\n", type); |
| 3691 | } |
| 3692 | |
| 3693 | static ssize_t |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3694 | action_store(mddev_t *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3695 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3696 | if (!mddev->pers || !mddev->pers->sync_request) |
| 3697 | return -EINVAL; |
| 3698 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 3699 | if (cmd_match(page, "frozen")) |
| 3700 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3701 | else |
| 3702 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 3703 | |
| 3704 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3705 | if (mddev->sync_thread) { |
| 3706 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 3707 | md_unregister_thread(mddev->sync_thread); |
| 3708 | mddev->sync_thread = NULL; |
| 3709 | mddev->recovery = 0; |
| 3710 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3711 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 3712 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3713 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3714 | else if (cmd_match(page, "resync")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3715 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 3716 | else if (cmd_match(page, "recover")) { |
| 3717 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 3718 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3719 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3720 | int err; |
| 3721 | if (mddev->pers->start_reshape == NULL) |
| 3722 | return -EINVAL; |
| 3723 | err = mddev->pers->start_reshape(mddev); |
| 3724 | if (err) |
| 3725 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 3726 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 3727 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 3728 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3729 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 3730 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3731 | return -EINVAL; |
| 3732 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 3733 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 3734 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 3735 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3736 | md_wakeup_thread(mddev->thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 3737 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3738 | return len; |
| 3739 | } |
| 3740 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3741 | static ssize_t |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3742 | mismatch_cnt_show(mddev_t *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3743 | { |
| 3744 | return sprintf(page, "%llu\n", |
| 3745 | (unsigned long long) mddev->resync_mismatches); |
| 3746 | } |
| 3747 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3748 | static struct md_sysfs_entry md_scan_mode = |
| 3749 | __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3750 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 3751 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3752 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3753 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3754 | static ssize_t |
| 3755 | sync_min_show(mddev_t *mddev, char *page) |
| 3756 | { |
| 3757 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 3758 | mddev->sync_speed_min ? "local": "system"); |
| 3759 | } |
| 3760 | |
| 3761 | static ssize_t |
| 3762 | sync_min_store(mddev_t *mddev, const char *buf, size_t len) |
| 3763 | { |
| 3764 | int min; |
| 3765 | char *e; |
| 3766 | if (strncmp(buf, "system", 6)==0) { |
| 3767 | mddev->sync_speed_min = 0; |
| 3768 | return len; |
| 3769 | } |
| 3770 | min = simple_strtoul(buf, &e, 10); |
| 3771 | if (buf == e || (*e && *e != '\n') || min <= 0) |
| 3772 | return -EINVAL; |
| 3773 | mddev->sync_speed_min = min; |
| 3774 | return len; |
| 3775 | } |
| 3776 | |
| 3777 | static struct md_sysfs_entry md_sync_min = |
| 3778 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 3779 | |
| 3780 | static ssize_t |
| 3781 | sync_max_show(mddev_t *mddev, char *page) |
| 3782 | { |
| 3783 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 3784 | mddev->sync_speed_max ? "local": "system"); |
| 3785 | } |
| 3786 | |
| 3787 | static ssize_t |
| 3788 | sync_max_store(mddev_t *mddev, const char *buf, size_t len) |
| 3789 | { |
| 3790 | int max; |
| 3791 | char *e; |
| 3792 | if (strncmp(buf, "system", 6)==0) { |
| 3793 | mddev->sync_speed_max = 0; |
| 3794 | return len; |
| 3795 | } |
| 3796 | max = simple_strtoul(buf, &e, 10); |
| 3797 | if (buf == e || (*e && *e != '\n') || max <= 0) |
| 3798 | return -EINVAL; |
| 3799 | mddev->sync_speed_max = max; |
| 3800 | return len; |
| 3801 | } |
| 3802 | |
| 3803 | static struct md_sysfs_entry md_sync_max = |
| 3804 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 3805 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 3806 | static ssize_t |
| 3807 | degraded_show(mddev_t *mddev, char *page) |
| 3808 | { |
| 3809 | return sprintf(page, "%d\n", mddev->degraded); |
| 3810 | } |
| 3811 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3812 | |
| 3813 | static ssize_t |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 3814 | sync_force_parallel_show(mddev_t *mddev, char *page) |
| 3815 | { |
| 3816 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 3817 | } |
| 3818 | |
| 3819 | static ssize_t |
| 3820 | sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len) |
| 3821 | { |
| 3822 | long n; |
| 3823 | |
| 3824 | if (strict_strtol(buf, 10, &n)) |
| 3825 | return -EINVAL; |
| 3826 | |
| 3827 | if (n != 0 && n != 1) |
| 3828 | return -EINVAL; |
| 3829 | |
| 3830 | mddev->parallel_resync = n; |
| 3831 | |
| 3832 | if (mddev->sync_thread) |
| 3833 | wake_up(&resync_wait); |
| 3834 | |
| 3835 | return len; |
| 3836 | } |
| 3837 | |
| 3838 | /* force parallel resync, even with shared block devices */ |
| 3839 | static struct md_sysfs_entry md_sync_force_parallel = |
| 3840 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 3841 | sync_force_parallel_show, sync_force_parallel_store); |
| 3842 | |
| 3843 | static ssize_t |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3844 | sync_speed_show(mddev_t *mddev, char *page) |
| 3845 | { |
| 3846 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3847 | if (mddev->curr_resync == 0) |
| 3848 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3849 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 3850 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3851 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 3852 | db = resync - mddev->resync_mark_cnt; |
| 3853 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3854 | } |
| 3855 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3856 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3857 | |
| 3858 | static ssize_t |
| 3859 | sync_completed_show(mddev_t *mddev, char *page) |
| 3860 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3861 | unsigned long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3862 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3863 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3864 | return sprintf(page, "none\n"); |
| 3865 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3866 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3867 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3868 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3869 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3870 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 3871 | resync = mddev->curr_resync_completed; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3872 | return sprintf(page, "%lu / %lu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3873 | } |
| 3874 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3875 | static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 3876 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3877 | static ssize_t |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3878 | min_sync_show(mddev_t *mddev, char *page) |
| 3879 | { |
| 3880 | return sprintf(page, "%llu\n", |
| 3881 | (unsigned long long)mddev->resync_min); |
| 3882 | } |
| 3883 | static ssize_t |
| 3884 | min_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3885 | { |
| 3886 | unsigned long long min; |
| 3887 | if (strict_strtoull(buf, 10, &min)) |
| 3888 | return -EINVAL; |
| 3889 | if (min > mddev->resync_max) |
| 3890 | return -EINVAL; |
| 3891 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3892 | return -EBUSY; |
| 3893 | |
| 3894 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3895 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3896 | sector_t temp = min; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3897 | if (sector_div(temp, mddev->chunk_sectors)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3898 | return -EINVAL; |
| 3899 | } |
| 3900 | mddev->resync_min = min; |
| 3901 | |
| 3902 | return len; |
| 3903 | } |
| 3904 | |
| 3905 | static struct md_sysfs_entry md_min_sync = |
| 3906 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 3907 | |
| 3908 | static ssize_t |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3909 | max_sync_show(mddev_t *mddev, char *page) |
| 3910 | { |
| 3911 | if (mddev->resync_max == MaxSector) |
| 3912 | return sprintf(page, "max\n"); |
| 3913 | else |
| 3914 | return sprintf(page, "%llu\n", |
| 3915 | (unsigned long long)mddev->resync_max); |
| 3916 | } |
| 3917 | static ssize_t |
| 3918 | max_sync_store(mddev_t *mddev, const char *buf, size_t len) |
| 3919 | { |
| 3920 | if (strncmp(buf, "max", 3) == 0) |
| 3921 | mddev->resync_max = MaxSector; |
| 3922 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 3923 | unsigned long long max; |
| 3924 | if (strict_strtoull(buf, 10, &max)) |
| 3925 | return -EINVAL; |
| 3926 | if (max < mddev->resync_min) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3927 | return -EINVAL; |
| 3928 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 3929 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3930 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 3931 | return -EBUSY; |
| 3932 | |
| 3933 | /* Must be a multiple of chunk_size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3934 | if (mddev->chunk_sectors) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 3935 | sector_t temp = max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3936 | if (sector_div(temp, mddev->chunk_sectors)) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3937 | return -EINVAL; |
| 3938 | } |
| 3939 | mddev->resync_max = max; |
| 3940 | } |
| 3941 | wake_up(&mddev->recovery_wait); |
| 3942 | return len; |
| 3943 | } |
| 3944 | |
| 3945 | static struct md_sysfs_entry md_max_sync = |
| 3946 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 3947 | |
| 3948 | static ssize_t |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3949 | suspend_lo_show(mddev_t *mddev, char *page) |
| 3950 | { |
| 3951 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 3952 | } |
| 3953 | |
| 3954 | static ssize_t |
| 3955 | suspend_lo_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_hi || |
| 3966 | (new > mddev->suspend_lo && new < mddev->suspend_hi)) { |
| 3967 | mddev->suspend_lo = new; |
| 3968 | mddev->pers->quiesce(mddev, 2); |
| 3969 | return len; |
| 3970 | } else |
| 3971 | return -EINVAL; |
| 3972 | } |
| 3973 | static struct md_sysfs_entry md_suspend_lo = |
| 3974 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 3975 | |
| 3976 | |
| 3977 | static ssize_t |
| 3978 | suspend_hi_show(mddev_t *mddev, char *page) |
| 3979 | { |
| 3980 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 3981 | } |
| 3982 | |
| 3983 | static ssize_t |
| 3984 | suspend_hi_store(mddev_t *mddev, const char *buf, size_t len) |
| 3985 | { |
| 3986 | char *e; |
| 3987 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 3988 | |
NeilBrown | b8d966e | 2009-07-01 11:14:04 +1000 | [diff] [blame] | 3989 | if (mddev->pers == NULL || |
| 3990 | mddev->pers->quiesce == NULL) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 3991 | return -EINVAL; |
| 3992 | if (buf == e || (*e && *e != '\n')) |
| 3993 | return -EINVAL; |
| 3994 | if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) || |
| 3995 | (new > mddev->suspend_lo && new > mddev->suspend_hi)) { |
| 3996 | mddev->suspend_hi = new; |
| 3997 | mddev->pers->quiesce(mddev, 1); |
| 3998 | mddev->pers->quiesce(mddev, 0); |
| 3999 | return len; |
| 4000 | } else |
| 4001 | return -EINVAL; |
| 4002 | } |
| 4003 | static struct md_sysfs_entry md_suspend_hi = |
| 4004 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4005 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4006 | static ssize_t |
| 4007 | reshape_position_show(mddev_t *mddev, char *page) |
| 4008 | { |
| 4009 | if (mddev->reshape_position != MaxSector) |
| 4010 | return sprintf(page, "%llu\n", |
| 4011 | (unsigned long long)mddev->reshape_position); |
| 4012 | strcpy(page, "none\n"); |
| 4013 | return 5; |
| 4014 | } |
| 4015 | |
| 4016 | static ssize_t |
| 4017 | reshape_position_store(mddev_t *mddev, const char *buf, size_t len) |
| 4018 | { |
| 4019 | char *e; |
| 4020 | unsigned long long new = simple_strtoull(buf, &e, 10); |
| 4021 | if (mddev->pers) |
| 4022 | return -EBUSY; |
| 4023 | if (buf == e || (*e && *e != '\n')) |
| 4024 | return -EINVAL; |
| 4025 | mddev->reshape_position = new; |
| 4026 | mddev->delta_disks = 0; |
| 4027 | mddev->new_level = mddev->level; |
| 4028 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4029 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4030 | return len; |
| 4031 | } |
| 4032 | |
| 4033 | static struct md_sysfs_entry md_reshape_position = |
| 4034 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4035 | reshape_position_store); |
| 4036 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4037 | static ssize_t |
| 4038 | array_size_show(mddev_t *mddev, char *page) |
| 4039 | { |
| 4040 | if (mddev->external_size) |
| 4041 | return sprintf(page, "%llu\n", |
| 4042 | (unsigned long long)mddev->array_sectors/2); |
| 4043 | else |
| 4044 | return sprintf(page, "default\n"); |
| 4045 | } |
| 4046 | |
| 4047 | static ssize_t |
| 4048 | array_size_store(mddev_t *mddev, const char *buf, size_t len) |
| 4049 | { |
| 4050 | sector_t sectors; |
| 4051 | |
| 4052 | if (strncmp(buf, "default", 7) == 0) { |
| 4053 | if (mddev->pers) |
| 4054 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4055 | else |
| 4056 | sectors = mddev->array_sectors; |
| 4057 | |
| 4058 | mddev->external_size = 0; |
| 4059 | } else { |
| 4060 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
| 4061 | return -EINVAL; |
| 4062 | if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
NeilBrown | 2b69c83 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4063 | return -E2BIG; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4064 | |
| 4065 | mddev->external_size = 1; |
| 4066 | } |
| 4067 | |
| 4068 | mddev->array_sectors = sectors; |
| 4069 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4070 | if (mddev->pers) |
| 4071 | revalidate_disk(mddev->gendisk); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4072 | |
| 4073 | return len; |
| 4074 | } |
| 4075 | |
| 4076 | static struct md_sysfs_entry md_array_size = |
| 4077 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4078 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4079 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4080 | static struct attribute *md_default_attrs[] = { |
| 4081 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4082 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4083 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4084 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4085 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4086 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4087 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4088 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4089 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4090 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4091 | &md_reshape_position.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4092 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4093 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4094 | NULL, |
| 4095 | }; |
| 4096 | |
| 4097 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4098 | &md_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4099 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4100 | &md_sync_min.attr, |
| 4101 | &md_sync_max.attr, |
| 4102 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4103 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4104 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4105 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4106 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4107 | &md_suspend_lo.attr, |
| 4108 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4109 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4110 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4111 | NULL, |
| 4112 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4113 | static struct attribute_group md_redundancy_group = { |
| 4114 | .name = NULL, |
| 4115 | .attrs = md_redundancy_attrs, |
| 4116 | }; |
| 4117 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4118 | |
| 4119 | static ssize_t |
| 4120 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4121 | { |
| 4122 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4123 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4124 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4125 | |
| 4126 | if (!entry->show) |
| 4127 | return -EIO; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4128 | rv = mddev_lock(mddev); |
| 4129 | if (!rv) { |
| 4130 | rv = entry->show(mddev, page); |
| 4131 | mddev_unlock(mddev); |
| 4132 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4133 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | static ssize_t |
| 4137 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4138 | const char *page, size_t length) |
| 4139 | { |
| 4140 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
| 4141 | mddev_t *mddev = container_of(kobj, struct mddev_s, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4142 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4143 | |
| 4144 | if (!entry->store) |
| 4145 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4146 | if (!capable(CAP_SYS_ADMIN)) |
| 4147 | return -EACCES; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4148 | rv = mddev_lock(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4149 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4150 | mddev->hold_active = 0; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 4151 | if (!rv) { |
| 4152 | rv = entry->store(mddev, page, length); |
| 4153 | mddev_unlock(mddev); |
| 4154 | } |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4155 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | static void md_free(struct kobject *ko) |
| 4159 | { |
| 4160 | mddev_t *mddev = container_of(ko, mddev_t, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4161 | |
| 4162 | if (mddev->sysfs_state) |
| 4163 | sysfs_put(mddev->sysfs_state); |
| 4164 | |
| 4165 | if (mddev->gendisk) { |
| 4166 | del_gendisk(mddev->gendisk); |
| 4167 | put_disk(mddev->gendisk); |
| 4168 | } |
| 4169 | if (mddev->queue) |
| 4170 | blk_cleanup_queue(mddev->queue); |
| 4171 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4172 | kfree(mddev); |
| 4173 | } |
| 4174 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4175 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4176 | .show = md_attr_show, |
| 4177 | .store = md_attr_store, |
| 4178 | }; |
| 4179 | static struct kobj_type md_ktype = { |
| 4180 | .release = md_free, |
| 4181 | .sysfs_ops = &md_sysfs_ops, |
| 4182 | .default_attrs = md_default_attrs, |
| 4183 | }; |
| 4184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | int mdp_major = 0; |
| 4186 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4187 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4188 | { |
| 4189 | mddev_t *mddev = container_of(ws, mddev_t, del_work); |
| 4190 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4191 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4192 | kobject_del(&mddev->kobj); |
| 4193 | kobject_put(&mddev->kobj); |
| 4194 | } |
| 4195 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4196 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4198 | static DEFINE_MUTEX(disks_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | mddev_t *mddev = mddev_find(dev); |
| 4200 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4201 | int partitioned; |
| 4202 | int shift; |
| 4203 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4204 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | |
| 4206 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4207 | return -ENODEV; |
| 4208 | |
| 4209 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4210 | shift = partitioned ? MdpMinorShift : 0; |
| 4211 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4213 | /* wait for any previous instance if this device |
| 4214 | * to be completed removed (mddev_delayed_delete). |
| 4215 | */ |
| 4216 | flush_scheduled_work(); |
| 4217 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4218 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4219 | error = -EEXIST; |
| 4220 | if (mddev->gendisk) |
| 4221 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4222 | |
| 4223 | if (name) { |
| 4224 | /* Need to ensure that 'name' is not a duplicate. |
| 4225 | */ |
| 4226 | mddev_t *mddev2; |
| 4227 | spin_lock(&all_mddevs_lock); |
| 4228 | |
| 4229 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4230 | if (mddev2->gendisk && |
| 4231 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4232 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4233 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4234 | } |
| 4235 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4237 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4238 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4239 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4240 | if (!mddev->queue) |
| 4241 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4242 | mddev->queue->queuedata = mddev; |
| 4243 | |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4244 | /* Can be unlocked because the queue is new: no concurrency */ |
| 4245 | queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue); |
| 4246 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4247 | blk_queue_make_request(mddev->queue, md_make_request); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | disk = alloc_disk(1 << shift); |
| 4250 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4251 | blk_cleanup_queue(mddev->queue); |
| 4252 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4253 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4255 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4257 | if (name) |
| 4258 | strcpy(disk->disk_name, name); |
| 4259 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 4261 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | disk->fops = &md_fops; |
| 4264 | disk->private_data = mddev; |
| 4265 | disk->queue = mddev->queue; |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4266 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4267 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 4268 | * remove it now. |
| 4269 | */ |
| 4270 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | add_disk(disk); |
| 4272 | mddev->gendisk = disk; |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 4273 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 4274 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4275 | if (error) { |
| 4276 | /* This isn't possible, but as kobject_init_and_add is marked |
| 4277 | * __must_check, we must do something with the result |
| 4278 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4279 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 4280 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4281 | error = 0; |
| 4282 | } |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4283 | if (sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
| 4284 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4285 | abort: |
| 4286 | mutex_unlock(&disks_mutex); |
| 4287 | if (!error) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4288 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 4289 | mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, NULL, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4290 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4291 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4292 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 4296 | { |
| 4297 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | return NULL; |
| 4299 | } |
| 4300 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4301 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 4302 | { |
| 4303 | /* val must be "md_*" where * is not all digits. |
| 4304 | * We allocate an array with a large free minor number, and |
| 4305 | * set the name to val. val must not already be an active name. |
| 4306 | */ |
| 4307 | int len = strlen(val); |
| 4308 | char buf[DISK_NAME_LEN]; |
| 4309 | |
| 4310 | while (len && val[len-1] == '\n') |
| 4311 | len--; |
| 4312 | if (len >= DISK_NAME_LEN) |
| 4313 | return -E2BIG; |
| 4314 | strlcpy(buf, val, len+1); |
| 4315 | if (strncmp(buf, "md_", 3) != 0) |
| 4316 | return -EINVAL; |
| 4317 | return md_alloc(0, buf); |
| 4318 | } |
| 4319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | static void md_safemode_timeout(unsigned long data) |
| 4321 | { |
| 4322 | mddev_t *mddev = (mddev_t *) data; |
| 4323 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4324 | if (!atomic_read(&mddev->writes_pending)) { |
| 4325 | mddev->safemode = 1; |
| 4326 | if (mddev->external) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4327 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | md_wakeup_thread(mddev->thread); |
| 4330 | } |
| 4331 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4332 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4334 | static int md_run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4335 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4336 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4337 | mdk_rdev_t *rdev; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4338 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4340 | if (list_empty(&mddev->disks)) |
| 4341 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | |
| 4344 | if (mddev->pers) |
| 4345 | return -EBUSY; |
| 4346 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 4347 | /* These two calls synchronise us with the |
| 4348 | * sysfs_remove_group calls in mddev_unlock, |
| 4349 | * so they must have completed. |
| 4350 | */ |
| 4351 | mutex_lock(&mddev->open_mutex); |
| 4352 | mutex_unlock(&mddev->open_mutex); |
| 4353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | /* |
| 4355 | * Analyze all RAID superblock(s) |
| 4356 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4357 | if (!mddev->raid_disks) { |
| 4358 | if (!mddev->persistent) |
| 4359 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4360 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4363 | if (mddev->level != LEVEL_NONE) |
| 4364 | request_module("md-level-%d", mddev->level); |
| 4365 | else if (mddev->clevel[0]) |
| 4366 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | |
| 4368 | /* |
| 4369 | * Drop all container device buffers, from now on |
| 4370 | * the only valid external interface is through the md |
| 4371 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4373 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4374 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | continue; |
| 4376 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 4377 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4378 | |
| 4379 | /* perform some consistency tests on the device. |
| 4380 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4381 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4382 | */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4383 | if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4384 | if (mddev->dev_sectors && |
| 4385 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4386 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4387 | printk("md: %s: data overlaps metadata\n", |
| 4388 | mdname(mddev)); |
| 4389 | return -EINVAL; |
| 4390 | } |
| 4391 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 4392 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 4393 | > rdev->data_offset) { |
| 4394 | printk("md: %s: metadata overlaps data\n", |
| 4395 | mdname(mddev)); |
| 4396 | return -EINVAL; |
| 4397 | } |
| 4398 | } |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 4399 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | } |
| 4401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4403 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4404 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4406 | if (mddev->level != LEVEL_NONE) |
| 4407 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 4408 | mddev->level); |
| 4409 | else |
| 4410 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 4411 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | return -EINVAL; |
| 4413 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4414 | mddev->pers = pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4416 | if (mddev->level != pers->level) { |
| 4417 | mddev->level = pers->level; |
| 4418 | mddev->new_level = pers->level; |
| 4419 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4420 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4422 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4423 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4424 | /* This personality cannot handle reshaping... */ |
| 4425 | mddev->pers = NULL; |
| 4426 | module_put(pers->owner); |
| 4427 | return -EINVAL; |
| 4428 | } |
| 4429 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4430 | if (pers->sync_request) { |
| 4431 | /* Warn if this is a potentially silly |
| 4432 | * configuration. |
| 4433 | */ |
| 4434 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 4435 | mdk_rdev_t *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4436 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4437 | |
| 4438 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4439 | list_for_each_entry(rdev2, &mddev->disks, same_set) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4440 | if (rdev < rdev2 && |
| 4441 | rdev->bdev->bd_contains == |
| 4442 | rdev2->bdev->bd_contains) { |
| 4443 | printk(KERN_WARNING |
| 4444 | "%s: WARNING: %s appears to be" |
| 4445 | " on the same physical disk as" |
| 4446 | " %s.\n", |
| 4447 | mdname(mddev), |
| 4448 | bdevname(rdev->bdev,b), |
| 4449 | bdevname(rdev2->bdev,b2)); |
| 4450 | warned = 1; |
| 4451 | } |
| 4452 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4453 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 4454 | if (warned) |
| 4455 | printk(KERN_WARNING |
| 4456 | "True protection against single-disk" |
| 4457 | " failure might be compromised.\n"); |
| 4458 | } |
| 4459 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 4460 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4461 | /* may be over-ridden by personality */ |
| 4462 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 4463 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 4464 | mddev->barriers_work = 1; |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 4465 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 4467 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 4468 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 4469 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4470 | err = mddev->pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 4471 | if (err) |
| 4472 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4473 | else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) { |
| 4474 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 4475 | " but 'external_size' not in effect?\n", __func__); |
| 4476 | printk(KERN_ERR |
| 4477 | "md: invalid array_size %llu > default size %llu\n", |
| 4478 | (unsigned long long)mddev->array_sectors / 2, |
| 4479 | (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2); |
| 4480 | err = -EINVAL; |
| 4481 | mddev->pers->stop(mddev); |
| 4482 | } |
| 4483 | if (err == 0 && mddev->pers->sync_request) { |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4484 | err = bitmap_create(mddev); |
| 4485 | if (err) { |
| 4486 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 4487 | mdname(mddev), err); |
| 4488 | mddev->pers->stop(mddev); |
| 4489 | } |
| 4490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | if (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4492 | module_put(mddev->pers->owner); |
| 4493 | mddev->pers = NULL; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4494 | bitmap_destroy(mddev); |
| 4495 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4496 | } |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4497 | if (mddev->pers->sync_request) { |
| 4498 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 4499 | printk(KERN_WARNING |
| 4500 | "md: cannot register extra attributes for %s\n", |
| 4501 | mdname(mddev)); |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 4502 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4503 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 4504 | mddev->ro = 0; |
| 4505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4507 | atomic_set(&mddev->max_corr_read_errors, |
| 4508 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | mddev->safemode = 0; |
| 4510 | mddev->safemode_timer.function = md_safemode_timeout; |
| 4511 | mddev->safemode_timer.data = (unsigned long) mddev; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4512 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4513 | mddev->in_sync = 1; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4514 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4515 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4516 | if (rdev->raid_disk >= 0) { |
| 4517 | char nm[20]; |
| 4518 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 4519 | if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm)) |
| 4520 | printk("md: cannot register %s for %s\n", |
| 4521 | nm, mdname(mddev)); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 4522 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | |
| 4524 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4525 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4526 | if (mddev->flags) |
| 4527 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4528 | |
NeilBrown | 0b8c9de | 2006-07-10 04:44:15 -0700 | [diff] [blame] | 4529 | md_wakeup_thread(mddev->thread); |
| 4530 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 4531 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4532 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4533 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 4534 | if (mddev->sysfs_action) |
| 4535 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4536 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4537 | return 0; |
| 4538 | } |
| 4539 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 4540 | static int do_md_run(mddev_t *mddev) |
| 4541 | { |
| 4542 | int err; |
| 4543 | |
| 4544 | err = md_run(mddev); |
| 4545 | if (err) |
| 4546 | goto out; |
| 4547 | |
| 4548 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4549 | revalidate_disk(mddev->gendisk); |
| 4550 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 4551 | out: |
| 4552 | return err; |
| 4553 | } |
| 4554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4555 | static int restart_array(mddev_t *mddev) |
| 4556 | { |
| 4557 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4559 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4561 | return -ENXIO; |
| 4562 | if (!mddev->pers) |
| 4563 | return -EINVAL; |
| 4564 | if (!mddev->ro) |
| 4565 | return -EBUSY; |
| 4566 | mddev->safemode = 0; |
| 4567 | mddev->ro = 0; |
| 4568 | set_disk_ro(disk, 0); |
| 4569 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 4570 | mdname(mddev)); |
| 4571 | /* Kick recovery or resync if necessary */ |
| 4572 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4573 | md_wakeup_thread(mddev->thread); |
| 4574 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4575 | sysfs_notify_dirent(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 4576 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4577 | } |
| 4578 | |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4579 | /* similar to deny_write_access, but accounts for our holding a reference |
| 4580 | * to the file ourselves */ |
| 4581 | static int deny_bitmap_write_access(struct file * file) |
| 4582 | { |
| 4583 | struct inode *inode = file->f_mapping->host; |
| 4584 | |
| 4585 | spin_lock(&inode->i_lock); |
| 4586 | if (atomic_read(&inode->i_writecount) > 1) { |
| 4587 | spin_unlock(&inode->i_lock); |
| 4588 | return -ETXTBSY; |
| 4589 | } |
| 4590 | atomic_set(&inode->i_writecount, -1); |
| 4591 | spin_unlock(&inode->i_lock); |
| 4592 | |
| 4593 | return 0; |
| 4594 | } |
| 4595 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4596 | void restore_bitmap_write_access(struct file *file) |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 4597 | { |
| 4598 | struct inode *inode = file->f_mapping->host; |
| 4599 | |
| 4600 | spin_lock(&inode->i_lock); |
| 4601 | atomic_set(&inode->i_writecount, 1); |
| 4602 | spin_unlock(&inode->i_lock); |
| 4603 | } |
| 4604 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4605 | static void md_clean(mddev_t *mddev) |
| 4606 | { |
| 4607 | mddev->array_sectors = 0; |
| 4608 | mddev->external_size = 0; |
| 4609 | mddev->dev_sectors = 0; |
| 4610 | mddev->raid_disks = 0; |
| 4611 | mddev->recovery_cp = 0; |
| 4612 | mddev->resync_min = 0; |
| 4613 | mddev->resync_max = MaxSector; |
| 4614 | mddev->reshape_position = MaxSector; |
| 4615 | mddev->external = 0; |
| 4616 | mddev->persistent = 0; |
| 4617 | mddev->level = LEVEL_NONE; |
| 4618 | mddev->clevel[0] = 0; |
| 4619 | mddev->flags = 0; |
| 4620 | mddev->ro = 0; |
| 4621 | mddev->metadata_type[0] = 0; |
| 4622 | mddev->chunk_sectors = 0; |
| 4623 | mddev->ctime = mddev->utime = 0; |
| 4624 | mddev->layout = 0; |
| 4625 | mddev->max_disks = 0; |
| 4626 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 4627 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4628 | mddev->delta_disks = 0; |
| 4629 | mddev->new_level = LEVEL_NONE; |
| 4630 | mddev->new_layout = 0; |
| 4631 | mddev->new_chunk_sectors = 0; |
| 4632 | mddev->curr_resync = 0; |
| 4633 | mddev->resync_mismatches = 0; |
| 4634 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 4635 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 4636 | mddev->recovery = 0; |
| 4637 | mddev->in_sync = 0; |
| 4638 | mddev->degraded = 0; |
| 4639 | mddev->barriers_work = 0; |
| 4640 | mddev->safemode = 0; |
| 4641 | mddev->bitmap_info.offset = 0; |
| 4642 | mddev->bitmap_info.default_offset = 0; |
| 4643 | mddev->bitmap_info.chunksize = 0; |
| 4644 | mddev->bitmap_info.daemon_sleep = 0; |
| 4645 | mddev->bitmap_info.max_write_behind = 0; |
| 4646 | } |
| 4647 | |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 4648 | static void md_stop_writes(mddev_t *mddev) |
| 4649 | { |
| 4650 | if (mddev->sync_thread) { |
| 4651 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4652 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4653 | md_unregister_thread(mddev->sync_thread); |
| 4654 | mddev->sync_thread = NULL; |
| 4655 | } |
| 4656 | |
| 4657 | del_timer_sync(&mddev->safemode_timer); |
| 4658 | |
| 4659 | bitmap_flush(mddev); |
| 4660 | md_super_wait(mddev); |
| 4661 | |
| 4662 | if (!mddev->in_sync || mddev->flags) { |
| 4663 | /* mark array as shutdown cleanly */ |
| 4664 | mddev->in_sync = 1; |
| 4665 | md_update_sb(mddev, 1); |
| 4666 | } |
| 4667 | } |
| 4668 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4669 | static void md_stop(mddev_t *mddev) |
| 4670 | { |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4671 | md_stop_writes(mddev); |
| 4672 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4673 | mddev->pers->stop(mddev); |
| 4674 | if (mddev->pers->sync_request && mddev->to_remove == NULL) |
| 4675 | mddev->to_remove = &md_redundancy_group; |
| 4676 | module_put(mddev->pers->owner); |
| 4677 | mddev->pers = NULL; |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 4678 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4679 | } |
| 4680 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4681 | static int md_set_readonly(mddev_t *mddev, int is_open) |
| 4682 | { |
| 4683 | int err = 0; |
| 4684 | mutex_lock(&mddev->open_mutex); |
| 4685 | if (atomic_read(&mddev->openers) > is_open) { |
| 4686 | printk("md: %s still in use.\n",mdname(mddev)); |
| 4687 | err = -EBUSY; |
| 4688 | goto out; |
| 4689 | } |
| 4690 | if (mddev->pers) { |
| 4691 | md_stop_writes(mddev); |
| 4692 | |
| 4693 | err = -ENXIO; |
| 4694 | if (mddev->ro==1) |
| 4695 | goto out; |
| 4696 | mddev->ro = 1; |
| 4697 | set_disk_ro(mddev->gendisk, 1); |
| 4698 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4699 | sysfs_notify_dirent(mddev->sysfs_state); |
| 4700 | err = 0; |
| 4701 | } |
| 4702 | out: |
| 4703 | mutex_unlock(&mddev->open_mutex); |
| 4704 | return err; |
| 4705 | } |
| 4706 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4707 | /* mode: |
| 4708 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4709 | * 2 - stop but do not disassemble array |
| 4710 | */ |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4711 | 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] | 4712 | { |
| 4713 | int err = 0; |
| 4714 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4715 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4716 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4717 | mutex_lock(&mddev->open_mutex); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4718 | if (atomic_read(&mddev->openers) > is_open) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 4719 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4720 | err = -EBUSY; |
| 4721 | } else if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4723 | if (mddev->ro) |
| 4724 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4725 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4726 | md_stop(mddev); |
| 4727 | mddev->queue->merge_bvec_fn = NULL; |
| 4728 | mddev->queue->unplug_fn = NULL; |
| 4729 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4730 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4731 | /* tell userspace to handle 'inactive' */ |
| 4732 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4733 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4734 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 4735 | if (rdev->raid_disk >= 0) { |
| 4736 | char nm[20]; |
| 4737 | sprintf(nm, "rd%d", rdev->raid_disk); |
| 4738 | sysfs_remove_link(&mddev->kobj, nm); |
| 4739 | } |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 4740 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4741 | set_capacity(disk, 0); |
| 4742 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4743 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4744 | if (mddev->ro) |
| 4745 | mddev->ro = 0; |
| 4746 | |
NeilBrown | 80ffb3c | 2009-08-18 10:35:26 +1000 | [diff] [blame] | 4747 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4748 | } |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 4749 | mutex_unlock(&mddev->open_mutex); |
| 4750 | if (err) |
| 4751 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4752 | /* |
| 4753 | * Free resources if final stop |
| 4754 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4755 | if (mode == 0) { |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 4756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 4758 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4759 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4760 | if (mddev->bitmap_info.file) { |
| 4761 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 4762 | fput(mddev->bitmap_info.file); |
| 4763 | mddev->bitmap_info.file = NULL; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4764 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4765 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 4766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4767 | export_array(mddev); |
| 4768 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 4769 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 4770 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4771 | if (mddev->hold_active == UNTIL_STOP) |
| 4772 | mddev->hold_active = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4773 | |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 4774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | err = 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 4776 | blk_integrity_unregister(disk); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 4777 | md_new_event(mddev); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 4778 | sysfs_notify_dirent(mddev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4779 | return err; |
| 4780 | } |
| 4781 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4782 | #ifndef MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | static void autorun_array(mddev_t *mddev) |
| 4784 | { |
| 4785 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4786 | int err; |
| 4787 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 4788 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4790 | |
| 4791 | printk(KERN_INFO "md: running: "); |
| 4792 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4793 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | char b[BDEVNAME_SIZE]; |
| 4795 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 4796 | } |
| 4797 | printk("\n"); |
| 4798 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4799 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | if (err) { |
| 4801 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4802 | do_md_stop(mddev, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | } |
| 4804 | } |
| 4805 | |
| 4806 | /* |
| 4807 | * lets try to run arrays based on all disks that have arrived |
| 4808 | * until now. (those are in pending_raid_disks) |
| 4809 | * |
| 4810 | * the method: pick the first pending disk, collect all disks with |
| 4811 | * the same UUID, remove all from the pending list and put them into |
| 4812 | * the 'same_array' list. Then order this list based on superblock |
| 4813 | * update time (freshest comes first), kick out 'old' disks and |
| 4814 | * compare superblocks. If everything's fine then run it. |
| 4815 | * |
| 4816 | * If "unit" is allocated, then bump its reference count |
| 4817 | */ |
| 4818 | static void autorun_devices(int part) |
| 4819 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4820 | mdk_rdev_t *rdev0, *rdev, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | mddev_t *mddev; |
| 4822 | char b[BDEVNAME_SIZE]; |
| 4823 | |
| 4824 | printk(KERN_INFO "md: autorun ...\n"); |
| 4825 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4826 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 4828 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | rdev0 = list_entry(pending_raid_disks.next, |
| 4830 | mdk_rdev_t, same_set); |
| 4831 | |
| 4832 | printk(KERN_INFO "md: considering %s ...\n", |
| 4833 | bdevname(rdev0->bdev,b)); |
| 4834 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4835 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 4837 | printk(KERN_INFO "md: adding %s ...\n", |
| 4838 | bdevname(rdev->bdev,b)); |
| 4839 | list_move(&rdev->same_set, &candidates); |
| 4840 | } |
| 4841 | /* |
| 4842 | * now we have a set of devices, with all of them having |
| 4843 | * mostly sane superblocks. It's time to allocate the |
| 4844 | * mddev. |
| 4845 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 4846 | if (part) { |
| 4847 | dev = MKDEV(mdp_major, |
| 4848 | rdev0->preferred_minor << MdpMinorShift); |
| 4849 | unit = MINOR(dev) >> MdpMinorShift; |
| 4850 | } else { |
| 4851 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 4852 | unit = MINOR(dev); |
| 4853 | } |
| 4854 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 4856 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 4857 | break; |
| 4858 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | |
| 4860 | md_probe(dev, NULL, NULL); |
| 4861 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 4862 | if (!mddev || !mddev->gendisk) { |
| 4863 | if (mddev) |
| 4864 | mddev_put(mddev); |
| 4865 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4866 | "md: cannot allocate memory for md drive.\n"); |
| 4867 | break; |
| 4868 | } |
| 4869 | if (mddev_lock(mddev)) |
| 4870 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 4871 | mdname(mddev)); |
| 4872 | else if (mddev->raid_disks || mddev->major_version |
| 4873 | || !list_empty(&mddev->disks)) { |
| 4874 | printk(KERN_WARNING |
| 4875 | "md: %s already running, cannot run %s\n", |
| 4876 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 4877 | mddev_unlock(mddev); |
| 4878 | } else { |
| 4879 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 4880 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4881 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4882 | list_del_init(&rdev->same_set); |
| 4883 | if (bind_rdev_to_array(rdev, mddev)) |
| 4884 | export_rdev(rdev); |
| 4885 | } |
| 4886 | autorun_array(mddev); |
| 4887 | mddev_unlock(mddev); |
| 4888 | } |
| 4889 | /* on success, candidates will be empty, on error |
| 4890 | * it won't... |
| 4891 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4892 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4893 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 4895 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4896 | mddev_put(mddev); |
| 4897 | } |
| 4898 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 4899 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 4900 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4902 | static int get_version(void __user * arg) |
| 4903 | { |
| 4904 | mdu_version_t ver; |
| 4905 | |
| 4906 | ver.major = MD_MAJOR_VERSION; |
| 4907 | ver.minor = MD_MINOR_VERSION; |
| 4908 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 4909 | |
| 4910 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 4911 | return -EFAULT; |
| 4912 | |
| 4913 | return 0; |
| 4914 | } |
| 4915 | |
| 4916 | static int get_array_info(mddev_t * mddev, void __user * arg) |
| 4917 | { |
| 4918 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4919 | int nr,working,insync,failed,spare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4920 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4922 | nr=working=insync=failed=spare=0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 4923 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4925 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | failed++; |
| 4927 | else { |
| 4928 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 4929 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4930 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | else |
| 4932 | spare++; |
| 4933 | } |
| 4934 | } |
| 4935 | |
| 4936 | info.major_version = mddev->major_version; |
| 4937 | info.minor_version = mddev->minor_version; |
| 4938 | info.patch_version = MD_PATCHLEVEL_VERSION; |
| 4939 | info.ctime = mddev->ctime; |
| 4940 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4941 | info.size = mddev->dev_sectors / 2; |
| 4942 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 4943 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4944 | info.nr_disks = nr; |
| 4945 | info.raid_disks = mddev->raid_disks; |
| 4946 | info.md_minor = mddev->md_minor; |
| 4947 | info.not_persistent= !mddev->persistent; |
| 4948 | |
| 4949 | info.utime = mddev->utime; |
| 4950 | info.state = 0; |
| 4951 | if (mddev->in_sync) |
| 4952 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 4953 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 4954 | info.state = (1<<MD_SB_BITMAP_PRESENT); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4955 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4956 | info.working_disks = working; |
| 4957 | info.failed_disks = failed; |
| 4958 | info.spare_disks = spare; |
| 4959 | |
| 4960 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4961 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | |
| 4963 | if (copy_to_user(arg, &info, sizeof(info))) |
| 4964 | return -EFAULT; |
| 4965 | |
| 4966 | return 0; |
| 4967 | } |
| 4968 | |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 4969 | static int get_bitmap_file(mddev_t * mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4970 | { |
| 4971 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
| 4972 | char *ptr, *buf = NULL; |
| 4973 | int err = -ENOMEM; |
| 4974 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4975 | if (md_allow_write(mddev)) |
| 4976 | file = kmalloc(sizeof(*file), GFP_NOIO); |
| 4977 | else |
| 4978 | file = kmalloc(sizeof(*file), GFP_KERNEL); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 4979 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4980 | if (!file) |
| 4981 | goto out; |
| 4982 | |
| 4983 | /* bitmap disabled, zero the first byte and copy out */ |
| 4984 | if (!mddev->bitmap || !mddev->bitmap->file) { |
| 4985 | file->pathname[0] = '\0'; |
| 4986 | goto copy_out; |
| 4987 | } |
| 4988 | |
| 4989 | buf = kmalloc(sizeof(file->pathname), GFP_KERNEL); |
| 4990 | if (!buf) |
| 4991 | goto out; |
| 4992 | |
Christoph Hellwig | 6bcfd60 | 2008-05-23 13:04:34 -0700 | [diff] [blame] | 4993 | ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname)); |
| 4994 | if (IS_ERR(ptr)) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 4995 | goto out; |
| 4996 | |
| 4997 | strcpy(file->pathname, ptr); |
| 4998 | |
| 4999 | copy_out: |
| 5000 | err = 0; |
| 5001 | if (copy_to_user(arg, file, sizeof(*file))) |
| 5002 | err = -EFAULT; |
| 5003 | out: |
| 5004 | kfree(buf); |
| 5005 | kfree(file); |
| 5006 | return err; |
| 5007 | } |
| 5008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5009 | static int get_disk_info(mddev_t * mddev, void __user * arg) |
| 5010 | { |
| 5011 | mdu_disk_info_t info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | mdk_rdev_t *rdev; |
| 5013 | |
| 5014 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5015 | return -EFAULT; |
| 5016 | |
Andre Noll | 26ef379 | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5017 | rdev = find_rdev_nr(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | if (rdev) { |
| 5019 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5020 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5021 | info.raid_disk = rdev->raid_disk; |
| 5022 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5023 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5024 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5025 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5027 | info.state |= (1<<MD_DISK_SYNC); |
| 5028 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5029 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5030 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5031 | } else { |
| 5032 | info.major = info.minor = 0; |
| 5033 | info.raid_disk = -1; |
| 5034 | info.state = (1<<MD_DISK_REMOVED); |
| 5035 | } |
| 5036 | |
| 5037 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5038 | return -EFAULT; |
| 5039 | |
| 5040 | return 0; |
| 5041 | } |
| 5042 | |
| 5043 | static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info) |
| 5044 | { |
| 5045 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 5046 | mdk_rdev_t *rdev; |
| 5047 | dev_t dev = MKDEV(info->major,info->minor); |
| 5048 | |
| 5049 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5050 | return -EOVERFLOW; |
| 5051 | |
| 5052 | if (!mddev->raid_disks) { |
| 5053 | int err; |
| 5054 | /* expecting a device which has a superblock */ |
| 5055 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5056 | if (IS_ERR(rdev)) { |
| 5057 | printk(KERN_WARNING |
| 5058 | "md: md_import_device returned %ld\n", |
| 5059 | PTR_ERR(rdev)); |
| 5060 | return PTR_ERR(rdev); |
| 5061 | } |
| 5062 | if (!list_empty(&mddev->disks)) { |
| 5063 | mdk_rdev_t *rdev0 = list_entry(mddev->disks.next, |
| 5064 | mdk_rdev_t, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5065 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5067 | if (err < 0) { |
| 5068 | printk(KERN_WARNING |
| 5069 | "md: %s has different UUID to %s\n", |
| 5070 | bdevname(rdev->bdev,b), |
| 5071 | bdevname(rdev0->bdev,b2)); |
| 5072 | export_rdev(rdev); |
| 5073 | return -EINVAL; |
| 5074 | } |
| 5075 | } |
| 5076 | err = bind_rdev_to_array(rdev, mddev); |
| 5077 | if (err) |
| 5078 | export_rdev(rdev); |
| 5079 | return err; |
| 5080 | } |
| 5081 | |
| 5082 | /* |
| 5083 | * add_new_disk can be used once the array is assembled |
| 5084 | * to add "hot spares". They must already have a superblock |
| 5085 | * written |
| 5086 | */ |
| 5087 | if (mddev->pers) { |
| 5088 | int err; |
| 5089 | if (!mddev->pers->hot_add_disk) { |
| 5090 | printk(KERN_WARNING |
| 5091 | "%s: personality does not support diskops!\n", |
| 5092 | mdname(mddev)); |
| 5093 | return -EINVAL; |
| 5094 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5095 | if (mddev->persistent) |
| 5096 | rdev = md_import_device(dev, mddev->major_version, |
| 5097 | mddev->minor_version); |
| 5098 | else |
| 5099 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | if (IS_ERR(rdev)) { |
| 5101 | printk(KERN_WARNING |
| 5102 | "md: md_import_device returned %ld\n", |
| 5103 | PTR_ERR(rdev)); |
| 5104 | return PTR_ERR(rdev); |
| 5105 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 5106 | /* set save_raid_disk if appropriate */ |
| 5107 | if (!mddev->persistent) { |
| 5108 | if (info->state & (1<<MD_DISK_SYNC) && |
| 5109 | info->raid_disk < mddev->raid_disks) |
| 5110 | rdev->raid_disk = info->raid_disk; |
| 5111 | else |
| 5112 | rdev->raid_disk = -1; |
| 5113 | } else |
| 5114 | super_types[mddev->major_version]. |
| 5115 | validate_super(mddev, rdev); |
| 5116 | rdev->saved_raid_disk = rdev->raid_disk; |
| 5117 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5118 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5119 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5120 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5121 | else |
| 5122 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5124 | rdev->raid_disk = -1; |
| 5125 | err = bind_rdev_to_array(rdev, mddev); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 5126 | if (!err && !mddev->pers->hot_remove_disk) { |
| 5127 | /* If there is hot_add_disk but no hot_remove_disk |
| 5128 | * then added disks for geometry changes, |
| 5129 | * and should be added immediately. |
| 5130 | */ |
| 5131 | super_types[mddev->major_version]. |
| 5132 | validate_super(mddev, rdev); |
| 5133 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 5134 | if (err) |
| 5135 | unbind_rdev_from_array(rdev); |
| 5136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5137 | if (err) |
| 5138 | export_rdev(rdev); |
Neil Brown | 5266473 | 2008-06-28 08:31:44 +1000 | [diff] [blame] | 5139 | else |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 5140 | sysfs_notify_dirent(rdev->sysfs_state); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5141 | |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 5142 | md_update_sb(mddev, 1); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 5143 | if (mddev->degraded) |
| 5144 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c361777 | 2005-06-21 17:17:10 -0700 | [diff] [blame] | 5145 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 5146 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | return err; |
| 5148 | } |
| 5149 | |
| 5150 | /* otherwise, add_new_disk is only allowed |
| 5151 | * for major_version==0 superblocks |
| 5152 | */ |
| 5153 | if (mddev->major_version != 0) { |
| 5154 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 5155 | mdname(mddev)); |
| 5156 | return -EINVAL; |
| 5157 | } |
| 5158 | |
| 5159 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 5160 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5161 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5162 | if (IS_ERR(rdev)) { |
| 5163 | printk(KERN_WARNING |
| 5164 | "md: error, md_import_device() returned %ld\n", |
| 5165 | PTR_ERR(rdev)); |
| 5166 | return PTR_ERR(rdev); |
| 5167 | } |
| 5168 | rdev->desc_nr = info->number; |
| 5169 | if (info->raid_disk < mddev->raid_disks) |
| 5170 | rdev->raid_disk = info->raid_disk; |
| 5171 | else |
| 5172 | rdev->raid_disk = -1; |
| 5173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5175 | if (info->state & (1<<MD_DISK_SYNC)) |
| 5176 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5178 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 5179 | set_bit(WriteMostly, &rdev->flags); |
| 5180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5181 | if (!mddev->persistent) { |
| 5182 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5183 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5184 | } else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5185 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5186 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5188 | err = bind_rdev_to_array(rdev, mddev); |
| 5189 | if (err) { |
| 5190 | export_rdev(rdev); |
| 5191 | return err; |
| 5192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | return 0; |
| 5196 | } |
| 5197 | |
| 5198 | static int hot_remove_disk(mddev_t * mddev, dev_t dev) |
| 5199 | { |
| 5200 | char b[BDEVNAME_SIZE]; |
| 5201 | mdk_rdev_t *rdev; |
| 5202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5203 | rdev = find_rdev(mddev, dev); |
| 5204 | if (!rdev) |
| 5205 | return -ENXIO; |
| 5206 | |
| 5207 | if (rdev->raid_disk >= 0) |
| 5208 | goto busy; |
| 5209 | |
| 5210 | kick_rdev_from_array(rdev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5211 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5212 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5213 | |
| 5214 | return 0; |
| 5215 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 5216 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5218 | return -EBUSY; |
| 5219 | } |
| 5220 | |
| 5221 | static int hot_add_disk(mddev_t * mddev, dev_t dev) |
| 5222 | { |
| 5223 | char b[BDEVNAME_SIZE]; |
| 5224 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5225 | mdk_rdev_t *rdev; |
| 5226 | |
| 5227 | if (!mddev->pers) |
| 5228 | return -ENODEV; |
| 5229 | |
| 5230 | if (mddev->major_version != 0) { |
| 5231 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 5232 | " version-0 superblocks.\n", |
| 5233 | mdname(mddev)); |
| 5234 | return -EINVAL; |
| 5235 | } |
| 5236 | if (!mddev->pers->hot_add_disk) { |
| 5237 | printk(KERN_WARNING |
| 5238 | "%s: personality does not support diskops!\n", |
| 5239 | mdname(mddev)); |
| 5240 | return -EINVAL; |
| 5241 | } |
| 5242 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5243 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | if (IS_ERR(rdev)) { |
| 5245 | printk(KERN_WARNING |
| 5246 | "md: error, md_import_device() returned %ld\n", |
| 5247 | PTR_ERR(rdev)); |
| 5248 | return -EINVAL; |
| 5249 | } |
| 5250 | |
| 5251 | if (mddev->persistent) |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5252 | rdev->sb_start = calc_dev_sboffset(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5253 | else |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5254 | rdev->sb_start = rdev->bdev->bd_inode->i_size / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5255 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 5256 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5257 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5258 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | printk(KERN_WARNING |
| 5260 | "md: can not hot-add faulty %s disk to %s!\n", |
| 5261 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 5262 | err = -EINVAL; |
| 5263 | goto abort_export; |
| 5264 | } |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5265 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 5267 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 5268 | err = bind_rdev_to_array(rdev, mddev); |
| 5269 | if (err) |
| 5270 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | |
| 5272 | /* |
| 5273 | * The rest should better be atomic, we can have disk failures |
| 5274 | * noticed in interrupt contexts ... |
| 5275 | */ |
| 5276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5277 | rdev->raid_disk = -1; |
| 5278 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5279 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | |
| 5281 | /* |
| 5282 | * Kick recovery, maybe this spare has to be added to the |
| 5283 | * array immediately. |
| 5284 | */ |
| 5285 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5286 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5287 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | return 0; |
| 5289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | abort_export: |
| 5291 | export_rdev(rdev); |
| 5292 | return err; |
| 5293 | } |
| 5294 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5295 | static int set_bitmap_file(mddev_t *mddev, int fd) |
| 5296 | { |
| 5297 | int err; |
| 5298 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5299 | if (mddev->pers) { |
| 5300 | if (!mddev->pers->quiesce) |
| 5301 | return -EBUSY; |
| 5302 | if (mddev->recovery || mddev->sync_thread) |
| 5303 | return -EBUSY; |
| 5304 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5305 | } |
| 5306 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5307 | |
| 5308 | if (fd >= 0) { |
| 5309 | if (mddev->bitmap) |
| 5310 | return -EEXIST; /* cannot add when bitmap is present */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5311 | mddev->bitmap_info.file = fget(fd); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5312 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5313 | if (mddev->bitmap_info.file == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5314 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 5315 | mdname(mddev)); |
| 5316 | return -EBADF; |
| 5317 | } |
| 5318 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5319 | err = deny_bitmap_write_access(mddev->bitmap_info.file); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5320 | if (err) { |
| 5321 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 5322 | mdname(mddev)); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5323 | fput(mddev->bitmap_info.file); |
| 5324 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5325 | return err; |
| 5326 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5327 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5328 | } else if (mddev->bitmap == NULL) |
| 5329 | return -ENOENT; /* cannot remove what isn't there */ |
| 5330 | err = 0; |
| 5331 | if (mddev->pers) { |
| 5332 | mddev->pers->quiesce(mddev, 1); |
| 5333 | if (fd >= 0) |
| 5334 | err = bitmap_create(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5335 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5336 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5337 | fd = -1; /* make sure to put the file */ |
| 5338 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5339 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5340 | } |
| 5341 | if (fd < 0) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5342 | if (mddev->bitmap_info.file) { |
| 5343 | restore_bitmap_write_access(mddev->bitmap_info.file); |
| 5344 | fput(mddev->bitmap_info.file); |
NeilBrown | acc55e2 | 2006-06-26 00:27:47 -0700 | [diff] [blame] | 5345 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5346 | mddev->bitmap_info.file = NULL; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5347 | } |
| 5348 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5349 | return err; |
| 5350 | } |
| 5351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | /* |
| 5353 | * set_array_info is used two different ways |
| 5354 | * The original usage is when creating a new array. |
| 5355 | * In this usage, raid_disks is > 0 and it together with |
| 5356 | * level, size, not_persistent,layout,chunksize determine the |
| 5357 | * shape of the array. |
| 5358 | * This will always create an array with a type-0.90.0 superblock. |
| 5359 | * The newer usage is when assembling an array. |
| 5360 | * In this case raid_disks will be 0, and the major_version field is |
| 5361 | * use to determine which style super-blocks are to be found on the devices. |
| 5362 | * The minor and patch _version numbers are also kept incase the |
| 5363 | * super_block handler wishes to interpret them. |
| 5364 | */ |
| 5365 | static int set_array_info(mddev_t * mddev, mdu_array_info_t *info) |
| 5366 | { |
| 5367 | |
| 5368 | if (info->raid_disks == 0) { |
| 5369 | /* just setting version number for superblock loading */ |
| 5370 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 5371 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5372 | super_types[info->major_version].name == NULL) { |
| 5373 | /* maybe try to auto-load a module? */ |
| 5374 | printk(KERN_INFO |
| 5375 | "md: superblock version %d not known\n", |
| 5376 | info->major_version); |
| 5377 | return -EINVAL; |
| 5378 | } |
| 5379 | mddev->major_version = info->major_version; |
| 5380 | mddev->minor_version = info->minor_version; |
| 5381 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5382 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 5383 | /* ensure mddev_put doesn't delete this now that there |
| 5384 | * is some minimal configuration. |
| 5385 | */ |
| 5386 | mddev->ctime = get_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5387 | return 0; |
| 5388 | } |
| 5389 | mddev->major_version = MD_MAJOR_VERSION; |
| 5390 | mddev->minor_version = MD_MINOR_VERSION; |
| 5391 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
| 5392 | mddev->ctime = get_seconds(); |
| 5393 | |
| 5394 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 5395 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5396 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | mddev->raid_disks = info->raid_disks; |
| 5398 | /* don't set md_minor, it is determined by which /dev/md* was |
| 5399 | * openned |
| 5400 | */ |
| 5401 | if (info->state & (1<<MD_SB_CLEAN)) |
| 5402 | mddev->recovery_cp = MaxSector; |
| 5403 | else |
| 5404 | mddev->recovery_cp = 0; |
| 5405 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5406 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | |
| 5408 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5409 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | |
| 5411 | mddev->max_disks = MD_SB_DISKS; |
| 5412 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 5413 | if (mddev->persistent) |
| 5414 | mddev->flags = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5415 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5417 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
| 5418 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 5419 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5420 | mddev->reshape_position = MaxSector; |
| 5421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5422 | /* |
| 5423 | * Generate a 128 bit UUID |
| 5424 | */ |
| 5425 | get_random_bytes(mddev->uuid, 16); |
| 5426 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5427 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5428 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5429 | mddev->new_layout = mddev->layout; |
| 5430 | mddev->delta_disks = 0; |
| 5431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | return 0; |
| 5433 | } |
| 5434 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5435 | void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors) |
| 5436 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5437 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 5438 | |
| 5439 | if (mddev->external_size) |
| 5440 | return; |
| 5441 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 5442 | mddev->array_sectors = array_sectors; |
| 5443 | } |
| 5444 | EXPORT_SYMBOL(md_set_array_sectors); |
| 5445 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5446 | static int update_size(mddev_t *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5447 | { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5448 | mdk_rdev_t *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5449 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5450 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5451 | |
| 5452 | if (mddev->pers->resize == NULL) |
| 5453 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5454 | /* The "num_sectors" is the number of sectors of each device that |
| 5455 | * is used. This can only make sense for arrays with redundancy. |
| 5456 | * linear and raid0 always use whatever space is available. We can only |
| 5457 | * consider changing this number if no resync or reconstruction is |
| 5458 | * 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] | 5459 | * 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] | 5460 | * of each device. If num_sectors is zero, we find the largest size |
| 5461 | * that fits. |
| 5462 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5463 | */ |
| 5464 | if (mddev->sync_thread) |
| 5465 | return -EBUSY; |
NeilBrown | dba034e | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 5466 | if (mddev->bitmap) |
| 5467 | /* Sorry, cannot grow a bitmap yet, just remove it, |
| 5468 | * grow, and re-add. |
| 5469 | */ |
| 5470 | return -EBUSY; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5471 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5472 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 5473 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5474 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 5475 | num_sectors = avail; |
| 5476 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5477 | return -ENOSPC; |
| 5478 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5479 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5480 | if (!rv) |
| 5481 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5482 | return rv; |
| 5483 | } |
| 5484 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5485 | static int update_raid_disks(mddev_t *mddev, int raid_disks) |
| 5486 | { |
| 5487 | int rv; |
| 5488 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5489 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5490 | return -EINVAL; |
| 5491 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 5492 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5493 | return -EINVAL; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5494 | if (mddev->sync_thread || mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5495 | return -EBUSY; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5496 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
| 5497 | |
| 5498 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5499 | return rv; |
| 5500 | } |
| 5501 | |
| 5502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5503 | /* |
| 5504 | * update_array_info is used to change the configuration of an |
| 5505 | * on-line array. |
| 5506 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 5507 | * fields in the info are checked against the array. |
| 5508 | * Any differences that cannot be handled will cause an error. |
| 5509 | * Normally, only one change can be managed at a time. |
| 5510 | */ |
| 5511 | static int update_array_info(mddev_t *mddev, mdu_array_info_t *info) |
| 5512 | { |
| 5513 | int rv = 0; |
| 5514 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5515 | int state = 0; |
| 5516 | |
| 5517 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5518 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5519 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5520 | |
| 5521 | if (mddev->major_version != info->major_version || |
| 5522 | mddev->minor_version != info->minor_version || |
| 5523 | /* mddev->patch_version != info->patch_version || */ |
| 5524 | mddev->ctime != info->ctime || |
| 5525 | mddev->level != info->level || |
| 5526 | /* mddev->layout != info->layout || */ |
| 5527 | !mddev->persistent != info->not_persistent|| |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5528 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5529 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 5530 | ((state^info->state) & 0xfffffe00) |
| 5531 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | return -EINVAL; |
| 5533 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5534 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 5535 | cnt++; |
| 5536 | if (mddev->raid_disks != info->raid_disks) |
| 5537 | cnt++; |
| 5538 | if (mddev->layout != info->layout) |
| 5539 | cnt++; |
| 5540 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 5541 | cnt++; |
| 5542 | if (cnt == 0) |
| 5543 | return 0; |
| 5544 | if (cnt > 1) |
| 5545 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5546 | |
| 5547 | if (mddev->layout != info->layout) { |
| 5548 | /* Change layout |
| 5549 | * we don't need to do anything at the md level, the |
| 5550 | * personality will take care of it all. |
| 5551 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5552 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5554 | else { |
| 5555 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5556 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 5557 | if (rv) |
| 5558 | mddev->new_layout = mddev->layout; |
| 5559 | return rv; |
| 5560 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5562 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5563 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5564 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 5565 | if (mddev->raid_disks != info->raid_disks) |
| 5566 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5567 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5568 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5569 | if (mddev->pers->quiesce == NULL) |
| 5570 | return -EINVAL; |
| 5571 | if (mddev->recovery || mddev->sync_thread) |
| 5572 | return -EBUSY; |
| 5573 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
| 5574 | /* add the bitmap */ |
| 5575 | if (mddev->bitmap) |
| 5576 | return -EEXIST; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5577 | if (mddev->bitmap_info.default_offset == 0) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5578 | return -EINVAL; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5579 | mddev->bitmap_info.offset = |
| 5580 | mddev->bitmap_info.default_offset; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5581 | mddev->pers->quiesce(mddev, 1); |
| 5582 | rv = bitmap_create(mddev); |
| 5583 | if (rv) |
| 5584 | bitmap_destroy(mddev); |
| 5585 | mddev->pers->quiesce(mddev, 0); |
| 5586 | } else { |
| 5587 | /* remove the bitmap */ |
| 5588 | if (!mddev->bitmap) |
| 5589 | return -ENOENT; |
| 5590 | if (mddev->bitmap->file) |
| 5591 | return -EINVAL; |
| 5592 | mddev->pers->quiesce(mddev, 1); |
| 5593 | bitmap_destroy(mddev); |
| 5594 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5595 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5596 | } |
| 5597 | } |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5598 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5599 | return rv; |
| 5600 | } |
| 5601 | |
| 5602 | static int set_disk_faulty(mddev_t *mddev, dev_t dev) |
| 5603 | { |
| 5604 | mdk_rdev_t *rdev; |
| 5605 | |
| 5606 | if (mddev->pers == NULL) |
| 5607 | return -ENODEV; |
| 5608 | |
| 5609 | rdev = find_rdev(mddev, dev); |
| 5610 | if (!rdev) |
| 5611 | return -ENODEV; |
| 5612 | |
| 5613 | md_error(mddev, rdev); |
| 5614 | return 0; |
| 5615 | } |
| 5616 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 5617 | /* |
| 5618 | * We have a problem here : there is no easy way to give a CHS |
| 5619 | * virtual geometry. We currently pretend that we have a 2 heads |
| 5620 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 5621 | * dosfs just mad... ;-) |
| 5622 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5623 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 5624 | { |
| 5625 | mddev_t *mddev = bdev->bd_disk->private_data; |
| 5626 | |
| 5627 | geo->heads = 2; |
| 5628 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 5629 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5630 | return 0; |
| 5631 | } |
| 5632 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5633 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | unsigned int cmd, unsigned long arg) |
| 5635 | { |
| 5636 | int err = 0; |
| 5637 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | mddev_t *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5639 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | |
| 5641 | if (!capable(CAP_SYS_ADMIN)) |
| 5642 | return -EACCES; |
| 5643 | |
| 5644 | /* |
| 5645 | * Commands dealing with the RAID driver but not any |
| 5646 | * particular array: |
| 5647 | */ |
| 5648 | switch (cmd) |
| 5649 | { |
| 5650 | case RAID_VERSION: |
| 5651 | err = get_version(argp); |
| 5652 | goto done; |
| 5653 | |
| 5654 | case PRINT_RAID_DEBUG: |
| 5655 | err = 0; |
| 5656 | md_print_devices(); |
| 5657 | goto done; |
| 5658 | |
| 5659 | #ifndef MODULE |
| 5660 | case RAID_AUTORUN: |
| 5661 | err = 0; |
| 5662 | autostart_arrays(arg); |
| 5663 | goto done; |
| 5664 | #endif |
| 5665 | default:; |
| 5666 | } |
| 5667 | |
| 5668 | /* |
| 5669 | * Commands creating/starting a new array: |
| 5670 | */ |
| 5671 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5672 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | |
| 5674 | if (!mddev) { |
| 5675 | BUG(); |
| 5676 | goto abort; |
| 5677 | } |
| 5678 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | err = mddev_lock(mddev); |
| 5680 | if (err) { |
| 5681 | printk(KERN_INFO |
| 5682 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 5683 | err, cmd); |
| 5684 | goto abort; |
| 5685 | } |
| 5686 | |
| 5687 | switch (cmd) |
| 5688 | { |
| 5689 | case SET_ARRAY_INFO: |
| 5690 | { |
| 5691 | mdu_array_info_t info; |
| 5692 | if (!arg) |
| 5693 | memset(&info, 0, sizeof(info)); |
| 5694 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 5695 | err = -EFAULT; |
| 5696 | goto abort_unlock; |
| 5697 | } |
| 5698 | if (mddev->pers) { |
| 5699 | err = update_array_info(mddev, &info); |
| 5700 | if (err) { |
| 5701 | printk(KERN_WARNING "md: couldn't update" |
| 5702 | " array info. %d\n", err); |
| 5703 | goto abort_unlock; |
| 5704 | } |
| 5705 | goto done_unlock; |
| 5706 | } |
| 5707 | if (!list_empty(&mddev->disks)) { |
| 5708 | printk(KERN_WARNING |
| 5709 | "md: array %s already has disks!\n", |
| 5710 | mdname(mddev)); |
| 5711 | err = -EBUSY; |
| 5712 | goto abort_unlock; |
| 5713 | } |
| 5714 | if (mddev->raid_disks) { |
| 5715 | printk(KERN_WARNING |
| 5716 | "md: array %s already initialised!\n", |
| 5717 | mdname(mddev)); |
| 5718 | err = -EBUSY; |
| 5719 | goto abort_unlock; |
| 5720 | } |
| 5721 | err = set_array_info(mddev, &info); |
| 5722 | if (err) { |
| 5723 | printk(KERN_WARNING "md: couldn't set" |
| 5724 | " array info. %d\n", err); |
| 5725 | goto abort_unlock; |
| 5726 | } |
| 5727 | } |
| 5728 | goto done_unlock; |
| 5729 | |
| 5730 | default:; |
| 5731 | } |
| 5732 | |
| 5733 | /* |
| 5734 | * Commands querying/configuring an existing array: |
| 5735 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5736 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 5737 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 5738 | if ((!mddev->raid_disks && !mddev->external) |
| 5739 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 5740 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 5741 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | err = -ENODEV; |
| 5743 | goto abort_unlock; |
| 5744 | } |
| 5745 | |
| 5746 | /* |
| 5747 | * Commands even a read-only array can execute: |
| 5748 | */ |
| 5749 | switch (cmd) |
| 5750 | { |
| 5751 | case GET_ARRAY_INFO: |
| 5752 | err = get_array_info(mddev, argp); |
| 5753 | goto done_unlock; |
| 5754 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5755 | case GET_BITMAP_FILE: |
viro@ZenIV.linux.org.uk | 87162a2 | 2005-09-09 20:36:43 +0100 | [diff] [blame] | 5756 | err = get_bitmap_file(mddev, argp); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5757 | goto done_unlock; |
| 5758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5759 | case GET_DISK_INFO: |
| 5760 | err = get_disk_info(mddev, argp); |
| 5761 | goto done_unlock; |
| 5762 | |
| 5763 | case RESTART_ARRAY_RW: |
| 5764 | err = restart_array(mddev); |
| 5765 | goto done_unlock; |
| 5766 | |
| 5767 | case STOP_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5768 | err = do_md_stop(mddev, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | goto done_unlock; |
| 5770 | |
| 5771 | case STOP_ARRAY_RO: |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5772 | err = md_set_readonly(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | goto done_unlock; |
| 5774 | |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 5775 | case BLKROSET: |
| 5776 | if (get_user(ro, (int __user *)(arg))) { |
| 5777 | err = -EFAULT; |
| 5778 | goto done_unlock; |
| 5779 | } |
| 5780 | err = -EINVAL; |
| 5781 | |
| 5782 | /* if the bdev is going readonly the value of mddev->ro |
| 5783 | * does not matter, no writes are coming |
| 5784 | */ |
| 5785 | if (ro) |
| 5786 | goto done_unlock; |
| 5787 | |
| 5788 | /* are we are already prepared for writes? */ |
| 5789 | if (mddev->ro != 1) |
| 5790 | goto done_unlock; |
| 5791 | |
| 5792 | /* transitioning to readauto need only happen for |
| 5793 | * arrays that call md_write_start |
| 5794 | */ |
| 5795 | if (mddev->pers) { |
| 5796 | err = restart_array(mddev); |
| 5797 | if (err == 0) { |
| 5798 | mddev->ro = 2; |
| 5799 | set_disk_ro(mddev->gendisk, 0); |
| 5800 | } |
| 5801 | } |
| 5802 | goto done_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | } |
| 5804 | |
| 5805 | /* |
| 5806 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5807 | * superblock, so we do not allow them on read-only arrays. |
| 5808 | * However non-MD ioctls (e.g. get-size) will still come through |
| 5809 | * here and hit the 'default' below, so only disallow |
| 5810 | * 'md' ioctls, and switch to rw mode if started auto-readonly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5811 | */ |
Andre Noll | bb57fc6 | 2008-04-25 19:06:35 +0200 | [diff] [blame] | 5812 | if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5813 | if (mddev->ro == 2) { |
| 5814 | mddev->ro = 0; |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5815 | sysfs_notify_dirent(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5816 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5817 | md_wakeup_thread(mddev->thread); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5818 | } else { |
| 5819 | err = -EROFS; |
| 5820 | goto abort_unlock; |
| 5821 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5822 | } |
| 5823 | |
| 5824 | switch (cmd) |
| 5825 | { |
| 5826 | case ADD_NEW_DISK: |
| 5827 | { |
| 5828 | mdu_disk_info_t info; |
| 5829 | if (copy_from_user(&info, argp, sizeof(info))) |
| 5830 | err = -EFAULT; |
| 5831 | else |
| 5832 | err = add_new_disk(mddev, &info); |
| 5833 | goto done_unlock; |
| 5834 | } |
| 5835 | |
| 5836 | case HOT_REMOVE_DISK: |
| 5837 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
| 5838 | goto done_unlock; |
| 5839 | |
| 5840 | case HOT_ADD_DISK: |
| 5841 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
| 5842 | goto done_unlock; |
| 5843 | |
| 5844 | case SET_DISK_FAULTY: |
| 5845 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
| 5846 | goto done_unlock; |
| 5847 | |
| 5848 | case RUN_ARRAY: |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5849 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5850 | goto done_unlock; |
| 5851 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5852 | case SET_BITMAP_FILE: |
| 5853 | err = set_bitmap_file(mddev, (int)arg); |
| 5854 | goto done_unlock; |
| 5855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | err = -EINVAL; |
| 5858 | goto abort_unlock; |
| 5859 | } |
| 5860 | |
| 5861 | done_unlock: |
| 5862 | abort_unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5863 | if (mddev->hold_active == UNTIL_IOCTL && |
| 5864 | err != -EINVAL) |
| 5865 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | mddev_unlock(mddev); |
| 5867 | |
| 5868 | return err; |
| 5869 | done: |
| 5870 | if (err) |
| 5871 | MD_BUG(); |
| 5872 | abort: |
| 5873 | return err; |
| 5874 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5875 | #ifdef CONFIG_COMPAT |
| 5876 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 5877 | unsigned int cmd, unsigned long arg) |
| 5878 | { |
| 5879 | switch (cmd) { |
| 5880 | case HOT_REMOVE_DISK: |
| 5881 | case HOT_ADD_DISK: |
| 5882 | case SET_DISK_FAULTY: |
| 5883 | case SET_BITMAP_FILE: |
| 5884 | /* These take in integer arg, do not convert */ |
| 5885 | break; |
| 5886 | default: |
| 5887 | arg = (unsigned long)compat_ptr(arg); |
| 5888 | break; |
| 5889 | } |
| 5890 | |
| 5891 | return md_ioctl(bdev, mode, cmd, arg); |
| 5892 | } |
| 5893 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5894 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5895 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | { |
| 5897 | /* |
| 5898 | * Succeed if we can lock the mddev, which confirms that |
| 5899 | * it isn't being stopped right now. |
| 5900 | */ |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5901 | mddev_t *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | int err; |
| 5903 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5904 | if (mddev->gendisk != bdev->bd_disk) { |
| 5905 | /* we are racing with mddev_put which is discarding this |
| 5906 | * bd_disk. |
| 5907 | */ |
| 5908 | mddev_put(mddev); |
| 5909 | /* Wait until bdev->bd_disk is definitely gone */ |
| 5910 | flush_scheduled_work(); |
| 5911 | /* Then retry the open from the top */ |
| 5912 | return -ERESTARTSYS; |
| 5913 | } |
| 5914 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 5915 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5916 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | goto out; |
| 5918 | |
| 5919 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5920 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5921 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | |
NeilBrown | f3b99be | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 5923 | check_disk_size_change(mddev->gendisk, bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5924 | out: |
| 5925 | return err; |
| 5926 | } |
| 5927 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5928 | static int md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | { |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5930 | mddev_t *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 5932 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5933 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | mddev_put(mddev); |
| 5935 | |
| 5936 | return 0; |
| 5937 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 5938 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5939 | { |
| 5940 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 5941 | .open = md_open, |
| 5942 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 5943 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 5944 | #ifdef CONFIG_COMPAT |
| 5945 | .compat_ioctl = md_compat_ioctl, |
| 5946 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 5947 | .getgeo = md_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | }; |
| 5949 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 5950 | static int md_thread(void * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | { |
| 5952 | mdk_thread_t *thread = arg; |
| 5953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | /* |
| 5955 | * md_thread is a 'system-thread', it's priority should be very |
| 5956 | * high. We avoid resource deadlocks individually in each |
| 5957 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 5958 | * the very same RT priority as kswapd, thus we will never get |
| 5959 | * into a priority inversion deadlock. |
| 5960 | * |
| 5961 | * we definitely have to have equal or higher priority than |
| 5962 | * bdflush, otherwise bdflush will deadlock if there are too |
| 5963 | * many dirty RAID5 blocks. |
| 5964 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 5966 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5967 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5968 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 5969 | /* We need to wait INTERRUPTIBLE so that |
| 5970 | * we don't add to the load-average. |
| 5971 | * That means we need to be sure no signals are |
| 5972 | * pending |
| 5973 | */ |
| 5974 | if (signal_pending(current)) |
| 5975 | flush_signals(current); |
| 5976 | |
| 5977 | wait_event_interruptible_timeout |
| 5978 | (thread->wqueue, |
| 5979 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 5980 | || kthread_should_stop(), |
| 5981 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5982 | |
| 5983 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 5984 | |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 5985 | thread->run(thread->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5986 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 5987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | return 0; |
| 5989 | } |
| 5990 | |
| 5991 | void md_wakeup_thread(mdk_thread_t *thread) |
| 5992 | { |
| 5993 | if (thread) { |
| 5994 | dprintk("md: waking up MD thread %s.\n", thread->tsk->comm); |
| 5995 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 5996 | wake_up(&thread->wqueue); |
| 5997 | } |
| 5998 | } |
| 5999 | |
| 6000 | mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev, |
| 6001 | const char *name) |
| 6002 | { |
| 6003 | mdk_thread_t *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6005 | thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | if (!thread) |
| 6007 | return NULL; |
| 6008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6009 | init_waitqueue_head(&thread->wqueue); |
| 6010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | thread->run = run; |
| 6012 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6013 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6014 | thread->tsk = kthread_run(md_thread, thread, |
| 6015 | "%s_%s", |
| 6016 | mdname(thread->mddev), |
| 6017 | name ?: mddev->pers->name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6018 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6019 | kfree(thread); |
| 6020 | return NULL; |
| 6021 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6022 | return thread; |
| 6023 | } |
| 6024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | void md_unregister_thread(mdk_thread_t *thread) |
| 6026 | { |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6027 | if (!thread) |
| 6028 | return; |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 6029 | dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 6030 | |
| 6031 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6032 | kfree(thread); |
| 6033 | } |
| 6034 | |
| 6035 | void md_error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 6036 | { |
| 6037 | if (!mddev) { |
| 6038 | MD_BUG(); |
| 6039 | return; |
| 6040 | } |
| 6041 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6042 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6044 | |
| 6045 | if (mddev->external) |
| 6046 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6047 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6048 | dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n", |
| 6049 | mdname(mddev), |
| 6050 | MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev), |
| 6051 | __builtin_return_address(0),__builtin_return_address(1), |
| 6052 | __builtin_return_address(2),__builtin_return_address(3)); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6053 | */ |
Andrew Morton | d0a0a5e | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 6054 | if (!mddev->pers) |
| 6055 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | if (!mddev->pers->error_handler) |
| 6057 | return; |
| 6058 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 6059 | if (mddev->degraded) |
| 6060 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | c0cc75f | 2010-03-22 10:28:51 +1100 | [diff] [blame] | 6061 | sysfs_notify_dirent(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6063 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6064 | md_wakeup_thread(mddev->thread); |
NeilBrown | c331eb0 | 2006-05-30 21:27:13 -0700 | [diff] [blame] | 6065 | md_new_event_inintr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | } |
| 6067 | |
| 6068 | /* seq_file implementation /proc/mdstat */ |
| 6069 | |
| 6070 | static void status_unused(struct seq_file *seq) |
| 6071 | { |
| 6072 | int i = 0; |
| 6073 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | |
| 6075 | seq_printf(seq, "unused devices: "); |
| 6076 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6077 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6078 | char b[BDEVNAME_SIZE]; |
| 6079 | i++; |
| 6080 | seq_printf(seq, "%s ", |
| 6081 | bdevname(rdev->bdev,b)); |
| 6082 | } |
| 6083 | if (!i) |
| 6084 | seq_printf(seq, "<none>"); |
| 6085 | |
| 6086 | seq_printf(seq, "\n"); |
| 6087 | } |
| 6088 | |
| 6089 | |
| 6090 | static void status_resync(struct seq_file *seq, mddev_t * mddev) |
| 6091 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6092 | sector_t max_sectors, resync, res; |
| 6093 | unsigned long dt, db; |
| 6094 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6095 | int scale; |
| 6096 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6098 | resync = mddev->curr_resync - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | |
| 6100 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6101 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6102 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6103 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6104 | |
| 6105 | /* |
| 6106 | * Should not happen. |
| 6107 | */ |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6108 | if (!max_sectors) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | MD_BUG(); |
| 6110 | return; |
| 6111 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6112 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6113 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6114 | * u32, as those are the requirements for sector_div. |
| 6115 | * Thus 'scale' must be at least 10 |
| 6116 | */ |
| 6117 | scale = 10; |
| 6118 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6119 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6120 | scale++; |
| 6121 | } |
| 6122 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6123 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6124 | |
| 6125 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6126 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6127 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6128 | seq_printf(seq, "["); |
| 6129 | for (i = 0; i < x; i++) |
| 6130 | seq_printf(seq, "="); |
| 6131 | seq_printf(seq, ">"); |
| 6132 | for (i = 0; i < y; i++) |
| 6133 | seq_printf(seq, "."); |
| 6134 | seq_printf(seq, "] "); |
| 6135 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 6136 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6137 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 6138 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6139 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 6140 | "check" : |
| 6141 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 6142 | "resync" : "recovery"))), |
| 6143 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6144 | (unsigned long long) resync/2, |
| 6145 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | |
| 6147 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6148 | * dt: time from mark until now |
| 6149 | * db: blocks written from mark until now |
| 6150 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6151 | * |
| 6152 | * rt is a sector_t, so could be 32bit or 64bit. |
| 6153 | * So we divide before multiply in case it is 32bit and close |
| 6154 | * to the limit. |
| 6155 | * We scale the divisor (db) by 32 to avoid loosing precision |
| 6156 | * near the end of resync when the number of remaining sectors |
| 6157 | * is close to 'db'. |
| 6158 | * We then divide rt by 32 after multiplying by db to compensate. |
| 6159 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6160 | */ |
| 6161 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 6162 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6163 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 6164 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 6166 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 6167 | sector_div(rt, db/32+1); |
| 6168 | rt *= dt; |
| 6169 | rt >>= 5; |
| 6170 | |
| 6171 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 6172 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6174 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 6178 | { |
| 6179 | struct list_head *tmp; |
| 6180 | loff_t l = *pos; |
| 6181 | mddev_t *mddev; |
| 6182 | |
| 6183 | if (l >= 0x10000) |
| 6184 | return NULL; |
| 6185 | if (!l--) |
| 6186 | /* header */ |
| 6187 | return (void*)1; |
| 6188 | |
| 6189 | spin_lock(&all_mddevs_lock); |
| 6190 | list_for_each(tmp,&all_mddevs) |
| 6191 | if (!l--) { |
| 6192 | mddev = list_entry(tmp, mddev_t, all_mddevs); |
| 6193 | mddev_get(mddev); |
| 6194 | spin_unlock(&all_mddevs_lock); |
| 6195 | return mddev; |
| 6196 | } |
| 6197 | spin_unlock(&all_mddevs_lock); |
| 6198 | if (!l--) |
| 6199 | return (void*)2;/* tail */ |
| 6200 | return NULL; |
| 6201 | } |
| 6202 | |
| 6203 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 6204 | { |
| 6205 | struct list_head *tmp; |
| 6206 | mddev_t *next_mddev, *mddev = v; |
| 6207 | |
| 6208 | ++*pos; |
| 6209 | if (v == (void*)2) |
| 6210 | return NULL; |
| 6211 | |
| 6212 | spin_lock(&all_mddevs_lock); |
| 6213 | if (v == (void*)1) |
| 6214 | tmp = all_mddevs.next; |
| 6215 | else |
| 6216 | tmp = mddev->all_mddevs.next; |
| 6217 | if (tmp != &all_mddevs) |
| 6218 | next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs)); |
| 6219 | else { |
| 6220 | next_mddev = (void*)2; |
| 6221 | *pos = 0x10000; |
| 6222 | } |
| 6223 | spin_unlock(&all_mddevs_lock); |
| 6224 | |
| 6225 | if (v != (void*)1) |
| 6226 | mddev_put(mddev); |
| 6227 | return next_mddev; |
| 6228 | |
| 6229 | } |
| 6230 | |
| 6231 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 6232 | { |
| 6233 | mddev_t *mddev = v; |
| 6234 | |
| 6235 | if (mddev && v != (void*)1 && v != (void*)2) |
| 6236 | mddev_put(mddev); |
| 6237 | } |
| 6238 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6239 | struct mdstat_info { |
| 6240 | int event; |
| 6241 | }; |
| 6242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | static int md_seq_show(struct seq_file *seq, void *v) |
| 6244 | { |
| 6245 | mddev_t *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6246 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6247 | mdk_rdev_t *rdev; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6248 | struct mdstat_info *mi = seq->private; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6249 | struct bitmap *bitmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | |
| 6251 | if (v == (void*)1) { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6252 | struct mdk_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6253 | seq_printf(seq, "Personalities : "); |
| 6254 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6255 | list_for_each_entry(pers, &pers_list, list) |
| 6256 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6257 | |
| 6258 | spin_unlock(&pers_lock); |
| 6259 | seq_printf(seq, "\n"); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6260 | mi->event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6261 | return 0; |
| 6262 | } |
| 6263 | if (v == (void*)2) { |
| 6264 | status_unused(seq); |
| 6265 | return 0; |
| 6266 | } |
| 6267 | |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6268 | if (mddev_lock(mddev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6269 | return -EINTR; |
Ingo Molnar | 5dc5cf7 | 2006-04-20 02:43:23 -0700 | [diff] [blame] | 6270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6271 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 6272 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 6273 | mddev->pers ? "" : "in"); |
| 6274 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6275 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6276 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6277 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 6278 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6279 | seq_printf(seq, " %s", mddev->pers->name); |
| 6280 | } |
| 6281 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6282 | sectors = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6283 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6284 | char b[BDEVNAME_SIZE]; |
| 6285 | seq_printf(seq, " %s[%d]", |
| 6286 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6287 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6288 | seq_printf(seq, "(W)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6289 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | seq_printf(seq, "(F)"); |
| 6291 | continue; |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6292 | } else if (rdev->raid_disk < 0) |
| 6293 | seq_printf(seq, "(S)"); /* spare */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6294 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6295 | } |
| 6296 | |
| 6297 | if (!list_empty(&mddev->disks)) { |
| 6298 | if (mddev->pers) |
| 6299 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6300 | (unsigned long long) |
| 6301 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | else |
| 6303 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6304 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6305 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6306 | if (mddev->persistent) { |
| 6307 | if (mddev->major_version != 0 || |
| 6308 | mddev->minor_version != 90) { |
| 6309 | seq_printf(seq," super %d.%d", |
| 6310 | mddev->major_version, |
| 6311 | mddev->minor_version); |
| 6312 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6313 | } else if (mddev->external) |
| 6314 | seq_printf(seq, " super external:%s", |
| 6315 | mddev->metadata_type); |
| 6316 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 6317 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | |
| 6319 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6320 | mddev->pers->status(seq, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6321 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6322 | if (mddev->pers->sync_request) { |
| 6323 | if (mddev->curr_resync > 2) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6324 | status_resync(seq, mddev); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 6325 | seq_printf(seq, "\n "); |
| 6326 | } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2) |
| 6327 | seq_printf(seq, "\tresync=DELAYED\n "); |
| 6328 | else if (mddev->recovery_cp < MaxSector) |
| 6329 | seq_printf(seq, "\tresync=PENDING\n "); |
| 6330 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6331 | } else |
| 6332 | seq_printf(seq, "\n "); |
| 6333 | |
| 6334 | if ((bitmap = mddev->bitmap)) { |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6335 | unsigned long chunk_kb; |
| 6336 | unsigned long flags; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6337 | spin_lock_irqsave(&bitmap->lock, flags); |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6338 | chunk_kb = mddev->bitmap_info.chunksize >> 10; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6339 | seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], " |
| 6340 | "%lu%s chunk", |
| 6341 | bitmap->pages - bitmap->missing_pages, |
| 6342 | bitmap->pages, |
| 6343 | (bitmap->pages - bitmap->missing_pages) |
| 6344 | << (PAGE_SHIFT - 10), |
NeilBrown | 42a04b5 | 2009-12-14 12:49:53 +1100 | [diff] [blame] | 6345 | chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize, |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6346 | chunk_kb ? "KB" : "B"); |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6347 | if (bitmap->file) { |
| 6348 | seq_printf(seq, ", file: "); |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 6349 | seq_path(seq, &bitmap->file->f_path, " \t\n"); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6350 | } |
NeilBrown | 78d742d | 2005-06-21 17:17:15 -0700 | [diff] [blame] | 6351 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6352 | seq_printf(seq, "\n"); |
| 6353 | spin_unlock_irqrestore(&bitmap->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6354 | } |
| 6355 | |
| 6356 | seq_printf(seq, "\n"); |
| 6357 | } |
| 6358 | mddev_unlock(mddev); |
| 6359 | |
| 6360 | return 0; |
| 6361 | } |
| 6362 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 6363 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6364 | .start = md_seq_start, |
| 6365 | .next = md_seq_next, |
| 6366 | .stop = md_seq_stop, |
| 6367 | .show = md_seq_show, |
| 6368 | }; |
| 6369 | |
| 6370 | static int md_seq_open(struct inode *inode, struct file *file) |
| 6371 | { |
| 6372 | int error; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6373 | struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL); |
| 6374 | if (mi == NULL) |
| 6375 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6376 | |
| 6377 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6378 | if (error) |
| 6379 | kfree(mi); |
| 6380 | else { |
| 6381 | struct seq_file *p = file->private_data; |
| 6382 | p->private = mi; |
| 6383 | mi->event = atomic_read(&md_event_count); |
| 6384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | return error; |
| 6386 | } |
| 6387 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6388 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 6389 | { |
| 6390 | struct seq_file *m = filp->private_data; |
| 6391 | struct mdstat_info *mi = m->private; |
| 6392 | int mask; |
| 6393 | |
| 6394 | poll_wait(filp, &md_event_waiters, wait); |
| 6395 | |
| 6396 | /* always allow read */ |
| 6397 | mask = POLLIN | POLLRDNORM; |
| 6398 | |
| 6399 | if (mi->event != atomic_read(&md_event_count)) |
| 6400 | mask |= POLLERR | POLLPRI; |
| 6401 | return mask; |
| 6402 | } |
| 6403 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 6404 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 6405 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | .open = md_seq_open, |
| 6407 | .read = seq_read, |
| 6408 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 6409 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6410 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6411 | }; |
| 6412 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6413 | int register_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6414 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6415 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6416 | list_add_tail(&p->list, &pers_list); |
| 6417 | 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] | 6418 | spin_unlock(&pers_lock); |
| 6419 | return 0; |
| 6420 | } |
| 6421 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6422 | int unregister_md_personality(struct mdk_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6424 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6426 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6427 | spin_unlock(&pers_lock); |
| 6428 | return 0; |
| 6429 | } |
| 6430 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6431 | static int is_mddev_idle(mddev_t *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | { |
| 6433 | mdk_rdev_t * rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6435 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | |
| 6437 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6438 | rcu_read_lock(); |
| 6439 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6440 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6441 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 6442 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 6443 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 6444 | /* sync IO will cause sync_io to increase before the disk_stats |
| 6445 | * as sync_io is counted when a request starts, and |
| 6446 | * disk_stats is counted when it completes. |
| 6447 | * So resync activity will cause curr_events to be smaller than |
| 6448 | * when there was no such activity. |
| 6449 | * non-sync IO will cause disk_stat to increase without |
| 6450 | * increasing sync_io so curr_events will (eventually) |
| 6451 | * be larger than it was before. Once it becomes |
| 6452 | * substantially larger, the test below will cause |
| 6453 | * the array to appear non-idle, and resync will slow |
| 6454 | * down. |
| 6455 | * If there is a lot of outstanding resync activity when |
| 6456 | * we set last_event to curr_events, then all that activity |
| 6457 | * completing might cause the array to appear non-idle |
| 6458 | * and resync will be slowed down even though there might |
| 6459 | * not have been non-resync activity. This will only |
| 6460 | * happen once though. 'last_events' will soon reflect |
| 6461 | * the state where there is little or no outstanding |
| 6462 | * resync requests, and further resync activity will |
| 6463 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6464 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6466 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | rdev->last_events = curr_events; |
| 6468 | idle = 0; |
| 6469 | } |
| 6470 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6471 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | return idle; |
| 6473 | } |
| 6474 | |
| 6475 | void md_done_sync(mddev_t *mddev, int blocks, int ok) |
| 6476 | { |
| 6477 | /* another "blocks" (512byte) blocks have been synced */ |
| 6478 | atomic_sub(blocks, &mddev->recovery_active); |
| 6479 | wake_up(&mddev->recovery_wait); |
| 6480 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6481 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6482 | md_wakeup_thread(mddev->thread); |
| 6483 | // stop recovery, signal do_sync .... |
| 6484 | } |
| 6485 | } |
| 6486 | |
| 6487 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6488 | /* md_write_start(mddev, bi) |
| 6489 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6490 | * in superblock) before writing, schedule a superblock update |
| 6491 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6492 | */ |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6493 | void md_write_start(mddev_t *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6495 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6496 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6497 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6498 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6499 | BUG_ON(mddev->ro == 1); |
| 6500 | if (mddev->ro == 2) { |
| 6501 | /* need to switch to read/write */ |
| 6502 | mddev->ro = 0; |
| 6503 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6504 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 6505 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6506 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6507 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6508 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6509 | if (mddev->safemode == 1) |
| 6510 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6511 | if (mddev->in_sync) { |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6512 | spin_lock_irq(&mddev->write_lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6513 | if (mddev->in_sync) { |
| 6514 | mddev->in_sync = 0; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6515 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6516 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6517 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 6518 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 6519 | spin_unlock_irq(&mddev->write_lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 6520 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6521 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6522 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 6523 | wait_event(mddev->sb_wait, |
| 6524 | !test_bit(MD_CHANGE_CLEAN, &mddev->flags) && |
| 6525 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | } |
| 6527 | |
| 6528 | void md_write_end(mddev_t *mddev) |
| 6529 | { |
| 6530 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 6531 | if (mddev->safemode == 2) |
| 6532 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 6533 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6534 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 6535 | } |
| 6536 | } |
| 6537 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6538 | /* md_allow_write(mddev) |
| 6539 | * Calling this ensures that the array is marked 'active' so that writes |
| 6540 | * may proceed without blocking. It is important to call this before |
| 6541 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 6542 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6543 | * |
| 6544 | * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock |
| 6545 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6546 | */ |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6547 | int md_allow_write(mddev_t *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6548 | { |
| 6549 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6550 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6551 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6552 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 6553 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6554 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6555 | |
| 6556 | spin_lock_irq(&mddev->write_lock); |
| 6557 | if (mddev->in_sync) { |
| 6558 | mddev->in_sync = 0; |
| 6559 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 6560 | if (mddev->safemode_delay && |
| 6561 | mddev->safemode == 0) |
| 6562 | mddev->safemode = 1; |
| 6563 | spin_unlock_irq(&mddev->write_lock); |
| 6564 | md_update_sb(mddev, 0); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 6565 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6566 | } else |
| 6567 | spin_unlock_irq(&mddev->write_lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6568 | |
| 6569 | if (test_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 6570 | return -EAGAIN; |
| 6571 | else |
| 6572 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 6573 | } |
| 6574 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 6575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6576 | #define SYNC_MARKS 10 |
| 6577 | #define SYNC_MARK_STEP (3*HZ) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6578 | void md_do_sync(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6579 | { |
| 6580 | mddev_t *mddev2; |
| 6581 | unsigned int currspeed = 0, |
| 6582 | window; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6583 | sector_t max_sectors,j, io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6584 | unsigned long mark[SYNC_MARKS]; |
| 6585 | sector_t mark_cnt[SYNC_MARKS]; |
| 6586 | int last_mark,m; |
| 6587 | struct list_head *tmp; |
| 6588 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6589 | int skipped = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6590 | mdk_rdev_t *rdev; |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6591 | char *desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | |
| 6593 | /* just incase thread restarts... */ |
| 6594 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 6595 | return; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6596 | if (mddev->ro) /* never try to sync a read-only array */ |
| 6597 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6598 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6599 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6600 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 6601 | desc = "data-check"; |
| 6602 | else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6603 | desc = "requested-resync"; |
| 6604 | else |
| 6605 | desc = "resync"; |
| 6606 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6607 | desc = "reshape"; |
| 6608 | else |
| 6609 | desc = "recovery"; |
| 6610 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | /* we overload curr_resync somewhat here. |
| 6612 | * 0 == not engaged in resync at all |
| 6613 | * 2 == checking that there is no conflict with another sync |
| 6614 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 6615 | * commense |
| 6616 | * other == active in resync - this many blocks |
| 6617 | * |
| 6618 | * Before starting a resync we must have set curr_resync to |
| 6619 | * 2, and then checked that every "conflicting" array has curr_resync |
| 6620 | * less than ours. When we find one that is the same or higher |
| 6621 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 6622 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 6623 | * This will mean we have to start checking from the beginning again. |
| 6624 | * |
| 6625 | */ |
| 6626 | |
| 6627 | do { |
| 6628 | mddev->curr_resync = 2; |
| 6629 | |
| 6630 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6631 | if (kthread_should_stop()) |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 6632 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 6633 | |
| 6634 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6635 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 6636 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6637 | if (mddev2 == mddev) |
| 6638 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 6639 | if (!mddev->parallel_resync |
| 6640 | && mddev2->curr_resync |
| 6641 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6642 | DEFINE_WAIT(wq); |
| 6643 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 6644 | /* arbitrarily yield */ |
| 6645 | mddev->curr_resync = 1; |
| 6646 | wake_up(&resync_wait); |
| 6647 | } |
| 6648 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 6649 | /* no need to wait here, we can wait the next |
| 6650 | * time 'round when curr_resync == 2 |
| 6651 | */ |
| 6652 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6653 | /* We need to wait 'interruptible' so as not to |
| 6654 | * contribute to the load average, and not to |
| 6655 | * be caught by 'softlockup' |
| 6656 | */ |
| 6657 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | 787453c | 2005-11-08 21:39:43 -0800 | [diff] [blame] | 6658 | if (!kthread_should_stop() && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 6659 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6660 | printk(KERN_INFO "md: delaying %s of %s" |
| 6661 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6663 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6664 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 6665 | if (signal_pending(current)) |
| 6666 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6667 | schedule(); |
| 6668 | finish_wait(&resync_wait, &wq); |
| 6669 | goto try_again; |
| 6670 | } |
| 6671 | finish_wait(&resync_wait, &wq); |
| 6672 | } |
| 6673 | } |
| 6674 | } while (mddev->curr_resync < 2); |
| 6675 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6676 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6677 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6678 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6679 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6680 | */ |
| 6681 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 6682 | mddev->resync_mismatches = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6683 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6684 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6685 | j = mddev->resync_min; |
| 6686 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6687 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 6688 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6689 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6690 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6691 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6692 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6693 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6694 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6695 | rcu_read_lock(); |
| 6696 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6697 | if (rdev->raid_disk >= 0 && |
| 6698 | !test_bit(Faulty, &rdev->flags) && |
| 6699 | !test_bit(In_sync, &rdev->flags) && |
| 6700 | rdev->recovery_offset < j) |
| 6701 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6702 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6703 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6704 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6705 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 6706 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 6707 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 6708 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6709 | "(but not more than %d KB/sec) for %s.\n", |
| 6710 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6712 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6713 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6714 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6715 | for (m = 0; m < SYNC_MARKS; m++) { |
| 6716 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6717 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | } |
| 6719 | last_mark = 0; |
| 6720 | mddev->resync_mark = mark[last_mark]; |
| 6721 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 6722 | |
| 6723 | /* |
| 6724 | * Tune reconstruction: |
| 6725 | */ |
| 6726 | window = 32*(PAGE_SIZE/512); |
| 6727 | printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n", |
| 6728 | window/2,(unsigned long long) max_sectors/2); |
| 6729 | |
| 6730 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | last_check = 0; |
| 6732 | |
| 6733 | if (j>2) { |
| 6734 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6735 | "md: resuming %s of %s from checkpoint.\n", |
| 6736 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | mddev->curr_resync = j; |
| 6738 | } |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6739 | mddev->curr_resync_completed = mddev->curr_resync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6740 | |
| 6741 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6742 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6743 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6744 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6745 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 6746 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 6747 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 6748 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 6749 | > (max_sectors >> 4)) || |
| 6750 | (j - mddev->curr_resync_completed)*2 |
| 6751 | >= mddev->resync_max - mddev->curr_resync_completed |
| 6752 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6753 | /* time to update curr_resync_completed */ |
| 6754 | blk_unplug(mddev->queue); |
| 6755 | wait_event(mddev->recovery_wait, |
| 6756 | atomic_read(&mddev->recovery_active) == 0); |
| 6757 | mddev->curr_resync_completed = |
| 6758 | mddev->curr_resync; |
| 6759 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6760 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6761 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6762 | |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 6763 | while (j >= mddev->resync_max && !kthread_should_stop()) { |
| 6764 | /* As this condition is controlled by user-space, |
| 6765 | * we can block indefinitely, so use '_interruptible' |
| 6766 | * to avoid triggering warnings. |
| 6767 | */ |
| 6768 | flush_signals(current); /* just in case */ |
| 6769 | wait_event_interruptible(mddev->recovery_wait, |
| 6770 | mddev->resync_max > j |
| 6771 | || kthread_should_stop()); |
| 6772 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 6773 | |
| 6774 | if (kthread_should_stop()) |
| 6775 | goto interrupted; |
| 6776 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6777 | sectors = mddev->pers->sync_request(mddev, j, &skipped, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6778 | currspeed < speed_min(mddev)); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6779 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6780 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | goto out; |
| 6782 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6783 | |
| 6784 | if (!skipped) { /* actual IO requested */ |
| 6785 | io_sectors += sectors; |
| 6786 | atomic_add(sectors, &mddev->recovery_active); |
| 6787 | } |
| 6788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | j += sectors; |
| 6790 | if (j>1) mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 6791 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6792 | if (last_check == 0) |
| 6793 | /* this is the earliers that rebuilt will be |
| 6794 | * visible in /proc/mdstat |
| 6795 | */ |
| 6796 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6797 | |
| 6798 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6799 | continue; |
| 6800 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6801 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6802 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6803 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6804 | break; |
| 6805 | |
| 6806 | repeat: |
| 6807 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 6808 | /* step marks */ |
| 6809 | int next = (last_mark+1) % SYNC_MARKS; |
| 6810 | |
| 6811 | mddev->resync_mark = mark[next]; |
| 6812 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 6813 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6814 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6815 | last_mark = next; |
| 6816 | } |
| 6817 | |
| 6818 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6819 | if (kthread_should_stop()) |
| 6820 | goto interrupted; |
| 6821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | |
| 6823 | /* |
| 6824 | * this loop exits only if either when we are slower than |
| 6825 | * the 'hard' speed limit, or the system was IO-idle for |
| 6826 | * a jiffy. |
| 6827 | * the system might be non-idle CPU-wise, but we only care |
| 6828 | * about not overloading the IO subsystem. (things like an |
| 6829 | * e2fsck being done on the RAID array should execute fast) |
| 6830 | */ |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6831 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | cond_resched(); |
| 6833 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6834 | currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2 |
| 6835 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 6837 | if (currspeed > speed_min(mddev)) { |
| 6838 | if ((currspeed > speed_max(mddev)) || |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 6839 | !is_mddev_idle(mddev, 0)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 6840 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6841 | goto repeat; |
| 6842 | } |
| 6843 | } |
| 6844 | } |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6845 | printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6846 | /* |
| 6847 | * this also signals 'finished resyncing' to md_stop |
| 6848 | */ |
| 6849 | out: |
Alan D. Brunelle | 2ad8b1e | 2007-11-07 14:26:56 -0500 | [diff] [blame] | 6850 | blk_unplug(mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6851 | |
| 6852 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 6853 | |
| 6854 | /* tell personality that we are finished */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6855 | mddev->pers->sync_request(mddev, max_sectors, &skipped, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6857 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6858 | mddev->curr_resync > 2) { |
| 6859 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 6860 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6861 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 6862 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 6863 | "md: checkpointing %s of %s.\n", |
| 6864 | desc, mdname(mddev)); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6865 | mddev->recovery_cp = mddev->curr_resync; |
| 6866 | } |
| 6867 | } else |
| 6868 | mddev->recovery_cp = MaxSector; |
| 6869 | } else { |
| 6870 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6871 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6872 | rcu_read_lock(); |
| 6873 | list_for_each_entry_rcu(rdev, &mddev->disks, same_set) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6874 | if (rdev->raid_disk >= 0 && |
| 6875 | !test_bit(Faulty, &rdev->flags) && |
| 6876 | !test_bit(In_sync, &rdev->flags) && |
| 6877 | rdev->recovery_offset < mddev->curr_resync) |
| 6878 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 6879 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 6880 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6881 | } |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 6882 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6884 | skip: |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 6885 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6886 | /* We completed so min/max setting can be forgotten if used. */ |
| 6887 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6888 | mddev->resync_min = 0; |
| 6889 | mddev->resync_max = MaxSector; |
| 6890 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 6891 | mddev->resync_min = mddev->curr_resync_completed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | mddev->curr_resync = 0; |
NeilBrown | efa5933 | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 6893 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6894 | mddev->curr_resync_completed = 0; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6895 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | wake_up(&resync_wait); |
| 6897 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
| 6898 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6899 | return; |
| 6900 | |
| 6901 | interrupted: |
| 6902 | /* |
| 6903 | * got a signal, exit. |
| 6904 | */ |
| 6905 | printk(KERN_INFO |
| 6906 | "md: md_do_sync() got signal ... exiting\n"); |
| 6907 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 6908 | goto out; |
| 6909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6910 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6911 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6912 | |
| 6913 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6914 | static int remove_and_add_spares(mddev_t *mddev) |
| 6915 | { |
| 6916 | mdk_rdev_t *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6917 | int spares = 0; |
| 6918 | |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6919 | mddev->curr_resync_completed = 0; |
| 6920 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6921 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6922 | if (rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 6923 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6924 | (test_bit(Faulty, &rdev->flags) || |
| 6925 | ! test_bit(In_sync, &rdev->flags)) && |
| 6926 | atomic_read(&rdev->nr_pending)==0) { |
| 6927 | if (mddev->pers->hot_remove_disk( |
| 6928 | mddev, rdev->raid_disk)==0) { |
| 6929 | char nm[20]; |
| 6930 | sprintf(nm,"rd%d", rdev->raid_disk); |
| 6931 | sysfs_remove_link(&mddev->kobj, nm); |
| 6932 | rdev->raid_disk = -1; |
| 6933 | } |
| 6934 | } |
| 6935 | |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 6936 | if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) { |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6937 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6938 | if (rdev->raid_disk >= 0 && |
Dan Williams | e542713 | 2008-07-28 23:28:06 -0700 | [diff] [blame] | 6939 | !test_bit(In_sync, &rdev->flags) && |
| 6940 | !test_bit(Blocked, &rdev->flags)) |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6941 | spares++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6942 | if (rdev->raid_disk < 0 |
| 6943 | && !test_bit(Faulty, &rdev->flags)) { |
| 6944 | rdev->recovery_offset = 0; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6945 | if (mddev->pers-> |
| 6946 | hot_add_disk(mddev, rdev) == 0) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6947 | char nm[20]; |
| 6948 | sprintf(nm, "rd%d", rdev->raid_disk); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6949 | if (sysfs_create_link(&mddev->kobj, |
| 6950 | &rdev->kobj, nm)) |
| 6951 | printk(KERN_WARNING |
| 6952 | "md: cannot register " |
| 6953 | "%s for %s\n", |
| 6954 | nm, mdname(mddev)); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6955 | spares++; |
| 6956 | md_new_event(mddev); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 6957 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6958 | } else |
| 6959 | break; |
| 6960 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6961 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 6962 | } |
| 6963 | return spares; |
| 6964 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | /* |
| 6966 | * This routine is regularly called by all per-raid-array threads to |
| 6967 | * deal with generic issues like resync and super-block update. |
| 6968 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 6969 | * need this as they never do any recovery or update the superblock. |
| 6970 | * |
| 6971 | * It does not do any resync itself, but rather "forks" off other threads |
| 6972 | * to do that as needed. |
| 6973 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 6974 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 6975 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6976 | * and wakeups up this thread which will reap the thread and finish up. |
| 6977 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 6978 | * |
| 6979 | * The overall approach is: |
| 6980 | * 1/ if the superblock needs updating, update it. |
| 6981 | * 2/ If a recovery thread is running, don't do anything else. |
| 6982 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 6983 | * 4/ If there are any faulty devices, remove them. |
| 6984 | * 5/ If array is degraded, try to add spares devices |
| 6985 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 6986 | */ |
| 6987 | void md_check_recovery(mddev_t *mddev) |
| 6988 | { |
| 6989 | mdk_rdev_t *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6990 | |
| 6991 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 6992 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 6993 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6994 | |
| 6995 | if (mddev->ro) |
| 6996 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 6997 | |
| 6998 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 6999 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7000 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 7001 | mdname(mddev)); |
| 7002 | mddev->safemode = 2; |
| 7003 | } |
| 7004 | flush_signals(current); |
| 7005 | } |
| 7006 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7007 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 7008 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7009 | if ( ! ( |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7010 | (mddev->flags && !mddev->external) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7011 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7012 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7013 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7014 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 7015 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7016 | )) |
| 7017 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7018 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 7019 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7020 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7021 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7022 | if (mddev->ro) { |
| 7023 | /* Only thing we do on a ro array is remove |
| 7024 | * failed devices. |
| 7025 | */ |
| 7026 | remove_and_add_spares(mddev); |
| 7027 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7028 | goto unlock; |
| 7029 | } |
| 7030 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7031 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7032 | int did_change = 0; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7033 | spin_lock_irq(&mddev->write_lock); |
| 7034 | if (mddev->safemode && |
| 7035 | !atomic_read(&mddev->writes_pending) && |
| 7036 | !mddev->in_sync && |
| 7037 | mddev->recovery_cp == MaxSector) { |
| 7038 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7039 | did_change = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7040 | if (mddev->persistent) |
| 7041 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 7042 | } |
| 7043 | if (mddev->safemode == 1) |
| 7044 | mddev->safemode = 0; |
| 7045 | spin_unlock_irq(&mddev->write_lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7046 | if (did_change) |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 7047 | sysfs_notify_dirent(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7048 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 7049 | |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7050 | if (mddev->flags) |
| 7051 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7052 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7053 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 7054 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 7055 | /* resync/recovery still happening */ |
| 7056 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7057 | goto unlock; |
| 7058 | } |
| 7059 | if (mddev->sync_thread) { |
| 7060 | /* resync has finished, collect result */ |
| 7061 | md_unregister_thread(mddev->sync_thread); |
| 7062 | mddev->sync_thread = NULL; |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7063 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 7064 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7065 | /* success...*/ |
| 7066 | /* activate any spares */ |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 7067 | if (mddev->pers->spare_active(mddev)) |
| 7068 | sysfs_notify(&mddev->kobj, NULL, |
| 7069 | "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7070 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7071 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7072 | mddev->pers->finish_reshape) |
| 7073 | mddev->pers->finish_reshape(mddev); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7074 | md_update_sb(mddev, 1); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7075 | |
| 7076 | /* if array is no-longer degraded, then any saved_raid_disk |
| 7077 | * information must be scrapped |
| 7078 | */ |
| 7079 | if (!mddev->degraded) |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7080 | list_for_each_entry(rdev, &mddev->disks, same_set) |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 7081 | rdev->saved_raid_disk = -1; |
| 7082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7083 | mddev->recovery = 0; |
| 7084 | /* flag recovery needed just to double check */ |
| 7085 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7086 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7087 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7088 | goto unlock; |
| 7089 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7090 | /* Set RUNNING before clearing NEEDED to avoid |
| 7091 | * any transients in the value of "sync_action". |
| 7092 | */ |
| 7093 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7094 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7095 | /* Clear some bits that don't mean anything, but |
| 7096 | * might be left set |
| 7097 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7098 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7099 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7100 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7101 | if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
| 7102 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7103 | /* no recovery is running. |
| 7104 | * remove any failed drives, then |
| 7105 | * add spares if possible. |
| 7106 | * Spare are also removed and re-added, to allow |
| 7107 | * the personality to fail the re-add. |
| 7108 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7109 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7110 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7111 | if (mddev->pers->check_reshape == NULL || |
| 7112 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7113 | /* Cannot proceed */ |
| 7114 | goto unlock; |
| 7115 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7116 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 7117 | } else if ((spares = remove_and_add_spares(mddev))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7118 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7119 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 7120 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7121 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7122 | } else if (mddev->recovery_cp < MaxSector) { |
| 7123 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7124 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7125 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 7126 | /* nothing to be done ... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | goto unlock; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7129 | if (mddev->pers->sync_request) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 7130 | if (spares && mddev->bitmap && ! mddev->bitmap->file) { |
| 7131 | /* We are adding a device or devices to an array |
| 7132 | * which has the bitmap stored on all devices. |
| 7133 | * So make sure all bitmap pages get written |
| 7134 | */ |
| 7135 | bitmap_write_all(mddev->bitmap); |
| 7136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7137 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 7138 | mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7139 | "resync"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | if (!mddev->sync_thread) { |
| 7141 | printk(KERN_ERR "%s: could not start resync" |
| 7142 | " thread...\n", |
| 7143 | mdname(mddev)); |
| 7144 | /* leave the spares where they are, it shouldn't hurt */ |
| 7145 | mddev->recovery = 0; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7146 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7147 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7148 | sysfs_notify_dirent(mddev->sysfs_action); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7149 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | } |
| 7151 | unlock: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7152 | if (!mddev->sync_thread) { |
| 7153 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7154 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 7155 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 7156 | if (mddev->sysfs_action) |
| 7157 | sysfs_notify_dirent(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7159 | mddev_unlock(mddev); |
| 7160 | } |
| 7161 | } |
| 7162 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7163 | void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev) |
| 7164 | { |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 7165 | sysfs_notify_dirent(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7166 | wait_event_timeout(rdev->blocked_wait, |
| 7167 | !test_bit(Blocked, &rdev->flags), |
| 7168 | msecs_to_jiffies(5000)); |
| 7169 | rdev_dec_pending(rdev, mddev); |
| 7170 | } |
| 7171 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 7172 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7173 | static int md_notify_reboot(struct notifier_block *this, |
| 7174 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7175 | { |
| 7176 | struct list_head *tmp; |
| 7177 | mddev_t *mddev; |
| 7178 | |
| 7179 | if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) { |
| 7180 | |
| 7181 | printk(KERN_INFO "md: stopping all md devices.\n"); |
| 7182 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7183 | for_each_mddev(mddev, tmp) |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7184 | if (mddev_trylock(mddev)) { |
NeilBrown | 2b25000 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 7185 | /* Force a switch to readonly even array |
| 7186 | * appears to still be in use. Hence |
| 7187 | * the '100'. |
| 7188 | */ |
NeilBrown | a4bd82d0 | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 7189 | md_set_readonly(mddev, 100); |
Neil Brown | c71d488 | 2006-05-26 10:39:25 +1000 | [diff] [blame] | 7190 | mddev_unlock(mddev); |
| 7191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | /* |
| 7193 | * certain more exotic SCSI devices are known to be |
| 7194 | * volatile wrt too early system reboots. While the |
| 7195 | * right place to handle this issue is the given |
| 7196 | * driver, we do want to have a safe RAID driver ... |
| 7197 | */ |
| 7198 | mdelay(1000*1); |
| 7199 | } |
| 7200 | return NOTIFY_DONE; |
| 7201 | } |
| 7202 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7203 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | .notifier_call = md_notify_reboot, |
| 7205 | .next = NULL, |
| 7206 | .priority = INT_MAX, /* before any real devices */ |
| 7207 | }; |
| 7208 | |
| 7209 | static void md_geninit(void) |
| 7210 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
| 7212 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 7213 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7214 | } |
| 7215 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 7216 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7217 | { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7218 | if (register_blkdev(MD_MAJOR, "md")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7219 | return -1; |
| 7220 | if ((mdp_major=register_blkdev(0, "mdp"))<=0) { |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7221 | unregister_blkdev(MD_MAJOR, "md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7222 | return -1; |
| 7223 | } |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7224 | blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7225 | md_probe, NULL, NULL); |
| 7226 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7227 | md_probe, NULL, NULL); |
| 7228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7229 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 7230 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7231 | |
| 7232 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7233 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | |
| 7237 | #ifndef MODULE |
| 7238 | |
| 7239 | /* |
| 7240 | * Searches all registered partitions for autorun RAID arrays |
| 7241 | * at boot time. |
| 7242 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7243 | |
| 7244 | static LIST_HEAD(all_detected_devices); |
| 7245 | struct detected_devices_node { |
| 7246 | struct list_head list; |
| 7247 | dev_t dev; |
| 7248 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7249 | |
| 7250 | void md_autodetect_dev(dev_t dev) |
| 7251 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7252 | struct detected_devices_node *node_detected_dev; |
| 7253 | |
| 7254 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 7255 | if (node_detected_dev) { |
| 7256 | node_detected_dev->dev = dev; |
| 7257 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 7258 | } else { |
| 7259 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 7260 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 7261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7262 | } |
| 7263 | |
| 7264 | |
| 7265 | static void autostart_arrays(int part) |
| 7266 | { |
| 7267 | mdk_rdev_t *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7268 | struct detected_devices_node *node_detected_dev; |
| 7269 | dev_t dev; |
| 7270 | int i_scanned, i_passed; |
| 7271 | |
| 7272 | i_scanned = 0; |
| 7273 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7274 | |
| 7275 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 7276 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7277 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 7278 | i_scanned++; |
| 7279 | node_detected_dev = list_entry(all_detected_devices.next, |
| 7280 | struct detected_devices_node, list); |
| 7281 | list_del(&node_detected_dev->list); |
| 7282 | dev = node_detected_dev->dev; |
| 7283 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 7284 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7285 | if (IS_ERR(rdev)) |
| 7286 | continue; |
| 7287 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7288 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7289 | MD_BUG(); |
| 7290 | continue; |
| 7291 | } |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 7292 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7293 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7294 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7295 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 7296 | |
| 7297 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 7298 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7299 | |
| 7300 | autorun_devices(part); |
| 7301 | } |
| 7302 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 7303 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7304 | |
| 7305 | static __exit void md_exit(void) |
| 7306 | { |
| 7307 | mddev_t *mddev; |
| 7308 | struct list_head *tmp; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 7309 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7310 | blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 7311 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7312 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7313 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7314 | unregister_blkdev(mdp_major, "mdp"); |
| 7315 | unregister_reboot_notifier(&md_notifier); |
| 7316 | unregister_sysctl_table(raid_table_header); |
| 7317 | remove_proc_entry("mdstat", NULL); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7318 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7319 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7320 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7321 | } |
| 7322 | } |
| 7323 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 7324 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7325 | module_exit(md_exit) |
| 7326 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7327 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 7328 | { |
| 7329 | return sprintf(buffer, "%d", start_readonly); |
| 7330 | } |
| 7331 | static int set_ro(const char *val, struct kernel_param *kp) |
| 7332 | { |
| 7333 | char *e; |
| 7334 | int num = simple_strtoul(val, &e, 10); |
| 7335 | if (*val && (*e == '\0' || *e == '\n')) { |
| 7336 | start_readonly = num; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 7337 | return 0; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7338 | } |
| 7339 | return -EINVAL; |
| 7340 | } |
| 7341 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 7342 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 7343 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7344 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7345 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7346 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7347 | EXPORT_SYMBOL(register_md_personality); |
| 7348 | EXPORT_SYMBOL(unregister_md_personality); |
| 7349 | EXPORT_SYMBOL(md_error); |
| 7350 | EXPORT_SYMBOL(md_done_sync); |
| 7351 | EXPORT_SYMBOL(md_write_start); |
| 7352 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7353 | EXPORT_SYMBOL(md_register_thread); |
| 7354 | EXPORT_SYMBOL(md_unregister_thread); |
| 7355 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7356 | EXPORT_SYMBOL(md_check_recovery); |
| 7357 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7358 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 7359 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 7360 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |