Thomas Gleixner | af1a889 | 2019-05-20 19:08:12 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | md.c : Multiple Devices driver for Linux |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4 | Copyright (C) 1998, 1999, 2000 Ingo Molnar |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | completely rewritten, based on the MD driver code from Marc Zyngier |
| 7 | |
| 8 | Changes: |
| 9 | |
| 10 | - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar |
| 11 | - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com> |
| 12 | - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net> |
| 13 | - kerneld support by Boris Tobotras <boris@xtalk.msk.su> |
| 14 | - kmod support by: Cyrus Durgin |
| 15 | - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com> |
| 16 | - Devfs support by Richard Gooch <rgooch@atnf.csiro.au> |
| 17 | |
| 18 | - lots of fixes and improvements to the RAID1/RAID5 and generic |
| 19 | RAID code (such as request based resynchronization): |
| 20 | |
| 21 | Neil Brown <neilb@cse.unsw.edu.au>. |
| 22 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 23 | - persistent bitmap code |
| 24 | Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc. |
| 25 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 26 | |
| 27 | Errors, Warnings, etc. |
| 28 | Please use: |
| 29 | pr_crit() for error conditions that risk data loss |
| 30 | pr_err() for error conditions that are unexpected, like an IO error |
| 31 | or internal inconsistency |
| 32 | pr_warn() for error conditions that could have been predicated, like |
| 33 | adding a device to an array when it has incompatible metadata |
| 34 | pr_info() for every interesting, very rare events, like an array starting |
| 35 | or stopping, or resync starting or stopping |
| 36 | pr_debug() for everything else. |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | */ |
| 39 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 40 | #include <linux/sched/mm.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 41 | #include <linux/sched/signal.h> |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 42 | #include <linux/kthread.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 43 | #include <linux/blkdev.h> |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 44 | #include <linux/badblocks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 46 | #include <linux/seq_file.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 47 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 48 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 49 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 50 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 51 | #include <linux/hdreg.h> |
| 52 | #include <linux/proc_fs.h> |
| 53 | #include <linux/random.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 54 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 55 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 56 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 57 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 58 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 59 | #include <linux/raid/md_p.h> |
| 60 | #include <linux/raid/md_u.h> |
Christoph Hellwig | 74cc979c | 2020-03-24 08:25:19 +0100 | [diff] [blame] | 61 | #include <linux/raid/detect.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 62 | #include <linux/slab.h> |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 63 | #include <linux/percpu-refcount.h> |
Christoph Hellwig | c6a564ff | 2020-03-25 16:48:42 +0100 | [diff] [blame] | 64 | #include <linux/part_stat.h> |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 65 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 66 | #include <trace/events/block.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 67 | #include "md.h" |
Mike Snitzer | 935fe09 | 2017-10-10 17:02:41 -0400 | [diff] [blame] | 68 | #include "md-bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 69 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #ifndef MODULE |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 72 | static void autostart_arrays(int part); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #endif |
| 74 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 75 | /* pers_list is a list of registered personalities protected |
| 76 | * by pers_lock. |
| 77 | * pers_lock does extra service to protect accesses to |
| 78 | * mddev->thread when the mutex cannot be held. |
| 79 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 80 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | static DEFINE_SPINLOCK(pers_lock); |
| 82 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 83 | static struct kobj_type md_ktype; |
| 84 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 85 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 86 | EXPORT_SYMBOL(md_cluster_ops); |
Christoph Hellwig | 2b598ee | 2019-04-04 18:56:14 +0200 | [diff] [blame] | 87 | static struct module *md_cluster_mod; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 88 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 89 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 90 | static struct workqueue_struct *md_wq; |
| 91 | static struct workqueue_struct *md_misc_wq; |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 92 | static struct workqueue_struct *md_rdev_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 93 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 94 | static int remove_and_add_spares(struct mddev *mddev, |
| 95 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 96 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 99 | * Default number of read corrections we'll attempt on an rdev |
| 100 | * before ejecting it from the array. We divide the read error |
| 101 | * count by 2 for every hour elapsed between read errors. |
| 102 | */ |
| 103 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 104 | /* Default safemode delay: 200 msec */ |
| 105 | #define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1) |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 106 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 108 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 109 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 110 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 112 | * speed limit - in case reconstruction slows down your system despite |
| 113 | * idle IO detection. |
| 114 | * |
| 115 | * 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] | 116 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | */ |
| 118 | |
| 119 | static int sysctl_speed_limit_min = 1000; |
| 120 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 121 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 122 | { |
| 123 | return mddev->sync_speed_min ? |
| 124 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 125 | } |
| 126 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 127 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 128 | { |
| 129 | return mddev->sync_speed_max ? |
| 130 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 131 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 133 | static void rdev_uninit_serial(struct md_rdev *rdev) |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 134 | { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 135 | if (!test_and_clear_bit(CollisionCheck, &rdev->flags)) |
| 136 | return; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 137 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 138 | kvfree(rdev->serial); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 139 | rdev->serial = NULL; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 140 | } |
| 141 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 142 | static void rdevs_uninit_serial(struct mddev *mddev) |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 143 | { |
| 144 | struct md_rdev *rdev; |
| 145 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 146 | rdev_for_each(rdev, mddev) |
| 147 | rdev_uninit_serial(rdev); |
| 148 | } |
| 149 | |
| 150 | static int rdev_init_serial(struct md_rdev *rdev) |
| 151 | { |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 152 | /* serial_nums equals with BARRIER_BUCKETS_NR */ |
| 153 | int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t)))); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 154 | struct serial_in_rdev *serial = NULL; |
| 155 | |
| 156 | if (test_bit(CollisionCheck, &rdev->flags)) |
| 157 | return 0; |
| 158 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 159 | serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums, |
| 160 | GFP_KERNEL); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 161 | if (!serial) |
| 162 | return -ENOMEM; |
| 163 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 164 | for (i = 0; i < serial_nums; i++) { |
| 165 | struct serial_in_rdev *serial_tmp = &serial[i]; |
| 166 | |
| 167 | spin_lock_init(&serial_tmp->serial_lock); |
| 168 | serial_tmp->serial_rb = RB_ROOT_CACHED; |
| 169 | init_waitqueue_head(&serial_tmp->serial_io_wait); |
| 170 | } |
| 171 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 172 | rdev->serial = serial; |
| 173 | set_bit(CollisionCheck, &rdev->flags); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int rdevs_init_serial(struct mddev *mddev) |
| 179 | { |
| 180 | struct md_rdev *rdev; |
| 181 | int ret = 0; |
| 182 | |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 183 | rdev_for_each(rdev, mddev) { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 184 | ret = rdev_init_serial(rdev); |
| 185 | if (ret) |
| 186 | break; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 187 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 188 | |
| 189 | /* Free all resources if pool is not existed */ |
| 190 | if (ret && !mddev->serial_info_pool) |
| 191 | rdevs_uninit_serial(mddev); |
| 192 | |
| 193 | return ret; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 196 | /* |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 197 | * rdev needs to enable serial stuffs if it meets the conditions: |
| 198 | * 1. it is multi-queue device flaged with writemostly. |
| 199 | * 2. the write-behind mode is enabled. |
| 200 | */ |
| 201 | static int rdev_need_serial(struct md_rdev *rdev) |
| 202 | { |
| 203 | return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 && |
Christoph Hellwig | e556f6b | 2020-06-26 10:01:56 +0200 | [diff] [blame] | 204 | rdev->bdev->bd_disk->queue->nr_hw_queues != 1 && |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 205 | test_bit(WriteMostly, &rdev->flags)); |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Init resource for rdev(s), then create serial_info_pool if: |
| 210 | * 1. rdev is the first device which return true from rdev_enable_serial. |
| 211 | * 2. rdev is NULL, means we want to enable serialization for all rdevs. |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 212 | */ |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 213 | void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev, |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 214 | bool is_suspend) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 215 | { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 216 | int ret = 0; |
| 217 | |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 218 | if (rdev && !rdev_need_serial(rdev) && |
| 219 | !test_bit(CollisionCheck, &rdev->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 220 | return; |
| 221 | |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 222 | if (!is_suspend) |
| 223 | mddev_suspend(mddev); |
| 224 | |
| 225 | if (!rdev) |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 226 | ret = rdevs_init_serial(mddev); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 227 | else |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 228 | ret = rdev_init_serial(rdev); |
| 229 | if (ret) |
| 230 | goto abort; |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 231 | |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 232 | if (mddev->serial_info_pool == NULL) { |
Coly Li | 3024ba2 | 2020-04-09 22:17:23 +0800 | [diff] [blame] | 233 | /* |
| 234 | * already in memalloc noio context by |
| 235 | * mddev_suspend() |
| 236 | */ |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 237 | mddev->serial_info_pool = |
| 238 | mempool_create_kmalloc_pool(NR_SERIAL_INFOS, |
| 239 | sizeof(struct serial_info)); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 240 | if (!mddev->serial_info_pool) { |
| 241 | rdevs_uninit_serial(mddev); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 242 | pr_err("can't alloc memory pool for serialization\n"); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 243 | } |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 244 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 245 | |
| 246 | abort: |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 247 | if (!is_suspend) |
| 248 | mddev_resume(mddev); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 249 | } |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 250 | |
| 251 | /* |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 252 | * Free resource from rdev(s), and destroy serial_info_pool under conditions: |
| 253 | * 1. rdev is the last device flaged with CollisionCheck. |
| 254 | * 2. when bitmap is destroyed while policy is not enabled. |
| 255 | * 3. for disable policy, the pool is destroyed only when no rdev needs it. |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 256 | */ |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 257 | void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev, |
| 258 | bool is_suspend) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 259 | { |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 260 | if (rdev && !test_bit(CollisionCheck, &rdev->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 261 | return; |
| 262 | |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 263 | if (mddev->serial_info_pool) { |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 264 | struct md_rdev *temp; |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 265 | int num = 0; /* used to track if other rdevs need the pool */ |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 266 | |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 267 | if (!is_suspend) |
| 268 | mddev_suspend(mddev); |
| 269 | rdev_for_each(temp, mddev) { |
| 270 | if (!rdev) { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 271 | if (!mddev->serialize_policy || |
| 272 | !rdev_need_serial(temp)) |
| 273 | rdev_uninit_serial(temp); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 274 | else |
| 275 | num++; |
| 276 | } else if (temp != rdev && |
| 277 | test_bit(CollisionCheck, &temp->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 278 | num++; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if (rdev) |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 282 | rdev_uninit_serial(rdev); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 283 | |
| 284 | if (num) |
| 285 | pr_info("The mempool could be used by other devices\n"); |
| 286 | else { |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 287 | mempool_destroy(mddev->serial_info_pool); |
| 288 | mddev->serial_info_pool = NULL; |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 289 | } |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 290 | if (!is_suspend) |
| 291 | mddev_resume(mddev); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | static struct ctl_table_header *raid_table_header; |
| 296 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 297 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | .procname = "speed_limit_min", |
| 300 | .data = &sysctl_speed_limit_min, |
| 301 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 302 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 303 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | }, |
| 305 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | .procname = "speed_limit_max", |
| 307 | .data = &sysctl_speed_limit_max, |
| 308 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 309 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 310 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 312 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 315 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | .procname = "raid", |
| 318 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 319 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | .child = raid_table, |
| 321 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 322 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | }; |
| 324 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 325 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | .procname = "dev", |
| 328 | .maxlen = 0, |
| 329 | .mode = 0555, |
| 330 | .child = raid_dir_table, |
| 331 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 332 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | }; |
| 334 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 335 | static const struct block_device_operations md_fops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 337 | static int start_readonly; |
| 338 | |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 339 | /* |
| 340 | * The original mechanism for creating an md device is to create |
| 341 | * a device node in /dev and to open it. This causes races with device-close. |
| 342 | * The preferred method is to write to the "new_array" module parameter. |
| 343 | * This can avoid races. |
| 344 | * Setting create_on_open to false disables the original mechanism |
| 345 | * so all the races disappear. |
| 346 | */ |
| 347 | static bool create_on_open = true; |
| 348 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 349 | struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs, |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 350 | struct mddev *mddev) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 351 | { |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 352 | if (!mddev || !bioset_initialized(&mddev->bio_set)) |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 353 | return bio_alloc(gfp_mask, nr_iovecs); |
| 354 | |
Marcos Paulo de Souza | 6251691 | 2019-01-14 06:31:56 -0700 | [diff] [blame] | 355 | return bio_alloc_bioset(gfp_mask, nr_iovecs, &mddev->bio_set); |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 356 | } |
| 357 | EXPORT_SYMBOL_GPL(bio_alloc_mddev); |
| 358 | |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 359 | static struct bio *md_bio_alloc_sync(struct mddev *mddev) |
| 360 | { |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 361 | if (!mddev || !bioset_initialized(&mddev->sync_set)) |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 362 | return bio_alloc(GFP_NOIO, 1); |
| 363 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 364 | return bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set); |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 365 | } |
| 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 368 | * We have a system wide 'event count' that is incremented |
| 369 | * on any 'interesting' event, and readers of /proc/mdstat |
| 370 | * can use 'poll' or 'select' to find out when the event |
| 371 | * count increases. |
| 372 | * |
| 373 | * Events are: |
| 374 | * start array, stop array, error, add device, remove device, |
| 375 | * start build, activate spare |
| 376 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 377 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 378 | static atomic_t md_event_count; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 379 | void md_new_event(struct mddev *mddev) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 380 | { |
| 381 | atomic_inc(&md_event_count); |
| 382 | wake_up(&md_event_waiters); |
| 383 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 384 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 385 | |
| 386 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | * Enables to iterate over all existing md arrays |
| 388 | * all_mddevs_lock protects this list. |
| 389 | */ |
| 390 | static LIST_HEAD(all_mddevs); |
| 391 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | /* |
| 394 | * iterates through all used mddevs in the system. |
| 395 | * We take care to grab the all_mddevs_lock whenever navigating |
| 396 | * the list, and to always hold a refcount when unlocked. |
| 397 | * Any code which breaks out of this loop while own |
| 398 | * a reference to the current mddev and must mddev_put it. |
| 399 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 400 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 402 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 403 | _tmp = all_mddevs.next; \ |
| 404 | _mddev = NULL;}); \ |
| 405 | ({ if (_tmp != &all_mddevs) \ |
| 406 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 408 | if (_mddev) mddev_put(_mddev); \ |
| 409 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 410 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 412 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | ) |
| 414 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 415 | /* Rather than calling directly into the personality make_request function, |
| 416 | * IO requests come here first so that we can check if the device is |
| 417 | * being suspended pending a reconfiguration. |
| 418 | * We hold a refcount over the call to ->make_request. By the time that |
| 419 | * call has finished, the bio has been linked into some internal structure |
| 420 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 421 | */ |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 422 | static bool is_suspended(struct mddev *mddev, struct bio *bio) |
| 423 | { |
| 424 | if (mddev->suspended) |
| 425 | return true; |
| 426 | if (bio_data_dir(bio) != WRITE) |
| 427 | return false; |
| 428 | if (mddev->suspend_lo >= mddev->suspend_hi) |
| 429 | return false; |
| 430 | if (bio->bi_iter.bi_sector >= mddev->suspend_hi) |
| 431 | return false; |
| 432 | if (bio_end_sector(bio) < mddev->suspend_lo) |
| 433 | return false; |
| 434 | return true; |
| 435 | } |
| 436 | |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 437 | void md_handle_request(struct mddev *mddev, struct bio *bio) |
| 438 | { |
| 439 | check_suspended: |
| 440 | rcu_read_lock(); |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 441 | if (is_suspended(mddev, bio)) { |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 442 | DEFINE_WAIT(__wait); |
| 443 | for (;;) { |
| 444 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 445 | TASK_UNINTERRUPTIBLE); |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 446 | if (!is_suspended(mddev, bio)) |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 447 | break; |
| 448 | rcu_read_unlock(); |
| 449 | schedule(); |
| 450 | rcu_read_lock(); |
| 451 | } |
| 452 | finish_wait(&mddev->sb_wait, &__wait); |
| 453 | } |
| 454 | atomic_inc(&mddev->active_io); |
| 455 | rcu_read_unlock(); |
| 456 | |
| 457 | if (!mddev->pers->make_request(mddev, bio)) { |
| 458 | atomic_dec(&mddev->active_io); |
| 459 | wake_up(&mddev->sb_wait); |
| 460 | goto check_suspended; |
| 461 | } |
| 462 | |
| 463 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 464 | wake_up(&mddev->sb_wait); |
| 465 | } |
| 466 | EXPORT_SYMBOL(md_handle_request); |
| 467 | |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 468 | struct md_io { |
| 469 | struct mddev *mddev; |
| 470 | bio_end_io_t *orig_bi_end_io; |
| 471 | void *orig_bi_private; |
| 472 | unsigned long start_time; |
| 473 | }; |
| 474 | |
| 475 | static void md_end_io(struct bio *bio) |
| 476 | { |
| 477 | struct md_io *md_io = bio->bi_private; |
| 478 | struct mddev *mddev = md_io->mddev; |
| 479 | |
| 480 | disk_end_io_acct(mddev->gendisk, bio_op(bio), md_io->start_time); |
| 481 | |
| 482 | bio->bi_end_io = md_io->orig_bi_end_io; |
| 483 | bio->bi_private = md_io->orig_bi_private; |
| 484 | |
| 485 | mempool_free(md_io, &mddev->md_io_pool); |
| 486 | |
| 487 | if (bio->bi_end_io) |
| 488 | bio->bi_end_io(bio); |
| 489 | } |
| 490 | |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 491 | static blk_qc_t md_submit_bio(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 493 | const int rw = bio_data_dir(bio); |
Christoph Hellwig | e4fc5a7 | 2020-05-08 18:15:14 +0200 | [diff] [blame] | 494 | struct mddev *mddev = bio->bi_disk->private_data; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 495 | |
Colin Ian King | 9a5a859 | 2020-07-02 12:35:02 +0100 | [diff] [blame] | 496 | if (mddev == NULL || mddev->pers == NULL) { |
| 497 | bio_io_error(bio); |
| 498 | return BLK_QC_T_NONE; |
| 499 | } |
| 500 | |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 501 | if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) { |
| 502 | bio_io_error(bio); |
| 503 | return BLK_QC_T_NONE; |
| 504 | } |
| 505 | |
Christoph Hellwig | f695ca3 | 2020-07-01 10:59:39 +0200 | [diff] [blame] | 506 | blk_queue_split(&bio); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 507 | |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 508 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 509 | if (bio_sectors(bio) != 0) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 510 | bio->bi_status = BLK_STS_IOERR; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 511 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 512 | return BLK_QC_T_NONE; |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 513 | } |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 514 | |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 515 | if (bio->bi_end_io != md_end_io) { |
| 516 | struct md_io *md_io; |
| 517 | |
| 518 | md_io = mempool_alloc(&mddev->md_io_pool, GFP_NOIO); |
| 519 | md_io->mddev = mddev; |
| 520 | md_io->orig_bi_end_io = bio->bi_end_io; |
| 521 | md_io->orig_bi_private = bio->bi_private; |
| 522 | |
| 523 | bio->bi_end_io = md_end_io; |
| 524 | bio->bi_private = md_io; |
| 525 | |
| 526 | md_io->start_time = disk_start_io_acct(mddev->gendisk, |
| 527 | bio_sectors(bio), |
| 528 | bio_op(bio)); |
| 529 | } |
| 530 | |
Shaohua Li | 9c573de | 2016-04-25 16:52:38 -0700 | [diff] [blame] | 531 | /* bio could be mergeable after passing to underlayer */ |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 532 | bio->bi_opf &= ~REQ_NOMERGE; |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 533 | |
| 534 | md_handle_request(mddev, bio); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 535 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 536 | return BLK_QC_T_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | } |
| 538 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 539 | /* mddev_suspend makes sure no new requests are submitted |
| 540 | * to the device, and that any requests that have been submitted |
| 541 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 542 | * Once mddev_detach() is called and completes, the module will be |
| 543 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 544 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 545 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 546 | { |
Heinz Mauelshagen | 092398d | 2016-05-03 19:43:57 +0200 | [diff] [blame] | 547 | WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 548 | lockdep_assert_held(&mddev->reconfig_mutex); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 549 | if (mddev->suspended++) |
| 550 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 551 | synchronize_rcu(); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 552 | wake_up(&mddev->sb_wait); |
NeilBrown | 35bfc52 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 553 | set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags); |
| 554 | smp_mb__after_atomic(); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 555 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 556 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 35bfc52 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 557 | clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags); |
| 558 | wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags)); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 559 | |
| 560 | del_timer_sync(&mddev->safemode_timer); |
Coly Li | 78f57ef | 2020-04-09 22:17:20 +0800 | [diff] [blame] | 561 | /* restrict memory reclaim I/O during raid array is suspend */ |
| 562 | mddev->noio_flag = memalloc_noio_save(); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 563 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 564 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 565 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 566 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 567 | { |
Coly Li | 78f57ef | 2020-04-09 22:17:20 +0800 | [diff] [blame] | 568 | /* entred the memalloc scope from mddev_suspend() */ |
| 569 | memalloc_noio_restore(mddev->noio_flag); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 570 | lockdep_assert_held(&mddev->reconfig_mutex); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 571 | if (--mddev->suspended) |
| 572 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 573 | wake_up(&mddev->sb_wait); |
| 574 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 575 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 576 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 577 | md_wakeup_thread(mddev->thread); |
| 578 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 579 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 580 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 581 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 582 | int mddev_congested(struct mddev *mddev, int bits) |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 583 | { |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 584 | struct md_personality *pers = mddev->pers; |
| 585 | int ret = 0; |
| 586 | |
| 587 | rcu_read_lock(); |
| 588 | if (mddev->suspended) |
| 589 | ret = 1; |
| 590 | else if (pers && pers->congested) |
| 591 | ret = pers->congested(mddev, bits); |
| 592 | rcu_read_unlock(); |
| 593 | return ret; |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 594 | } |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 595 | EXPORT_SYMBOL_GPL(mddev_congested); |
| 596 | static int md_congested(void *data, int bits) |
| 597 | { |
| 598 | struct mddev *mddev = data; |
| 599 | return mddev_congested(mddev, bits); |
| 600 | } |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 601 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 602 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 603 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 604 | */ |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 605 | |
| 606 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 607 | { |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 608 | struct md_rdev *rdev = bio->bi_private; |
| 609 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 610 | |
| 611 | rdev_dec_pending(rdev, mddev); |
| 612 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 613 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 614 | /* The pre-request flush has finished */ |
| 615 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 616 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 617 | bio_put(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 618 | } |
| 619 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 620 | static void md_submit_flush_data(struct work_struct *ws); |
| 621 | |
| 622 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 623 | { |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 624 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 625 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 626 | |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 627 | mddev->start_flush = ktime_get_boottime(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 628 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 629 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 630 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 631 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 632 | if (rdev->raid_disk >= 0 && |
| 633 | !test_bit(Faulty, &rdev->flags)) { |
| 634 | /* Take two references, one is dropped |
| 635 | * when request finishes, one after |
| 636 | * we reclaim rcu_read_lock |
| 637 | */ |
| 638 | struct bio *bi; |
| 639 | atomic_inc(&rdev->nr_pending); |
| 640 | atomic_inc(&rdev->nr_pending); |
| 641 | rcu_read_unlock(); |
Shaohua Li | b5e1b8c | 2012-05-21 09:26:59 +1000 | [diff] [blame] | 642 | bi = bio_alloc_mddev(GFP_NOIO, 0, mddev); |
Xiao Ni | 5a409b4 | 2018-05-21 11:49:54 +0800 | [diff] [blame] | 643 | bi->bi_end_io = md_end_flush; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 644 | bi->bi_private = rdev; |
| 645 | bio_set_dev(bi, rdev->bdev); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 646 | bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 647 | atomic_inc(&mddev->flush_pending); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 648 | submit_bio(bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 649 | rcu_read_lock(); |
| 650 | rdev_dec_pending(rdev, mddev); |
| 651 | } |
| 652 | rcu_read_unlock(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 653 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 654 | queue_work(md_wq, &mddev->flush_work); |
| 655 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 656 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 657 | static void md_submit_flush_data(struct work_struct *ws) |
| 658 | { |
| 659 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
| 660 | struct bio *bio = mddev->flush_bio; |
| 661 | |
| 662 | /* |
| 663 | * must reset flush_bio before calling into md_handle_request to avoid a |
| 664 | * deadlock, because other bios passed md_handle_request suspend check |
| 665 | * could wait for this and below md_handle_request could wait for those |
| 666 | * bios because of suspend check |
| 667 | */ |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 668 | mddev->last_flush = mddev->start_flush; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 669 | mddev->flush_bio = NULL; |
| 670 | wake_up(&mddev->sb_wait); |
| 671 | |
| 672 | if (bio->bi_iter.bi_size == 0) { |
| 673 | /* an empty barrier - all done */ |
| 674 | bio_endio(bio); |
| 675 | } else { |
| 676 | bio->bi_opf &= ~REQ_PREFLUSH; |
| 677 | md_handle_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 678 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 679 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 680 | |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 681 | /* |
| 682 | * Manages consolidation of flushes and submitting any flushes needed for |
| 683 | * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is |
| 684 | * being finished in another context. Returns false if the flushing is |
| 685 | * complete but still needs the I/O portion of the bio to be processed. |
| 686 | */ |
| 687 | bool md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 688 | { |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 689 | ktime_t start = ktime_get_boottime(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 690 | spin_lock_irq(&mddev->lock); |
| 691 | wait_event_lock_irq(mddev->sb_wait, |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 692 | !mddev->flush_bio || |
| 693 | ktime_after(mddev->last_flush, start), |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 694 | mddev->lock); |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 695 | if (!ktime_after(mddev->last_flush, start)) { |
| 696 | WARN_ON(mddev->flush_bio); |
| 697 | mddev->flush_bio = bio; |
| 698 | bio = NULL; |
| 699 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 700 | spin_unlock_irq(&mddev->lock); |
| 701 | |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 702 | if (!bio) { |
| 703 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 704 | queue_work(md_wq, &mddev->flush_work); |
| 705 | } else { |
| 706 | /* flush was performed for some other bio while we waited. */ |
| 707 | if (bio->bi_iter.bi_size == 0) |
| 708 | /* an empty barrier - all done */ |
| 709 | bio_endio(bio); |
| 710 | else { |
| 711 | bio->bi_opf &= ~REQ_PREFLUSH; |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 712 | return false; |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 713 | } |
| 714 | } |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 715 | return true; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 716 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 717 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 718 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 719 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | { |
| 721 | atomic_inc(&mddev->active); |
| 722 | return mddev; |
| 723 | } |
| 724 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 725 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 726 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 727 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | { |
| 729 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 730 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 731 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 732 | mddev->ctime == 0 && !mddev->hold_active) { |
| 733 | /* Array is not configured at all, and not held active, |
| 734 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 735 | list_del_init(&mddev->all_mddevs); |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * Call queue_work inside the spinlock so that |
| 739 | * flush_workqueue() after mddev_find will succeed in waiting |
| 740 | * for the work to be done. |
| 741 | */ |
| 742 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 743 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 744 | } |
| 745 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 748 | static void md_safemode_timeout(struct timer_list *t); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 749 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 750 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 751 | { |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 752 | kobject_init(&mddev->kobj, &md_ktype); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 753 | mutex_init(&mddev->open_mutex); |
| 754 | mutex_init(&mddev->reconfig_mutex); |
| 755 | mutex_init(&mddev->bitmap_info.mutex); |
| 756 | INIT_LIST_HEAD(&mddev->disks); |
| 757 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 758 | timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 759 | atomic_set(&mddev->active, 1); |
| 760 | atomic_set(&mddev->openers, 0); |
| 761 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 762 | spin_lock_init(&mddev->lock); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 763 | atomic_set(&mddev->flush_pending, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 764 | init_waitqueue_head(&mddev->sb_wait); |
| 765 | init_waitqueue_head(&mddev->recovery_wait); |
| 766 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 767 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 768 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 769 | mddev->resync_min = 0; |
| 770 | mddev->resync_max = MaxSector; |
| 771 | mddev->level = LEVEL_NONE; |
| 772 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 773 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 774 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 775 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 777 | struct mddev *mddev, *new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 779 | if (unit && MAJOR(unit) != MD_MAJOR) |
| 780 | unit &= ~((1<<MdpMinorShift)-1); |
| 781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | retry: |
| 783 | spin_lock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 785 | if (unit) { |
| 786 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 787 | if (mddev->unit == unit) { |
| 788 | mddev_get(mddev); |
| 789 | spin_unlock(&all_mddevs_lock); |
| 790 | kfree(new); |
| 791 | return mddev; |
| 792 | } |
| 793 | |
| 794 | if (new) { |
| 795 | list_add(&new->all_mddevs, &all_mddevs); |
| 796 | spin_unlock(&all_mddevs_lock); |
| 797 | new->hold_active = UNTIL_IOCTL; |
| 798 | return new; |
| 799 | } |
| 800 | } else if (new) { |
| 801 | /* find an unused unit number */ |
| 802 | static int next_minor = 512; |
| 803 | int start = next_minor; |
| 804 | int is_free = 0; |
| 805 | int dev = 0; |
| 806 | while (!is_free) { |
| 807 | dev = MKDEV(MD_MAJOR, next_minor); |
| 808 | next_minor++; |
| 809 | if (next_minor > MINORMASK) |
| 810 | next_minor = 0; |
| 811 | if (next_minor == start) { |
| 812 | /* Oh dear, all in use. */ |
| 813 | spin_unlock(&all_mddevs_lock); |
| 814 | kfree(new); |
| 815 | return NULL; |
| 816 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 817 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 818 | is_free = 1; |
| 819 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 820 | if (mddev->unit == dev) { |
| 821 | is_free = 0; |
| 822 | break; |
| 823 | } |
| 824 | } |
| 825 | new->unit = dev; |
| 826 | new->md_minor = MINOR(dev); |
| 827 | new->hold_active = UNTIL_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | list_add(&new->all_mddevs, &all_mddevs); |
| 829 | spin_unlock(&all_mddevs_lock); |
| 830 | return new; |
| 831 | } |
| 832 | spin_unlock(&all_mddevs_lock); |
| 833 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 834 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | if (!new) |
| 836 | return NULL; |
| 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | new->unit = unit; |
| 839 | if (MAJOR(unit) == MD_MAJOR) |
| 840 | new->md_minor = MINOR(unit); |
| 841 | else |
| 842 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 843 | |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 844 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | goto retry; |
| 847 | } |
| 848 | |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 849 | static struct attribute_group md_redundancy_group; |
| 850 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 851 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 853 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 854 | /* These cannot be removed under reconfig_mutex as |
| 855 | * an access to the files will try to take reconfig_mutex |
| 856 | * while holding the file unremovable, which leads to |
| 857 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 858 | * So hold set sysfs_active while the remove in happeing, |
| 859 | * and anything else which might set ->to_remove or my |
| 860 | * otherwise change the sysfs namespace will fail with |
| 861 | * -EBUSY if sysfs_active is still set. |
| 862 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 863 | * test it under the same mutex to ensure its correct value |
| 864 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 865 | */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 866 | struct attribute_group *to_remove = mddev->to_remove; |
| 867 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 868 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 869 | mutex_unlock(&mddev->reconfig_mutex); |
| 870 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 871 | if (mddev->kobj.sd) { |
| 872 | if (to_remove != &md_redundancy_group) |
| 873 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 874 | if (mddev->pers == NULL || |
| 875 | mddev->pers->sync_request == NULL) { |
| 876 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 877 | if (mddev->sysfs_action) |
| 878 | sysfs_put(mddev->sysfs_action); |
| 879 | mddev->sysfs_action = NULL; |
| 880 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 881 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 882 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 883 | } else |
| 884 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 886 | /* As we've dropped the mutex we need a spinlock to |
| 887 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 888 | */ |
| 889 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 890 | md_wakeup_thread(mddev->thread); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 891 | wake_up(&mddev->sb_wait); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 892 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 894 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 896 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 897 | { |
| 898 | struct md_rdev *rdev; |
| 899 | |
| 900 | rdev_for_each_rcu(rdev, mddev) |
| 901 | if (rdev->desc_nr == nr) |
| 902 | return rdev; |
| 903 | |
| 904 | return NULL; |
| 905 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 906 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 907 | |
| 908 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 910 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 912 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | if (rdev->bdev->bd_dev == dev) |
| 914 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | return NULL; |
| 917 | } |
| 918 | |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 919 | struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 920 | { |
| 921 | struct md_rdev *rdev; |
| 922 | |
| 923 | rdev_for_each_rcu(rdev, mddev) |
| 924 | if (rdev->bdev->bd_dev == dev) |
| 925 | return rdev; |
| 926 | |
| 927 | return NULL; |
| 928 | } |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 929 | EXPORT_SYMBOL_GPL(md_find_rdev_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 930 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 931 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 932 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 933 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 934 | list_for_each_entry(pers, &pers_list, list) { |
| 935 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 936 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 937 | if (strcmp(pers->name, clevel)==0) |
| 938 | return pers; |
| 939 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 940 | return NULL; |
| 941 | } |
| 942 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 943 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 944 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | { |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 946 | sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512; |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 947 | return MD_NEW_SIZE_SECTORS(num_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } |
| 949 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 950 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | rdev->sb_page = alloc_page(GFP_KERNEL); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 953 | if (!rdev->sb_page) |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 954 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | return 0; |
| 956 | } |
| 957 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 958 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | { |
| 960 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 961 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | rdev->sb_loaded = 0; |
| 963 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 964 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 965 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 967 | if (rdev->bb_page) { |
| 968 | put_page(rdev->bb_page); |
| 969 | rdev->bb_page = NULL; |
| 970 | } |
Dan Williams | d3b407fb | 2016-01-06 12:19:22 -0800 | [diff] [blame] | 971 | badblocks_exit(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 973 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 975 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 976 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 977 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 978 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 979 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 980 | if (bio->bi_status) { |
Guoqing Jiang | b3db8a2 | 2020-07-28 12:01:41 +0200 | [diff] [blame^] | 981 | pr_err("md: %s gets error=%d\n", __func__, |
| 982 | blk_status_to_errno(bio->bi_status)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 983 | md_error(mddev, rdev); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 984 | if (!test_bit(Faulty, &rdev->flags) |
| 985 | && (bio->bi_opf & MD_FAILFAST)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 986 | set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 987 | set_bit(LastDev, &rdev->flags); |
| 988 | } |
| 989 | } else |
| 990 | clear_bit(LastDev, &rdev->flags); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 991 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 992 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 993 | wake_up(&mddev->sb_wait); |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 994 | rdev_dec_pending(rdev, mddev); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 995 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 996 | } |
| 997 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 998 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 999 | sector_t sector, int size, struct page *page) |
| 1000 | { |
| 1001 | /* write first size bytes of page to sector of rdev |
| 1002 | * Increment mddev->pending_writes before returning |
| 1003 | * and decrement it on completion, waking up sb_wait |
| 1004 | * if zero is reached. |
| 1005 | * If an error occurred, call md_error |
| 1006 | */ |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1007 | struct bio *bio; |
| 1008 | int ff = 0; |
| 1009 | |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 1010 | if (!page) |
| 1011 | return; |
| 1012 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1013 | if (test_bit(Faulty, &rdev->flags)) |
| 1014 | return; |
| 1015 | |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 1016 | bio = md_bio_alloc_sync(mddev); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1017 | |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 1018 | atomic_inc(&rdev->nr_pending); |
| 1019 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1020 | bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1021 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1022 | bio_add_page(bio, page, size, 0); |
| 1023 | bio->bi_private = rdev; |
| 1024 | bio->bi_end_io = super_written; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1025 | |
| 1026 | if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) && |
| 1027 | test_bit(FailFast, &rdev->flags) && |
| 1028 | !test_bit(LastDev, &rdev->flags)) |
| 1029 | ff = MD_FAILFAST; |
Jan Kara | 5a8948f | 2017-05-31 09:44:33 +0200 | [diff] [blame] | 1030 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1031 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1032 | atomic_inc(&mddev->pending_writes); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1033 | submit_bio(bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1036 | int md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 1037 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1038 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 1039 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1040 | if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags)) |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1041 | return -EAGAIN; |
| 1042 | return 0; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1045 | int sync_page_io(struct md_rdev *rdev, sector_t sector, int size, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1046 | struct page *page, int op, int op_flags, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | { |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 1048 | struct bio *bio = md_bio_alloc_sync(rdev->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | int ret; |
| 1050 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1051 | if (metadata_op && rdev->meta_bdev) |
| 1052 | bio_set_dev(bio, rdev->meta_bdev); |
| 1053 | else |
| 1054 | bio_set_dev(bio, rdev->bdev); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1055 | bio_set_op_attrs(bio, op, op_flags); |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1056 | if (metadata_op) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1057 | bio->bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 1058 | else if (rdev->mddev->reshape_position != MaxSector && |
| 1059 | (rdev->mddev->reshape_backwards == |
| 1060 | (sector >= rdev->mddev->reshape_position))) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1061 | bio->bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1062 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1063 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | bio_add_page(bio, page, size, 0); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1065 | |
| 1066 | submit_bio_wait(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1068 | ret = !bio->bi_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | bio_put(bio); |
| 1070 | return ret; |
| 1071 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1072 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1074 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | { |
| 1076 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 1077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | if (rdev->sb_loaded) |
| 1079 | return 0; |
| 1080 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1081 | if (!sync_page_io(rdev, 0, size, rdev->sb_page, REQ_OP_READ, 0, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | goto fail; |
| 1083 | rdev->sb_loaded = 1; |
| 1084 | return 0; |
| 1085 | |
| 1086 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1087 | pr_err("md: disabled device %s, could not read superblock.\n", |
| 1088 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | return -EINVAL; |
| 1090 | } |
| 1091 | |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1092 | static int md_uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1094 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 1095 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 1096 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 1097 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1100 | static int md_sb_equal(mdp_super_t *sb1, mdp_super_t *sb2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | { |
| 1102 | int ret; |
| 1103 | mdp_super_t *tmp1, *tmp2; |
| 1104 | |
| 1105 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 1106 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 1107 | |
| 1108 | if (!tmp1 || !tmp2) { |
| 1109 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | goto abort; |
| 1111 | } |
| 1112 | |
| 1113 | *tmp1 = *sb1; |
| 1114 | *tmp2 = *sb2; |
| 1115 | |
| 1116 | /* |
| 1117 | * nr_disks is not constant |
| 1118 | */ |
| 1119 | tmp1->nr_disks = 0; |
| 1120 | tmp2->nr_disks = 0; |
| 1121 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 1122 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 1124 | kfree(tmp1); |
| 1125 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | return ret; |
| 1127 | } |
| 1128 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1129 | static u32 md_csum_fold(u32 csum) |
| 1130 | { |
| 1131 | csum = (csum & 0xffff) + (csum >> 16); |
| 1132 | return (csum & 0xffff) + (csum >> 16); |
| 1133 | } |
| 1134 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1135 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1137 | u64 newcsum = 0; |
| 1138 | u32 *sb32 = (u32*)sb; |
| 1139 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | unsigned int disk_csum, csum; |
| 1141 | |
| 1142 | disk_csum = sb->sb_csum; |
| 1143 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1144 | |
| 1145 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 1146 | newcsum += sb32[i]; |
| 1147 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 1148 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1149 | #ifdef CONFIG_ALPHA |
| 1150 | /* This used to use csum_partial, which was wrong for several |
| 1151 | * reasons including that different results are returned on |
| 1152 | * different architectures. It isn't critical that we get exactly |
| 1153 | * the same return value as before (we always csum_fold before |
| 1154 | * testing, and that removes any differences). However as we |
| 1155 | * know that csum_partial always returned a 16bit value on |
| 1156 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 1157 | */ |
| 1158 | sb->sb_csum = md_csum_fold(disk_csum); |
| 1159 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1161 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | return csum; |
| 1163 | } |
| 1164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | /* |
| 1166 | * Handle superblock details. |
| 1167 | * We want to be able to handle multiple superblock formats |
| 1168 | * so we have a common interface to them all, and an array of |
| 1169 | * different handlers. |
| 1170 | * We rely on user-space to write the initial superblock, and support |
| 1171 | * reading and updating of superblocks. |
| 1172 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1173 | * 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] | 1174 | * loads and validates a superblock on dev. |
| 1175 | * if refdev != NULL, compare superblocks on both devices |
| 1176 | * Return: |
| 1177 | * 0 - dev has a superblock that is compatible with refdev |
| 1178 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1179 | * so dev should be used as the refdev in future |
| 1180 | * -EINVAL superblock incompatible or invalid |
| 1181 | * -othererror e.g. -EIO |
| 1182 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1183 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | * Verify that dev is acceptable into mddev. |
| 1185 | * The first time, mddev->raid_disks will be 0, and data from |
| 1186 | * dev should be merged in. Subsequent calls check that dev |
| 1187 | * is new enough. Return 0 or -EINVAL |
| 1188 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1189 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | * Update the superblock for rdev with data in mddev |
| 1191 | * This does not write to disc. |
| 1192 | * |
| 1193 | */ |
| 1194 | |
| 1195 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1196 | char *name; |
| 1197 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1198 | int (*load_super)(struct md_rdev *rdev, |
| 1199 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1200 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1201 | int (*validate_super)(struct mddev *mddev, |
| 1202 | struct md_rdev *rdev); |
| 1203 | void (*sync_super)(struct mddev *mddev, |
| 1204 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1205 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1206 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1207 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 1208 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | }; |
| 1210 | |
| 1211 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1212 | * Check that the given mddev has no bitmap. |
| 1213 | * |
| 1214 | * This function is called from the run method of all personalities that do not |
| 1215 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1216 | * has a bitmap. Otherwise, it returns 0. |
| 1217 | * |
| 1218 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1219 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1220 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1221 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1222 | return 0; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1223 | pr_warn("%s: bitmaps are not supported for %s\n", |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1224 | mdname(mddev), mddev->pers->name); |
| 1225 | return 1; |
| 1226 | } |
| 1227 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1228 | |
| 1229 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1230 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1232 | 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] | 1233 | { |
| 1234 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1235 | mdp_super_t *sb; |
| 1236 | int ret; |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1237 | bool spare_disk = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | |
| 1239 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1240 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | * it's at the end of the disk. |
| 1242 | * |
| 1243 | * It also happens to be a multiple of 4Kb. |
| 1244 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1245 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1247 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1248 | if (ret) |
| 1249 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | |
| 1251 | ret = -EINVAL; |
| 1252 | |
| 1253 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1254 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | |
| 1256 | if (sb->md_magic != MD_SB_MAGIC) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1257 | pr_warn("md: invalid raid superblock magic on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | goto abort; |
| 1259 | } |
| 1260 | |
| 1261 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1262 | sb->minor_version < 90 || |
| 1263 | sb->minor_version > 91) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1264 | pr_warn("Bad version number %d.%d on %s\n", |
| 1265 | sb->major_version, sb->minor_version, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | goto abort; |
| 1267 | } |
| 1268 | |
| 1269 | if (sb->raid_disks <= 0) |
| 1270 | goto abort; |
| 1271 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1272 | if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1273 | pr_warn("md: invalid superblock checksum on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | goto abort; |
| 1275 | } |
| 1276 | |
| 1277 | rdev->preferred_minor = sb->md_minor; |
| 1278 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1279 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1280 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1281 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
| 1283 | if (sb->level == LEVEL_MULTIPATH) |
| 1284 | rdev->desc_nr = -1; |
| 1285 | else |
| 1286 | rdev->desc_nr = sb->this_disk.number; |
| 1287 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1288 | /* not spare disk, or LEVEL_MULTIPATH */ |
| 1289 | if (sb->level == LEVEL_MULTIPATH || |
| 1290 | (rdev->desc_nr >= 0 && |
Yufen Yu | 3b7436c | 2019-12-10 15:01:29 +0800 | [diff] [blame] | 1291 | rdev->desc_nr < MD_SB_DISKS && |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1292 | sb->disks[rdev->desc_nr].state & |
| 1293 | ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))) |
| 1294 | spare_disk = false; |
| 1295 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1296 | if (!refdev) { |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1297 | if (!spare_disk) |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1298 | ret = 1; |
| 1299 | else |
| 1300 | ret = 0; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1301 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1303 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1304 | if (!md_uuid_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1305 | pr_warn("md: %s has different UUID to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | b, bdevname(refdev->bdev,b2)); |
| 1307 | goto abort; |
| 1308 | } |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1309 | if (!md_sb_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1310 | pr_warn("md: %s has same UUID but different superblock to %s\n", |
| 1311 | b, bdevname(refdev->bdev, b2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | goto abort; |
| 1313 | } |
| 1314 | ev1 = md_event(sb); |
| 1315 | ev2 = md_event(refsb); |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1316 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1317 | if (!spare_disk && ev1 > ev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1319 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | ret = 0; |
| 1321 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1322 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1323 | /* Limit to 4TB as metadata cannot record more than that. |
| 1324 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1325 | * record this size) |
| 1326 | */ |
Christoph Hellwig | 72deb45 | 2019-04-05 18:08:59 +0200 | [diff] [blame] | 1327 | if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1328 | rdev->sectors = (sector_t)(2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1330 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1331 | /* "this cannot possibly happen" ... */ |
| 1332 | ret = -EINVAL; |
| 1333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | abort: |
| 1335 | return ret; |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * validate_super for 0.90.0 |
| 1340 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1341 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | { |
| 1343 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1344 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1345 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1347 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1348 | clear_bit(Faulty, &rdev->flags); |
| 1349 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1350 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1351 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1352 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | if (mddev->raid_disks == 0) { |
| 1354 | mddev->major_version = 0; |
| 1355 | mddev->minor_version = sb->minor_version; |
| 1356 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1357 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1358 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | mddev->ctime = sb->ctime; |
| 1360 | mddev->utime = sb->utime; |
| 1361 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1362 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | mddev->layout = sb->layout; |
| 1364 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1365 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1366 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1367 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1368 | mddev->bitmap_info.space = 0; |
| 1369 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1370 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1371 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1372 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1374 | if (mddev->minor_version >= 91) { |
| 1375 | mddev->reshape_position = sb->reshape_position; |
| 1376 | mddev->delta_disks = sb->delta_disks; |
| 1377 | mddev->new_level = sb->new_level; |
| 1378 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1379 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1380 | if (mddev->delta_disks < 0) |
| 1381 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1382 | } else { |
| 1383 | mddev->reshape_position = MaxSector; |
| 1384 | mddev->delta_disks = 0; |
| 1385 | mddev->new_level = mddev->level; |
| 1386 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1387 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1388 | } |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1389 | if (mddev->level == 0) |
| 1390 | mddev->layout = -1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1393 | mddev->recovery_cp = MaxSector; |
| 1394 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1395 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | sb->events_lo == sb->cp_events_lo) { |
| 1397 | mddev->recovery_cp = sb->recovery_cp; |
| 1398 | } else |
| 1399 | mddev->recovery_cp = 0; |
| 1400 | } |
| 1401 | |
| 1402 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1403 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1404 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1405 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1406 | |
| 1407 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1408 | |
| 1409 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1410 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1411 | mddev->bitmap_info.offset = |
| 1412 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1413 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1414 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1415 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1416 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1417 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1418 | /* Insist on good event counter while assembling, except |
| 1419 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1421 | if (sb->disks[rdev->desc_nr].state & ( |
| 1422 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1423 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1424 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1425 | } else if (mddev->bitmap) { |
| 1426 | /* if adding to array with a bitmap, then we can accept an |
| 1427 | * older device ... but not too old. |
| 1428 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1429 | if (ev1 < mddev->bitmap->events_cleared) |
| 1430 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1431 | if (ev1 < mddev->events) |
| 1432 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1433 | } else { |
| 1434 | if (ev1 < mddev->events) |
| 1435 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1436 | return 0; |
| 1437 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | desc = sb->disks + rdev->desc_nr; |
| 1441 | |
| 1442 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1443 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1444 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1445 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1446 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1448 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1449 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1450 | /* active but not in sync implies recovery up to |
| 1451 | * reshape position. We don't know exactly where |
| 1452 | * that is, so set to zero for now */ |
| 1453 | if (mddev->minor_version >= 91) { |
| 1454 | rdev->recovery_offset = 0; |
| 1455 | rdev->raid_disk = desc->raid_disk; |
| 1456 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1458 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1459 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1460 | if (desc->state & (1<<MD_DISK_FAILFAST)) |
| 1461 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1462 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1463 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * sync_super for 0.90.0 |
| 1469 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1470 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | { |
| 1472 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1473 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | /* make rdev->sb match mddev data.. |
| 1477 | * |
| 1478 | * 1/ zero out disks |
| 1479 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1480 | * 3/ any empty disks < next_spare become removed |
| 1481 | * |
| 1482 | * disks[0] gets initialised to REMOVED because |
| 1483 | * we cannot be sure from other fields if it has |
| 1484 | * been initialised or not. |
| 1485 | */ |
| 1486 | int i; |
| 1487 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1488 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1489 | rdev->sb_size = MD_SB_BYTES; |
| 1490 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1491 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | |
| 1493 | memset(sb, 0, sizeof(*sb)); |
| 1494 | |
| 1495 | sb->md_magic = MD_SB_MAGIC; |
| 1496 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | sb->patch_version = mddev->patch_version; |
| 1498 | sb->gvalid_words = 0; /* ignored */ |
| 1499 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1500 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1501 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1502 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1503 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1504 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1506 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | sb->raid_disks = mddev->raid_disks; |
| 1508 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1509 | sb->not_persistent = 0; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1510 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | sb->state = 0; |
| 1512 | sb->events_hi = (mddev->events>>32); |
| 1513 | sb->events_lo = (u32)mddev->events; |
| 1514 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1515 | if (mddev->reshape_position == MaxSector) |
| 1516 | sb->minor_version = 90; |
| 1517 | else { |
| 1518 | sb->minor_version = 91; |
| 1519 | sb->reshape_position = mddev->reshape_position; |
| 1520 | sb->new_level = mddev->new_level; |
| 1521 | sb->delta_disks = mddev->delta_disks; |
| 1522 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1523 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1524 | } |
| 1525 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | if (mddev->in_sync) |
| 1527 | { |
| 1528 | sb->recovery_cp = mddev->recovery_cp; |
| 1529 | sb->cp_events_hi = (mddev->events>>32); |
| 1530 | sb->cp_events_lo = (u32)mddev->events; |
| 1531 | if (mddev->recovery_cp == MaxSector) |
| 1532 | sb->state = (1<< MD_SB_CLEAN); |
| 1533 | } else |
| 1534 | sb->recovery_cp = 0; |
| 1535 | |
| 1536 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1537 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1539 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1540 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1543 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1545 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1546 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1547 | |
| 1548 | if (rdev2->raid_disk >= 0 && |
| 1549 | sb->minor_version >= 91) |
| 1550 | /* we have nowhere to store the recovery_offset, |
| 1551 | * but if it is not below the reshape_position, |
| 1552 | * we can piggy-back on that. |
| 1553 | */ |
| 1554 | is_active = 1; |
| 1555 | if (rdev2->raid_disk < 0 || |
| 1556 | test_bit(Faulty, &rdev2->flags)) |
| 1557 | is_active = 0; |
| 1558 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1559 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1561 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1562 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | d = &sb->disks[rdev2->desc_nr]; |
| 1564 | nr_disks++; |
| 1565 | d->number = rdev2->desc_nr; |
| 1566 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1567 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1568 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | d->raid_disk = rdev2->raid_disk; |
| 1570 | else |
| 1571 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1572 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1574 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1576 | if (test_bit(In_sync, &rdev2->flags)) |
| 1577 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | active++; |
| 1579 | working++; |
| 1580 | } else { |
| 1581 | d->state = 0; |
| 1582 | spare++; |
| 1583 | working++; |
| 1584 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1585 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1586 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1587 | if (test_bit(FailFast, &rdev2->flags)) |
| 1588 | d->state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1591 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1592 | mdp_disk_t *d = &sb->disks[i]; |
| 1593 | if (d->state == 0 && d->number == 0) { |
| 1594 | d->number = i; |
| 1595 | d->raid_disk = i; |
| 1596 | d->state = (1<<MD_DISK_REMOVED); |
| 1597 | d->state |= (1<<MD_DISK_FAULTY); |
| 1598 | failed++; |
| 1599 | } |
| 1600 | } |
| 1601 | sb->nr_disks = nr_disks; |
| 1602 | sb->active_disks = active; |
| 1603 | sb->working_disks = working; |
| 1604 | sb->failed_disks = failed; |
| 1605 | sb->spare_disks = spare; |
| 1606 | |
| 1607 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1608 | sb->sb_csum = calc_sb_csum(sb); |
| 1609 | } |
| 1610 | |
| 1611 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1612 | * rdev_size_change for 0.90.0 |
| 1613 | */ |
| 1614 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1615 | 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] | 1616 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1617 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1618 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1619 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1620 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1621 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1622 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1623 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1624 | /* Limit to 4TB as metadata cannot record more than that. |
| 1625 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1626 | */ |
Christoph Hellwig | 72deb45 | 2019-04-05 18:08:59 +0200 | [diff] [blame] | 1627 | if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1628 | num_sectors = (sector_t)(2ULL << 32) - 2; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1629 | do { |
| 1630 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1631 | rdev->sb_page); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1632 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1633 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1634 | } |
| 1635 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1636 | static int |
| 1637 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1638 | { |
| 1639 | /* non-zero offset changes not possible with v0.90 */ |
| 1640 | return new_offset == 0; |
| 1641 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1642 | |
| 1643 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | * version 1 superblock |
| 1645 | */ |
| 1646 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1647 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1649 | __le32 disk_csum; |
| 1650 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | unsigned long long newcsum; |
| 1652 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1653 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | |
| 1655 | disk_csum = sb->sb_csum; |
| 1656 | sb->sb_csum = 0; |
| 1657 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1658 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | newcsum += le32_to_cpu(*isuper++); |
| 1660 | |
| 1661 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1662 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
| 1664 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1665 | sb->sb_csum = disk_csum; |
| 1666 | return cpu_to_le32(csum); |
| 1667 | } |
| 1668 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1669 | 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] | 1670 | { |
| 1671 | struct mdp_superblock_1 *sb; |
| 1672 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1673 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1674 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1676 | int bmask; |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1677 | bool spare_disk = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | |
| 1679 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1680 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | * It is always aligned to a 4K boundary and |
| 1682 | * depeding on minor_version, it can be: |
| 1683 | * 0: At least 8K, but less than 12K, from end of device |
| 1684 | * 1: At start of device |
| 1685 | * 2: 4K from start of device. |
| 1686 | */ |
| 1687 | switch(minor_version) { |
| 1688 | case 0: |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 1689 | sb_start = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1690 | sb_start -= 8*2; |
| 1691 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | break; |
| 1693 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1694 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | break; |
| 1696 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1697 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | break; |
| 1699 | default: |
| 1700 | return -EINVAL; |
| 1701 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1702 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1704 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1705 | * and it is safe to read 4k, so we do that |
| 1706 | */ |
| 1707 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | if (ret) return ret; |
| 1709 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1710 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
| 1712 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1713 | sb->major_version != cpu_to_le32(1) || |
| 1714 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1715 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1716 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | return -EINVAL; |
| 1718 | |
| 1719 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1720 | pr_warn("md: invalid superblock checksum on %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | bdevname(rdev->bdev,b)); |
| 1722 | return -EINVAL; |
| 1723 | } |
| 1724 | if (le64_to_cpu(sb->data_size) < 10) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1725 | pr_warn("md: data_size too small on %s\n", |
| 1726 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | return -EINVAL; |
| 1728 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1729 | if (sb->pad0 || |
| 1730 | sb->pad3[0] || |
| 1731 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1732 | /* Some padding is non-zero, might be a new feature */ |
| 1733 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | rdev->preferred_minor = 0xffff; |
| 1736 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1737 | rdev->new_data_offset = rdev->data_offset; |
| 1738 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1739 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1740 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1741 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1743 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1744 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1745 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1746 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1747 | |
| 1748 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1749 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1750 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1751 | if (minor_version |
| 1752 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1753 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1754 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1755 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1756 | rdev->desc_nr = -1; |
| 1757 | else |
| 1758 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1759 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1760 | if (!rdev->bb_page) { |
| 1761 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1762 | if (!rdev->bb_page) |
| 1763 | return -ENOMEM; |
| 1764 | } |
| 1765 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1766 | rdev->badblocks.count == 0) { |
| 1767 | /* need to load the bad block list. |
| 1768 | * Currently we limit it to one page. |
| 1769 | */ |
| 1770 | s32 offset; |
| 1771 | sector_t bb_sector; |
Christoph Hellwig | 00485d0 | 2019-04-04 18:56:12 +0200 | [diff] [blame] | 1772 | __le64 *bbp; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1773 | int i; |
| 1774 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1775 | if (sectors > (PAGE_SIZE / 512)) |
| 1776 | return -EINVAL; |
| 1777 | offset = le32_to_cpu(sb->bblog_offset); |
| 1778 | if (offset == 0) |
| 1779 | return -EINVAL; |
| 1780 | bb_sector = (long long)offset; |
| 1781 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1782 | rdev->bb_page, REQ_OP_READ, 0, true)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1783 | return -EIO; |
Christoph Hellwig | 00485d0 | 2019-04-04 18:56:12 +0200 | [diff] [blame] | 1784 | bbp = (__le64 *)page_address(rdev->bb_page); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1785 | rdev->badblocks.shift = sb->bblog_shift; |
| 1786 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1787 | u64 bb = le64_to_cpu(*bbp); |
| 1788 | int count = bb & (0x3ff); |
| 1789 | u64 sector = bb >> 10; |
| 1790 | sector <<= sb->bblog_shift; |
| 1791 | count <<= sb->bblog_shift; |
| 1792 | if (bb + 1 == 0) |
| 1793 | break; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 1794 | if (badblocks_set(&rdev->badblocks, sector, count, 1)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1795 | return -EINVAL; |
| 1796 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1797 | } else if (sb->bblog_offset != 0) |
| 1798 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1799 | |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1800 | if ((le32_to_cpu(sb->feature_map) & |
| 1801 | (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) { |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1802 | rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset); |
| 1803 | rdev->ppl.size = le16_to_cpu(sb->ppl.size); |
| 1804 | rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset; |
| 1805 | } |
| 1806 | |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1807 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) && |
| 1808 | sb->level != 0) |
| 1809 | return -EINVAL; |
| 1810 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1811 | /* not spare disk, or LEVEL_MULTIPATH */ |
| 1812 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) || |
| 1813 | (rdev->desc_nr >= 0 && |
| 1814 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1815 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1816 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))) |
| 1817 | spare_disk = false; |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1818 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1819 | if (!refdev) { |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1820 | if (!spare_disk) |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1821 | ret = 1; |
| 1822 | else |
| 1823 | ret = 0; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1824 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1826 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | |
| 1828 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1829 | sb->level != refsb->level || |
| 1830 | sb->layout != refsb->layout || |
| 1831 | sb->chunksize != refsb->chunksize) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1832 | pr_warn("md: %s has strangely different superblock to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | bdevname(rdev->bdev,b), |
| 1834 | bdevname(refdev->bdev,b2)); |
| 1835 | return -EINVAL; |
| 1836 | } |
| 1837 | ev1 = le64_to_cpu(sb->events); |
| 1838 | ev2 = le64_to_cpu(refsb->events); |
| 1839 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1840 | if (!spare_disk && ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1841 | ret = 1; |
| 1842 | else |
| 1843 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1845 | if (minor_version) { |
| 1846 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9); |
| 1847 | sectors -= rdev->data_offset; |
| 1848 | } else |
| 1849 | sectors = rdev->sb_start; |
| 1850 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1852 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1853 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1856 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1858 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1859 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1861 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1862 | clear_bit(Faulty, &rdev->flags); |
| 1863 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1864 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1865 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | if (mddev->raid_disks == 0) { |
| 1868 | mddev->major_version = 1; |
| 1869 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1870 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1871 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1872 | mddev->ctime = le64_to_cpu(sb->ctime); |
| 1873 | mddev->utime = le64_to_cpu(sb->utime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1875 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | mddev->layout = le32_to_cpu(sb->layout); |
| 1877 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1878 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1879 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1880 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1881 | mddev->bitmap_info.space = 0; |
| 1882 | /* Default location for bitmap is 1K after superblock |
| 1883 | * using 3K - total of 4K |
| 1884 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1885 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1886 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1887 | mddev->reshape_backwards = 0; |
| 1888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1890 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1891 | |
| 1892 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1893 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1894 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1895 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1896 | mddev->bitmap_info.offset = |
| 1897 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1898 | /* Metadata doesn't record how much space is available. |
| 1899 | * For 1.0, we assume we can use up to the superblock |
| 1900 | * if before, else to 4K beyond superblock. |
| 1901 | * For others, assume no change is possible. |
| 1902 | */ |
| 1903 | if (mddev->minor_version > 0) |
| 1904 | mddev->bitmap_info.space = 0; |
| 1905 | else if (mddev->bitmap_info.offset > 0) |
| 1906 | mddev->bitmap_info.space = |
| 1907 | 8 - mddev->bitmap_info.offset; |
| 1908 | else |
| 1909 | mddev->bitmap_info.space = |
| 1910 | -mddev->bitmap_info.offset; |
| 1911 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1912 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1913 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1914 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1915 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1916 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1917 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1918 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1919 | if (mddev->delta_disks < 0 || |
| 1920 | (mddev->delta_disks == 0 && |
| 1921 | (le32_to_cpu(sb->feature_map) |
| 1922 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1923 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1924 | } else { |
| 1925 | mddev->reshape_position = MaxSector; |
| 1926 | mddev->delta_disks = 0; |
| 1927 | mddev->new_level = mddev->level; |
| 1928 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1929 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1930 | } |
| 1931 | |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1932 | if (mddev->level == 0 && |
| 1933 | !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT)) |
| 1934 | mddev->layout = -1; |
| 1935 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 1936 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame] | 1937 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1938 | |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1939 | if (le32_to_cpu(sb->feature_map) & |
| 1940 | (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) { |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1941 | if (le32_to_cpu(sb->feature_map) & |
| 1942 | (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL)) |
| 1943 | return -EINVAL; |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1944 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) && |
| 1945 | (le32_to_cpu(sb->feature_map) & |
| 1946 | MD_FEATURE_MULTIPLE_PPLS)) |
| 1947 | return -EINVAL; |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1948 | set_bit(MD_HAS_PPL, &mddev->flags); |
| 1949 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1950 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1951 | /* Insist of good event counter while assembling, except for |
| 1952 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1954 | if (rdev->desc_nr >= 0 && |
| 1955 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1956 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1957 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1958 | if (ev1 < mddev->events) |
| 1959 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1960 | } else if (mddev->bitmap) { |
| 1961 | /* If adding to array with a bitmap, then we can accept an |
| 1962 | * older device, but not too old. |
| 1963 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1964 | if (ev1 < mddev->bitmap->events_cleared) |
| 1965 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1966 | if (ev1 < mddev->events) |
| 1967 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1968 | } else { |
| 1969 | if (ev1 < mddev->events) |
| 1970 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1971 | return 0; |
| 1972 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1974 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1975 | if (rdev->desc_nr < 0 || |
| 1976 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1977 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1978 | rdev->desc_nr = -1; |
| 1979 | } else |
| 1980 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1982 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1984 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1985 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1987 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1988 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1989 | /* journal device without journal feature */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1990 | pr_warn("md: journal device provided without journal feature, ignoring the device\n"); |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1991 | return -EINVAL; |
| 1992 | } |
| 1993 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1994 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1995 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1996 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1998 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1999 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2000 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2001 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2002 | if (!(le32_to_cpu(sb->feature_map) & |
| 2003 | MD_FEATURE_RECOVERY_BITMAP)) |
| 2004 | rdev->saved_raid_disk = -1; |
Guoqing Jiang | 062f5b2a | 2019-07-24 11:09:20 +0200 | [diff] [blame] | 2005 | } else { |
| 2006 | /* |
| 2007 | * If the array is FROZEN, then the device can't |
| 2008 | * be in_sync with rest of array. |
| 2009 | */ |
| 2010 | if (!test_bit(MD_RECOVERY_FROZEN, |
| 2011 | &mddev->recovery)) |
| 2012 | set_bit(In_sync, &rdev->flags); |
| 2013 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | rdev->raid_disk = role; |
| 2015 | break; |
| 2016 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 2017 | if (sb->devflags & WriteMostly1) |
| 2018 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2019 | if (sb->devflags & FailFast1) |
| 2020 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2021 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 2022 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 2023 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2024 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 2025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | return 0; |
| 2027 | } |
| 2028 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2029 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | { |
| 2031 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2032 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | int max_dev, i; |
| 2034 | /* make rdev->sb match mddev and rdev data. */ |
| 2035 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2036 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | |
| 2038 | sb->feature_map = 0; |
| 2039 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2040 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 2042 | |
| 2043 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 2044 | sb->events = cpu_to_le64(mddev->events); |
| 2045 | if (mddev->in_sync) |
| 2046 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 2047 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 2048 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | else |
| 2050 | sb->resync_offset = cpu_to_le64(0); |
| 2051 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 2052 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 2053 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 2054 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2055 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2056 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 2057 | sb->level = cpu_to_le32(mddev->level); |
| 2058 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2059 | if (test_bit(FailFast, &rdev->flags)) |
| 2060 | sb->devflags |= FailFast1; |
| 2061 | else |
| 2062 | sb->devflags &= ~FailFast1; |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 2063 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 2064 | if (test_bit(WriteMostly, &rdev->flags)) |
| 2065 | sb->devflags |= WriteMostly1; |
| 2066 | else |
| 2067 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2068 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 2069 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 2070 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 2071 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 2072 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 2073 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2074 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2075 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2076 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2077 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 2078 | sb->feature_map |= |
| 2079 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 2080 | sb->recovery_offset = |
| 2081 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2082 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 2083 | sb->feature_map |= |
| 2084 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2085 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 2086 | /* Note: recovery_offset and journal_tail share space */ |
| 2087 | if (test_bit(Journal, &rdev->flags)) |
| 2088 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2089 | if (test_bit(Replacement, &rdev->flags)) |
| 2090 | sb->feature_map |= |
| 2091 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2092 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2093 | if (mddev->reshape_position != MaxSector) { |
| 2094 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 2095 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 2096 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 2097 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 2098 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2099 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2100 | if (mddev->delta_disks == 0 && |
| 2101 | mddev->reshape_backwards) |
| 2102 | sb->feature_map |
| 2103 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2104 | if (rdev->new_data_offset != rdev->data_offset) { |
| 2105 | sb->feature_map |
| 2106 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 2107 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 2108 | - rdev->data_offset)); |
| 2109 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2110 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2111 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 2112 | if (mddev_is_clustered(mddev)) |
| 2113 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 2114 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2115 | if (rdev->badblocks.count == 0) |
| 2116 | /* Nothing to do for bad blocks*/ ; |
| 2117 | else if (sb->bblog_offset == 0) |
| 2118 | /* Cannot record bad blocks on this device */ |
| 2119 | md_error(mddev, rdev); |
| 2120 | else { |
| 2121 | struct badblocks *bb = &rdev->badblocks; |
Christoph Hellwig | ae50640 | 2019-04-04 18:56:13 +0200 | [diff] [blame] | 2122 | __le64 *bbp = (__le64 *)page_address(rdev->bb_page); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2123 | u64 *p = bb->page; |
| 2124 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 2125 | if (bb->changed) { |
| 2126 | unsigned seq; |
| 2127 | |
| 2128 | retry: |
| 2129 | seq = read_seqbegin(&bb->lock); |
| 2130 | |
| 2131 | memset(bbp, 0xff, PAGE_SIZE); |
| 2132 | |
| 2133 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 2134 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2135 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 2136 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 2137 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2138 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2139 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2140 | if (read_seqretry(&bb->lock, seq)) |
| 2141 | goto retry; |
| 2142 | |
| 2143 | bb->sector = (rdev->sb_start + |
| 2144 | (int)le32_to_cpu(sb->bblog_offset)); |
| 2145 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2146 | } |
| 2147 | } |
| 2148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2149 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2150 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | if (rdev2->desc_nr+1 > max_dev) |
| 2152 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2153 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 2154 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 2155 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2156 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 2157 | rdev->sb_size = max_dev * 2 + 256; |
| 2158 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 2159 | if (rdev->sb_size & bmask) |
| 2160 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 2161 | } else |
| 2162 | max_dev = le32_to_cpu(sb->max_dev); |
| 2163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | for (i=0; i<max_dev;i++) |
Lidong Zhong | 8df7202 | 2017-06-12 10:45:55 +0800 | [diff] [blame] | 2165 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2166 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 2167 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 2168 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 2170 | if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 2171 | if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags)) |
| 2172 | sb->feature_map |= |
| 2173 | cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS); |
| 2174 | else |
| 2175 | sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 2176 | sb->ppl.offset = cpu_to_le16(rdev->ppl.offset); |
| 2177 | sb->ppl.size = cpu_to_le16(rdev->ppl.size); |
| 2178 | } |
| 2179 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2180 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2182 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 2183 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2184 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 2186 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 2187 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 2188 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2189 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 2191 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | } |
| 2193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | sb->sb_csum = calc_sb_1_csum(sb); |
| 2195 | } |
| 2196 | |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2197 | static sector_t super_1_choose_bm_space(sector_t dev_size) |
| 2198 | { |
| 2199 | sector_t bm_space; |
| 2200 | |
| 2201 | /* if the device is bigger than 8Gig, save 64k for bitmap |
| 2202 | * usage, if bigger than 200Gig, save 128k |
| 2203 | */ |
| 2204 | if (dev_size < 64*2) |
| 2205 | bm_space = 0; |
| 2206 | else if (dev_size - 64*2 >= 200*1024*1024*2) |
| 2207 | bm_space = 128*2; |
| 2208 | else if (dev_size - 4*2 > 8*1024*1024*2) |
| 2209 | bm_space = 64*2; |
| 2210 | else |
| 2211 | bm_space = 4*2; |
| 2212 | return bm_space; |
| 2213 | } |
| 2214 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2215 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2216 | 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] | 2217 | { |
| 2218 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2219 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2220 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2221 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2222 | if (rdev->data_offset != rdev->new_data_offset) |
| 2223 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2224 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2225 | /* minor versions 1 and 2; superblock before data */ |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 2226 | max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2227 | max_sectors -= rdev->data_offset; |
| 2228 | if (!num_sectors || num_sectors > max_sectors) |
| 2229 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 2230 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2231 | /* minor version 0 with bitmap we can't move */ |
| 2232 | return 0; |
| 2233 | } else { |
| 2234 | /* minor version 0; superblock after data */ |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2235 | sector_t sb_start, bm_space; |
| 2236 | sector_t dev_size = i_size_read(rdev->bdev->bd_inode) >> 9; |
| 2237 | |
| 2238 | /* 8K is for superblock */ |
| 2239 | sb_start = dev_size - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2240 | sb_start &= ~(sector_t)(4*2 - 1); |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2241 | |
| 2242 | bm_space = super_1_choose_bm_space(dev_size); |
| 2243 | |
| 2244 | /* Space that can be used to store date needs to decrease |
| 2245 | * superblock bitmap space and bad block space(4K) |
| 2246 | */ |
| 2247 | max_sectors = sb_start - bm_space - 4*2; |
| 2248 | |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2249 | if (!num_sectors || num_sectors > max_sectors) |
| 2250 | num_sectors = max_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2251 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2252 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2253 | sb->data_size = cpu_to_le64(num_sectors); |
Jason Yan | 3fb632e | 2017-03-10 11:27:23 +0800 | [diff] [blame] | 2254 | sb->super_offset = cpu_to_le64(rdev->sb_start); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2255 | sb->sb_csum = calc_sb_1_csum(sb); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2256 | do { |
| 2257 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
| 2258 | rdev->sb_page); |
| 2259 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 2260 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2261 | |
| 2262 | } |
| 2263 | |
| 2264 | static int |
| 2265 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 2266 | unsigned long long new_offset) |
| 2267 | { |
| 2268 | /* All necessary checks on new >= old have been done */ |
| 2269 | struct bitmap *bitmap; |
| 2270 | if (new_offset >= rdev->data_offset) |
| 2271 | return 1; |
| 2272 | |
| 2273 | /* with 1.0 metadata, there is no metadata to tread on |
| 2274 | * so we can always move back */ |
| 2275 | if (rdev->mddev->minor_version == 0) |
| 2276 | return 1; |
| 2277 | |
| 2278 | /* otherwise we must be sure not to step on |
| 2279 | * any metadata, so stay: |
| 2280 | * 36K beyond start of superblock |
| 2281 | * beyond end of badblocks |
| 2282 | * beyond write-intent bitmap |
| 2283 | */ |
| 2284 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 2285 | return 0; |
| 2286 | bitmap = rdev->mddev->bitmap; |
| 2287 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 2288 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 2289 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2290 | return 0; |
| 2291 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 2292 | return 0; |
| 2293 | |
| 2294 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 2297 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | [0] = { |
| 2299 | .name = "0.90.0", |
| 2300 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2301 | .load_super = super_90_load, |
| 2302 | .validate_super = super_90_validate, |
| 2303 | .sync_super = super_90_sync, |
| 2304 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2305 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | }, |
| 2307 | [1] = { |
| 2308 | .name = "md-1", |
| 2309 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2310 | .load_super = super_1_load, |
| 2311 | .validate_super = super_1_validate, |
| 2312 | .sync_super = super_1_sync, |
| 2313 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2314 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | }, |
| 2316 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2318 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2319 | { |
| 2320 | if (mddev->sync_super) { |
| 2321 | mddev->sync_super(mddev, rdev); |
| 2322 | return; |
| 2323 | } |
| 2324 | |
| 2325 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 2326 | |
| 2327 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 2328 | } |
| 2329 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2330 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2332 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2334 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2335 | rdev_for_each_rcu(rdev, mddev1) { |
| 2336 | if (test_bit(Faulty, &rdev->flags) || |
| 2337 | test_bit(Journal, &rdev->flags) || |
| 2338 | rdev->raid_disk == -1) |
| 2339 | continue; |
| 2340 | rdev_for_each_rcu(rdev2, mddev2) { |
| 2341 | if (test_bit(Faulty, &rdev2->flags) || |
| 2342 | test_bit(Journal, &rdev2->flags) || |
| 2343 | rdev2->raid_disk == -1) |
| 2344 | continue; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2345 | if (rdev->bdev->bd_contains == |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2346 | rdev2->bdev->bd_contains) { |
| 2347 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2348 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2349 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2350 | } |
| 2351 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2352 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | return 0; |
| 2354 | } |
| 2355 | |
| 2356 | static LIST_HEAD(pending_raid_disks); |
| 2357 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2358 | /* |
| 2359 | * Try to register data integrity profile for an mddev |
| 2360 | * |
| 2361 | * This is called when an array is started and after a disk has been kicked |
| 2362 | * from the array. It only succeeds if all working and active component devices |
| 2363 | * are integrity capable with matching profiles. |
| 2364 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2365 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2366 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2367 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2368 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2369 | if (list_empty(&mddev->disks)) |
| 2370 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2371 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2372 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2373 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2374 | /* skip spares and non-functional disks */ |
| 2375 | if (test_bit(Faulty, &rdev->flags)) |
| 2376 | continue; |
| 2377 | if (rdev->raid_disk < 0) |
| 2378 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2379 | if (!reference) { |
| 2380 | /* Use the first rdev as the reference */ |
| 2381 | reference = rdev; |
| 2382 | continue; |
| 2383 | } |
| 2384 | /* does this rdev's profile match the reference profile? */ |
| 2385 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2386 | rdev->bdev->bd_disk) < 0) |
| 2387 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2388 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2389 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2390 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2391 | /* |
| 2392 | * All component devices are integrity capable and have matching |
| 2393 | * profiles, register the common profile for the md device. |
| 2394 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2395 | blk_integrity_register(mddev->gendisk, |
| 2396 | bdev_get_integrity(reference->bdev)); |
| 2397 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2398 | pr_debug("md: data integrity enabled on %s\n", mdname(mddev)); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 2399 | if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2400 | pr_err("md: failed to create integrity pool for %s\n", |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2401 | mdname(mddev)); |
| 2402 | return -EINVAL; |
| 2403 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2404 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2405 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2406 | EXPORT_SYMBOL(md_integrity_register); |
| 2407 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2408 | /* |
| 2409 | * Attempt to add an rdev, but only if it is consistent with the current |
| 2410 | * integrity profile |
| 2411 | */ |
| 2412 | int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2413 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2414 | struct blk_integrity *bi_mddev; |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2415 | char name[BDEVNAME_SIZE]; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2416 | |
| 2417 | if (!mddev->gendisk) |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2418 | return 0; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2419 | |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2420 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2421 | |
| 2422 | if (!bi_mddev) /* nothing to do */ |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2423 | return 0; |
| 2424 | |
| 2425 | if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2426 | pr_err("%s: incompatible integrity profile for %s\n", |
| 2427 | mdname(mddev), bdevname(rdev->bdev, name)); |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2428 | return -ENXIO; |
| 2429 | } |
| 2430 | |
| 2431 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2432 | } |
| 2433 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2434 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2435 | 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] | 2436 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2437 | char b[BDEVNAME_SIZE]; |
NeilBrown | f637b9f | 2005-11-08 21:39:37 -0800 | [diff] [blame] | 2438 | struct kobject *ko; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2439 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2441 | /* prevent duplicates */ |
| 2442 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2443 | return -EEXIST; |
| 2444 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 2445 | if ((bdev_read_only(rdev->bdev) || bdev_read_only(rdev->meta_bdev)) && |
| 2446 | mddev->pers) |
| 2447 | return -EROFS; |
| 2448 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2449 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2450 | if (!test_bit(Journal, &rdev->flags) && |
| 2451 | rdev->sectors && |
| 2452 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2453 | if (mddev->pers) { |
| 2454 | /* Cannot change size, so fail |
| 2455 | * If mddev->level <= 0, then we don't care |
| 2456 | * about aligning sizes (e.g. linear) |
| 2457 | */ |
| 2458 | if (mddev->level > 0) |
| 2459 | return -ENOSPC; |
| 2460 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2461 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | |
| 2464 | /* Verify rdev->desc_nr is unique. |
| 2465 | * If it is -1, assign a free number, else |
| 2466 | * check number is not in use |
| 2467 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2468 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | if (rdev->desc_nr < 0) { |
| 2470 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2471 | if (mddev->pers) |
| 2472 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2473 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | choice++; |
| 2475 | rdev->desc_nr = choice; |
| 2476 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2477 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2478 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2480 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2482 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2483 | if (!test_bit(Journal, &rdev->flags) && |
| 2484 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2485 | pr_warn("md: %s: array is limited to %d devices\n", |
| 2486 | mdname(mddev), mddev->max_disks); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2487 | return -EBUSY; |
| 2488 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2489 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2490 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2491 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | rdev->mddev = mddev; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2493 | pr_debug("md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2494 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 2495 | if (mddev->raid_disks) |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 2496 | mddev_create_serial_pool(mddev, rdev, false); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 2497 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2498 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2499 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2500 | |
Tejun Heo | 0762b8b | 2008-08-25 19:56:12 +0900 | [diff] [blame] | 2501 | ko = &part_to_dev(rdev->bdev->bd_part)->kobj; |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 2502 | /* failure here is OK */ |
| 2503 | err = sysfs_create_link(&rdev->kobj, ko, "block"); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2504 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2505 | rdev->sysfs_unack_badblocks = |
| 2506 | sysfs_get_dirent_safe(rdev->kobj.sd, "unacknowledged_bad_blocks"); |
| 2507 | rdev->sysfs_badblocks = |
| 2508 | sysfs_get_dirent_safe(rdev->kobj.sd, "bad_blocks"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2509 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2510 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2511 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2512 | |
| 2513 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2514 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2517 | |
| 2518 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2519 | pr_warn("md: failed to register dev-%s for %s\n", |
| 2520 | b, mdname(mddev)); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2521 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | } |
| 2523 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2524 | static void rdev_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2525 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2526 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2527 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2528 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2529 | } |
| 2530 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2531 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | { |
| 2533 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2534 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2535 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2536 | list_del_rcu(&rdev->same_set); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2537 | pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 2538 | mddev_destroy_serial_pool(rdev->mddev, rdev, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2540 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2541 | sysfs_put(rdev->sysfs_state); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2542 | sysfs_put(rdev->sysfs_unack_badblocks); |
| 2543 | sysfs_put(rdev->sysfs_badblocks); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2544 | rdev->sysfs_state = NULL; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2545 | rdev->sysfs_unack_badblocks = NULL; |
| 2546 | rdev->sysfs_badblocks = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2547 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2548 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2549 | * writing to 'remove' to "dev/state". We also need |
| 2550 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2551 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2552 | synchronize_rcu(); |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2553 | INIT_WORK(&rdev->del_work, rdev_delayed_delete); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2554 | kobject_get(&rdev->kobj); |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2555 | queue_work(md_rdev_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | } |
| 2557 | |
| 2558 | /* |
| 2559 | * prevent the device from being mounted, repartitioned or |
| 2560 | * otherwise reused by a RAID array (or any other kernel |
| 2561 | * subsystem), by bd_claiming the device. |
| 2562 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2563 | 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] | 2564 | { |
| 2565 | int err = 0; |
| 2566 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2568 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2569 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | if (IS_ERR(bdev)) { |
Christoph Hellwig | ea3edd4 | 2020-03-24 08:25:11 +0100 | [diff] [blame] | 2571 | pr_warn("md: could not open device unknown-block(%u,%u).\n", |
| 2572 | MAJOR(dev), MINOR(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | return PTR_ERR(bdev); |
| 2574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | rdev->bdev = bdev; |
| 2576 | return err; |
| 2577 | } |
| 2578 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2579 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | { |
| 2581 | struct block_device *bdev = rdev->bdev; |
| 2582 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2583 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | void md_autodetect_dev(dev_t dev); |
| 2587 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2588 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | { |
| 2590 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2591 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2592 | pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2593 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2595 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2596 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | #endif |
| 2598 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2599 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | } |
| 2601 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2602 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | { |
| 2604 | unbind_rdev_from_array(rdev); |
| 2605 | export_rdev(rdev); |
| 2606 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2607 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2609 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2610 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2611 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2613 | while (!list_empty(&mddev->disks)) { |
| 2614 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2615 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2616 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | mddev->raid_disks = 0; |
| 2619 | mddev->major_version = 0; |
| 2620 | } |
| 2621 | |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2622 | static bool set_in_sync(struct mddev *mddev) |
| 2623 | { |
Shaohua Li | efa4b77 | 2017-10-18 22:08:13 -0700 | [diff] [blame] | 2624 | lockdep_assert_held(&mddev->lock); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2625 | if (!mddev->in_sync) { |
| 2626 | mddev->sync_checkers++; |
| 2627 | spin_unlock(&mddev->lock); |
| 2628 | percpu_ref_switch_to_atomic_sync(&mddev->writes_pending); |
| 2629 | spin_lock(&mddev->lock); |
| 2630 | if (!mddev->in_sync && |
| 2631 | percpu_ref_is_zero(&mddev->writes_pending)) { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2632 | mddev->in_sync = 1; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2633 | /* |
| 2634 | * Ensure ->in_sync is visible before we clear |
| 2635 | * ->sync_checkers. |
| 2636 | */ |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2637 | smp_mb(); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2638 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2639 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 2640 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2641 | if (--mddev->sync_checkers == 0) |
| 2642 | percpu_ref_switch_to_percpu(&mddev->writes_pending); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2643 | } |
| 2644 | if (mddev->safemode == 1) |
| 2645 | mddev->safemode = 0; |
| 2646 | return mddev->in_sync; |
| 2647 | } |
| 2648 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2649 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2651 | /* Update each superblock (in-memory image), but |
| 2652 | * if we are allowed to, skip spares which already |
| 2653 | * have the right event counter, or have one earlier |
| 2654 | * (which would mean they aren't being marked as dirty |
| 2655 | * with the rest of the array) |
| 2656 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2657 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2658 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2659 | if (rdev->sb_events == mddev->events || |
| 2660 | (nospares && |
| 2661 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2662 | rdev->sb_events+1 == mddev->events)) { |
| 2663 | /* Don't update this superblock */ |
| 2664 | rdev->sb_loaded = 2; |
| 2665 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2666 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2667 | rdev->sb_loaded = 1; |
| 2668 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | } |
| 2670 | } |
| 2671 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2672 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2673 | { |
| 2674 | struct md_rdev *rdev; |
| 2675 | struct mdp_superblock_1 *sb; |
| 2676 | int role; |
| 2677 | |
| 2678 | /* Find a good rdev */ |
| 2679 | rdev_for_each(rdev, mddev) |
| 2680 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2681 | break; |
| 2682 | |
| 2683 | /* No good device found. */ |
| 2684 | if (!rdev) |
| 2685 | return false; |
| 2686 | |
| 2687 | sb = page_address(rdev->sb_page); |
| 2688 | /* Check if a device has become faulty or a spare become active */ |
| 2689 | rdev_for_each(rdev, mddev) { |
| 2690 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2691 | /* Device activated? */ |
| 2692 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2693 | !test_bit(Faulty, &rdev->flags)) |
| 2694 | return true; |
| 2695 | /* Device turned faulty? */ |
| 2696 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2697 | return true; |
| 2698 | } |
| 2699 | |
| 2700 | /* Check if any mddev parameters have changed */ |
| 2701 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2702 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Jason Yan | 1345921 | 2017-03-10 11:49:12 +0800 | [diff] [blame] | 2703 | (mddev->layout != le32_to_cpu(sb->layout)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2704 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2705 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2706 | return true; |
| 2707 | |
| 2708 | return false; |
| 2709 | } |
| 2710 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2711 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2713 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2714 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2715 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2716 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2717 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2719 | if (mddev->ro) { |
| 2720 | if (force_change) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2721 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2722 | return; |
| 2723 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2724 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2725 | repeat: |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2726 | if (mddev_is_clustered(mddev)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2727 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2728 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2729 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2730 | nospares = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2731 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2732 | /* Has someone else has updated the sb */ |
| 2733 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2734 | if (ret == 0) |
| 2735 | md_cluster_ops->metadata_update_cancel(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2736 | bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2737 | BIT(MD_SB_CHANGE_DEVS) | |
| 2738 | BIT(MD_SB_CHANGE_CLEAN)); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2739 | return; |
| 2740 | } |
| 2741 | } |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2742 | |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 2743 | /* |
| 2744 | * First make sure individual recovery_offsets are correct |
| 2745 | * curr_resync_completed can only be used during recovery. |
| 2746 | * During reshape/resync it might use array-addresses rather |
| 2747 | * that device addresses. |
| 2748 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2749 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2750 | if (rdev->raid_disk >= 0 && |
| 2751 | mddev->delta_disks >= 0 && |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 2752 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 2753 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) && |
| 2754 | !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2755 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2756 | !test_bit(In_sync, &rdev->flags) && |
| 2757 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2758 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2759 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2760 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2761 | if (!mddev->persistent) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2762 | clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2763 | clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2764 | if (!mddev->external) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2765 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2766 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2767 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2768 | rdev->badblocks.changed = 0; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2769 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2770 | md_error(mddev, rdev); |
| 2771 | } |
| 2772 | clear_bit(Blocked, &rdev->flags); |
| 2773 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2774 | wake_up(&rdev->blocked_wait); |
| 2775 | } |
| 2776 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2777 | wake_up(&mddev->sb_wait); |
| 2778 | return; |
| 2779 | } |
| 2780 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2781 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2782 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2783 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2784 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2785 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2786 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2787 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2788 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2789 | * though if events isn't the right even/odd, we will have to do |
| 2790 | * spares after all |
| 2791 | */ |
| 2792 | nospares = 1; |
| 2793 | if (force_change) |
| 2794 | nospares = 0; |
| 2795 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2796 | /* If the array is degraded, then skipping spares is both |
| 2797 | * dangerous and fairly pointless. |
| 2798 | * Dangerous because a device that was removed from the array |
| 2799 | * might have a event_count that still looks up-to-date, |
| 2800 | * so it can be re-added without a resync. |
| 2801 | * Pointless because if there are any spares to skip, |
| 2802 | * then a recovery will happen and soon that array won't |
| 2803 | * be degraded any more and the spare can go back to sleep then. |
| 2804 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2805 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2806 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2807 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2808 | |
| 2809 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2810 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2811 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2812 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2813 | && mddev->can_decrease_events |
| 2814 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2815 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2816 | mddev->can_decrease_events = 0; |
| 2817 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2818 | /* otherwise we have to go forward and ... */ |
| 2819 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2820 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2821 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2823 | /* |
| 2824 | * This 64-bit counter should never wrap. |
| 2825 | * Either we are in around ~1 trillion A.C., assuming |
| 2826 | * 1 reboot per second, or we have a bug... |
| 2827 | */ |
| 2828 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2829 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2830 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2831 | if (rdev->badblocks.changed) |
| 2832 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2833 | if (test_bit(Faulty, &rdev->flags)) |
| 2834 | set_bit(FaultRecorded, &rdev->flags); |
| 2835 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2836 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2837 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2838 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2840 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2841 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 2843 | if (mddev->queue) |
| 2844 | blk_add_trace_msg(mddev->queue, "md md_update_sb"); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2845 | rewrite: |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2846 | md_bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2847 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2849 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2850 | if (rdev->sb_loaded != 1) |
| 2851 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2853 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2854 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2855 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2856 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2857 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2858 | bdevname(rdev->bdev, b), |
| 2859 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2860 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2861 | if (rdev->badblocks.size) { |
| 2862 | md_super_write(mddev, rdev, |
| 2863 | rdev->badblocks.sector, |
| 2864 | rdev->badblocks.size << 9, |
| 2865 | rdev->bb_page); |
| 2866 | rdev->badblocks.size = 0; |
| 2867 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2868 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2869 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2870 | pr_debug("md: %s (skipping faulty)\n", |
| 2871 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2872 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2873 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | /* only need to write one superblock... */ |
| 2875 | break; |
| 2876 | } |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2877 | if (md_super_wait(mddev) < 0) |
| 2878 | goto rewrite; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2879 | /* if there was a failure, MD_SB_CHANGE_DEVS was set, and we re-write super */ |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2880 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2881 | if (mddev_is_clustered(mddev) && ret == 0) |
| 2882 | md_cluster_ops->metadata_update_finish(mddev); |
| 2883 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2884 | if (mddev->in_sync != sync_req || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2885 | !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2886 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN))) |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2887 | /* have to write it out again */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2888 | goto repeat; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2889 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2890 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2891 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2892 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2893 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2894 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2895 | clear_bit(Blocked, &rdev->flags); |
| 2896 | |
| 2897 | if (any_badblocks_changed) |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2898 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2899 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2900 | wake_up(&rdev->blocked_wait); |
| 2901 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2903 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2905 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2906 | { |
| 2907 | struct mddev *mddev = rdev->mddev; |
| 2908 | int err = 0; |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2909 | bool add_journal = test_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2910 | |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2911 | if (!mddev->pers->hot_remove_disk || add_journal) { |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2912 | /* If there is hot_add_disk but no hot_remove_disk |
| 2913 | * then added disks for geometry changes, |
| 2914 | * and should be added immediately. |
| 2915 | */ |
| 2916 | super_types[mddev->major_version]. |
| 2917 | validate_super(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2918 | if (add_journal) |
| 2919 | mddev_suspend(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2920 | err = mddev->pers->hot_add_disk(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2921 | if (add_journal) |
| 2922 | mddev_resume(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2923 | if (err) { |
Guoqing Jiang | db76767 | 2016-06-02 23:32:05 -0400 | [diff] [blame] | 2924 | md_kick_rdev_from_array(rdev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2925 | return err; |
| 2926 | } |
| 2927 | } |
| 2928 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2929 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2930 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2931 | if (mddev->degraded) |
| 2932 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2933 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2934 | md_new_event(mddev); |
| 2935 | md_wakeup_thread(mddev->thread); |
| 2936 | return 0; |
| 2937 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2939 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2940 | * We want to accept with case. For this we use cmd_match. |
| 2941 | */ |
| 2942 | static int cmd_match(const char *cmd, const char *str) |
| 2943 | { |
| 2944 | /* See if cmd, written into a sysfs file, matches |
| 2945 | * str. They must either be the same, or cmd can |
| 2946 | * have a trailing newline |
| 2947 | */ |
| 2948 | while (*cmd && *str && *cmd == *str) { |
| 2949 | cmd++; |
| 2950 | str++; |
| 2951 | } |
| 2952 | if (*cmd == '\n') |
| 2953 | cmd++; |
| 2954 | if (*str || *cmd) |
| 2955 | return 0; |
| 2956 | return 1; |
| 2957 | } |
| 2958 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2959 | struct rdev_sysfs_entry { |
| 2960 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2961 | ssize_t (*show)(struct md_rdev *, char *); |
| 2962 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2963 | }; |
| 2964 | |
| 2965 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2966 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2967 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2968 | char *sep = ","; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2969 | size_t len = 0; |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 2970 | unsigned long flags = READ_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2971 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2972 | if (test_bit(Faulty, &flags) || |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 2973 | (!test_bit(ExternalBbl, &flags) && |
| 2974 | rdev->badblocks.unacked_exist)) |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2975 | len += sprintf(page+len, "faulty%s", sep); |
| 2976 | if (test_bit(In_sync, &flags)) |
| 2977 | len += sprintf(page+len, "in_sync%s", sep); |
| 2978 | if (test_bit(Journal, &flags)) |
| 2979 | len += sprintf(page+len, "journal%s", sep); |
| 2980 | if (test_bit(WriteMostly, &flags)) |
| 2981 | len += sprintf(page+len, "write_mostly%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2982 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2983 | (rdev->badblocks.unacked_exist |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2984 | && !test_bit(Faulty, &flags))) |
| 2985 | len += sprintf(page+len, "blocked%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2986 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2987 | !test_bit(Journal, &flags) && |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2988 | !test_bit(In_sync, &flags)) |
| 2989 | len += sprintf(page+len, "spare%s", sep); |
| 2990 | if (test_bit(WriteErrorSeen, &flags)) |
| 2991 | len += sprintf(page+len, "write_error%s", sep); |
| 2992 | if (test_bit(WantReplacement, &flags)) |
| 2993 | len += sprintf(page+len, "want_replacement%s", sep); |
| 2994 | if (test_bit(Replacement, &flags)) |
| 2995 | len += sprintf(page+len, "replacement%s", sep); |
| 2996 | if (test_bit(ExternalBbl, &flags)) |
| 2997 | len += sprintf(page+len, "external_bbl%s", sep); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2998 | if (test_bit(FailFast, &flags)) |
| 2999 | len += sprintf(page+len, "failfast%s", sep); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 3000 | |
| 3001 | if (len) |
| 3002 | len -= strlen(sep); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3003 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3004 | return len+sprintf(page+len, "\n"); |
| 3005 | } |
| 3006 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3007 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3008 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3009 | { |
| 3010 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3011 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3012 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3013 | * writemostly - sets write_mostly |
| 3014 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3015 | * blocked - sets the Blocked flags |
| 3016 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3017 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 3018 | * -insync - clear Insync for a device with a slot assigned, |
| 3019 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3020 | * write_error - sets WriteErrorSeen |
| 3021 | * -write_error - clears WriteErrorSeen |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3022 | * {,-}failfast - set/clear FailFast |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3023 | */ |
| 3024 | int err = -EINVAL; |
| 3025 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 3026 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 3027 | if (test_bit(Faulty, &rdev->flags)) |
| 3028 | err = 0; |
| 3029 | else |
| 3030 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3031 | } else if (cmd_match(buf, "remove")) { |
Shaohua Li | 5d88178 | 2016-07-28 09:06:34 -0700 | [diff] [blame] | 3032 | if (rdev->mddev->pers) { |
| 3033 | clear_bit(Blocked, &rdev->flags); |
| 3034 | remove_and_add_spares(rdev->mddev, rdev); |
| 3035 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3036 | if (rdev->raid_disk >= 0) |
| 3037 | err = -EBUSY; |
| 3038 | else { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3039 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3040 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 3041 | if (mddev_is_clustered(mddev)) |
| 3042 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 3043 | |
| 3044 | if (err == 0) { |
| 3045 | md_kick_rdev_from_array(rdev); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 3046 | if (mddev->pers) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 3047 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 3048 | md_wakeup_thread(mddev->thread); |
| 3049 | } |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 3050 | md_new_event(mddev); |
| 3051 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3052 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3053 | } else if (cmd_match(buf, "writemostly")) { |
| 3054 | set_bit(WriteMostly, &rdev->flags); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 3055 | mddev_create_serial_pool(rdev->mddev, rdev, false); |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3056 | err = 0; |
| 3057 | } else if (cmd_match(buf, "-writemostly")) { |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 3058 | mddev_destroy_serial_pool(rdev->mddev, rdev, false); |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3059 | clear_bit(WriteMostly, &rdev->flags); |
| 3060 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3061 | } else if (cmd_match(buf, "blocked")) { |
| 3062 | set_bit(Blocked, &rdev->flags); |
| 3063 | err = 0; |
| 3064 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3065 | if (!test_bit(Faulty, &rdev->flags) && |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 3066 | !test_bit(ExternalBbl, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 3067 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3068 | /* metadata handler doesn't understand badblocks, |
| 3069 | * so we need to fail the device |
| 3070 | */ |
| 3071 | md_error(rdev->mddev, rdev); |
| 3072 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3073 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3074 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3075 | wake_up(&rdev->blocked_wait); |
| 3076 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3077 | md_wakeup_thread(rdev->mddev->thread); |
| 3078 | |
| 3079 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3080 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 3081 | set_bit(In_sync, &rdev->flags); |
| 3082 | err = 0; |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3083 | } else if (cmd_match(buf, "failfast")) { |
| 3084 | set_bit(FailFast, &rdev->flags); |
| 3085 | err = 0; |
| 3086 | } else if (cmd_match(buf, "-failfast")) { |
| 3087 | clear_bit(FailFast, &rdev->flags); |
| 3088 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3089 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 3090 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 3091 | if (rdev->mddev->pers == NULL) { |
| 3092 | clear_bit(In_sync, &rdev->flags); |
| 3093 | rdev->saved_raid_disk = rdev->raid_disk; |
| 3094 | rdev->raid_disk = -1; |
| 3095 | err = 0; |
| 3096 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3097 | } else if (cmd_match(buf, "write_error")) { |
| 3098 | set_bit(WriteErrorSeen, &rdev->flags); |
| 3099 | err = 0; |
| 3100 | } else if (cmd_match(buf, "-write_error")) { |
| 3101 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 3102 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3103 | } else if (cmd_match(buf, "want_replacement")) { |
| 3104 | /* Any non-spare device that is not a replacement can |
| 3105 | * become want_replacement at any time, but we then need to |
| 3106 | * check if recovery is needed. |
| 3107 | */ |
| 3108 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3109 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3110 | !test_bit(Replacement, &rdev->flags)) |
| 3111 | set_bit(WantReplacement, &rdev->flags); |
| 3112 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3113 | md_wakeup_thread(rdev->mddev->thread); |
| 3114 | err = 0; |
| 3115 | } else if (cmd_match(buf, "-want_replacement")) { |
| 3116 | /* Clearing 'want_replacement' is always allowed. |
| 3117 | * Once replacements starts it is too late though. |
| 3118 | */ |
| 3119 | err = 0; |
| 3120 | clear_bit(WantReplacement, &rdev->flags); |
| 3121 | } else if (cmd_match(buf, "replacement")) { |
| 3122 | /* Can only set a device as a replacement when array has not |
| 3123 | * yet been started. Once running, replacement is automatic |
| 3124 | * from spares, or by assigning 'slot'. |
| 3125 | */ |
| 3126 | if (rdev->mddev->pers) |
| 3127 | err = -EBUSY; |
| 3128 | else { |
| 3129 | set_bit(Replacement, &rdev->flags); |
| 3130 | err = 0; |
| 3131 | } |
| 3132 | } else if (cmd_match(buf, "-replacement")) { |
| 3133 | /* Similarly, can only clear Replacement before start */ |
| 3134 | if (rdev->mddev->pers) |
| 3135 | err = -EBUSY; |
| 3136 | else { |
| 3137 | clear_bit(Replacement, &rdev->flags); |
| 3138 | err = 0; |
| 3139 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 3140 | } else if (cmd_match(buf, "re-add")) { |
Yufen Yu | ee37e62 | 2019-04-02 14:22:14 +0800 | [diff] [blame] | 3141 | if (!rdev->mddev->pers) |
| 3142 | err = -EINVAL; |
| 3143 | else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) && |
| 3144 | rdev->saved_raid_disk >= 0) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 3145 | /* clear_bit is performed _after_ all the devices |
| 3146 | * have their local Faulty bit cleared. If any writes |
| 3147 | * happen in the meantime in the local node, they |
| 3148 | * will land in the local bitmap, which will be synced |
| 3149 | * by this node eventually |
| 3150 | */ |
| 3151 | if (!mddev_is_clustered(rdev->mddev) || |
| 3152 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 3153 | clear_bit(Faulty, &rdev->flags); |
| 3154 | err = add_bound_rdev(rdev); |
| 3155 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 3156 | } else |
| 3157 | err = -EBUSY; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 3158 | } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) { |
| 3159 | set_bit(ExternalBbl, &rdev->flags); |
| 3160 | rdev->badblocks.shift = 0; |
| 3161 | err = 0; |
| 3162 | } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) { |
| 3163 | clear_bit(ExternalBbl, &rdev->flags); |
| 3164 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3165 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3166 | if (!err) |
| 3167 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3168 | return err ? err : len; |
| 3169 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3170 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3171 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3172 | |
| 3173 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3174 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3175 | { |
| 3176 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 3177 | } |
| 3178 | |
| 3179 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3180 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3181 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3182 | unsigned int n; |
| 3183 | int rv; |
| 3184 | |
| 3185 | rv = kstrtouint(buf, 10, &n); |
| 3186 | if (rv < 0) |
| 3187 | return rv; |
| 3188 | atomic_set(&rdev->corrected_errors, n); |
| 3189 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3190 | } |
| 3191 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3192 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3193 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3194 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3195 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3196 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3197 | if (test_bit(Journal, &rdev->flags)) |
| 3198 | return sprintf(page, "journal\n"); |
| 3199 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3200 | return sprintf(page, "none\n"); |
| 3201 | else |
| 3202 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 3203 | } |
| 3204 | |
| 3205 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3206 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3207 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3208 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3209 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3210 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3211 | if (test_bit(Journal, &rdev->flags)) |
| 3212 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3213 | if (strncmp(buf, "none", 4)==0) |
| 3214 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3215 | else { |
| 3216 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 3217 | if (err < 0) |
| 3218 | return err; |
| 3219 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3220 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3221 | /* Setting 'slot' on an active array requires also |
| 3222 | * updating the 'rd%d' link, and communicating |
| 3223 | * with the personality with ->hot_*_disk. |
| 3224 | * For now we only support removing |
| 3225 | * failed/spare devices. This normally happens automatically, |
| 3226 | * but not when the metadata is externally managed. |
| 3227 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3228 | if (rdev->raid_disk == -1) |
| 3229 | return -EEXIST; |
| 3230 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 3231 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3232 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 3233 | clear_bit(Blocked, &rdev->flags); |
| 3234 | remove_and_add_spares(rdev->mddev, rdev); |
| 3235 | if (rdev->raid_disk >= 0) |
| 3236 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3237 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3238 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3239 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3240 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3241 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3242 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 3243 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3244 | |
| 3245 | if (rdev->raid_disk != -1) |
| 3246 | return -EBUSY; |
| 3247 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 3248 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 3249 | return -EBUSY; |
| 3250 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3251 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 3252 | return -EINVAL; |
| 3253 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3254 | if (slot >= rdev->mddev->raid_disks && |
| 3255 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 3256 | return -ENOSPC; |
| 3257 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3258 | rdev->raid_disk = slot; |
| 3259 | if (test_bit(In_sync, &rdev->flags)) |
| 3260 | rdev->saved_raid_disk = slot; |
| 3261 | else |
| 3262 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 3263 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 3264 | clear_bit(Bitmap_sync, &rdev->flags); |
Guoqing Jiang | 3f79cc2 | 2020-04-04 23:57:11 +0200 | [diff] [blame] | 3265 | err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 3266 | if (err) { |
| 3267 | rdev->raid_disk = -1; |
| 3268 | return err; |
| 3269 | } else |
| 3270 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 3271 | /* failure here is OK */; |
| 3272 | sysfs_link_rdev(rdev->mddev, rdev); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3273 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3274 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3275 | if (slot >= rdev->mddev->raid_disks && |
| 3276 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3277 | return -ENOSPC; |
| 3278 | rdev->raid_disk = slot; |
| 3279 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3280 | clear_bit(Faulty, &rdev->flags); |
| 3281 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3282 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3283 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3284 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3285 | return len; |
| 3286 | } |
| 3287 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3288 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3289 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3290 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3291 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3292 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3293 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 3294 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3298 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3299 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3300 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3301 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3302 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 3303 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3304 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3305 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3306 | /* Must set offset before size, so overlap checks |
| 3307 | * can be sane */ |
| 3308 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3309 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3310 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3311 | return len; |
| 3312 | } |
| 3313 | |
| 3314 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3315 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3316 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3317 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 3318 | { |
| 3319 | return sprintf(page, "%llu\n", |
| 3320 | (unsigned long long)rdev->new_data_offset); |
| 3321 | } |
| 3322 | |
| 3323 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 3324 | const char *buf, size_t len) |
| 3325 | { |
| 3326 | unsigned long long new_offset; |
| 3327 | struct mddev *mddev = rdev->mddev; |
| 3328 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3329 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3330 | return -EINVAL; |
| 3331 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3332 | if (mddev->sync_thread || |
| 3333 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3334 | return -EBUSY; |
| 3335 | if (new_offset == rdev->data_offset) |
| 3336 | /* reset is always permitted */ |
| 3337 | ; |
| 3338 | else if (new_offset > rdev->data_offset) { |
| 3339 | /* must not push array size beyond rdev_sectors */ |
| 3340 | if (new_offset - rdev->data_offset |
| 3341 | + mddev->dev_sectors > rdev->sectors) |
| 3342 | return -E2BIG; |
| 3343 | } |
| 3344 | /* Metadata worries about other space details. */ |
| 3345 | |
| 3346 | /* decreasing the offset is inconsistent with a backwards |
| 3347 | * reshape. |
| 3348 | */ |
| 3349 | if (new_offset < rdev->data_offset && |
| 3350 | mddev->reshape_backwards) |
| 3351 | return -EINVAL; |
| 3352 | /* Increasing offset is inconsistent with forwards |
| 3353 | * reshape. reshape_direction should be set to |
| 3354 | * 'backwards' first. |
| 3355 | */ |
| 3356 | if (new_offset > rdev->data_offset && |
| 3357 | !mddev->reshape_backwards) |
| 3358 | return -EINVAL; |
| 3359 | |
| 3360 | if (mddev->pers && mddev->persistent && |
| 3361 | !super_types[mddev->major_version] |
| 3362 | .allow_new_offset(rdev, new_offset)) |
| 3363 | return -E2BIG; |
| 3364 | rdev->new_data_offset = new_offset; |
| 3365 | if (new_offset > rdev->data_offset) |
| 3366 | mddev->reshape_backwards = 1; |
| 3367 | else if (new_offset < rdev->data_offset) |
| 3368 | mddev->reshape_backwards = 0; |
| 3369 | |
| 3370 | return len; |
| 3371 | } |
| 3372 | static struct rdev_sysfs_entry rdev_new_offset = |
| 3373 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 3374 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3375 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3376 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3377 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3378 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3379 | } |
| 3380 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3381 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 3382 | { |
| 3383 | /* check if two start/length pairs overlap */ |
| 3384 | if (s1+l1 <= s2) |
| 3385 | return 0; |
| 3386 | if (s2+l2 <= s1) |
| 3387 | return 0; |
| 3388 | return 1; |
| 3389 | } |
| 3390 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3391 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 3392 | { |
| 3393 | unsigned long long blocks; |
| 3394 | sector_t new; |
| 3395 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3396 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3397 | return -EINVAL; |
| 3398 | |
| 3399 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 3400 | return -EINVAL; /* sector conversion overflow */ |
| 3401 | |
| 3402 | new = blocks * 2; |
| 3403 | if (new != blocks * 2) |
| 3404 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 3405 | |
| 3406 | *sectors = new; |
| 3407 | return 0; |
| 3408 | } |
| 3409 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3410 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3411 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3412 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3413 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3414 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3415 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3416 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3417 | if (test_bit(Journal, &rdev->flags)) |
| 3418 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3419 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3420 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3421 | if (rdev->data_offset != rdev->new_data_offset) |
| 3422 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3423 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3424 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3425 | sectors = super_types[my_mddev->major_version]. |
| 3426 | rdev_size_change(rdev, sectors); |
| 3427 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3428 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3429 | } else if (!sectors) |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3430 | sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3431 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 3432 | if (!my_mddev->pers->resize) |
| 3433 | /* Cannot change size for RAID0 or Linear etc */ |
| 3434 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3435 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3436 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3437 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3438 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3439 | rdev->sectors = sectors; |
| 3440 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3441 | /* Need to check that all other rdevs with the same |
| 3442 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 3443 | * the rdev lists safely. |
| 3444 | * This check does not provide a hard guarantee, it |
| 3445 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3446 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3447 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3448 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3449 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3450 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3451 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3452 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3453 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3454 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3455 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3456 | if (rdev->bdev == rdev2->bdev && |
| 3457 | rdev != rdev2 && |
| 3458 | overlaps(rdev->data_offset, rdev->sectors, |
| 3459 | rdev2->data_offset, |
| 3460 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3461 | overlap = 1; |
| 3462 | break; |
| 3463 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3464 | if (overlap) { |
| 3465 | mddev_put(mddev); |
| 3466 | break; |
| 3467 | } |
| 3468 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3469 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3470 | if (overlap) { |
| 3471 | /* Someone else could have slipped in a size |
| 3472 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3473 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3474 | * safe, and trust userspace not to race with |
| 3475 | * itself |
| 3476 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3477 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3478 | return -EBUSY; |
| 3479 | } |
| 3480 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3481 | return len; |
| 3482 | } |
| 3483 | |
| 3484 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3485 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3486 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3487 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3488 | { |
| 3489 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3490 | |
| 3491 | if (test_bit(In_sync, &rdev->flags) || |
| 3492 | recovery_start == MaxSector) |
| 3493 | return sprintf(page, "none\n"); |
| 3494 | |
| 3495 | return sprintf(page, "%llu\n", recovery_start); |
| 3496 | } |
| 3497 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3498 | 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] | 3499 | { |
| 3500 | unsigned long long recovery_start; |
| 3501 | |
| 3502 | if (cmd_match(buf, "none")) |
| 3503 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3504 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3505 | return -EINVAL; |
| 3506 | |
| 3507 | if (rdev->mddev->pers && |
| 3508 | rdev->raid_disk >= 0) |
| 3509 | return -EBUSY; |
| 3510 | |
| 3511 | rdev->recovery_offset = recovery_start; |
| 3512 | if (recovery_start == MaxSector) |
| 3513 | set_bit(In_sync, &rdev->flags); |
| 3514 | else |
| 3515 | clear_bit(In_sync, &rdev->flags); |
| 3516 | return len; |
| 3517 | } |
| 3518 | |
| 3519 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3520 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3521 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3522 | /* sysfs access to bad-blocks list. |
| 3523 | * We present two files. |
| 3524 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 3525 | * are recorded as bad. The list is truncated to fit within |
| 3526 | * the one-page limit of sysfs. |
| 3527 | * Writing "sector length" to this file adds an acknowledged |
| 3528 | * bad block list. |
| 3529 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 3530 | * been acknowledged. Writing to this file adds bad blocks |
| 3531 | * without acknowledging them. This is largely for testing. |
| 3532 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3533 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3534 | { |
| 3535 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3536 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3537 | 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] | 3538 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3539 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3540 | /* Maybe that ack was all we needed */ |
| 3541 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3542 | wake_up(&rdev->blocked_wait); |
| 3543 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3544 | } |
| 3545 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3546 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3547 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3548 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3549 | { |
| 3550 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3551 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3552 | 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] | 3553 | { |
| 3554 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3555 | } |
| 3556 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3557 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3558 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3559 | static ssize_t |
| 3560 | ppl_sector_show(struct md_rdev *rdev, char *page) |
| 3561 | { |
| 3562 | return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector); |
| 3563 | } |
| 3564 | |
| 3565 | static ssize_t |
| 3566 | ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3567 | { |
| 3568 | unsigned long long sector; |
| 3569 | |
| 3570 | if (kstrtoull(buf, 10, §or) < 0) |
| 3571 | return -EINVAL; |
| 3572 | if (sector != (sector_t)sector) |
| 3573 | return -EINVAL; |
| 3574 | |
| 3575 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3576 | rdev->raid_disk >= 0) |
| 3577 | return -EBUSY; |
| 3578 | |
| 3579 | if (rdev->mddev->persistent) { |
| 3580 | if (rdev->mddev->major_version == 0) |
| 3581 | return -EINVAL; |
| 3582 | if ((sector > rdev->sb_start && |
| 3583 | sector - rdev->sb_start > S16_MAX) || |
| 3584 | (sector < rdev->sb_start && |
| 3585 | rdev->sb_start - sector > -S16_MIN)) |
| 3586 | return -EINVAL; |
| 3587 | rdev->ppl.offset = sector - rdev->sb_start; |
| 3588 | } else if (!rdev->mddev->external) { |
| 3589 | return -EBUSY; |
| 3590 | } |
| 3591 | rdev->ppl.sector = sector; |
| 3592 | return len; |
| 3593 | } |
| 3594 | |
| 3595 | static struct rdev_sysfs_entry rdev_ppl_sector = |
| 3596 | __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store); |
| 3597 | |
| 3598 | static ssize_t |
| 3599 | ppl_size_show(struct md_rdev *rdev, char *page) |
| 3600 | { |
| 3601 | return sprintf(page, "%u\n", rdev->ppl.size); |
| 3602 | } |
| 3603 | |
| 3604 | static ssize_t |
| 3605 | ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3606 | { |
| 3607 | unsigned int size; |
| 3608 | |
| 3609 | if (kstrtouint(buf, 10, &size) < 0) |
| 3610 | return -EINVAL; |
| 3611 | |
| 3612 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3613 | rdev->raid_disk >= 0) |
| 3614 | return -EBUSY; |
| 3615 | |
| 3616 | if (rdev->mddev->persistent) { |
| 3617 | if (rdev->mddev->major_version == 0) |
| 3618 | return -EINVAL; |
| 3619 | if (size > U16_MAX) |
| 3620 | return -EINVAL; |
| 3621 | } else if (!rdev->mddev->external) { |
| 3622 | return -EBUSY; |
| 3623 | } |
| 3624 | rdev->ppl.size = size; |
| 3625 | return len; |
| 3626 | } |
| 3627 | |
| 3628 | static struct rdev_sysfs_entry rdev_ppl_size = |
| 3629 | __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store); |
| 3630 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3631 | static struct attribute *rdev_default_attrs[] = { |
| 3632 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3633 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3634 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3635 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3636 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3637 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3638 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3639 | &rdev_bad_blocks.attr, |
| 3640 | &rdev_unack_bad_blocks.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3641 | &rdev_ppl_sector.attr, |
| 3642 | &rdev_ppl_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3643 | NULL, |
| 3644 | }; |
| 3645 | static ssize_t |
| 3646 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3647 | { |
| 3648 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3649 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3650 | |
| 3651 | if (!entry->show) |
| 3652 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3653 | if (!rdev->mddev) |
Marcos Paulo de Souza | 168b305 | 2019-06-14 15:41:06 -0700 | [diff] [blame] | 3654 | return -ENODEV; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3655 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | static ssize_t |
| 3659 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3660 | const char *page, size_t length) |
| 3661 | { |
| 3662 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3663 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3664 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3665 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3666 | |
| 3667 | if (!entry->store) |
| 3668 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3669 | if (!capable(CAP_SYS_ADMIN)) |
| 3670 | return -EACCES; |
Pawel Baldysiak | c42d324 | 2019-03-27 13:48:21 +0100 | [diff] [blame] | 3671 | rv = mddev ? mddev_lock(mddev) : -ENODEV; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3672 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3673 | if (rdev->mddev == NULL) |
Pawel Baldysiak | c42d324 | 2019-03-27 13:48:21 +0100 | [diff] [blame] | 3674 | rv = -ENODEV; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3675 | else |
| 3676 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3677 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3678 | } |
| 3679 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3680 | } |
| 3681 | |
| 3682 | static void rdev_free(struct kobject *ko) |
| 3683 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3684 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3685 | kfree(rdev); |
| 3686 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3687 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3688 | .show = rdev_attr_show, |
| 3689 | .store = rdev_attr_store, |
| 3690 | }; |
| 3691 | static struct kobj_type rdev_ktype = { |
| 3692 | .release = rdev_free, |
| 3693 | .sysfs_ops = &rdev_sysfs_ops, |
| 3694 | .default_attrs = rdev_default_attrs, |
| 3695 | }; |
| 3696 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3697 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3698 | { |
| 3699 | rdev->desc_nr = -1; |
| 3700 | rdev->saved_raid_disk = -1; |
| 3701 | rdev->raid_disk = -1; |
| 3702 | rdev->flags = 0; |
| 3703 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3704 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3705 | rdev->sb_events = 0; |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 3706 | rdev->last_read_error = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3707 | rdev->sb_loaded = 0; |
| 3708 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3709 | atomic_set(&rdev->nr_pending, 0); |
| 3710 | atomic_set(&rdev->read_errors, 0); |
| 3711 | atomic_set(&rdev->corrected_errors, 0); |
| 3712 | |
| 3713 | INIT_LIST_HEAD(&rdev->same_set); |
| 3714 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3715 | |
| 3716 | /* Add space to store bad block list. |
| 3717 | * This reserves the space even on arrays where it cannot |
| 3718 | * be used - I wonder if that matters |
| 3719 | */ |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3720 | return badblocks_init(&rdev->badblocks, 0); |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3721 | } |
| 3722 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3723 | /* |
| 3724 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3725 | * |
| 3726 | * mark the device faulty if: |
| 3727 | * |
| 3728 | * - the device is nonexistent (zero size) |
| 3729 | * - the device has no valid superblock |
| 3730 | * |
| 3731 | * a faulty rdev _never_ has rdev->sb set. |
| 3732 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3733 | 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] | 3734 | { |
| 3735 | char b[BDEVNAME_SIZE]; |
| 3736 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3737 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3738 | sector_t size; |
| 3739 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3740 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3741 | if (!rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3744 | err = md_rdev_init(rdev); |
| 3745 | if (err) |
| 3746 | goto abort_free; |
| 3747 | err = alloc_disk_sb(rdev); |
| 3748 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3749 | goto abort_free; |
| 3750 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3751 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3752 | if (err) |
| 3753 | goto abort_free; |
| 3754 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3755 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3756 | |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 3757 | size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3758 | if (!size) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3759 | pr_warn("md: %s has zero or unknown size, marking faulty!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | bdevname(rdev->bdev,b)); |
| 3761 | err = -EINVAL; |
| 3762 | goto abort_free; |
| 3763 | } |
| 3764 | |
| 3765 | if (super_format >= 0) { |
| 3766 | err = super_types[super_format]. |
| 3767 | load_super(rdev, NULL, super_minor); |
| 3768 | if (err == -EINVAL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3769 | pr_warn("md: %s does not have a valid v%d.%d superblock, not importing!\n", |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 3770 | bdevname(rdev->bdev,b), |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3771 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | goto abort_free; |
| 3773 | } |
| 3774 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3775 | pr_warn("md: could not read %s's sb, not importing!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | bdevname(rdev->bdev,b)); |
| 3777 | goto abort_free; |
| 3778 | } |
| 3779 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | return rdev; |
| 3782 | |
| 3783 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3784 | if (rdev->bdev) |
| 3785 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3786 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | kfree(rdev); |
| 3788 | return ERR_PTR(err); |
| 3789 | } |
| 3790 | |
| 3791 | /* |
| 3792 | * Check a full RAID array for plausibility |
| 3793 | */ |
| 3794 | |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3795 | static int analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | { |
| 3797 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3798 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | char b[BDEVNAME_SIZE]; |
| 3800 | |
| 3801 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3802 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | switch (super_types[mddev->major_version]. |
| 3804 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3805 | case 1: |
| 3806 | freshest = rdev; |
| 3807 | break; |
| 3808 | case 0: |
| 3809 | break; |
| 3810 | default: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3811 | pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3813 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | } |
| 3815 | |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3816 | /* Cannot find a valid fresh disk */ |
| 3817 | if (!freshest) { |
| 3818 | pr_warn("md: cannot find a valid disk\n"); |
| 3819 | return -EINVAL; |
| 3820 | } |
| 3821 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3822 | super_types[mddev->major_version]. |
| 3823 | validate_super(mddev, freshest); |
| 3824 | |
| 3825 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3826 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3827 | if (mddev->max_disks && |
| 3828 | (rdev->desc_nr >= mddev->max_disks || |
| 3829 | i > mddev->max_disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3830 | pr_warn("md: %s: %s: only %d devices permitted\n", |
| 3831 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3832 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3833 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3834 | continue; |
| 3835 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3836 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | if (super_types[mddev->major_version]. |
| 3838 | validate_super(mddev, rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3839 | pr_warn("md: kicking non-fresh %s from array!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3840 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3841 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | continue; |
| 3843 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3844 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3845 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3846 | rdev->desc_nr = i++; |
| 3847 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3848 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3849 | } else if (rdev->raid_disk >= |
| 3850 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3851 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3852 | rdev->raid_disk = -1; |
| 3853 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | } |
| 3855 | } |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3856 | |
| 3857 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | } |
| 3859 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3860 | /* Read a fixed-point number. |
| 3861 | * Numbers in sysfs attributes should be in "standard" units where |
| 3862 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3863 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3864 | * milliseconds or jiffies. |
| 3865 | * This function takes a decimal number with a possible fractional |
| 3866 | * component, and produces an integer which is the result of |
| 3867 | * multiplying that number by 10^'scale'. |
| 3868 | * all without any floating-point arithmetic. |
| 3869 | */ |
| 3870 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3871 | { |
| 3872 | unsigned long result = 0; |
| 3873 | long decimals = -1; |
| 3874 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3875 | if (*cp == '.') |
| 3876 | decimals = 0; |
| 3877 | else if (decimals < scale) { |
| 3878 | unsigned int value; |
| 3879 | value = *cp - '0'; |
| 3880 | result = result * 10 + value; |
| 3881 | if (decimals >= 0) |
| 3882 | decimals++; |
| 3883 | } |
| 3884 | cp++; |
| 3885 | } |
| 3886 | if (*cp == '\n') |
| 3887 | cp++; |
| 3888 | if (*cp) |
| 3889 | return -EINVAL; |
| 3890 | if (decimals < 0) |
| 3891 | decimals = 0; |
Andy Shevchenko | cf89160 | 2019-07-23 23:41:55 +0300 | [diff] [blame] | 3892 | *res = result * int_pow(10, scale - decimals); |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3893 | return 0; |
| 3894 | } |
| 3895 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3896 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3897 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3898 | { |
| 3899 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3900 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3901 | } |
| 3902 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3903 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3904 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3905 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3906 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3907 | if (mddev_is_clustered(mddev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3908 | pr_warn("md: Safemode is disabled for clustered mode\n"); |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3909 | return -EINVAL; |
| 3910 | } |
| 3911 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3912 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3913 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3914 | if (msec == 0) |
| 3915 | mddev->safemode_delay = 0; |
| 3916 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3917 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3918 | unsigned long new_delay = (msec*HZ)/1000; |
| 3919 | |
| 3920 | if (new_delay == 0) |
| 3921 | new_delay = 1; |
| 3922 | mddev->safemode_delay = new_delay; |
| 3923 | if (new_delay < old_delay || old_delay == 0) |
| 3924 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3925 | } |
| 3926 | return len; |
| 3927 | } |
| 3928 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3929 | __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] | 3930 | |
| 3931 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3932 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3933 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3934 | struct md_personality *p; |
| 3935 | int ret; |
| 3936 | spin_lock(&mddev->lock); |
| 3937 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3938 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3939 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3940 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3941 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3942 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3943 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3944 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3945 | ret = 0; |
| 3946 | spin_unlock(&mddev->lock); |
| 3947 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3948 | } |
| 3949 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3950 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3951 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3952 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3953 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3954 | ssize_t rv; |
| 3955 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3956 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3957 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3958 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3959 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3960 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3961 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3962 | return -EINVAL; |
| 3963 | |
| 3964 | rv = mddev_lock(mddev); |
| 3965 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3966 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3967 | |
| 3968 | if (mddev->pers == NULL) { |
| 3969 | strncpy(mddev->clevel, buf, slen); |
| 3970 | if (mddev->clevel[slen-1] == '\n') |
| 3971 | slen--; |
| 3972 | mddev->clevel[slen] = 0; |
| 3973 | mddev->level = LEVEL_NONE; |
| 3974 | rv = len; |
| 3975 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3976 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3977 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3978 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3979 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3980 | |
| 3981 | /* request to change the personality. Need to ensure: |
| 3982 | * - array is not engaged in resync/recovery/reshape |
| 3983 | * - old personality can be suspended |
| 3984 | * - new personality will access other array. |
| 3985 | */ |
| 3986 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3987 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3988 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3989 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3990 | mddev->reshape_position != MaxSector || |
| 3991 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3992 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3993 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3994 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3995 | if (!mddev->pers->quiesce) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3996 | pr_warn("md: %s: %s does not support online personality change\n", |
| 3997 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3998 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4002 | strncpy(clevel, buf, slen); |
| 4003 | if (clevel[slen-1] == '\n') |
| 4004 | slen--; |
| 4005 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4006 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 4007 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4008 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 4009 | if (request_module("md-%s", clevel) != 0) |
| 4010 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4011 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 4012 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4013 | if (!pers || !try_module_get(pers->owner)) { |
| 4014 | spin_unlock(&pers_lock); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4015 | pr_warn("md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4016 | rv = -EINVAL; |
| 4017 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4018 | } |
| 4019 | spin_unlock(&pers_lock); |
| 4020 | |
| 4021 | if (pers == mddev->pers) { |
| 4022 | /* Nothing to do! */ |
| 4023 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4024 | rv = len; |
| 4025 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4026 | } |
| 4027 | if (!pers->takeover) { |
| 4028 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4029 | pr_warn("md: %s: %s does not support personality takeover\n", |
| 4030 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4031 | rv = -EINVAL; |
| 4032 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4033 | } |
| 4034 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4035 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4036 | rdev->new_raid_disk = rdev->raid_disk; |
| 4037 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4038 | /* ->takeover must set new_* and/or delta_disks |
| 4039 | * if it succeeds, and may set them when it fails. |
| 4040 | */ |
| 4041 | priv = pers->takeover(mddev); |
| 4042 | if (IS_ERR(priv)) { |
| 4043 | mddev->new_level = mddev->level; |
| 4044 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4045 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4046 | mddev->raid_disks -= mddev->delta_disks; |
| 4047 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4048 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4049 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4050 | pr_warn("md: %s: %s would not accept array\n", |
| 4051 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4052 | rv = PTR_ERR(priv); |
| 4053 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | /* Looks like we have a winner */ |
| 4057 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 4058 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4059 | |
| 4060 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4061 | oldpers = mddev->pers; |
| 4062 | oldpriv = mddev->private; |
| 4063 | mddev->pers = pers; |
| 4064 | mddev->private = priv; |
| 4065 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 4066 | mddev->level = mddev->new_level; |
| 4067 | mddev->layout = mddev->new_layout; |
| 4068 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 4069 | mddev->delta_disks = 0; |
| 4070 | mddev->reshape_backwards = 0; |
| 4071 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4072 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4073 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4074 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 4075 | mddev->external) { |
| 4076 | /* We are converting from a no-redundancy array |
| 4077 | * to a redundancy array and metadata is managed |
| 4078 | * externally so we need to be sure that writes |
| 4079 | * won't block due to a need to transition |
| 4080 | * clean->dirty |
| 4081 | * until external management is started. |
| 4082 | */ |
| 4083 | mddev->in_sync = 0; |
| 4084 | mddev->safemode_delay = 0; |
| 4085 | mddev->safemode = 0; |
| 4086 | } |
| 4087 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4088 | oldpers->free(mddev, oldpriv); |
| 4089 | |
| 4090 | if (oldpers->sync_request == NULL && |
| 4091 | pers->sync_request != NULL) { |
| 4092 | /* need to add the md_redundancy_group */ |
| 4093 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4094 | pr_warn("md: cannot register extra attributes for %s\n", |
| 4095 | mdname(mddev)); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4096 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
| 4097 | } |
| 4098 | if (oldpers->sync_request != NULL && |
| 4099 | pers->sync_request == NULL) { |
| 4100 | /* need to remove the md_redundancy_group */ |
| 4101 | if (mddev->to_remove == NULL) |
| 4102 | mddev->to_remove = &md_redundancy_group; |
| 4103 | } |
| 4104 | |
Alexey Obitotskiy | 4cb9da7 | 2016-06-23 12:11:01 +0200 | [diff] [blame] | 4105 | module_put(oldpers->owner); |
| 4106 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4107 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4108 | if (rdev->raid_disk < 0) |
| 4109 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4110 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4111 | rdev->new_raid_disk = -1; |
| 4112 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 4113 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4114 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4115 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4116 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4117 | if (rdev->raid_disk < 0) |
| 4118 | continue; |
| 4119 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 4120 | continue; |
| 4121 | rdev->raid_disk = rdev->new_raid_disk; |
| 4122 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 4123 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4124 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4125 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4126 | pr_warn("md: cannot register rd%d for %s after level change\n", |
| 4127 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 4128 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4129 | } |
| 4130 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4131 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 4132 | /* this is now an array without redundancy, so |
| 4133 | * it must always be in_sync |
| 4134 | */ |
| 4135 | mddev->in_sync = 1; |
| 4136 | del_timer_sync(&mddev->safemode_timer); |
| 4137 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 4138 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4139 | pers->run(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4140 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 4141 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 4142 | if (!mddev->thread) |
| 4143 | md_update_sb(mddev, 1); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 4144 | sysfs_notify_dirent_safe(mddev->sysfs_level); |
Dan Williams | bb7f8d2 | 2010-05-01 18:14:57 -0700 | [diff] [blame] | 4145 | md_new_event(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4146 | rv = len; |
| 4147 | out_unlock: |
| 4148 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4149 | return rv; |
| 4150 | } |
| 4151 | |
| 4152 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4153 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4154 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4155 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4156 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4157 | { |
| 4158 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4159 | if (mddev->reshape_position != MaxSector && |
| 4160 | mddev->layout != mddev->new_layout) |
| 4161 | return sprintf(page, "%d (%d)\n", |
| 4162 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4163 | return sprintf(page, "%d\n", mddev->layout); |
| 4164 | } |
| 4165 | |
| 4166 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4167 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4168 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4169 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4170 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4171 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4172 | err = kstrtouint(buf, 10, &n); |
| 4173 | if (err < 0) |
| 4174 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4175 | err = mddev_lock(mddev); |
| 4176 | if (err) |
| 4177 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4178 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4179 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 4180 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4181 | err = -EBUSY; |
| 4182 | else if (mddev->ro) |
| 4183 | err = -EROFS; |
| 4184 | else { |
| 4185 | mddev->new_layout = n; |
| 4186 | err = mddev->pers->check_reshape(mddev); |
| 4187 | if (err) |
| 4188 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 4189 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4190 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4191 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4192 | if (mddev->reshape_position == MaxSector) |
| 4193 | mddev->layout = n; |
| 4194 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4195 | mddev_unlock(mddev); |
| 4196 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4197 | } |
| 4198 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4199 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4200 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4201 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4202 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4203 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 4204 | if (mddev->raid_disks == 0) |
| 4205 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4206 | if (mddev->reshape_position != MaxSector && |
| 4207 | mddev->delta_disks != 0) |
| 4208 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 4209 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4210 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 4211 | } |
| 4212 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4213 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4214 | |
| 4215 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4216 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4217 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4218 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4219 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4220 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4221 | err = kstrtouint(buf, 10, &n); |
| 4222 | if (err < 0) |
| 4223 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4224 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4225 | err = mddev_lock(mddev); |
| 4226 | if (err) |
| 4227 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4228 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4229 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4230 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4231 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4232 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4233 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4234 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4235 | rdev_for_each(rdev, mddev) { |
| 4236 | if (olddisks < n && |
| 4237 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4238 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4239 | if (olddisks > n && |
| 4240 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4241 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4242 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4243 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4244 | mddev->delta_disks = n - olddisks; |
| 4245 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4246 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4247 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4248 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4249 | out_unlock: |
| 4250 | mddev_unlock(mddev); |
| 4251 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4252 | } |
| 4253 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4254 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4255 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4256 | static ssize_t |
Sebastian Parschauer | ec164d07 | 2020-07-28 12:01:39 +0200 | [diff] [blame] | 4257 | uuid_show(struct mddev *mddev, char *page) |
| 4258 | { |
| 4259 | return sprintf(page, "%pU\n", mddev->uuid); |
| 4260 | } |
| 4261 | static struct md_sysfs_entry md_uuid = |
| 4262 | __ATTR(uuid, S_IRUGO, uuid_show, NULL); |
| 4263 | |
| 4264 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4265 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4266 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4267 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4268 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 4269 | return sprintf(page, "%d (%d)\n", |
| 4270 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4271 | mddev->chunk_sectors << 9); |
| 4272 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4273 | } |
| 4274 | |
| 4275 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4276 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4277 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4278 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4279 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4280 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4281 | err = kstrtoul(buf, 10, &n); |
| 4282 | if (err < 0) |
| 4283 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4284 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4285 | err = mddev_lock(mddev); |
| 4286 | if (err) |
| 4287 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4288 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 4289 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4290 | err = -EBUSY; |
| 4291 | else if (mddev->ro) |
| 4292 | err = -EROFS; |
| 4293 | else { |
| 4294 | mddev->new_chunk_sectors = n >> 9; |
| 4295 | err = mddev->pers->check_reshape(mddev); |
| 4296 | if (err) |
| 4297 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 4298 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4299 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4300 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4301 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4302 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4303 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4304 | mddev_unlock(mddev); |
| 4305 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4306 | } |
| 4307 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4308 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4309 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4310 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4311 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4312 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4313 | if (mddev->recovery_cp == MaxSector) |
| 4314 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4315 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 4316 | } |
| 4317 | |
| 4318 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4319 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4320 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4321 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4322 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4323 | |
| 4324 | if (cmd_match(buf, "none")) |
| 4325 | n = MaxSector; |
| 4326 | else { |
| 4327 | err = kstrtoull(buf, 10, &n); |
| 4328 | if (err < 0) |
| 4329 | return err; |
| 4330 | if (n != (sector_t)n) |
| 4331 | return -EINVAL; |
| 4332 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4333 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4334 | err = mddev_lock(mddev); |
| 4335 | if (err) |
| 4336 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 4337 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4338 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4339 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4340 | if (!err) { |
| 4341 | mddev->recovery_cp = n; |
| 4342 | if (mddev->pers) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4343 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4344 | } |
| 4345 | mddev_unlock(mddev); |
| 4346 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4347 | } |
| 4348 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4349 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 4350 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4351 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4352 | /* |
| 4353 | * The array state can be: |
| 4354 | * |
| 4355 | * clear |
| 4356 | * No devices, no size, no level |
| 4357 | * Equivalent to STOP_ARRAY ioctl |
| 4358 | * inactive |
| 4359 | * May have some settings, but array is not active |
| 4360 | * all IO results in error |
| 4361 | * When written, doesn't tear down array, but just stops it |
| 4362 | * suspended (not supported yet) |
| 4363 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 4364 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4365 | * readonly |
| 4366 | * no resync can happen. no superblocks get written. |
| 4367 | * write requests fail |
| 4368 | * read-auto |
| 4369 | * like readonly, but behaves like 'clean' on a write request. |
| 4370 | * |
| 4371 | * clean - no pending writes, but otherwise active. |
| 4372 | * When written to inactive array, starts without resync |
| 4373 | * If a write request arrives then |
| 4374 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 4375 | * if not known, block and switch to write-pending |
| 4376 | * If written to an active array that has pending writes, then fails. |
| 4377 | * active |
| 4378 | * fully active: IO and resync can be happening. |
| 4379 | * When written to inactive array, starts with resync |
| 4380 | * |
| 4381 | * write-pending |
| 4382 | * clean, but writes are blocked waiting for 'active' to be written. |
| 4383 | * |
| 4384 | * active-idle |
| 4385 | * like active, but no writes have been seen for a while (100msec). |
| 4386 | * |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4387 | * broken |
| 4388 | * RAID0/LINEAR-only: same as clean, but array is missing a member. |
| 4389 | * It's useful because RAID0/LINEAR mounted-arrays aren't stopped |
| 4390 | * when a member is gone, so this state will at least alert the |
| 4391 | * user that something is wrong. |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4392 | */ |
| 4393 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4394 | write_pending, active_idle, broken, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 4395 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4396 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4397 | "write-pending", "active-idle", "broken", NULL }; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4398 | |
| 4399 | static int match_word(const char *word, char **list) |
| 4400 | { |
| 4401 | int n; |
| 4402 | for (n=0; list[n]; n++) |
| 4403 | if (cmd_match(word, list[n])) |
| 4404 | break; |
| 4405 | return n; |
| 4406 | } |
| 4407 | |
| 4408 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4409 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4410 | { |
| 4411 | enum array_state st = inactive; |
| 4412 | |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4413 | if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4414 | switch(mddev->ro) { |
| 4415 | case 1: |
| 4416 | st = readonly; |
| 4417 | break; |
| 4418 | case 2: |
| 4419 | st = read_auto; |
| 4420 | break; |
| 4421 | case 0: |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4422 | spin_lock(&mddev->lock); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4423 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4424 | st = write_pending; |
Tomasz Majchrzak | 16f8894 | 2016-10-24 12:47:28 +0200 | [diff] [blame] | 4425 | else if (mddev->in_sync) |
| 4426 | st = clean; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4427 | else if (mddev->safemode) |
| 4428 | st = active_idle; |
| 4429 | else |
| 4430 | st = active; |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4431 | spin_unlock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4432 | } |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4433 | |
| 4434 | if (test_bit(MD_BROKEN, &mddev->flags) && st == clean) |
| 4435 | st = broken; |
| 4436 | } else { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4437 | if (list_empty(&mddev->disks) && |
| 4438 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4439 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4440 | st = clear; |
| 4441 | else |
| 4442 | st = inactive; |
| 4443 | } |
| 4444 | return sprintf(page, "%s\n", array_states[st]); |
| 4445 | } |
| 4446 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4447 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 4448 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
| 4449 | static int do_md_run(struct mddev *mddev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4450 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4451 | |
| 4452 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4453 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4454 | { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4455 | int err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4456 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4457 | |
| 4458 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 4459 | /* don't take reconfig_mutex when toggling between |
| 4460 | * clean and active |
| 4461 | */ |
| 4462 | spin_lock(&mddev->lock); |
| 4463 | if (st == active) { |
| 4464 | restart_array(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4465 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
Tomasz Majchrzak | 91a6c4a | 2016-10-25 17:07:08 +0200 | [diff] [blame] | 4466 | md_wakeup_thread(mddev->thread); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4467 | wake_up(&mddev->sb_wait); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4468 | } else /* st == clean */ { |
| 4469 | restart_array(mddev); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4470 | if (!set_in_sync(mddev)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4471 | err = -EBUSY; |
| 4472 | } |
Tomasz Majchrzak | 573275b | 2016-06-30 10:47:09 +0200 | [diff] [blame] | 4473 | if (!err) |
| 4474 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4475 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 4476 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4477 | } |
| 4478 | err = mddev_lock(mddev); |
| 4479 | if (err) |
| 4480 | return err; |
| 4481 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4482 | switch(st) { |
| 4483 | case bad_word: |
| 4484 | break; |
| 4485 | case clear: |
| 4486 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4487 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4488 | break; |
| 4489 | case inactive: |
| 4490 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4491 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4492 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4493 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4494 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4495 | break; |
| 4496 | case suspended: |
| 4497 | break; /* not supported yet */ |
| 4498 | case readonly: |
| 4499 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4500 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4501 | else { |
| 4502 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4503 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4504 | err = do_md_run(mddev); |
| 4505 | } |
| 4506 | break; |
| 4507 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4508 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4509 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4510 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4511 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4512 | err = restart_array(mddev); |
| 4513 | if (err == 0) { |
| 4514 | mddev->ro = 2; |
| 4515 | set_disk_ro(mddev->gendisk, 0); |
| 4516 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4517 | } else { |
| 4518 | mddev->ro = 2; |
| 4519 | err = do_md_run(mddev); |
| 4520 | } |
| 4521 | break; |
| 4522 | case clean: |
| 4523 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4524 | err = restart_array(mddev); |
| 4525 | if (err) |
| 4526 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4527 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4528 | if (!set_in_sync(mddev)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4529 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4530 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 4531 | } else |
| 4532 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4533 | break; |
| 4534 | case active: |
| 4535 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4536 | err = restart_array(mddev); |
| 4537 | if (err) |
| 4538 | break; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4539 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4540 | wake_up(&mddev->sb_wait); |
| 4541 | err = 0; |
| 4542 | } else { |
| 4543 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4544 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4545 | err = do_md_run(mddev); |
| 4546 | } |
| 4547 | break; |
| 4548 | case write_pending: |
| 4549 | case active_idle: |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4550 | case broken: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4551 | /* these cannot be set */ |
| 4552 | break; |
| 4553 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4554 | |
| 4555 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4556 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4557 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4558 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4559 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4560 | mddev_unlock(mddev); |
| 4561 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4562 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4563 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4564 | __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] | 4565 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4566 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4567 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4568 | return sprintf(page, "%d\n", |
| 4569 | atomic_read(&mddev->max_corr_read_errors)); |
| 4570 | } |
| 4571 | |
| 4572 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4573 | 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] | 4574 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4575 | unsigned int n; |
| 4576 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4577 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4578 | rv = kstrtouint(buf, 10, &n); |
| 4579 | if (rv < 0) |
| 4580 | return rv; |
| 4581 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4582 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | static struct md_sysfs_entry max_corr_read_errors = |
| 4586 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4587 | max_corrected_read_errors_store); |
| 4588 | |
| 4589 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4590 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4591 | { |
| 4592 | return -EINVAL; |
| 4593 | } |
| 4594 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4595 | /* need to ensure rdev_delayed_delete() has completed */ |
| 4596 | static void flush_rdev_wq(struct mddev *mddev) |
| 4597 | { |
| 4598 | struct md_rdev *rdev; |
| 4599 | |
| 4600 | rcu_read_lock(); |
| 4601 | rdev_for_each_rcu(rdev, mddev) |
| 4602 | if (work_pending(&rdev->del_work)) { |
| 4603 | flush_workqueue(md_rdev_misc_wq); |
| 4604 | break; |
| 4605 | } |
| 4606 | rcu_read_unlock(); |
| 4607 | } |
| 4608 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4609 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4610 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4611 | { |
| 4612 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4613 | /* The new device is added to the array. |
| 4614 | * If the array has a persistent superblock, we read the |
| 4615 | * superblock to initialise info and check validity. |
| 4616 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4617 | * which mainly checks size. |
| 4618 | */ |
| 4619 | char *e; |
| 4620 | int major = simple_strtoul(buf, &e, 10); |
| 4621 | int minor; |
| 4622 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4623 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4624 | int err; |
| 4625 | |
| 4626 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4627 | return -EINVAL; |
| 4628 | minor = simple_strtoul(e+1, &e, 10); |
| 4629 | if (*e && *e != '\n') |
| 4630 | return -EINVAL; |
| 4631 | dev = MKDEV(major, minor); |
| 4632 | if (major != MAJOR(dev) || |
| 4633 | minor != MINOR(dev)) |
| 4634 | return -EOVERFLOW; |
| 4635 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4636 | flush_rdev_wq(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4637 | err = mddev_lock(mddev); |
| 4638 | if (err) |
| 4639 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4640 | if (mddev->persistent) { |
| 4641 | rdev = md_import_device(dev, mddev->major_version, |
| 4642 | mddev->minor_version); |
| 4643 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4644 | struct md_rdev *rdev0 |
| 4645 | = list_entry(mddev->disks.next, |
| 4646 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4647 | err = super_types[mddev->major_version] |
| 4648 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4649 | if (err < 0) |
| 4650 | goto out; |
| 4651 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4652 | } else if (mddev->external) |
| 4653 | rdev = md_import_device(dev, -2, -1); |
| 4654 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4655 | rdev = md_import_device(dev, -1, -1); |
| 4656 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4657 | if (IS_ERR(rdev)) { |
| 4658 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4659 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4660 | } |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4661 | err = bind_rdev_to_array(rdev, mddev); |
| 4662 | out: |
| 4663 | if (err) |
| 4664 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4665 | mddev_unlock(mddev); |
Alexey Obitotskiy | 5492c46 | 2017-07-28 15:49:25 +0200 | [diff] [blame] | 4666 | if (!err) |
| 4667 | md_new_event(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4668 | return err ? err : len; |
| 4669 | } |
| 4670 | |
| 4671 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4672 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4673 | |
| 4674 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4675 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4676 | { |
| 4677 | char *end; |
| 4678 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4679 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4680 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4681 | err = mddev_lock(mddev); |
| 4682 | if (err) |
| 4683 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4684 | if (!mddev->bitmap) |
| 4685 | goto out; |
| 4686 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4687 | while (*buf) { |
| 4688 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4689 | if (buf == end) break; |
| 4690 | if (*end == '-') { /* range */ |
| 4691 | buf = end + 1; |
| 4692 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4693 | if (buf == end) break; |
| 4694 | } |
| 4695 | if (*end && !isspace(*end)) break; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4696 | md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4697 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4698 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4699 | md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4700 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4701 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4702 | return len; |
| 4703 | } |
| 4704 | |
| 4705 | static struct md_sysfs_entry md_bitmap = |
| 4706 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4707 | |
| 4708 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4709 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4710 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4711 | return sprintf(page, "%llu\n", |
| 4712 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4713 | } |
| 4714 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4715 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4716 | |
| 4717 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4718 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4719 | { |
| 4720 | /* If array is inactive, we can reduce the component size, but |
| 4721 | * not increase it (except from 0). |
| 4722 | * If array is active, we can try an on-line resize |
| 4723 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4724 | sector_t sectors; |
| 4725 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4726 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4727 | if (err < 0) |
| 4728 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4729 | err = mddev_lock(mddev); |
| 4730 | if (err) |
| 4731 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4732 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4733 | err = update_size(mddev, sectors); |
Xiao Ni | 4ba1e78 | 2016-06-12 17:18:00 +0800 | [diff] [blame] | 4734 | if (err == 0) |
| 4735 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4736 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4737 | if (mddev->dev_sectors == 0 || |
| 4738 | mddev->dev_sectors > sectors) |
| 4739 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4740 | else |
| 4741 | err = -ENOSPC; |
| 4742 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4743 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4744 | return err ? err : len; |
| 4745 | } |
| 4746 | |
| 4747 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4748 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4749 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4750 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4751 | * This is one of |
| 4752 | * 'none' for arrays with no metadata (good luck...) |
| 4753 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4754 | * or N.M for internally known formats |
| 4755 | */ |
| 4756 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4757 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4758 | { |
| 4759 | if (mddev->persistent) |
| 4760 | return sprintf(page, "%d.%d\n", |
| 4761 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4762 | else if (mddev->external) |
| 4763 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4764 | else |
| 4765 | return sprintf(page, "none\n"); |
| 4766 | } |
| 4767 | |
| 4768 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4769 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4770 | { |
| 4771 | int major, minor; |
| 4772 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4773 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4774 | /* Changing the details of 'external' metadata is |
| 4775 | * always permitted. Otherwise there must be |
| 4776 | * no devices attached to the array. |
| 4777 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4778 | |
| 4779 | err = mddev_lock(mddev); |
| 4780 | if (err) |
| 4781 | return err; |
| 4782 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4783 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4784 | ; |
| 4785 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4786 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4787 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4788 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4789 | if (cmd_match(buf, "none")) { |
| 4790 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4791 | mddev->external = 0; |
| 4792 | mddev->major_version = 0; |
| 4793 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4794 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4795 | } |
| 4796 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4797 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4798 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4799 | namelen = sizeof(mddev->metadata_type)-1; |
| 4800 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4801 | mddev->metadata_type[namelen] = 0; |
| 4802 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4803 | mddev->metadata_type[--namelen] = 0; |
| 4804 | mddev->persistent = 0; |
| 4805 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4806 | mddev->major_version = 0; |
| 4807 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4808 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4809 | } |
| 4810 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4811 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4812 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4813 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4814 | buf = e+1; |
| 4815 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4816 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4817 | goto out_unlock; |
| 4818 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4819 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4820 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4821 | mddev->major_version = major; |
| 4822 | mddev->minor_version = minor; |
| 4823 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4824 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4825 | err = 0; |
| 4826 | out_unlock: |
| 4827 | mddev_unlock(mddev); |
| 4828 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4832 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4833 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4834 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4835 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4836 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4837 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4838 | unsigned long recovery = mddev->recovery; |
| 4839 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4840 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4841 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4842 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4843 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4844 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4845 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4846 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4847 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4848 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4849 | type = "check"; |
| 4850 | else |
| 4851 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4852 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4853 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4854 | else if (mddev->reshape_position != MaxSector) |
| 4855 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4856 | } |
| 4857 | return sprintf(page, "%s\n", type); |
| 4858 | } |
| 4859 | |
| 4860 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4861 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4862 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4863 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4864 | return -EINVAL; |
| 4865 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4866 | |
| 4867 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4868 | if (cmd_match(page, "frozen")) |
| 4869 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4870 | else |
| 4871 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4872 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4873 | mddev_lock(mddev) == 0) { |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4874 | if (work_pending(&mddev->del_work)) |
| 4875 | flush_workqueue(md_misc_wq); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4876 | if (mddev->sync_thread) { |
| 4877 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4878 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4879 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4880 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4881 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4882 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4883 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4884 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4885 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4886 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4887 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4888 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4889 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4890 | int err; |
| 4891 | if (mddev->pers->start_reshape == NULL) |
| 4892 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4893 | err = mddev_lock(mddev); |
| 4894 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4895 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4896 | err = -EBUSY; |
| 4897 | else { |
| 4898 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4899 | err = mddev->pers->start_reshape(mddev); |
| 4900 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4901 | mddev_unlock(mddev); |
| 4902 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4903 | if (err) |
| 4904 | return err; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 4905 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4906 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4907 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4908 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4909 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4910 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4911 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4912 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4913 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4914 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4915 | if (mddev->ro == 2) { |
| 4916 | /* A write to sync_action is enough to justify |
| 4917 | * canceling read-auto mode |
| 4918 | */ |
| 4919 | mddev->ro = 0; |
| 4920 | md_wakeup_thread(mddev->sync_thread); |
| 4921 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4922 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4923 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4924 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4925 | return len; |
| 4926 | } |
| 4927 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4928 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4929 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4930 | |
| 4931 | static ssize_t |
| 4932 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4933 | { |
| 4934 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4935 | } |
| 4936 | |
| 4937 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4938 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4939 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4940 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4941 | { |
| 4942 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4943 | (unsigned long long) |
| 4944 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4945 | } |
| 4946 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4947 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4948 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4949 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4950 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4951 | { |
| 4952 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4953 | mddev->sync_speed_min ? "local": "system"); |
| 4954 | } |
| 4955 | |
| 4956 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4957 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4958 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4959 | unsigned int min; |
| 4960 | int rv; |
| 4961 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4962 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4963 | min = 0; |
| 4964 | } else { |
| 4965 | rv = kstrtouint(buf, 10, &min); |
| 4966 | if (rv < 0) |
| 4967 | return rv; |
| 4968 | if (min == 0) |
| 4969 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4970 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4971 | mddev->sync_speed_min = min; |
| 4972 | return len; |
| 4973 | } |
| 4974 | |
| 4975 | static struct md_sysfs_entry md_sync_min = |
| 4976 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4977 | |
| 4978 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4979 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4980 | { |
| 4981 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4982 | mddev->sync_speed_max ? "local": "system"); |
| 4983 | } |
| 4984 | |
| 4985 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4986 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4987 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4988 | unsigned int max; |
| 4989 | int rv; |
| 4990 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4991 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4992 | max = 0; |
| 4993 | } else { |
| 4994 | rv = kstrtouint(buf, 10, &max); |
| 4995 | if (rv < 0) |
| 4996 | return rv; |
| 4997 | if (max == 0) |
| 4998 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4999 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5000 | mddev->sync_speed_max = max; |
| 5001 | return len; |
| 5002 | } |
| 5003 | |
| 5004 | static struct md_sysfs_entry md_sync_max = |
| 5005 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 5006 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 5007 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5008 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 5009 | { |
| 5010 | return sprintf(page, "%d\n", mddev->degraded); |
| 5011 | } |
| 5012 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5013 | |
| 5014 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5015 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5016 | { |
| 5017 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 5018 | } |
| 5019 | |
| 5020 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5021 | 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] | 5022 | { |
| 5023 | long n; |
| 5024 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5025 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5026 | return -EINVAL; |
| 5027 | |
| 5028 | if (n != 0 && n != 1) |
| 5029 | return -EINVAL; |
| 5030 | |
| 5031 | mddev->parallel_resync = n; |
| 5032 | |
| 5033 | if (mddev->sync_thread) |
| 5034 | wake_up(&resync_wait); |
| 5035 | |
| 5036 | return len; |
| 5037 | } |
| 5038 | |
| 5039 | /* force parallel resync, even with shared block devices */ |
| 5040 | static struct md_sysfs_entry md_sync_force_parallel = |
| 5041 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 5042 | sync_force_parallel_show, sync_force_parallel_store); |
| 5043 | |
| 5044 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5045 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5046 | { |
| 5047 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 5048 | if (mddev->curr_resync == 0) |
| 5049 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 5050 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 5051 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5052 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 5053 | db = resync - mddev->resync_mark_cnt; |
| 5054 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5055 | } |
| 5056 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 5057 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5058 | |
| 5059 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5060 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5061 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5062 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5063 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5064 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 5065 | return sprintf(page, "none\n"); |
| 5066 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 5067 | if (mddev->curr_resync == 1 || |
| 5068 | mddev->curr_resync == 2) |
| 5069 | return sprintf(page, "delayed\n"); |
| 5070 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 5071 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 5072 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5073 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5074 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5075 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5076 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5077 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5078 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5079 | } |
| 5080 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 5081 | static struct md_sysfs_entry md_sync_completed = |
| 5082 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5083 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5084 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5085 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5086 | { |
| 5087 | return sprintf(page, "%llu\n", |
| 5088 | (unsigned long long)mddev->resync_min); |
| 5089 | } |
| 5090 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5091 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5092 | { |
| 5093 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5094 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5095 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5096 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5097 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5098 | |
| 5099 | spin_lock(&mddev->lock); |
| 5100 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5101 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5102 | goto out_unlock; |
| 5103 | |
| 5104 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5105 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5106 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5107 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 5108 | /* Round down to multiple of 4K for safety */ |
| 5109 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5110 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5111 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5112 | out_unlock: |
| 5113 | spin_unlock(&mddev->lock); |
| 5114 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | static struct md_sysfs_entry md_min_sync = |
| 5118 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 5119 | |
| 5120 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5121 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5122 | { |
| 5123 | if (mddev->resync_max == MaxSector) |
| 5124 | return sprintf(page, "max\n"); |
| 5125 | else |
| 5126 | return sprintf(page, "%llu\n", |
| 5127 | (unsigned long long)mddev->resync_max); |
| 5128 | } |
| 5129 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5130 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5131 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5132 | int err; |
| 5133 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5134 | if (strncmp(buf, "max", 3) == 0) |
| 5135 | mddev->resync_max = MaxSector; |
| 5136 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5137 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5138 | int chunk; |
| 5139 | |
| 5140 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5141 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5142 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5143 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5144 | goto out_unlock; |
| 5145 | |
| 5146 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5147 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 5148 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5149 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5150 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5151 | |
| 5152 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5153 | chunk = mddev->chunk_sectors; |
| 5154 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 5155 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5156 | |
| 5157 | err = -EINVAL; |
| 5158 | if (sector_div(temp, chunk)) |
| 5159 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5160 | } |
| 5161 | mddev->resync_max = max; |
| 5162 | } |
| 5163 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5164 | err = 0; |
| 5165 | out_unlock: |
| 5166 | spin_unlock(&mddev->lock); |
| 5167 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5168 | } |
| 5169 | |
| 5170 | static struct md_sysfs_entry md_max_sync = |
| 5171 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 5172 | |
| 5173 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5174 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5175 | { |
| 5176 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 5177 | } |
| 5178 | |
| 5179 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5180 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5181 | { |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5182 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5183 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5184 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5185 | err = kstrtoull(buf, 10, &new); |
| 5186 | if (err < 0) |
| 5187 | return err; |
| 5188 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5189 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5190 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5191 | err = mddev_lock(mddev); |
| 5192 | if (err) |
| 5193 | return err; |
| 5194 | err = -EINVAL; |
| 5195 | if (mddev->pers == NULL || |
| 5196 | mddev->pers->quiesce == NULL) |
| 5197 | goto unlock; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5198 | mddev_suspend(mddev); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5199 | mddev->suspend_lo = new; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5200 | mddev_resume(mddev); |
| 5201 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5202 | err = 0; |
| 5203 | unlock: |
| 5204 | mddev_unlock(mddev); |
| 5205 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5206 | } |
| 5207 | static struct md_sysfs_entry md_suspend_lo = |
| 5208 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 5209 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5210 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5211 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5212 | { |
| 5213 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 5214 | } |
| 5215 | |
| 5216 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5217 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5218 | { |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5219 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5220 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5221 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5222 | err = kstrtoull(buf, 10, &new); |
| 5223 | if (err < 0) |
| 5224 | return err; |
| 5225 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5226 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5227 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5228 | err = mddev_lock(mddev); |
| 5229 | if (err) |
| 5230 | return err; |
| 5231 | err = -EINVAL; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5232 | if (mddev->pers == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5233 | goto unlock; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5234 | |
| 5235 | mddev_suspend(mddev); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5236 | mddev->suspend_hi = new; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5237 | mddev_resume(mddev); |
| 5238 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5239 | err = 0; |
| 5240 | unlock: |
| 5241 | mddev_unlock(mddev); |
| 5242 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5243 | } |
| 5244 | static struct md_sysfs_entry md_suspend_hi = |
| 5245 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 5246 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5247 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5248 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5249 | { |
| 5250 | if (mddev->reshape_position != MaxSector) |
| 5251 | return sprintf(page, "%llu\n", |
| 5252 | (unsigned long long)mddev->reshape_position); |
| 5253 | strcpy(page, "none\n"); |
| 5254 | return 5; |
| 5255 | } |
| 5256 | |
| 5257 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5258 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5259 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5260 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5261 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5262 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5263 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5264 | err = kstrtoull(buf, 10, &new); |
| 5265 | if (err < 0) |
| 5266 | return err; |
| 5267 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5268 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5269 | err = mddev_lock(mddev); |
| 5270 | if (err) |
| 5271 | return err; |
| 5272 | err = -EBUSY; |
| 5273 | if (mddev->pers) |
| 5274 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5275 | mddev->reshape_position = new; |
| 5276 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5277 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5278 | mddev->new_level = mddev->level; |
| 5279 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5280 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5281 | rdev_for_each(rdev, mddev) |
| 5282 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5283 | err = 0; |
| 5284 | unlock: |
| 5285 | mddev_unlock(mddev); |
| 5286 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | static struct md_sysfs_entry md_reshape_position = |
| 5290 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 5291 | reshape_position_store); |
| 5292 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5293 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5294 | reshape_direction_show(struct mddev *mddev, char *page) |
| 5295 | { |
| 5296 | return sprintf(page, "%s\n", |
| 5297 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 5298 | } |
| 5299 | |
| 5300 | static ssize_t |
| 5301 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 5302 | { |
| 5303 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5304 | int err; |
| 5305 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5306 | if (cmd_match(buf, "forwards")) |
| 5307 | backwards = 0; |
| 5308 | else if (cmd_match(buf, "backwards")) |
| 5309 | backwards = 1; |
| 5310 | else |
| 5311 | return -EINVAL; |
| 5312 | if (mddev->reshape_backwards == backwards) |
| 5313 | return len; |
| 5314 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5315 | err = mddev_lock(mddev); |
| 5316 | if (err) |
| 5317 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5318 | /* check if we are allowed to change */ |
| 5319 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5320 | err = -EBUSY; |
| 5321 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5322 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5323 | err = -EINVAL; |
| 5324 | else |
| 5325 | mddev->reshape_backwards = backwards; |
| 5326 | mddev_unlock(mddev); |
| 5327 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | static struct md_sysfs_entry md_reshape_direction = |
| 5331 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 5332 | reshape_direction_store); |
| 5333 | |
| 5334 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5335 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5336 | { |
| 5337 | if (mddev->external_size) |
| 5338 | return sprintf(page, "%llu\n", |
| 5339 | (unsigned long long)mddev->array_sectors/2); |
| 5340 | else |
| 5341 | return sprintf(page, "default\n"); |
| 5342 | } |
| 5343 | |
| 5344 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5345 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5346 | { |
| 5347 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5348 | int err; |
| 5349 | |
| 5350 | err = mddev_lock(mddev); |
| 5351 | if (err) |
| 5352 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5353 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5354 | /* cluster raid doesn't support change array_sectors */ |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 5355 | if (mddev_is_clustered(mddev)) { |
| 5356 | mddev_unlock(mddev); |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5357 | return -EINVAL; |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 5358 | } |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5359 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5360 | if (strncmp(buf, "default", 7) == 0) { |
| 5361 | if (mddev->pers) |
| 5362 | sectors = mddev->pers->size(mddev, 0, 0); |
| 5363 | else |
| 5364 | sectors = mddev->array_sectors; |
| 5365 | |
| 5366 | mddev->external_size = 0; |
| 5367 | } else { |
| 5368 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5369 | err = -EINVAL; |
| 5370 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 5371 | err = -E2BIG; |
| 5372 | else |
| 5373 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5374 | } |
| 5375 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5376 | if (!err) { |
| 5377 | mddev->array_sectors = sectors; |
| 5378 | if (mddev->pers) { |
| 5379 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 5380 | revalidate_disk(mddev->gendisk); |
| 5381 | } |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 5382 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5383 | mddev_unlock(mddev); |
| 5384 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5385 | } |
| 5386 | |
| 5387 | static struct md_sysfs_entry md_array_size = |
| 5388 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 5389 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5390 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5391 | static ssize_t |
| 5392 | consistency_policy_show(struct mddev *mddev, char *page) |
| 5393 | { |
| 5394 | int ret; |
| 5395 | |
| 5396 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5397 | ret = sprintf(page, "journal\n"); |
| 5398 | } else if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 5399 | ret = sprintf(page, "ppl\n"); |
| 5400 | } else if (mddev->bitmap) { |
| 5401 | ret = sprintf(page, "bitmap\n"); |
| 5402 | } else if (mddev->pers) { |
| 5403 | if (mddev->pers->sync_request) |
| 5404 | ret = sprintf(page, "resync\n"); |
| 5405 | else |
| 5406 | ret = sprintf(page, "none\n"); |
| 5407 | } else { |
| 5408 | ret = sprintf(page, "unknown\n"); |
| 5409 | } |
| 5410 | |
| 5411 | return ret; |
| 5412 | } |
| 5413 | |
| 5414 | static ssize_t |
| 5415 | consistency_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5416 | { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5417 | int err = 0; |
| 5418 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5419 | if (mddev->pers) { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5420 | if (mddev->pers->change_consistency_policy) |
| 5421 | err = mddev->pers->change_consistency_policy(mddev, buf); |
| 5422 | else |
| 5423 | err = -EBUSY; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5424 | } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) { |
| 5425 | set_bit(MD_HAS_PPL, &mddev->flags); |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5426 | } else { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5427 | err = -EINVAL; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5428 | } |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5429 | |
| 5430 | return err ? err : len; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | static struct md_sysfs_entry md_consistency_policy = |
| 5434 | __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show, |
| 5435 | consistency_policy_store); |
| 5436 | |
Guoqing Jiang | 9a56784 | 2019-07-24 11:09:19 +0200 | [diff] [blame] | 5437 | static ssize_t fail_last_dev_show(struct mddev *mddev, char *page) |
| 5438 | { |
| 5439 | return sprintf(page, "%d\n", mddev->fail_last_dev); |
| 5440 | } |
| 5441 | |
| 5442 | /* |
| 5443 | * Setting fail_last_dev to true to allow last device to be forcibly removed |
| 5444 | * from RAID1/RAID10. |
| 5445 | */ |
| 5446 | static ssize_t |
| 5447 | fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len) |
| 5448 | { |
| 5449 | int ret; |
| 5450 | bool value; |
| 5451 | |
| 5452 | ret = kstrtobool(buf, &value); |
| 5453 | if (ret) |
| 5454 | return ret; |
| 5455 | |
| 5456 | if (value != mddev->fail_last_dev) |
| 5457 | mddev->fail_last_dev = value; |
| 5458 | |
| 5459 | return len; |
| 5460 | } |
| 5461 | static struct md_sysfs_entry md_fail_last_dev = |
| 5462 | __ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show, |
| 5463 | fail_last_dev_store); |
| 5464 | |
Guoqing Jiang | 3938f5f | 2019-12-23 10:48:56 +0100 | [diff] [blame] | 5465 | static ssize_t serialize_policy_show(struct mddev *mddev, char *page) |
| 5466 | { |
| 5467 | if (mddev->pers == NULL || (mddev->pers->level != 1)) |
| 5468 | return sprintf(page, "n/a\n"); |
| 5469 | else |
| 5470 | return sprintf(page, "%d\n", mddev->serialize_policy); |
| 5471 | } |
| 5472 | |
| 5473 | /* |
| 5474 | * Setting serialize_policy to true to enforce write IO is not reordered |
| 5475 | * for raid1. |
| 5476 | */ |
| 5477 | static ssize_t |
| 5478 | serialize_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5479 | { |
| 5480 | int err; |
| 5481 | bool value; |
| 5482 | |
| 5483 | err = kstrtobool(buf, &value); |
| 5484 | if (err) |
| 5485 | return err; |
| 5486 | |
| 5487 | if (value == mddev->serialize_policy) |
| 5488 | return len; |
| 5489 | |
| 5490 | err = mddev_lock(mddev); |
| 5491 | if (err) |
| 5492 | return err; |
| 5493 | if (mddev->pers == NULL || (mddev->pers->level != 1)) { |
| 5494 | pr_err("md: serialize_policy is only effective for raid1\n"); |
| 5495 | err = -EINVAL; |
| 5496 | goto unlock; |
| 5497 | } |
| 5498 | |
| 5499 | mddev_suspend(mddev); |
| 5500 | if (value) |
| 5501 | mddev_create_serial_pool(mddev, NULL, true); |
| 5502 | else |
| 5503 | mddev_destroy_serial_pool(mddev, NULL, true); |
| 5504 | mddev->serialize_policy = value; |
| 5505 | mddev_resume(mddev); |
| 5506 | unlock: |
| 5507 | mddev_unlock(mddev); |
| 5508 | return err ?: len; |
| 5509 | } |
| 5510 | |
| 5511 | static struct md_sysfs_entry md_serialize_policy = |
| 5512 | __ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show, |
| 5513 | serialize_policy_store); |
| 5514 | |
| 5515 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5516 | static struct attribute *md_default_attrs[] = { |
| 5517 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 5518 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5519 | &md_raid_disks.attr, |
Sebastian Parschauer | ec164d07 | 2020-07-28 12:01:39 +0200 | [diff] [blame] | 5520 | &md_uuid.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 5521 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5522 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 5523 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 5524 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 5525 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5526 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5527 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5528 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5529 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5530 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5531 | &max_corr_read_errors.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5532 | &md_consistency_policy.attr, |
Guoqing Jiang | 9a56784 | 2019-07-24 11:09:19 +0200 | [diff] [blame] | 5533 | &md_fail_last_dev.attr, |
Guoqing Jiang | 3938f5f | 2019-12-23 10:48:56 +0100 | [diff] [blame] | 5534 | &md_serialize_policy.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5535 | NULL, |
| 5536 | }; |
| 5537 | |
| 5538 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5539 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 5540 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5541 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5542 | &md_sync_min.attr, |
| 5543 | &md_sync_max.attr, |
| 5544 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5545 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5546 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5547 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5548 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5549 | &md_suspend_lo.attr, |
| 5550 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 5551 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 5552 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5553 | NULL, |
| 5554 | }; |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5555 | static struct attribute_group md_redundancy_group = { |
| 5556 | .name = NULL, |
| 5557 | .attrs = md_redundancy_attrs, |
| 5558 | }; |
| 5559 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5560 | static ssize_t |
| 5561 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 5562 | { |
| 5563 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5564 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5565 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5566 | |
| 5567 | if (!entry->show) |
| 5568 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5569 | spin_lock(&all_mddevs_lock); |
| 5570 | if (list_empty(&mddev->all_mddevs)) { |
| 5571 | spin_unlock(&all_mddevs_lock); |
| 5572 | return -EBUSY; |
| 5573 | } |
| 5574 | mddev_get(mddev); |
| 5575 | spin_unlock(&all_mddevs_lock); |
| 5576 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 5577 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5578 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5579 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5580 | } |
| 5581 | |
| 5582 | static ssize_t |
| 5583 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 5584 | const char *page, size_t length) |
| 5585 | { |
| 5586 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5587 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5588 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5589 | |
| 5590 | if (!entry->store) |
| 5591 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 5592 | if (!capable(CAP_SYS_ADMIN)) |
| 5593 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5594 | spin_lock(&all_mddevs_lock); |
| 5595 | if (list_empty(&mddev->all_mddevs)) { |
| 5596 | spin_unlock(&all_mddevs_lock); |
| 5597 | return -EBUSY; |
| 5598 | } |
| 5599 | mddev_get(mddev); |
| 5600 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5601 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5602 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5603 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5604 | } |
| 5605 | |
| 5606 | static void md_free(struct kobject *ko) |
| 5607 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5608 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5609 | |
| 5610 | if (mddev->sysfs_state) |
| 5611 | sysfs_put(mddev->sysfs_state); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 5612 | if (mddev->sysfs_completed) |
| 5613 | sysfs_put(mddev->sysfs_completed); |
| 5614 | if (mddev->sysfs_degraded) |
| 5615 | sysfs_put(mddev->sysfs_degraded); |
| 5616 | if (mddev->sysfs_level) |
| 5617 | sysfs_put(mddev->sysfs_level); |
| 5618 | |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5619 | |
Bart Van Assche | d8115c35 | 2018-02-28 10:15:29 -0800 | [diff] [blame] | 5620 | if (mddev->gendisk) |
| 5621 | del_gendisk(mddev->gendisk); |
NeilBrown | 6cd18e7 | 2015-04-27 14:12:22 +1000 | [diff] [blame] | 5622 | if (mddev->queue) |
| 5623 | blk_cleanup_queue(mddev->queue); |
Bart Van Assche | d8115c35 | 2018-02-28 10:15:29 -0800 | [diff] [blame] | 5624 | if (mddev->gendisk) |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5625 | put_disk(mddev->gendisk); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5626 | percpu_ref_exit(&mddev->writes_pending); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5627 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5628 | bioset_exit(&mddev->bio_set); |
| 5629 | bioset_exit(&mddev->sync_set); |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 5630 | mempool_exit(&mddev->md_io_pool); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5631 | kfree(mddev); |
| 5632 | } |
| 5633 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 5634 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5635 | .show = md_attr_show, |
| 5636 | .store = md_attr_store, |
| 5637 | }; |
| 5638 | static struct kobj_type md_ktype = { |
| 5639 | .release = md_free, |
| 5640 | .sysfs_ops = &md_sysfs_ops, |
| 5641 | .default_attrs = md_default_attrs, |
| 5642 | }; |
| 5643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | int mdp_major = 0; |
| 5645 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5646 | static void mddev_delayed_delete(struct work_struct *ws) |
| 5647 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5648 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5649 | |
NeilBrown | 43a7050 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 5650 | sysfs_remove_group(&mddev->kobj, &md_bitmap_group); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5651 | kobject_del(&mddev->kobj); |
| 5652 | kobject_put(&mddev->kobj); |
| 5653 | } |
| 5654 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5655 | static void no_op(struct percpu_ref *r) {} |
| 5656 | |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5657 | int mddev_init_writes_pending(struct mddev *mddev) |
| 5658 | { |
| 5659 | if (mddev->writes_pending.percpu_count_ptr) |
| 5660 | return 0; |
Roman Gushchin | ddde2af | 2019-05-07 10:01:49 -0700 | [diff] [blame] | 5661 | if (percpu_ref_init(&mddev->writes_pending, no_op, |
| 5662 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0) |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5663 | return -ENOMEM; |
| 5664 | /* We want to start with the refcount at zero */ |
| 5665 | percpu_ref_put(&mddev->writes_pending); |
| 5666 | return 0; |
| 5667 | } |
| 5668 | EXPORT_SYMBOL_GPL(mddev_init_writes_pending); |
| 5669 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5670 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5672 | /* |
| 5673 | * If dev is zero, name is the name of a device to allocate with |
| 5674 | * an arbitrary minor number. It will be "md_???" |
| 5675 | * If dev is non-zero it must be a device number with a MAJOR of |
| 5676 | * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then |
| 5677 | * the device is being created by opening a node in /dev. |
| 5678 | * If "name" is not NULL, the device is being created by |
| 5679 | * writing to /sys/module/md_mod/parameters/new_array. |
| 5680 | */ |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5681 | static DEFINE_MUTEX(disks_mutex); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5682 | struct mddev *mddev = mddev_find(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5684 | int partitioned; |
| 5685 | int shift; |
| 5686 | int unit; |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5687 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | |
| 5689 | if (!mddev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5690 | return -ENODEV; |
| 5691 | |
| 5692 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 5693 | shift = partitioned ? MdpMinorShift : 0; |
| 5694 | unit = MINOR(mddev->unit) >> shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5696 | /* wait for any previous instance of this device to be |
| 5697 | * completely removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5698 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5699 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5700 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5701 | mutex_lock(&disks_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5702 | error = -EEXIST; |
| 5703 | if (mddev->gendisk) |
| 5704 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5705 | |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5706 | if (name && !dev) { |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5707 | /* Need to ensure that 'name' is not a duplicate. |
| 5708 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5709 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5710 | spin_lock(&all_mddevs_lock); |
| 5711 | |
| 5712 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5713 | if (mddev2->gendisk && |
| 5714 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5715 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5716 | goto abort; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5717 | } |
| 5718 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5719 | } |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5720 | if (name && dev) |
| 5721 | /* |
| 5722 | * Creating /dev/mdNNN via "newarray", so adjust hold_active. |
| 5723 | */ |
| 5724 | mddev->hold_active = UNTIL_STOP; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5725 | |
Artur Paszkiewicz | 41d2d84 | 2020-07-03 11:13:09 +0200 | [diff] [blame] | 5726 | error = mempool_init_kmalloc_pool(&mddev->md_io_pool, BIO_POOL_SIZE, |
| 5727 | sizeof(struct md_io)); |
| 5728 | if (error) |
| 5729 | goto abort; |
| 5730 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5731 | error = -ENOMEM; |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 5732 | mddev->queue = blk_alloc_queue(NUMA_NO_NODE); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5733 | if (!mddev->queue) |
| 5734 | goto abort; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5735 | |
Martin K. Petersen | b1bd055 | 2012-01-11 16:27:11 +0100 | [diff] [blame] | 5736 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | disk = alloc_disk(1 << shift); |
| 5739 | if (!disk) { |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5740 | blk_cleanup_queue(mddev->queue); |
| 5741 | mddev->queue = NULL; |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5742 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | } |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5744 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5745 | disk->first_minor = unit << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5746 | if (name) |
| 5747 | strcpy(disk->disk_name, name); |
| 5748 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5749 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5750 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5751 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5752 | disk->fops = &md_fops; |
| 5753 | disk->private_data = mddev; |
| 5754 | disk->queue = mddev->queue; |
Jens Axboe | 56883a7 | 2016-03-30 10:16:53 -0600 | [diff] [blame] | 5755 | blk_queue_write_cache(mddev->queue, true, true); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5756 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5757 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5758 | * remove it now. |
| 5759 | */ |
| 5760 | disk->flags |= GENHD_FL_EXT_DEVT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | mddev->gendisk = disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5762 | /* As soon as we call add_disk(), another thread could get |
| 5763 | * through to md_open, so make sure it doesn't get too far |
| 5764 | */ |
| 5765 | mutex_lock(&mddev->open_mutex); |
| 5766 | add_disk(disk); |
| 5767 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5768 | error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md"); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5769 | if (error) { |
| 5770 | /* This isn't possible, but as kobject_init_and_add is marked |
| 5771 | * __must_check, we must do something with the result |
| 5772 | */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5773 | pr_debug("md: cannot register %s/md - name in use\n", |
| 5774 | disk->disk_name); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5775 | error = 0; |
| 5776 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5777 | if (mddev->kobj.sd && |
| 5778 | sysfs_create_group(&mddev->kobj, &md_bitmap_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5779 | pr_debug("pointless warning\n"); |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5780 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5781 | abort: |
| 5782 | mutex_unlock(&disks_mutex); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5783 | if (!error && mddev->kobj.sd) { |
Greg Kroah-Hartman | 3830c62 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 5784 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5785 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 5786 | mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed"); |
| 5787 | mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded"); |
| 5788 | mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level"); |
NeilBrown | b62b759 | 2008-10-21 13:25:21 +1100 | [diff] [blame] | 5789 | } |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5790 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5791 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5792 | } |
| 5793 | |
| 5794 | static struct kobject *md_probe(dev_t dev, int *part, void *data) |
| 5795 | { |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5796 | if (create_on_open) |
| 5797 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5798 | return NULL; |
| 5799 | } |
| 5800 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 5801 | static int add_named_array(const char *val, const struct kernel_param *kp) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5802 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5803 | /* |
| 5804 | * val must be "md_*" or "mdNNN". |
| 5805 | * For "md_*" we allocate an array with a large free minor number, and |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5806 | * set the name to val. val must not already be an active name. |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5807 | * For "mdNNN" we allocate an array with the minor number NNN |
| 5808 | * which must not already be in use. |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5809 | */ |
| 5810 | int len = strlen(val); |
| 5811 | char buf[DISK_NAME_LEN]; |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5812 | unsigned long devnum; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5813 | |
| 5814 | while (len && val[len-1] == '\n') |
| 5815 | len--; |
| 5816 | if (len >= DISK_NAME_LEN) |
| 5817 | return -E2BIG; |
| 5818 | strlcpy(buf, val, len+1); |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5819 | if (strncmp(buf, "md_", 3) == 0) |
| 5820 | return md_alloc(0, buf); |
| 5821 | if (strncmp(buf, "md", 2) == 0 && |
| 5822 | isdigit(buf[2]) && |
| 5823 | kstrtoul(buf+2, 10, &devnum) == 0 && |
| 5824 | devnum <= MINORMASK) |
| 5825 | return md_alloc(MKDEV(MD_MAJOR, devnum), NULL); |
| 5826 | |
| 5827 | return -EINVAL; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5828 | } |
| 5829 | |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 5830 | static void md_safemode_timeout(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | { |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 5832 | struct mddev *mddev = from_timer(mddev, t, safemode_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5834 | mddev->safemode = 1; |
| 5835 | if (mddev->external) |
| 5836 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5838 | md_wakeup_thread(mddev->thread); |
| 5839 | } |
| 5840 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5841 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5842 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5843 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5844 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5845 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5846 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5847 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5848 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5849 | if (list_empty(&mddev->disks)) |
| 5850 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5851 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | |
| 5853 | if (mddev->pers) |
| 5854 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5855 | /* Cannot run until previous stop completes properly */ |
| 5856 | if (mddev->sysfs_active) |
| 5857 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | /* |
| 5860 | * Analyze all RAID superblock(s) |
| 5861 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5862 | if (!mddev->raid_disks) { |
| 5863 | if (!mddev->persistent) |
| 5864 | return -EINVAL; |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 5865 | err = analyze_sbs(mddev); |
| 5866 | if (err) |
| 5867 | return -EINVAL; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5868 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5870 | if (mddev->level != LEVEL_NONE) |
| 5871 | request_module("md-level-%d", mddev->level); |
| 5872 | else if (mddev->clevel[0]) |
| 5873 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | |
| 5875 | /* |
| 5876 | * Drop all container device buffers, from now on |
| 5877 | * the only valid external interface is through the md |
| 5878 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | */ |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 5880 | mddev->has_superblocks = false; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5881 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5882 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5883 | continue; |
| 5884 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5885 | invalidate_bdev(rdev->bdev); |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 5886 | if (mddev->ro != 1 && |
| 5887 | (bdev_read_only(rdev->bdev) || |
| 5888 | bdev_read_only(rdev->meta_bdev))) { |
| 5889 | mddev->ro = 1; |
| 5890 | if (mddev->gendisk) |
| 5891 | set_disk_ro(mddev->gendisk, 1); |
| 5892 | } |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5893 | |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 5894 | if (rdev->sb_page) |
| 5895 | mddev->has_superblocks = true; |
| 5896 | |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5897 | /* perform some consistency tests on the device. |
| 5898 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5899 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5900 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5901 | if (rdev->meta_bdev) { |
| 5902 | /* Nothing to check */; |
| 5903 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5904 | if (mddev->dev_sectors && |
| 5905 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5906 | > rdev->sb_start) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5907 | pr_warn("md: %s: data overlaps metadata\n", |
| 5908 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5909 | return -EINVAL; |
| 5910 | } |
| 5911 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5912 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5913 | > rdev->data_offset) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5914 | pr_warn("md: %s: metadata overlaps data\n", |
| 5915 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5916 | return -EINVAL; |
| 5917 | } |
| 5918 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5919 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5920 | } |
| 5921 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5922 | if (!bioset_initialized(&mddev->bio_set)) { |
| 5923 | err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); |
| 5924 | if (err) |
| 5925 | return err; |
Ming Lei | 1027317 | 2017-02-14 23:29:00 +0800 | [diff] [blame] | 5926 | } |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5927 | if (!bioset_initialized(&mddev->sync_set)) { |
| 5928 | err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); |
| 5929 | if (err) |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5930 | return err; |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 5931 | } |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5934 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5935 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5937 | if (mddev->level != LEVEL_NONE) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5938 | pr_warn("md: personality for level %d is not loaded!\n", |
| 5939 | mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5940 | else |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5941 | pr_warn("md: personality for level %s is not loaded!\n", |
| 5942 | mddev->clevel); |
Shaohua Li | bfc9dfd | 2018-06-13 08:39:49 -0700 | [diff] [blame] | 5943 | err = -EINVAL; |
| 5944 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5945 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5947 | if (mddev->level != pers->level) { |
| 5948 | mddev->level = pers->level; |
| 5949 | mddev->new_level = pers->level; |
| 5950 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5951 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5952 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5953 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5954 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5955 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5956 | module_put(pers->owner); |
Shaohua Li | bfc9dfd | 2018-06-13 08:39:49 -0700 | [diff] [blame] | 5957 | err = -EINVAL; |
| 5958 | goto abort; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5959 | } |
| 5960 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5961 | if (pers->sync_request) { |
| 5962 | /* Warn if this is a potentially silly |
| 5963 | * configuration. |
| 5964 | */ |
| 5965 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5966 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5967 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5968 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5969 | rdev_for_each(rdev, mddev) |
| 5970 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5971 | if (rdev < rdev2 && |
| 5972 | rdev->bdev->bd_contains == |
| 5973 | rdev2->bdev->bd_contains) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5974 | pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n", |
| 5975 | mdname(mddev), |
| 5976 | bdevname(rdev->bdev,b), |
| 5977 | bdevname(rdev2->bdev,b2)); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5978 | warned = 1; |
| 5979 | } |
| 5980 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5981 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5982 | if (warned) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5983 | pr_warn("True protection against single-disk failure might be compromised.\n"); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5984 | } |
| 5985 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5986 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5987 | /* may be over-ridden by personality */ |
| 5988 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5989 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5990 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5992 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5993 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5994 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5995 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5996 | if (err) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5997 | pr_warn("md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5998 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5999 | WARN_ONCE(!mddev->external_size, |
| 6000 | "%s: default size too small, but 'external_size' not in effect?\n", |
| 6001 | __func__); |
| 6002 | pr_warn("md: invalid array_size %llu > default size %llu\n", |
| 6003 | (unsigned long long)mddev->array_sectors / 2, |
| 6004 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6005 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6006 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6007 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 6008 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6009 | struct bitmap *bitmap; |
| 6010 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6011 | bitmap = md_bitmap_create(mddev, -1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6012 | if (IS_ERR(bitmap)) { |
| 6013 | err = PTR_ERR(bitmap); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6014 | pr_warn("%s: failed to create bitmap (%d)\n", |
| 6015 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 6016 | } else |
| 6017 | mddev->bitmap = bitmap; |
| 6018 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 6019 | } |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 6020 | if (err) |
| 6021 | goto bitmap_abort; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6022 | |
| 6023 | if (mddev->bitmap_info.max_write_behind > 0) { |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 6024 | bool create_pool = false; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6025 | |
| 6026 | rdev_for_each(rdev, mddev) { |
| 6027 | if (test_bit(WriteMostly, &rdev->flags) && |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 6028 | rdev_init_serial(rdev)) |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 6029 | create_pool = true; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6030 | } |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 6031 | if (create_pool && mddev->serial_info_pool == NULL) { |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 6032 | mddev->serial_info_pool = |
| 6033 | mempool_create_kmalloc_pool(NR_SERIAL_INFOS, |
| 6034 | sizeof(struct serial_info)); |
| 6035 | if (!mddev->serial_info_pool) { |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6036 | err = -ENOMEM; |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 6037 | goto bitmap_abort; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 6038 | } |
| 6039 | } |
| 6040 | } |
| 6041 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 6042 | if (mddev->queue) { |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 6043 | bool nonrot = true; |
| 6044 | |
| 6045 | rdev_for_each(rdev, mddev) { |
| 6046 | if (rdev->raid_disk >= 0 && |
| 6047 | !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 6048 | nonrot = false; |
| 6049 | break; |
| 6050 | } |
| 6051 | } |
| 6052 | if (mddev->degraded) |
| 6053 | nonrot = false; |
| 6054 | if (nonrot) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 6055 | blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue); |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 6056 | else |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 6057 | blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 6058 | mddev->queue->backing_dev_info->congested_data = mddev; |
| 6059 | mddev->queue->backing_dev_info->congested_fn = md_congested; |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 6060 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6061 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6062 | if (mddev->kobj.sd && |
| 6063 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6064 | pr_warn("md: cannot register extra attributes for %s\n", |
| 6065 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6066 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6067 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 6068 | mddev->ro = 0; |
| 6069 | |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6070 | atomic_set(&mddev->max_corr_read_errors, |
| 6071 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6073 | if (mddev_is_clustered(mddev)) |
| 6074 | mddev->safemode_delay = 0; |
| 6075 | else |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 6076 | mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6077 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6078 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6079 | spin_lock(&mddev->lock); |
| 6080 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6081 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6082 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 6083 | if (rdev->raid_disk >= 0) |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 6084 | sysfs_link_rdev(mddev, rdev); /* failure here is OK */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6085 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 6086 | if (mddev->degraded && !mddev->ro) |
| 6087 | /* This ensures that recovering status is reported immediately |
| 6088 | * via sysfs - until a lack of spares is confirmed. |
| 6089 | */ |
| 6090 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6091 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6092 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6093 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6094 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6096 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6097 | return 0; |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6098 | |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 6099 | bitmap_abort: |
| 6100 | mddev_detach(mddev); |
| 6101 | if (mddev->private) |
| 6102 | pers->free(mddev, mddev->private); |
| 6103 | mddev->private = NULL; |
| 6104 | module_put(pers->owner); |
| 6105 | md_bitmap_destroy(mddev); |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6106 | abort: |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 6107 | bioset_exit(&mddev->bio_set); |
| 6108 | bioset_exit(&mddev->sync_set); |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6109 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6111 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6112 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6113 | static int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6114 | { |
| 6115 | int err; |
| 6116 | |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6117 | set_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6118 | err = md_run(mddev); |
| 6119 | if (err) |
| 6120 | goto out; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6121 | err = md_bitmap_load(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6122 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6123 | md_bitmap_destroy(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6124 | goto out; |
| 6125 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 6126 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6127 | if (mddev_is_clustered(mddev)) |
| 6128 | md_allow_write(mddev); |
| 6129 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 6130 | /* run start up tasks that require md_thread */ |
| 6131 | md_start(mddev); |
| 6132 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 6133 | md_wakeup_thread(mddev->thread); |
| 6134 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 6135 | |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6136 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 6137 | revalidate_disk(mddev->gendisk); |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6138 | clear_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6139 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6140 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6141 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 6142 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 6143 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6144 | out: |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6145 | clear_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6146 | return err; |
| 6147 | } |
| 6148 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 6149 | int md_start(struct mddev *mddev) |
| 6150 | { |
| 6151 | int ret = 0; |
| 6152 | |
| 6153 | if (mddev->pers->start) { |
| 6154 | set_bit(MD_RECOVERY_WAIT, &mddev->recovery); |
| 6155 | md_wakeup_thread(mddev->thread); |
| 6156 | ret = mddev->pers->start(mddev); |
| 6157 | clear_bit(MD_RECOVERY_WAIT, &mddev->recovery); |
| 6158 | md_wakeup_thread(mddev->sync_thread); |
| 6159 | } |
| 6160 | return ret; |
| 6161 | } |
| 6162 | EXPORT_SYMBOL_GPL(md_start); |
| 6163 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6164 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6165 | { |
| 6166 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6167 | struct md_rdev *rdev; |
| 6168 | bool has_journal = false; |
| 6169 | bool has_readonly = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6170 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6171 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6173 | return -ENXIO; |
| 6174 | if (!mddev->pers) |
| 6175 | return -EINVAL; |
| 6176 | if (!mddev->ro) |
| 6177 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6178 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6179 | rcu_read_lock(); |
| 6180 | rdev_for_each_rcu(rdev, mddev) { |
| 6181 | if (test_bit(Journal, &rdev->flags) && |
| 6182 | !test_bit(Faulty, &rdev->flags)) |
| 6183 | has_journal = true; |
| 6184 | if (bdev_read_only(rdev->bdev)) |
| 6185 | has_readonly = true; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6186 | } |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6187 | rcu_read_unlock(); |
| 6188 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal) |
| 6189 | /* Don't restart rw with journal missing/faulty */ |
| 6190 | return -EINVAL; |
| 6191 | if (has_readonly) |
| 6192 | return -EROFS; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6193 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6194 | mddev->safemode = 0; |
| 6195 | mddev->ro = 0; |
| 6196 | set_disk_ro(disk, 0); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6197 | pr_debug("md: %s switched to read-write mode.\n", mdname(mddev)); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6198 | /* Kick recovery or resync if necessary */ |
| 6199 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6200 | md_wakeup_thread(mddev->thread); |
| 6201 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6202 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6203 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6204 | } |
| 6205 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6206 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6207 | { |
| 6208 | mddev->array_sectors = 0; |
| 6209 | mddev->external_size = 0; |
| 6210 | mddev->dev_sectors = 0; |
| 6211 | mddev->raid_disks = 0; |
| 6212 | mddev->recovery_cp = 0; |
| 6213 | mddev->resync_min = 0; |
| 6214 | mddev->resync_max = MaxSector; |
| 6215 | mddev->reshape_position = MaxSector; |
| 6216 | mddev->external = 0; |
| 6217 | mddev->persistent = 0; |
| 6218 | mddev->level = LEVEL_NONE; |
| 6219 | mddev->clevel[0] = 0; |
| 6220 | mddev->flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6221 | mddev->sb_flags = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6222 | mddev->ro = 0; |
| 6223 | mddev->metadata_type[0] = 0; |
| 6224 | mddev->chunk_sectors = 0; |
| 6225 | mddev->ctime = mddev->utime = 0; |
| 6226 | mddev->layout = 0; |
| 6227 | mddev->max_disks = 0; |
| 6228 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 6229 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6230 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6231 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6232 | mddev->new_level = LEVEL_NONE; |
| 6233 | mddev->new_layout = 0; |
| 6234 | mddev->new_chunk_sectors = 0; |
| 6235 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 6236 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6237 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 6238 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 6239 | mddev->recovery = 0; |
| 6240 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6241 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6242 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6243 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 6244 | mddev->private = NULL; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 6245 | mddev->cluster_info = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6246 | mddev->bitmap_info.offset = 0; |
| 6247 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6248 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6249 | mddev->bitmap_info.chunksize = 0; |
| 6250 | mddev->bitmap_info.daemon_sleep = 0; |
| 6251 | mddev->bitmap_info.max_write_behind = 0; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 6252 | mddev->bitmap_info.nodes = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6253 | } |
| 6254 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6255 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6256 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 6257 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Guoqing Jiang | 21e0958 | 2020-04-04 23:57:07 +0200 | [diff] [blame] | 6258 | if (work_pending(&mddev->del_work)) |
| 6259 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6260 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6261 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 6262 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6263 | } |
| 6264 | |
| 6265 | del_timer_sync(&mddev->safemode_timer); |
| 6266 | |
Shaohua Li | 034e33f | 2016-11-21 10:29:19 -0800 | [diff] [blame] | 6267 | if (mddev->pers && mddev->pers->quiesce) { |
| 6268 | mddev->pers->quiesce(mddev, 1); |
| 6269 | mddev->pers->quiesce(mddev, 0); |
| 6270 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6271 | md_bitmap_flush(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6272 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6273 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6274 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6275 | mddev->sb_flags)) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6276 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6277 | if (!mddev_is_clustered(mddev)) |
| 6278 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6279 | md_update_sb(mddev, 1); |
| 6280 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 6281 | /* disable policy to guarantee rdevs free resources for serialization */ |
| 6282 | mddev->serialize_policy = 0; |
| 6283 | mddev_destroy_serial_pool(mddev, NULL, true); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6284 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6285 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6286 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6287 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6288 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6289 | __md_stop_writes(mddev); |
| 6290 | mddev_unlock(mddev); |
| 6291 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6292 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6293 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6294 | static void mddev_detach(struct mddev *mddev) |
| 6295 | { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6296 | md_bitmap_wait_behind_writes(mddev); |
Guoqing Jiang | 6b40bec | 2020-02-11 11:10:04 +0100 | [diff] [blame] | 6297 | if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6298 | mddev->pers->quiesce(mddev, 1); |
| 6299 | mddev->pers->quiesce(mddev, 0); |
| 6300 | } |
| 6301 | md_unregister_thread(&mddev->thread); |
| 6302 | if (mddev->queue) |
| 6303 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 6304 | } |
| 6305 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6306 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6307 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6308 | struct md_personality *pers = mddev->pers; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6309 | md_bitmap_destroy(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6310 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 6311 | /* Ensure ->event_work is done */ |
Guoqing Jiang | 21e0958 | 2020-04-04 23:57:07 +0200 | [diff] [blame] | 6312 | if (mddev->event_work.func) |
| 6313 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6314 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6315 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6316 | spin_unlock(&mddev->lock); |
| 6317 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 6318 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6319 | if (pers->sync_request && mddev->to_remove == NULL) |
| 6320 | mddev->to_remove = &md_redundancy_group; |
| 6321 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 6322 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Jack Wang | 6aaa58c | 2018-10-19 16:21:31 +0200 | [diff] [blame] | 6323 | } |
| 6324 | |
| 6325 | void md_stop(struct mddev *mddev) |
| 6326 | { |
| 6327 | /* stop the array and free an attached data structures. |
| 6328 | * This is called from dm-raid |
| 6329 | */ |
| 6330 | __md_stop(mddev); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 6331 | bioset_exit(&mddev->bio_set); |
| 6332 | bioset_exit(&mddev->sync_set); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6333 | } |
| 6334 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6335 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6336 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6337 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6338 | { |
| 6339 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6340 | int did_freeze = 0; |
| 6341 | |
| 6342 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 6343 | did_freeze = 1; |
| 6344 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 6345 | md_wakeup_thread(mddev->thread); |
| 6346 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6347 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6348 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6349 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6350 | /* Thread might be blocked waiting for metadata update |
| 6351 | * which will now never happen */ |
| 6352 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6353 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6354 | if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 6355 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6356 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6357 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 6358 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 6359 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6360 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6361 | mddev_lock_nointr(mddev); |
| 6362 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6363 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6364 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6365 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6366 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6367 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6368 | if (did_freeze) { |
| 6369 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6370 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6371 | md_wakeup_thread(mddev->thread); |
| 6372 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6373 | err = -EBUSY; |
| 6374 | goto out; |
| 6375 | } |
| 6376 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6377 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6378 | |
| 6379 | err = -ENXIO; |
| 6380 | if (mddev->ro==1) |
| 6381 | goto out; |
| 6382 | mddev->ro = 1; |
| 6383 | set_disk_ro(mddev->gendisk, 1); |
| 6384 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6385 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6386 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6387 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6388 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6389 | } |
| 6390 | out: |
| 6391 | mutex_unlock(&mddev->open_mutex); |
| 6392 | return err; |
| 6393 | } |
| 6394 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6395 | /* mode: |
| 6396 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6397 | * 2 - stop but do not disassemble array |
| 6398 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6399 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6400 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6401 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6403 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6404 | int did_freeze = 0; |
| 6405 | |
| 6406 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 6407 | did_freeze = 1; |
| 6408 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 6409 | md_wakeup_thread(mddev->thread); |
| 6410 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6411 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6412 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6413 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6414 | /* Thread might be blocked waiting for metadata update |
| 6415 | * which will now never happen */ |
| 6416 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6417 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6418 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6419 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 6420 | !test_bit(MD_RECOVERY_RUNNING, |
| 6421 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6422 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6424 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6425 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6426 | mddev->sysfs_active || |
| 6427 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6428 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6429 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6430 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6431 | if (did_freeze) { |
| 6432 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6433 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6434 | md_wakeup_thread(mddev->thread); |
| 6435 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6436 | return -EBUSY; |
| 6437 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6438 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6439 | if (mddev->ro) |
| 6440 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6441 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6442 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6443 | __md_stop(mddev); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 6444 | mddev->queue->backing_dev_info->congested_fn = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6445 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6446 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6447 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 6448 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6449 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 6450 | if (rdev->raid_disk >= 0) |
| 6451 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 6452 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6453 | set_capacity(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6454 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6455 | mddev->changed = 1; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6456 | revalidate_disk(disk); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 6457 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6458 | if (mddev->ro) |
| 6459 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6460 | } else |
| 6461 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6462 | /* |
| 6463 | * Free resources if final stop |
| 6464 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6465 | if (mode == 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6466 | pr_info("md: %s stopped.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6468 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6469 | struct file *f = mddev->bitmap_info.file; |
| 6470 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6471 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6472 | spin_unlock(&mddev->lock); |
| 6473 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 6474 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6475 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 6476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | export_array(mddev); |
| 6478 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6479 | md_clean(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6480 | if (mddev->hold_active == UNTIL_STOP) |
| 6481 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6482 | } |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6483 | md_new_event(mddev); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6484 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6485 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | } |
| 6487 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6488 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6489 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6490 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6491 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6492 | int err; |
| 6493 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 6494 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6497 | pr_info("md: running: "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6498 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6499 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | char b[BDEVNAME_SIZE]; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6501 | pr_cont("<%s>", bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6503 | pr_cont("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6505 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6507 | pr_warn("md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6508 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6509 | } |
| 6510 | } |
| 6511 | |
| 6512 | /* |
| 6513 | * lets try to run arrays based on all disks that have arrived |
| 6514 | * until now. (those are in pending_raid_disks) |
| 6515 | * |
| 6516 | * the method: pick the first pending disk, collect all disks with |
| 6517 | * the same UUID, remove all from the pending list and put them into |
| 6518 | * the 'same_array' list. Then order this list based on superblock |
| 6519 | * update time (freshest comes first), kick out 'old' disks and |
| 6520 | * compare superblocks. If everything's fine then run it. |
| 6521 | * |
| 6522 | * If "unit" is allocated, then bump its reference count |
| 6523 | */ |
| 6524 | static void autorun_devices(int part) |
| 6525 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6526 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6527 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6528 | char b[BDEVNAME_SIZE]; |
| 6529 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6530 | pr_info("md: autorun ...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6531 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6532 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 6534 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6536 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6537 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6538 | pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6540 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6541 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6542 | pr_debug("md: adding %s ...\n", |
| 6543 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6544 | list_move(&rdev->same_set, &candidates); |
| 6545 | } |
| 6546 | /* |
| 6547 | * now we have a set of devices, with all of them having |
| 6548 | * mostly sane superblocks. It's time to allocate the |
| 6549 | * mddev. |
| 6550 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6551 | if (part) { |
| 6552 | dev = MKDEV(mdp_major, |
| 6553 | rdev0->preferred_minor << MdpMinorShift); |
| 6554 | unit = MINOR(dev) >> MdpMinorShift; |
| 6555 | } else { |
| 6556 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 6557 | unit = MINOR(dev); |
| 6558 | } |
| 6559 | if (rdev0->preferred_minor != unit) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6560 | pr_warn("md: unit number in %s is bad: %d\n", |
| 6561 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6562 | break; |
| 6563 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6564 | |
| 6565 | md_probe(dev, NULL, NULL); |
| 6566 | mddev = mddev_find(dev); |
Neil Brown | 9bbbca3 | 2008-06-28 08:31:17 +1000 | [diff] [blame] | 6567 | if (!mddev || !mddev->gendisk) { |
| 6568 | if (mddev) |
| 6569 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | break; |
| 6571 | } |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6572 | if (mddev_lock(mddev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6573 | pr_warn("md: %s locked, cannot run\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | else if (mddev->raid_disks || mddev->major_version |
| 6575 | || !list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6576 | pr_warn("md: %s already running, cannot run %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6577 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 6578 | mddev_unlock(mddev); |
| 6579 | } else { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6580 | pr_debug("md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 6581 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6582 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6583 | list_del_init(&rdev->same_set); |
| 6584 | if (bind_rdev_to_array(rdev, mddev)) |
| 6585 | export_rdev(rdev); |
| 6586 | } |
| 6587 | autorun_array(mddev); |
| 6588 | mddev_unlock(mddev); |
| 6589 | } |
| 6590 | /* on success, candidates will be empty, on error |
| 6591 | * it won't... |
| 6592 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6593 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6594 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6595 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6596 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | mddev_put(mddev); |
| 6598 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6599 | pr_info("md: ... autorun DONE.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6600 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6601 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6602 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6603 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6604 | { |
| 6605 | mdu_version_t ver; |
| 6606 | |
| 6607 | ver.major = MD_MAJOR_VERSION; |
| 6608 | ver.minor = MD_MINOR_VERSION; |
| 6609 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 6610 | |
| 6611 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 6612 | return -EFAULT; |
| 6613 | |
| 6614 | return 0; |
| 6615 | } |
| 6616 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6617 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6618 | { |
| 6619 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6620 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6621 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6622 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6623 | nr = working = insync = failed = spare = 0; |
| 6624 | rcu_read_lock(); |
| 6625 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6626 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6627 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6628 | failed++; |
| 6629 | else { |
| 6630 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6631 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6632 | insync++; |
Song Liu | b347af8 | 2016-08-11 17:14:45 -0700 | [diff] [blame] | 6633 | else if (test_bit(Journal, &rdev->flags)) |
| 6634 | /* TODO: add journal count to md_u.h */ |
| 6635 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6636 | else |
| 6637 | spare++; |
| 6638 | } |
| 6639 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6640 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6641 | |
| 6642 | info.major_version = mddev->major_version; |
| 6643 | info.minor_version = mddev->minor_version; |
| 6644 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6645 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6646 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6647 | info.size = mddev->dev_sectors / 2; |
| 6648 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 6649 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | info.nr_disks = nr; |
| 6651 | info.raid_disks = mddev->raid_disks; |
| 6652 | info.md_minor = mddev->md_minor; |
| 6653 | info.not_persistent= !mddev->persistent; |
| 6654 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6655 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | info.state = 0; |
| 6657 | if (mddev->in_sync) |
| 6658 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6659 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 6660 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 6661 | if (mddev_is_clustered(mddev)) |
| 6662 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6663 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6664 | info.working_disks = working; |
| 6665 | info.failed_disks = failed; |
| 6666 | info.spare_disks = spare; |
| 6667 | |
| 6668 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6669 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6670 | |
| 6671 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6672 | return -EFAULT; |
| 6673 | |
| 6674 | return 0; |
| 6675 | } |
| 6676 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6677 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6678 | { |
| 6679 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6680 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6681 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6682 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6683 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6684 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6685 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6686 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6687 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6688 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6689 | /* bitmap enabled */ |
| 6690 | if (mddev->bitmap_info.file) { |
| 6691 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 6692 | sizeof(file->pathname)); |
| 6693 | if (IS_ERR(ptr)) |
| 6694 | err = PTR_ERR(ptr); |
| 6695 | else |
| 6696 | memmove(file->pathname, ptr, |
| 6697 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 6698 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6699 | spin_unlock(&mddev->lock); |
| 6700 | |
| 6701 | if (err == 0 && |
| 6702 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6703 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6704 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6705 | kfree(file); |
| 6706 | return err; |
| 6707 | } |
| 6708 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6709 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6710 | { |
| 6711 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6712 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6713 | |
| 6714 | if (copy_from_user(&info, arg, sizeof(info))) |
| 6715 | return -EFAULT; |
| 6716 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6717 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 6718 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6719 | if (rdev) { |
| 6720 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 6721 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 6722 | info.raid_disk = rdev->raid_disk; |
| 6723 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6724 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6726 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | info.state |= (1<<MD_DISK_ACTIVE); |
| 6728 | info.state |= (1<<MD_DISK_SYNC); |
| 6729 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 6730 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6731 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6732 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6733 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6734 | if (test_bit(FailFast, &rdev->flags)) |
| 6735 | info.state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6736 | } else { |
| 6737 | info.major = info.minor = 0; |
| 6738 | info.raid_disk = -1; |
| 6739 | info.state = (1<<MD_DISK_REMOVED); |
| 6740 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6741 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6742 | |
| 6743 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6744 | return -EFAULT; |
| 6745 | |
| 6746 | return 0; |
| 6747 | } |
| 6748 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6749 | 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] | 6750 | { |
| 6751 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6752 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6753 | dev_t dev = MKDEV(info->major,info->minor); |
| 6754 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6755 | if (mddev_is_clustered(mddev) && |
| 6756 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6757 | pr_warn("%s: Cannot add to clustered mddev.\n", |
| 6758 | mdname(mddev)); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6759 | return -EINVAL; |
| 6760 | } |
| 6761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 6763 | return -EOVERFLOW; |
| 6764 | |
| 6765 | if (!mddev->raid_disks) { |
| 6766 | int err; |
| 6767 | /* expecting a device which has a superblock */ |
| 6768 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 6769 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6770 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6771 | PTR_ERR(rdev)); |
| 6772 | return PTR_ERR(rdev); |
| 6773 | } |
| 6774 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6775 | struct md_rdev *rdev0 |
| 6776 | = list_entry(mddev->disks.next, |
| 6777 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6778 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6779 | .load_super(rdev, rdev0, mddev->minor_version); |
| 6780 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6781 | pr_warn("md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6782 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | bdevname(rdev0->bdev,b2)); |
| 6784 | export_rdev(rdev); |
| 6785 | return -EINVAL; |
| 6786 | } |
| 6787 | } |
| 6788 | err = bind_rdev_to_array(rdev, mddev); |
| 6789 | if (err) |
| 6790 | export_rdev(rdev); |
| 6791 | return err; |
| 6792 | } |
| 6793 | |
| 6794 | /* |
| 6795 | * add_new_disk can be used once the array is assembled |
| 6796 | * to add "hot spares". They must already have a superblock |
| 6797 | * written |
| 6798 | */ |
| 6799 | if (mddev->pers) { |
| 6800 | int err; |
| 6801 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6802 | pr_warn("%s: personality does not support diskops!\n", |
| 6803 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6804 | return -EINVAL; |
| 6805 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6806 | if (mddev->persistent) |
| 6807 | rdev = md_import_device(dev, mddev->major_version, |
| 6808 | mddev->minor_version); |
| 6809 | else |
| 6810 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6811 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6812 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | PTR_ERR(rdev)); |
| 6814 | return PTR_ERR(rdev); |
| 6815 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6816 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6817 | if (!mddev->persistent) { |
| 6818 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6819 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6820 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6821 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6822 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6823 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6824 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6825 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6826 | } else |
| 6827 | super_types[mddev->major_version]. |
| 6828 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6829 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6830 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6831 | /* This was a hot-add request, but events doesn't |
| 6832 | * match, so reject it. |
| 6833 | */ |
| 6834 | export_rdev(rdev); |
| 6835 | return -EINVAL; |
| 6836 | } |
| 6837 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6838 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6839 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6840 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6841 | else |
| 6842 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6843 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6844 | set_bit(FailFast, &rdev->flags); |
| 6845 | else |
| 6846 | clear_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6847 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6848 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6849 | struct md_rdev *rdev2; |
| 6850 | bool has_journal = false; |
| 6851 | |
| 6852 | /* make sure no existing journal disk */ |
| 6853 | rdev_for_each(rdev2, mddev) { |
| 6854 | if (test_bit(Journal, &rdev2->flags)) { |
| 6855 | has_journal = true; |
| 6856 | break; |
| 6857 | } |
| 6858 | } |
NeilBrown | 230b55f | 2017-10-17 14:24:09 +1100 | [diff] [blame] | 6859 | if (has_journal || mddev->bitmap) { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6860 | export_rdev(rdev); |
| 6861 | return -EBUSY; |
| 6862 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6863 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6864 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6865 | /* |
| 6866 | * check whether the device shows up in other nodes |
| 6867 | */ |
| 6868 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6869 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6870 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6871 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6872 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6873 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6874 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6875 | export_rdev(rdev); |
| 6876 | return err; |
| 6877 | } |
| 6878 | } |
| 6879 | } |
| 6880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6881 | rdev->raid_disk = -1; |
| 6882 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6884 | if (err) |
| 6885 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6886 | |
| 6887 | if (mddev_is_clustered(mddev)) { |
Guoqing Jiang | e566aef | 2016-08-12 13:42:34 +0800 | [diff] [blame] | 6888 | if (info->state & (1 << MD_DISK_CANDIDATE)) { |
| 6889 | if (!err) { |
| 6890 | err = md_cluster_ops->new_disk_ack(mddev, |
| 6891 | err == 0); |
| 6892 | if (err) |
| 6893 | md_kick_rdev_from_array(rdev); |
| 6894 | } |
| 6895 | } else { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6896 | if (err) |
| 6897 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6898 | else |
| 6899 | err = add_bound_rdev(rdev); |
| 6900 | } |
| 6901 | |
| 6902 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6903 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | return err; |
| 6906 | } |
| 6907 | |
| 6908 | /* otherwise, add_new_disk is only allowed |
| 6909 | * for major_version==0 superblocks |
| 6910 | */ |
| 6911 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6912 | pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6913 | return -EINVAL; |
| 6914 | } |
| 6915 | |
| 6916 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6917 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6918 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6919 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6920 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | PTR_ERR(rdev)); |
| 6922 | return PTR_ERR(rdev); |
| 6923 | } |
| 6924 | rdev->desc_nr = info->number; |
| 6925 | if (info->raid_disk < mddev->raid_disks) |
| 6926 | rdev->raid_disk = info->raid_disk; |
| 6927 | else |
| 6928 | rdev->raid_disk = -1; |
| 6929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6930 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6931 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6932 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6933 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6934 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6935 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6936 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6937 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6938 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6939 | if (!mddev->persistent) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6940 | pr_debug("md: nonpersistent superblock ...\n"); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6941 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
| 6942 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6943 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6944 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6945 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6946 | err = bind_rdev_to_array(rdev, mddev); |
| 6947 | if (err) { |
| 6948 | export_rdev(rdev); |
| 6949 | return err; |
| 6950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | } |
| 6952 | |
| 6953 | return 0; |
| 6954 | } |
| 6955 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6956 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6957 | { |
| 6958 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6959 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6960 | |
Yufen Yu | c42a0e2 | 2018-05-04 18:08:10 +0800 | [diff] [blame] | 6961 | if (!mddev->pers) |
| 6962 | return -ENODEV; |
| 6963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | rdev = find_rdev(mddev, dev); |
| 6965 | if (!rdev) |
| 6966 | return -ENXIO; |
| 6967 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6968 | if (rdev->raid_disk < 0) |
| 6969 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6970 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6971 | clear_bit(Blocked, &rdev->flags); |
| 6972 | remove_and_add_spares(mddev, rdev); |
| 6973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6974 | if (rdev->raid_disk >= 0) |
| 6975 | goto busy; |
| 6976 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6977 | kick_rdev: |
Goldwyn Rodrigues | 54a8839 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 6978 | if (mddev_is_clustered(mddev)) |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6979 | md_cluster_ops->remove_disk(mddev, rdev); |
| 6980 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6981 | md_kick_rdev_from_array(rdev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6982 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 6983 | if (mddev->thread) |
| 6984 | md_wakeup_thread(mddev->thread); |
| 6985 | else |
| 6986 | md_update_sb(mddev, 1); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 6987 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6988 | |
| 6989 | return 0; |
| 6990 | busy: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6991 | pr_debug("md: cannot remove active disk %s from %s ...\n", |
| 6992 | bdevname(rdev->bdev,b), mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | return -EBUSY; |
| 6994 | } |
| 6995 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6996 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6997 | { |
| 6998 | char b[BDEVNAME_SIZE]; |
| 6999 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7000 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | |
| 7002 | if (!mddev->pers) |
| 7003 | return -ENODEV; |
| 7004 | |
| 7005 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7006 | pr_warn("%s: HOT_ADD may only be used with version-0 superblocks.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | mdname(mddev)); |
| 7008 | return -EINVAL; |
| 7009 | } |
| 7010 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7011 | pr_warn("%s: personality does not support diskops!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7012 | mdname(mddev)); |
| 7013 | return -EINVAL; |
| 7014 | } |
| 7015 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7016 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7017 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7018 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7019 | PTR_ERR(rdev)); |
| 7020 | return -EINVAL; |
| 7021 | } |
| 7022 | |
| 7023 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7024 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7025 | else |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 7026 | rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7027 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 7028 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7029 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7030 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7031 | pr_warn("md: can not hot-add faulty %s disk to %s!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7032 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 7033 | err = -EINVAL; |
| 7034 | goto abort_export; |
| 7035 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7036 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7037 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7038 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 7039 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 7040 | err = bind_rdev_to_array(rdev, mddev); |
| 7041 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 7042 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7043 | |
| 7044 | /* |
| 7045 | * The rest should better be atomic, we can have disk failures |
| 7046 | * noticed in interrupt contexts ... |
| 7047 | */ |
| 7048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | rdev->raid_disk = -1; |
| 7050 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7051 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 7052 | if (!mddev->thread) |
| 7053 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7054 | /* |
| 7055 | * Kick recovery, maybe this spare has to be added to the |
| 7056 | * array immediately. |
| 7057 | */ |
| 7058 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7059 | md_wakeup_thread(mddev->thread); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 7060 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7061 | return 0; |
| 7062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7063 | abort_export: |
| 7064 | export_rdev(rdev); |
| 7065 | return err; |
| 7066 | } |
| 7067 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7068 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7069 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7070 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7071 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7072 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 7073 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7074 | return -EBUSY; |
| 7075 | if (mddev->recovery || mddev->sync_thread) |
| 7076 | return -EBUSY; |
| 7077 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7078 | } |
| 7079 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7080 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7081 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7082 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7083 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7084 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 7085 | return -EEXIST; /* cannot add when bitmap is present */ |
| 7086 | f = fget(fd); |
| 7087 | |
| 7088 | if (f == NULL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7089 | pr_warn("%s: error: failed to get bitmap file\n", |
| 7090 | mdname(mddev)); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7091 | return -EBADF; |
| 7092 | } |
| 7093 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7094 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7095 | if (!S_ISREG(inode->i_mode)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7096 | pr_warn("%s: error: bitmap file must be a regular file\n", |
| 7097 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7098 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7099 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7100 | pr_warn("%s: error: bitmap file must open for write\n", |
| 7101 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7102 | err = -EBADF; |
| 7103 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7104 | pr_warn("%s: error: bitmap file is already in use\n", |
| 7105 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7106 | err = -EBUSY; |
| 7107 | } |
| 7108 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7109 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7110 | return err; |
| 7111 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7112 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7113 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7114 | } else if (mddev->bitmap == NULL) |
| 7115 | return -ENOENT; /* cannot remove what isn't there */ |
| 7116 | err = 0; |
| 7117 | if (mddev->pers) { |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 7118 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7119 | struct bitmap *bitmap; |
| 7120 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7121 | bitmap = md_bitmap_create(mddev, -1); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7122 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7123 | if (!IS_ERR(bitmap)) { |
| 7124 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7125 | err = md_bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 7126 | } else |
| 7127 | err = PTR_ERR(bitmap); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7128 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7129 | md_bitmap_destroy(mddev); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7130 | fd = -1; |
| 7131 | } |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7132 | mddev_resume(mddev); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7133 | } else if (fd < 0) { |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7134 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7135 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7136 | mddev_resume(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 7137 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 7138 | } |
| 7139 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7140 | struct file *f = mddev->bitmap_info.file; |
| 7141 | if (f) { |
| 7142 | spin_lock(&mddev->lock); |
| 7143 | mddev->bitmap_info.file = NULL; |
| 7144 | spin_unlock(&mddev->lock); |
| 7145 | fput(f); |
| 7146 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7147 | } |
| 7148 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7149 | return err; |
| 7150 | } |
| 7151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | /* |
| 7153 | * set_array_info is used two different ways |
| 7154 | * The original usage is when creating a new array. |
| 7155 | * In this usage, raid_disks is > 0 and it together with |
| 7156 | * level, size, not_persistent,layout,chunksize determine the |
| 7157 | * shape of the array. |
| 7158 | * This will always create an array with a type-0.90.0 superblock. |
| 7159 | * The newer usage is when assembling an array. |
| 7160 | * In this case raid_disks will be 0, and the major_version field is |
| 7161 | * use to determine which style super-blocks are to be found on the devices. |
| 7162 | * The minor and patch _version numbers are also kept incase the |
| 7163 | * super_block handler wishes to interpret them. |
| 7164 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7165 | 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] | 7166 | { |
| 7167 | |
| 7168 | if (info->raid_disks == 0) { |
| 7169 | /* just setting version number for superblock loading */ |
| 7170 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 7171 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7172 | super_types[info->major_version].name == NULL) { |
| 7173 | /* maybe try to auto-load a module? */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7174 | pr_warn("md: superblock version %d not known\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7175 | info->major_version); |
| 7176 | return -EINVAL; |
| 7177 | } |
| 7178 | mddev->major_version = info->major_version; |
| 7179 | mddev->minor_version = info->minor_version; |
| 7180 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7181 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 7182 | /* ensure mddev_put doesn't delete this now that there |
| 7183 | * is some minimal configuration. |
| 7184 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7185 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7186 | return 0; |
| 7187 | } |
| 7188 | mddev->major_version = MD_MAJOR_VERSION; |
| 7189 | mddev->minor_version = MD_MINOR_VERSION; |
| 7190 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7191 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | |
| 7193 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 7194 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7195 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7196 | mddev->raid_disks = info->raid_disks; |
| 7197 | /* don't set md_minor, it is determined by which /dev/md* was |
| 7198 | * openned |
| 7199 | */ |
| 7200 | if (info->state & (1<<MD_SB_CLEAN)) |
| 7201 | mddev->recovery_cp = MaxSector; |
| 7202 | else |
| 7203 | mddev->recovery_cp = 0; |
| 7204 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7205 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | |
| 7207 | mddev->layout = info->layout; |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 7208 | if (mddev->level == 0) |
| 7209 | /* Cannot trust RAID0 layout info here */ |
| 7210 | mddev->layout = -1; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7211 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7212 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7213 | if (mddev->persistent) { |
NeilBrown | 1b3bae4 | 2017-03-01 07:31:28 +1100 | [diff] [blame] | 7214 | mddev->max_disks = MD_SB_DISKS; |
| 7215 | mddev->flags = 0; |
| 7216 | mddev->sb_flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7217 | } |
| 7218 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7219 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7220 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 7221 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7222 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 7223 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7224 | mddev->reshape_position = MaxSector; |
| 7225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7226 | /* |
| 7227 | * Generate a 128 bit UUID |
| 7228 | */ |
| 7229 | get_random_bytes(mddev->uuid, 16); |
| 7230 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7231 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7232 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7233 | mddev->new_layout = mddev->layout; |
| 7234 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7235 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7237 | return 0; |
| 7238 | } |
| 7239 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7240 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 7241 | { |
Shaohua Li | efa4b77 | 2017-10-18 22:08:13 -0700 | [diff] [blame] | 7242 | lockdep_assert_held(&mddev->reconfig_mutex); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 7243 | |
| 7244 | if (mddev->external_size) |
| 7245 | return; |
| 7246 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 7247 | mddev->array_sectors = array_sectors; |
| 7248 | } |
| 7249 | EXPORT_SYMBOL(md_set_array_sectors); |
| 7250 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7251 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7252 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7253 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7254 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7255 | int fit = (num_sectors == 0); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 7256 | sector_t old_dev_sectors = mddev->dev_sectors; |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 7257 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7258 | if (mddev->pers->resize == NULL) |
| 7259 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7260 | /* The "num_sectors" is the number of sectors of each device that |
| 7261 | * is used. This can only make sense for arrays with redundancy. |
| 7262 | * linear and raid0 always use whatever space is available. We can only |
| 7263 | * consider changing this number if no resync or reconstruction is |
| 7264 | * 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] | 7265 | * 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] | 7266 | * of each device. If num_sectors is zero, we find the largest size |
| 7267 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7268 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7269 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 7270 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7271 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 7272 | if (mddev->ro) |
| 7273 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7274 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7275 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7276 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 7277 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7278 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 7279 | num_sectors = avail; |
| 7280 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7281 | return -ENOSPC; |
| 7282 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7283 | rv = mddev->pers->resize(mddev, num_sectors); |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 7284 | if (!rv) { |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 7285 | if (mddev_is_clustered(mddev)) |
| 7286 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
| 7287 | else if (mddev->queue) { |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 7288 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 7289 | revalidate_disk(mddev->gendisk); |
| 7290 | } |
| 7291 | } |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7292 | return rv; |
| 7293 | } |
| 7294 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7295 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7296 | { |
| 7297 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7298 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7299 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7300 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7301 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 7302 | if (mddev->ro) |
| 7303 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7304 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 7305 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7306 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7307 | if (mddev->sync_thread || |
| 7308 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 7309 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7310 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7311 | |
| 7312 | rdev_for_each(rdev, mddev) { |
| 7313 | if (mddev->raid_disks < raid_disks && |
| 7314 | rdev->data_offset < rdev->new_data_offset) |
| 7315 | return -EINVAL; |
| 7316 | if (mddev->raid_disks > raid_disks && |
| 7317 | rdev->data_offset > rdev->new_data_offset) |
| 7318 | return -EINVAL; |
| 7319 | } |
| 7320 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7321 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7322 | if (mddev->delta_disks < 0) |
| 7323 | mddev->reshape_backwards = 1; |
| 7324 | else if (mddev->delta_disks > 0) |
| 7325 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7326 | |
| 7327 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7328 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 7329 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7330 | mddev->reshape_backwards = 0; |
| 7331 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7332 | return rv; |
| 7333 | } |
| 7334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7335 | /* |
| 7336 | * update_array_info is used to change the configuration of an |
| 7337 | * on-line array. |
| 7338 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 7339 | * fields in the info are checked against the array. |
| 7340 | * Any differences that cannot be handled will cause an error. |
| 7341 | * Normally, only one change can be managed at a time. |
| 7342 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7343 | 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] | 7344 | { |
| 7345 | int rv = 0; |
| 7346 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7347 | int state = 0; |
| 7348 | |
| 7349 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7350 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7351 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7352 | |
| 7353 | if (mddev->major_version != info->major_version || |
| 7354 | mddev->minor_version != info->minor_version || |
| 7355 | /* mddev->patch_version != info->patch_version || */ |
| 7356 | mddev->ctime != info->ctime || |
| 7357 | mddev->level != info->level || |
| 7358 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 7359 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7360 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7361 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 7362 | ((state^info->state) & 0xfffffe00) |
| 7363 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7364 | return -EINVAL; |
| 7365 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7366 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 7367 | cnt++; |
| 7368 | if (mddev->raid_disks != info->raid_disks) |
| 7369 | cnt++; |
| 7370 | if (mddev->layout != info->layout) |
| 7371 | cnt++; |
| 7372 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 7373 | cnt++; |
| 7374 | if (cnt == 0) |
| 7375 | return 0; |
| 7376 | if (cnt > 1) |
| 7377 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7378 | |
| 7379 | if (mddev->layout != info->layout) { |
| 7380 | /* Change layout |
| 7381 | * we don't need to do anything at the md level, the |
| 7382 | * personality will take care of it all. |
| 7383 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7384 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7385 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7386 | else { |
| 7387 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7388 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7389 | if (rv) |
| 7390 | mddev->new_layout = mddev->layout; |
| 7391 | return rv; |
| 7392 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7393 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7394 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7395 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7396 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7397 | if (mddev->raid_disks != info->raid_disks) |
| 7398 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7399 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7400 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7401 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 7402 | rv = -EINVAL; |
| 7403 | goto err; |
| 7404 | } |
| 7405 | if (mddev->recovery || mddev->sync_thread) { |
| 7406 | rv = -EBUSY; |
| 7407 | goto err; |
| 7408 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7409 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7410 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7411 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7412 | if (mddev->bitmap) { |
| 7413 | rv = -EEXIST; |
| 7414 | goto err; |
| 7415 | } |
| 7416 | if (mddev->bitmap_info.default_offset == 0) { |
| 7417 | rv = -EINVAL; |
| 7418 | goto err; |
| 7419 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7420 | mddev->bitmap_info.offset = |
| 7421 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 7422 | mddev->bitmap_info.space = |
| 7423 | mddev->bitmap_info.default_space; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7424 | bitmap = md_bitmap_create(mddev, -1); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7425 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7426 | if (!IS_ERR(bitmap)) { |
| 7427 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7428 | rv = md_bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 7429 | } else |
| 7430 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7431 | if (rv) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7432 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7433 | mddev_resume(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7434 | } else { |
| 7435 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7436 | if (!mddev->bitmap) { |
| 7437 | rv = -ENOENT; |
| 7438 | goto err; |
| 7439 | } |
| 7440 | if (mddev->bitmap->storage.file) { |
| 7441 | rv = -EINVAL; |
| 7442 | goto err; |
| 7443 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7444 | if (mddev->bitmap_info.nodes) { |
| 7445 | /* hold PW on all the bitmap lock */ |
| 7446 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7447 | pr_warn("md: can't change bitmap to none since the array is in use by more than one node\n"); |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7448 | rv = -EPERM; |
| 7449 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 7450 | goto err; |
| 7451 | } |
| 7452 | |
| 7453 | mddev->bitmap_info.nodes = 0; |
| 7454 | md_cluster_ops->leave(mddev); |
Zhao Heming | edee9df | 2020-07-21 02:08:53 +0800 | [diff] [blame] | 7455 | module_put(md_cluster_mod); |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 7456 | mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY; |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7457 | } |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7458 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7459 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7460 | mddev_resume(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7461 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7462 | } |
| 7463 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7464 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7465 | return rv; |
| 7466 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7467 | return rv; |
| 7468 | } |
| 7469 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7470 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7471 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7472 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7473 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7474 | |
| 7475 | if (mddev->pers == NULL) |
| 7476 | return -ENODEV; |
| 7477 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7478 | rcu_read_lock(); |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 7479 | rdev = md_find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7480 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7481 | err = -ENODEV; |
| 7482 | else { |
| 7483 | md_error(mddev, rdev); |
| 7484 | if (!test_bit(Faulty, &rdev->flags)) |
| 7485 | err = -EBUSY; |
| 7486 | } |
| 7487 | rcu_read_unlock(); |
| 7488 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7489 | } |
| 7490 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 7491 | /* |
| 7492 | * We have a problem here : there is no easy way to give a CHS |
| 7493 | * virtual geometry. We currently pretend that we have a 2 heads |
| 7494 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 7495 | * dosfs just mad... ;-) |
| 7496 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7497 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 7498 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7499 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7500 | |
| 7501 | geo->heads = 2; |
| 7502 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 7503 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7504 | return 0; |
| 7505 | } |
| 7506 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7507 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 7508 | { |
| 7509 | switch (cmd) { |
| 7510 | case ADD_NEW_DISK: |
| 7511 | case BLKROSET: |
| 7512 | case GET_ARRAY_INFO: |
| 7513 | case GET_BITMAP_FILE: |
| 7514 | case GET_DISK_INFO: |
| 7515 | case HOT_ADD_DISK: |
| 7516 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7517 | case RAID_AUTORUN: |
| 7518 | case RAID_VERSION: |
| 7519 | case RESTART_ARRAY_RW: |
| 7520 | case RUN_ARRAY: |
| 7521 | case SET_ARRAY_INFO: |
| 7522 | case SET_BITMAP_FILE: |
| 7523 | case SET_DISK_FAULTY: |
| 7524 | case STOP_ARRAY: |
| 7525 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7526 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7527 | return true; |
| 7528 | default: |
| 7529 | return false; |
| 7530 | } |
| 7531 | } |
| 7532 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7533 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7534 | unsigned int cmd, unsigned long arg) |
| 7535 | { |
| 7536 | int err = 0; |
| 7537 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7538 | struct mddev *mddev = NULL; |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 7539 | int ro; |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7540 | bool did_set_md_closing = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7541 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7542 | if (!md_ioctl_valid(cmd)) |
| 7543 | return -ENOTTY; |
| 7544 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 7545 | switch (cmd) { |
| 7546 | case RAID_VERSION: |
| 7547 | case GET_ARRAY_INFO: |
| 7548 | case GET_DISK_INFO: |
| 7549 | break; |
| 7550 | default: |
| 7551 | if (!capable(CAP_SYS_ADMIN)) |
| 7552 | return -EACCES; |
| 7553 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7554 | |
| 7555 | /* |
| 7556 | * Commands dealing with the RAID driver but not any |
| 7557 | * particular array: |
| 7558 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7559 | switch (cmd) { |
| 7560 | case RAID_VERSION: |
| 7561 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7562 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7564 | #ifndef MODULE |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7565 | case RAID_AUTORUN: |
| 7566 | err = 0; |
| 7567 | autostart_arrays(arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7568 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7569 | #endif |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7570 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7571 | } |
| 7572 | |
| 7573 | /* |
| 7574 | * Commands creating/starting a new array: |
| 7575 | */ |
| 7576 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7577 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7578 | |
| 7579 | if (!mddev) { |
| 7580 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7581 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7582 | } |
| 7583 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7584 | /* Some actions do not requires the mutex */ |
| 7585 | switch (cmd) { |
| 7586 | case GET_ARRAY_INFO: |
| 7587 | if (!mddev->raid_disks && !mddev->external) |
| 7588 | err = -ENODEV; |
| 7589 | else |
| 7590 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7591 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7592 | |
| 7593 | case GET_DISK_INFO: |
| 7594 | if (!mddev->raid_disks && !mddev->external) |
| 7595 | err = -ENODEV; |
| 7596 | else |
| 7597 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7598 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7599 | |
| 7600 | case SET_DISK_FAULTY: |
| 7601 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7602 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7603 | |
| 7604 | case GET_BITMAP_FILE: |
| 7605 | err = get_bitmap_file(mddev, argp); |
| 7606 | goto out; |
| 7607 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7608 | } |
| 7609 | |
Guoqing Jiang | 78b990c | 2020-04-04 23:57:10 +0200 | [diff] [blame] | 7610 | if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK) |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 7611 | flush_rdev_wq(mddev); |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 7612 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7613 | if (cmd == HOT_REMOVE_DISK) |
| 7614 | /* need to ensure recovery thread has run */ |
| 7615 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 7616 | !test_bit(MD_RECOVERY_NEEDED, |
Shaohua Li | 82a301c | 2016-12-08 15:48:18 -0800 | [diff] [blame] | 7617 | &mddev->recovery), |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7618 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7619 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 7620 | /* Need to flush page cache, and ensure no-one else opens |
| 7621 | * and writes |
| 7622 | */ |
| 7623 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 7624 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7625 | mutex_unlock(&mddev->open_mutex); |
| 7626 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7627 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7628 | } |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7629 | WARN_ON_ONCE(test_bit(MD_CLOSING, &mddev->flags)); |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7630 | set_bit(MD_CLOSING, &mddev->flags); |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7631 | did_set_md_closing = true; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7632 | mutex_unlock(&mddev->open_mutex); |
| 7633 | sync_blockdev(bdev); |
| 7634 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7635 | err = mddev_lock(mddev); |
| 7636 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7637 | pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 7638 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7639 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7640 | } |
| 7641 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7642 | if (cmd == SET_ARRAY_INFO) { |
| 7643 | mdu_array_info_t info; |
| 7644 | if (!arg) |
| 7645 | memset(&info, 0, sizeof(info)); |
| 7646 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 7647 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7648 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7649 | } |
| 7650 | if (mddev->pers) { |
| 7651 | err = update_array_info(mddev, &info); |
| 7652 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7653 | pr_warn("md: couldn't update array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7654 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7655 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7656 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7657 | } |
| 7658 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7659 | pr_warn("md: array %s already has disks!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7660 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7661 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7662 | } |
| 7663 | if (mddev->raid_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7664 | pr_warn("md: array %s already initialised!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7665 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7666 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7667 | } |
| 7668 | err = set_array_info(mddev, &info); |
| 7669 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7670 | pr_warn("md: couldn't set array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7671 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7672 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7673 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7674 | } |
| 7675 | |
| 7676 | /* |
| 7677 | * Commands querying/configuring an existing array: |
| 7678 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7679 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7680 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 7681 | if ((!mddev->raid_disks && !mddev->external) |
| 7682 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 7683 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 7684 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7685 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7686 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7687 | } |
| 7688 | |
| 7689 | /* |
| 7690 | * Commands even a read-only array can execute: |
| 7691 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7692 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7693 | case RESTART_ARRAY_RW: |
| 7694 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7695 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7696 | |
| 7697 | case STOP_ARRAY: |
| 7698 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7699 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7700 | |
| 7701 | case STOP_ARRAY_RO: |
| 7702 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7703 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7704 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7705 | case HOT_REMOVE_DISK: |
| 7706 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7707 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7708 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7709 | case ADD_NEW_DISK: |
| 7710 | /* We can support ADD_NEW_DISK on read-only arrays |
Wei Fang | 466ad29 | 2016-03-21 19:19:30 +0800 | [diff] [blame] | 7711 | * only if we are re-adding a preexisting device. |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7712 | * So require mddev->pers and MD_DISK_SYNC. |
| 7713 | */ |
| 7714 | if (mddev->pers) { |
| 7715 | mdu_disk_info_t info; |
| 7716 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7717 | err = -EFAULT; |
| 7718 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 7719 | /* Need to clear read-only for this */ |
| 7720 | break; |
| 7721 | else |
| 7722 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7723 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7724 | } |
| 7725 | break; |
| 7726 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7727 | case BLKROSET: |
| 7728 | if (get_user(ro, (int __user *)(arg))) { |
| 7729 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7730 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7731 | } |
| 7732 | err = -EINVAL; |
| 7733 | |
| 7734 | /* if the bdev is going readonly the value of mddev->ro |
| 7735 | * does not matter, no writes are coming |
| 7736 | */ |
| 7737 | if (ro) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7738 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7739 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7740 | /* are we are already prepared for writes? */ |
| 7741 | if (mddev->ro != 1) |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7742 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7743 | |
| 7744 | /* transitioning to readauto need only happen for |
| 7745 | * arrays that call md_write_start |
| 7746 | */ |
| 7747 | if (mddev->pers) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7748 | err = restart_array(mddev); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7749 | if (err == 0) { |
| 7750 | mddev->ro = 2; |
| 7751 | set_disk_ro(mddev->gendisk, 0); |
Dan Williams | e221835 | 2010-05-12 08:25:37 +1000 | [diff] [blame] | 7752 | } |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7753 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7754 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7755 | } |
| 7756 | |
| 7757 | /* |
| 7758 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7759 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7760 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 7761 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7762 | if (mddev->ro == 2) { |
| 7763 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7764 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7765 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7766 | /* mddev_unlock will wake thread */ |
| 7767 | /* If a device failed while we were read-only, we |
| 7768 | * need to make sure the metadata is updated now. |
| 7769 | */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7770 | if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) { |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7771 | mddev_unlock(mddev); |
| 7772 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7773 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) && |
| 7774 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 7775 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7776 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7777 | } else { |
| 7778 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7779 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7780 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7781 | } |
| 7782 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7783 | switch (cmd) { |
| 7784 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7785 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7786 | mdu_disk_info_t info; |
| 7787 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7788 | err = -EFAULT; |
| 7789 | else |
| 7790 | err = add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7791 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7792 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7793 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7794 | case CLUSTERED_DISK_NACK: |
| 7795 | if (mddev_is_clustered(mddev)) |
| 7796 | md_cluster_ops->new_disk_ack(mddev, false); |
| 7797 | else |
| 7798 | err = -EINVAL; |
| 7799 | goto unlock; |
| 7800 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7801 | case HOT_ADD_DISK: |
| 7802 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7803 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7804 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7805 | case RUN_ARRAY: |
| 7806 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7807 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7808 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7809 | case SET_BITMAP_FILE: |
| 7810 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7811 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7812 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7813 | default: |
| 7814 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7815 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7816 | } |
| 7817 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7818 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7819 | if (mddev->hold_active == UNTIL_IOCTL && |
| 7820 | err != -EINVAL) |
| 7821 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7822 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7823 | out: |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7824 | if(did_set_md_closing) |
| 7825 | clear_bit(MD_CLOSING, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7826 | return err; |
| 7827 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7828 | #ifdef CONFIG_COMPAT |
| 7829 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 7830 | unsigned int cmd, unsigned long arg) |
| 7831 | { |
| 7832 | switch (cmd) { |
| 7833 | case HOT_REMOVE_DISK: |
| 7834 | case HOT_ADD_DISK: |
| 7835 | case SET_DISK_FAULTY: |
| 7836 | case SET_BITMAP_FILE: |
| 7837 | /* These take in integer arg, do not convert */ |
| 7838 | break; |
| 7839 | default: |
| 7840 | arg = (unsigned long)compat_ptr(arg); |
| 7841 | break; |
| 7842 | } |
| 7843 | |
| 7844 | return md_ioctl(bdev, mode, cmd, arg); |
| 7845 | } |
| 7846 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7847 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7848 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7849 | { |
| 7850 | /* |
| 7851 | * Succeed if we can lock the mddev, which confirms that |
| 7852 | * it isn't being stopped right now. |
| 7853 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7854 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7855 | int err; |
| 7856 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7857 | if (!mddev) |
| 7858 | return -ENODEV; |
| 7859 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7860 | if (mddev->gendisk != bdev->bd_disk) { |
| 7861 | /* we are racing with mddev_put which is discarding this |
| 7862 | * bd_disk. |
| 7863 | */ |
| 7864 | mddev_put(mddev); |
| 7865 | /* Wait until bdev->bd_disk is definitely gone */ |
Guoqing Jiang | f6766ff | 2020-04-04 23:57:09 +0200 | [diff] [blame] | 7866 | if (work_pending(&mddev->del_work)) |
| 7867 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7868 | /* Then retry the open from the top */ |
| 7869 | return -ERESTARTSYS; |
| 7870 | } |
| 7871 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7872 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7873 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7874 | goto out; |
| 7875 | |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7876 | if (test_bit(MD_CLOSING, &mddev->flags)) { |
| 7877 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7878 | err = -ENODEV; |
| 7879 | goto out; |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7880 | } |
| 7881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7882 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7883 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7884 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7885 | |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7886 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7887 | out: |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7888 | if (err) |
| 7889 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7890 | return err; |
| 7891 | } |
| 7892 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7893 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7894 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7895 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7896 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7897 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7898 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7899 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7900 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7901 | |
| 7902 | static int md_media_changed(struct gendisk *disk) |
| 7903 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7904 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7905 | |
| 7906 | return mddev->changed; |
| 7907 | } |
| 7908 | |
| 7909 | static int md_revalidate(struct gendisk *disk) |
| 7910 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7911 | struct mddev *mddev = disk->private_data; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7912 | |
| 7913 | mddev->changed = 0; |
| 7914 | return 0; |
| 7915 | } |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 7916 | static const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7917 | { |
| 7918 | .owner = THIS_MODULE, |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 7919 | .submit_bio = md_submit_bio, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7920 | .open = md_open, |
| 7921 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7922 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7923 | #ifdef CONFIG_COMPAT |
| 7924 | .compat_ioctl = md_compat_ioctl, |
| 7925 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7926 | .getgeo = md_getgeo, |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7927 | .media_changed = md_media_changed, |
| 7928 | .revalidate_disk= md_revalidate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7929 | }; |
| 7930 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7931 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7932 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7933 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7935 | /* |
| 7936 | * md_thread is a 'system-thread', it's priority should be very |
| 7937 | * high. We avoid resource deadlocks individually in each |
| 7938 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7939 | * the very same RT priority as kswapd, thus we will never get |
| 7940 | * into a priority inversion deadlock. |
| 7941 | * |
| 7942 | * we definitely have to have equal or higher priority than |
| 7943 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7944 | * many dirty RAID5 blocks. |
| 7945 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7946 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7947 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7948 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7949 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7950 | /* We need to wait INTERRUPTIBLE so that |
| 7951 | * we don't add to the load-average. |
| 7952 | * That means we need to be sure no signals are |
| 7953 | * pending |
| 7954 | */ |
| 7955 | if (signal_pending(current)) |
| 7956 | flush_signals(current); |
| 7957 | |
| 7958 | wait_event_interruptible_timeout |
| 7959 | (thread->wqueue, |
| 7960 | test_bit(THREAD_WAKEUP, &thread->flags) |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7961 | || kthread_should_stop() || kthread_should_park(), |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7962 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7963 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7964 | clear_bit(THREAD_WAKEUP, &thread->flags); |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7965 | if (kthread_should_park()) |
| 7966 | kthread_parkme(); |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7967 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7968 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7969 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7971 | return 0; |
| 7972 | } |
| 7973 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7974 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7975 | { |
| 7976 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7977 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Guoqing Jiang | d1d9014 | 2017-10-09 10:32:48 +0800 | [diff] [blame] | 7978 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7979 | wake_up(&thread->wqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7980 | } |
| 7981 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7982 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7983 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7984 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7985 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7986 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7987 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7988 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7989 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7990 | if (!thread) |
| 7991 | return NULL; |
| 7992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7993 | init_waitqueue_head(&thread->wqueue); |
| 7994 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7995 | thread->run = run; |
| 7996 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7997 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7998 | thread->tsk = kthread_run(md_thread, thread, |
| 7999 | "%s_%s", |
| 8000 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 8001 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 8002 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8003 | kfree(thread); |
| 8004 | return NULL; |
| 8005 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8006 | return thread; |
| 8007 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8008 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8009 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 8010 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8011 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 8012 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8013 | if (!thread) |
| 8014 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 8015 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 8016 | /* Locking ensures that mddev_unlock does not wake_up a |
| 8017 | * non-existent thread |
| 8018 | */ |
| 8019 | spin_lock(&pers_lock); |
| 8020 | *threadp = NULL; |
| 8021 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 8022 | |
| 8023 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8024 | kfree(thread); |
| 8025 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8026 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8027 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8028 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8029 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8030 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8031 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 8032 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 8033 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8034 | return; |
| 8035 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 8036 | if (mddev->degraded) |
| 8037 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8038 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8039 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 8040 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8041 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 8042 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 8043 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | bb9ef71 | 2015-12-28 10:46:38 +0800 | [diff] [blame] | 8044 | md_new_event(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8045 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8046 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8047 | |
| 8048 | /* seq_file implementation /proc/mdstat */ |
| 8049 | |
| 8050 | static void status_unused(struct seq_file *seq) |
| 8051 | { |
| 8052 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8053 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8054 | |
| 8055 | seq_printf(seq, "unused devices: "); |
| 8056 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 8057 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8058 | char b[BDEVNAME_SIZE]; |
| 8059 | i++; |
| 8060 | seq_printf(seq, "%s ", |
| 8061 | bdevname(rdev->bdev,b)); |
| 8062 | } |
| 8063 | if (!i) |
| 8064 | seq_printf(seq, "<none>"); |
| 8065 | |
| 8066 | seq_printf(seq, "\n"); |
| 8067 | } |
| 8068 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8069 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8070 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8071 | sector_t max_sectors, resync, res; |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8072 | unsigned long dt, db = 0; |
| 8073 | sector_t rt, curr_mark_cnt, resync_mark_cnt; |
| 8074 | int scale, recovery_active; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8075 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8076 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8077 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8078 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8079 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8080 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8081 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8082 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8083 | resync = mddev->curr_resync; |
| 8084 | if (resync <= 3) { |
| 8085 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 8086 | /* Still cleaning up */ |
| 8087 | resync = max_sectors; |
Nate Dailey | d2e2ec8 | 2017-11-30 11:33:30 -0500 | [diff] [blame] | 8088 | } else if (resync > max_sectors) |
| 8089 | resync = max_sectors; |
| 8090 | else |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8091 | resync -= atomic_read(&mddev->recovery_active); |
| 8092 | |
| 8093 | if (resync == 0) { |
Guoqing Jiang | 0357ba2 | 2018-07-02 16:26:25 +0800 | [diff] [blame] | 8094 | if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) { |
| 8095 | struct md_rdev *rdev; |
| 8096 | |
| 8097 | rdev_for_each(rdev, mddev) |
| 8098 | if (rdev->raid_disk >= 0 && |
| 8099 | !test_bit(Faulty, &rdev->flags) && |
| 8100 | rdev->recovery_offset != MaxSector && |
| 8101 | rdev->recovery_offset) { |
| 8102 | seq_printf(seq, "\trecover=REMOTE"); |
| 8103 | return 1; |
| 8104 | } |
| 8105 | if (mddev->reshape_position != MaxSector) |
| 8106 | seq_printf(seq, "\treshape=REMOTE"); |
| 8107 | else |
| 8108 | seq_printf(seq, "\tresync=REMOTE"); |
| 8109 | return 1; |
| 8110 | } |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8111 | if (mddev->recovery_cp < MaxSector) { |
| 8112 | seq_printf(seq, "\tresync=PENDING"); |
| 8113 | return 1; |
| 8114 | } |
| 8115 | return 0; |
| 8116 | } |
| 8117 | if (resync < 3) { |
| 8118 | seq_printf(seq, "\tresync=DELAYED"); |
| 8119 | return 1; |
| 8120 | } |
| 8121 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8122 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8123 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8124 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8125 | * u32, as those are the requirements for sector_div. |
| 8126 | * Thus 'scale' must be at least 10 |
| 8127 | */ |
| 8128 | scale = 10; |
| 8129 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8130 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8131 | scale++; |
| 8132 | } |
| 8133 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8134 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8135 | |
| 8136 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8137 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8138 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8139 | seq_printf(seq, "["); |
| 8140 | for (i = 0; i < x; i++) |
| 8141 | seq_printf(seq, "="); |
| 8142 | seq_printf(seq, ">"); |
| 8143 | for (i = 0; i < y; i++) |
| 8144 | seq_printf(seq, "."); |
| 8145 | seq_printf(seq, "] "); |
| 8146 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8147 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 8148 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 8149 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8150 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 8151 | "check" : |
| 8152 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 8153 | "resync" : "recovery"))), |
| 8154 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8155 | (unsigned long long) resync/2, |
| 8156 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8157 | |
| 8158 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8159 | * dt: time from mark until now |
| 8160 | * db: blocks written from mark until now |
| 8161 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8162 | * |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8163 | * rt is a sector_t, which is always 64bit now. We are keeping |
| 8164 | * the original algorithm, but it is not really necessary. |
| 8165 | * |
| 8166 | * Original algorithm: |
| 8167 | * So we divide before multiply in case it is 32bit and close |
| 8168 | * to the limit. |
| 8169 | * We scale the divisor (db) by 32 to avoid losing precision |
| 8170 | * near the end of resync when the number of remaining sectors |
| 8171 | * is close to 'db'. |
| 8172 | * We then divide rt by 32 after multiplying by db to compensate. |
| 8173 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8174 | */ |
| 8175 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 8176 | if (!dt) dt++; |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8177 | |
| 8178 | curr_mark_cnt = mddev->curr_mark_cnt; |
| 8179 | recovery_active = atomic_read(&mddev->recovery_active); |
| 8180 | resync_mark_cnt = mddev->resync_mark_cnt; |
| 8181 | |
| 8182 | if (curr_mark_cnt >= (recovery_active + resync_mark_cnt)) |
| 8183 | db = curr_mark_cnt - (recovery_active + resync_mark_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8184 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8185 | rt = max_sectors - resync; /* number of remaining sectors */ |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8186 | rt = div64_u64(rt, db/32+1); |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8187 | rt *= dt; |
| 8188 | rt >>= 5; |
| 8189 | |
| 8190 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 8191 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8192 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8193 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8194 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8195 | } |
| 8196 | |
| 8197 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 8198 | { |
| 8199 | struct list_head *tmp; |
| 8200 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8201 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8202 | |
| 8203 | if (l >= 0x10000) |
| 8204 | return NULL; |
| 8205 | if (!l--) |
| 8206 | /* header */ |
| 8207 | return (void*)1; |
| 8208 | |
| 8209 | spin_lock(&all_mddevs_lock); |
| 8210 | list_for_each(tmp,&all_mddevs) |
| 8211 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8212 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8213 | mddev_get(mddev); |
| 8214 | spin_unlock(&all_mddevs_lock); |
| 8215 | return mddev; |
| 8216 | } |
| 8217 | spin_unlock(&all_mddevs_lock); |
| 8218 | if (!l--) |
| 8219 | return (void*)2;/* tail */ |
| 8220 | return NULL; |
| 8221 | } |
| 8222 | |
| 8223 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 8224 | { |
| 8225 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8226 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8228 | ++*pos; |
| 8229 | if (v == (void*)2) |
| 8230 | return NULL; |
| 8231 | |
| 8232 | spin_lock(&all_mddevs_lock); |
| 8233 | if (v == (void*)1) |
| 8234 | tmp = all_mddevs.next; |
| 8235 | else |
| 8236 | tmp = mddev->all_mddevs.next; |
| 8237 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8238 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8239 | else { |
| 8240 | next_mddev = (void*)2; |
| 8241 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8243 | spin_unlock(&all_mddevs_lock); |
| 8244 | |
| 8245 | if (v != (void*)1) |
| 8246 | mddev_put(mddev); |
| 8247 | return next_mddev; |
| 8248 | |
| 8249 | } |
| 8250 | |
| 8251 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 8252 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8253 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8254 | |
| 8255 | if (mddev && v != (void*)1 && v != (void*)2) |
| 8256 | mddev_put(mddev); |
| 8257 | } |
| 8258 | |
| 8259 | static int md_seq_show(struct seq_file *seq, void *v) |
| 8260 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8261 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8262 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8263 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8264 | |
| 8265 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8266 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8267 | seq_printf(seq, "Personalities : "); |
| 8268 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8269 | list_for_each_entry(pers, &pers_list, list) |
| 8270 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8271 | |
| 8272 | spin_unlock(&pers_lock); |
| 8273 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8274 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8275 | return 0; |
| 8276 | } |
| 8277 | if (v == (void*)2) { |
| 8278 | status_unused(seq); |
| 8279 | return 0; |
| 8280 | } |
| 8281 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8282 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8283 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 8284 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 8285 | mddev->pers ? "" : "in"); |
| 8286 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8287 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8288 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8289 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 8290 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8291 | seq_printf(seq, " %s", mddev->pers->name); |
| 8292 | } |
| 8293 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8294 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 8295 | rcu_read_lock(); |
| 8296 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8297 | char b[BDEVNAME_SIZE]; |
| 8298 | seq_printf(seq, " %s[%d]", |
| 8299 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 8300 | if (test_bit(WriteMostly, &rdev->flags)) |
| 8301 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 8302 | if (test_bit(Journal, &rdev->flags)) |
| 8303 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8304 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8305 | seq_printf(seq, "(F)"); |
| 8306 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8307 | } |
| 8308 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8309 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8310 | if (test_bit(Replacement, &rdev->flags)) |
| 8311 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8312 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8313 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 8314 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8315 | |
| 8316 | if (!list_empty(&mddev->disks)) { |
| 8317 | if (mddev->pers) |
| 8318 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 8319 | (unsigned long long) |
| 8320 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8321 | else |
| 8322 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8323 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8324 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8325 | if (mddev->persistent) { |
| 8326 | if (mddev->major_version != 0 || |
| 8327 | mddev->minor_version != 90) { |
| 8328 | seq_printf(seq," super %d.%d", |
| 8329 | mddev->major_version, |
| 8330 | mddev->minor_version); |
| 8331 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 8332 | } else if (mddev->external) |
| 8333 | seq_printf(seq, " super external:%s", |
| 8334 | mddev->metadata_type); |
| 8335 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8336 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8337 | |
| 8338 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8339 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8340 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8341 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8342 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8343 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8344 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 8345 | } else |
| 8346 | seq_printf(seq, "\n "); |
| 8347 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 8348 | md_bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8349 | |
| 8350 | seq_printf(seq, "\n"); |
| 8351 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8352 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8354 | return 0; |
| 8355 | } |
| 8356 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 8357 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8358 | .start = md_seq_start, |
| 8359 | .next = md_seq_next, |
| 8360 | .stop = md_seq_stop, |
| 8361 | .show = md_seq_show, |
| 8362 | }; |
| 8363 | |
| 8364 | static int md_seq_open(struct inode *inode, struct file *file) |
| 8365 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8366 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8367 | int error; |
| 8368 | |
| 8369 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8370 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8371 | return error; |
| 8372 | |
| 8373 | seq = file->private_data; |
| 8374 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8375 | return error; |
| 8376 | } |
| 8377 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8378 | static int md_unloading; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 8379 | static __poll_t mdstat_poll(struct file *filp, poll_table *wait) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8380 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8381 | struct seq_file *seq = filp->private_data; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 8382 | __poll_t mask; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8383 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8384 | if (md_unloading) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8385 | return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8386 | poll_wait(filp, &md_event_waiters, wait); |
| 8387 | |
| 8388 | /* always allow read */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8389 | mask = EPOLLIN | EPOLLRDNORM; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8390 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8391 | if (seq->poll_event != atomic_read(&md_event_count)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8392 | mask |= EPOLLERR | EPOLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8393 | return mask; |
| 8394 | } |
| 8395 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 8396 | static const struct proc_ops mdstat_proc_ops = { |
| 8397 | .proc_open = md_seq_open, |
| 8398 | .proc_read = seq_read, |
| 8399 | .proc_lseek = seq_lseek, |
| 8400 | .proc_release = seq_release, |
| 8401 | .proc_poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8402 | }; |
| 8403 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8404 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8405 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8406 | pr_debug("md: %s personality registered for level %d\n", |
| 8407 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8408 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8409 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8410 | spin_unlock(&pers_lock); |
| 8411 | return 0; |
| 8412 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8413 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8414 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8415 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8416 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8417 | pr_debug("md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8418 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8419 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8420 | spin_unlock(&pers_lock); |
| 8421 | return 0; |
| 8422 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8423 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8424 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8425 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 8426 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8427 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8428 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8429 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8430 | if (md_cluster_ops != NULL) |
| 8431 | ret = -EALREADY; |
| 8432 | else { |
| 8433 | md_cluster_ops = ops; |
| 8434 | md_cluster_mod = module; |
| 8435 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8436 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8437 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8438 | } |
| 8439 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 8440 | |
| 8441 | int unregister_md_cluster_operations(void) |
| 8442 | { |
| 8443 | spin_lock(&pers_lock); |
| 8444 | md_cluster_ops = NULL; |
| 8445 | spin_unlock(&pers_lock); |
| 8446 | return 0; |
| 8447 | } |
| 8448 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 8449 | |
| 8450 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 8451 | { |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 8452 | int ret; |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 8453 | if (!md_cluster_ops) |
| 8454 | request_module("md-cluster"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8455 | spin_lock(&pers_lock); |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 8456 | /* ensure module won't be unloaded */ |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8457 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8458 | pr_warn("can't find md-cluster module or get it's reference.\n"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8459 | spin_unlock(&pers_lock); |
| 8460 | return -ENOENT; |
| 8461 | } |
| 8462 | spin_unlock(&pers_lock); |
| 8463 | |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 8464 | ret = md_cluster_ops->join(mddev, nodes); |
| 8465 | if (!ret) |
| 8466 | mddev->safemode_delay = 0; |
| 8467 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8468 | } |
| 8469 | |
| 8470 | void md_cluster_stop(struct mddev *mddev) |
| 8471 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 8472 | if (!md_cluster_ops) |
| 8473 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8474 | md_cluster_ops->leave(mddev); |
| 8475 | module_put(md_cluster_mod); |
| 8476 | } |
| 8477 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8478 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8479 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8480 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8481 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8482 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8483 | |
| 8484 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 8485 | rcu_read_lock(); |
| 8486 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8487 | struct gendisk *disk = rdev->bdev->bd_contains->bd_disk; |
Michael Callahan | 59767fb | 2018-07-18 04:47:37 -0700 | [diff] [blame] | 8488 | curr_events = (int)part_stat_read_accum(&disk->part0, sectors) - |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8489 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 8490 | /* sync IO will cause sync_io to increase before the disk_stats |
| 8491 | * as sync_io is counted when a request starts, and |
| 8492 | * disk_stats is counted when it completes. |
| 8493 | * So resync activity will cause curr_events to be smaller than |
| 8494 | * when there was no such activity. |
| 8495 | * non-sync IO will cause disk_stat to increase without |
| 8496 | * increasing sync_io so curr_events will (eventually) |
| 8497 | * be larger than it was before. Once it becomes |
| 8498 | * substantially larger, the test below will cause |
| 8499 | * the array to appear non-idle, and resync will slow |
| 8500 | * down. |
| 8501 | * If there is a lot of outstanding resync activity when |
| 8502 | * we set last_event to curr_events, then all that activity |
| 8503 | * completing might cause the array to appear non-idle |
| 8504 | * and resync will be slowed down even though there might |
| 8505 | * not have been non-resync activity. This will only |
| 8506 | * happen once though. 'last_events' will soon reflect |
| 8507 | * the state where there is little or no outstanding |
| 8508 | * resync requests, and further resync activity will |
| 8509 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8510 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8511 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8512 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8513 | rdev->last_events = curr_events; |
| 8514 | idle = 0; |
| 8515 | } |
| 8516 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 8517 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8518 | return idle; |
| 8519 | } |
| 8520 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8521 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8522 | { |
| 8523 | /* another "blocks" (512byte) blocks have been synced */ |
| 8524 | atomic_sub(blocks, &mddev->recovery_active); |
| 8525 | wake_up(&mddev->recovery_wait); |
| 8526 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8527 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8528 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8529 | md_wakeup_thread(mddev->thread); |
| 8530 | // stop recovery, signal do_sync .... |
| 8531 | } |
| 8532 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8533 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8534 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8535 | /* md_write_start(mddev, bi) |
| 8536 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8537 | * in superblock) before writing, schedule a superblock update |
| 8538 | * and wait for it to complete. |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8539 | * A return value of 'false' means that the write wasn't recorded |
| 8540 | * and cannot proceed as the array is being suspend. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8541 | */ |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8542 | bool md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8543 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8544 | int did_change = 0; |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 8545 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8546 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8547 | return true; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8548 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8549 | BUG_ON(mddev->ro == 1); |
| 8550 | if (mddev->ro == 2) { |
| 8551 | /* need to switch to read/write */ |
| 8552 | mddev->ro = 0; |
| 8553 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8554 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 8555 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8556 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8557 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8558 | rcu_read_lock(); |
| 8559 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8560 | smp_mb(); /* Match smp_mb in set_in_sync() */ |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8561 | if (mddev->safemode == 1) |
| 8562 | mddev->safemode = 0; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8563 | /* sync_checkers is always 0 when writes_pending is in per-cpu mode */ |
NeilBrown | 81fe48e | 2017-08-08 16:56:36 +1000 | [diff] [blame] | 8564 | if (mddev->in_sync || mddev->sync_checkers) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8565 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8566 | if (mddev->in_sync) { |
| 8567 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8568 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8569 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8570 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8571 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8572 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8573 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8574 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8575 | rcu_read_unlock(); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8576 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8577 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 8578 | if (!mddev->has_superblocks) |
| 8579 | return true; |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 8580 | wait_event(mddev->sb_wait, |
NeilBrown | d47c8ad | 2017-10-05 16:23:16 +1100 | [diff] [blame] | 8581 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) || |
| 8582 | mddev->suspended); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8583 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
| 8584 | percpu_ref_put(&mddev->writes_pending); |
| 8585 | return false; |
| 8586 | } |
| 8587 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8588 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8589 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8590 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8591 | /* md_write_inc can only be called when md_write_start() has |
| 8592 | * already been called at least once of the current request. |
| 8593 | * It increments the counter and is useful when a single request |
| 8594 | * is split into several parts. Each part causes an increment and |
| 8595 | * so needs a matching md_write_end(). |
| 8596 | * Unlike md_write_start(), it is safe to call md_write_inc() inside |
| 8597 | * a spinlocked region. |
| 8598 | */ |
| 8599 | void md_write_inc(struct mddev *mddev, struct bio *bi) |
| 8600 | { |
| 8601 | if (bio_data_dir(bi) != WRITE) |
| 8602 | return; |
| 8603 | WARN_ON_ONCE(mddev->in_sync || mddev->ro); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8604 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8605 | } |
| 8606 | EXPORT_SYMBOL(md_write_inc); |
| 8607 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8608 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8609 | { |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8610 | percpu_ref_put(&mddev->writes_pending); |
| 8611 | |
| 8612 | if (mddev->safemode == 2) |
| 8613 | md_wakeup_thread(mddev->thread); |
| 8614 | else if (mddev->safemode_delay) |
| 8615 | /* The roundup() ensures this only performs locking once |
| 8616 | * every ->safemode_delay jiffies |
| 8617 | */ |
| 8618 | mod_timer(&mddev->safemode_timer, |
| 8619 | roundup(jiffies, mddev->safemode_delay) + |
| 8620 | mddev->safemode_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8621 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8622 | |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8623 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8624 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8625 | /* md_allow_write(mddev) |
| 8626 | * Calling this ensures that the array is marked 'active' so that writes |
| 8627 | * may proceed without blocking. It is important to call this before |
| 8628 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 8629 | * Must be called with mddev_lock held. |
| 8630 | */ |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8631 | void md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8632 | { |
| 8633 | if (!mddev->pers) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8634 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8635 | if (mddev->ro) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8636 | return; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 8637 | if (!mddev->pers->sync_request) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8638 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8639 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8640 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8641 | if (mddev->in_sync) { |
| 8642 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8643 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8644 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8645 | if (mddev->safemode_delay && |
| 8646 | mddev->safemode == 0) |
| 8647 | mddev->safemode = 1; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8648 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8649 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8650 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8651 | /* wait for the dirty state to be recorded in the metadata */ |
| 8652 | wait_event(mddev->sb_wait, |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8653 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8654 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8655 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8656 | } |
| 8657 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 8658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8659 | #define SYNC_MARKS 10 |
| 8660 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8661 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8662 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8663 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8664 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8665 | struct mddev *mddev2; |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8666 | unsigned int currspeed = 0, window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8667 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8668 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8669 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8670 | sector_t mark_cnt[SYNC_MARKS]; |
| 8671 | int last_mark,m; |
| 8672 | struct list_head *tmp; |
| 8673 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8674 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8675 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8676 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8677 | struct blk_plug plug; |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8678 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8679 | |
| 8680 | /* just incase thread restarts... */ |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8681 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
| 8682 | test_bit(MD_RECOVERY_WAIT, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8683 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8684 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 8685 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8686 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8687 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8688 | |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8689 | if (mddev_is_clustered(mddev)) { |
| 8690 | ret = md_cluster_ops->resync_start(mddev); |
| 8691 | if (ret) |
| 8692 | goto skip; |
| 8693 | |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8694 | set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags); |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8695 | if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8696 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || |
| 8697 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
| 8698 | && ((unsigned long long)mddev->curr_resync_completed |
| 8699 | < (unsigned long long)mddev->resync_max_sectors)) |
| 8700 | goto skip; |
| 8701 | } |
| 8702 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8703 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8704 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8705 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8706 | action = "check"; |
| 8707 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8708 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8709 | action = "repair"; |
| 8710 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8711 | desc = "resync"; |
| 8712 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 8713 | desc = "reshape"; |
| 8714 | else |
| 8715 | desc = "recovery"; |
| 8716 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8717 | mddev->last_sync_action = action ?: desc; |
| 8718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8719 | /* we overload curr_resync somewhat here. |
| 8720 | * 0 == not engaged in resync at all |
| 8721 | * 2 == checking that there is no conflict with another sync |
| 8722 | * 1 == like 2, but have yielded to allow conflicting resync to |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8723 | * commence |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8724 | * other == active in resync - this many blocks |
| 8725 | * |
| 8726 | * Before starting a resync we must have set curr_resync to |
| 8727 | * 2, and then checked that every "conflicting" array has curr_resync |
| 8728 | * less than ours. When we find one that is the same or higher |
| 8729 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 8730 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 8731 | * This will mean we have to start checking from the beginning again. |
| 8732 | * |
| 8733 | */ |
| 8734 | |
| 8735 | do { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8736 | int mddev2_minor = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8737 | mddev->curr_resync = 2; |
| 8738 | |
| 8739 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 8740 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8741 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8742 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8743 | if (mddev2 == mddev) |
| 8744 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 8745 | if (!mddev->parallel_resync |
| 8746 | && mddev2->curr_resync |
| 8747 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8748 | DEFINE_WAIT(wq); |
| 8749 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 8750 | /* arbitrarily yield */ |
| 8751 | mddev->curr_resync = 1; |
| 8752 | wake_up(&resync_wait); |
| 8753 | } |
| 8754 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 8755 | /* no need to wait here, we can wait the next |
| 8756 | * time 'round when curr_resync == 2 |
| 8757 | */ |
| 8758 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8759 | /* We need to wait 'interruptible' so as not to |
| 8760 | * contribute to the load average, and not to |
| 8761 | * be caught by 'softlockup' |
| 8762 | */ |
| 8763 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8764 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 8765 | mddev2->curr_resync >= mddev->curr_resync) { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8766 | if (mddev2_minor != mddev2->md_minor) { |
| 8767 | mddev2_minor = mddev2->md_minor; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8768 | pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n", |
| 8769 | desc, mdname(mddev), |
| 8770 | mdname(mddev2)); |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8771 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8772 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8773 | if (signal_pending(current)) |
| 8774 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8775 | schedule(); |
| 8776 | finish_wait(&resync_wait, &wq); |
| 8777 | goto try_again; |
| 8778 | } |
| 8779 | finish_wait(&resync_wait, &wq); |
| 8780 | } |
| 8781 | } |
| 8782 | } while (mddev->curr_resync < 2); |
| 8783 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8784 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8785 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8786 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8787 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8788 | */ |
| 8789 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 8790 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8791 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8792 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8793 | j = mddev->resync_min; |
| 8794 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8795 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8796 | |
Guoqing Jiang | cb9ee15 | 2018-10-18 16:37:47 +0800 | [diff] [blame] | 8797 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8798 | max_sectors = mddev->resync_max_sectors; |
Guoqing Jiang | cb9ee15 | 2018-10-18 16:37:47 +0800 | [diff] [blame] | 8799 | /* |
| 8800 | * If the original node aborts reshaping then we continue the |
| 8801 | * reshaping, so set j again to avoid restart reshape from the |
| 8802 | * first beginning |
| 8803 | */ |
| 8804 | if (mddev_is_clustered(mddev) && |
| 8805 | mddev->reshape_position != MaxSector) |
| 8806 | j = mddev->reshape_position; |
| 8807 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8808 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8809 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8810 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8811 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8812 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8813 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8814 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8815 | !test_bit(Faulty, &rdev->flags) && |
| 8816 | !test_bit(In_sync, &rdev->flags) && |
| 8817 | rdev->recovery_offset < j) |
| 8818 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8819 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 8820 | |
| 8821 | /* If there is a bitmap, we need to make sure all |
| 8822 | * writes that started before we added a spare |
| 8823 | * complete before we start doing a recovery. |
| 8824 | * Otherwise the write might complete and (via |
| 8825 | * bitmap_endwrite) set a bit in the bitmap after the |
| 8826 | * recovery has checked that bit and skipped that |
| 8827 | * region. |
| 8828 | */ |
| 8829 | if (mddev->bitmap) { |
| 8830 | mddev->pers->quiesce(mddev, 1); |
| 8831 | mddev->pers->quiesce(mddev, 0); |
| 8832 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8833 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8834 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8835 | pr_info("md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 8836 | pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev)); |
| 8837 | pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n", |
| 8838 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8839 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8840 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8841 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8842 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8843 | for (m = 0; m < SYNC_MARKS; m++) { |
| 8844 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8845 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8846 | } |
| 8847 | last_mark = 0; |
| 8848 | mddev->resync_mark = mark[last_mark]; |
| 8849 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 8850 | |
| 8851 | /* |
| 8852 | * Tune reconstruction: |
| 8853 | */ |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8854 | window = 32 * (PAGE_SIZE / 512); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8855 | pr_debug("md: using %dk window, over a total of %lluk.\n", |
| 8856 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8857 | |
| 8858 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8859 | last_check = 0; |
| 8860 | |
| 8861 | if (j>2) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8862 | pr_debug("md: resuming %s of %s from checkpoint.\n", |
| 8863 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8864 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8865 | } else |
| 8866 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8867 | mddev->curr_resync_completed = j; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 8868 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8869 | md_new_event(mddev); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8870 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8871 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8872 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8873 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8874 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8875 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8876 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8877 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8878 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8879 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 8880 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 8881 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8882 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8883 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 8884 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 8885 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8886 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8887 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8888 | wait_event(mddev->recovery_wait, |
| 8889 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8890 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8891 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 8892 | j > mddev->recovery_cp) |
| 8893 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8894 | update_time = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8895 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 8896 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8897 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8898 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8899 | while (j >= mddev->resync_max && |
| 8900 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8901 | /* As this condition is controlled by user-space, |
| 8902 | * we can block indefinitely, so use '_interruptible' |
| 8903 | * to avoid triggering warnings. |
| 8904 | */ |
| 8905 | flush_signals(current); /* just in case */ |
| 8906 | wait_event_interruptible(mddev->recovery_wait, |
| 8907 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8908 | || test_bit(MD_RECOVERY_INTR, |
| 8909 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8910 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8911 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8912 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8913 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8914 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8915 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8916 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8917 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8918 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8919 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8920 | |
| 8921 | if (!skipped) { /* actual IO requested */ |
| 8922 | io_sectors += sectors; |
| 8923 | atomic_add(sectors, &mddev->recovery_active); |
| 8924 | } |
| 8925 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8926 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8927 | break; |
| 8928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8929 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8930 | if (j > max_sectors) |
| 8931 | /* when skipping, extra large numbers can be returned. */ |
| 8932 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8933 | if (j > 2) |
| 8934 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8935 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8936 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8937 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8938 | * visible in /proc/mdstat |
| 8939 | */ |
| 8940 | md_new_event(mddev); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8941 | |
| 8942 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8943 | continue; |
| 8944 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8945 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8946 | repeat: |
| 8947 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8948 | /* step marks */ |
| 8949 | int next = (last_mark+1) % SYNC_MARKS; |
| 8950 | |
| 8951 | mddev->resync_mark = mark[next]; |
| 8952 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8953 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8954 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8955 | last_mark = next; |
| 8956 | } |
| 8957 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8958 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8959 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8960 | |
| 8961 | /* |
| 8962 | * this loop exits only if either when we are slower than |
| 8963 | * the 'hard' speed limit, or the system was IO-idle for |
| 8964 | * a jiffy. |
| 8965 | * the system might be non-idle CPU-wise, but we only care |
| 8966 | * about not overloading the IO subsystem. (things like an |
| 8967 | * e2fsck being done on the RAID array should execute fast) |
| 8968 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8969 | cond_resched(); |
| 8970 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8971 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8972 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8973 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8974 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8975 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8976 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8977 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8978 | goto repeat; |
| 8979 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8980 | if (!is_mddev_idle(mddev, 0)) { |
| 8981 | /* |
| 8982 | * Give other IO more of a chance. |
| 8983 | * The faster the devices, the less we wait. |
| 8984 | */ |
| 8985 | wait_event(mddev->recovery_wait, |
| 8986 | !atomic_read(&mddev->recovery_active)); |
| 8987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8988 | } |
| 8989 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8990 | pr_info("md: %s: %s %s.\n",mdname(mddev), desc, |
| 8991 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8992 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8993 | /* |
| 8994 | * this also signals 'finished resyncing' to md_stop |
| 8995 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8996 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8997 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 8998 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8999 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 9000 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 9001 | mddev->curr_resync > 3) { |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 9002 | mddev->curr_resync_completed = mddev->curr_resync; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9003 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 9004 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 9005 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9006 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9007 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 9008 | mddev->curr_resync > 3) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 9009 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 9010 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 9011 | if (mddev->curr_resync >= mddev->recovery_cp) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9012 | pr_debug("md: checkpointing %s of %s.\n", |
| 9013 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 9014 | if (test_bit(MD_RECOVERY_ERROR, |
| 9015 | &mddev->recovery)) |
| 9016 | mddev->recovery_cp = |
| 9017 | mddev->curr_resync_completed; |
| 9018 | else |
| 9019 | mddev->recovery_cp = |
| 9020 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 9021 | } |
| 9022 | } else |
| 9023 | mddev->recovery_cp = MaxSector; |
| 9024 | } else { |
| 9025 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 9026 | mddev->curr_resync = MaxSector; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 9027 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 9028 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) { |
| 9029 | rcu_read_lock(); |
| 9030 | rdev_for_each_rcu(rdev, mddev) |
| 9031 | if (rdev->raid_disk >= 0 && |
| 9032 | mddev->delta_disks >= 0 && |
| 9033 | !test_bit(Journal, &rdev->flags) && |
| 9034 | !test_bit(Faulty, &rdev->flags) && |
| 9035 | !test_bit(In_sync, &rdev->flags) && |
| 9036 | rdev->recovery_offset < mddev->curr_resync) |
| 9037 | rdev->recovery_offset = mddev->curr_resync; |
| 9038 | rcu_read_unlock(); |
| 9039 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 9040 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9041 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 9042 | skip: |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 9043 | /* set CHANGE_PENDING here since maybe another update is needed, |
| 9044 | * so other nodes are informed. It should be harmless for normal |
| 9045 | * raid */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9046 | set_mask_bits(&mddev->sb_flags, 0, |
| 9047 | BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS)); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 9048 | |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 9049 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 9050 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 9051 | mddev->delta_disks > 0 && |
| 9052 | mddev->pers->finish_reshape && |
| 9053 | mddev->pers->size && |
| 9054 | mddev->queue) { |
| 9055 | mddev_lock_nointr(mddev); |
| 9056 | md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0)); |
| 9057 | mddev_unlock(mddev); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9058 | if (!mddev_is_clustered(mddev)) { |
| 9059 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 9060 | revalidate_disk(mddev->gendisk); |
| 9061 | } |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 9062 | } |
| 9063 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9064 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 9065 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 9066 | /* We completed so min/max setting can be forgotten if used. */ |
| 9067 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 9068 | mddev->resync_min = 0; |
| 9069 | mddev->resync_max = MaxSector; |
| 9070 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 9071 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 9072 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9073 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9074 | spin_unlock(&mddev->lock); |
| 9075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9076 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9077 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 9078 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9079 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 9080 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9081 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9082 | static int remove_and_add_spares(struct mddev *mddev, |
| 9083 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9084 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9085 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9086 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 9087 | int removed = 0; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9088 | bool remove_some = false; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9089 | |
NeilBrown | 39772f0 | 2018-02-03 09:19:30 +1100 | [diff] [blame] | 9090 | if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 9091 | /* Mustn't remove devices when resync thread is running */ |
| 9092 | return 0; |
| 9093 | |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9094 | rdev_for_each(rdev, mddev) { |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9095 | if ((this == NULL || rdev == this) && |
| 9096 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9097 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9098 | test_bit(Faulty, &rdev->flags) && |
| 9099 | atomic_read(&rdev->nr_pending)==0) { |
| 9100 | /* Faulty non-Blocked devices with nr_pending == 0 |
| 9101 | * never get nr_pending incremented, |
| 9102 | * never get Faulty cleared, and never get Blocked set. |
| 9103 | * So we can synchronize_rcu now rather than once per device |
| 9104 | */ |
| 9105 | remove_some = true; |
| 9106 | set_bit(RemoveSynchronized, &rdev->flags); |
| 9107 | } |
| 9108 | } |
| 9109 | |
| 9110 | if (remove_some) |
| 9111 | synchronize_rcu(); |
| 9112 | rdev_for_each(rdev, mddev) { |
| 9113 | if ((this == NULL || rdev == this) && |
| 9114 | rdev->raid_disk >= 0 && |
| 9115 | !test_bit(Blocked, &rdev->flags) && |
| 9116 | ((test_bit(RemoveSynchronized, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 9117 | (!test_bit(In_sync, &rdev->flags) && |
| 9118 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9119 | atomic_read(&rdev->nr_pending)==0)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9120 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 9121 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 9122 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | 011abdc | 2018-04-26 14:46:29 +1000 | [diff] [blame] | 9123 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9124 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 9125 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9126 | } |
| 9127 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9128 | if (remove_some && test_bit(RemoveSynchronized, &rdev->flags)) |
| 9129 | clear_bit(RemoveSynchronized, &rdev->flags); |
| 9130 | } |
| 9131 | |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 9132 | if (removed && mddev->kobj.sd) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9133 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9134 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 9135 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9136 | goto no_add; |
| 9137 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 9138 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 9139 | if (this && this != rdev) |
| 9140 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9141 | if (test_bit(Candidate, &rdev->flags)) |
| 9142 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 9143 | if (rdev->raid_disk >= 0 && |
| 9144 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 9145 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 9146 | !test_bit(Faulty, &rdev->flags)) |
| 9147 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9148 | if (rdev->raid_disk >= 0) |
| 9149 | continue; |
| 9150 | if (test_bit(Faulty, &rdev->flags)) |
| 9151 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9152 | if (!test_bit(Journal, &rdev->flags)) { |
| 9153 | if (mddev->ro && |
| 9154 | ! (rdev->saved_raid_disk >= 0 && |
| 9155 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 9156 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9157 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9158 | rdev->recovery_offset = 0; |
| 9159 | } |
Guoqing Jiang | 3f79cc2 | 2020-04-04 23:57:11 +0200 | [diff] [blame] | 9160 | if (mddev->pers->hot_add_disk(mddev, rdev) == 0) { |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 9161 | /* failure here is OK */ |
| 9162 | sysfs_link_rdev(mddev, rdev); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9163 | if (!test_bit(Journal, &rdev->flags)) |
| 9164 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9165 | md_new_event(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9166 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9167 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9168 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9169 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 9170 | if (removed) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9171 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9172 | return spares; |
| 9173 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 9174 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9175 | static void md_start_sync(struct work_struct *ws) |
| 9176 | { |
| 9177 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 9178 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9179 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 9180 | mddev, |
| 9181 | "resync"); |
| 9182 | if (!mddev->sync_thread) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9183 | pr_warn("%s: could not start resync thread...\n", |
| 9184 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9185 | /* leave the spares where they are, it shouldn't hurt */ |
| 9186 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9187 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 9188 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 9189 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 9190 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9191 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9192 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 9193 | &mddev->recovery)) |
| 9194 | if (mddev->sysfs_action) |
| 9195 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9196 | } else |
| 9197 | md_wakeup_thread(mddev->sync_thread); |
| 9198 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9199 | md_new_event(mddev); |
| 9200 | } |
| 9201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9202 | /* |
| 9203 | * This routine is regularly called by all per-raid-array threads to |
| 9204 | * deal with generic issues like resync and super-block update. |
| 9205 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 9206 | * need this as they never do any recovery or update the superblock. |
| 9207 | * |
| 9208 | * It does not do any resync itself, but rather "forks" off other threads |
| 9209 | * to do that as needed. |
| 9210 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 9211 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9212 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9213 | * and wakeups up this thread which will reap the thread and finish up. |
| 9214 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 9215 | * |
| 9216 | * The overall approach is: |
| 9217 | * 1/ if the superblock needs updating, update it. |
| 9218 | * 2/ If a recovery thread is running, don't do anything else. |
| 9219 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 9220 | * 4/ If there are any faulty devices, remove them. |
| 9221 | * 5/ If array is degraded, try to add spares devices |
| 9222 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 9223 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9224 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9225 | { |
NeilBrown | 059421e | 2018-10-03 15:04:41 +1000 | [diff] [blame] | 9226 | if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) { |
| 9227 | /* Write superblock - thread that called mddev_suspend() |
| 9228 | * holds reconfig_mutex for us. |
| 9229 | */ |
| 9230 | set_bit(MD_UPDATING_SB, &mddev->flags); |
| 9231 | smp_mb__after_atomic(); |
| 9232 | if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags)) |
| 9233 | md_update_sb(mddev, 0); |
| 9234 | clear_bit_unlock(MD_UPDATING_SB, &mddev->flags); |
| 9235 | wake_up(&mddev->sb_wait); |
| 9236 | } |
| 9237 | |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 9238 | if (mddev->suspended) |
| 9239 | return; |
| 9240 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 9241 | if (mddev->bitmap) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9242 | md_bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9243 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9244 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 9245 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9246 | pr_debug("md: %s in immediate safe mode\n", |
| 9247 | mdname(mddev)); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9248 | mddev->safemode = 2; |
| 9249 | } |
| 9250 | flush_signals(current); |
| 9251 | } |
| 9252 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9253 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 9254 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9255 | if ( ! ( |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9256 | (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9257 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9258 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 9259 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 9260 | (mddev->safemode == 2 |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9261 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9262 | )) |
| 9263 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9264 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 9265 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9266 | int spares = 0; |
NeilBrown | 480523f | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 9267 | bool try_set_sync = mddev->safemode != 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9268 | |
Shaohua Li | afc1f55 | 2017-08-11 20:34:45 -0700 | [diff] [blame] | 9269 | if (!mddev->external && mddev->safemode == 1) |
NeilBrown | 33182d1 | 2017-08-08 16:56:36 +1000 | [diff] [blame] | 9270 | mddev->safemode = 0; |
| 9271 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9272 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 9273 | struct md_rdev *rdev; |
| 9274 | if (!mddev->external && mddev->in_sync) |
| 9275 | /* 'Blocked' flag not needed as failed devices |
| 9276 | * will be recorded if array switched to read/write. |
| 9277 | * Leaving it set will prevent the device |
| 9278 | * from being removed. |
| 9279 | */ |
| 9280 | rdev_for_each(rdev, mddev) |
| 9281 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9282 | /* On a read-only array we can: |
| 9283 | * - remove failed devices |
| 9284 | * - add already-in_sync devices if the array itself |
| 9285 | * is in-sync. |
| 9286 | * As we only add devices that are already in-sync, |
| 9287 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9288 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9289 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9290 | /* There is no thread, but we need to call |
| 9291 | * ->spare_active and clear saved_raid_disk |
| 9292 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 9293 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9294 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 9295 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9296 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9297 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9298 | goto unlock; |
| 9299 | } |
| 9300 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 9301 | if (mddev_is_clustered(mddev)) { |
| 9302 | struct md_rdev *rdev; |
| 9303 | /* kick the device if another node issued a |
| 9304 | * remove disk. |
| 9305 | */ |
| 9306 | rdev_for_each(rdev, mddev) { |
| 9307 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 9308 | rdev->raid_disk < 0) |
| 9309 | md_kick_rdev_from_array(rdev); |
| 9310 | } |
| 9311 | } |
| 9312 | |
NeilBrown | 480523f | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 9313 | if (try_set_sync && !mddev->external && !mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 9314 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 9315 | set_in_sync(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 9316 | spin_unlock(&mddev->lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9317 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9318 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9319 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 9320 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 9321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9322 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 9323 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 9324 | /* resync/recovery still happening */ |
| 9325 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9326 | goto unlock; |
| 9327 | } |
| 9328 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9329 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9330 | goto unlock; |
| 9331 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9332 | /* Set RUNNING before clearing NEEDED to avoid |
| 9333 | * any transients in the value of "sync_action". |
| 9334 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 9335 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9336 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9337 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9338 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9339 | /* Clear some bits that don't mean anything, but |
| 9340 | * might be left set |
| 9341 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9342 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 9343 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9344 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 9345 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 9346 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9347 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9348 | /* no recovery is running. |
| 9349 | * remove any failed drives, then |
| 9350 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 9351 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9352 | * the personality to fail the re-add. |
| 9353 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9354 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9355 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 9356 | if (mddev->pers->check_reshape == NULL || |
| 9357 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9358 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9359 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9360 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9361 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9362 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9363 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9364 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 9365 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9366 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9367 | } else if (mddev->recovery_cp < MaxSector) { |
| 9368 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9369 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9370 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 9371 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9372 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9374 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 9375 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 9376 | /* We are adding a device or devices to an array |
| 9377 | * which has the bitmap stored on all devices. |
| 9378 | * So make sure all bitmap pages get written |
| 9379 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9380 | md_bitmap_write_all(mddev->bitmap); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 9381 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9382 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 9383 | queue_work(md_misc_wq, &mddev->del_work); |
| 9384 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9385 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9386 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9387 | if (!mddev->sync_thread) { |
| 9388 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9389 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9390 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 9391 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 9392 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 9393 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9394 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9395 | unlock: |
| 9396 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9397 | mddev_unlock(mddev); |
| 9398 | } |
| 9399 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 9400 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9401 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9402 | void md_reap_sync_thread(struct mddev *mddev) |
| 9403 | { |
| 9404 | struct md_rdev *rdev; |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9405 | sector_t old_dev_sectors = mddev->dev_sectors; |
| 9406 | bool is_reshaped = false; |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9407 | |
| 9408 | /* resync has finished, collect result */ |
| 9409 | md_unregister_thread(&mddev->sync_thread); |
| 9410 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
Guoqing Jiang | 0d8ed0e9 | 2019-07-24 11:09:21 +0200 | [diff] [blame] | 9411 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 9412 | mddev->degraded != mddev->raid_disks) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9413 | /* success...*/ |
| 9414 | /* activate any spares */ |
| 9415 | if (mddev->pers->spare_active(mddev)) { |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9416 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9417 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9418 | } |
| 9419 | } |
| 9420 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9421 | mddev->pers->finish_reshape) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9422 | mddev->pers->finish_reshape(mddev); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9423 | if (mddev_is_clustered(mddev)) |
| 9424 | is_reshaped = true; |
| 9425 | } |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9426 | |
| 9427 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 9428 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9429 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 9430 | if (!mddev->degraded) |
| 9431 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9432 | rdev->saved_raid_disk = -1; |
| 9433 | |
| 9434 | md_update_sb(mddev, 1); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9435 | /* MD_SB_CHANGE_PENDING should be cleared by md_update_sb, so we can |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 9436 | * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by |
| 9437 | * clustered raid */ |
| 9438 | if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags)) |
| 9439 | md_cluster_ops->resync_finish(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9440 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 9441 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9442 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9443 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 9444 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 9445 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9446 | /* |
| 9447 | * We call md_cluster_ops->update_size here because sync_size could |
| 9448 | * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared, |
| 9449 | * so it is time to update size across cluster. |
| 9450 | */ |
| 9451 | if (mddev_is_clustered(mddev) && is_reshaped |
| 9452 | && !test_bit(MD_CLOSING, &mddev->flags)) |
| 9453 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9454 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9455 | /* flag recovery needed just to double check */ |
| 9456 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9457 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9458 | md_new_event(mddev); |
| 9459 | if (mddev->event_work.func) |
| 9460 | queue_work(md_misc_wq, &mddev->event_work); |
| 9461 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 9462 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9463 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9464 | 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] | 9465 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 9466 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9467 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 9468 | !test_bit(Blocked, &rdev->flags) && |
| 9469 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9470 | msecs_to_jiffies(5000)); |
| 9471 | rdev_dec_pending(rdev, mddev); |
| 9472 | } |
| 9473 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 9474 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9475 | void md_finish_reshape(struct mddev *mddev) |
| 9476 | { |
| 9477 | /* called be personality module when reshape completes. */ |
| 9478 | struct md_rdev *rdev; |
| 9479 | |
| 9480 | rdev_for_each(rdev, mddev) { |
| 9481 | if (rdev->data_offset > rdev->new_data_offset) |
| 9482 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 9483 | else |
| 9484 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 9485 | rdev->data_offset = rdev->new_data_offset; |
| 9486 | } |
| 9487 | } |
| 9488 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9489 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9490 | /* Bad block management */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9491 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9492 | /* Returns 1 on success, 0 on failure */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9493 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9494 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9495 | { |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 9496 | struct mddev *mddev = rdev->mddev; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9497 | int rv; |
| 9498 | if (is_new) |
| 9499 | s += rdev->new_data_offset; |
| 9500 | else |
| 9501 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9502 | rv = badblocks_set(&rdev->badblocks, s, sectors, 0); |
| 9503 | if (rv == 0) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9504 | /* Make sure they get written out promptly */ |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9505 | if (test_bit(ExternalBbl, &rdev->flags)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9506 | sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks); |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 9507 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9508 | set_mask_bits(&mddev->sb_flags, 0, |
| 9509 | BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9510 | md_wakeup_thread(rdev->mddev->thread); |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9511 | return 1; |
| 9512 | } else |
| 9513 | return 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9514 | } |
| 9515 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 9516 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9517 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 9518 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9519 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9520 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9521 | if (is_new) |
| 9522 | s += rdev->new_data_offset; |
| 9523 | else |
| 9524 | s += rdev->data_offset; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9525 | rv = badblocks_clear(&rdev->badblocks, s, sectors); |
| 9526 | if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9527 | sysfs_notify_dirent_safe(rdev->sysfs_badblocks); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9528 | return rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9529 | } |
| 9530 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 9531 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9532 | static int md_notify_reboot(struct notifier_block *this, |
| 9533 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9534 | { |
| 9535 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9536 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9537 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9538 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9539 | for_each_mddev(mddev, tmp) { |
| 9540 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 9541 | if (mddev->pers) |
| 9542 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 9543 | if (mddev->persistent) |
| 9544 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9545 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9546 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9547 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9548 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9549 | /* |
| 9550 | * certain more exotic SCSI devices are known to be |
| 9551 | * volatile wrt too early system reboots. While the |
| 9552 | * right place to handle this issue is the given |
| 9553 | * driver, we do want to have a safe RAID driver ... |
| 9554 | */ |
| 9555 | if (need_delay) |
| 9556 | mdelay(1000*1); |
| 9557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9558 | return NOTIFY_DONE; |
| 9559 | } |
| 9560 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9561 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9562 | .notifier_call = md_notify_reboot, |
| 9563 | .next = NULL, |
| 9564 | .priority = INT_MAX, /* before any real devices */ |
| 9565 | }; |
| 9566 | |
| 9567 | static void md_geninit(void) |
| 9568 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 9569 | 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] | 9570 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 9571 | proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9572 | } |
| 9573 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9574 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9575 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9576 | int ret = -ENOMEM; |
| 9577 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 9578 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9579 | if (!md_wq) |
| 9580 | goto err_wq; |
| 9581 | |
| 9582 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 9583 | if (!md_misc_wq) |
| 9584 | goto err_misc_wq; |
| 9585 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9586 | md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0); |
| 9587 | if (!md_misc_wq) |
| 9588 | goto err_rdev_misc_wq; |
| 9589 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9590 | if ((ret = register_blkdev(MD_MAJOR, "md")) < 0) |
| 9591 | goto err_md; |
| 9592 | |
| 9593 | if ((ret = register_blkdev(0, "mdp")) < 0) |
| 9594 | goto err_mdp; |
| 9595 | mdp_major = ret; |
| 9596 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9597 | blk_register_region(MKDEV(MD_MAJOR, 0), 512, THIS_MODULE, |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9598 | md_probe, NULL, NULL); |
| 9599 | blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9600 | md_probe, NULL, NULL); |
| 9601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9602 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 9603 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9604 | |
| 9605 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 9606 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9607 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9608 | err_mdp: |
| 9609 | unregister_blkdev(MD_MAJOR, "md"); |
| 9610 | err_md: |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9611 | destroy_workqueue(md_rdev_misc_wq); |
| 9612 | err_rdev_misc_wq: |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9613 | destroy_workqueue(md_misc_wq); |
| 9614 | err_misc_wq: |
| 9615 | destroy_workqueue(md_wq); |
| 9616 | err_wq: |
| 9617 | return ret; |
| 9618 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9619 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9620 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9621 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9622 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 9623 | struct md_rdev *rdev2; |
| 9624 | int role, ret; |
| 9625 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9626 | |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 9627 | /* |
| 9628 | * If size is changed in another node then we need to |
| 9629 | * do resize as well. |
| 9630 | */ |
| 9631 | if (mddev->dev_sectors != le64_to_cpu(sb->size)) { |
| 9632 | ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size)); |
| 9633 | if (ret) |
| 9634 | pr_info("md-cluster: resize failed\n"); |
| 9635 | else |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9636 | md_bitmap_update_sb(mddev->bitmap); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 9637 | } |
| 9638 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9639 | /* Check for change of roles in the active devices */ |
| 9640 | rdev_for_each(rdev2, mddev) { |
| 9641 | if (test_bit(Faulty, &rdev2->flags)) |
| 9642 | continue; |
| 9643 | |
| 9644 | /* Check if the roles changed */ |
| 9645 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9646 | |
| 9647 | if (test_bit(Candidate, &rdev2->flags)) { |
| 9648 | if (role == 0xfffe) { |
| 9649 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 9650 | md_kick_rdev_from_array(rdev2); |
| 9651 | continue; |
| 9652 | } |
| 9653 | else |
| 9654 | clear_bit(Candidate, &rdev2->flags); |
| 9655 | } |
| 9656 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9657 | if (role != rdev2->raid_disk) { |
Guoqing Jiang | ca1e98e | 2018-10-18 16:37:45 +0800 | [diff] [blame] | 9658 | /* |
| 9659 | * got activated except reshape is happening. |
| 9660 | */ |
| 9661 | if (rdev2->raid_disk == -1 && role != 0xffff && |
| 9662 | !(le32_to_cpu(sb->feature_map) & |
| 9663 | MD_FEATURE_RESHAPE_ACTIVE)) { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9664 | rdev2->saved_raid_disk = role; |
| 9665 | ret = remove_and_add_spares(mddev, rdev2); |
| 9666 | pr_info("Activated spare: %s\n", |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9667 | bdevname(rdev2->bdev,b)); |
Guoqing Jiang | a578183 | 2016-05-02 11:33:14 -0400 | [diff] [blame] | 9668 | /* wakeup mddev->thread here, so array could |
| 9669 | * perform resync with the new activated disk */ |
| 9670 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9671 | md_wakeup_thread(mddev->thread); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9672 | } |
| 9673 | /* device faulty |
| 9674 | * We just want to do the minimum to mark the disk |
| 9675 | * as faulty. The recovery is performed by the |
| 9676 | * one who initiated the error. |
| 9677 | */ |
| 9678 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9679 | md_error(mddev, rdev2); |
| 9680 | clear_bit(Blocked, &rdev2->flags); |
| 9681 | } |
| 9682 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9683 | } |
| 9684 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 9685 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) |
| 9686 | update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9687 | |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 9688 | /* |
| 9689 | * Since mddev->delta_disks has already updated in update_raid_disks, |
| 9690 | * so it is time to check reshape. |
| 9691 | */ |
| 9692 | if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) && |
| 9693 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 9694 | /* |
| 9695 | * reshape is happening in the remote node, we need to |
| 9696 | * update reshape_position and call start_reshape. |
| 9697 | */ |
Christoph Hellwig | ed4d0a4e | 2019-04-04 18:56:10 +0200 | [diff] [blame] | 9698 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 9699 | if (mddev->pers->update_reshape_pos) |
| 9700 | mddev->pers->update_reshape_pos(mddev); |
| 9701 | if (mddev->pers->start_reshape) |
| 9702 | mddev->pers->start_reshape(mddev); |
| 9703 | } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) && |
| 9704 | mddev->reshape_position != MaxSector && |
| 9705 | !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 9706 | /* reshape is just done in another node. */ |
| 9707 | mddev->reshape_position = MaxSector; |
| 9708 | if (mddev->pers->update_reshape_pos) |
| 9709 | mddev->pers->update_reshape_pos(mddev); |
| 9710 | } |
| 9711 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9712 | /* Finally set the event to be up to date */ |
| 9713 | mddev->events = le64_to_cpu(sb->events); |
| 9714 | } |
| 9715 | |
| 9716 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9717 | { |
| 9718 | int err; |
| 9719 | struct page *swapout = rdev->sb_page; |
| 9720 | struct mdp_superblock_1 *sb; |
| 9721 | |
| 9722 | /* Store the sb page of the rdev in the swapout temporary |
| 9723 | * variable in case we err in the future |
| 9724 | */ |
| 9725 | rdev->sb_page = NULL; |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9726 | err = alloc_disk_sb(rdev); |
| 9727 | if (err == 0) { |
| 9728 | ClearPageUptodate(rdev->sb_page); |
| 9729 | rdev->sb_loaded = 0; |
| 9730 | err = super_types[mddev->major_version]. |
| 9731 | load_super(rdev, NULL, mddev->minor_version); |
| 9732 | } |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9733 | if (err < 0) { |
| 9734 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9735 | __func__, __LINE__, rdev->desc_nr, err); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9736 | if (rdev->sb_page) |
| 9737 | put_page(rdev->sb_page); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9738 | rdev->sb_page = swapout; |
| 9739 | rdev->sb_loaded = 1; |
| 9740 | return err; |
| 9741 | } |
| 9742 | |
| 9743 | sb = page_address(rdev->sb_page); |
| 9744 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9745 | * is not set |
| 9746 | */ |
| 9747 | |
| 9748 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9749 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9750 | |
| 9751 | /* The other node finished recovery, call spare_active to set |
| 9752 | * device In_sync and mddev->degraded |
| 9753 | */ |
| 9754 | if (rdev->recovery_offset == MaxSector && |
| 9755 | !test_bit(In_sync, &rdev->flags) && |
| 9756 | mddev->pers->spare_active(mddev)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9757 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9758 | |
| 9759 | put_page(swapout); |
| 9760 | return 0; |
| 9761 | } |
| 9762 | |
| 9763 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9764 | { |
| 9765 | struct md_rdev *rdev; |
| 9766 | int err; |
| 9767 | |
| 9768 | /* Find the rdev */ |
| 9769 | rdev_for_each_rcu(rdev, mddev) { |
| 9770 | if (rdev->desc_nr == nr) |
| 9771 | break; |
| 9772 | } |
| 9773 | |
| 9774 | if (!rdev || rdev->desc_nr != nr) { |
| 9775 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9776 | return; |
| 9777 | } |
| 9778 | |
| 9779 | err = read_rdev(mddev, rdev); |
| 9780 | if (err < 0) |
| 9781 | return; |
| 9782 | |
| 9783 | check_sb_changes(mddev, rdev); |
| 9784 | |
| 9785 | /* Read all rdev's to update recovery_offset */ |
Guoqing Jiang | 0ea9924 | 2018-04-09 17:01:21 +0800 | [diff] [blame] | 9786 | rdev_for_each_rcu(rdev, mddev) { |
| 9787 | if (!test_bit(Faulty, &rdev->flags)) |
| 9788 | read_rdev(mddev, rdev); |
| 9789 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9790 | } |
| 9791 | EXPORT_SYMBOL(md_reload_sb); |
| 9792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9793 | #ifndef MODULE |
| 9794 | |
| 9795 | /* |
| 9796 | * Searches all registered partitions for autorun RAID arrays |
| 9797 | * at boot time. |
| 9798 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9799 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9800 | static DEFINE_MUTEX(detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9801 | static LIST_HEAD(all_detected_devices); |
| 9802 | struct detected_devices_node { |
| 9803 | struct list_head list; |
| 9804 | dev_t dev; |
| 9805 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9806 | |
| 9807 | void md_autodetect_dev(dev_t dev) |
| 9808 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9809 | struct detected_devices_node *node_detected_dev; |
| 9810 | |
| 9811 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9812 | if (node_detected_dev) { |
| 9813 | node_detected_dev->dev = dev; |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9814 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9815 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9816 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9818 | } |
| 9819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9820 | static void autostart_arrays(int part) |
| 9821 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9822 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9823 | struct detected_devices_node *node_detected_dev; |
| 9824 | dev_t dev; |
| 9825 | int i_scanned, i_passed; |
| 9826 | |
| 9827 | i_scanned = 0; |
| 9828 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9829 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9830 | pr_info("md: Autodetecting RAID arrays.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9831 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9832 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9833 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9834 | i_scanned++; |
| 9835 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9836 | struct detected_devices_node, list); |
| 9837 | list_del(&node_detected_dev->list); |
| 9838 | dev = node_detected_dev->dev; |
| 9839 | kfree(node_detected_dev); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9840 | mutex_unlock(&detected_devices_mutex); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9841 | rdev = md_import_device(dev,0, 90); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9842 | mutex_lock(&detected_devices_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9843 | if (IS_ERR(rdev)) |
| 9844 | continue; |
| 9845 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9846 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9847 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9848 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9849 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9850 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9851 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9852 | } |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9853 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9854 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9855 | pr_debug("md: Scanned %d and added %d devices.\n", i_scanned, i_passed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9856 | |
| 9857 | autorun_devices(part); |
| 9858 | } |
| 9859 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9860 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9861 | |
| 9862 | static __exit void md_exit(void) |
| 9863 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9864 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9865 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9866 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9867 | |
NeilBrown | af5628f | 2014-07-31 13:54:54 +1000 | [diff] [blame] | 9868 | blk_unregister_region(MKDEV(MD_MAJOR,0), 512); |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 9869 | blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9870 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9871 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9872 | unregister_blkdev(mdp_major, "mdp"); |
| 9873 | unregister_reboot_notifier(&md_notifier); |
| 9874 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9875 | |
| 9876 | /* We cannot unload the modules while some process is |
| 9877 | * waiting for us in select() or poll() - wake them up |
| 9878 | */ |
| 9879 | md_unloading = 1; |
| 9880 | while (waitqueue_active(&md_event_waiters)) { |
| 9881 | /* not safe to leave yet */ |
| 9882 | wake_up(&md_event_waiters); |
| 9883 | msleep(delay); |
| 9884 | delay += delay; |
| 9885 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9886 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9887 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9888 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9889 | export_array(mddev); |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9890 | mddev->ctime = 0; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9891 | mddev->hold_active = 0; |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9892 | /* |
| 9893 | * for_each_mddev() will call mddev_put() at the end of each |
| 9894 | * iteration. As the mddev is now fully clear, this will |
| 9895 | * schedule the mddev for destruction by a workqueue, and the |
| 9896 | * destroy_workqueue() below will wait for that to complete. |
| 9897 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9898 | } |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9899 | destroy_workqueue(md_rdev_misc_wq); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9900 | destroy_workqueue(md_misc_wq); |
| 9901 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9902 | } |
| 9903 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9904 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9905 | module_exit(md_exit) |
| 9906 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9907 | static int get_ro(char *buffer, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9908 | { |
Xiongfeng Wang | 3f99980 | 2020-05-11 16:23:25 +0800 | [diff] [blame] | 9909 | return sprintf(buffer, "%d\n", start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9910 | } |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9911 | static int set_ro(const char *val, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9912 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9913 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9914 | } |
| 9915 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9916 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9917 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9918 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 9919 | module_param(create_on_open, bool, S_IRUSR|S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9921 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9922 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9923 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9924 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |