Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | md.c : Multiple Devices driver for Linux |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 39 | #include <linux/fs.h> |
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> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 46 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 47 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 48 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 49 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 50 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 51 | #include <linux/raid/md_p.h> |
| 52 | #include <linux/raid/md_u.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 53 | #include <linux/slab.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 55 | #include "bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 56 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 59 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
| 61 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 62 | /* pers_list is a list of registered personalities protected |
| 63 | * by pers_lock. |
| 64 | * pers_lock does extra service to protect accesses to |
| 65 | * mddev->thread when the mutex cannot be held. |
| 66 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 67 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static DEFINE_SPINLOCK(pers_lock); |
| 69 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 70 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 71 | EXPORT_SYMBOL(md_cluster_ops); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 72 | struct module *md_cluster_mod; |
| 73 | EXPORT_SYMBOL(md_cluster_mod); |
| 74 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 75 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 76 | static struct workqueue_struct *md_wq; |
| 77 | static struct workqueue_struct *md_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 78 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 79 | static int remove_and_add_spares(struct mddev *mddev, |
| 80 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 81 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 84 | * Default number of read corrections we'll attempt on an rdev |
| 85 | * before ejecting it from the array. We divide the read error |
| 86 | * count by 2 for every hour elapsed between read errors. |
| 87 | */ |
| 88 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
| 89 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 91 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 92 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 93 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 95 | * speed limit - in case reconstruction slows down your system despite |
| 96 | * idle IO detection. |
| 97 | * |
| 98 | * 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] | 99 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
| 101 | |
| 102 | static int sysctl_speed_limit_min = 1000; |
| 103 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 104 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 105 | { |
| 106 | return mddev->sync_speed_min ? |
| 107 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 108 | } |
| 109 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 110 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 111 | { |
| 112 | return mddev->sync_speed_max ? |
| 113 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | static struct ctl_table_header *raid_table_header; |
| 117 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 118 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .procname = "speed_limit_min", |
| 121 | .data = &sysctl_speed_limit_min, |
| 122 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 123 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 124 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }, |
| 126 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | .procname = "speed_limit_max", |
| 128 | .data = &sysctl_speed_limit_max, |
| 129 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 130 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 131 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 136 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .procname = "raid", |
| 139 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 140 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | .child = raid_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 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 146 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .procname = "dev", |
| 149 | .maxlen = 0, |
| 150 | .mode = 0555, |
| 151 | .child = raid_dir_table, |
| 152 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 153 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 156 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 158 | static int start_readonly; |
| 159 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 160 | /* bio_clone_mddev |
| 161 | * like bio_clone, but with a local bio set |
| 162 | */ |
| 163 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 164 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 165 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 166 | { |
| 167 | struct bio *b; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 168 | |
| 169 | if (!mddev || !mddev->bio_set) |
| 170 | return bio_alloc(gfp_mask, nr_iovecs); |
| 171 | |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 172 | b = bio_alloc_bioset(gfp_mask, nr_iovecs, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 173 | if (!b) |
| 174 | return NULL; |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 175 | return b; |
| 176 | } |
| 177 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 178 | |
| 179 | struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 180 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 181 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 182 | if (!mddev || !mddev->bio_set) |
| 183 | return bio_clone(bio, gfp_mask); |
| 184 | |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 185 | return bio_clone_bioset(bio, gfp_mask, mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 186 | } |
| 187 | EXPORT_SYMBOL_GPL(bio_clone_mddev); |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 190 | * We have a system wide 'event count' that is incremented |
| 191 | * on any 'interesting' event, and readers of /proc/mdstat |
| 192 | * can use 'poll' or 'select' to find out when the event |
| 193 | * count increases. |
| 194 | * |
| 195 | * Events are: |
| 196 | * start array, stop array, error, add device, remove device, |
| 197 | * start build, activate spare |
| 198 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 199 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 200 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 201 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 202 | { |
| 203 | atomic_inc(&md_event_count); |
| 204 | wake_up(&md_event_waiters); |
| 205 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 206 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 207 | |
| 208 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | * Enables to iterate over all existing md arrays |
| 210 | * all_mddevs_lock protects this list. |
| 211 | */ |
| 212 | static LIST_HEAD(all_mddevs); |
| 213 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | /* |
| 216 | * iterates through all used mddevs in the system. |
| 217 | * We take care to grab the all_mddevs_lock whenever navigating |
| 218 | * the list, and to always hold a refcount when unlocked. |
| 219 | * Any code which breaks out of this loop while own |
| 220 | * a reference to the current mddev and must mddev_put it. |
| 221 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 222 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 224 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 225 | _tmp = all_mddevs.next; \ |
| 226 | _mddev = NULL;}); \ |
| 227 | ({ if (_tmp != &all_mddevs) \ |
| 228 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 230 | if (_mddev) mddev_put(_mddev); \ |
| 231 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 232 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 234 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | ) |
| 236 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 237 | /* Rather than calling directly into the personality make_request function, |
| 238 | * IO requests come here first so that we can check if the device is |
| 239 | * being suspended pending a reconfiguration. |
| 240 | * We hold a refcount over the call to ->make_request. By the time that |
| 241 | * call has finished, the bio has been linked into some internal structure |
| 242 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 243 | */ |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 244 | static blk_qc_t md_make_request(struct request_queue *q, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 246 | const int rw = bio_data_dir(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 247 | struct mddev *mddev = q->queuedata; |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 248 | unsigned int sectors; |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 249 | int cpu; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 250 | |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 251 | blk_queue_split(q, &bio, q->bio_split); |
| 252 | |
NeilBrown | 274d8cb | 2016-01-04 16:16:58 +1100 | [diff] [blame] | 253 | if (mddev == NULL || mddev->pers == NULL) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 254 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 255 | return BLK_QC_T_NONE; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 256 | } |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 257 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 258 | if (bio_sectors(bio) != 0) |
| 259 | bio->bi_error = -EROFS; |
| 260 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 261 | return BLK_QC_T_NONE; |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 262 | } |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 263 | smp_rmb(); /* Ensure implications of 'active' are visible */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 264 | rcu_read_lock(); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 265 | if (mddev->suspended) { |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 266 | DEFINE_WAIT(__wait); |
| 267 | for (;;) { |
| 268 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 269 | TASK_UNINTERRUPTIBLE); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 270 | if (!mddev->suspended) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 271 | break; |
| 272 | rcu_read_unlock(); |
| 273 | schedule(); |
| 274 | rcu_read_lock(); |
| 275 | } |
| 276 | finish_wait(&mddev->sb_wait, &__wait); |
| 277 | } |
| 278 | atomic_inc(&mddev->active_io); |
| 279 | rcu_read_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 280 | |
Chris Mason | e91ece5 | 2011-02-07 19:21:48 -0500 | [diff] [blame] | 281 | /* |
| 282 | * save the sectors now since our bio can |
| 283 | * go away inside make_request |
| 284 | */ |
| 285 | sectors = bio_sectors(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 286 | mddev->pers->make_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 287 | |
Gu Zheng | 74672d0 | 2015-04-03 08:44:47 +0800 | [diff] [blame] | 288 | cpu = part_stat_lock(); |
| 289 | part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]); |
| 290 | part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors); |
| 291 | part_stat_unlock(); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 292 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 293 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 294 | wake_up(&mddev->sb_wait); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 295 | |
| 296 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 299 | /* mddev_suspend makes sure no new requests are submitted |
| 300 | * to the device, and that any requests that have been submitted |
| 301 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 302 | * Once mddev_detach() is called and completes, the module will be |
| 303 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 304 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 305 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 306 | { |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 307 | if (mddev->suspended++) |
| 308 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 309 | synchronize_rcu(); |
| 310 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 311 | mddev->pers->quiesce(mddev, 1); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 312 | |
| 313 | del_timer_sync(&mddev->safemode_timer); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 314 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 315 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 316 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 317 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 318 | { |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 319 | if (--mddev->suspended) |
| 320 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 321 | wake_up(&mddev->sb_wait); |
| 322 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 323 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 324 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 325 | md_wakeup_thread(mddev->thread); |
| 326 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 327 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 328 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 329 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 330 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 331 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 332 | struct md_personality *pers = mddev->pers; |
| 333 | int ret = 0; |
| 334 | |
| 335 | rcu_read_lock(); |
| 336 | if (mddev->suspended) |
| 337 | ret = 1; |
| 338 | else if (pers && pers->congested) |
| 339 | ret = pers->congested(mddev, bits); |
| 340 | rcu_read_unlock(); |
| 341 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 342 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 343 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 344 | static int md_congested(void *data, int bits) |
| 345 | { |
| 346 | struct mddev *mddev = data; |
| 347 | return mddev_congested(mddev, bits); |
| 348 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 349 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 350 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 351 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 352 | */ |
| 353 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 354 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 355 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 356 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 357 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 358 | |
| 359 | rdev_dec_pending(rdev, mddev); |
| 360 | |
| 361 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 362 | /* The pre-request flush has finished */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 363 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 364 | } |
| 365 | bio_put(bio); |
| 366 | } |
| 367 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 368 | static void md_submit_flush_data(struct work_struct *ws); |
| 369 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 370 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 371 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 372 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 373 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 374 | |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 375 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 376 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 377 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 378 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 379 | if (rdev->raid_disk >= 0 && |
| 380 | !test_bit(Faulty, &rdev->flags)) { |
| 381 | /* Take two references, one is dropped |
| 382 | * when request finishes, one after |
| 383 | * we reclaim rcu_read_lock |
| 384 | */ |
| 385 | struct bio *bi; |
| 386 | atomic_inc(&rdev->nr_pending); |
| 387 | atomic_inc(&rdev->nr_pending); |
| 388 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 389 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 390 | bi->bi_end_io = md_end_flush; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 391 | bi->bi_private = rdev; |
| 392 | bi->bi_bdev = rdev->bdev; |
| 393 | atomic_inc(&mddev->flush_pending); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 394 | submit_bio(WRITE_FLUSH, bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 395 | rcu_read_lock(); |
| 396 | rdev_dec_pending(rdev, mddev); |
| 397 | } |
| 398 | rcu_read_unlock(); |
NeilBrown | a7a07e6 | 2010-12-09 16:04:25 +1100 | [diff] [blame] | 399 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 400 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 401 | } |
| 402 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 403 | static void md_submit_flush_data(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 404 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 405 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 406 | struct bio *bio = mddev->flush_bio; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 407 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 408 | if (bio->bi_iter.bi_size == 0) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 409 | /* an empty barrier - all done */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 410 | bio_endio(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 411 | else { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 412 | bio->bi_rw &= ~REQ_FLUSH; |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 413 | mddev->pers->make_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 414 | } |
NeilBrown | 2b74e12 | 2010-12-09 15:59:01 +1100 | [diff] [blame] | 415 | |
| 416 | mddev->flush_bio = NULL; |
| 417 | wake_up(&mddev->sb_wait); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 418 | } |
| 419 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 420 | void md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 421 | { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 422 | spin_lock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 423 | wait_event_lock_irq(mddev->sb_wait, |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 424 | !mddev->flush_bio, |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 425 | mddev->lock); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 426 | mddev->flush_bio = bio; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 427 | spin_unlock_irq(&mddev->lock); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 428 | |
NeilBrown | a035fc3 | 2010-12-09 16:17:51 +1100 | [diff] [blame] | 429 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 430 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 431 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 432 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 433 | |
NeilBrown | 74018dc | 2012-07-31 09:08:15 +0200 | [diff] [blame] | 434 | void md_unplug(struct blk_plug_cb *cb, bool from_schedule) |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 435 | { |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 436 | struct mddev *mddev = cb->data; |
| 437 | md_wakeup_thread(mddev->thread); |
| 438 | kfree(cb); |
NeilBrown | 97658cd | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 439 | } |
NeilBrown | 9cbb175 | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 440 | EXPORT_SYMBOL(md_unplug); |
NeilBrown | 2ac8740 | 2010-06-01 19:37:29 +1000 | [diff] [blame] | 441 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 442 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
| 444 | atomic_inc(&mddev->active); |
| 445 | return mddev; |
| 446 | } |
| 447 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 448 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 449 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 450 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 452 | struct bio_set *bs = NULL; |
| 453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 455 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 456 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 457 | mddev->ctime == 0 && !mddev->hold_active) { |
| 458 | /* Array is not configured at all, and not held active, |
| 459 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 460 | list_del_init(&mddev->all_mddevs); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 461 | bs = mddev->bio_set; |
| 462 | mddev->bio_set = NULL; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 463 | if (mddev->gendisk) { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 464 | /* We did a probe so need to clean up. Call |
| 465 | * queue_work inside the spinlock so that |
| 466 | * flush_workqueue() after mddev_find will |
| 467 | * succeed in waiting for the work to be done. |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 468 | */ |
| 469 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 470 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 471 | } else |
| 472 | kfree(mddev); |
| 473 | } |
| 474 | spin_unlock(&all_mddevs_lock); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 475 | if (bs) |
| 476 | bioset_free(bs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 479 | static void md_safemode_timeout(unsigned long data); |
| 480 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 481 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 482 | { |
| 483 | mutex_init(&mddev->open_mutex); |
| 484 | mutex_init(&mddev->reconfig_mutex); |
| 485 | mutex_init(&mddev->bitmap_info.mutex); |
| 486 | INIT_LIST_HEAD(&mddev->disks); |
| 487 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 488 | setup_timer(&mddev->safemode_timer, md_safemode_timeout, |
| 489 | (unsigned long) mddev); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 490 | atomic_set(&mddev->active, 1); |
| 491 | atomic_set(&mddev->openers, 0); |
| 492 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 493 | spin_lock_init(&mddev->lock); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 494 | atomic_set(&mddev->flush_pending, 0); |
| 495 | init_waitqueue_head(&mddev->sb_wait); |
| 496 | init_waitqueue_head(&mddev->recovery_wait); |
| 497 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 498 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 499 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 500 | mddev->resync_min = 0; |
| 501 | mddev->resync_max = MaxSector; |
| 502 | mddev->level = LEVEL_NONE; |
| 503 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 504 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 505 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 506 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 508 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 510 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 511 | unit &= ~((1<<MdpMinorShift)-1); |
| 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | retry: |
| 514 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 516 | if (unit) { |
| 517 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 518 | if (mddev->unit == unit) { |
| 519 | mddev_get(mddev); |
| 520 | spin_unlock(&all_mddevs_lock); |
| 521 | kfree(new); |
| 522 | return mddev; |
| 523 | } |
| 524 | |
| 525 | if (new) { |
| 526 | list_add(&new->all_mddevs, &all_mddevs); |
| 527 | spin_unlock(&all_mddevs_lock); |
| 528 | new->hold_active = UNTIL_IOCTL; |
| 529 | return new; |
| 530 | } |
| 531 | } else if (new) { |
| 532 | /* find an unused unit number */ |
| 533 | static int next_minor = 512; |
| 534 | int start = next_minor; |
| 535 | int is_free = 0; |
| 536 | int dev = 0; |
| 537 | while (!is_free) { |
| 538 | dev = MKDEV(MD_MAJOR, next_minor); |
| 539 | next_minor++; |
| 540 | if (next_minor > MINORMASK) |
| 541 | next_minor = 0; |
| 542 | if (next_minor == start) { |
| 543 | /* Oh dear, all in use. */ |
| 544 | spin_unlock(&all_mddevs_lock); |
| 545 | kfree(new); |
| 546 | return NULL; |
| 547 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 548 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 549 | is_free = 1; |
| 550 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 551 | if (mddev->unit == dev) { |
| 552 | is_free = 0; |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | new->unit = dev; |
| 557 | new->md_minor = MINOR(dev); |
| 558 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | list_add(&new->all_mddevs, &all_mddevs); |
| 560 | spin_unlock(&all_mddevs_lock); |
| 561 | return new; |
| 562 | } |
| 563 | spin_unlock(&all_mddevs_lock); |
| 564 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 565 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | if (!new) |
| 567 | return NULL; |
| 568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | new->unit = unit; |
| 570 | if (MAJOR(unit) == MD_MAJOR) |
| 571 | new->md_minor = MINOR(unit); |
| 572 | else |
| 573 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 574 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 575 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | goto retry; |
| 578 | } |
| 579 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 580 | static struct attribute_group md_redundancy_group; |
| 581 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 582 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 584 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 585 | /* These cannot be removed under reconfig_mutex as |
| 586 | * an access to the files will try to take reconfig_mutex |
| 587 | * while holding the file unremovable, which leads to |
| 588 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 589 | * So hold set sysfs_active while the remove in happeing, |
| 590 | * and anything else which might set ->to_remove or my |
| 591 | * otherwise change the sysfs namespace will fail with |
| 592 | * -EBUSY if sysfs_active is still set. |
| 593 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 594 | * test it under the same mutex to ensure its correct value |
| 595 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 596 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 597 | struct attribute_group *to_remove = mddev->to_remove; |
| 598 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 599 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 600 | mutex_unlock(&mddev->reconfig_mutex); |
| 601 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 602 | if (mddev->kobj.sd) { |
| 603 | if (to_remove != &md_redundancy_group) |
| 604 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 605 | if (mddev->pers == NULL || |
| 606 | mddev->pers->sync_request == NULL) { |
| 607 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 608 | if (mddev->sysfs_action) |
| 609 | sysfs_put(mddev->sysfs_action); |
| 610 | mddev->sysfs_action = NULL; |
| 611 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 612 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 613 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 614 | } else |
| 615 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 617 | /* As we've dropped the mutex we need a spinlock to |
| 618 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 619 | */ |
| 620 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 621 | md_wakeup_thread(mddev->thread); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 622 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 624 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 626 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 627 | { |
| 628 | struct md_rdev *rdev; |
| 629 | |
| 630 | rdev_for_each_rcu(rdev, mddev) |
| 631 | if (rdev->desc_nr == nr) |
| 632 | return rdev; |
| 633 | |
| 634 | return NULL; |
| 635 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 636 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 637 | |
| 638 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 640 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 642 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | if (rdev->bdev->bd_dev == dev) |
| 644 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | return NULL; |
| 647 | } |
| 648 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 649 | static struct md_rdev *find_rdev_rcu(struct mddev *mddev, dev_t dev) |
| 650 | { |
| 651 | struct md_rdev *rdev; |
| 652 | |
| 653 | rdev_for_each_rcu(rdev, mddev) |
| 654 | if (rdev->bdev->bd_dev == dev) |
| 655 | return rdev; |
| 656 | |
| 657 | return NULL; |
| 658 | } |
| 659 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 660 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 661 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 662 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 663 | list_for_each_entry(pers, &pers_list, list) { |
| 664 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 665 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 666 | if (strcmp(pers->name, clevel)==0) |
| 667 | return pers; |
| 668 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 669 | return NULL; |
| 670 | } |
| 671 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 672 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 673 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 675 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 676 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | } |
| 678 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 679 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | rdev->sb_page = alloc_page(GFP_KERNEL); |
| 682 | if (!rdev->sb_page) { |
| 683 | printk(KERN_ALERT "md: out of memory.\n"); |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 684 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 690 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
| 692 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 693 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | rdev->sb_loaded = 0; |
| 695 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 696 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 697 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 699 | if (rdev->bb_page) { |
| 700 | put_page(rdev->bb_page); |
| 701 | rdev->bb_page = NULL; |
| 702 | } |
NeilBrown | 4fa2f32 | 2012-05-22 13:55:01 +1000 | [diff] [blame] | 703 | kfree(rdev->badblocks.page); |
| 704 | rdev->badblocks.page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 706 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 708 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 709 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 710 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 711 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 712 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 713 | if (bio->bi_error) { |
| 714 | printk("md: super_written gets error=%d\n", bio->bi_error); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 715 | md_error(mddev, rdev); |
NeilBrown | 3a0f5bb | 2006-10-03 01:16:03 -0700 | [diff] [blame] | 716 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 717 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 718 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 719 | wake_up(&mddev->sb_wait); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 720 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 721 | } |
| 722 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 723 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 724 | sector_t sector, int size, struct page *page) |
| 725 | { |
| 726 | /* write first size bytes of page to sector of rdev |
| 727 | * Increment mddev->pending_writes before returning |
| 728 | * and decrement it on completion, waking up sb_wait |
| 729 | * if zero is reached. |
| 730 | * If an error occurred, call md_error |
| 731 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 732 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 733 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 734 | bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 735 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 736 | bio_add_page(bio, page, size, 0); |
| 737 | bio->bi_private = rdev; |
| 738 | bio->bi_end_io = super_written; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 739 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 740 | atomic_inc(&mddev->pending_writes); |
Namhyung Kim | a5bf4df | 2011-08-25 14:43:34 +1000 | [diff] [blame] | 741 | submit_bio(WRITE_FLUSH_FUA, bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 742 | } |
| 743 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 744 | void md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 745 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 746 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 747 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 748 | } |
| 749 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 750 | int sync_page_io(struct md_rdev *rdev, sector_t sector, int size, |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 751 | struct page *page, int rw, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | { |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 753 | struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | int ret; |
| 755 | |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 756 | bio->bi_bdev = (metadata_op && rdev->meta_bdev) ? |
| 757 | rdev->meta_bdev : rdev->bdev; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 758 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 759 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 760 | else if (rdev->mddev->reshape_position != MaxSector && |
| 761 | (rdev->mddev->reshape_backwards == |
| 762 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 763 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 764 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 765 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | bio_add_page(bio, page, size, 0); |
Kent Overstreet | c170bbb | 2013-11-24 16:32:22 -0700 | [diff] [blame] | 767 | submit_bio_wait(rw, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 769 | ret = !bio->bi_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | bio_put(bio); |
| 771 | return ret; |
| 772 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 773 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 775 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | { |
| 777 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 778 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | if (rdev->sb_loaded) |
| 780 | return 0; |
| 781 | |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 782 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | goto fail; |
| 784 | rdev->sb_loaded = 1; |
| 785 | return 0; |
| 786 | |
| 787 | fail: |
| 788 | printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n", |
| 789 | bdevname(rdev->bdev,b)); |
| 790 | return -EINVAL; |
| 791 | } |
| 792 | |
| 793 | static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 794 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 795 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 796 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 797 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 798 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
| 802 | { |
| 803 | int ret; |
| 804 | mdp_super_t *tmp1, *tmp2; |
| 805 | |
| 806 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 807 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 808 | |
| 809 | if (!tmp1 || !tmp2) { |
| 810 | ret = 0; |
Andre Noll | 35020f1 | 2008-03-23 15:10:33 +0100 | [diff] [blame] | 811 | printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | goto abort; |
| 813 | } |
| 814 | |
| 815 | *tmp1 = *sb1; |
| 816 | *tmp2 = *sb2; |
| 817 | |
| 818 | /* |
| 819 | * nr_disks is not constant |
| 820 | */ |
| 821 | tmp1->nr_disks = 0; |
| 822 | tmp2->nr_disks = 0; |
| 823 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 824 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 826 | kfree(tmp1); |
| 827 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | return ret; |
| 829 | } |
| 830 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 831 | static u32 md_csum_fold(u32 csum) |
| 832 | { |
| 833 | csum = (csum & 0xffff) + (csum >> 16); |
| 834 | return (csum & 0xffff) + (csum >> 16); |
| 835 | } |
| 836 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 837 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 839 | u64 newcsum = 0; |
| 840 | u32 *sb32 = (u32*)sb; |
| 841 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | unsigned int disk_csum, csum; |
| 843 | |
| 844 | disk_csum = sb->sb_csum; |
| 845 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 846 | |
| 847 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 848 | newcsum += sb32[i]; |
| 849 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 850 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 851 | #ifdef CONFIG_ALPHA |
| 852 | /* This used to use csum_partial, which was wrong for several |
| 853 | * reasons including that different results are returned on |
| 854 | * different architectures. It isn't critical that we get exactly |
| 855 | * the same return value as before (we always csum_fold before |
| 856 | * testing, and that removes any differences). However as we |
| 857 | * know that csum_partial always returned a 16bit value on |
| 858 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 859 | */ |
| 860 | sb->sb_csum = md_csum_fold(disk_csum); |
| 861 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 863 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | return csum; |
| 865 | } |
| 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | /* |
| 868 | * Handle superblock details. |
| 869 | * We want to be able to handle multiple superblock formats |
| 870 | * so we have a common interface to them all, and an array of |
| 871 | * different handlers. |
| 872 | * We rely on user-space to write the initial superblock, and support |
| 873 | * reading and updating of superblocks. |
| 874 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 875 | * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | * loads and validates a superblock on dev. |
| 877 | * if refdev != NULL, compare superblocks on both devices |
| 878 | * Return: |
| 879 | * 0 - dev has a superblock that is compatible with refdev |
| 880 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 881 | * so dev should be used as the refdev in future |
| 882 | * -EINVAL superblock incompatible or invalid |
| 883 | * -othererror e.g. -EIO |
| 884 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 885 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | * Verify that dev is acceptable into mddev. |
| 887 | * The first time, mddev->raid_disks will be 0, and data from |
| 888 | * dev should be merged in. Subsequent calls check that dev |
| 889 | * is new enough. Return 0 or -EINVAL |
| 890 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 891 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | * Update the superblock for rdev with data in mddev |
| 893 | * This does not write to disc. |
| 894 | * |
| 895 | */ |
| 896 | |
| 897 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 898 | char *name; |
| 899 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 900 | int (*load_super)(struct md_rdev *rdev, |
| 901 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 902 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 903 | int (*validate_super)(struct mddev *mddev, |
| 904 | struct md_rdev *rdev); |
| 905 | void (*sync_super)(struct mddev *mddev, |
| 906 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 907 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 908 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 909 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 910 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | }; |
| 912 | |
| 913 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 914 | * Check that the given mddev has no bitmap. |
| 915 | * |
| 916 | * This function is called from the run method of all personalities that do not |
| 917 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 918 | * has a bitmap. Otherwise, it returns 0. |
| 919 | * |
| 920 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 921 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 922 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 923 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 924 | return 0; |
| 925 | printk(KERN_ERR "%s: bitmaps are not supported for %s\n", |
| 926 | mdname(mddev), mddev->pers->name); |
| 927 | return 1; |
| 928 | } |
| 929 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 930 | |
| 931 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 932 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 934 | static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | { |
| 936 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 937 | mdp_super_t *sb; |
| 938 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
| 940 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 941 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | * it's at the end of the disk. |
| 943 | * |
| 944 | * It also happens to be a multiple of 4Kb. |
| 945 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 946 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 948 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | if (ret) return ret; |
| 950 | |
| 951 | ret = -EINVAL; |
| 952 | |
| 953 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 954 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
| 956 | if (sb->md_magic != MD_SB_MAGIC) { |
| 957 | printk(KERN_ERR "md: invalid raid superblock magic on %s\n", |
| 958 | b); |
| 959 | goto abort; |
| 960 | } |
| 961 | |
| 962 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 963 | sb->minor_version < 90 || |
| 964 | sb->minor_version > 91) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | printk(KERN_WARNING "Bad version number %d.%d on %s\n", |
| 966 | sb->major_version, sb->minor_version, |
| 967 | b); |
| 968 | goto abort; |
| 969 | } |
| 970 | |
| 971 | if (sb->raid_disks <= 0) |
| 972 | goto abort; |
| 973 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 974 | 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] | 975 | printk(KERN_WARNING "md: invalid superblock checksum on %s\n", |
| 976 | b); |
| 977 | goto abort; |
| 978 | } |
| 979 | |
| 980 | rdev->preferred_minor = sb->md_minor; |
| 981 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 982 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 983 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 984 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
| 986 | if (sb->level == LEVEL_MULTIPATH) |
| 987 | rdev->desc_nr = -1; |
| 988 | else |
| 989 | rdev->desc_nr = sb->this_disk.number; |
| 990 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 991 | if (!refdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 993 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 995 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | if (!uuid_equal(refsb, sb)) { |
| 997 | printk(KERN_WARNING "md: %s has different UUID to %s\n", |
| 998 | b, bdevname(refdev->bdev,b2)); |
| 999 | goto abort; |
| 1000 | } |
| 1001 | if (!sb_equal(refsb, sb)) { |
| 1002 | printk(KERN_WARNING "md: %s has same UUID" |
| 1003 | " but different superblock to %s\n", |
| 1004 | b, bdevname(refdev->bdev, b2)); |
| 1005 | goto abort; |
| 1006 | } |
| 1007 | ev1 = md_event(sb); |
| 1008 | ev2 = md_event(refsb); |
| 1009 | if (ev1 > ev2) |
| 1010 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1011 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | ret = 0; |
| 1013 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1014 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1015 | /* Limit to 4TB as metadata cannot record more than that. |
| 1016 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1017 | * record this size) |
| 1018 | */ |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1019 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)rdev->sectors >= (2ULL << 32) && |
| 1020 | sb->level >= 1) |
| 1021 | rdev->sectors = (sector_t)(2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1023 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1024 | /* "this cannot possibly happen" ... */ |
| 1025 | ret = -EINVAL; |
| 1026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | abort: |
| 1028 | return ret; |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * validate_super for 0.90.0 |
| 1033 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1034 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | { |
| 1036 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1037 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1038 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1040 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1041 | clear_bit(Faulty, &rdev->flags); |
| 1042 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1043 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1044 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1045 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | if (mddev->raid_disks == 0) { |
| 1047 | mddev->major_version = 0; |
| 1048 | mddev->minor_version = sb->minor_version; |
| 1049 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1050 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1051 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | mddev->ctime = sb->ctime; |
| 1053 | mddev->utime = sb->utime; |
| 1054 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1055 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | mddev->layout = sb->layout; |
| 1057 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1058 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1059 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1060 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1061 | mddev->bitmap_info.space = 0; |
| 1062 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1063 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1064 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1065 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1067 | if (mddev->minor_version >= 91) { |
| 1068 | mddev->reshape_position = sb->reshape_position; |
| 1069 | mddev->delta_disks = sb->delta_disks; |
| 1070 | mddev->new_level = sb->new_level; |
| 1071 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1072 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1073 | if (mddev->delta_disks < 0) |
| 1074 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1075 | } else { |
| 1076 | mddev->reshape_position = MaxSector; |
| 1077 | mddev->delta_disks = 0; |
| 1078 | mddev->new_level = mddev->level; |
| 1079 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1080 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1084 | mddev->recovery_cp = MaxSector; |
| 1085 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1086 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | sb->events_lo == sb->cp_events_lo) { |
| 1088 | mddev->recovery_cp = sb->recovery_cp; |
| 1089 | } else |
| 1090 | mddev->recovery_cp = 0; |
| 1091 | } |
| 1092 | |
| 1093 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1094 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1095 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1096 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1097 | |
| 1098 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1099 | |
| 1100 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1101 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1102 | mddev->bitmap_info.offset = |
| 1103 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1104 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1105 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1106 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1107 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1108 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1109 | /* Insist on good event counter while assembling, except |
| 1110 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1112 | if (sb->disks[rdev->desc_nr].state & ( |
| 1113 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1114 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1115 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1116 | } else if (mddev->bitmap) { |
| 1117 | /* if adding to array with a bitmap, then we can accept an |
| 1118 | * older device ... but not too old. |
| 1119 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1120 | if (ev1 < mddev->bitmap->events_cleared) |
| 1121 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1122 | if (ev1 < mddev->events) |
| 1123 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1124 | } else { |
| 1125 | if (ev1 < mddev->events) |
| 1126 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1127 | return 0; |
| 1128 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | desc = sb->disks + rdev->desc_nr; |
| 1132 | |
| 1133 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1134 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1135 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1136 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1137 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1139 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1140 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1141 | /* active but not in sync implies recovery up to |
| 1142 | * reshape position. We don't know exactly where |
| 1143 | * that is, so set to zero for now */ |
| 1144 | if (mddev->minor_version >= 91) { |
| 1145 | rdev->recovery_offset = 0; |
| 1146 | rdev->raid_disk = desc->raid_disk; |
| 1147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1149 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1150 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1151 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1152 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * sync_super for 0.90.0 |
| 1158 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1159 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | { |
| 1161 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1162 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | /* make rdev->sb match mddev data.. |
| 1166 | * |
| 1167 | * 1/ zero out disks |
| 1168 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1169 | * 3/ any empty disks < next_spare become removed |
| 1170 | * |
| 1171 | * disks[0] gets initialised to REMOVED because |
| 1172 | * we cannot be sure from other fields if it has |
| 1173 | * been initialised or not. |
| 1174 | */ |
| 1175 | int i; |
| 1176 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1177 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1178 | rdev->sb_size = MD_SB_BYTES; |
| 1179 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1180 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | |
| 1182 | memset(sb, 0, sizeof(*sb)); |
| 1183 | |
| 1184 | sb->md_magic = MD_SB_MAGIC; |
| 1185 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | sb->patch_version = mddev->patch_version; |
| 1187 | sb->gvalid_words = 0; /* ignored */ |
| 1188 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1189 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1190 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1191 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1192 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1193 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1195 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | sb->raid_disks = mddev->raid_disks; |
| 1197 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1198 | sb->not_persistent = 0; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1199 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | sb->state = 0; |
| 1201 | sb->events_hi = (mddev->events>>32); |
| 1202 | sb->events_lo = (u32)mddev->events; |
| 1203 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1204 | if (mddev->reshape_position == MaxSector) |
| 1205 | sb->minor_version = 90; |
| 1206 | else { |
| 1207 | sb->minor_version = 91; |
| 1208 | sb->reshape_position = mddev->reshape_position; |
| 1209 | sb->new_level = mddev->new_level; |
| 1210 | sb->delta_disks = mddev->delta_disks; |
| 1211 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1212 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1213 | } |
| 1214 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | if (mddev->in_sync) |
| 1216 | { |
| 1217 | sb->recovery_cp = mddev->recovery_cp; |
| 1218 | sb->cp_events_hi = (mddev->events>>32); |
| 1219 | sb->cp_events_lo = (u32)mddev->events; |
| 1220 | if (mddev->recovery_cp == MaxSector) |
| 1221 | sb->state = (1<< MD_SB_CLEAN); |
| 1222 | } else |
| 1223 | sb->recovery_cp = 0; |
| 1224 | |
| 1225 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1226 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1228 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1229 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1232 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1234 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1235 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1236 | |
| 1237 | if (rdev2->raid_disk >= 0 && |
| 1238 | sb->minor_version >= 91) |
| 1239 | /* we have nowhere to store the recovery_offset, |
| 1240 | * but if it is not below the reshape_position, |
| 1241 | * we can piggy-back on that. |
| 1242 | */ |
| 1243 | is_active = 1; |
| 1244 | if (rdev2->raid_disk < 0 || |
| 1245 | test_bit(Faulty, &rdev2->flags)) |
| 1246 | is_active = 0; |
| 1247 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1248 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1250 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1251 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | d = &sb->disks[rdev2->desc_nr]; |
| 1253 | nr_disks++; |
| 1254 | d->number = rdev2->desc_nr; |
| 1255 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1256 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1257 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | d->raid_disk = rdev2->raid_disk; |
| 1259 | else |
| 1260 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1261 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1263 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1265 | if (test_bit(In_sync, &rdev2->flags)) |
| 1266 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | active++; |
| 1268 | working++; |
| 1269 | } else { |
| 1270 | d->state = 0; |
| 1271 | spare++; |
| 1272 | working++; |
| 1273 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1274 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1275 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1278 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1279 | mdp_disk_t *d = &sb->disks[i]; |
| 1280 | if (d->state == 0 && d->number == 0) { |
| 1281 | d->number = i; |
| 1282 | d->raid_disk = i; |
| 1283 | d->state = (1<<MD_DISK_REMOVED); |
| 1284 | d->state |= (1<<MD_DISK_FAULTY); |
| 1285 | failed++; |
| 1286 | } |
| 1287 | } |
| 1288 | sb->nr_disks = nr_disks; |
| 1289 | sb->active_disks = active; |
| 1290 | sb->working_disks = working; |
| 1291 | sb->failed_disks = failed; |
| 1292 | sb->spare_disks = spare; |
| 1293 | |
| 1294 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1295 | sb->sb_csum = calc_sb_csum(sb); |
| 1296 | } |
| 1297 | |
| 1298 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1299 | * rdev_size_change for 0.90.0 |
| 1300 | */ |
| 1301 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1302 | super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1303 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1304 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1305 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1306 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1307 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1308 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1309 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1310 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1311 | /* Limit to 4TB as metadata cannot record more than that. |
| 1312 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1313 | */ |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1314 | if (IS_ENABLED(CONFIG_LBDAF) && (u64)num_sectors >= (2ULL << 32) && |
| 1315 | rdev->mddev->level >= 1) |
| 1316 | num_sectors = (sector_t)(2ULL << 32) - 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1317 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1318 | rdev->sb_page); |
| 1319 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1320 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1321 | } |
| 1322 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1323 | static int |
| 1324 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1325 | { |
| 1326 | /* non-zero offset changes not possible with v0.90 */ |
| 1327 | return new_offset == 0; |
| 1328 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1329 | |
| 1330 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | * version 1 superblock |
| 1332 | */ |
| 1333 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1334 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1336 | __le32 disk_csum; |
| 1337 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | unsigned long long newcsum; |
| 1339 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1340 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
| 1342 | disk_csum = sb->sb_csum; |
| 1343 | sb->sb_csum = 0; |
| 1344 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1345 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | newcsum += le32_to_cpu(*isuper++); |
| 1347 | |
| 1348 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1349 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
| 1351 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1352 | sb->sb_csum = disk_csum; |
| 1353 | return cpu_to_le32(csum); |
| 1354 | } |
| 1355 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1356 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 1357 | int acknowledged); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1358 | static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | { |
| 1360 | struct mdp_superblock_1 *sb; |
| 1361 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1362 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1363 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1365 | int bmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | |
| 1367 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1368 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | * It is always aligned to a 4K boundary and |
| 1370 | * depeding on minor_version, it can be: |
| 1371 | * 0: At least 8K, but less than 12K, from end of device |
| 1372 | * 1: At start of device |
| 1373 | * 2: 4K from start of device. |
| 1374 | */ |
| 1375 | switch(minor_version) { |
| 1376 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1377 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1378 | sb_start -= 8*2; |
| 1379 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | break; |
| 1381 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1382 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | break; |
| 1384 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1385 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | break; |
| 1387 | default: |
| 1388 | return -EINVAL; |
| 1389 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1390 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1392 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1393 | * and it is safe to read 4k, so we do that |
| 1394 | */ |
| 1395 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | if (ret) return ret; |
| 1397 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1398 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
| 1400 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1401 | sb->major_version != cpu_to_le32(1) || |
| 1402 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1403 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1404 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | return -EINVAL; |
| 1406 | |
| 1407 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
| 1408 | printk("md: invalid superblock checksum on %s\n", |
| 1409 | bdevname(rdev->bdev,b)); |
| 1410 | return -EINVAL; |
| 1411 | } |
| 1412 | if (le64_to_cpu(sb->data_size) < 10) { |
| 1413 | printk("md: data_size too small on %s\n", |
| 1414 | bdevname(rdev->bdev,b)); |
| 1415 | return -EINVAL; |
| 1416 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1417 | if (sb->pad0 || |
| 1418 | sb->pad3[0] || |
| 1419 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1420 | /* Some padding is non-zero, might be a new feature */ |
| 1421 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | rdev->preferred_minor = 0xffff; |
| 1424 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1425 | rdev->new_data_offset = rdev->data_offset; |
| 1426 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1427 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1428 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1429 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1431 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1432 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1433 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1434 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1435 | |
| 1436 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1437 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1438 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1439 | if (minor_version |
| 1440 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1441 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1442 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1443 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1444 | rdev->desc_nr = -1; |
| 1445 | else |
| 1446 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1447 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1448 | if (!rdev->bb_page) { |
| 1449 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1450 | if (!rdev->bb_page) |
| 1451 | return -ENOMEM; |
| 1452 | } |
| 1453 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1454 | rdev->badblocks.count == 0) { |
| 1455 | /* need to load the bad block list. |
| 1456 | * Currently we limit it to one page. |
| 1457 | */ |
| 1458 | s32 offset; |
| 1459 | sector_t bb_sector; |
| 1460 | u64 *bbp; |
| 1461 | int i; |
| 1462 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1463 | if (sectors > (PAGE_SIZE / 512)) |
| 1464 | return -EINVAL; |
| 1465 | offset = le32_to_cpu(sb->bblog_offset); |
| 1466 | if (offset == 0) |
| 1467 | return -EINVAL; |
| 1468 | bb_sector = (long long)offset; |
| 1469 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
| 1470 | rdev->bb_page, READ, true)) |
| 1471 | return -EIO; |
| 1472 | bbp = (u64 *)page_address(rdev->bb_page); |
| 1473 | rdev->badblocks.shift = sb->bblog_shift; |
| 1474 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1475 | u64 bb = le64_to_cpu(*bbp); |
| 1476 | int count = bb & (0x3ff); |
| 1477 | u64 sector = bb >> 10; |
| 1478 | sector <<= sb->bblog_shift; |
| 1479 | count <<= sb->bblog_shift; |
| 1480 | if (bb + 1 == 0) |
| 1481 | break; |
| 1482 | if (md_set_badblocks(&rdev->badblocks, |
| 1483 | sector, count, 1) == 0) |
| 1484 | return -EINVAL; |
| 1485 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1486 | } else if (sb->bblog_offset != 0) |
| 1487 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1488 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1489 | if (!refdev) { |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1490 | ret = 1; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1491 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1493 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | |
| 1495 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1496 | sb->level != refsb->level || |
| 1497 | sb->layout != refsb->layout || |
| 1498 | sb->chunksize != refsb->chunksize) { |
| 1499 | printk(KERN_WARNING "md: %s has strangely different" |
| 1500 | " superblock to %s\n", |
| 1501 | bdevname(rdev->bdev,b), |
| 1502 | bdevname(refdev->bdev,b2)); |
| 1503 | return -EINVAL; |
| 1504 | } |
| 1505 | ev1 = le64_to_cpu(sb->events); |
| 1506 | ev2 = le64_to_cpu(refsb->events); |
| 1507 | |
| 1508 | if (ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1509 | ret = 1; |
| 1510 | else |
| 1511 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1513 | if (minor_version) { |
| 1514 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1515 | sectors -= rdev->data_offset; |
| 1516 | } else |
| 1517 | sectors = rdev->sb_start; |
| 1518 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1520 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1521 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1524 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1526 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1527 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1529 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1530 | clear_bit(Faulty, &rdev->flags); |
| 1531 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1532 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1533 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | if (mddev->raid_disks == 0) { |
| 1536 | mddev->major_version = 1; |
| 1537 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1538 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1539 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1540 | mddev->ctime = le64_to_cpu(sb->ctime); |
| 1541 | mddev->utime = le64_to_cpu(sb->utime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1543 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | mddev->layout = le32_to_cpu(sb->layout); |
| 1545 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1546 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1547 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1548 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1549 | mddev->bitmap_info.space = 0; |
| 1550 | /* Default location for bitmap is 1K after superblock |
| 1551 | * using 3K - total of 4K |
| 1552 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1553 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1554 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1555 | mddev->reshape_backwards = 0; |
| 1556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1558 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1559 | |
| 1560 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1561 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1562 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1563 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1564 | mddev->bitmap_info.offset = |
| 1565 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1566 | /* Metadata doesn't record how much space is available. |
| 1567 | * For 1.0, we assume we can use up to the superblock |
| 1568 | * if before, else to 4K beyond superblock. |
| 1569 | * For others, assume no change is possible. |
| 1570 | */ |
| 1571 | if (mddev->minor_version > 0) |
| 1572 | mddev->bitmap_info.space = 0; |
| 1573 | else if (mddev->bitmap_info.offset > 0) |
| 1574 | mddev->bitmap_info.space = |
| 1575 | 8 - mddev->bitmap_info.offset; |
| 1576 | else |
| 1577 | mddev->bitmap_info.space = |
| 1578 | -mddev->bitmap_info.offset; |
| 1579 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1580 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1581 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1582 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1583 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1584 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1585 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1586 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1587 | if (mddev->delta_disks < 0 || |
| 1588 | (mddev->delta_disks == 0 && |
| 1589 | (le32_to_cpu(sb->feature_map) |
| 1590 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1591 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1592 | } else { |
| 1593 | mddev->reshape_position = MaxSector; |
| 1594 | mddev->delta_disks = 0; |
| 1595 | mddev->new_level = mddev->level; |
| 1596 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1597 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame^] | 1600 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) { |
| 1601 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
| 1602 | if (mddev->recovery_cp == MaxSector) |
| 1603 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); |
| 1604 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1605 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1606 | /* Insist of good event counter while assembling, except for |
| 1607 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1609 | if (rdev->desc_nr >= 0 && |
| 1610 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1611 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1612 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1613 | if (ev1 < mddev->events) |
| 1614 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1615 | } else if (mddev->bitmap) { |
| 1616 | /* If adding to array with a bitmap, then we can accept an |
| 1617 | * older device, but not too old. |
| 1618 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1619 | if (ev1 < mddev->bitmap->events_cleared) |
| 1620 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1621 | if (ev1 < mddev->events) |
| 1622 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1623 | } else { |
| 1624 | if (ev1 < mddev->events) |
| 1625 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1626 | return 0; |
| 1627 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1629 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1630 | if (rdev->desc_nr < 0 || |
| 1631 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1632 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1633 | rdev->desc_nr = -1; |
| 1634 | } else |
| 1635 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1637 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1639 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1640 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1642 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1643 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1644 | /* journal device without journal feature */ |
| 1645 | printk(KERN_WARNING |
| 1646 | "md: journal device provided without journal feature, ignoring the device\n"); |
| 1647 | return -EINVAL; |
| 1648 | } |
| 1649 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1650 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1651 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1652 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1654 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1655 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1656 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1657 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1658 | if (!(le32_to_cpu(sb->feature_map) & |
| 1659 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1660 | rdev->saved_raid_disk = -1; |
| 1661 | } else |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1662 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | rdev->raid_disk = role; |
| 1664 | break; |
| 1665 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1666 | if (sb->devflags & WriteMostly1) |
| 1667 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1668 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1669 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1670 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1671 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | return 0; |
| 1674 | } |
| 1675 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1676 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | { |
| 1678 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1679 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | int max_dev, i; |
| 1681 | /* make rdev->sb match mddev and rdev data. */ |
| 1682 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1683 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | |
| 1685 | sb->feature_map = 0; |
| 1686 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1687 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1689 | |
| 1690 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1691 | sb->events = cpu_to_le64(mddev->events); |
| 1692 | if (mddev->in_sync) |
| 1693 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 1694 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 1695 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | else |
| 1697 | sb->resync_offset = cpu_to_le64(0); |
| 1698 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1699 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1700 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1701 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1702 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1703 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1704 | sb->level = cpu_to_le32(mddev->level); |
| 1705 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1706 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1707 | if (test_bit(WriteMostly, &rdev->flags)) |
| 1708 | sb->devflags |= WriteMostly1; |
| 1709 | else |
| 1710 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1711 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 1712 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 1713 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1714 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 1715 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1716 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1717 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1718 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 1719 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1720 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1721 | sb->feature_map |= |
| 1722 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 1723 | sb->recovery_offset = |
| 1724 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1725 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 1726 | sb->feature_map |= |
| 1727 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1728 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1729 | /* Note: recovery_offset and journal_tail share space */ |
| 1730 | if (test_bit(Journal, &rdev->flags)) |
| 1731 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1732 | if (test_bit(Replacement, &rdev->flags)) |
| 1733 | sb->feature_map |= |
| 1734 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1735 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1736 | if (mddev->reshape_position != MaxSector) { |
| 1737 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 1738 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 1739 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 1740 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 1741 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1742 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1743 | if (mddev->delta_disks == 0 && |
| 1744 | mddev->reshape_backwards) |
| 1745 | sb->feature_map |
| 1746 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1747 | if (rdev->new_data_offset != rdev->data_offset) { |
| 1748 | sb->feature_map |
| 1749 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 1750 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 1751 | - rdev->data_offset)); |
| 1752 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1753 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1754 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 1755 | if (mddev_is_clustered(mddev)) |
| 1756 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 1757 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1758 | if (rdev->badblocks.count == 0) |
| 1759 | /* Nothing to do for bad blocks*/ ; |
| 1760 | else if (sb->bblog_offset == 0) |
| 1761 | /* Cannot record bad blocks on this device */ |
| 1762 | md_error(mddev, rdev); |
| 1763 | else { |
| 1764 | struct badblocks *bb = &rdev->badblocks; |
| 1765 | u64 *bbp = (u64 *)page_address(rdev->bb_page); |
| 1766 | u64 *p = bb->page; |
| 1767 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 1768 | if (bb->changed) { |
| 1769 | unsigned seq; |
| 1770 | |
| 1771 | retry: |
| 1772 | seq = read_seqbegin(&bb->lock); |
| 1773 | |
| 1774 | memset(bbp, 0xff, PAGE_SIZE); |
| 1775 | |
| 1776 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1777 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1778 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 1779 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 1780 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1781 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 1782 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1783 | if (read_seqretry(&bb->lock, seq)) |
| 1784 | goto retry; |
| 1785 | |
| 1786 | bb->sector = (rdev->sb_start + |
| 1787 | (int)le32_to_cpu(sb->bblog_offset)); |
| 1788 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1789 | } |
| 1790 | } |
| 1791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1793 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | if (rdev2->desc_nr+1 > max_dev) |
| 1795 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1796 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1797 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 1798 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 1799 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 1800 | rdev->sb_size = max_dev * 2 + 256; |
| 1801 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 1802 | if (rdev->sb_size & bmask) |
| 1803 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 1804 | } else |
| 1805 | max_dev = le32_to_cpu(sb->max_dev); |
| 1806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | for (i=0; i<max_dev;i++) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1808 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1809 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1810 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 1811 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1813 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1815 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1816 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1817 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 1819 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1820 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 1821 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1822 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1824 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | sb->sb_csum = calc_sb_1_csum(sb); |
| 1828 | } |
| 1829 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1830 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1831 | super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1832 | { |
| 1833 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1834 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1835 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1836 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1837 | if (rdev->data_offset != rdev->new_data_offset) |
| 1838 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1839 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1840 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1841 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1842 | max_sectors -= rdev->data_offset; |
| 1843 | if (!num_sectors || num_sectors > max_sectors) |
| 1844 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1845 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1846 | /* minor version 0 with bitmap we can't move */ |
| 1847 | return 0; |
| 1848 | } else { |
| 1849 | /* minor version 0; superblock after data */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1850 | sector_t sb_start; |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1851 | sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1852 | sb_start &= ~(sector_t)(4*2 - 1); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1853 | max_sectors = rdev->sectors + sb_start - rdev->sb_start; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1854 | if (!num_sectors || num_sectors > max_sectors) |
| 1855 | num_sectors = max_sectors; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1856 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1857 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1858 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1859 | sb->data_size = cpu_to_le64(num_sectors); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1860 | sb->super_offset = rdev->sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1861 | sb->sb_csum = calc_sb_1_csum(sb); |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1862 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1863 | rdev->sb_page); |
| 1864 | md_super_wait(rdev->mddev); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1865 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1866 | |
| 1867 | } |
| 1868 | |
| 1869 | static int |
| 1870 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 1871 | unsigned long long new_offset) |
| 1872 | { |
| 1873 | /* All necessary checks on new >= old have been done */ |
| 1874 | struct bitmap *bitmap; |
| 1875 | if (new_offset >= rdev->data_offset) |
| 1876 | return 1; |
| 1877 | |
| 1878 | /* with 1.0 metadata, there is no metadata to tread on |
| 1879 | * so we can always move back */ |
| 1880 | if (rdev->mddev->minor_version == 0) |
| 1881 | return 1; |
| 1882 | |
| 1883 | /* otherwise we must be sure not to step on |
| 1884 | * any metadata, so stay: |
| 1885 | * 36K beyond start of superblock |
| 1886 | * beyond end of badblocks |
| 1887 | * beyond write-intent bitmap |
| 1888 | */ |
| 1889 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 1890 | return 0; |
| 1891 | bitmap = rdev->mddev->bitmap; |
| 1892 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 1893 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 1894 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1895 | return 0; |
| 1896 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 1897 | return 0; |
| 1898 | |
| 1899 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1900 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 1902 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | [0] = { |
| 1904 | .name = "0.90.0", |
| 1905 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1906 | .load_super = super_90_load, |
| 1907 | .validate_super = super_90_validate, |
| 1908 | .sync_super = super_90_sync, |
| 1909 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1910 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | }, |
| 1912 | [1] = { |
| 1913 | .name = "md-1", |
| 1914 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1915 | .load_super = super_1_load, |
| 1916 | .validate_super = super_1_validate, |
| 1917 | .sync_super = super_1_sync, |
| 1918 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1919 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | }, |
| 1921 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1923 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 1924 | { |
| 1925 | if (mddev->sync_super) { |
| 1926 | mddev->sync_super(mddev, rdev); |
| 1927 | return; |
| 1928 | } |
| 1929 | |
| 1930 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 1931 | |
| 1932 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 1933 | } |
| 1934 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1935 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1937 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1939 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1940 | rdev_for_each_rcu(rdev, mddev1) { |
| 1941 | if (test_bit(Faulty, &rdev->flags) || |
| 1942 | test_bit(Journal, &rdev->flags) || |
| 1943 | rdev->raid_disk == -1) |
| 1944 | continue; |
| 1945 | rdev_for_each_rcu(rdev2, mddev2) { |
| 1946 | if (test_bit(Faulty, &rdev2->flags) || |
| 1947 | test_bit(Journal, &rdev2->flags) || |
| 1948 | rdev2->raid_disk == -1) |
| 1949 | continue; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1950 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1951 | rdev2->bdev->bd_contains) { |
| 1952 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 1953 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1954 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 1955 | } |
| 1956 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 1957 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | return 0; |
| 1959 | } |
| 1960 | |
| 1961 | static LIST_HEAD(pending_raid_disks); |
| 1962 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1963 | /* |
| 1964 | * Try to register data integrity profile for an mddev |
| 1965 | * |
| 1966 | * This is called when an array is started and after a disk has been kicked |
| 1967 | * from the array. It only succeeds if all working and active component devices |
| 1968 | * are integrity capable with matching profiles. |
| 1969 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1970 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1971 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1972 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1973 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1974 | if (list_empty(&mddev->disks)) |
| 1975 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 1976 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 1977 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1978 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1979 | /* skip spares and non-functional disks */ |
| 1980 | if (test_bit(Faulty, &rdev->flags)) |
| 1981 | continue; |
| 1982 | if (rdev->raid_disk < 0) |
| 1983 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1984 | if (!reference) { |
| 1985 | /* Use the first rdev as the reference */ |
| 1986 | reference = rdev; |
| 1987 | continue; |
| 1988 | } |
| 1989 | /* does this rdev's profile match the reference profile? */ |
| 1990 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 1991 | rdev->bdev->bd_disk) < 0) |
| 1992 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 1993 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 1994 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 1995 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1996 | /* |
| 1997 | * All component devices are integrity capable and have matching |
| 1998 | * profiles, register the common profile for the md device. |
| 1999 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2000 | blk_integrity_register(mddev->gendisk, |
| 2001 | bdev_get_integrity(reference->bdev)); |
| 2002 | |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2003 | printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev)); |
| 2004 | if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) { |
| 2005 | printk(KERN_ERR "md: failed to create integrity pool for %s\n", |
| 2006 | mdname(mddev)); |
| 2007 | return -EINVAL; |
| 2008 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2009 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2010 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2011 | EXPORT_SYMBOL(md_integrity_register); |
| 2012 | |
| 2013 | /* Disable data integrity if non-capable/non-matching disk is being added */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2014 | void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2015 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2016 | struct blk_integrity *bi_rdev; |
| 2017 | struct blk_integrity *bi_mddev; |
| 2018 | |
| 2019 | if (!mddev->gendisk) |
| 2020 | return; |
| 2021 | |
| 2022 | bi_rdev = bdev_get_integrity(rdev->bdev); |
| 2023 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2024 | |
| 2025 | if (!bi_mddev) /* nothing to do */ |
| 2026 | return; |
| 2027 | if (rdev->raid_disk < 0) /* skip spares */ |
| 2028 | return; |
| 2029 | if (bi_rdev && blk_integrity_compare(mddev->gendisk, |
| 2030 | rdev->bdev->bd_disk) >= 0) |
| 2031 | return; |
Dan Williams | c7bfced | 2015-10-21 13:20:02 -0400 | [diff] [blame] | 2032 | WARN_ON_ONCE(!mddev->suspended); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2033 | printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev)); |
| 2034 | blk_integrity_unregister(mddev->gendisk); |
| 2035 | } |
| 2036 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2037 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2038 | static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2040 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2041 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2042 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2044 | /* prevent duplicates */ |
| 2045 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2046 | return -EEXIST; |
| 2047 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2048 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2049 | if (!test_bit(Journal, &rdev->flags) && |
| 2050 | rdev->sectors && |
| 2051 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2052 | if (mddev->pers) { |
| 2053 | /* Cannot change size, so fail |
| 2054 | * If mddev->level <= 0, then we don't care |
| 2055 | * about aligning sizes (e.g. linear) |
| 2056 | */ |
| 2057 | if (mddev->level > 0) |
| 2058 | return -ENOSPC; |
| 2059 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2060 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2061 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | |
| 2063 | /* Verify rdev->desc_nr is unique. |
| 2064 | * If it is -1, assign a free number, else |
| 2065 | * check number is not in use |
| 2066 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2067 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | if (rdev->desc_nr < 0) { |
| 2069 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2070 | if (mddev->pers) |
| 2071 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2072 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | choice++; |
| 2074 | rdev->desc_nr = choice; |
| 2075 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2076 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2077 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2081 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2082 | if (!test_bit(Journal, &rdev->flags) && |
| 2083 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2084 | printk(KERN_WARNING "md: %s: array is limited to %d devices\n", |
| 2085 | mdname(mddev), mddev->max_disks); |
| 2086 | return -EBUSY; |
| 2087 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2088 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2089 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | rdev->mddev = mddev; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2092 | printk(KERN_INFO "md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2093 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2094 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2095 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2096 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2097 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2098 | if (sysfs_create_link(&rdev->kobj, ko, "block")) |
| 2099 | /* failure here is OK */; |
| 2100 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2101 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2102 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2103 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2104 | |
| 2105 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2106 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2109 | |
| 2110 | fail: |
| 2111 | printk(KERN_WARNING "md: failed to register dev-%s for %s\n", |
| 2112 | b, mdname(mddev)); |
| 2113 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2116 | static void md_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2117 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2118 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2119 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2120 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2123 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | { |
| 2125 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2126 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2127 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2128 | list_del_rcu(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
| 2130 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2131 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2132 | sysfs_put(rdev->sysfs_state); |
| 2133 | rdev->sysfs_state = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2134 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2135 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2136 | * writing to 'remove' to "dev/state". We also need |
| 2137 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2138 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2139 | synchronize_rcu(); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2140 | INIT_WORK(&rdev->del_work, md_delayed_delete); |
| 2141 | kobject_get(&rdev->kobj); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 2142 | queue_work(md_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | /* |
| 2146 | * prevent the device from being mounted, repartitioned or |
| 2147 | * otherwise reused by a RAID array (or any other kernel |
| 2148 | * subsystem), by bd_claiming the device. |
| 2149 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2150 | static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | { |
| 2152 | int err = 0; |
| 2153 | struct block_device *bdev; |
| 2154 | char b[BDEVNAME_SIZE]; |
| 2155 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2156 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2157 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | if (IS_ERR(bdev)) { |
| 2159 | printk(KERN_ERR "md: could not open %s.\n", |
| 2160 | __bdevname(dev, b)); |
| 2161 | return PTR_ERR(bdev); |
| 2162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | rdev->bdev = bdev; |
| 2164 | return err; |
| 2165 | } |
| 2166 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2167 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | { |
| 2169 | struct block_device *bdev = rdev->bdev; |
| 2170 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2171 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | void md_autodetect_dev(dev_t dev); |
| 2175 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2176 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | { |
| 2178 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | printk(KERN_INFO "md: export_rdev(%s)\n", |
| 2181 | bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2182 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2184 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2185 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | #endif |
| 2187 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2188 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2191 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | { |
| 2193 | unbind_rdev_from_array(rdev); |
| 2194 | export_rdev(rdev); |
| 2195 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2196 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2198 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2200 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2202 | while (!list_empty(&mddev->disks)) { |
| 2203 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2204 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2205 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | mddev->raid_disks = 0; |
| 2208 | mddev->major_version = 0; |
| 2209 | } |
| 2210 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2211 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2213 | /* Update each superblock (in-memory image), but |
| 2214 | * if we are allowed to, skip spares which already |
| 2215 | * have the right event counter, or have one earlier |
| 2216 | * (which would mean they aren't being marked as dirty |
| 2217 | * with the rest of the array) |
| 2218 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2219 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2220 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2221 | if (rdev->sb_events == mddev->events || |
| 2222 | (nospares && |
| 2223 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2224 | rdev->sb_events+1 == mddev->events)) { |
| 2225 | /* Don't update this superblock */ |
| 2226 | rdev->sb_loaded = 2; |
| 2227 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2228 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2229 | rdev->sb_loaded = 1; |
| 2230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | } |
| 2232 | } |
| 2233 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2234 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2235 | { |
| 2236 | struct md_rdev *rdev; |
| 2237 | struct mdp_superblock_1 *sb; |
| 2238 | int role; |
| 2239 | |
| 2240 | /* Find a good rdev */ |
| 2241 | rdev_for_each(rdev, mddev) |
| 2242 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2243 | break; |
| 2244 | |
| 2245 | /* No good device found. */ |
| 2246 | if (!rdev) |
| 2247 | return false; |
| 2248 | |
| 2249 | sb = page_address(rdev->sb_page); |
| 2250 | /* Check if a device has become faulty or a spare become active */ |
| 2251 | rdev_for_each(rdev, mddev) { |
| 2252 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2253 | /* Device activated? */ |
| 2254 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2255 | !test_bit(Faulty, &rdev->flags)) |
| 2256 | return true; |
| 2257 | /* Device turned faulty? */ |
| 2258 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2259 | return true; |
| 2260 | } |
| 2261 | |
| 2262 | /* Check if any mddev parameters have changed */ |
| 2263 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2264 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2265 | (mddev->layout != le64_to_cpu(sb->layout)) || |
| 2266 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2267 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2268 | return true; |
| 2269 | |
| 2270 | return false; |
| 2271 | } |
| 2272 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2273 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2275 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2276 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2277 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2278 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2279 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2281 | if (mddev->ro) { |
| 2282 | if (force_change) |
| 2283 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2284 | return; |
| 2285 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2286 | |
| 2287 | if (mddev_is_clustered(mddev)) { |
| 2288 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2289 | force_change = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2290 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2291 | /* Has someone else has updated the sb */ |
| 2292 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2293 | if (ret == 0) |
| 2294 | md_cluster_ops->metadata_update_cancel(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2295 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 2296 | return; |
| 2297 | } |
| 2298 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | repeat: |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2300 | /* First make sure individual recovery_offsets are correct */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2301 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2302 | if (rdev->raid_disk >= 0 && |
| 2303 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2304 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2305 | !test_bit(In_sync, &rdev->flags) && |
| 2306 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2307 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2308 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2309 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2310 | if (!mddev->persistent) { |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 2311 | clear_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2312 | clear_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2313 | if (!mddev->external) { |
NeilBrown | d97a41d | 2010-10-28 17:30:20 +1100 | [diff] [blame] | 2314 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2315 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2316 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2317 | rdev->badblocks.changed = 0; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2318 | md_ack_all_badblocks(&rdev->badblocks); |
| 2319 | md_error(mddev, rdev); |
| 2320 | } |
| 2321 | clear_bit(Blocked, &rdev->flags); |
| 2322 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2323 | wake_up(&rdev->blocked_wait); |
| 2324 | } |
| 2325 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2326 | wake_up(&mddev->sb_wait); |
| 2327 | return; |
| 2328 | } |
| 2329 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2330 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2331 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2332 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2333 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2334 | if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags)) |
| 2335 | force_change = 1; |
| 2336 | if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags)) |
| 2337 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2338 | * though if events isn't the right even/odd, we will have to do |
| 2339 | * spares after all |
| 2340 | */ |
| 2341 | nospares = 1; |
| 2342 | if (force_change) |
| 2343 | nospares = 0; |
| 2344 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2345 | /* If the array is degraded, then skipping spares is both |
| 2346 | * dangerous and fairly pointless. |
| 2347 | * Dangerous because a device that was removed from the array |
| 2348 | * might have a event_count that still looks up-to-date, |
| 2349 | * so it can be re-added without a resync. |
| 2350 | * Pointless because if there are any spares to skip, |
| 2351 | * then a recovery will happen and soon that array won't |
| 2352 | * be degraded any more and the spare can go back to sleep then. |
| 2353 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2354 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2355 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2356 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2357 | |
| 2358 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2359 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2360 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2361 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2362 | && mddev->can_decrease_events |
| 2363 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2364 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2365 | mddev->can_decrease_events = 0; |
| 2366 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2367 | /* otherwise we have to go forward and ... */ |
| 2368 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2369 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2372 | /* |
| 2373 | * This 64-bit counter should never wrap. |
| 2374 | * Either we are in around ~1 trillion A.C., assuming |
| 2375 | * 1 reboot per second, or we have a bug... |
| 2376 | */ |
| 2377 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2378 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2379 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2380 | if (rdev->badblocks.changed) |
| 2381 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2382 | if (test_bit(Faulty, &rdev->flags)) |
| 2383 | set_bit(FaultRecorded, &rdev->flags); |
| 2384 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2385 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2386 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2387 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2389 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2390 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | |
NeilBrown | 4ad1366 | 2007-07-17 04:06:13 -0700 | [diff] [blame] | 2392 | bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2393 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2395 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2396 | if (rdev->sb_loaded != 1) |
| 2397 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2399 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2400 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2401 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2402 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2403 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2404 | bdevname(rdev->bdev, b), |
| 2405 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2406 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2407 | if (rdev->badblocks.size) { |
| 2408 | md_super_write(mddev, rdev, |
| 2409 | rdev->badblocks.sector, |
| 2410 | rdev->badblocks.size << 9, |
| 2411 | rdev->bb_page); |
| 2412 | rdev->badblocks.size = 0; |
| 2413 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2414 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2415 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2416 | pr_debug("md: %s (skipping faulty)\n", |
| 2417 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2418 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2419 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | /* only need to write one superblock... */ |
| 2421 | break; |
| 2422 | } |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 2423 | md_super_wait(mddev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2424 | /* 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] | 2425 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2426 | spin_lock(&mddev->lock); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2427 | if (mddev->in_sync != sync_req || |
| 2428 | test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2429 | /* have to write it out again */ |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2430 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2431 | goto repeat; |
| 2432 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2433 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2434 | spin_unlock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2435 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2436 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 2437 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2438 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2439 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2440 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2441 | clear_bit(Blocked, &rdev->flags); |
| 2442 | |
| 2443 | if (any_badblocks_changed) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2444 | md_ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2445 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2446 | wake_up(&rdev->blocked_wait); |
| 2447 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2448 | |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2449 | if (mddev_is_clustered(mddev) && ret == 0) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2450 | md_cluster_ops->metadata_update_finish(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2452 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2454 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2455 | { |
| 2456 | struct mddev *mddev = rdev->mddev; |
| 2457 | int err = 0; |
| 2458 | |
| 2459 | if (!mddev->pers->hot_remove_disk) { |
| 2460 | /* If there is hot_add_disk but no hot_remove_disk |
| 2461 | * then added disks for geometry changes, |
| 2462 | * and should be added immediately. |
| 2463 | */ |
| 2464 | super_types[mddev->major_version]. |
| 2465 | validate_super(mddev, rdev); |
| 2466 | err = mddev->pers->hot_add_disk(mddev, rdev); |
| 2467 | if (err) { |
| 2468 | unbind_rdev_from_array(rdev); |
| 2469 | export_rdev(rdev); |
| 2470 | return err; |
| 2471 | } |
| 2472 | } |
| 2473 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2474 | |
| 2475 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2476 | if (mddev->degraded) |
| 2477 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2478 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2479 | md_new_event(mddev); |
| 2480 | md_wakeup_thread(mddev->thread); |
| 2481 | return 0; |
| 2482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2484 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2485 | * We want to accept with case. For this we use cmd_match. |
| 2486 | */ |
| 2487 | static int cmd_match(const char *cmd, const char *str) |
| 2488 | { |
| 2489 | /* See if cmd, written into a sysfs file, matches |
| 2490 | * str. They must either be the same, or cmd can |
| 2491 | * have a trailing newline |
| 2492 | */ |
| 2493 | while (*cmd && *str && *cmd == *str) { |
| 2494 | cmd++; |
| 2495 | str++; |
| 2496 | } |
| 2497 | if (*cmd == '\n') |
| 2498 | cmd++; |
| 2499 | if (*str || *cmd) |
| 2500 | return 0; |
| 2501 | return 1; |
| 2502 | } |
| 2503 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2504 | struct rdev_sysfs_entry { |
| 2505 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2506 | ssize_t (*show)(struct md_rdev *, char *); |
| 2507 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2508 | }; |
| 2509 | |
| 2510 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2511 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2512 | { |
| 2513 | char *sep = ""; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2514 | size_t len = 0; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2515 | unsigned long flags = ACCESS_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2516 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2517 | if (test_bit(Faulty, &flags) || |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2518 | rdev->badblocks.unacked_exist) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2519 | len+= sprintf(page+len, "%sfaulty",sep); |
| 2520 | sep = ","; |
| 2521 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2522 | if (test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2523 | len += sprintf(page+len, "%sin_sync",sep); |
| 2524 | sep = ","; |
| 2525 | } |
Shaohua Li | ac6096e | 2015-10-04 09:20:11 -0700 | [diff] [blame] | 2526 | if (test_bit(Journal, &flags)) { |
| 2527 | len += sprintf(page+len, "%sjournal",sep); |
| 2528 | sep = ","; |
| 2529 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2530 | if (test_bit(WriteMostly, &flags)) { |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2531 | len += sprintf(page+len, "%swrite_mostly",sep); |
| 2532 | sep = ","; |
| 2533 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2534 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2535 | (rdev->badblocks.unacked_exist |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2536 | && !test_bit(Faulty, &flags))) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2537 | len += sprintf(page+len, "%sblocked", sep); |
| 2538 | sep = ","; |
| 2539 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2540 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2541 | !test_bit(Journal, &flags) && |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2542 | !test_bit(In_sync, &flags)) { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2543 | len += sprintf(page+len, "%sspare", sep); |
| 2544 | sep = ","; |
| 2545 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2546 | if (test_bit(WriteErrorSeen, &flags)) { |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2547 | len += sprintf(page+len, "%swrite_error", sep); |
| 2548 | sep = ","; |
| 2549 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2550 | if (test_bit(WantReplacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2551 | len += sprintf(page+len, "%swant_replacement", sep); |
| 2552 | sep = ","; |
| 2553 | } |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2554 | if (test_bit(Replacement, &flags)) { |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2555 | len += sprintf(page+len, "%sreplacement", sep); |
| 2556 | sep = ","; |
| 2557 | } |
| 2558 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2559 | return len+sprintf(page+len, "\n"); |
| 2560 | } |
| 2561 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2562 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2563 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2564 | { |
| 2565 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2566 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2567 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2568 | * writemostly - sets write_mostly |
| 2569 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2570 | * blocked - sets the Blocked flags |
| 2571 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2572 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2573 | * -insync - clear Insync for a device with a slot assigned, |
| 2574 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2575 | * write_error - sets WriteErrorSeen |
| 2576 | * -write_error - clears WriteErrorSeen |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2577 | */ |
| 2578 | int err = -EINVAL; |
| 2579 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2580 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2581 | if (test_bit(Faulty, &rdev->flags)) |
| 2582 | err = 0; |
| 2583 | else |
| 2584 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2585 | } else if (cmd_match(buf, "remove")) { |
| 2586 | if (rdev->raid_disk >= 0) |
| 2587 | err = -EBUSY; |
| 2588 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2589 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2590 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2591 | if (mddev_is_clustered(mddev)) |
| 2592 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2593 | |
| 2594 | if (err == 0) { |
| 2595 | md_kick_rdev_from_array(rdev); |
| 2596 | if (mddev->pers) |
| 2597 | md_update_sb(mddev, 1); |
| 2598 | md_new_event(mddev); |
| 2599 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2600 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2601 | } else if (cmd_match(buf, "writemostly")) { |
| 2602 | set_bit(WriteMostly, &rdev->flags); |
| 2603 | err = 0; |
| 2604 | } else if (cmd_match(buf, "-writemostly")) { |
| 2605 | clear_bit(WriteMostly, &rdev->flags); |
| 2606 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2607 | } else if (cmd_match(buf, "blocked")) { |
| 2608 | set_bit(Blocked, &rdev->flags); |
| 2609 | err = 0; |
| 2610 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2611 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 2612 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2613 | /* metadata handler doesn't understand badblocks, |
| 2614 | * so we need to fail the device |
| 2615 | */ |
| 2616 | md_error(rdev->mddev, rdev); |
| 2617 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2618 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2619 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2620 | wake_up(&rdev->blocked_wait); |
| 2621 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2622 | md_wakeup_thread(rdev->mddev->thread); |
| 2623 | |
| 2624 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2625 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 2626 | set_bit(In_sync, &rdev->flags); |
| 2627 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2628 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 2629 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 2630 | if (rdev->mddev->pers == NULL) { |
| 2631 | clear_bit(In_sync, &rdev->flags); |
| 2632 | rdev->saved_raid_disk = rdev->raid_disk; |
| 2633 | rdev->raid_disk = -1; |
| 2634 | err = 0; |
| 2635 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2636 | } else if (cmd_match(buf, "write_error")) { |
| 2637 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2638 | err = 0; |
| 2639 | } else if (cmd_match(buf, "-write_error")) { |
| 2640 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 2641 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2642 | } else if (cmd_match(buf, "want_replacement")) { |
| 2643 | /* Any non-spare device that is not a replacement can |
| 2644 | * become want_replacement at any time, but we then need to |
| 2645 | * check if recovery is needed. |
| 2646 | */ |
| 2647 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2648 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2649 | !test_bit(Replacement, &rdev->flags)) |
| 2650 | set_bit(WantReplacement, &rdev->flags); |
| 2651 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2652 | md_wakeup_thread(rdev->mddev->thread); |
| 2653 | err = 0; |
| 2654 | } else if (cmd_match(buf, "-want_replacement")) { |
| 2655 | /* Clearing 'want_replacement' is always allowed. |
| 2656 | * Once replacements starts it is too late though. |
| 2657 | */ |
| 2658 | err = 0; |
| 2659 | clear_bit(WantReplacement, &rdev->flags); |
| 2660 | } else if (cmd_match(buf, "replacement")) { |
| 2661 | /* Can only set a device as a replacement when array has not |
| 2662 | * yet been started. Once running, replacement is automatic |
| 2663 | * from spares, or by assigning 'slot'. |
| 2664 | */ |
| 2665 | if (rdev->mddev->pers) |
| 2666 | err = -EBUSY; |
| 2667 | else { |
| 2668 | set_bit(Replacement, &rdev->flags); |
| 2669 | err = 0; |
| 2670 | } |
| 2671 | } else if (cmd_match(buf, "-replacement")) { |
| 2672 | /* Similarly, can only clear Replacement before start */ |
| 2673 | if (rdev->mddev->pers) |
| 2674 | err = -EBUSY; |
| 2675 | else { |
| 2676 | clear_bit(Replacement, &rdev->flags); |
| 2677 | err = 0; |
| 2678 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2679 | } else if (cmd_match(buf, "re-add")) { |
| 2680 | if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1)) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 2681 | /* clear_bit is performed _after_ all the devices |
| 2682 | * have their local Faulty bit cleared. If any writes |
| 2683 | * happen in the meantime in the local node, they |
| 2684 | * will land in the local bitmap, which will be synced |
| 2685 | * by this node eventually |
| 2686 | */ |
| 2687 | if (!mddev_is_clustered(rdev->mddev) || |
| 2688 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 2689 | clear_bit(Faulty, &rdev->flags); |
| 2690 | err = add_bound_rdev(rdev); |
| 2691 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2692 | } else |
| 2693 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2694 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2695 | if (!err) |
| 2696 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2697 | return err ? err : len; |
| 2698 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2699 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 2700 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2701 | |
| 2702 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2703 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2704 | { |
| 2705 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 2706 | } |
| 2707 | |
| 2708 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2709 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2710 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2711 | unsigned int n; |
| 2712 | int rv; |
| 2713 | |
| 2714 | rv = kstrtouint(buf, 10, &n); |
| 2715 | if (rv < 0) |
| 2716 | return rv; |
| 2717 | atomic_set(&rdev->corrected_errors, n); |
| 2718 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2719 | } |
| 2720 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2721 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2722 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2723 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2724 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2725 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2726 | if (test_bit(Journal, &rdev->flags)) |
| 2727 | return sprintf(page, "journal\n"); |
| 2728 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2729 | return sprintf(page, "none\n"); |
| 2730 | else |
| 2731 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 2732 | } |
| 2733 | |
| 2734 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2735 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2736 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2737 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2738 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2739 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2740 | if (test_bit(Journal, &rdev->flags)) |
| 2741 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2742 | if (strncmp(buf, "none", 4)==0) |
| 2743 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 2744 | else { |
| 2745 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 2746 | if (err < 0) |
| 2747 | return err; |
| 2748 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2749 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2750 | /* Setting 'slot' on an active array requires also |
| 2751 | * updating the 'rd%d' link, and communicating |
| 2752 | * with the personality with ->hot_*_disk. |
| 2753 | * For now we only support removing |
| 2754 | * failed/spare devices. This normally happens automatically, |
| 2755 | * but not when the metadata is externally managed. |
| 2756 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2757 | if (rdev->raid_disk == -1) |
| 2758 | return -EEXIST; |
| 2759 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 2760 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2761 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 2762 | clear_bit(Blocked, &rdev->flags); |
| 2763 | remove_and_add_spares(rdev->mddev, rdev); |
| 2764 | if (rdev->raid_disk >= 0) |
| 2765 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2766 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 2767 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2768 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2769 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2770 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2771 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2772 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2773 | |
| 2774 | if (rdev->raid_disk != -1) |
| 2775 | return -EBUSY; |
| 2776 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 2777 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 2778 | return -EBUSY; |
| 2779 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2780 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 2781 | return -EINVAL; |
| 2782 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2783 | if (slot >= rdev->mddev->raid_disks && |
| 2784 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 2785 | return -ENOSPC; |
| 2786 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2787 | rdev->raid_disk = slot; |
| 2788 | if (test_bit(In_sync, &rdev->flags)) |
| 2789 | rdev->saved_raid_disk = slot; |
| 2790 | else |
| 2791 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 2792 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 2793 | clear_bit(Bitmap_sync, &rdev->flags); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2794 | err = rdev->mddev->pers-> |
| 2795 | hot_add_disk(rdev->mddev, rdev); |
| 2796 | if (err) { |
| 2797 | rdev->raid_disk = -1; |
| 2798 | return err; |
| 2799 | } else |
| 2800 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2801 | if (sysfs_link_rdev(rdev->mddev, rdev)) |
| 2802 | /* failure here is OK */; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 2803 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2804 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 2805 | if (slot >= rdev->mddev->raid_disks && |
| 2806 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2807 | return -ENOSPC; |
| 2808 | rdev->raid_disk = slot; |
| 2809 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2810 | clear_bit(Faulty, &rdev->flags); |
| 2811 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2812 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2813 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2814 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2815 | return len; |
| 2816 | } |
| 2817 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2818 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2819 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2820 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2821 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2822 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2823 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 2824 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2828 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2829 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2830 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2831 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2832 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 2833 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2834 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2835 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2836 | /* Must set offset before size, so overlap checks |
| 2837 | * can be sane */ |
| 2838 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2839 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 2840 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2841 | return len; |
| 2842 | } |
| 2843 | |
| 2844 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 2845 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 2846 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2847 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 2848 | { |
| 2849 | return sprintf(page, "%llu\n", |
| 2850 | (unsigned long long)rdev->new_data_offset); |
| 2851 | } |
| 2852 | |
| 2853 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 2854 | const char *buf, size_t len) |
| 2855 | { |
| 2856 | unsigned long long new_offset; |
| 2857 | struct mddev *mddev = rdev->mddev; |
| 2858 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2859 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2860 | return -EINVAL; |
| 2861 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 2862 | if (mddev->sync_thread || |
| 2863 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2864 | return -EBUSY; |
| 2865 | if (new_offset == rdev->data_offset) |
| 2866 | /* reset is always permitted */ |
| 2867 | ; |
| 2868 | else if (new_offset > rdev->data_offset) { |
| 2869 | /* must not push array size beyond rdev_sectors */ |
| 2870 | if (new_offset - rdev->data_offset |
| 2871 | + mddev->dev_sectors > rdev->sectors) |
| 2872 | return -E2BIG; |
| 2873 | } |
| 2874 | /* Metadata worries about other space details. */ |
| 2875 | |
| 2876 | /* decreasing the offset is inconsistent with a backwards |
| 2877 | * reshape. |
| 2878 | */ |
| 2879 | if (new_offset < rdev->data_offset && |
| 2880 | mddev->reshape_backwards) |
| 2881 | return -EINVAL; |
| 2882 | /* Increasing offset is inconsistent with forwards |
| 2883 | * reshape. reshape_direction should be set to |
| 2884 | * 'backwards' first. |
| 2885 | */ |
| 2886 | if (new_offset > rdev->data_offset && |
| 2887 | !mddev->reshape_backwards) |
| 2888 | return -EINVAL; |
| 2889 | |
| 2890 | if (mddev->pers && mddev->persistent && |
| 2891 | !super_types[mddev->major_version] |
| 2892 | .allow_new_offset(rdev, new_offset)) |
| 2893 | return -E2BIG; |
| 2894 | rdev->new_data_offset = new_offset; |
| 2895 | if (new_offset > rdev->data_offset) |
| 2896 | mddev->reshape_backwards = 1; |
| 2897 | else if (new_offset < rdev->data_offset) |
| 2898 | mddev->reshape_backwards = 0; |
| 2899 | |
| 2900 | return len; |
| 2901 | } |
| 2902 | static struct rdev_sysfs_entry rdev_new_offset = |
| 2903 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 2904 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2905 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2906 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2907 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2908 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2909 | } |
| 2910 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2911 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 2912 | { |
| 2913 | /* check if two start/length pairs overlap */ |
| 2914 | if (s1+l1 <= s2) |
| 2915 | return 0; |
| 2916 | if (s2+l2 <= s1) |
| 2917 | return 0; |
| 2918 | return 1; |
| 2919 | } |
| 2920 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2921 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 2922 | { |
| 2923 | unsigned long long blocks; |
| 2924 | sector_t new; |
| 2925 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 2926 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2927 | return -EINVAL; |
| 2928 | |
| 2929 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 2930 | return -EINVAL; /* sector conversion overflow */ |
| 2931 | |
| 2932 | new = blocks * 2; |
| 2933 | if (new != blocks * 2) |
| 2934 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 2935 | |
| 2936 | *sectors = new; |
| 2937 | return 0; |
| 2938 | } |
| 2939 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2940 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2941 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 2942 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2943 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2944 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2945 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 2946 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2947 | if (test_bit(Journal, &rdev->flags)) |
| 2948 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 2949 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2950 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2951 | if (rdev->data_offset != rdev->new_data_offset) |
| 2952 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2953 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 2954 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2955 | sectors = super_types[my_mddev->major_version]. |
| 2956 | rdev_size_change(rdev, sectors); |
| 2957 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2958 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2959 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2960 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2961 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 2962 | if (!my_mddev->pers->resize) |
| 2963 | /* Cannot change size for RAID0 or Linear etc */ |
| 2964 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2965 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2966 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 2967 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2968 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2969 | rdev->sectors = sectors; |
| 2970 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2971 | /* Need to check that all other rdevs with the same |
| 2972 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 2973 | * the rdev lists safely. |
| 2974 | * This check does not provide a hard guarantee, it |
| 2975 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2976 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2977 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2978 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2979 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2980 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2981 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 2982 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2983 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2984 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2985 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 2986 | if (rdev->bdev == rdev2->bdev && |
| 2987 | rdev != rdev2 && |
| 2988 | overlaps(rdev->data_offset, rdev->sectors, |
| 2989 | rdev2->data_offset, |
| 2990 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2991 | overlap = 1; |
| 2992 | break; |
| 2993 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 2994 | if (overlap) { |
| 2995 | mddev_put(mddev); |
| 2996 | break; |
| 2997 | } |
| 2998 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 2999 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3000 | if (overlap) { |
| 3001 | /* Someone else could have slipped in a size |
| 3002 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3003 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3004 | * safe, and trust userspace not to race with |
| 3005 | * itself |
| 3006 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3007 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3008 | return -EBUSY; |
| 3009 | } |
| 3010 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3011 | return len; |
| 3012 | } |
| 3013 | |
| 3014 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3015 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3016 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3017 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3018 | { |
| 3019 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3020 | |
| 3021 | if (test_bit(In_sync, &rdev->flags) || |
| 3022 | recovery_start == MaxSector) |
| 3023 | return sprintf(page, "none\n"); |
| 3024 | |
| 3025 | return sprintf(page, "%llu\n", recovery_start); |
| 3026 | } |
| 3027 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3028 | static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3029 | { |
| 3030 | unsigned long long recovery_start; |
| 3031 | |
| 3032 | if (cmd_match(buf, "none")) |
| 3033 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3034 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3035 | return -EINVAL; |
| 3036 | |
| 3037 | if (rdev->mddev->pers && |
| 3038 | rdev->raid_disk >= 0) |
| 3039 | return -EBUSY; |
| 3040 | |
| 3041 | rdev->recovery_offset = recovery_start; |
| 3042 | if (recovery_start == MaxSector) |
| 3043 | set_bit(In_sync, &rdev->flags); |
| 3044 | else |
| 3045 | clear_bit(In_sync, &rdev->flags); |
| 3046 | return len; |
| 3047 | } |
| 3048 | |
| 3049 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3050 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3051 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3052 | static ssize_t |
| 3053 | badblocks_show(struct badblocks *bb, char *page, int unack); |
| 3054 | static ssize_t |
| 3055 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack); |
| 3056 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3057 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3058 | { |
| 3059 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3060 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3061 | static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3062 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3063 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3064 | /* Maybe that ack was all we needed */ |
| 3065 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3066 | wake_up(&rdev->blocked_wait); |
| 3067 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3068 | } |
| 3069 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3070 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3071 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3072 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3073 | { |
| 3074 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3075 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3076 | static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3077 | { |
| 3078 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3079 | } |
| 3080 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3081 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3082 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3083 | static struct attribute *rdev_default_attrs[] = { |
| 3084 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3085 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3086 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3087 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3088 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3089 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3090 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3091 | &rdev_bad_blocks.attr, |
| 3092 | &rdev_unack_bad_blocks.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3093 | NULL, |
| 3094 | }; |
| 3095 | static ssize_t |
| 3096 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3097 | { |
| 3098 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3099 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3100 | |
| 3101 | if (!entry->show) |
| 3102 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3103 | if (!rdev->mddev) |
| 3104 | return -EBUSY; |
| 3105 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | static ssize_t |
| 3109 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3110 | const char *page, size_t length) |
| 3111 | { |
| 3112 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3113 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3114 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3115 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3116 | |
| 3117 | if (!entry->store) |
| 3118 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3119 | if (!capable(CAP_SYS_ADMIN)) |
| 3120 | return -EACCES; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3121 | rv = mddev ? mddev_lock(mddev): -EBUSY; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3122 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3123 | if (rdev->mddev == NULL) |
| 3124 | rv = -EBUSY; |
| 3125 | else |
| 3126 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3127 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3128 | } |
| 3129 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | static void rdev_free(struct kobject *ko) |
| 3133 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3134 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3135 | kfree(rdev); |
| 3136 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3137 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3138 | .show = rdev_attr_show, |
| 3139 | .store = rdev_attr_store, |
| 3140 | }; |
| 3141 | static struct kobj_type rdev_ktype = { |
| 3142 | .release = rdev_free, |
| 3143 | .sysfs_ops = &rdev_sysfs_ops, |
| 3144 | .default_attrs = rdev_default_attrs, |
| 3145 | }; |
| 3146 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3147 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3148 | { |
| 3149 | rdev->desc_nr = -1; |
| 3150 | rdev->saved_raid_disk = -1; |
| 3151 | rdev->raid_disk = -1; |
| 3152 | rdev->flags = 0; |
| 3153 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3154 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3155 | rdev->sb_events = 0; |
| 3156 | rdev->last_read_error.tv_sec = 0; |
| 3157 | rdev->last_read_error.tv_nsec = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3158 | rdev->sb_loaded = 0; |
| 3159 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3160 | atomic_set(&rdev->nr_pending, 0); |
| 3161 | atomic_set(&rdev->read_errors, 0); |
| 3162 | atomic_set(&rdev->corrected_errors, 0); |
| 3163 | |
| 3164 | INIT_LIST_HEAD(&rdev->same_set); |
| 3165 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3166 | |
| 3167 | /* Add space to store bad block list. |
| 3168 | * This reserves the space even on arrays where it cannot |
| 3169 | * be used - I wonder if that matters |
| 3170 | */ |
| 3171 | rdev->badblocks.count = 0; |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3172 | rdev->badblocks.shift = -1; /* disabled until explicitly enabled */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3173 | rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3174 | seqlock_init(&rdev->badblocks.lock); |
| 3175 | if (rdev->badblocks.page == NULL) |
| 3176 | return -ENOMEM; |
| 3177 | |
| 3178 | return 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3179 | } |
| 3180 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | /* |
| 3182 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3183 | * |
| 3184 | * mark the device faulty if: |
| 3185 | * |
| 3186 | * - the device is nonexistent (zero size) |
| 3187 | * - the device has no valid superblock |
| 3188 | * |
| 3189 | * a faulty rdev _never_ has rdev->sb set. |
| 3190 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3191 | static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | { |
| 3193 | char b[BDEVNAME_SIZE]; |
| 3194 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3195 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | sector_t size; |
| 3197 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3198 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3199 | if (!rdev) { |
| 3200 | printk(KERN_ERR "md: could not alloc mem for new device!\n"); |
| 3201 | return ERR_PTR(-ENOMEM); |
| 3202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3204 | err = md_rdev_init(rdev); |
| 3205 | if (err) |
| 3206 | goto abort_free; |
| 3207 | err = alloc_disk_sb(rdev); |
| 3208 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | goto abort_free; |
| 3210 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3211 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | if (err) |
| 3213 | goto abort_free; |
| 3214 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3215 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3216 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3217 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3218 | if (!size) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3219 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3220 | "md: %s has zero or unknown size, marking faulty!\n", |
| 3221 | bdevname(rdev->bdev,b)); |
| 3222 | err = -EINVAL; |
| 3223 | goto abort_free; |
| 3224 | } |
| 3225 | |
| 3226 | if (super_format >= 0) { |
| 3227 | err = super_types[super_format]. |
| 3228 | load_super(rdev, NULL, super_minor); |
| 3229 | if (err == -EINVAL) { |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3230 | printk(KERN_WARNING |
| 3231 | "md: %s does not have a valid v%d.%d " |
| 3232 | "superblock, not importing!\n", |
| 3233 | bdevname(rdev->bdev,b), |
| 3234 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | goto abort_free; |
| 3236 | } |
| 3237 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3238 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | "md: could not read %s's sb, not importing!\n", |
| 3240 | bdevname(rdev->bdev,b)); |
| 3241 | goto abort_free; |
| 3242 | } |
| 3243 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | return rdev; |
| 3246 | |
| 3247 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3248 | if (rdev->bdev) |
| 3249 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3250 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | kfree(rdev); |
| 3252 | return ERR_PTR(err); |
| 3253 | } |
| 3254 | |
| 3255 | /* |
| 3256 | * Check a full RAID array for plausibility |
| 3257 | */ |
| 3258 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3259 | static void analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | { |
| 3261 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3262 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | char b[BDEVNAME_SIZE]; |
| 3264 | |
| 3265 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3266 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | switch (super_types[mddev->major_version]. |
| 3268 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3269 | case 1: |
| 3270 | freshest = rdev; |
| 3271 | break; |
| 3272 | case 0: |
| 3273 | break; |
| 3274 | default: |
| 3275 | printk( KERN_ERR \ |
| 3276 | "md: fatal superblock inconsistency in %s" |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3277 | " -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3279 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | } |
| 3281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3282 | super_types[mddev->major_version]. |
| 3283 | validate_super(mddev, freshest); |
| 3284 | |
| 3285 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3286 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3287 | if (mddev->max_disks && |
| 3288 | (rdev->desc_nr >= mddev->max_disks || |
| 3289 | i > mddev->max_disks)) { |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3290 | printk(KERN_WARNING |
| 3291 | "md: %s: %s: only %d devices permitted\n", |
| 3292 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3293 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3294 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3295 | continue; |
| 3296 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3297 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | if (super_types[mddev->major_version]. |
| 3299 | validate_super(mddev, rdev)) { |
| 3300 | printk(KERN_WARNING "md: kicking non-fresh %s" |
| 3301 | " from array!\n", |
| 3302 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3303 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3304 | continue; |
| 3305 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3306 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3308 | rdev->desc_nr = i++; |
| 3309 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3310 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3311 | } else if (rdev->raid_disk >= |
| 3312 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3313 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3314 | rdev->raid_disk = -1; |
| 3315 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | } |
| 3317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | } |
| 3319 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3320 | /* Read a fixed-point number. |
| 3321 | * Numbers in sysfs attributes should be in "standard" units where |
| 3322 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3323 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3324 | * milliseconds or jiffies. |
| 3325 | * This function takes a decimal number with a possible fractional |
| 3326 | * component, and produces an integer which is the result of |
| 3327 | * multiplying that number by 10^'scale'. |
| 3328 | * all without any floating-point arithmetic. |
| 3329 | */ |
| 3330 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3331 | { |
| 3332 | unsigned long result = 0; |
| 3333 | long decimals = -1; |
| 3334 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3335 | if (*cp == '.') |
| 3336 | decimals = 0; |
| 3337 | else if (decimals < scale) { |
| 3338 | unsigned int value; |
| 3339 | value = *cp - '0'; |
| 3340 | result = result * 10 + value; |
| 3341 | if (decimals >= 0) |
| 3342 | decimals++; |
| 3343 | } |
| 3344 | cp++; |
| 3345 | } |
| 3346 | if (*cp == '\n') |
| 3347 | cp++; |
| 3348 | if (*cp) |
| 3349 | return -EINVAL; |
| 3350 | if (decimals < 0) |
| 3351 | decimals = 0; |
| 3352 | while (decimals < scale) { |
| 3353 | result *= 10; |
| 3354 | decimals ++; |
| 3355 | } |
| 3356 | *res = result; |
| 3357 | return 0; |
| 3358 | } |
| 3359 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3360 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3361 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3362 | { |
| 3363 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3364 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3365 | } |
| 3366 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3367 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3368 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3369 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3370 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3371 | if (mddev_is_clustered(mddev)) { |
| 3372 | pr_info("md: Safemode is disabled for clustered mode\n"); |
| 3373 | return -EINVAL; |
| 3374 | } |
| 3375 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3376 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3377 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3378 | if (msec == 0) |
| 3379 | mddev->safemode_delay = 0; |
| 3380 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3381 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3382 | unsigned long new_delay = (msec*HZ)/1000; |
| 3383 | |
| 3384 | if (new_delay == 0) |
| 3385 | new_delay = 1; |
| 3386 | mddev->safemode_delay = new_delay; |
| 3387 | if (new_delay < old_delay || old_delay == 0) |
| 3388 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3389 | } |
| 3390 | return len; |
| 3391 | } |
| 3392 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3393 | __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] | 3394 | |
| 3395 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3396 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3397 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3398 | struct md_personality *p; |
| 3399 | int ret; |
| 3400 | spin_lock(&mddev->lock); |
| 3401 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3402 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3403 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3404 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3405 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3406 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3407 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3408 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3409 | ret = 0; |
| 3410 | spin_unlock(&mddev->lock); |
| 3411 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3412 | } |
| 3413 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3414 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3415 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3416 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3417 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3418 | ssize_t rv; |
| 3419 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3420 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3421 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3422 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3423 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3424 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3425 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3426 | return -EINVAL; |
| 3427 | |
| 3428 | rv = mddev_lock(mddev); |
| 3429 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3430 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3431 | |
| 3432 | if (mddev->pers == NULL) { |
| 3433 | strncpy(mddev->clevel, buf, slen); |
| 3434 | if (mddev->clevel[slen-1] == '\n') |
| 3435 | slen--; |
| 3436 | mddev->clevel[slen] = 0; |
| 3437 | mddev->level = LEVEL_NONE; |
| 3438 | rv = len; |
| 3439 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3440 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3441 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3442 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3443 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3444 | |
| 3445 | /* request to change the personality. Need to ensure: |
| 3446 | * - array is not engaged in resync/recovery/reshape |
| 3447 | * - old personality can be suspended |
| 3448 | * - new personality will access other array. |
| 3449 | */ |
| 3450 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3451 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3452 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3453 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3454 | mddev->reshape_position != MaxSector || |
| 3455 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3456 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3457 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3458 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3459 | if (!mddev->pers->quiesce) { |
| 3460 | printk(KERN_WARNING "md: %s: %s does not support online personality change\n", |
| 3461 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3462 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3463 | } |
| 3464 | |
| 3465 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3466 | strncpy(clevel, buf, slen); |
| 3467 | if (clevel[slen-1] == '\n') |
| 3468 | slen--; |
| 3469 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3470 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3471 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3472 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3473 | if (request_module("md-%s", clevel) != 0) |
| 3474 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3475 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3476 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3477 | if (!pers || !try_module_get(pers->owner)) { |
| 3478 | spin_unlock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3479 | printk(KERN_WARNING "md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3480 | rv = -EINVAL; |
| 3481 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3482 | } |
| 3483 | spin_unlock(&pers_lock); |
| 3484 | |
| 3485 | if (pers == mddev->pers) { |
| 3486 | /* Nothing to do! */ |
| 3487 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3488 | rv = len; |
| 3489 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3490 | } |
| 3491 | if (!pers->takeover) { |
| 3492 | module_put(pers->owner); |
| 3493 | printk(KERN_WARNING "md: %s: %s does not support personality takeover\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3494 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3495 | rv = -EINVAL; |
| 3496 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3497 | } |
| 3498 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3499 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3500 | rdev->new_raid_disk = rdev->raid_disk; |
| 3501 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3502 | /* ->takeover must set new_* and/or delta_disks |
| 3503 | * if it succeeds, and may set them when it fails. |
| 3504 | */ |
| 3505 | priv = pers->takeover(mddev); |
| 3506 | if (IS_ERR(priv)) { |
| 3507 | mddev->new_level = mddev->level; |
| 3508 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3509 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3510 | mddev->raid_disks -= mddev->delta_disks; |
| 3511 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3512 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3513 | module_put(pers->owner); |
| 3514 | printk(KERN_WARNING "md: %s: %s would not accept array\n", |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3515 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3516 | rv = PTR_ERR(priv); |
| 3517 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | /* Looks like we have a winner */ |
| 3521 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 3522 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3523 | |
| 3524 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3525 | oldpers = mddev->pers; |
| 3526 | oldpriv = mddev->private; |
| 3527 | mddev->pers = pers; |
| 3528 | mddev->private = priv; |
| 3529 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 3530 | mddev->level = mddev->new_level; |
| 3531 | mddev->layout = mddev->new_layout; |
| 3532 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 3533 | mddev->delta_disks = 0; |
| 3534 | mddev->reshape_backwards = 0; |
| 3535 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3536 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3537 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3538 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 3539 | mddev->external) { |
| 3540 | /* We are converting from a no-redundancy array |
| 3541 | * to a redundancy array and metadata is managed |
| 3542 | * externally so we need to be sure that writes |
| 3543 | * won't block due to a need to transition |
| 3544 | * clean->dirty |
| 3545 | * until external management is started. |
| 3546 | */ |
| 3547 | mddev->in_sync = 0; |
| 3548 | mddev->safemode_delay = 0; |
| 3549 | mddev->safemode = 0; |
| 3550 | } |
| 3551 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3552 | oldpers->free(mddev, oldpriv); |
| 3553 | |
| 3554 | if (oldpers->sync_request == NULL && |
| 3555 | pers->sync_request != NULL) { |
| 3556 | /* need to add the md_redundancy_group */ |
| 3557 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
| 3558 | printk(KERN_WARNING |
| 3559 | "md: cannot register extra attributes for %s\n", |
| 3560 | mdname(mddev)); |
| 3561 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 3562 | } |
| 3563 | if (oldpers->sync_request != NULL && |
| 3564 | pers->sync_request == NULL) { |
| 3565 | /* need to remove the md_redundancy_group */ |
| 3566 | if (mddev->to_remove == NULL) |
| 3567 | mddev->to_remove = &md_redundancy_group; |
| 3568 | } |
| 3569 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3570 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3571 | if (rdev->raid_disk < 0) |
| 3572 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3573 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3574 | rdev->new_raid_disk = -1; |
| 3575 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3576 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3577 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3578 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3579 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3580 | if (rdev->raid_disk < 0) |
| 3581 | continue; |
| 3582 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 3583 | continue; |
| 3584 | rdev->raid_disk = rdev->new_raid_disk; |
| 3585 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3586 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3587 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3588 | if (sysfs_link_rdev(mddev, rdev)) |
| 3589 | printk(KERN_WARNING "md: cannot register rd%d" |
| 3590 | " for %s after level change\n", |
| 3591 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 3592 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3593 | } |
| 3594 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3595 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 3596 | /* this is now an array without redundancy, so |
| 3597 | * it must always be in_sync |
| 3598 | */ |
| 3599 | mddev->in_sync = 1; |
| 3600 | del_timer_sync(&mddev->safemode_timer); |
| 3601 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 3602 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3603 | pers->run(mddev); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3604 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 3605 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 3606 | if (!mddev->thread) |
| 3607 | md_update_sb(mddev, 1); |
Maciej Trela | 5cac7861 | 2010-04-14 17:17:39 +1000 | [diff] [blame] | 3608 | sysfs_notify(&mddev->kobj, NULL, "level"); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 3609 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3610 | rv = len; |
| 3611 | out_unlock: |
| 3612 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3613 | return rv; |
| 3614 | } |
| 3615 | |
| 3616 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3617 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3618 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3619 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3620 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3621 | { |
| 3622 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3623 | if (mddev->reshape_position != MaxSector && |
| 3624 | mddev->layout != mddev->new_layout) |
| 3625 | return sprintf(page, "%d (%d)\n", |
| 3626 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3627 | return sprintf(page, "%d\n", mddev->layout); |
| 3628 | } |
| 3629 | |
| 3630 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3631 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3632 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3633 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3634 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3635 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3636 | err = kstrtouint(buf, 10, &n); |
| 3637 | if (err < 0) |
| 3638 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3639 | err = mddev_lock(mddev); |
| 3640 | if (err) |
| 3641 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3642 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3643 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3644 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3645 | err = -EBUSY; |
| 3646 | else if (mddev->ro) |
| 3647 | err = -EROFS; |
| 3648 | else { |
| 3649 | mddev->new_layout = n; |
| 3650 | err = mddev->pers->check_reshape(mddev); |
| 3651 | if (err) |
| 3652 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3653 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3654 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3655 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3656 | if (mddev->reshape_position == MaxSector) |
| 3657 | mddev->layout = n; |
| 3658 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3659 | mddev_unlock(mddev); |
| 3660 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3661 | } |
| 3662 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3663 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 3664 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3665 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3666 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3667 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 3668 | if (mddev->raid_disks == 0) |
| 3669 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3670 | if (mddev->reshape_position != MaxSector && |
| 3671 | mddev->delta_disks != 0) |
| 3672 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 3673 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3674 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 3675 | } |
| 3676 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3677 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3678 | |
| 3679 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3680 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3681 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3682 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3683 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3684 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3685 | err = kstrtouint(buf, 10, &n); |
| 3686 | if (err < 0) |
| 3687 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3688 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3689 | err = mddev_lock(mddev); |
| 3690 | if (err) |
| 3691 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3692 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3693 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3694 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3695 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3696 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3697 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3698 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3699 | rdev_for_each(rdev, mddev) { |
| 3700 | if (olddisks < n && |
| 3701 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3702 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3703 | if (olddisks > n && |
| 3704 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3705 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3706 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3707 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3708 | mddev->delta_disks = n - olddisks; |
| 3709 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3710 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3711 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3712 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3713 | out_unlock: |
| 3714 | mddev_unlock(mddev); |
| 3715 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 3716 | } |
| 3717 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3718 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3719 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 3720 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3721 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3722 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 3723 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3724 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 3725 | return sprintf(page, "%d (%d)\n", |
| 3726 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3727 | mddev->chunk_sectors << 9); |
| 3728 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3732 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3733 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3734 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3735 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3736 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3737 | err = kstrtoul(buf, 10, &n); |
| 3738 | if (err < 0) |
| 3739 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3740 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3741 | err = mddev_lock(mddev); |
| 3742 | if (err) |
| 3743 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3744 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 3745 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3746 | err = -EBUSY; |
| 3747 | else if (mddev->ro) |
| 3748 | err = -EROFS; |
| 3749 | else { |
| 3750 | mddev->new_chunk_sectors = n >> 9; |
| 3751 | err = mddev->pers->check_reshape(mddev); |
| 3752 | if (err) |
| 3753 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 3754 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3755 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3756 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3757 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3758 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 3759 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3760 | mddev_unlock(mddev); |
| 3761 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3762 | } |
| 3763 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3764 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 3765 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3766 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3767 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3768 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 3769 | if (mddev->recovery_cp == MaxSector) |
| 3770 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3771 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 3772 | } |
| 3773 | |
| 3774 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3775 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3776 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3777 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3778 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3779 | |
| 3780 | if (cmd_match(buf, "none")) |
| 3781 | n = MaxSector; |
| 3782 | else { |
| 3783 | err = kstrtoull(buf, 10, &n); |
| 3784 | if (err < 0) |
| 3785 | return err; |
| 3786 | if (n != (sector_t)n) |
| 3787 | return -EINVAL; |
| 3788 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3789 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3790 | err = mddev_lock(mddev); |
| 3791 | if (err) |
| 3792 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 3793 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3794 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3795 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3796 | if (!err) { |
| 3797 | mddev->recovery_cp = n; |
| 3798 | if (mddev->pers) |
| 3799 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3800 | } |
| 3801 | mddev_unlock(mddev); |
| 3802 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3803 | } |
| 3804 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3805 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 3806 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 3807 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3808 | /* |
| 3809 | * The array state can be: |
| 3810 | * |
| 3811 | * clear |
| 3812 | * No devices, no size, no level |
| 3813 | * Equivalent to STOP_ARRAY ioctl |
| 3814 | * inactive |
| 3815 | * May have some settings, but array is not active |
| 3816 | * all IO results in error |
| 3817 | * When written, doesn't tear down array, but just stops it |
| 3818 | * suspended (not supported yet) |
| 3819 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 3820 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3821 | * readonly |
| 3822 | * no resync can happen. no superblocks get written. |
| 3823 | * write requests fail |
| 3824 | * read-auto |
| 3825 | * like readonly, but behaves like 'clean' on a write request. |
| 3826 | * |
| 3827 | * clean - no pending writes, but otherwise active. |
| 3828 | * When written to inactive array, starts without resync |
| 3829 | * If a write request arrives then |
| 3830 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 3831 | * if not known, block and switch to write-pending |
| 3832 | * If written to an active array that has pending writes, then fails. |
| 3833 | * active |
| 3834 | * fully active: IO and resync can be happening. |
| 3835 | * When written to inactive array, starts with resync |
| 3836 | * |
| 3837 | * write-pending |
| 3838 | * clean, but writes are blocked waiting for 'active' to be written. |
| 3839 | * |
| 3840 | * active-idle |
| 3841 | * like active, but no writes have been seen for a while (100msec). |
| 3842 | * |
| 3843 | */ |
| 3844 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
| 3845 | write_pending, active_idle, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3846 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3847 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
| 3848 | "write-pending", "active-idle", NULL }; |
| 3849 | |
| 3850 | static int match_word(const char *word, char **list) |
| 3851 | { |
| 3852 | int n; |
| 3853 | for (n=0; list[n]; n++) |
| 3854 | if (cmd_match(word, list[n])) |
| 3855 | break; |
| 3856 | return n; |
| 3857 | } |
| 3858 | |
| 3859 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3860 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3861 | { |
| 3862 | enum array_state st = inactive; |
| 3863 | |
| 3864 | if (mddev->pers) |
| 3865 | switch(mddev->ro) { |
| 3866 | case 1: |
| 3867 | st = readonly; |
| 3868 | break; |
| 3869 | case 2: |
| 3870 | st = read_auto; |
| 3871 | break; |
| 3872 | case 0: |
| 3873 | if (mddev->in_sync) |
| 3874 | st = clean; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3875 | else if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3876 | st = write_pending; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3877 | else if (mddev->safemode) |
| 3878 | st = active_idle; |
| 3879 | else |
| 3880 | st = active; |
| 3881 | } |
| 3882 | else { |
| 3883 | if (list_empty(&mddev->disks) && |
| 3884 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3885 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3886 | st = clear; |
| 3887 | else |
| 3888 | st = inactive; |
| 3889 | } |
| 3890 | return sprintf(page, "%s\n", array_states[st]); |
| 3891 | } |
| 3892 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3893 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 3894 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 3895 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3896 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3897 | |
| 3898 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3899 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3900 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3901 | int err; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3902 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3903 | |
| 3904 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 3905 | /* don't take reconfig_mutex when toggling between |
| 3906 | * clean and active |
| 3907 | */ |
| 3908 | spin_lock(&mddev->lock); |
| 3909 | if (st == active) { |
| 3910 | restart_array(mddev); |
| 3911 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
| 3912 | wake_up(&mddev->sb_wait); |
| 3913 | err = 0; |
| 3914 | } else /* st == clean */ { |
| 3915 | restart_array(mddev); |
| 3916 | if (atomic_read(&mddev->writes_pending) == 0) { |
| 3917 | if (mddev->in_sync == 0) { |
| 3918 | mddev->in_sync = 1; |
| 3919 | if (mddev->safemode == 1) |
| 3920 | mddev->safemode = 0; |
| 3921 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
| 3922 | } |
| 3923 | err = 0; |
| 3924 | } else |
| 3925 | err = -EBUSY; |
| 3926 | } |
| 3927 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 3928 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3929 | } |
| 3930 | err = mddev_lock(mddev); |
| 3931 | if (err) |
| 3932 | return err; |
| 3933 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3934 | switch(st) { |
| 3935 | case bad_word: |
| 3936 | break; |
| 3937 | case clear: |
| 3938 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3939 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3940 | break; |
| 3941 | case inactive: |
| 3942 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3943 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3944 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 3945 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3946 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3947 | break; |
| 3948 | case suspended: |
| 3949 | break; /* not supported yet */ |
| 3950 | case readonly: |
| 3951 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3952 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3953 | else { |
| 3954 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3955 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3956 | err = do_md_run(mddev); |
| 3957 | } |
| 3958 | break; |
| 3959 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3960 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3961 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3962 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 3963 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3964 | err = restart_array(mddev); |
| 3965 | if (err == 0) { |
| 3966 | mddev->ro = 2; |
| 3967 | set_disk_ro(mddev->gendisk, 0); |
| 3968 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3969 | } else { |
| 3970 | mddev->ro = 2; |
| 3971 | err = do_md_run(mddev); |
| 3972 | } |
| 3973 | break; |
| 3974 | case clean: |
| 3975 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 3976 | err = restart_array(mddev); |
| 3977 | if (err) |
| 3978 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3979 | spin_lock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3980 | if (atomic_read(&mddev->writes_pending) == 0) { |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3981 | if (mddev->in_sync == 0) { |
| 3982 | mddev->in_sync = 1; |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 3983 | if (mddev->safemode == 1) |
| 3984 | mddev->safemode = 0; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3985 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3986 | } |
| 3987 | err = 0; |
| 3988 | } else |
| 3989 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 3990 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 3991 | } else |
| 3992 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3993 | break; |
| 3994 | case active: |
| 3995 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 3996 | err = restart_array(mddev); |
| 3997 | if (err) |
| 3998 | break; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 3999 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4000 | wake_up(&mddev->sb_wait); |
| 4001 | err = 0; |
| 4002 | } else { |
| 4003 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4004 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4005 | err = do_md_run(mddev); |
| 4006 | } |
| 4007 | break; |
| 4008 | case write_pending: |
| 4009 | case active_idle: |
| 4010 | /* these cannot be set */ |
| 4011 | break; |
| 4012 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4013 | |
| 4014 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4015 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4016 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4017 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4018 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4019 | mddev_unlock(mddev); |
| 4020 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4021 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4022 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4023 | __ATTR_PREALLOC(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4024 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4025 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4026 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4027 | return sprintf(page, "%d\n", |
| 4028 | atomic_read(&mddev->max_corr_read_errors)); |
| 4029 | } |
| 4030 | |
| 4031 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4032 | max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len) |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4033 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4034 | unsigned int n; |
| 4035 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4036 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4037 | rv = kstrtouint(buf, 10, &n); |
| 4038 | if (rv < 0) |
| 4039 | return rv; |
| 4040 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4041 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4042 | } |
| 4043 | |
| 4044 | static struct md_sysfs_entry max_corr_read_errors = |
| 4045 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4046 | max_corrected_read_errors_store); |
| 4047 | |
| 4048 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4049 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4050 | { |
| 4051 | return -EINVAL; |
| 4052 | } |
| 4053 | |
| 4054 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4055 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4056 | { |
| 4057 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4058 | /* The new device is added to the array. |
| 4059 | * If the array has a persistent superblock, we read the |
| 4060 | * superblock to initialise info and check validity. |
| 4061 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4062 | * which mainly checks size. |
| 4063 | */ |
| 4064 | char *e; |
| 4065 | int major = simple_strtoul(buf, &e, 10); |
| 4066 | int minor; |
| 4067 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4068 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4069 | int err; |
| 4070 | |
| 4071 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4072 | return -EINVAL; |
| 4073 | minor = simple_strtoul(e+1, &e, 10); |
| 4074 | if (*e && *e != '\n') |
| 4075 | return -EINVAL; |
| 4076 | dev = MKDEV(major, minor); |
| 4077 | if (major != MAJOR(dev) || |
| 4078 | minor != MINOR(dev)) |
| 4079 | return -EOVERFLOW; |
| 4080 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4081 | flush_workqueue(md_misc_wq); |
| 4082 | |
| 4083 | err = mddev_lock(mddev); |
| 4084 | if (err) |
| 4085 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4086 | if (mddev->persistent) { |
| 4087 | rdev = md_import_device(dev, mddev->major_version, |
| 4088 | mddev->minor_version); |
| 4089 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4090 | struct md_rdev *rdev0 |
| 4091 | = list_entry(mddev->disks.next, |
| 4092 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4093 | err = super_types[mddev->major_version] |
| 4094 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4095 | if (err < 0) |
| 4096 | goto out; |
| 4097 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4098 | } else if (mddev->external) |
| 4099 | rdev = md_import_device(dev, -2, -1); |
| 4100 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4101 | rdev = md_import_device(dev, -1, -1); |
| 4102 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4103 | if (IS_ERR(rdev)) { |
| 4104 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4105 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4106 | } |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4107 | err = bind_rdev_to_array(rdev, mddev); |
| 4108 | out: |
| 4109 | if (err) |
| 4110 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4111 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4112 | return err ? err : len; |
| 4113 | } |
| 4114 | |
| 4115 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4116 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4117 | |
| 4118 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4119 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4120 | { |
| 4121 | char *end; |
| 4122 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4123 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4124 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4125 | err = mddev_lock(mddev); |
| 4126 | if (err) |
| 4127 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4128 | if (!mddev->bitmap) |
| 4129 | goto out; |
| 4130 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4131 | while (*buf) { |
| 4132 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4133 | if (buf == end) break; |
| 4134 | if (*end == '-') { /* range */ |
| 4135 | buf = end + 1; |
| 4136 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4137 | if (buf == end) break; |
| 4138 | } |
| 4139 | if (*end && !isspace(*end)) break; |
| 4140 | bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4141 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4142 | } |
| 4143 | bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
| 4144 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4145 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4146 | return len; |
| 4147 | } |
| 4148 | |
| 4149 | static struct md_sysfs_entry md_bitmap = |
| 4150 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4151 | |
| 4152 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4153 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4154 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4155 | return sprintf(page, "%llu\n", |
| 4156 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4157 | } |
| 4158 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4159 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4160 | |
| 4161 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4162 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4163 | { |
| 4164 | /* If array is inactive, we can reduce the component size, but |
| 4165 | * not increase it (except from 0). |
| 4166 | * If array is active, we can try an on-line resize |
| 4167 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4168 | sector_t sectors; |
| 4169 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4170 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4171 | if (err < 0) |
| 4172 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4173 | err = mddev_lock(mddev); |
| 4174 | if (err) |
| 4175 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4176 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4177 | err = update_size(mddev, sectors); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4178 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4179 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4180 | if (mddev->dev_sectors == 0 || |
| 4181 | mddev->dev_sectors > sectors) |
| 4182 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4183 | else |
| 4184 | err = -ENOSPC; |
| 4185 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4186 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4187 | return err ? err : len; |
| 4188 | } |
| 4189 | |
| 4190 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4191 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4192 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4193 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4194 | * This is one of |
| 4195 | * 'none' for arrays with no metadata (good luck...) |
| 4196 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4197 | * or N.M for internally known formats |
| 4198 | */ |
| 4199 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4200 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4201 | { |
| 4202 | if (mddev->persistent) |
| 4203 | return sprintf(page, "%d.%d\n", |
| 4204 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4205 | else if (mddev->external) |
| 4206 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4207 | else |
| 4208 | return sprintf(page, "none\n"); |
| 4209 | } |
| 4210 | |
| 4211 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4212 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4213 | { |
| 4214 | int major, minor; |
| 4215 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4216 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4217 | /* Changing the details of 'external' metadata is |
| 4218 | * always permitted. Otherwise there must be |
| 4219 | * no devices attached to the array. |
| 4220 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4221 | |
| 4222 | err = mddev_lock(mddev); |
| 4223 | if (err) |
| 4224 | return err; |
| 4225 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4226 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4227 | ; |
| 4228 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4229 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4230 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4231 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4232 | if (cmd_match(buf, "none")) { |
| 4233 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4234 | mddev->external = 0; |
| 4235 | mddev->major_version = 0; |
| 4236 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4237 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4238 | } |
| 4239 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4240 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4241 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4242 | namelen = sizeof(mddev->metadata_type)-1; |
| 4243 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4244 | mddev->metadata_type[namelen] = 0; |
| 4245 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4246 | mddev->metadata_type[--namelen] = 0; |
| 4247 | mddev->persistent = 0; |
| 4248 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4249 | mddev->major_version = 0; |
| 4250 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4251 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4252 | } |
| 4253 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4254 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4255 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4256 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4257 | buf = e+1; |
| 4258 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4259 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4260 | goto out_unlock; |
| 4261 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4262 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4263 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4264 | mddev->major_version = major; |
| 4265 | mddev->minor_version = minor; |
| 4266 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4267 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4268 | err = 0; |
| 4269 | out_unlock: |
| 4270 | mddev_unlock(mddev); |
| 4271 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4272 | } |
| 4273 | |
| 4274 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4275 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4276 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4277 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4278 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4279 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4280 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4281 | unsigned long recovery = mddev->recovery; |
| 4282 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4283 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4284 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4285 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4286 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4287 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4288 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4289 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4290 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4291 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4292 | type = "check"; |
| 4293 | else |
| 4294 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4295 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4296 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4297 | else if (mddev->reshape_position != MaxSector) |
| 4298 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4299 | } |
| 4300 | return sprintf(page, "%s\n", type); |
| 4301 | } |
| 4302 | |
| 4303 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4304 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4305 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4306 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4307 | return -EINVAL; |
| 4308 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4309 | |
| 4310 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4311 | if (cmd_match(page, "frozen")) |
| 4312 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4313 | else |
| 4314 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4315 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4316 | mddev_lock(mddev) == 0) { |
| 4317 | flush_workqueue(md_misc_wq); |
| 4318 | if (mddev->sync_thread) { |
| 4319 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4320 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4321 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4322 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4323 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4324 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4325 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4326 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4327 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4328 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4329 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4330 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4331 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4332 | int err; |
| 4333 | if (mddev->pers->start_reshape == NULL) |
| 4334 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4335 | err = mddev_lock(mddev); |
| 4336 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4337 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4338 | err = -EBUSY; |
| 4339 | else { |
| 4340 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4341 | err = mddev->pers->start_reshape(mddev); |
| 4342 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4343 | mddev_unlock(mddev); |
| 4344 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4345 | if (err) |
| 4346 | return err; |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 4347 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4348 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4349 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4350 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4351 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4352 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4353 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4354 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4355 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4356 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4357 | if (mddev->ro == 2) { |
| 4358 | /* A write to sync_action is enough to justify |
| 4359 | * canceling read-auto mode |
| 4360 | */ |
| 4361 | mddev->ro = 0; |
| 4362 | md_wakeup_thread(mddev->sync_thread); |
| 4363 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4364 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4365 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4366 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4367 | return len; |
| 4368 | } |
| 4369 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4370 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4371 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4372 | |
| 4373 | static ssize_t |
| 4374 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4375 | { |
| 4376 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4377 | } |
| 4378 | |
| 4379 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4380 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4381 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4382 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4383 | { |
| 4384 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4385 | (unsigned long long) |
| 4386 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4387 | } |
| 4388 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4389 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4390 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4391 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4392 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4393 | { |
| 4394 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4395 | mddev->sync_speed_min ? "local": "system"); |
| 4396 | } |
| 4397 | |
| 4398 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4399 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4400 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4401 | unsigned int min; |
| 4402 | int rv; |
| 4403 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4404 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4405 | min = 0; |
| 4406 | } else { |
| 4407 | rv = kstrtouint(buf, 10, &min); |
| 4408 | if (rv < 0) |
| 4409 | return rv; |
| 4410 | if (min == 0) |
| 4411 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4412 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4413 | mddev->sync_speed_min = min; |
| 4414 | return len; |
| 4415 | } |
| 4416 | |
| 4417 | static struct md_sysfs_entry md_sync_min = |
| 4418 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4419 | |
| 4420 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4421 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4422 | { |
| 4423 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4424 | mddev->sync_speed_max ? "local": "system"); |
| 4425 | } |
| 4426 | |
| 4427 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4428 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4429 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4430 | unsigned int max; |
| 4431 | int rv; |
| 4432 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4433 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4434 | max = 0; |
| 4435 | } else { |
| 4436 | rv = kstrtouint(buf, 10, &max); |
| 4437 | if (rv < 0) |
| 4438 | return rv; |
| 4439 | if (max == 0) |
| 4440 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4441 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4442 | mddev->sync_speed_max = max; |
| 4443 | return len; |
| 4444 | } |
| 4445 | |
| 4446 | static struct md_sysfs_entry md_sync_max = |
| 4447 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4448 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4449 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4450 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4451 | { |
| 4452 | return sprintf(page, "%d\n", mddev->degraded); |
| 4453 | } |
| 4454 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4455 | |
| 4456 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4457 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4458 | { |
| 4459 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4460 | } |
| 4461 | |
| 4462 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4463 | sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4464 | { |
| 4465 | long n; |
| 4466 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4467 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4468 | return -EINVAL; |
| 4469 | |
| 4470 | if (n != 0 && n != 1) |
| 4471 | return -EINVAL; |
| 4472 | |
| 4473 | mddev->parallel_resync = n; |
| 4474 | |
| 4475 | if (mddev->sync_thread) |
| 4476 | wake_up(&resync_wait); |
| 4477 | |
| 4478 | return len; |
| 4479 | } |
| 4480 | |
| 4481 | /* force parallel resync, even with shared block devices */ |
| 4482 | static struct md_sysfs_entry md_sync_force_parallel = |
| 4483 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 4484 | sync_force_parallel_show, sync_force_parallel_store); |
| 4485 | |
| 4486 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4487 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4488 | { |
| 4489 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4490 | if (mddev->curr_resync == 0) |
| 4491 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4492 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 4493 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4494 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 4495 | db = resync - mddev->resync_mark_cnt; |
| 4496 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4497 | } |
| 4498 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4499 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4500 | |
| 4501 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4502 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4503 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4504 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4505 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4506 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4507 | return sprintf(page, "none\n"); |
| 4508 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 4509 | if (mddev->curr_resync == 1 || |
| 4510 | mddev->curr_resync == 2) |
| 4511 | return sprintf(page, "delayed\n"); |
| 4512 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 4513 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 4514 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4515 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4516 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4517 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4518 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4519 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4520 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4521 | } |
| 4522 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4523 | static struct md_sysfs_entry md_sync_completed = |
| 4524 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4525 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4526 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4527 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4528 | { |
| 4529 | return sprintf(page, "%llu\n", |
| 4530 | (unsigned long long)mddev->resync_min); |
| 4531 | } |
| 4532 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4533 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4534 | { |
| 4535 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4536 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4537 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4538 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4539 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4540 | |
| 4541 | spin_lock(&mddev->lock); |
| 4542 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4543 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4544 | goto out_unlock; |
| 4545 | |
| 4546 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4547 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4548 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4549 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 4550 | /* Round down to multiple of 4K for safety */ |
| 4551 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4552 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4553 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4554 | out_unlock: |
| 4555 | spin_unlock(&mddev->lock); |
| 4556 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | static struct md_sysfs_entry md_min_sync = |
| 4560 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 4561 | |
| 4562 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4563 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4564 | { |
| 4565 | if (mddev->resync_max == MaxSector) |
| 4566 | return sprintf(page, "max\n"); |
| 4567 | else |
| 4568 | return sprintf(page, "%llu\n", |
| 4569 | (unsigned long long)mddev->resync_max); |
| 4570 | } |
| 4571 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4572 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4573 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4574 | int err; |
| 4575 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4576 | if (strncmp(buf, "max", 3) == 0) |
| 4577 | mddev->resync_max = MaxSector; |
| 4578 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4579 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4580 | int chunk; |
| 4581 | |
| 4582 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4583 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4584 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4585 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4586 | goto out_unlock; |
| 4587 | |
| 4588 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4589 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 4590 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4591 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4592 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4593 | |
| 4594 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4595 | chunk = mddev->chunk_sectors; |
| 4596 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 4597 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4598 | |
| 4599 | err = -EINVAL; |
| 4600 | if (sector_div(temp, chunk)) |
| 4601 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4602 | } |
| 4603 | mddev->resync_max = max; |
| 4604 | } |
| 4605 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4606 | err = 0; |
| 4607 | out_unlock: |
| 4608 | spin_unlock(&mddev->lock); |
| 4609 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4610 | } |
| 4611 | |
| 4612 | static struct md_sysfs_entry md_max_sync = |
| 4613 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 4614 | |
| 4615 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4616 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4617 | { |
| 4618 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 4619 | } |
| 4620 | |
| 4621 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4622 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4623 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4624 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4625 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4626 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4627 | err = kstrtoull(buf, 10, &new); |
| 4628 | if (err < 0) |
| 4629 | return err; |
| 4630 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4631 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4632 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4633 | err = mddev_lock(mddev); |
| 4634 | if (err) |
| 4635 | return err; |
| 4636 | err = -EINVAL; |
| 4637 | if (mddev->pers == NULL || |
| 4638 | mddev->pers->quiesce == NULL) |
| 4639 | goto unlock; |
| 4640 | old = mddev->suspend_lo; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4641 | mddev->suspend_lo = new; |
| 4642 | if (new >= old) |
| 4643 | /* Shrinking suspended region */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4644 | mddev->pers->quiesce(mddev, 2); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4645 | else { |
| 4646 | /* Expanding suspended region - need to wait */ |
| 4647 | mddev->pers->quiesce(mddev, 1); |
| 4648 | mddev->pers->quiesce(mddev, 0); |
| 4649 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4650 | err = 0; |
| 4651 | unlock: |
| 4652 | mddev_unlock(mddev); |
| 4653 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4654 | } |
| 4655 | static struct md_sysfs_entry md_suspend_lo = |
| 4656 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 4657 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4658 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4659 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4660 | { |
| 4661 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 4662 | } |
| 4663 | |
| 4664 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4665 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4666 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4667 | unsigned long long old, new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4668 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4669 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4670 | err = kstrtoull(buf, 10, &new); |
| 4671 | if (err < 0) |
| 4672 | return err; |
| 4673 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4674 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4675 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4676 | err = mddev_lock(mddev); |
| 4677 | if (err) |
| 4678 | return err; |
| 4679 | err = -EINVAL; |
| 4680 | if (mddev->pers == NULL || |
| 4681 | mddev->pers->quiesce == NULL) |
| 4682 | goto unlock; |
| 4683 | old = mddev->suspend_hi; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4684 | mddev->suspend_hi = new; |
| 4685 | if (new <= old) |
| 4686 | /* Shrinking suspended region */ |
| 4687 | mddev->pers->quiesce(mddev, 2); |
| 4688 | else { |
| 4689 | /* Expanding suspended region - need to wait */ |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4690 | mddev->pers->quiesce(mddev, 1); |
| 4691 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4692 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4693 | err = 0; |
| 4694 | unlock: |
| 4695 | mddev_unlock(mddev); |
| 4696 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4697 | } |
| 4698 | static struct md_sysfs_entry md_suspend_hi = |
| 4699 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 4700 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4701 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4702 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4703 | { |
| 4704 | if (mddev->reshape_position != MaxSector) |
| 4705 | return sprintf(page, "%llu\n", |
| 4706 | (unsigned long long)mddev->reshape_position); |
| 4707 | strcpy(page, "none\n"); |
| 4708 | return 5; |
| 4709 | } |
| 4710 | |
| 4711 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4712 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4713 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4714 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4715 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4716 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4717 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4718 | err = kstrtoull(buf, 10, &new); |
| 4719 | if (err < 0) |
| 4720 | return err; |
| 4721 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4722 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4723 | err = mddev_lock(mddev); |
| 4724 | if (err) |
| 4725 | return err; |
| 4726 | err = -EBUSY; |
| 4727 | if (mddev->pers) |
| 4728 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4729 | mddev->reshape_position = new; |
| 4730 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4731 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4732 | mddev->new_level = mddev->level; |
| 4733 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4734 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4735 | rdev_for_each(rdev, mddev) |
| 4736 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4737 | err = 0; |
| 4738 | unlock: |
| 4739 | mddev_unlock(mddev); |
| 4740 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | static struct md_sysfs_entry md_reshape_position = |
| 4744 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 4745 | reshape_position_store); |
| 4746 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4747 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4748 | reshape_direction_show(struct mddev *mddev, char *page) |
| 4749 | { |
| 4750 | return sprintf(page, "%s\n", |
| 4751 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 4752 | } |
| 4753 | |
| 4754 | static ssize_t |
| 4755 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 4756 | { |
| 4757 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4758 | int err; |
| 4759 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4760 | if (cmd_match(buf, "forwards")) |
| 4761 | backwards = 0; |
| 4762 | else if (cmd_match(buf, "backwards")) |
| 4763 | backwards = 1; |
| 4764 | else |
| 4765 | return -EINVAL; |
| 4766 | if (mddev->reshape_backwards == backwards) |
| 4767 | return len; |
| 4768 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4769 | err = mddev_lock(mddev); |
| 4770 | if (err) |
| 4771 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4772 | /* check if we are allowed to change */ |
| 4773 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4774 | err = -EBUSY; |
| 4775 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4776 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4777 | err = -EINVAL; |
| 4778 | else |
| 4779 | mddev->reshape_backwards = backwards; |
| 4780 | mddev_unlock(mddev); |
| 4781 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | static struct md_sysfs_entry md_reshape_direction = |
| 4785 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 4786 | reshape_direction_store); |
| 4787 | |
| 4788 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4789 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4790 | { |
| 4791 | if (mddev->external_size) |
| 4792 | return sprintf(page, "%llu\n", |
| 4793 | (unsigned long long)mddev->array_sectors/2); |
| 4794 | else |
| 4795 | return sprintf(page, "default\n"); |
| 4796 | } |
| 4797 | |
| 4798 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4799 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4800 | { |
| 4801 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4802 | int err; |
| 4803 | |
| 4804 | err = mddev_lock(mddev); |
| 4805 | if (err) |
| 4806 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4807 | |
| 4808 | if (strncmp(buf, "default", 7) == 0) { |
| 4809 | if (mddev->pers) |
| 4810 | sectors = mddev->pers->size(mddev, 0, 0); |
| 4811 | else |
| 4812 | sectors = mddev->array_sectors; |
| 4813 | |
| 4814 | mddev->external_size = 0; |
| 4815 | } else { |
| 4816 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4817 | err = -EINVAL; |
| 4818 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 4819 | err = -E2BIG; |
| 4820 | else |
| 4821 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4822 | } |
| 4823 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4824 | if (!err) { |
| 4825 | mddev->array_sectors = sectors; |
| 4826 | if (mddev->pers) { |
| 4827 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4828 | revalidate_disk(mddev->gendisk); |
| 4829 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 4830 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4831 | mddev_unlock(mddev); |
| 4832 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | static struct md_sysfs_entry md_array_size = |
| 4836 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 4837 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4838 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4839 | static struct attribute *md_default_attrs[] = { |
| 4840 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4841 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4842 | &md_raid_disks.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4843 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4844 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4845 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4846 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4847 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 4848 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4849 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4850 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4851 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4852 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4853 | &max_corr_read_errors.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4854 | NULL, |
| 4855 | }; |
| 4856 | |
| 4857 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4858 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4859 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4860 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4861 | &md_sync_min.attr, |
| 4862 | &md_sync_max.attr, |
| 4863 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4864 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4865 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 4866 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4867 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4868 | &md_suspend_lo.attr, |
| 4869 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4870 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4871 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4872 | NULL, |
| 4873 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 4874 | static struct attribute_group md_redundancy_group = { |
| 4875 | .name = NULL, |
| 4876 | .attrs = md_redundancy_attrs, |
| 4877 | }; |
| 4878 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4879 | static ssize_t |
| 4880 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 4881 | { |
| 4882 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4883 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4884 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4885 | |
| 4886 | if (!entry->show) |
| 4887 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4888 | spin_lock(&all_mddevs_lock); |
| 4889 | if (list_empty(&mddev->all_mddevs)) { |
| 4890 | spin_unlock(&all_mddevs_lock); |
| 4891 | return -EBUSY; |
| 4892 | } |
| 4893 | mddev_get(mddev); |
| 4894 | spin_unlock(&all_mddevs_lock); |
| 4895 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4896 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4897 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4898 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4899 | } |
| 4900 | |
| 4901 | static ssize_t |
| 4902 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 4903 | const char *page, size_t length) |
| 4904 | { |
| 4905 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4906 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4907 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4908 | |
| 4909 | if (!entry->store) |
| 4910 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 4911 | if (!capable(CAP_SYS_ADMIN)) |
| 4912 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4913 | spin_lock(&all_mddevs_lock); |
| 4914 | if (list_empty(&mddev->all_mddevs)) { |
| 4915 | spin_unlock(&all_mddevs_lock); |
| 4916 | return -EBUSY; |
| 4917 | } |
| 4918 | mddev_get(mddev); |
| 4919 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4920 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 4921 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4922 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4923 | } |
| 4924 | |
| 4925 | static void md_free(struct kobject *ko) |
| 4926 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4927 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4928 | |
| 4929 | if (mddev->sysfs_state) |
| 4930 | sysfs_put(mddev->sysfs_state); |
| 4931 | |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 4932 | if (mddev->queue) |
| 4933 | blk_cleanup_queue(mddev->queue); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4934 | if (mddev->gendisk) { |
| 4935 | del_gendisk(mddev->gendisk); |
| 4936 | put_disk(mddev->gendisk); |
| 4937 | } |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 4938 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4939 | kfree(mddev); |
| 4940 | } |
| 4941 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 4942 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4943 | .show = md_attr_show, |
| 4944 | .store = md_attr_store, |
| 4945 | }; |
| 4946 | static struct kobj_type md_ktype = { |
| 4947 | .release = md_free, |
| 4948 | .sysfs_ops = &md_sysfs_ops, |
| 4949 | .default_attrs = md_default_attrs, |
| 4950 | }; |
| 4951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | int mdp_major = 0; |
| 4953 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4954 | static void mddev_delayed_delete(struct work_struct *ws) |
| 4955 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4956 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4957 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 4958 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 4959 | kobject_del(&mddev->kobj); |
| 4960 | kobject_put(&mddev->kobj); |
| 4961 | } |
| 4962 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4963 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | { |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4965 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4966 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4968 | int partitioned; |
| 4969 | int shift; |
| 4970 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 4971 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4972 | |
| 4973 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4974 | return -ENODEV; |
| 4975 | |
| 4976 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 4977 | shift = partitioned ? MdpMinorShift : 0; |
| 4978 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4980 | /* wait for any previous instance of this device to be |
| 4981 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4982 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 4983 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4984 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 4985 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 4986 | error = -EEXIST; |
| 4987 | if (mddev->gendisk) |
| 4988 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4989 | |
| 4990 | if (name) { |
| 4991 | /* Need to ensure that 'name' is not a duplicate. |
| 4992 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4993 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 4994 | spin_lock(&all_mddevs_lock); |
| 4995 | |
| 4996 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 4997 | if (mddev2->gendisk && |
| 4998 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 4999 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5000 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5001 | } |
| 5002 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5003 | } |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5004 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5005 | error = -ENOMEM; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5006 | mddev->queue = blk_alloc_queue(GFP_KERNEL); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5007 | if (!mddev->queue) |
| 5008 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5009 | mddev->queue->queuedata = mddev; |
| 5010 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5011 | blk_queue_make_request(mddev->queue, md_make_request); |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5012 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | disk = alloc_disk(1 << shift); |
| 5015 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5016 | blk_cleanup_queue(mddev->queue); |
| 5017 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5018 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5019 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5020 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5021 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5022 | if (name) |
| 5023 | strcpy(disk->disk_name, name); |
| 5024 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5025 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5026 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | disk->fops = &md_fops; |
| 5029 | disk->private_data = mddev; |
| 5030 | disk->queue = mddev->queue; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5031 | blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5032 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5033 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5034 | * remove it now. |
| 5035 | */ |
| 5036 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5038 | /* As soon as we call add_disk(), another thread could get |
| 5039 | * through to md_open, so make sure it doesn't get too far |
| 5040 | */ |
| 5041 | mutex_lock(&mddev->open_mutex); |
| 5042 | add_disk(disk); |
| 5043 | |
Tejun Heo | ed9e198 | 2008-08-25 19:56:05 +0900 | [diff] [blame] | 5044 | error = kobject_init_and_add(&mddev->kobj, &md_ktype, |
| 5045 | &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5046 | if (error) { |
| 5047 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5048 | * __must_check, we must do something with the result |
| 5049 | */ |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5050 | printk(KERN_WARNING "md: cannot register %s/md - name in use\n", |
| 5051 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5052 | error = 0; |
| 5053 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5054 | if (mddev->kobj.sd && |
| 5055 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5056 | printk(KERN_DEBUG "pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5057 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5058 | abort: |
| 5059 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5060 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5061 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5062 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5063 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5064 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5065 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5069 | { |
| 5070 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | return NULL; |
| 5072 | } |
| 5073 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5074 | static int add_named_array(const char *val, struct kernel_param *kp) |
| 5075 | { |
| 5076 | /* val must be "md_*" where * is not all digits. |
| 5077 | * We allocate an array with a large free minor number, and |
| 5078 | * set the name to val. val must not already be an active name. |
| 5079 | */ |
| 5080 | int len = strlen(val); |
| 5081 | char buf[DISK_NAME_LEN]; |
| 5082 | |
| 5083 | while (len && val[len-1] == '\n') |
| 5084 | len--; |
| 5085 | if (len >= DISK_NAME_LEN) |
| 5086 | return -E2BIG; |
| 5087 | strlcpy(buf, val, len+1); |
| 5088 | if (strncmp(buf, "md_", 3) != 0) |
| 5089 | return -EINVAL; |
| 5090 | return md_alloc(0, buf); |
| 5091 | } |
| 5092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | static void md_safemode_timeout(unsigned long data) |
| 5094 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5095 | struct mddev *mddev = (struct mddev *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5097 | if (!atomic_read(&mddev->writes_pending)) { |
| 5098 | mddev->safemode = 1; |
| 5099 | if (mddev->external) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5100 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 5101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | md_wakeup_thread(mddev->thread); |
| 5103 | } |
| 5104 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5105 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5107 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5108 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5109 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5110 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5111 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5112 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5113 | if (list_empty(&mddev->disks)) |
| 5114 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5116 | |
| 5117 | if (mddev->pers) |
| 5118 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5119 | /* Cannot run until previous stop completes properly */ |
| 5120 | if (mddev->sysfs_active) |
| 5121 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | /* |
| 5124 | * Analyze all RAID superblock(s) |
| 5125 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5126 | if (!mddev->raid_disks) { |
| 5127 | if (!mddev->persistent) |
| 5128 | return -EINVAL; |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5129 | analyze_sbs(mddev); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5131 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5132 | if (mddev->level != LEVEL_NONE) |
| 5133 | request_module("md-level-%d", mddev->level); |
| 5134 | else if (mddev->clevel[0]) |
| 5135 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | |
| 5137 | /* |
| 5138 | * Drop all container device buffers, from now on |
| 5139 | * the only valid external interface is through the md |
| 5140 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5142 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5143 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | continue; |
| 5145 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5146 | invalidate_bdev(rdev->bdev); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5147 | |
| 5148 | /* perform some consistency tests on the device. |
| 5149 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5150 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5151 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5152 | if (rdev->meta_bdev) { |
| 5153 | /* Nothing to check */; |
| 5154 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5155 | if (mddev->dev_sectors && |
| 5156 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5157 | > rdev->sb_start) { |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5158 | printk("md: %s: data overlaps metadata\n", |
| 5159 | mdname(mddev)); |
| 5160 | return -EINVAL; |
| 5161 | } |
| 5162 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5163 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5164 | > rdev->data_offset) { |
| 5165 | printk("md: %s: metadata overlaps data\n", |
| 5166 | mdname(mddev)); |
| 5167 | return -EINVAL; |
| 5168 | } |
| 5169 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5170 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5171 | } |
| 5172 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5173 | if (mddev->bio_set == NULL) |
Kent Overstreet | 395c72a | 2012-09-06 15:34:55 -0700 | [diff] [blame] | 5174 | mddev->bio_set = bioset_create(BIO_POOL_SIZE, 0); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5176 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5177 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5178 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5180 | if (mddev->level != LEVEL_NONE) |
| 5181 | printk(KERN_WARNING "md: personality for level %d is not loaded!\n", |
| 5182 | mddev->level); |
| 5183 | else |
| 5184 | printk(KERN_WARNING "md: personality for level %s is not loaded!\n", |
| 5185 | mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5186 | return -EINVAL; |
| 5187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5188 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5189 | if (mddev->level != pers->level) { |
| 5190 | mddev->level = pers->level; |
| 5191 | mddev->new_level = pers->level; |
| 5192 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5193 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5195 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5196 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5197 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5198 | module_put(pers->owner); |
| 5199 | return -EINVAL; |
| 5200 | } |
| 5201 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5202 | if (pers->sync_request) { |
| 5203 | /* Warn if this is a potentially silly |
| 5204 | * configuration. |
| 5205 | */ |
| 5206 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5207 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5208 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5209 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5210 | rdev_for_each(rdev, mddev) |
| 5211 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5212 | if (rdev < rdev2 && |
| 5213 | rdev->bdev->bd_contains == |
| 5214 | rdev2->bdev->bd_contains) { |
| 5215 | printk(KERN_WARNING |
| 5216 | "%s: WARNING: %s appears to be" |
| 5217 | " on the same physical disk as" |
| 5218 | " %s.\n", |
| 5219 | mdname(mddev), |
| 5220 | bdevname(rdev->bdev,b), |
| 5221 | bdevname(rdev2->bdev,b2)); |
| 5222 | warned = 1; |
| 5223 | } |
| 5224 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5225 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5226 | if (warned) |
| 5227 | printk(KERN_WARNING |
| 5228 | "True protection against single-disk" |
| 5229 | " failure might be compromised.\n"); |
| 5230 | } |
| 5231 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5232 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5233 | /* may be over-ridden by personality */ |
| 5234 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5235 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5236 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5237 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5238 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5239 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5240 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5241 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5242 | if (err) |
| 5243 | printk(KERN_ERR "md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5244 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5245 | WARN_ONCE(!mddev->external_size, "%s: default size too small," |
| 5246 | " but 'external_size' not in effect?\n", __func__); |
| 5247 | printk(KERN_ERR |
| 5248 | "md: invalid array_size %llu > default size %llu\n", |
| 5249 | (unsigned long long)mddev->array_sectors / 2, |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5250 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5251 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5252 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5253 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5254 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5255 | struct bitmap *bitmap; |
| 5256 | |
| 5257 | bitmap = bitmap_create(mddev, -1); |
| 5258 | if (IS_ERR(bitmap)) { |
| 5259 | err = PTR_ERR(bitmap); |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5260 | printk(KERN_ERR "%s: failed to create bitmap (%d)\n", |
| 5261 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5262 | } else |
| 5263 | mddev->bitmap = bitmap; |
| 5264 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5265 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5266 | if (err) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5267 | mddev_detach(mddev); |
NeilBrown | 0c35bd4 | 2015-03-13 11:51:18 +1100 | [diff] [blame] | 5268 | if (mddev->private) |
| 5269 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5270 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5271 | module_put(pers->owner); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5272 | bitmap_destroy(mddev); |
| 5273 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5275 | if (mddev->queue) { |
| 5276 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5277 | mddev->queue->backing_dev_info.congested_fn = md_congested; |
| 5278 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5279 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5280 | if (mddev->kobj.sd && |
| 5281 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5282 | printk(KERN_WARNING |
| 5283 | "md: cannot register extra attributes for %s\n", |
| 5284 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5285 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5286 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 5287 | mddev->ro = 0; |
| 5288 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5289 | atomic_set(&mddev->writes_pending,0); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5290 | atomic_set(&mddev->max_corr_read_errors, |
| 5291 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5292 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5293 | if (mddev_is_clustered(mddev)) |
| 5294 | mddev->safemode_delay = 0; |
| 5295 | else |
| 5296 | mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5298 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5299 | spin_lock(&mddev->lock); |
| 5300 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5301 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5302 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5303 | if (rdev->raid_disk >= 0) |
| 5304 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5305 | /* failure here is OK */; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5306 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 5307 | if (mddev->degraded && !mddev->ro) |
| 5308 | /* This ensures that recovering status is reported immediately |
| 5309 | * via sysfs - until a lack of spares is confirmed. |
| 5310 | */ |
| 5311 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5313 | |
NeilBrown | 7a0a535 | 2013-08-27 16:28:23 +1000 | [diff] [blame] | 5314 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5315 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5317 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5318 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5319 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | a99ac97 | 2008-06-28 08:31:43 +1000 | [diff] [blame] | 5320 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | return 0; |
| 5322 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5323 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5325 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5326 | { |
| 5327 | int err; |
| 5328 | |
| 5329 | err = md_run(mddev); |
| 5330 | if (err) |
| 5331 | goto out; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 5332 | err = bitmap_load(mddev); |
| 5333 | if (err) { |
| 5334 | bitmap_destroy(mddev); |
| 5335 | goto out; |
| 5336 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5337 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5338 | if (mddev_is_clustered(mddev)) |
| 5339 | md_allow_write(mddev); |
| 5340 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 5341 | md_wakeup_thread(mddev->thread); |
| 5342 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 5343 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5344 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5345 | revalidate_disk(mddev->gendisk); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5346 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 5347 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
| 5348 | out: |
| 5349 | return err; |
| 5350 | } |
| 5351 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5352 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | { |
| 5354 | struct gendisk *disk = mddev->gendisk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5356 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5358 | return -ENXIO; |
| 5359 | if (!mddev->pers) |
| 5360 | return -EINVAL; |
| 5361 | if (!mddev->ro) |
| 5362 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 5363 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5364 | struct md_rdev *rdev; |
| 5365 | bool has_journal = false; |
| 5366 | |
| 5367 | rcu_read_lock(); |
| 5368 | rdev_for_each_rcu(rdev, mddev) { |
| 5369 | if (test_bit(Journal, &rdev->flags) && |
| 5370 | !test_bit(Faulty, &rdev->flags)) { |
| 5371 | has_journal = true; |
| 5372 | break; |
| 5373 | } |
| 5374 | } |
| 5375 | rcu_read_unlock(); |
| 5376 | |
| 5377 | /* Don't restart rw with journal missing/faulty */ |
| 5378 | if (!has_journal) |
| 5379 | return -EINVAL; |
| 5380 | } |
| 5381 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5382 | mddev->safemode = 0; |
| 5383 | mddev->ro = 0; |
| 5384 | set_disk_ro(disk, 0); |
| 5385 | printk(KERN_INFO "md: %s switched to read-write mode.\n", |
| 5386 | mdname(mddev)); |
| 5387 | /* Kick recovery or resync if necessary */ |
| 5388 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5389 | md_wakeup_thread(mddev->thread); |
| 5390 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5391 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 5392 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | } |
| 5394 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5395 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5396 | { |
| 5397 | mddev->array_sectors = 0; |
| 5398 | mddev->external_size = 0; |
| 5399 | mddev->dev_sectors = 0; |
| 5400 | mddev->raid_disks = 0; |
| 5401 | mddev->recovery_cp = 0; |
| 5402 | mddev->resync_min = 0; |
| 5403 | mddev->resync_max = MaxSector; |
| 5404 | mddev->reshape_position = MaxSector; |
| 5405 | mddev->external = 0; |
| 5406 | mddev->persistent = 0; |
| 5407 | mddev->level = LEVEL_NONE; |
| 5408 | mddev->clevel[0] = 0; |
| 5409 | mddev->flags = 0; |
| 5410 | mddev->ro = 0; |
| 5411 | mddev->metadata_type[0] = 0; |
| 5412 | mddev->chunk_sectors = 0; |
| 5413 | mddev->ctime = mddev->utime = 0; |
| 5414 | mddev->layout = 0; |
| 5415 | mddev->max_disks = 0; |
| 5416 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 5417 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5418 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5419 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5420 | mddev->new_level = LEVEL_NONE; |
| 5421 | mddev->new_layout = 0; |
| 5422 | mddev->new_chunk_sectors = 0; |
| 5423 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 5424 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5425 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 5426 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 5427 | mddev->recovery = 0; |
| 5428 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5429 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5430 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5431 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5432 | mddev->private = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5433 | mddev->bitmap_info.offset = 0; |
| 5434 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 5435 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5436 | mddev->bitmap_info.chunksize = 0; |
| 5437 | mddev->bitmap_info.daemon_sleep = 0; |
| 5438 | mddev->bitmap_info.max_write_behind = 0; |
| 5439 | } |
| 5440 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5441 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5442 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 5443 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5444 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5445 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5446 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 5447 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5448 | } |
| 5449 | |
| 5450 | del_timer_sync(&mddev->safemode_timer); |
| 5451 | |
| 5452 | bitmap_flush(mddev); |
| 5453 | md_super_wait(mddev); |
| 5454 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 5455 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5456 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
| 5457 | (mddev->flags & MD_UPDATE_SB_FLAGS))) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5458 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 5459 | if (!mddev_is_clustered(mddev)) |
| 5460 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5461 | md_update_sb(mddev, 1); |
| 5462 | } |
| 5463 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5464 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5465 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5466 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 5467 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5468 | __md_stop_writes(mddev); |
| 5469 | mddev_unlock(mddev); |
| 5470 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5471 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 5472 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5473 | static void mddev_detach(struct mddev *mddev) |
| 5474 | { |
| 5475 | struct bitmap *bitmap = mddev->bitmap; |
| 5476 | /* wait for behind writes to complete */ |
| 5477 | if (bitmap && atomic_read(&bitmap->behind_writes) > 0) { |
| 5478 | printk(KERN_INFO "md:%s: behind writes in progress - waiting to stop.\n", |
| 5479 | mdname(mddev)); |
| 5480 | /* need to kick something here to make sure I/O goes? */ |
| 5481 | wait_event(bitmap->behind_wait, |
| 5482 | atomic_read(&bitmap->behind_writes) == 0); |
| 5483 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5484 | if (mddev->pers && mddev->pers->quiesce) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5485 | mddev->pers->quiesce(mddev, 1); |
| 5486 | mddev->pers->quiesce(mddev, 0); |
| 5487 | } |
| 5488 | md_unregister_thread(&mddev->thread); |
| 5489 | if (mddev->queue) |
| 5490 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 5491 | } |
| 5492 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5493 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5494 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5495 | struct md_personality *pers = mddev->pers; |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 5496 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 5497 | /* Ensure ->event_work is done */ |
| 5498 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5499 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5500 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5501 | spin_unlock(&mddev->lock); |
| 5502 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 5503 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5504 | if (pers->sync_request && mddev->to_remove == NULL) |
| 5505 | mddev->to_remove = &md_redundancy_group; |
| 5506 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 5507 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5508 | } |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5509 | |
| 5510 | void md_stop(struct mddev *mddev) |
| 5511 | { |
| 5512 | /* stop the array and free an attached data structures. |
| 5513 | * This is called from dm-raid |
| 5514 | */ |
| 5515 | __md_stop(mddev); |
| 5516 | bitmap_destroy(mddev); |
| 5517 | if (mddev->bio_set) |
| 5518 | bioset_free(mddev->bio_set); |
| 5519 | } |
| 5520 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 5521 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5522 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5523 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5524 | { |
| 5525 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5526 | int did_freeze = 0; |
| 5527 | |
| 5528 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5529 | did_freeze = 1; |
| 5530 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5531 | md_wakeup_thread(mddev->thread); |
| 5532 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5533 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5534 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5535 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5536 | /* Thread might be blocked waiting for metadata update |
| 5537 | * which will now never happen */ |
| 5538 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5539 | |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5540 | if (mddev->external && test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
| 5541 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5542 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5543 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 5544 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 5545 | wait_event(mddev->sb_wait, |
| 5546 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5547 | mddev_lock_nointr(mddev); |
| 5548 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5549 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5550 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5551 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5552 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5553 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5554 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5555 | if (did_freeze) { |
| 5556 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5557 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5558 | md_wakeup_thread(mddev->thread); |
| 5559 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5560 | err = -EBUSY; |
| 5561 | goto out; |
| 5562 | } |
| 5563 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5564 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5565 | |
| 5566 | err = -ENXIO; |
| 5567 | if (mddev->ro==1) |
| 5568 | goto out; |
| 5569 | mddev->ro = 1; |
| 5570 | set_disk_ro(mddev->gendisk, 1); |
| 5571 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5572 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5573 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5574 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5575 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5576 | } |
| 5577 | out: |
| 5578 | mutex_unlock(&mddev->open_mutex); |
| 5579 | return err; |
| 5580 | } |
| 5581 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5582 | /* mode: |
| 5583 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5584 | * 2 - stop but do not disassemble array |
| 5585 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5586 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5587 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5588 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5590 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5591 | int did_freeze = 0; |
| 5592 | |
| 5593 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 5594 | did_freeze = 1; |
| 5595 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 5596 | md_wakeup_thread(mddev->thread); |
| 5597 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5598 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5599 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5600 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5601 | /* Thread might be blocked waiting for metadata update |
| 5602 | * which will now never happen */ |
| 5603 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5604 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5605 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5606 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 5607 | !test_bit(MD_RECOVERY_RUNNING, |
| 5608 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5609 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 5611 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 5612 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5613 | mddev->sysfs_active || |
| 5614 | mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 5615 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5616 | (bdev && !test_bit(MD_STILL_CLOSED, &mddev->flags))) { |
Neil Brown | df5b20c | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 5617 | printk("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5618 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5619 | if (did_freeze) { |
| 5620 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 5621 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5622 | md_wakeup_thread(mddev->thread); |
| 5623 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 5624 | return -EBUSY; |
| 5625 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5626 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5627 | if (mddev->ro) |
| 5628 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5629 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5630 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 5631 | __md_stop(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5632 | mddev->queue->backing_dev_info.congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5633 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5634 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5635 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5636 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5637 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5638 | if (rdev->raid_disk >= 0) |
| 5639 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 5640 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5641 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5642 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 5643 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5644 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 5645 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5646 | if (mddev->ro) |
| 5647 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5648 | } else |
| 5649 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5650 | /* |
| 5651 | * Free resources if final stop |
| 5652 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5653 | if (mode == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | printk(KERN_INFO "md: %s stopped.\n", mdname(mddev)); |
| 5655 | |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5656 | bitmap_destroy(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5657 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5658 | struct file *f = mddev->bitmap_info.file; |
| 5659 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5660 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5661 | spin_unlock(&mddev->lock); |
| 5662 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5663 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5664 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 5665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5666 | export_array(mddev); |
| 5667 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 5668 | md_clean(mddev); |
NeilBrown | 934d9c2 | 2008-10-28 17:01:23 +1100 | [diff] [blame] | 5669 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5670 | if (mddev->hold_active == UNTIL_STOP) |
| 5671 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 5672 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 5673 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5674 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 5675 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5676 | } |
| 5677 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5678 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5679 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5680 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5681 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5682 | int err; |
| 5683 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5684 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5686 | |
| 5687 | printk(KERN_INFO "md: running: "); |
| 5688 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5689 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | char b[BDEVNAME_SIZE]; |
| 5691 | printk("<%s>", bdevname(rdev->bdev,b)); |
| 5692 | } |
| 5693 | printk("\n"); |
| 5694 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5695 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5696 | if (err) { |
| 5697 | printk(KERN_WARNING "md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 5698 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | } |
| 5700 | } |
| 5701 | |
| 5702 | /* |
| 5703 | * lets try to run arrays based on all disks that have arrived |
| 5704 | * until now. (those are in pending_raid_disks) |
| 5705 | * |
| 5706 | * the method: pick the first pending disk, collect all disks with |
| 5707 | * the same UUID, remove all from the pending list and put them into |
| 5708 | * the 'same_array' list. Then order this list based on superblock |
| 5709 | * update time (freshest comes first), kick out 'old' disks and |
| 5710 | * compare superblocks. If everything's fine then run it. |
| 5711 | * |
| 5712 | * If "unit" is allocated, then bump its reference count |
| 5713 | */ |
| 5714 | static void autorun_devices(int part) |
| 5715 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5716 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5717 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5718 | char b[BDEVNAME_SIZE]; |
| 5719 | |
| 5720 | printk(KERN_INFO "md: autorun ...\n"); |
| 5721 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5722 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 5724 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5726 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | |
| 5728 | printk(KERN_INFO "md: considering %s ...\n", |
| 5729 | bdevname(rdev0->bdev,b)); |
| 5730 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5731 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
| 5733 | printk(KERN_INFO "md: adding %s ...\n", |
| 5734 | bdevname(rdev->bdev,b)); |
| 5735 | list_move(&rdev->same_set, &candidates); |
| 5736 | } |
| 5737 | /* |
| 5738 | * now we have a set of devices, with all of them having |
| 5739 | * mostly sane superblocks. It's time to allocate the |
| 5740 | * mddev. |
| 5741 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 5742 | if (part) { |
| 5743 | dev = MKDEV(mdp_major, |
| 5744 | rdev0->preferred_minor << MdpMinorShift); |
| 5745 | unit = MINOR(dev) >> MdpMinorShift; |
| 5746 | } else { |
| 5747 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 5748 | unit = MINOR(dev); |
| 5749 | } |
| 5750 | if (rdev0->preferred_minor != unit) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5751 | printk(KERN_INFO "md: unit number in %s is bad: %d\n", |
| 5752 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
| 5753 | break; |
| 5754 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5755 | |
| 5756 | md_probe(dev, NULL, NULL); |
| 5757 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 5758 | if (!mddev || !mddev->gendisk) { |
| 5759 | if (mddev) |
| 5760 | mddev_put(mddev); |
| 5761 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | "md: cannot allocate memory for md drive.\n"); |
| 5763 | break; |
| 5764 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5765 | if (mddev_lock(mddev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | printk(KERN_WARNING "md: %s locked, cannot run\n", |
| 5767 | mdname(mddev)); |
| 5768 | else if (mddev->raid_disks || mddev->major_version |
| 5769 | || !list_empty(&mddev->disks)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5770 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | "md: %s already running, cannot run %s\n", |
| 5772 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 5773 | mddev_unlock(mddev); |
| 5774 | } else { |
| 5775 | printk(KERN_INFO "md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5776 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5777 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5778 | list_del_init(&rdev->same_set); |
| 5779 | if (bind_rdev_to_array(rdev, mddev)) |
| 5780 | export_rdev(rdev); |
| 5781 | } |
| 5782 | autorun_array(mddev); |
| 5783 | mddev_unlock(mddev); |
| 5784 | } |
| 5785 | /* on success, candidates will be empty, on error |
| 5786 | * it won't... |
| 5787 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5788 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5789 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 5791 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | mddev_put(mddev); |
| 5793 | } |
| 5794 | printk(KERN_INFO "md: ... autorun DONE.\n"); |
| 5795 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 5796 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5798 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | { |
| 5800 | mdu_version_t ver; |
| 5801 | |
| 5802 | ver.major = MD_MAJOR_VERSION; |
| 5803 | ver.minor = MD_MINOR_VERSION; |
| 5804 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 5805 | |
| 5806 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 5807 | return -EFAULT; |
| 5808 | |
| 5809 | return 0; |
| 5810 | } |
| 5811 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5812 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5813 | { |
| 5814 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5815 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5816 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5818 | nr = working = insync = failed = spare = 0; |
| 5819 | rcu_read_lock(); |
| 5820 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5821 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5822 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5823 | failed++; |
| 5824 | else { |
| 5825 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5826 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5827 | insync++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | else |
| 5829 | spare++; |
| 5830 | } |
| 5831 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5832 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | |
| 5834 | info.major_version = mddev->major_version; |
| 5835 | info.minor_version = mddev->minor_version; |
| 5836 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 5837 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5839 | info.size = mddev->dev_sectors / 2; |
| 5840 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 5841 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5842 | info.nr_disks = nr; |
| 5843 | info.raid_disks = mddev->raid_disks; |
| 5844 | info.md_minor = mddev->md_minor; |
| 5845 | info.not_persistent= !mddev->persistent; |
| 5846 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 5847 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | info.state = 0; |
| 5849 | if (mddev->in_sync) |
| 5850 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 5851 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 5852 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 5853 | if (mddev_is_clustered(mddev)) |
| 5854 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5855 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | info.working_disks = working; |
| 5857 | info.failed_disks = failed; |
| 5858 | info.spare_disks = spare; |
| 5859 | |
| 5860 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5861 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5862 | |
| 5863 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5864 | return -EFAULT; |
| 5865 | |
| 5866 | return 0; |
| 5867 | } |
| 5868 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5869 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5870 | { |
| 5871 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5872 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5873 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5874 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5875 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5876 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5877 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5878 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5879 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5880 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 5881 | /* bitmap enabled */ |
| 5882 | if (mddev->bitmap_info.file) { |
| 5883 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 5884 | sizeof(file->pathname)); |
| 5885 | if (IS_ERR(ptr)) |
| 5886 | err = PTR_ERR(ptr); |
| 5887 | else |
| 5888 | memmove(file->pathname, ptr, |
| 5889 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 5890 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5891 | spin_unlock(&mddev->lock); |
| 5892 | |
| 5893 | if (err == 0 && |
| 5894 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5895 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 5896 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 5897 | kfree(file); |
| 5898 | return err; |
| 5899 | } |
| 5900 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5901 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | { |
| 5903 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5904 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | |
| 5906 | if (copy_from_user(&info, arg, sizeof(info))) |
| 5907 | return -EFAULT; |
| 5908 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5909 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 5910 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5911 | if (rdev) { |
| 5912 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 5913 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 5914 | info.raid_disk = rdev->raid_disk; |
| 5915 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5916 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5918 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5919 | info.state |= (1<<MD_DISK_ACTIVE); |
| 5920 | info.state |= (1<<MD_DISK_SYNC); |
| 5921 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 5922 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 5923 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 5924 | if (test_bit(WriteMostly, &rdev->flags)) |
| 5925 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5926 | } else { |
| 5927 | info.major = info.minor = 0; |
| 5928 | info.raid_disk = -1; |
| 5929 | info.state = (1<<MD_DISK_REMOVED); |
| 5930 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 5931 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | |
| 5933 | if (copy_to_user(arg, &info, sizeof(info))) |
| 5934 | return -EFAULT; |
| 5935 | |
| 5936 | return 0; |
| 5937 | } |
| 5938 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5939 | static int add_new_disk(struct mddev *mddev, mdu_disk_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | { |
| 5941 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5942 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | dev_t dev = MKDEV(info->major,info->minor); |
| 5944 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5945 | if (mddev_is_clustered(mddev) && |
| 5946 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 5947 | pr_err("%s: Cannot add to clustered mddev.\n", |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 5948 | mdname(mddev)); |
| 5949 | return -EINVAL; |
| 5950 | } |
| 5951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 5953 | return -EOVERFLOW; |
| 5954 | |
| 5955 | if (!mddev->raid_disks) { |
| 5956 | int err; |
| 5957 | /* expecting a device which has a superblock */ |
| 5958 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 5959 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5960 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5961 | "md: md_import_device returned %ld\n", |
| 5962 | PTR_ERR(rdev)); |
| 5963 | return PTR_ERR(rdev); |
| 5964 | } |
| 5965 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5966 | struct md_rdev *rdev0 |
| 5967 | = list_entry(mddev->disks.next, |
| 5968 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5969 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5970 | .load_super(rdev, rdev0, mddev->minor_version); |
| 5971 | if (err < 0) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5972 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5973 | "md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5974 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | bdevname(rdev0->bdev,b2)); |
| 5976 | export_rdev(rdev); |
| 5977 | return -EINVAL; |
| 5978 | } |
| 5979 | } |
| 5980 | err = bind_rdev_to_array(rdev, mddev); |
| 5981 | if (err) |
| 5982 | export_rdev(rdev); |
| 5983 | return err; |
| 5984 | } |
| 5985 | |
| 5986 | /* |
| 5987 | * add_new_disk can be used once the array is assembled |
| 5988 | * to add "hot spares". They must already have a superblock |
| 5989 | * written |
| 5990 | */ |
| 5991 | if (mddev->pers) { |
| 5992 | int err; |
| 5993 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 5994 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5995 | "%s: personality does not support diskops!\n", |
| 5996 | mdname(mddev)); |
| 5997 | return -EINVAL; |
| 5998 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 5999 | if (mddev->persistent) |
| 6000 | rdev = md_import_device(dev, mddev->major_version, |
| 6001 | mddev->minor_version); |
| 6002 | else |
| 6003 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6005 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | "md: md_import_device returned %ld\n", |
| 6007 | PTR_ERR(rdev)); |
| 6008 | return PTR_ERR(rdev); |
| 6009 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6010 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6011 | if (!mddev->persistent) { |
| 6012 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6013 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6014 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6015 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6016 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6017 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6018 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6019 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6020 | } else |
| 6021 | super_types[mddev->major_version]. |
| 6022 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6023 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6024 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6025 | /* This was a hot-add request, but events doesn't |
| 6026 | * match, so reject it. |
| 6027 | */ |
| 6028 | export_rdev(rdev); |
| 6029 | return -EINVAL; |
| 6030 | } |
| 6031 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6032 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6033 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6034 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6035 | else |
| 6036 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6037 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6038 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6039 | struct md_rdev *rdev2; |
| 6040 | bool has_journal = false; |
| 6041 | |
| 6042 | /* make sure no existing journal disk */ |
| 6043 | rdev_for_each(rdev2, mddev) { |
| 6044 | if (test_bit(Journal, &rdev2->flags)) { |
| 6045 | has_journal = true; |
| 6046 | break; |
| 6047 | } |
| 6048 | } |
| 6049 | if (has_journal) { |
| 6050 | export_rdev(rdev); |
| 6051 | return -EBUSY; |
| 6052 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6053 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6054 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6055 | /* |
| 6056 | * check whether the device shows up in other nodes |
| 6057 | */ |
| 6058 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6059 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6060 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6061 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6062 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6063 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6064 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6065 | export_rdev(rdev); |
| 6066 | return err; |
| 6067 | } |
| 6068 | } |
| 6069 | } |
| 6070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6071 | rdev->raid_disk = -1; |
| 6072 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6074 | if (err) |
| 6075 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6076 | |
| 6077 | if (mddev_is_clustered(mddev)) { |
| 6078 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
| 6079 | md_cluster_ops->new_disk_ack(mddev, (err == 0)); |
| 6080 | else { |
| 6081 | if (err) |
| 6082 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6083 | else |
| 6084 | err = add_bound_rdev(rdev); |
| 6085 | } |
| 6086 | |
| 6087 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6088 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6090 | return err; |
| 6091 | } |
| 6092 | |
| 6093 | /* otherwise, add_new_disk is only allowed |
| 6094 | * for major_version==0 superblocks |
| 6095 | */ |
| 6096 | if (mddev->major_version != 0) { |
| 6097 | printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n", |
| 6098 | mdname(mddev)); |
| 6099 | return -EINVAL; |
| 6100 | } |
| 6101 | |
| 6102 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6103 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6104 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6106 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | "md: error, md_import_device() returned %ld\n", |
| 6108 | PTR_ERR(rdev)); |
| 6109 | return PTR_ERR(rdev); |
| 6110 | } |
| 6111 | rdev->desc_nr = info->number; |
| 6112 | if (info->raid_disk < mddev->raid_disks) |
| 6113 | rdev->raid_disk = info->raid_disk; |
| 6114 | else |
| 6115 | rdev->raid_disk = -1; |
| 6116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6118 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6119 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6121 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6122 | set_bit(WriteMostly, &rdev->flags); |
| 6123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6124 | if (!mddev->persistent) { |
| 6125 | printk(KERN_INFO "md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6126 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6127 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6128 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6129 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6131 | err = bind_rdev_to_array(rdev, mddev); |
| 6132 | if (err) { |
| 6133 | export_rdev(rdev); |
| 6134 | return err; |
| 6135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | return 0; |
| 6139 | } |
| 6140 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6141 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | { |
| 6143 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6144 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | rdev = find_rdev(mddev, dev); |
| 6147 | if (!rdev) |
| 6148 | return -ENXIO; |
| 6149 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6150 | if (rdev->raid_disk < 0) |
| 6151 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6152 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6153 | clear_bit(Blocked, &rdev->flags); |
| 6154 | remove_and_add_spares(mddev, rdev); |
| 6155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6156 | if (rdev->raid_disk >= 0) |
| 6157 | goto busy; |
| 6158 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6159 | kick_rdev: |
Goldwyn Rodrigues | 54a8839 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6160 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6161 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6162 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6163 | md_kick_rdev_from_array(rdev); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6164 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6165 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6166 | |
| 6167 | return 0; |
| 6168 | busy: |
Nick Andrew | fdefa4d | 2008-04-21 22:42:58 +0000 | [diff] [blame] | 6169 | printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6171 | return -EBUSY; |
| 6172 | } |
| 6173 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6174 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6175 | { |
| 6176 | char b[BDEVNAME_SIZE]; |
| 6177 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6178 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | |
| 6180 | if (!mddev->pers) |
| 6181 | return -ENODEV; |
| 6182 | |
| 6183 | if (mddev->major_version != 0) { |
| 6184 | printk(KERN_WARNING "%s: HOT_ADD may only be used with" |
| 6185 | " version-0 superblocks.\n", |
| 6186 | mdname(mddev)); |
| 6187 | return -EINVAL; |
| 6188 | } |
| 6189 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6190 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6191 | "%s: personality does not support diskops!\n", |
| 6192 | mdname(mddev)); |
| 6193 | return -EINVAL; |
| 6194 | } |
| 6195 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6196 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6197 | if (IS_ERR(rdev)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6198 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6199 | "md: error, md_import_device() returned %ld\n", |
| 6200 | PTR_ERR(rdev)); |
| 6201 | return -EINVAL; |
| 6202 | } |
| 6203 | |
| 6204 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6205 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6206 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6207 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6208 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6209 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6210 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6211 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6212 | printk(KERN_WARNING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6213 | "md: can not hot-add faulty %s disk to %s!\n", |
| 6214 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6215 | err = -EINVAL; |
| 6216 | goto abort_export; |
| 6217 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6218 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6219 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6220 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6221 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6222 | err = bind_rdev_to_array(rdev, mddev); |
| 6223 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 6224 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6225 | |
| 6226 | /* |
| 6227 | * The rest should better be atomic, we can have disk failures |
| 6228 | * noticed in interrupt contexts ... |
| 6229 | */ |
| 6230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | rdev->raid_disk = -1; |
| 6232 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6233 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6234 | /* |
| 6235 | * Kick recovery, maybe this spare has to be added to the |
| 6236 | * array immediately. |
| 6237 | */ |
| 6238 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6239 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6240 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | return 0; |
| 6242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | abort_export: |
| 6244 | export_rdev(rdev); |
| 6245 | return err; |
| 6246 | } |
| 6247 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6248 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6249 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6250 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6251 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6252 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 6253 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6254 | return -EBUSY; |
| 6255 | if (mddev->recovery || mddev->sync_thread) |
| 6256 | return -EBUSY; |
| 6257 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6258 | } |
| 6259 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6260 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6261 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6262 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6263 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6264 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 6265 | return -EEXIST; /* cannot add when bitmap is present */ |
| 6266 | f = fget(fd); |
| 6267 | |
| 6268 | if (f == NULL) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6269 | printk(KERN_ERR "%s: error: failed to get bitmap file\n", |
| 6270 | mdname(mddev)); |
| 6271 | return -EBADF; |
| 6272 | } |
| 6273 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6274 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6275 | if (!S_ISREG(inode->i_mode)) { |
| 6276 | printk(KERN_ERR "%s: error: bitmap file must be a regular file\n", |
| 6277 | mdname(mddev)); |
| 6278 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6279 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6280 | printk(KERN_ERR "%s: error: bitmap file must open for write\n", |
| 6281 | mdname(mddev)); |
| 6282 | err = -EBADF; |
| 6283 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6284 | printk(KERN_ERR "%s: error: bitmap file is already in use\n", |
| 6285 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 6286 | err = -EBUSY; |
| 6287 | } |
| 6288 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6289 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6290 | return err; |
| 6291 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6292 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6293 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6294 | } else if (mddev->bitmap == NULL) |
| 6295 | return -ENOENT; /* cannot remove what isn't there */ |
| 6296 | err = 0; |
| 6297 | if (mddev->pers) { |
| 6298 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6299 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6300 | struct bitmap *bitmap; |
| 6301 | |
| 6302 | bitmap = bitmap_create(mddev, -1); |
| 6303 | if (!IS_ERR(bitmap)) { |
| 6304 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6305 | err = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6306 | } else |
| 6307 | err = PTR_ERR(bitmap); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6308 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6309 | if (fd < 0 || err) { |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6310 | bitmap_destroy(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6311 | fd = -1; /* make sure to put the file */ |
| 6312 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6313 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6314 | } |
| 6315 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6316 | struct file *f = mddev->bitmap_info.file; |
| 6317 | if (f) { |
| 6318 | spin_lock(&mddev->lock); |
| 6319 | mddev->bitmap_info.file = NULL; |
| 6320 | spin_unlock(&mddev->lock); |
| 6321 | fput(f); |
| 6322 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6323 | } |
| 6324 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6325 | return err; |
| 6326 | } |
| 6327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6328 | /* |
| 6329 | * set_array_info is used two different ways |
| 6330 | * The original usage is when creating a new array. |
| 6331 | * In this usage, raid_disks is > 0 and it together with |
| 6332 | * level, size, not_persistent,layout,chunksize determine the |
| 6333 | * shape of the array. |
| 6334 | * This will always create an array with a type-0.90.0 superblock. |
| 6335 | * The newer usage is when assembling an array. |
| 6336 | * In this case raid_disks will be 0, and the major_version field is |
| 6337 | * use to determine which style super-blocks are to be found on the devices. |
| 6338 | * The minor and patch _version numbers are also kept incase the |
| 6339 | * super_block handler wishes to interpret them. |
| 6340 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6341 | static int set_array_info(struct mddev *mddev, mdu_array_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6342 | { |
| 6343 | |
| 6344 | if (info->raid_disks == 0) { |
| 6345 | /* just setting version number for superblock loading */ |
| 6346 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 6347 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6348 | super_types[info->major_version].name == NULL) { |
| 6349 | /* maybe try to auto-load a module? */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6350 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6351 | "md: superblock version %d not known\n", |
| 6352 | info->major_version); |
| 6353 | return -EINVAL; |
| 6354 | } |
| 6355 | mddev->major_version = info->major_version; |
| 6356 | mddev->minor_version = info->minor_version; |
| 6357 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6358 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 6359 | /* ensure mddev_put doesn't delete this now that there |
| 6360 | * is some minimal configuration. |
| 6361 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6362 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | return 0; |
| 6364 | } |
| 6365 | mddev->major_version = MD_MAJOR_VERSION; |
| 6366 | mddev->minor_version = MD_MINOR_VERSION; |
| 6367 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6368 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6369 | |
| 6370 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 6371 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6372 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6373 | mddev->raid_disks = info->raid_disks; |
| 6374 | /* don't set md_minor, it is determined by which /dev/md* was |
| 6375 | * openned |
| 6376 | */ |
| 6377 | if (info->state & (1<<MD_SB_CLEAN)) |
| 6378 | mddev->recovery_cp = MaxSector; |
| 6379 | else |
| 6380 | mddev->recovery_cp = 0; |
| 6381 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6382 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | |
| 6384 | mddev->layout = info->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6385 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | |
| 6387 | mddev->max_disks = MD_SB_DISKS; |
| 6388 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 6389 | if (mddev->persistent) |
| 6390 | mddev->flags = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6391 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6392 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6393 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6394 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6395 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 6396 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6397 | mddev->reshape_position = MaxSector; |
| 6398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6399 | /* |
| 6400 | * Generate a 128 bit UUID |
| 6401 | */ |
| 6402 | get_random_bytes(mddev->uuid, 16); |
| 6403 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6404 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6405 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6406 | mddev->new_layout = mddev->layout; |
| 6407 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6408 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6410 | return 0; |
| 6411 | } |
| 6412 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6413 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6414 | { |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6415 | WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__); |
| 6416 | |
| 6417 | if (mddev->external_size) |
| 6418 | return; |
| 6419 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 6420 | mddev->array_sectors = array_sectors; |
| 6421 | } |
| 6422 | EXPORT_SYMBOL(md_set_array_sectors); |
| 6423 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6424 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6425 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6426 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6427 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6428 | int fit = (num_sectors == 0); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6429 | |
| 6430 | if (mddev->pers->resize == NULL) |
| 6431 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6432 | /* The "num_sectors" is the number of sectors of each device that |
| 6433 | * is used. This can only make sense for arrays with redundancy. |
| 6434 | * linear and raid0 always use whatever space is available. We can only |
| 6435 | * consider changing this number if no resync or reconstruction is |
| 6436 | * 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] | 6437 | * 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] | 6438 | * of each device. If num_sectors is zero, we find the largest size |
| 6439 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6440 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6441 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6442 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6443 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6444 | if (mddev->ro) |
| 6445 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6446 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6447 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6448 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 6449 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6450 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 6451 | num_sectors = avail; |
| 6452 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6453 | return -ENOSPC; |
| 6454 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6455 | rv = mddev->pers->resize(mddev, num_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6456 | if (!rv) |
| 6457 | revalidate_disk(mddev->gendisk); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 6458 | return rv; |
| 6459 | } |
| 6460 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6461 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6462 | { |
| 6463 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6464 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6465 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6466 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6467 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 6468 | if (mddev->ro) |
| 6469 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6470 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 6471 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6472 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6473 | if (mddev->sync_thread || |
| 6474 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6475 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6476 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6477 | |
| 6478 | rdev_for_each(rdev, mddev) { |
| 6479 | if (mddev->raid_disks < raid_disks && |
| 6480 | rdev->data_offset < rdev->new_data_offset) |
| 6481 | return -EINVAL; |
| 6482 | if (mddev->raid_disks > raid_disks && |
| 6483 | rdev->data_offset > rdev->new_data_offset) |
| 6484 | return -EINVAL; |
| 6485 | } |
| 6486 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6487 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6488 | if (mddev->delta_disks < 0) |
| 6489 | mddev->reshape_backwards = 1; |
| 6490 | else if (mddev->delta_disks > 0) |
| 6491 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6492 | |
| 6493 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6494 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 6495 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6496 | mddev->reshape_backwards = 0; |
| 6497 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6498 | return rv; |
| 6499 | } |
| 6500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | /* |
| 6502 | * update_array_info is used to change the configuration of an |
| 6503 | * on-line array. |
| 6504 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 6505 | * fields in the info are checked against the array. |
| 6506 | * Any differences that cannot be handled will cause an error. |
| 6507 | * Normally, only one change can be managed at a time. |
| 6508 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6509 | static int update_array_info(struct mddev *mddev, mdu_array_info_t *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | { |
| 6511 | int rv = 0; |
| 6512 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6513 | int state = 0; |
| 6514 | |
| 6515 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6516 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6517 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6518 | |
| 6519 | if (mddev->major_version != info->major_version || |
| 6520 | mddev->minor_version != info->minor_version || |
| 6521 | /* mddev->patch_version != info->patch_version || */ |
| 6522 | mddev->ctime != info->ctime || |
| 6523 | mddev->level != info->level || |
| 6524 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 6525 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6526 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6527 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 6528 | ((state^info->state) & 0xfffffe00) |
| 6529 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6530 | return -EINVAL; |
| 6531 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6532 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 6533 | cnt++; |
| 6534 | if (mddev->raid_disks != info->raid_disks) |
| 6535 | cnt++; |
| 6536 | if (mddev->layout != info->layout) |
| 6537 | cnt++; |
| 6538 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 6539 | cnt++; |
| 6540 | if (cnt == 0) |
| 6541 | return 0; |
| 6542 | if (cnt > 1) |
| 6543 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6544 | |
| 6545 | if (mddev->layout != info->layout) { |
| 6546 | /* Change layout |
| 6547 | * we don't need to do anything at the md level, the |
| 6548 | * personality will take care of it all. |
| 6549 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6550 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6551 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6552 | else { |
| 6553 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6554 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6555 | if (rv) |
| 6556 | mddev->new_layout = mddev->layout; |
| 6557 | return rv; |
| 6558 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6560 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 6561 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6562 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 6563 | if (mddev->raid_disks != info->raid_disks) |
| 6564 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6565 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6566 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6567 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 6568 | rv = -EINVAL; |
| 6569 | goto err; |
| 6570 | } |
| 6571 | if (mddev->recovery || mddev->sync_thread) { |
| 6572 | rv = -EBUSY; |
| 6573 | goto err; |
| 6574 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6575 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6576 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6577 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6578 | if (mddev->bitmap) { |
| 6579 | rv = -EEXIST; |
| 6580 | goto err; |
| 6581 | } |
| 6582 | if (mddev->bitmap_info.default_offset == 0) { |
| 6583 | rv = -EINVAL; |
| 6584 | goto err; |
| 6585 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6586 | mddev->bitmap_info.offset = |
| 6587 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6588 | mddev->bitmap_info.space = |
| 6589 | mddev->bitmap_info.default_space; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6590 | mddev->pers->quiesce(mddev, 1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6591 | bitmap = bitmap_create(mddev, -1); |
| 6592 | if (!IS_ERR(bitmap)) { |
| 6593 | mddev->bitmap = bitmap; |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6594 | rv = bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 6595 | } else |
| 6596 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6597 | if (rv) |
| 6598 | bitmap_destroy(mddev); |
| 6599 | mddev->pers->quiesce(mddev, 0); |
| 6600 | } else { |
| 6601 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6602 | if (!mddev->bitmap) { |
| 6603 | rv = -ENOENT; |
| 6604 | goto err; |
| 6605 | } |
| 6606 | if (mddev->bitmap->storage.file) { |
| 6607 | rv = -EINVAL; |
| 6608 | goto err; |
| 6609 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6610 | if (mddev->bitmap_info.nodes) { |
| 6611 | /* hold PW on all the bitmap lock */ |
| 6612 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
| 6613 | printk("md: can't change bitmap to none since the" |
| 6614 | " array is in use by more than one node\n"); |
| 6615 | rv = -EPERM; |
| 6616 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 6617 | goto err; |
| 6618 | } |
| 6619 | |
| 6620 | mddev->bitmap_info.nodes = 0; |
| 6621 | md_cluster_ops->leave(mddev); |
| 6622 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6623 | mddev->pers->quiesce(mddev, 1); |
| 6624 | bitmap_destroy(mddev); |
| 6625 | mddev->pers->quiesce(mddev, 0); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6626 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6627 | } |
| 6628 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6629 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6630 | return rv; |
| 6631 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6632 | return rv; |
| 6633 | } |
| 6634 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6635 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6636 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6637 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6638 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6639 | |
| 6640 | if (mddev->pers == NULL) |
| 6641 | return -ENODEV; |
| 6642 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6643 | rcu_read_lock(); |
| 6644 | rdev = find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6645 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6646 | err = -ENODEV; |
| 6647 | else { |
| 6648 | md_error(mddev, rdev); |
| 6649 | if (!test_bit(Faulty, &rdev->flags)) |
| 6650 | err = -EBUSY; |
| 6651 | } |
| 6652 | rcu_read_unlock(); |
| 6653 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6654 | } |
| 6655 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 6656 | /* |
| 6657 | * We have a problem here : there is no easy way to give a CHS |
| 6658 | * virtual geometry. We currently pretend that we have a 2 heads |
| 6659 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 6660 | * dosfs just mad... ;-) |
| 6661 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6662 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 6663 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6664 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6665 | |
| 6666 | geo->heads = 2; |
| 6667 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 6668 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 6669 | return 0; |
| 6670 | } |
| 6671 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6672 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 6673 | { |
| 6674 | switch (cmd) { |
| 6675 | case ADD_NEW_DISK: |
| 6676 | case BLKROSET: |
| 6677 | case GET_ARRAY_INFO: |
| 6678 | case GET_BITMAP_FILE: |
| 6679 | case GET_DISK_INFO: |
| 6680 | case HOT_ADD_DISK: |
| 6681 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6682 | case RAID_AUTORUN: |
| 6683 | case RAID_VERSION: |
| 6684 | case RESTART_ARRAY_RW: |
| 6685 | case RUN_ARRAY: |
| 6686 | case SET_ARRAY_INFO: |
| 6687 | case SET_BITMAP_FILE: |
| 6688 | case SET_DISK_FAULTY: |
| 6689 | case STOP_ARRAY: |
| 6690 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6691 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6692 | return true; |
| 6693 | default: |
| 6694 | return false; |
| 6695 | } |
| 6696 | } |
| 6697 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6698 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6699 | unsigned int cmd, unsigned long arg) |
| 6700 | { |
| 6701 | int err = 0; |
| 6702 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6703 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6704 | int ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6705 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 6706 | if (!md_ioctl_valid(cmd)) |
| 6707 | return -ENOTTY; |
| 6708 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 6709 | switch (cmd) { |
| 6710 | case RAID_VERSION: |
| 6711 | case GET_ARRAY_INFO: |
| 6712 | case GET_DISK_INFO: |
| 6713 | break; |
| 6714 | default: |
| 6715 | if (!capable(CAP_SYS_ADMIN)) |
| 6716 | return -EACCES; |
| 6717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | |
| 6719 | /* |
| 6720 | * Commands dealing with the RAID driver but not any |
| 6721 | * particular array: |
| 6722 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6723 | switch (cmd) { |
| 6724 | case RAID_VERSION: |
| 6725 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6726 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6728 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6729 | case RAID_AUTORUN: |
| 6730 | err = 0; |
| 6731 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6732 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6733 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6734 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | } |
| 6736 | |
| 6737 | /* |
| 6738 | * Commands creating/starting a new array: |
| 6739 | */ |
| 6740 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 6741 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6742 | |
| 6743 | if (!mddev) { |
| 6744 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6745 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6746 | } |
| 6747 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6748 | /* Some actions do not requires the mutex */ |
| 6749 | switch (cmd) { |
| 6750 | case GET_ARRAY_INFO: |
| 6751 | if (!mddev->raid_disks && !mddev->external) |
| 6752 | err = -ENODEV; |
| 6753 | else |
| 6754 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6755 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6756 | |
| 6757 | case GET_DISK_INFO: |
| 6758 | if (!mddev->raid_disks && !mddev->external) |
| 6759 | err = -ENODEV; |
| 6760 | else |
| 6761 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6762 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6763 | |
| 6764 | case SET_DISK_FAULTY: |
| 6765 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6766 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6767 | |
| 6768 | case GET_BITMAP_FILE: |
| 6769 | err = get_bitmap_file(mddev, argp); |
| 6770 | goto out; |
| 6771 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6772 | } |
| 6773 | |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 6774 | if (cmd == ADD_NEW_DISK) |
| 6775 | /* need to ensure md_delayed_delete() has completed */ |
| 6776 | flush_workqueue(md_misc_wq); |
| 6777 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 6778 | if (cmd == HOT_REMOVE_DISK) |
| 6779 | /* need to ensure recovery thread has run */ |
| 6780 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 6781 | !test_bit(MD_RECOVERY_NEEDED, |
| 6782 | &mddev->flags), |
| 6783 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6784 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 6785 | /* Need to flush page cache, and ensure no-one else opens |
| 6786 | * and writes |
| 6787 | */ |
| 6788 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6789 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6790 | mutex_unlock(&mddev->open_mutex); |
| 6791 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6792 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6793 | } |
| 6794 | set_bit(MD_STILL_CLOSED, &mddev->flags); |
| 6795 | mutex_unlock(&mddev->open_mutex); |
| 6796 | sync_blockdev(bdev); |
| 6797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6798 | err = mddev_lock(mddev); |
| 6799 | if (err) { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6800 | printk(KERN_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | "md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 6802 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6803 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6804 | } |
| 6805 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6806 | if (cmd == SET_ARRAY_INFO) { |
| 6807 | mdu_array_info_t info; |
| 6808 | if (!arg) |
| 6809 | memset(&info, 0, sizeof(info)); |
| 6810 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 6811 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6812 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6813 | } |
| 6814 | if (mddev->pers) { |
| 6815 | err = update_array_info(mddev, &info); |
| 6816 | if (err) { |
| 6817 | printk(KERN_WARNING "md: couldn't update" |
| 6818 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6819 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6820 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6821 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6822 | } |
| 6823 | if (!list_empty(&mddev->disks)) { |
| 6824 | printk(KERN_WARNING |
| 6825 | "md: array %s already has disks!\n", |
| 6826 | mdname(mddev)); |
| 6827 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6828 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6829 | } |
| 6830 | if (mddev->raid_disks) { |
| 6831 | printk(KERN_WARNING |
| 6832 | "md: array %s already initialised!\n", |
| 6833 | mdname(mddev)); |
| 6834 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6835 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6836 | } |
| 6837 | err = set_array_info(mddev, &info); |
| 6838 | if (err) { |
| 6839 | printk(KERN_WARNING "md: couldn't set" |
| 6840 | " array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6841 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6842 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6843 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6844 | } |
| 6845 | |
| 6846 | /* |
| 6847 | * Commands querying/configuring an existing array: |
| 6848 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6849 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 6850 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 6851 | if ((!mddev->raid_disks && !mddev->external) |
| 6852 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 6853 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 6854 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6855 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6856 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | } |
| 6858 | |
| 6859 | /* |
| 6860 | * Commands even a read-only array can execute: |
| 6861 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6862 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6863 | case RESTART_ARRAY_RW: |
| 6864 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6865 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6866 | |
| 6867 | case STOP_ARRAY: |
| 6868 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6869 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6870 | |
| 6871 | case STOP_ARRAY_RO: |
| 6872 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6873 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6874 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6875 | case HOT_REMOVE_DISK: |
| 6876 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6877 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6878 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6879 | case ADD_NEW_DISK: |
| 6880 | /* We can support ADD_NEW_DISK on read-only arrays |
| 6881 | * on if we are re-adding a preexisting device. |
| 6882 | * So require mddev->pers and MD_DISK_SYNC. |
| 6883 | */ |
| 6884 | if (mddev->pers) { |
| 6885 | mdu_disk_info_t info; |
| 6886 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6887 | err = -EFAULT; |
| 6888 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 6889 | /* Need to clear read-only for this */ |
| 6890 | break; |
| 6891 | else |
| 6892 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6893 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6894 | } |
| 6895 | break; |
| 6896 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6897 | case BLKROSET: |
| 6898 | if (get_user(ro, (int __user *)(arg))) { |
| 6899 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6900 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6901 | } |
| 6902 | err = -EINVAL; |
| 6903 | |
| 6904 | /* if the bdev is going readonly the value of mddev->ro |
| 6905 | * does not matter, no writes are coming |
| 6906 | */ |
| 6907 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6908 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6909 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6910 | /* are we are already prepared for writes? */ |
| 6911 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6912 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6913 | |
| 6914 | /* transitioning to readauto need only happen for |
| 6915 | * arrays that call md_write_start |
| 6916 | */ |
| 6917 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6918 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6919 | if (err == 0) { |
| 6920 | mddev->ro = 2; |
| 6921 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 6922 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6923 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6924 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6925 | } |
| 6926 | |
| 6927 | /* |
| 6928 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6929 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6930 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 6931 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6932 | if (mddev->ro == 2) { |
| 6933 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6934 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 6935 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6936 | /* mddev_unlock will wake thread */ |
| 6937 | /* If a device failed while we were read-only, we |
| 6938 | * need to make sure the metadata is updated now. |
| 6939 | */ |
| 6940 | if (test_bit(MD_CHANGE_DEVS, &mddev->flags)) { |
| 6941 | mddev_unlock(mddev); |
| 6942 | wait_event(mddev->sb_wait, |
| 6943 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) && |
| 6944 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6945 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 6946 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6947 | } else { |
| 6948 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6949 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 6950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | } |
| 6952 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6953 | switch (cmd) { |
| 6954 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6956 | mdu_disk_info_t info; |
| 6957 | if (copy_from_user(&info, argp, sizeof(info))) |
| 6958 | err = -EFAULT; |
| 6959 | else |
| 6960 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6961 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6962 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6964 | case CLUSTERED_DISK_NACK: |
| 6965 | if (mddev_is_clustered(mddev)) |
| 6966 | md_cluster_ops->new_disk_ack(mddev, false); |
| 6967 | else |
| 6968 | err = -EINVAL; |
| 6969 | goto unlock; |
| 6970 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6971 | case HOT_ADD_DISK: |
| 6972 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6973 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6974 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6975 | case RUN_ARRAY: |
| 6976 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6977 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6979 | case SET_BITMAP_FILE: |
| 6980 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6981 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6982 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 6983 | default: |
| 6984 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6985 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | } |
| 6987 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6988 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6989 | if (mddev->hold_active == UNTIL_IOCTL && |
| 6990 | err != -EINVAL) |
| 6991 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6992 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 6993 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6994 | return err; |
| 6995 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 6996 | #ifdef CONFIG_COMPAT |
| 6997 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 6998 | unsigned int cmd, unsigned long arg) |
| 6999 | { |
| 7000 | switch (cmd) { |
| 7001 | case HOT_REMOVE_DISK: |
| 7002 | case HOT_ADD_DISK: |
| 7003 | case SET_DISK_FAULTY: |
| 7004 | case SET_BITMAP_FILE: |
| 7005 | /* These take in integer arg, do not convert */ |
| 7006 | break; |
| 7007 | default: |
| 7008 | arg = (unsigned long)compat_ptr(arg); |
| 7009 | break; |
| 7010 | } |
| 7011 | |
| 7012 | return md_ioctl(bdev, mode, cmd, arg); |
| 7013 | } |
| 7014 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7015 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7016 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7017 | { |
| 7018 | /* |
| 7019 | * Succeed if we can lock the mddev, which confirms that |
| 7020 | * it isn't being stopped right now. |
| 7021 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7022 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | int err; |
| 7024 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7025 | if (!mddev) |
| 7026 | return -ENODEV; |
| 7027 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7028 | if (mddev->gendisk != bdev->bd_disk) { |
| 7029 | /* we are racing with mddev_put which is discarding this |
| 7030 | * bd_disk. |
| 7031 | */ |
| 7032 | mddev_put(mddev); |
| 7033 | /* Wait until bdev->bd_disk is definitely gone */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7034 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7035 | /* Then retry the open from the top */ |
| 7036 | return -ERESTARTSYS; |
| 7037 | } |
| 7038 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7039 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7040 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7041 | goto out; |
| 7042 | |
| 7043 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7044 | atomic_inc(&mddev->openers); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7045 | clear_bit(MD_STILL_CLOSED, &mddev->flags); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7046 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7047 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7048 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | out: |
| 7050 | return err; |
| 7051 | } |
| 7052 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7053 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7054 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7055 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7056 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7057 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7058 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7059 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7061 | |
| 7062 | static int md_media_changed(struct gendisk *disk) |
| 7063 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7064 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7065 | |
| 7066 | return mddev->changed; |
| 7067 | } |
| 7068 | |
| 7069 | static int md_revalidate(struct gendisk *disk) |
| 7070 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7071 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7072 | |
| 7073 | mddev->changed = 0; |
| 7074 | return 0; |
| 7075 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 7076 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7077 | { |
| 7078 | .owner = THIS_MODULE, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7079 | .open = md_open, |
| 7080 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7081 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7082 | #ifdef CONFIG_COMPAT |
| 7083 | .compat_ioctl = md_compat_ioctl, |
| 7084 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7085 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7086 | .media_changed = md_media_changed, |
| 7087 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7088 | }; |
| 7089 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7090 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7091 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7092 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7094 | /* |
| 7095 | * md_thread is a 'system-thread', it's priority should be very |
| 7096 | * high. We avoid resource deadlocks individually in each |
| 7097 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7098 | * the very same RT priority as kswapd, thus we will never get |
| 7099 | * into a priority inversion deadlock. |
| 7100 | * |
| 7101 | * we definitely have to have equal or higher priority than |
| 7102 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7103 | * many dirty RAID5 blocks. |
| 7104 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7105 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7106 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7107 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7109 | /* We need to wait INTERRUPTIBLE so that |
| 7110 | * we don't add to the load-average. |
| 7111 | * That means we need to be sure no signals are |
| 7112 | * pending |
| 7113 | */ |
| 7114 | if (signal_pending(current)) |
| 7115 | flush_signals(current); |
| 7116 | |
| 7117 | wait_event_interruptible_timeout |
| 7118 | (thread->wqueue, |
| 7119 | test_bit(THREAD_WAKEUP, &thread->flags) |
| 7120 | || kthread_should_stop(), |
| 7121 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7122 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7123 | clear_bit(THREAD_WAKEUP, &thread->flags); |
| 7124 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7125 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7126 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7128 | return 0; |
| 7129 | } |
| 7130 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7131 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7132 | { |
| 7133 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7134 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7136 | wake_up(&thread->wqueue); |
| 7137 | } |
| 7138 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7139 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7140 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7141 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7142 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7144 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7146 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7147 | if (!thread) |
| 7148 | return NULL; |
| 7149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | init_waitqueue_head(&thread->wqueue); |
| 7151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | thread->run = run; |
| 7153 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7154 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7155 | thread->tsk = kthread_run(md_thread, thread, |
| 7156 | "%s_%s", |
| 7157 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7158 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7159 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7160 | kfree(thread); |
| 7161 | return NULL; |
| 7162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | return thread; |
| 7164 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7165 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7167 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7168 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7169 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7170 | if (!thread) |
| 7171 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7172 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7173 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7174 | * non-existent thread |
| 7175 | */ |
| 7176 | spin_lock(&pers_lock); |
| 7177 | *threadp = NULL; |
| 7178 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7179 | |
| 7180 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7181 | kfree(thread); |
| 7182 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7183 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7184 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7185 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7186 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7187 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7188 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7189 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7190 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7191 | return; |
| 7192 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7193 | if (mddev->degraded) |
| 7194 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7195 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7196 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7197 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7198 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7199 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7200 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | bb9ef71 | 2015-12-28 10:46:38 +0800 | [diff] [blame] | 7201 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7202 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7203 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | |
| 7205 | /* seq_file implementation /proc/mdstat */ |
| 7206 | |
| 7207 | static void status_unused(struct seq_file *seq) |
| 7208 | { |
| 7209 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7210 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | |
| 7212 | seq_printf(seq, "unused devices: "); |
| 7213 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 7214 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7215 | char b[BDEVNAME_SIZE]; |
| 7216 | i++; |
| 7217 | seq_printf(seq, "%s ", |
| 7218 | bdevname(rdev->bdev,b)); |
| 7219 | } |
| 7220 | if (!i) |
| 7221 | seq_printf(seq, "<none>"); |
| 7222 | |
| 7223 | seq_printf(seq, "\n"); |
| 7224 | } |
| 7225 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7226 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7227 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7228 | sector_t max_sectors, resync, res; |
| 7229 | unsigned long dt, db; |
| 7230 | sector_t rt; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7231 | int scale; |
| 7232 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7233 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7234 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 7235 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7236 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7237 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7238 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7239 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7240 | resync = mddev->curr_resync; |
| 7241 | if (resync <= 3) { |
| 7242 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7243 | /* Still cleaning up */ |
| 7244 | resync = max_sectors; |
| 7245 | } else |
| 7246 | resync -= atomic_read(&mddev->recovery_active); |
| 7247 | |
| 7248 | if (resync == 0) { |
| 7249 | if (mddev->recovery_cp < MaxSector) { |
| 7250 | seq_printf(seq, "\tresync=PENDING"); |
| 7251 | return 1; |
| 7252 | } |
| 7253 | return 0; |
| 7254 | } |
| 7255 | if (resync < 3) { |
| 7256 | seq_printf(seq, "\tresync=DELAYED"); |
| 7257 | return 1; |
| 7258 | } |
| 7259 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 7260 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7261 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7262 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7263 | * u32, as those are the requirements for sector_div. |
| 7264 | * Thus 'scale' must be at least 10 |
| 7265 | */ |
| 7266 | scale = 10; |
| 7267 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7268 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7269 | scale++; |
| 7270 | } |
| 7271 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7272 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7273 | |
| 7274 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7275 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7276 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7277 | seq_printf(seq, "["); |
| 7278 | for (i = 0; i < x; i++) |
| 7279 | seq_printf(seq, "="); |
| 7280 | seq_printf(seq, ">"); |
| 7281 | for (i = 0; i < y; i++) |
| 7282 | seq_printf(seq, "."); |
| 7283 | seq_printf(seq, "] "); |
| 7284 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 7285 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7286 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 7287 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7288 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 7289 | "check" : |
| 7290 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 7291 | "resync" : "recovery"))), |
| 7292 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7293 | (unsigned long long) resync/2, |
| 7294 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7295 | |
| 7296 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7297 | * dt: time from mark until now |
| 7298 | * db: blocks written from mark until now |
| 7299 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7300 | * |
| 7301 | * rt is a sector_t, so could be 32bit or 64bit. |
| 7302 | * So we divide before multiply in case it is 32bit and close |
| 7303 | * to the limit. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 7304 | * We scale the divisor (db) by 32 to avoid losing precision |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7305 | * near the end of resync when the number of remaining sectors |
| 7306 | * is close to 'db'. |
| 7307 | * We then divide rt by 32 after multiplying by db to compensate. |
| 7308 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7309 | */ |
| 7310 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 7311 | if (!dt) dt++; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7312 | db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active)) |
| 7313 | - mddev->resync_mark_cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7314 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 7315 | rt = max_sectors - resync; /* number of remaining sectors */ |
| 7316 | sector_div(rt, db/32+1); |
| 7317 | rt *= dt; |
| 7318 | rt >>= 5; |
| 7319 | |
| 7320 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 7321 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7322 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 7323 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7324 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7325 | } |
| 7326 | |
| 7327 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 7328 | { |
| 7329 | struct list_head *tmp; |
| 7330 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7331 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7332 | |
| 7333 | if (l >= 0x10000) |
| 7334 | return NULL; |
| 7335 | if (!l--) |
| 7336 | /* header */ |
| 7337 | return (void*)1; |
| 7338 | |
| 7339 | spin_lock(&all_mddevs_lock); |
| 7340 | list_for_each(tmp,&all_mddevs) |
| 7341 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7342 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | mddev_get(mddev); |
| 7344 | spin_unlock(&all_mddevs_lock); |
| 7345 | return mddev; |
| 7346 | } |
| 7347 | spin_unlock(&all_mddevs_lock); |
| 7348 | if (!l--) |
| 7349 | return (void*)2;/* tail */ |
| 7350 | return NULL; |
| 7351 | } |
| 7352 | |
| 7353 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 7354 | { |
| 7355 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7356 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | ++*pos; |
| 7359 | if (v == (void*)2) |
| 7360 | return NULL; |
| 7361 | |
| 7362 | spin_lock(&all_mddevs_lock); |
| 7363 | if (v == (void*)1) |
| 7364 | tmp = all_mddevs.next; |
| 7365 | else |
| 7366 | tmp = mddev->all_mddevs.next; |
| 7367 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7368 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7369 | else { |
| 7370 | next_mddev = (void*)2; |
| 7371 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7373 | spin_unlock(&all_mddevs_lock); |
| 7374 | |
| 7375 | if (v != (void*)1) |
| 7376 | mddev_put(mddev); |
| 7377 | return next_mddev; |
| 7378 | |
| 7379 | } |
| 7380 | |
| 7381 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 7382 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7383 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7384 | |
| 7385 | if (mddev && v != (void*)1 && v != (void*)2) |
| 7386 | mddev_put(mddev); |
| 7387 | } |
| 7388 | |
| 7389 | static int md_seq_show(struct seq_file *seq, void *v) |
| 7390 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7391 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7392 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7393 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7394 | |
| 7395 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7396 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7397 | seq_printf(seq, "Personalities : "); |
| 7398 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7399 | list_for_each_entry(pers, &pers_list, list) |
| 7400 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7401 | |
| 7402 | spin_unlock(&pers_lock); |
| 7403 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7404 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7405 | return 0; |
| 7406 | } |
| 7407 | if (v == (void*)2) { |
| 7408 | status_unused(seq); |
| 7409 | return 0; |
| 7410 | } |
| 7411 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7412 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 7414 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 7415 | mddev->pers ? "" : "in"); |
| 7416 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7417 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7418 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7419 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 7420 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | seq_printf(seq, " %s", mddev->pers->name); |
| 7422 | } |
| 7423 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7424 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7425 | rcu_read_lock(); |
| 7426 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7427 | char b[BDEVNAME_SIZE]; |
| 7428 | seq_printf(seq, " %s[%d]", |
| 7429 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7430 | if (test_bit(WriteMostly, &rdev->flags)) |
| 7431 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 7432 | if (test_bit(Journal, &rdev->flags)) |
| 7433 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7434 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7435 | seq_printf(seq, "(F)"); |
| 7436 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7437 | } |
| 7438 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7439 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7440 | if (test_bit(Replacement, &rdev->flags)) |
| 7441 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7442 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7443 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 7444 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7445 | |
| 7446 | if (!list_empty(&mddev->disks)) { |
| 7447 | if (mddev->pers) |
| 7448 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7449 | (unsigned long long) |
| 7450 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | else |
| 7452 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7453 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7454 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7455 | if (mddev->persistent) { |
| 7456 | if (mddev->major_version != 0 || |
| 7457 | mddev->minor_version != 90) { |
| 7458 | seq_printf(seq," super %d.%d", |
| 7459 | mddev->major_version, |
| 7460 | mddev->minor_version); |
| 7461 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7462 | } else if (mddev->external) |
| 7463 | seq_printf(seq, " super external:%s", |
| 7464 | mddev->metadata_type); |
| 7465 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 7466 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7467 | |
| 7468 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7469 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7470 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7471 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 7472 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7473 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 7474 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7475 | } else |
| 7476 | seq_printf(seq, "\n "); |
| 7477 | |
NeilBrown | 5714896 | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 7478 | bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7479 | |
| 7480 | seq_printf(seq, "\n"); |
| 7481 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7482 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7484 | return 0; |
| 7485 | } |
| 7486 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 7487 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7488 | .start = md_seq_start, |
| 7489 | .next = md_seq_next, |
| 7490 | .stop = md_seq_stop, |
| 7491 | .show = md_seq_show, |
| 7492 | }; |
| 7493 | |
| 7494 | static int md_seq_open(struct inode *inode, struct file *file) |
| 7495 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7496 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7497 | int error; |
| 7498 | |
| 7499 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7500 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7501 | return error; |
| 7502 | |
| 7503 | seq = file->private_data; |
| 7504 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7505 | return error; |
| 7506 | } |
| 7507 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7508 | static int md_unloading; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7509 | static unsigned int mdstat_poll(struct file *filp, poll_table *wait) |
| 7510 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7511 | struct seq_file *seq = filp->private_data; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7512 | int mask; |
| 7513 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 7514 | if (md_unloading) |
kbuild test robot | 7d7e64f | 2014-12-03 16:07:59 +1100 | [diff] [blame] | 7515 | return POLLIN|POLLRDNORM|POLLERR|POLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7516 | poll_wait(filp, &md_event_waiters, wait); |
| 7517 | |
| 7518 | /* always allow read */ |
| 7519 | mask = POLLIN | POLLRDNORM; |
| 7520 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 7521 | if (seq->poll_event != atomic_read(&md_event_count)) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7522 | mask |= POLLERR | POLLPRI; |
| 7523 | return mask; |
| 7524 | } |
| 7525 | |
Arjan van de Ven | fa027c2 | 2007-02-12 00:55:33 -0800 | [diff] [blame] | 7526 | static const struct file_operations md_seq_fops = { |
Akinobu Mita | e24650c | 2006-10-17 00:09:38 -0700 | [diff] [blame] | 7527 | .owner = THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7528 | .open = md_seq_open, |
| 7529 | .read = seq_read, |
| 7530 | .llseek = seq_lseek, |
Martin Peschke | c3f94b40 | 2007-05-09 02:35:35 -0700 | [diff] [blame] | 7531 | .release = seq_release_private, |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7532 | .poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7533 | }; |
| 7534 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7535 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7536 | { |
Chao Yu | 50bd377 | 2014-09-25 15:28:34 +0800 | [diff] [blame] | 7537 | printk(KERN_INFO "md: %s personality registered for level %d\n", |
| 7538 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7539 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7540 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7541 | spin_unlock(&pers_lock); |
| 7542 | return 0; |
| 7543 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7544 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7545 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7546 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7547 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7548 | printk(KERN_INFO "md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7549 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7550 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7551 | spin_unlock(&pers_lock); |
| 7552 | return 0; |
| 7553 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7554 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7555 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7556 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 7557 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7558 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7559 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7560 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7561 | if (md_cluster_ops != NULL) |
| 7562 | ret = -EALREADY; |
| 7563 | else { |
| 7564 | md_cluster_ops = ops; |
| 7565 | md_cluster_mod = module; |
| 7566 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7567 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 7568 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7569 | } |
| 7570 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 7571 | |
| 7572 | int unregister_md_cluster_operations(void) |
| 7573 | { |
| 7574 | spin_lock(&pers_lock); |
| 7575 | md_cluster_ops = NULL; |
| 7576 | spin_unlock(&pers_lock); |
| 7577 | return 0; |
| 7578 | } |
| 7579 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 7580 | |
| 7581 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 7582 | { |
| 7583 | int err; |
| 7584 | |
| 7585 | err = request_module("md-cluster"); |
| 7586 | if (err) { |
| 7587 | pr_err("md-cluster module not found.\n"); |
Goldwyn Rodrigues | b0c26a7 | 2015-07-22 12:09:15 -0500 | [diff] [blame] | 7588 | return -ENOENT; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7589 | } |
| 7590 | |
| 7591 | spin_lock(&pers_lock); |
| 7592 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
| 7593 | spin_unlock(&pers_lock); |
| 7594 | return -ENOENT; |
| 7595 | } |
| 7596 | spin_unlock(&pers_lock); |
| 7597 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 7598 | return md_cluster_ops->join(mddev, nodes); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7599 | } |
| 7600 | |
| 7601 | void md_cluster_stop(struct mddev *mddev) |
| 7602 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 7603 | if (!md_cluster_ops) |
| 7604 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 7605 | md_cluster_ops->leave(mddev); |
| 7606 | module_put(md_cluster_mod); |
| 7607 | } |
| 7608 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7609 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7610 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7611 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7612 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7613 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7614 | |
| 7615 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7616 | rcu_read_lock(); |
| 7617 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7618 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7619 | curr_events = (int)part_stat_read(&disk->part0, sectors[0]) + |
| 7620 | (int)part_stat_read(&disk->part0, sectors[1]) - |
| 7621 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 7622 | /* sync IO will cause sync_io to increase before the disk_stats |
| 7623 | * as sync_io is counted when a request starts, and |
| 7624 | * disk_stats is counted when it completes. |
| 7625 | * So resync activity will cause curr_events to be smaller than |
| 7626 | * when there was no such activity. |
| 7627 | * non-sync IO will cause disk_stat to increase without |
| 7628 | * increasing sync_io so curr_events will (eventually) |
| 7629 | * be larger than it was before. Once it becomes |
| 7630 | * substantially larger, the test below will cause |
| 7631 | * the array to appear non-idle, and resync will slow |
| 7632 | * down. |
| 7633 | * If there is a lot of outstanding resync activity when |
| 7634 | * we set last_event to curr_events, then all that activity |
| 7635 | * completing might cause the array to appear non-idle |
| 7636 | * and resync will be slowed down even though there might |
| 7637 | * not have been non-resync activity. This will only |
| 7638 | * happen once though. 'last_events' will soon reflect |
| 7639 | * the state where there is little or no outstanding |
| 7640 | * resync requests, and further resync activity will |
| 7641 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 7642 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7643 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7644 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7645 | rdev->last_events = curr_events; |
| 7646 | idle = 0; |
| 7647 | } |
| 7648 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7649 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7650 | return idle; |
| 7651 | } |
| 7652 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7653 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7654 | { |
| 7655 | /* another "blocks" (512byte) blocks have been synced */ |
| 7656 | atomic_sub(blocks, &mddev->recovery_active); |
| 7657 | wake_up(&mddev->recovery_wait); |
| 7658 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7659 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 7660 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7661 | md_wakeup_thread(mddev->thread); |
| 7662 | // stop recovery, signal do_sync .... |
| 7663 | } |
| 7664 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7665 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7666 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7667 | /* md_write_start(mddev, bi) |
| 7668 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7669 | * in superblock) before writing, schedule a superblock update |
| 7670 | * and wait for it to complete. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7671 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7672 | void md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7673 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7674 | int did_change = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7675 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7676 | return; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7677 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7678 | BUG_ON(mddev->ro == 1); |
| 7679 | if (mddev->ro == 2) { |
| 7680 | /* need to switch to read/write */ |
| 7681 | mddev->ro = 0; |
| 7682 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7683 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 7684 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7685 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7686 | } |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7687 | atomic_inc(&mddev->writes_pending); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 7688 | if (mddev->safemode == 1) |
| 7689 | mddev->safemode = 0; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7690 | if (mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7691 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7692 | if (mddev->in_sync) { |
| 7693 | mddev->in_sync = 0; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7694 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7695 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7696 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7697 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 7698 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7699 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 7700 | } |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7701 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7702 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7703 | wait_event(mddev->sb_wait, |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 7704 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7705 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7706 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7707 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7708 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7709 | { |
| 7710 | if (atomic_dec_and_test(&mddev->writes_pending)) { |
| 7711 | if (mddev->safemode == 2) |
| 7712 | md_wakeup_thread(mddev->thread); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 7713 | else if (mddev->safemode_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7714 | mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay); |
| 7715 | } |
| 7716 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7717 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7718 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7719 | /* md_allow_write(mddev) |
| 7720 | * Calling this ensures that the array is marked 'active' so that writes |
| 7721 | * may proceed without blocking. It is important to call this before |
| 7722 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 7723 | * Must be called with mddev_lock held. |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7724 | * |
Guoqing Jiang | abf3508 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7725 | * In the ->external case MD_CHANGE_PENDING can not be cleared until mddev->lock |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7726 | * is dropped, so return -EAGAIN after notifying userspace. |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7727 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7728 | int md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7729 | { |
| 7730 | if (!mddev->pers) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7731 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7732 | if (mddev->ro) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7733 | return 0; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 7734 | if (!mddev->pers->sync_request) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7735 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7736 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7737 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7738 | if (mddev->in_sync) { |
| 7739 | mddev->in_sync = 0; |
| 7740 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7741 | set_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7742 | if (mddev->safemode_delay && |
| 7743 | mddev->safemode == 0) |
| 7744 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7745 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7746 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7747 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7748 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7749 | spin_unlock(&mddev->lock); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7750 | |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7751 | if (test_bit(MD_CHANGE_PENDING, &mddev->flags)) |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 7752 | return -EAGAIN; |
| 7753 | else |
| 7754 | return 0; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 7755 | } |
| 7756 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 7757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7758 | #define SYNC_MARKS 10 |
| 7759 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7760 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7761 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7762 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7763 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7764 | struct mddev *mddev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7765 | unsigned int currspeed = 0, |
| 7766 | window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 7767 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7768 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7769 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7770 | sector_t mark_cnt[SYNC_MARKS]; |
| 7771 | int last_mark,m; |
| 7772 | struct list_head *tmp; |
| 7773 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7774 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7775 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7776 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7777 | struct blk_plug plug; |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 7778 | bool cluster_resync_finished = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7779 | |
| 7780 | /* just incase thread restarts... */ |
| 7781 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 7782 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7783 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 7784 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7785 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 7786 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7787 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7788 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7789 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7790 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7791 | action = "check"; |
| 7792 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7793 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7794 | action = "repair"; |
| 7795 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7796 | desc = "resync"; |
| 7797 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 7798 | desc = "reshape"; |
| 7799 | else |
| 7800 | desc = "recovery"; |
| 7801 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 7802 | mddev->last_sync_action = action ?: desc; |
| 7803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7804 | /* we overload curr_resync somewhat here. |
| 7805 | * 0 == not engaged in resync at all |
| 7806 | * 2 == checking that there is no conflict with another sync |
| 7807 | * 1 == like 2, but have yielded to allow conflicting resync to |
| 7808 | * commense |
| 7809 | * other == active in resync - this many blocks |
| 7810 | * |
| 7811 | * Before starting a resync we must have set curr_resync to |
| 7812 | * 2, and then checked that every "conflicting" array has curr_resync |
| 7813 | * less than ours. When we find one that is the same or higher |
| 7814 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 7815 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 7816 | * This will mean we have to start checking from the beginning again. |
| 7817 | * |
| 7818 | */ |
| 7819 | |
| 7820 | do { |
| 7821 | mddev->curr_resync = 2; |
| 7822 | |
| 7823 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 7824 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7825 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 7826 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7827 | if (mddev2 == mddev) |
| 7828 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 7829 | if (!mddev->parallel_resync |
| 7830 | && mddev2->curr_resync |
| 7831 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7832 | DEFINE_WAIT(wq); |
| 7833 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 7834 | /* arbitrarily yield */ |
| 7835 | mddev->curr_resync = 1; |
| 7836 | wake_up(&resync_wait); |
| 7837 | } |
| 7838 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 7839 | /* no need to wait here, we can wait the next |
| 7840 | * time 'round when curr_resync == 2 |
| 7841 | */ |
| 7842 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7843 | /* We need to wait 'interruptible' so as not to |
| 7844 | * contribute to the load average, and not to |
| 7845 | * be caught by 'softlockup' |
| 7846 | */ |
| 7847 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7848 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 7849 | mddev2->curr_resync >= mddev->curr_resync) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7850 | printk(KERN_INFO "md: delaying %s of %s" |
| 7851 | " until %s has finished (they" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7852 | " share one or more physical units)\n", |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7853 | desc, mdname(mddev), mdname(mddev2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7854 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 7855 | if (signal_pending(current)) |
| 7856 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7857 | schedule(); |
| 7858 | finish_wait(&resync_wait, &wq); |
| 7859 | goto try_again; |
| 7860 | } |
| 7861 | finish_wait(&resync_wait, &wq); |
| 7862 | } |
| 7863 | } |
| 7864 | } while (mddev->curr_resync < 2); |
| 7865 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7866 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 7867 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7868 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7869 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7870 | */ |
| 7871 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 7872 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7873 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7874 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 7875 | j = mddev->resync_min; |
| 7876 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7877 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 7878 | |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 7879 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 7880 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7881 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7882 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7883 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7884 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7885 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7886 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7887 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7888 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7889 | !test_bit(Faulty, &rdev->flags) && |
| 7890 | !test_bit(In_sync, &rdev->flags) && |
| 7891 | rdev->recovery_offset < j) |
| 7892 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 7893 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 7894 | |
| 7895 | /* If there is a bitmap, we need to make sure all |
| 7896 | * writes that started before we added a spare |
| 7897 | * complete before we start doing a recovery. |
| 7898 | * Otherwise the write might complete and (via |
| 7899 | * bitmap_endwrite) set a bit in the bitmap after the |
| 7900 | * recovery has checked that bit and skipped that |
| 7901 | * region. |
| 7902 | */ |
| 7903 | if (mddev->bitmap) { |
| 7904 | mddev->pers->quiesce(mddev, 1); |
| 7905 | mddev->pers->quiesce(mddev, 0); |
| 7906 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7907 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7908 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7909 | printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 7910 | printk(KERN_INFO "md: minimum _guaranteed_ speed:" |
| 7911 | " %d KB/sec/disk.\n", speed_min(mddev)); |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 7912 | printk(KERN_INFO "md: using maximum available idle IO bandwidth " |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7913 | "(but not more than %d KB/sec) for %s.\n", |
| 7914 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7915 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 7916 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 7917 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7918 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7919 | for (m = 0; m < SYNC_MARKS; m++) { |
| 7920 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7921 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7922 | } |
| 7923 | last_mark = 0; |
| 7924 | mddev->resync_mark = mark[last_mark]; |
| 7925 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 7926 | |
| 7927 | /* |
| 7928 | * Tune reconstruction: |
| 7929 | */ |
| 7930 | window = 32*(PAGE_SIZE/512); |
Jonathan Brassow | ac42450c | 2011-06-07 17:48:35 -0500 | [diff] [blame] | 7931 | printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n", |
| 7932 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7933 | |
| 7934 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7935 | last_check = 0; |
| 7936 | |
| 7937 | if (j>2) { |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7938 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 7939 | "md: resuming %s of %s from checkpoint.\n", |
| 7940 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7941 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7942 | } else |
| 7943 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7944 | mddev->curr_resync_completed = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 7945 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 7946 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7947 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7948 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 7949 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7950 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7951 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7952 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7953 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7954 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7955 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 7956 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 7957 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 7958 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7959 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7960 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 7961 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 7962 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 7963 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7964 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7965 | wait_event(mddev->recovery_wait, |
| 7966 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 7967 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7968 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 7969 | j > mddev->recovery_cp) |
| 7970 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 7971 | update_time = jiffies; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 7972 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7973 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7974 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7975 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7976 | while (j >= mddev->resync_max && |
| 7977 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7978 | /* As this condition is controlled by user-space, |
| 7979 | * we can block indefinitely, so use '_interruptible' |
| 7980 | * to avoid triggering warnings. |
| 7981 | */ |
| 7982 | flush_signals(current); /* just in case */ |
| 7983 | wait_event_interruptible(mddev->recovery_wait, |
| 7984 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7985 | || test_bit(MD_RECOVERY_INTR, |
| 7986 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 7987 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7988 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7989 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 7990 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 7991 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 7992 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7993 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 7994 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 7995 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7996 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 7997 | |
| 7998 | if (!skipped) { /* actual IO requested */ |
| 7999 | io_sectors += sectors; |
| 8000 | atomic_add(sectors, &mddev->recovery_active); |
| 8001 | } |
| 8002 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8003 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8004 | break; |
| 8005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8006 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8007 | if (j > max_sectors) |
| 8008 | /* when skipping, extra large numbers can be returned. */ |
| 8009 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8010 | if (j > 2) |
| 8011 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8012 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8013 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8014 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8015 | * visible in /proc/mdstat |
| 8016 | */ |
| 8017 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8018 | |
| 8019 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8020 | continue; |
| 8021 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8022 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8023 | repeat: |
| 8024 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8025 | /* step marks */ |
| 8026 | int next = (last_mark+1) % SYNC_MARKS; |
| 8027 | |
| 8028 | mddev->resync_mark = mark[next]; |
| 8029 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8030 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8031 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8032 | last_mark = next; |
| 8033 | } |
| 8034 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8035 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8036 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8037 | |
| 8038 | /* |
| 8039 | * this loop exits only if either when we are slower than |
| 8040 | * the 'hard' speed limit, or the system was IO-idle for |
| 8041 | * a jiffy. |
| 8042 | * the system might be non-idle CPU-wise, but we only care |
| 8043 | * about not overloading the IO subsystem. (things like an |
| 8044 | * e2fsck being done on the RAID array should execute fast) |
| 8045 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8046 | cond_resched(); |
| 8047 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8048 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8049 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8050 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8051 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8052 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8053 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8054 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8055 | goto repeat; |
| 8056 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8057 | if (!is_mddev_idle(mddev, 0)) { |
| 8058 | /* |
| 8059 | * Give other IO more of a chance. |
| 8060 | * The faster the devices, the less we wait. |
| 8061 | */ |
| 8062 | wait_event(mddev->recovery_wait, |
| 8063 | !atomic_read(&mddev->recovery_active)); |
| 8064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8065 | } |
| 8066 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8067 | printk(KERN_INFO "md: %s: %s %s.\n",mdname(mddev), desc, |
| 8068 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8069 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8070 | /* |
| 8071 | * this also signals 'finished resyncing' to md_stop |
| 8072 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8073 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8074 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8075 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8076 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8077 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8078 | mddev->curr_resync > 2) { |
| 8079 | mddev->curr_resync_completed = mddev->curr_resync; |
| 8080 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 8081 | } |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8082 | /* tell personality and other nodes that we are finished */ |
| 8083 | if (mddev_is_clustered(mddev)) { |
| 8084 | md_cluster_ops->resync_finish(mddev); |
| 8085 | cluster_resync_finished = true; |
| 8086 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8087 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8088 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8089 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8090 | mddev->curr_resync > 2) { |
| 8091 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 8092 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8093 | if (mddev->curr_resync >= mddev->recovery_cp) { |
| 8094 | printk(KERN_INFO |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8095 | "md: checkpointing %s of %s.\n", |
| 8096 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8097 | if (test_bit(MD_RECOVERY_ERROR, |
| 8098 | &mddev->recovery)) |
| 8099 | mddev->recovery_cp = |
| 8100 | mddev->curr_resync_completed; |
| 8101 | else |
| 8102 | mddev->recovery_cp = |
| 8103 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8104 | } |
| 8105 | } else |
| 8106 | mddev->recovery_cp = MaxSector; |
| 8107 | } else { |
| 8108 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8109 | mddev->curr_resync = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8110 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8111 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8112 | if (rdev->raid_disk >= 0 && |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 8113 | mddev->delta_disks >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8114 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8115 | !test_bit(Faulty, &rdev->flags) && |
| 8116 | !test_bit(In_sync, &rdev->flags) && |
| 8117 | rdev->recovery_offset < mddev->curr_resync) |
| 8118 | rdev->recovery_offset = mddev->curr_resync; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8119 | rcu_read_unlock(); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8121 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 8122 | skip: |
NeilBrown | 1757128 | 2006-12-10 02:20:52 -0800 | [diff] [blame] | 8123 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8124 | |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8125 | if (mddev_is_clustered(mddev) && |
| 8126 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8127 | !cluster_resync_finished) |
| 8128 | md_cluster_ops->resync_finish(mddev); |
| 8129 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8130 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 8131 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8132 | /* We completed so min/max setting can be forgotten if used. */ |
| 8133 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8134 | mddev->resync_min = 0; |
| 8135 | mddev->resync_max = MaxSector; |
| 8136 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8137 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8138 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8139 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8140 | spin_unlock(&mddev->lock); |
| 8141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8142 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8143 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 8144 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8145 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8146 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8147 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8148 | static int remove_and_add_spares(struct mddev *mddev, |
| 8149 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8150 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8151 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8152 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8153 | int removed = 0; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8154 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8155 | rdev_for_each(rdev, mddev) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8156 | if ((this == NULL || rdev == this) && |
| 8157 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8158 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8159 | (test_bit(Faulty, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8160 | (!test_bit(In_sync, &rdev->flags) && |
| 8161 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8162 | atomic_read(&rdev->nr_pending)==0) { |
| 8163 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8164 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8165 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8166 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 8167 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8168 | } |
| 8169 | } |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 8170 | if (removed && mddev->kobj.sd) |
| 8171 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8172 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8173 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8174 | goto no_add; |
| 8175 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8176 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 8177 | if (this && this != rdev) |
| 8178 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 8179 | if (test_bit(Candidate, &rdev->flags)) |
| 8180 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8181 | if (rdev->raid_disk >= 0 && |
| 8182 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8183 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8184 | !test_bit(Faulty, &rdev->flags)) |
| 8185 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8186 | if (rdev->raid_disk >= 0) |
| 8187 | continue; |
| 8188 | if (test_bit(Faulty, &rdev->flags)) |
| 8189 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8190 | if (!test_bit(Journal, &rdev->flags)) { |
| 8191 | if (mddev->ro && |
| 8192 | ! (rdev->saved_raid_disk >= 0 && |
| 8193 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 8194 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8195 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8196 | rdev->recovery_offset = 0; |
| 8197 | } |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8198 | if (mddev->pers-> |
| 8199 | hot_add_disk(mddev, rdev) == 0) { |
| 8200 | if (sysfs_link_rdev(mddev, rdev)) |
| 8201 | /* failure here is OK */; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8202 | if (!test_bit(Journal, &rdev->flags)) |
| 8203 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8204 | md_new_event(mddev); |
| 8205 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8206 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8207 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8208 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 8209 | if (removed) |
| 8210 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8211 | return spares; |
| 8212 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 8213 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8214 | static void md_start_sync(struct work_struct *ws) |
| 8215 | { |
| 8216 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8217 | int ret = 0; |
| 8218 | |
| 8219 | if (mddev_is_clustered(mddev)) { |
| 8220 | ret = md_cluster_ops->resync_start(mddev); |
| 8221 | if (ret) { |
| 8222 | mddev->sync_thread = NULL; |
| 8223 | goto out; |
| 8224 | } |
| 8225 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8226 | |
| 8227 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 8228 | mddev, |
| 8229 | "resync"); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8230 | out: |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8231 | if (!mddev->sync_thread) { |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 8232 | if (!(mddev_is_clustered(mddev) && ret == -EAGAIN)) |
| 8233 | printk(KERN_ERR "%s: could not start resync" |
| 8234 | " thread...\n", |
| 8235 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8236 | /* leave the spares where they are, it shouldn't hurt */ |
| 8237 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8238 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8239 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8240 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 8241 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8242 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8243 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8244 | &mddev->recovery)) |
| 8245 | if (mddev->sysfs_action) |
| 8246 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8247 | } else |
| 8248 | md_wakeup_thread(mddev->sync_thread); |
| 8249 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8250 | md_new_event(mddev); |
| 8251 | } |
| 8252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8253 | /* |
| 8254 | * This routine is regularly called by all per-raid-array threads to |
| 8255 | * deal with generic issues like resync and super-block update. |
| 8256 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 8257 | * need this as they never do any recovery or update the superblock. |
| 8258 | * |
| 8259 | * It does not do any resync itself, but rather "forks" off other threads |
| 8260 | * to do that as needed. |
| 8261 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 8262 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8263 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8264 | * and wakeups up this thread which will reap the thread and finish up. |
| 8265 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 8266 | * |
| 8267 | * The overall approach is: |
| 8268 | * 1/ if the superblock needs updating, update it. |
| 8269 | * 2/ If a recovery thread is running, don't do anything else. |
| 8270 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 8271 | * 4/ If there are any faulty devices, remove them. |
| 8272 | * 5/ If array is degraded, try to add spares devices |
| 8273 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 8274 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8275 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8276 | { |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 8277 | if (mddev->suspended) |
| 8278 | return; |
| 8279 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 8280 | if (mddev->bitmap) |
NeilBrown | aa5cbd1 | 2009-12-14 12:49:46 +1100 | [diff] [blame] | 8281 | bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8282 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8283 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8284 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8285 | printk(KERN_INFO "md: %s in immediate safe mode\n", |
| 8286 | mdname(mddev)); |
| 8287 | mddev->safemode = 2; |
| 8288 | } |
| 8289 | flush_signals(current); |
| 8290 | } |
| 8291 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8292 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 8293 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8294 | if ( ! ( |
NeilBrown | 142d44c | 2013-11-28 10:34:18 +1100 | [diff] [blame] | 8295 | (mddev->flags & MD_UPDATE_SB_FLAGS & ~ (1<<MD_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8296 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8297 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
Guoqing Jiang | 15858fa | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 8298 | test_bit(MD_RELOAD_SB, &mddev->flags) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8299 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8300 | (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending) |
| 8301 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8302 | )) |
| 8303 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8304 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 8305 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8306 | int spares = 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8307 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8308 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 8309 | struct md_rdev *rdev; |
| 8310 | if (!mddev->external && mddev->in_sync) |
| 8311 | /* 'Blocked' flag not needed as failed devices |
| 8312 | * will be recorded if array switched to read/write. |
| 8313 | * Leaving it set will prevent the device |
| 8314 | * from being removed. |
| 8315 | */ |
| 8316 | rdev_for_each(rdev, mddev) |
| 8317 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8318 | /* On a read-only array we can: |
| 8319 | * - remove failed devices |
| 8320 | * - add already-in_sync devices if the array itself |
| 8321 | * is in-sync. |
| 8322 | * As we only add devices that are already in-sync, |
| 8323 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8324 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 8325 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8326 | /* There is no thread, but we need to call |
| 8327 | * ->spare_active and clear saved_raid_disk |
| 8328 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 8329 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8330 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 8331 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 8332 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | d4929ad | 2015-09-18 10:20:12 -0700 | [diff] [blame] | 8333 | clear_bit(MD_CHANGE_PENDING, &mddev->flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 8334 | goto unlock; |
| 8335 | } |
| 8336 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 8337 | if (mddev_is_clustered(mddev)) { |
| 8338 | struct md_rdev *rdev; |
| 8339 | /* kick the device if another node issued a |
| 8340 | * remove disk. |
| 8341 | */ |
| 8342 | rdev_for_each(rdev, mddev) { |
| 8343 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 8344 | rdev->raid_disk < 0) |
| 8345 | md_kick_rdev_from_array(rdev); |
| 8346 | } |
Guoqing Jiang | 15858fa | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 8347 | |
| 8348 | if (test_and_clear_bit(MD_RELOAD_SB, &mddev->flags)) |
| 8349 | md_reload_sb(mddev, mddev->good_device_nr); |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 8350 | } |
| 8351 | |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8352 | if (!mddev->external) { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8353 | int did_change = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8354 | spin_lock(&mddev->lock); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8355 | if (mddev->safemode && |
| 8356 | !atomic_read(&mddev->writes_pending) && |
| 8357 | !mddev->in_sync && |
| 8358 | mddev->recovery_cp == MaxSector) { |
| 8359 | mddev->in_sync = 1; |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8360 | did_change = 1; |
NeilBrown | 070dc6d | 2010-08-30 17:33:34 +1000 | [diff] [blame] | 8361 | set_bit(MD_CHANGE_CLEAN, &mddev->flags); |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8362 | } |
| 8363 | if (mddev->safemode == 1) |
| 8364 | mddev->safemode = 0; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8365 | spin_unlock(&mddev->lock); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8366 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8367 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8368 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 8369 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 8370 | if (mddev->flags & MD_UPDATE_SB_FLAGS) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 8371 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8373 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 8374 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 8375 | /* resync/recovery still happening */ |
| 8376 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8377 | goto unlock; |
| 8378 | } |
| 8379 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8380 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8381 | goto unlock; |
| 8382 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8383 | /* Set RUNNING before clearing NEEDED to avoid |
| 8384 | * any transients in the value of "sync_action". |
| 8385 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8386 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8387 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8388 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 8389 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8390 | /* Clear some bits that don't mean anything, but |
| 8391 | * might be left set |
| 8392 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8393 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8394 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8395 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 8396 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 8397 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8398 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8399 | /* no recovery is running. |
| 8400 | * remove any failed drives, then |
| 8401 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8402 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8403 | * the personality to fail the re-add. |
| 8404 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8405 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8406 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8407 | if (mddev->pers->check_reshape == NULL || |
| 8408 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8409 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8410 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 8411 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8412 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 8413 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8414 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8415 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 8416 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8417 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8418 | } else if (mddev->recovery_cp < MaxSector) { |
| 8419 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8420 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8421 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 8422 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8423 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8425 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 8426 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 8427 | /* We are adding a device or devices to an array |
| 8428 | * which has the bitmap stored on all devices. |
| 8429 | * So make sure all bitmap pages get written |
| 8430 | */ |
| 8431 | bitmap_write_all(mddev->bitmap); |
| 8432 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8433 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 8434 | queue_work(md_misc_wq, &mddev->del_work); |
| 8435 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8436 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8437 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8438 | if (!mddev->sync_thread) { |
| 8439 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8440 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8441 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 8442 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 8443 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8444 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8445 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 8446 | unlock: |
| 8447 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8448 | mddev_unlock(mddev); |
| 8449 | } |
| 8450 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8451 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8452 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8453 | void md_reap_sync_thread(struct mddev *mddev) |
| 8454 | { |
| 8455 | struct md_rdev *rdev; |
| 8456 | |
| 8457 | /* resync has finished, collect result */ |
| 8458 | md_unregister_thread(&mddev->sync_thread); |
| 8459 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 8460 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
| 8461 | /* success...*/ |
| 8462 | /* activate any spares */ |
| 8463 | if (mddev->pers->spare_active(mddev)) { |
| 8464 | sysfs_notify(&mddev->kobj, NULL, |
| 8465 | "degraded"); |
| 8466 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 8467 | } |
| 8468 | } |
| 8469 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8470 | mddev->pers->finish_reshape) |
| 8471 | mddev->pers->finish_reshape(mddev); |
| 8472 | |
| 8473 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8474 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8475 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 8476 | if (!mddev->degraded) |
| 8477 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8478 | rdev->saved_raid_disk = -1; |
| 8479 | |
| 8480 | md_update_sb(mddev, 1); |
| 8481 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8482 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8483 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8484 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8485 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 8486 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 8487 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8488 | /* flag recovery needed just to double check */ |
| 8489 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8490 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 8491 | md_new_event(mddev); |
| 8492 | if (mddev->event_work.func) |
| 8493 | queue_work(md_misc_wq, &mddev->event_work); |
| 8494 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8495 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 8496 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8497 | void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8498 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8499 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8500 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8501 | !test_bit(Blocked, &rdev->flags) && |
| 8502 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8503 | msecs_to_jiffies(5000)); |
| 8504 | rdev_dec_pending(rdev, mddev); |
| 8505 | } |
| 8506 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 8507 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8508 | void md_finish_reshape(struct mddev *mddev) |
| 8509 | { |
| 8510 | /* called be personality module when reshape completes. */ |
| 8511 | struct md_rdev *rdev; |
| 8512 | |
| 8513 | rdev_for_each(rdev, mddev) { |
| 8514 | if (rdev->data_offset > rdev->new_data_offset) |
| 8515 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 8516 | else |
| 8517 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 8518 | rdev->data_offset = rdev->new_data_offset; |
| 8519 | } |
| 8520 | } |
| 8521 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8522 | |
| 8523 | /* Bad block management. |
| 8524 | * We can record which blocks on each device are 'bad' and so just |
| 8525 | * fail those blocks, or that stripe, rather than the whole device. |
| 8526 | * Entries in the bad-block table are 64bits wide. This comprises: |
| 8527 | * Length of bad-range, in sectors: 0-511 for lengths 1-512 |
| 8528 | * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes) |
| 8529 | * A 'shift' can be set so that larger blocks are tracked and |
| 8530 | * consequently larger devices can be covered. |
| 8531 | * 'Acknowledged' flag - 1 bit. - the most significant bit. |
| 8532 | * |
| 8533 | * Locking of the bad-block table uses a seqlock so md_is_badblock |
| 8534 | * might need to retry if it is very unlucky. |
| 8535 | * We will sometimes want to check for bad blocks in a bi_end_io function, |
| 8536 | * so we use the write_seqlock_irq variant. |
| 8537 | * |
| 8538 | * When looking for a bad block we specify a range and want to |
| 8539 | * know if any block in the range is bad. So we binary-search |
| 8540 | * to the last range that starts at-or-before the given endpoint, |
| 8541 | * (or "before the sector after the target range") |
| 8542 | * then see if it ends after the given start. |
| 8543 | * We return |
| 8544 | * 0 if there are no known bad blocks in the range |
| 8545 | * 1 if there are known bad block which are all acknowledged |
| 8546 | * -1 if there are bad blocks which have not yet been acknowledged in metadata. |
| 8547 | * plus the start/length of the first bad section we overlap. |
| 8548 | */ |
| 8549 | int md_is_badblock(struct badblocks *bb, sector_t s, int sectors, |
| 8550 | sector_t *first_bad, int *bad_sectors) |
| 8551 | { |
| 8552 | int hi; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8553 | int lo; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8554 | u64 *p = bb->page; |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8555 | int rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8556 | sector_t target = s + sectors; |
| 8557 | unsigned seq; |
| 8558 | |
| 8559 | if (bb->shift > 0) { |
| 8560 | /* round the start down, and the end up */ |
| 8561 | s >>= bb->shift; |
| 8562 | target += (1<<bb->shift) - 1; |
| 8563 | target >>= bb->shift; |
| 8564 | sectors = target - s; |
| 8565 | } |
| 8566 | /* 'target' is now the first block after the bad range */ |
| 8567 | |
| 8568 | retry: |
| 8569 | seq = read_seqbegin(&bb->lock); |
majianpeng | ab05613 | 2012-11-06 17:13:44 +0800 | [diff] [blame] | 8570 | lo = 0; |
| 8571 | rv = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8572 | hi = bb->count; |
| 8573 | |
| 8574 | /* Binary search between lo and hi for 'target' |
| 8575 | * i.e. for the last range that starts before 'target' |
| 8576 | */ |
| 8577 | /* INVARIANT: ranges before 'lo' and at-or-after 'hi' |
| 8578 | * are known not to be the last range before target. |
| 8579 | * VARIANT: hi-lo is the number of possible |
| 8580 | * ranges, and decreases until it reaches 1 |
| 8581 | */ |
| 8582 | while (hi - lo > 1) { |
| 8583 | int mid = (lo + hi) / 2; |
| 8584 | sector_t a = BB_OFFSET(p[mid]); |
| 8585 | if (a < target) |
| 8586 | /* This could still be the one, earlier ranges |
| 8587 | * could not. */ |
| 8588 | lo = mid; |
| 8589 | else |
| 8590 | /* This and later ranges are definitely out. */ |
| 8591 | hi = mid; |
| 8592 | } |
| 8593 | /* 'lo' might be the last that started before target, but 'hi' isn't */ |
| 8594 | if (hi > lo) { |
| 8595 | /* need to check all range that end after 's' to see if |
| 8596 | * any are unacknowledged. |
| 8597 | */ |
| 8598 | while (lo >= 0 && |
| 8599 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8600 | if (BB_OFFSET(p[lo]) < target) { |
| 8601 | /* starts before the end, and finishes after |
| 8602 | * the start, so they must overlap |
| 8603 | */ |
| 8604 | if (rv != -1 && BB_ACK(p[lo])) |
| 8605 | rv = 1; |
| 8606 | else |
| 8607 | rv = -1; |
| 8608 | *first_bad = BB_OFFSET(p[lo]); |
| 8609 | *bad_sectors = BB_LEN(p[lo]); |
| 8610 | } |
| 8611 | lo--; |
| 8612 | } |
| 8613 | } |
| 8614 | |
| 8615 | if (read_seqretry(&bb->lock, seq)) |
| 8616 | goto retry; |
| 8617 | |
| 8618 | return rv; |
| 8619 | } |
| 8620 | EXPORT_SYMBOL_GPL(md_is_badblock); |
| 8621 | |
| 8622 | /* |
| 8623 | * Add a range of bad blocks to the table. |
| 8624 | * This might extend the table, or might contract it |
| 8625 | * if two adjacent ranges can be merged. |
| 8626 | * We binary-search to find the 'insertion' point, then |
| 8627 | * decide how best to handle it. |
| 8628 | */ |
| 8629 | static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors, |
| 8630 | int acknowledged) |
| 8631 | { |
| 8632 | u64 *p; |
| 8633 | int lo, hi; |
| 8634 | int rv = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8635 | unsigned long flags; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8636 | |
| 8637 | if (bb->shift < 0) |
| 8638 | /* badblocks are disabled */ |
| 8639 | return 0; |
| 8640 | |
| 8641 | if (bb->shift) { |
| 8642 | /* round the start down, and the end up */ |
| 8643 | sector_t next = s + sectors; |
| 8644 | s >>= bb->shift; |
| 8645 | next += (1<<bb->shift) - 1; |
| 8646 | next >>= bb->shift; |
| 8647 | sectors = next - s; |
| 8648 | } |
| 8649 | |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8650 | write_seqlock_irqsave(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8651 | |
| 8652 | p = bb->page; |
| 8653 | lo = 0; |
| 8654 | hi = bb->count; |
| 8655 | /* Find the last range that starts at-or-before 's' */ |
| 8656 | while (hi - lo > 1) { |
| 8657 | int mid = (lo + hi) / 2; |
| 8658 | sector_t a = BB_OFFSET(p[mid]); |
| 8659 | if (a <= s) |
| 8660 | lo = mid; |
| 8661 | else |
| 8662 | hi = mid; |
| 8663 | } |
| 8664 | if (hi > lo && BB_OFFSET(p[lo]) > s) |
| 8665 | hi = lo; |
| 8666 | |
| 8667 | if (hi > lo) { |
| 8668 | /* we found a range that might merge with the start |
| 8669 | * of our new range |
| 8670 | */ |
| 8671 | sector_t a = BB_OFFSET(p[lo]); |
| 8672 | sector_t e = a + BB_LEN(p[lo]); |
| 8673 | int ack = BB_ACK(p[lo]); |
| 8674 | if (e >= s) { |
| 8675 | /* Yes, we can merge with a previous range */ |
| 8676 | if (s == a && s + sectors >= e) |
| 8677 | /* new range covers old */ |
| 8678 | ack = acknowledged; |
| 8679 | else |
| 8680 | ack = ack && acknowledged; |
| 8681 | |
| 8682 | if (e < s + sectors) |
| 8683 | e = s + sectors; |
| 8684 | if (e - a <= BB_MAX_LEN) { |
| 8685 | p[lo] = BB_MAKE(a, e-a, ack); |
| 8686 | s = e; |
| 8687 | } else { |
| 8688 | /* does not all fit in one range, |
| 8689 | * make p[lo] maximal |
| 8690 | */ |
| 8691 | if (BB_LEN(p[lo]) != BB_MAX_LEN) |
| 8692 | p[lo] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8693 | s = a + BB_MAX_LEN; |
| 8694 | } |
| 8695 | sectors = e - s; |
| 8696 | } |
| 8697 | } |
| 8698 | if (sectors && hi < bb->count) { |
| 8699 | /* 'hi' points to the first range that starts after 's'. |
| 8700 | * Maybe we can merge with the start of that range */ |
| 8701 | sector_t a = BB_OFFSET(p[hi]); |
| 8702 | sector_t e = a + BB_LEN(p[hi]); |
| 8703 | int ack = BB_ACK(p[hi]); |
| 8704 | if (a <= s + sectors) { |
| 8705 | /* merging is possible */ |
| 8706 | if (e <= s + sectors) { |
| 8707 | /* full overlap */ |
| 8708 | e = s + sectors; |
| 8709 | ack = acknowledged; |
| 8710 | } else |
| 8711 | ack = ack && acknowledged; |
| 8712 | |
| 8713 | a = s; |
| 8714 | if (e - a <= BB_MAX_LEN) { |
| 8715 | p[hi] = BB_MAKE(a, e-a, ack); |
| 8716 | s = e; |
| 8717 | } else { |
| 8718 | p[hi] = BB_MAKE(a, BB_MAX_LEN, ack); |
| 8719 | s = a + BB_MAX_LEN; |
| 8720 | } |
| 8721 | sectors = e - s; |
| 8722 | lo = hi; |
| 8723 | hi++; |
| 8724 | } |
| 8725 | } |
| 8726 | if (sectors == 0 && hi < bb->count) { |
| 8727 | /* we might be able to combine lo and hi */ |
| 8728 | /* Note: 's' is at the end of 'lo' */ |
| 8729 | sector_t a = BB_OFFSET(p[hi]); |
| 8730 | int lolen = BB_LEN(p[lo]); |
| 8731 | int hilen = BB_LEN(p[hi]); |
| 8732 | int newlen = lolen + hilen - (s - a); |
| 8733 | if (s >= a && newlen < BB_MAX_LEN) { |
| 8734 | /* yes, we can combine them */ |
| 8735 | int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]); |
| 8736 | p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack); |
| 8737 | memmove(p + hi, p + hi + 1, |
| 8738 | (bb->count - hi - 1) * 8); |
| 8739 | bb->count--; |
| 8740 | } |
| 8741 | } |
| 8742 | while (sectors) { |
| 8743 | /* didn't merge (it all). |
| 8744 | * Need to add a range just before 'hi' */ |
| 8745 | if (bb->count >= MD_MAX_BADBLOCKS) { |
| 8746 | /* No room for more */ |
| 8747 | rv = 0; |
| 8748 | break; |
| 8749 | } else { |
| 8750 | int this_sectors = sectors; |
| 8751 | memmove(p + hi + 1, p + hi, |
| 8752 | (bb->count - hi) * 8); |
| 8753 | bb->count++; |
| 8754 | |
| 8755 | if (this_sectors > BB_MAX_LEN) |
| 8756 | this_sectors = BB_MAX_LEN; |
| 8757 | p[hi] = BB_MAKE(s, this_sectors, acknowledged); |
| 8758 | sectors -= this_sectors; |
| 8759 | s += this_sectors; |
| 8760 | } |
| 8761 | } |
| 8762 | |
| 8763 | bb->changed = 1; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8764 | if (!acknowledged) |
| 8765 | bb->unacked_exist = 1; |
Bian Yu | 905b029 | 2013-10-12 01:10:03 -0400 | [diff] [blame] | 8766 | write_sequnlock_irqrestore(&bb->lock, flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8767 | |
| 8768 | return rv; |
| 8769 | } |
| 8770 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8771 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8772 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8773 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8774 | int rv; |
| 8775 | if (is_new) |
| 8776 | s += rdev->new_data_offset; |
| 8777 | else |
| 8778 | s += rdev->data_offset; |
| 8779 | rv = md_set_badblocks(&rdev->badblocks, |
| 8780 | s, sectors, 0); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8781 | if (rv) { |
| 8782 | /* Make sure they get written out promptly */ |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 8783 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8784 | set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags); |
NeilBrown | 55ce74d | 2015-08-14 11:11:10 +1000 | [diff] [blame] | 8785 | set_bit(MD_CHANGE_PENDING, &rdev->mddev->flags); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8786 | md_wakeup_thread(rdev->mddev->thread); |
| 8787 | } |
| 8788 | return rv; |
| 8789 | } |
| 8790 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 8791 | |
| 8792 | /* |
| 8793 | * Remove a range of bad blocks from the table. |
| 8794 | * This may involve extending the table if we spilt a region, |
| 8795 | * but it must not fail. So if the table becomes full, we just |
| 8796 | * drop the remove request. |
| 8797 | */ |
| 8798 | static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors) |
| 8799 | { |
| 8800 | u64 *p; |
| 8801 | int lo, hi; |
| 8802 | sector_t target = s + sectors; |
| 8803 | int rv = 0; |
| 8804 | |
| 8805 | if (bb->shift > 0) { |
| 8806 | /* When clearing we round the start up and the end down. |
| 8807 | * This should not matter as the shift should align with |
| 8808 | * the block size and no rounding should ever be needed. |
| 8809 | * However it is better the think a block is bad when it |
| 8810 | * isn't than to think a block is not bad when it is. |
| 8811 | */ |
| 8812 | s += (1<<bb->shift) - 1; |
| 8813 | s >>= bb->shift; |
| 8814 | target >>= bb->shift; |
| 8815 | sectors = target - s; |
| 8816 | } |
| 8817 | |
| 8818 | write_seqlock_irq(&bb->lock); |
| 8819 | |
| 8820 | p = bb->page; |
| 8821 | lo = 0; |
| 8822 | hi = bb->count; |
| 8823 | /* Find the last range that starts before 'target' */ |
| 8824 | while (hi - lo > 1) { |
| 8825 | int mid = (lo + hi) / 2; |
| 8826 | sector_t a = BB_OFFSET(p[mid]); |
| 8827 | if (a < target) |
| 8828 | lo = mid; |
| 8829 | else |
| 8830 | hi = mid; |
| 8831 | } |
| 8832 | if (hi > lo) { |
| 8833 | /* p[lo] is the last range that could overlap the |
| 8834 | * current range. Earlier ranges could also overlap, |
| 8835 | * but only this one can overlap the end of the range. |
| 8836 | */ |
| 8837 | if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) { |
| 8838 | /* Partial overlap, leave the tail of this range */ |
| 8839 | int ack = BB_ACK(p[lo]); |
| 8840 | sector_t a = BB_OFFSET(p[lo]); |
| 8841 | sector_t end = a + BB_LEN(p[lo]); |
| 8842 | |
| 8843 | if (a < s) { |
| 8844 | /* we need to split this range */ |
| 8845 | if (bb->count >= MD_MAX_BADBLOCKS) { |
NeilBrown | 8b32bf5 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 8846 | rv = -ENOSPC; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8847 | goto out; |
| 8848 | } |
| 8849 | memmove(p+lo+1, p+lo, (bb->count - lo) * 8); |
| 8850 | bb->count++; |
| 8851 | p[lo] = BB_MAKE(a, s-a, ack); |
| 8852 | lo++; |
| 8853 | } |
| 8854 | p[lo] = BB_MAKE(target, end - target, ack); |
| 8855 | /* there is no longer an overlap */ |
| 8856 | hi = lo; |
| 8857 | lo--; |
| 8858 | } |
| 8859 | while (lo >= 0 && |
| 8860 | BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) { |
| 8861 | /* This range does overlap */ |
| 8862 | if (BB_OFFSET(p[lo]) < s) { |
| 8863 | /* Keep the early parts of this range. */ |
| 8864 | int ack = BB_ACK(p[lo]); |
| 8865 | sector_t start = BB_OFFSET(p[lo]); |
| 8866 | p[lo] = BB_MAKE(start, s - start, ack); |
| 8867 | /* now low doesn't overlap, so.. */ |
| 8868 | break; |
| 8869 | } |
| 8870 | lo--; |
| 8871 | } |
| 8872 | /* 'lo' is strictly before, 'hi' is strictly after, |
| 8873 | * anything between needs to be discarded |
| 8874 | */ |
| 8875 | if (hi - lo > 1) { |
| 8876 | memmove(p+lo+1, p+hi, (bb->count - hi) * 8); |
| 8877 | bb->count -= (hi - lo - 1); |
| 8878 | } |
| 8879 | } |
| 8880 | |
| 8881 | bb->changed = 1; |
| 8882 | out: |
| 8883 | write_sequnlock_irq(&bb->lock); |
| 8884 | return rv; |
| 8885 | } |
| 8886 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8887 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 8888 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8889 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8890 | if (is_new) |
| 8891 | s += rdev->new_data_offset; |
| 8892 | else |
| 8893 | s += rdev->data_offset; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8894 | return md_clear_badblocks(&rdev->badblocks, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8895 | s, sectors); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8896 | } |
| 8897 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 8898 | |
| 8899 | /* |
| 8900 | * Acknowledge all bad blocks in a list. |
| 8901 | * This only succeeds if ->changed is clear. It is used by |
| 8902 | * in-kernel metadata updates |
| 8903 | */ |
| 8904 | void md_ack_all_badblocks(struct badblocks *bb) |
| 8905 | { |
| 8906 | if (bb->page == NULL || bb->changed) |
| 8907 | /* no point even trying */ |
| 8908 | return; |
| 8909 | write_seqlock_irq(&bb->lock); |
| 8910 | |
majianpeng | ecb178b | 2012-03-19 12:46:42 +1100 | [diff] [blame] | 8911 | if (bb->changed == 0 && bb->unacked_exist) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8912 | u64 *p = bb->page; |
| 8913 | int i; |
| 8914 | for (i = 0; i < bb->count ; i++) { |
| 8915 | if (!BB_ACK(p[i])) { |
| 8916 | sector_t start = BB_OFFSET(p[i]); |
| 8917 | int len = BB_LEN(p[i]); |
| 8918 | p[i] = BB_MAKE(start, len, 1); |
| 8919 | } |
| 8920 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8921 | bb->unacked_exist = 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 8922 | } |
| 8923 | write_sequnlock_irq(&bb->lock); |
| 8924 | } |
| 8925 | EXPORT_SYMBOL_GPL(md_ack_all_badblocks); |
| 8926 | |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8927 | /* sysfs access to bad-blocks list. |
| 8928 | * We present two files. |
| 8929 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 8930 | * are recorded as bad. The list is truncated to fit within |
| 8931 | * the one-page limit of sysfs. |
| 8932 | * Writing "sector length" to this file adds an acknowledged |
| 8933 | * bad block list. |
| 8934 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 8935 | * been acknowledged. Writing to this file adds bad blocks |
| 8936 | * without acknowledging them. This is largely for testing. |
| 8937 | */ |
| 8938 | |
| 8939 | static ssize_t |
| 8940 | badblocks_show(struct badblocks *bb, char *page, int unack) |
| 8941 | { |
| 8942 | size_t len; |
| 8943 | int i; |
| 8944 | u64 *p = bb->page; |
| 8945 | unsigned seq; |
| 8946 | |
| 8947 | if (bb->shift < 0) |
| 8948 | return 0; |
| 8949 | |
| 8950 | retry: |
| 8951 | seq = read_seqbegin(&bb->lock); |
| 8952 | |
| 8953 | len = 0; |
| 8954 | i = 0; |
| 8955 | |
| 8956 | while (len < PAGE_SIZE && i < bb->count) { |
| 8957 | sector_t s = BB_OFFSET(p[i]); |
| 8958 | unsigned int length = BB_LEN(p[i]); |
| 8959 | int ack = BB_ACK(p[i]); |
| 8960 | i++; |
| 8961 | |
| 8962 | if (unack && ack) |
| 8963 | continue; |
| 8964 | |
| 8965 | len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n", |
| 8966 | (unsigned long long)s << bb->shift, |
| 8967 | length << bb->shift); |
| 8968 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8969 | if (unack && len == 0) |
| 8970 | bb->unacked_exist = 0; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 8971 | |
| 8972 | if (read_seqretry(&bb->lock, seq)) |
| 8973 | goto retry; |
| 8974 | |
| 8975 | return len; |
| 8976 | } |
| 8977 | |
| 8978 | #define DO_DEBUG 1 |
| 8979 | |
| 8980 | static ssize_t |
| 8981 | badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack) |
| 8982 | { |
| 8983 | unsigned long long sector; |
| 8984 | int length; |
| 8985 | char newline; |
| 8986 | #ifdef DO_DEBUG |
| 8987 | /* Allow clearing via sysfs *only* for testing/debugging. |
| 8988 | * Normally only a successful write may clear a badblock |
| 8989 | */ |
| 8990 | int clear = 0; |
| 8991 | if (page[0] == '-') { |
| 8992 | clear = 1; |
| 8993 | page++; |
| 8994 | } |
| 8995 | #endif /* DO_DEBUG */ |
| 8996 | |
| 8997 | switch (sscanf(page, "%llu %d%c", §or, &length, &newline)) { |
| 8998 | case 3: |
| 8999 | if (newline != '\n') |
| 9000 | return -EINVAL; |
| 9001 | case 2: |
| 9002 | if (length <= 0) |
| 9003 | return -EINVAL; |
| 9004 | break; |
| 9005 | default: |
| 9006 | return -EINVAL; |
| 9007 | } |
| 9008 | |
| 9009 | #ifdef DO_DEBUG |
| 9010 | if (clear) { |
| 9011 | md_clear_badblocks(bb, sector, length); |
| 9012 | return len; |
| 9013 | } |
| 9014 | #endif /* DO_DEBUG */ |
| 9015 | if (md_set_badblocks(bb, sector, length, !unack)) |
| 9016 | return len; |
| 9017 | else |
| 9018 | return -ENOSPC; |
| 9019 | } |
| 9020 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9021 | static int md_notify_reboot(struct notifier_block *this, |
| 9022 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9023 | { |
| 9024 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9025 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9026 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9027 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9028 | for_each_mddev(mddev, tmp) { |
| 9029 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 9030 | if (mddev->pers) |
| 9031 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 9032 | if (mddev->persistent) |
| 9033 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9034 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9035 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9036 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9037 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9038 | /* |
| 9039 | * certain more exotic SCSI devices are known to be |
| 9040 | * volatile wrt too early system reboots. While the |
| 9041 | * right place to handle this issue is the given |
| 9042 | * driver, we do want to have a safe RAID driver ... |
| 9043 | */ |
| 9044 | if (need_delay) |
| 9045 | mdelay(1000*1); |
| 9046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9047 | return NOTIFY_DONE; |
| 9048 | } |
| 9049 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9050 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9051 | .notifier_call = md_notify_reboot, |
| 9052 | .next = NULL, |
| 9053 | .priority = INT_MAX, /* before any real devices */ |
| 9054 | }; |
| 9055 | |
| 9056 | static void md_geninit(void) |
| 9057 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 9058 | pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9059 | |
Denis V. Lunev | c7705f344 | 2008-04-29 01:02:35 -0700 | [diff] [blame] | 9060 | proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9061 | } |
| 9062 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9063 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9064 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9065 | int ret = -ENOMEM; |
| 9066 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 9067 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9068 | if (!md_wq) |
| 9069 | goto err_wq; |
| 9070 | |
| 9071 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 9072 | if (!md_misc_wq) |
| 9073 | goto err_misc_wq; |
| 9074 | |
| 9075 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 9076 | goto err_md; |
| 9077 | |
| 9078 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 9079 | goto err_mdp; |
| 9080 | mdp_major = ret; |
| 9081 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9082 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9083 | md_probe, NULL, NULL); |
| 9084 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9085 | md_probe, NULL, NULL); |
| 9086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9087 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 9088 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9089 | |
| 9090 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 9091 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9092 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9093 | err_mdp: |
| 9094 | unregister_blkdev(MD_MAJOR, "md"); |
| 9095 | err_md: |
| 9096 | destroy_workqueue(md_misc_wq); |
| 9097 | err_misc_wq: |
| 9098 | destroy_workqueue(md_wq); |
| 9099 | err_wq: |
| 9100 | return ret; |
| 9101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9102 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9103 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9104 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9105 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 9106 | struct md_rdev *rdev2; |
| 9107 | int role, ret; |
| 9108 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9109 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9110 | /* Check for change of roles in the active devices */ |
| 9111 | rdev_for_each(rdev2, mddev) { |
| 9112 | if (test_bit(Faulty, &rdev2->flags)) |
| 9113 | continue; |
| 9114 | |
| 9115 | /* Check if the roles changed */ |
| 9116 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9117 | |
| 9118 | if (test_bit(Candidate, &rdev2->flags)) { |
| 9119 | if (role == 0xfffe) { |
| 9120 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 9121 | md_kick_rdev_from_array(rdev2); |
| 9122 | continue; |
| 9123 | } |
| 9124 | else |
| 9125 | clear_bit(Candidate, &rdev2->flags); |
| 9126 | } |
| 9127 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9128 | if (role != rdev2->raid_disk) { |
| 9129 | /* got activated */ |
| 9130 | if (rdev2->raid_disk == -1 && role != 0xffff) { |
| 9131 | rdev2->saved_raid_disk = role; |
| 9132 | ret = remove_and_add_spares(mddev, rdev2); |
| 9133 | pr_info("Activated spare: %s\n", |
| 9134 | bdevname(rdev2->bdev,b)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9135 | } |
| 9136 | /* device faulty |
| 9137 | * We just want to do the minimum to mark the disk |
| 9138 | * as faulty. The recovery is performed by the |
| 9139 | * one who initiated the error. |
| 9140 | */ |
| 9141 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9142 | md_error(mddev, rdev2); |
| 9143 | clear_bit(Blocked, &rdev2->flags); |
| 9144 | } |
| 9145 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9146 | } |
| 9147 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 9148 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 9149 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9150 | |
| 9151 | /* Finally set the event to be up to date */ |
| 9152 | mddev->events = le64_to_cpu(sb->events); |
| 9153 | } |
| 9154 | |
| 9155 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9156 | { |
| 9157 | int err; |
| 9158 | struct page *swapout = rdev->sb_page; |
| 9159 | struct mdp_superblock_1 *sb; |
| 9160 | |
| 9161 | /* Store the sb page of the rdev in the swapout temporary |
| 9162 | * variable in case we err in the future |
| 9163 | */ |
| 9164 | rdev->sb_page = NULL; |
| 9165 | alloc_disk_sb(rdev); |
| 9166 | ClearPageUptodate(rdev->sb_page); |
| 9167 | rdev->sb_loaded = 0; |
| 9168 | err = super_types[mddev->major_version].load_super(rdev, NULL, mddev->minor_version); |
| 9169 | |
| 9170 | if (err < 0) { |
| 9171 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9172 | __func__, __LINE__, rdev->desc_nr, err); |
| 9173 | put_page(rdev->sb_page); |
| 9174 | rdev->sb_page = swapout; |
| 9175 | rdev->sb_loaded = 1; |
| 9176 | return err; |
| 9177 | } |
| 9178 | |
| 9179 | sb = page_address(rdev->sb_page); |
| 9180 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9181 | * is not set |
| 9182 | */ |
| 9183 | |
| 9184 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9185 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9186 | |
| 9187 | /* The other node finished recovery, call spare_active to set |
| 9188 | * device In_sync and mddev->degraded |
| 9189 | */ |
| 9190 | if (rdev->recovery_offset == MaxSector && |
| 9191 | !test_bit(In_sync, &rdev->flags) && |
| 9192 | mddev->pers->spare_active(mddev)) |
| 9193 | sysfs_notify(&mddev->kobj, NULL, "degraded"); |
| 9194 | |
| 9195 | put_page(swapout); |
| 9196 | return 0; |
| 9197 | } |
| 9198 | |
| 9199 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9200 | { |
| 9201 | struct md_rdev *rdev; |
| 9202 | int err; |
| 9203 | |
| 9204 | /* Find the rdev */ |
| 9205 | rdev_for_each_rcu(rdev, mddev) { |
| 9206 | if (rdev->desc_nr == nr) |
| 9207 | break; |
| 9208 | } |
| 9209 | |
| 9210 | if (!rdev || rdev->desc_nr != nr) { |
| 9211 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9212 | return; |
| 9213 | } |
| 9214 | |
| 9215 | err = read_rdev(mddev, rdev); |
| 9216 | if (err < 0) |
| 9217 | return; |
| 9218 | |
| 9219 | check_sb_changes(mddev, rdev); |
| 9220 | |
| 9221 | /* Read all rdev's to update recovery_offset */ |
| 9222 | rdev_for_each_rcu(rdev, mddev) |
| 9223 | read_rdev(mddev, rdev); |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9224 | } |
| 9225 | EXPORT_SYMBOL(md_reload_sb); |
| 9226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9227 | #ifndef MODULE |
| 9228 | |
| 9229 | /* |
| 9230 | * Searches all registered partitions for autorun RAID arrays |
| 9231 | * at boot time. |
| 9232 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9233 | |
| 9234 | static LIST_HEAD(all_detected_devices); |
| 9235 | struct detected_devices_node { |
| 9236 | struct list_head list; |
| 9237 | dev_t dev; |
| 9238 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9239 | |
| 9240 | void md_autodetect_dev(dev_t dev) |
| 9241 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9242 | struct detected_devices_node *node_detected_dev; |
| 9243 | |
| 9244 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9245 | if (node_detected_dev) { |
| 9246 | node_detected_dev->dev = dev; |
| 9247 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
| 9248 | } else { |
| 9249 | printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed" |
| 9250 | ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev)); |
| 9251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9252 | } |
| 9253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9254 | static void autostart_arrays(int part) |
| 9255 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9256 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9257 | struct detected_devices_node *node_detected_dev; |
| 9258 | dev_t dev; |
| 9259 | int i_scanned, i_passed; |
| 9260 | |
| 9261 | i_scanned = 0; |
| 9262 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9263 | |
| 9264 | printk(KERN_INFO "md: Autodetecting RAID arrays.\n"); |
| 9265 | |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9266 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9267 | i_scanned++; |
| 9268 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9269 | struct detected_devices_node, list); |
| 9270 | list_del(&node_detected_dev->list); |
| 9271 | dev = node_detected_dev->dev; |
| 9272 | kfree(node_detected_dev); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9273 | rdev = md_import_device(dev,0, 90); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9274 | if (IS_ERR(rdev)) |
| 9275 | continue; |
| 9276 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9277 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9278 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9279 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9280 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9281 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9282 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9283 | } |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9284 | |
| 9285 | printk(KERN_INFO "md: Scanned %d and added %d devices.\n", |
| 9286 | i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9287 | |
| 9288 | autorun_devices(part); |
| 9289 | } |
| 9290 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9291 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9292 | |
| 9293 | static __exit void md_exit(void) |
| 9294 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9295 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9296 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9297 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9298 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9299 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9300 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9301 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9302 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9303 | unregister_blkdev(mdp_major, "mdp"); |
| 9304 | unregister_reboot_notifier(&md_notifier); |
| 9305 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9306 | |
| 9307 | /* We cannot unload the modules while some process is |
| 9308 | * waiting for us in select() or poll() - wake them up |
| 9309 | */ |
| 9310 | md_unloading = 1; |
| 9311 | while (waitqueue_active(&md_event_waiters)) { |
| 9312 | /* not safe to leave yet */ |
| 9313 | wake_up(&md_event_waiters); |
| 9314 | msleep(delay); |
| 9315 | delay += delay; |
| 9316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9317 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9318 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9319 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9320 | export_array(mddev); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9321 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9322 | } |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9323 | destroy_workqueue(md_misc_wq); |
| 9324 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9325 | } |
| 9326 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9327 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9328 | module_exit(md_exit) |
| 9329 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9330 | static int get_ro(char *buffer, struct kernel_param *kp) |
| 9331 | { |
| 9332 | return sprintf(buffer, "%d", start_readonly); |
| 9333 | } |
| 9334 | static int set_ro(const char *val, struct kernel_param *kp) |
| 9335 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9336 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9337 | } |
| 9338 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9339 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9340 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9341 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9343 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9344 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9345 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9346 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |