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> |
Christoph Hellwig | fe45e63 | 2021-09-20 14:33:27 +0200 | [diff] [blame] | 44 | #include <linux/blk-integrity.h> |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 45 | #include <linux/badblocks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <linux/sysctl.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 47 | #include <linux/seq_file.h> |
Al Viro | ff01bb4 | 2011-09-16 02:31:11 -0400 | [diff] [blame] | 48 | #include <linux/fs.h> |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 49 | #include <linux/poll.h> |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 50 | #include <linux/ctype.h> |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 51 | #include <linux/string.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 52 | #include <linux/hdreg.h> |
| 53 | #include <linux/proc_fs.h> |
| 54 | #include <linux/random.h> |
Christoph Hellwig | b81e0c2 | 2021-09-20 14:33:25 +0200 | [diff] [blame] | 55 | #include <linux/major.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 56 | #include <linux/module.h> |
NeilBrown | fb4d8c7 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 57 | #include <linux/reboot.h> |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 58 | #include <linux/file.h> |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 59 | #include <linux/compat.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 60 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 61 | #include <linux/raid/md_p.h> |
| 62 | #include <linux/raid/md_u.h> |
Christoph Hellwig | 74cc979c | 2020-03-24 08:25:19 +0100 | [diff] [blame] | 63 | #include <linux/raid/detect.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 64 | #include <linux/slab.h> |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 65 | #include <linux/percpu-refcount.h> |
Christoph Hellwig | c6a564ff | 2020-03-25 16:48:42 +0100 | [diff] [blame] | 66 | #include <linux/part_stat.h> |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 67 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 68 | #include <trace/events/block.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 69 | #include "md.h" |
Mike Snitzer | 935fe09 | 2017-10-10 17:02:41 -0400 | [diff] [blame] | 70 | #include "md-bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 71 | #include "md-cluster.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 73 | /* pers_list is a list of registered personalities protected |
| 74 | * by pers_lock. |
| 75 | * pers_lock does extra service to protect accesses to |
| 76 | * mddev->thread when the mutex cannot be held. |
| 77 | */ |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 78 | static LIST_HEAD(pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static DEFINE_SPINLOCK(pers_lock); |
| 80 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 81 | static struct kobj_type md_ktype; |
| 82 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 83 | struct md_cluster_operations *md_cluster_ops; |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 84 | EXPORT_SYMBOL(md_cluster_ops); |
Christoph Hellwig | 2b598ee | 2019-04-04 18:56:14 +0200 | [diff] [blame] | 85 | static struct module *md_cluster_mod; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 86 | |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 87 | static DECLARE_WAIT_QUEUE_HEAD(resync_wait); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 88 | static struct workqueue_struct *md_wq; |
| 89 | static struct workqueue_struct *md_misc_wq; |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 90 | static struct workqueue_struct *md_rdev_misc_wq; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 91 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 92 | static int remove_and_add_spares(struct mddev *mddev, |
| 93 | struct md_rdev *this); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 94 | static void mddev_detach(struct mddev *mddev); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 97 | * Default number of read corrections we'll attempt on an rdev |
| 98 | * before ejecting it from the array. We divide the read error |
| 99 | * count by 2 for every hour elapsed between read errors. |
| 100 | */ |
| 101 | #define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20 |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 102 | /* Default safemode delay: 200 msec */ |
| 103 | #define DEFAULT_SAFEMODE_DELAY ((200 * HZ)/1000 +1) |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 104 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit' |
| 106 | * is 1000 KB/sec, so the extra system load does not show up that much. |
| 107 | * Increase it if you want to have more _guaranteed_ speed. Note that |
Adrian Bunk | 338cec3 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 108 | * the RAID driver will use the maximum available bandwidth if the IO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | * subsystem is idle. There is also an 'absolute maximum' reconstruction |
| 110 | * speed limit - in case reconstruction slows down your system despite |
| 111 | * idle IO detection. |
| 112 | * |
| 113 | * 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] | 114 | * or /sys/block/mdX/md/sync_speed_{min,max} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | */ |
| 116 | |
| 117 | static int sysctl_speed_limit_min = 1000; |
| 118 | static int sysctl_speed_limit_max = 200000; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 119 | static inline int speed_min(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 120 | { |
| 121 | return mddev->sync_speed_min ? |
| 122 | mddev->sync_speed_min : sysctl_speed_limit_min; |
| 123 | } |
| 124 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 125 | static inline int speed_max(struct mddev *mddev) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 126 | { |
| 127 | return mddev->sync_speed_max ? |
| 128 | mddev->sync_speed_max : sysctl_speed_limit_max; |
| 129 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 131 | static void rdev_uninit_serial(struct md_rdev *rdev) |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 132 | { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 133 | if (!test_and_clear_bit(CollisionCheck, &rdev->flags)) |
| 134 | return; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 135 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 136 | kvfree(rdev->serial); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 137 | rdev->serial = NULL; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 138 | } |
| 139 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 140 | static void rdevs_uninit_serial(struct mddev *mddev) |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 141 | { |
| 142 | struct md_rdev *rdev; |
| 143 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 144 | rdev_for_each(rdev, mddev) |
| 145 | rdev_uninit_serial(rdev); |
| 146 | } |
| 147 | |
| 148 | static int rdev_init_serial(struct md_rdev *rdev) |
| 149 | { |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 150 | /* serial_nums equals with BARRIER_BUCKETS_NR */ |
| 151 | int i, serial_nums = 1 << ((PAGE_SHIFT - ilog2(sizeof(atomic_t)))); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 152 | struct serial_in_rdev *serial = NULL; |
| 153 | |
| 154 | if (test_bit(CollisionCheck, &rdev->flags)) |
| 155 | return 0; |
| 156 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 157 | serial = kvmalloc(sizeof(struct serial_in_rdev) * serial_nums, |
| 158 | GFP_KERNEL); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 159 | if (!serial) |
| 160 | return -ENOMEM; |
| 161 | |
Guoqing Jiang | 025471f | 2019-12-23 10:49:01 +0100 | [diff] [blame] | 162 | for (i = 0; i < serial_nums; i++) { |
| 163 | struct serial_in_rdev *serial_tmp = &serial[i]; |
| 164 | |
| 165 | spin_lock_init(&serial_tmp->serial_lock); |
| 166 | serial_tmp->serial_rb = RB_ROOT_CACHED; |
| 167 | init_waitqueue_head(&serial_tmp->serial_io_wait); |
| 168 | } |
| 169 | |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 170 | rdev->serial = serial; |
| 171 | set_bit(CollisionCheck, &rdev->flags); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int rdevs_init_serial(struct mddev *mddev) |
| 177 | { |
| 178 | struct md_rdev *rdev; |
| 179 | int ret = 0; |
| 180 | |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 181 | rdev_for_each(rdev, mddev) { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 182 | ret = rdev_init_serial(rdev); |
| 183 | if (ret) |
| 184 | break; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 185 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 186 | |
| 187 | /* Free all resources if pool is not existed */ |
| 188 | if (ret && !mddev->serial_info_pool) |
| 189 | rdevs_uninit_serial(mddev); |
| 190 | |
| 191 | return ret; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 194 | /* |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 195 | * rdev needs to enable serial stuffs if it meets the conditions: |
| 196 | * 1. it is multi-queue device flaged with writemostly. |
| 197 | * 2. the write-behind mode is enabled. |
| 198 | */ |
| 199 | static int rdev_need_serial(struct md_rdev *rdev) |
| 200 | { |
| 201 | return (rdev && rdev->mddev->bitmap_info.max_write_behind > 0 && |
Christoph Hellwig | e556f6b | 2020-06-26 10:01:56 +0200 | [diff] [blame] | 202 | rdev->bdev->bd_disk->queue->nr_hw_queues != 1 && |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 203 | test_bit(WriteMostly, &rdev->flags)); |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Init resource for rdev(s), then create serial_info_pool if: |
| 208 | * 1. rdev is the first device which return true from rdev_enable_serial. |
| 209 | * 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] | 210 | */ |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 211 | void mddev_create_serial_pool(struct mddev *mddev, struct md_rdev *rdev, |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 212 | bool is_suspend) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 213 | { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 214 | int ret = 0; |
| 215 | |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 216 | if (rdev && !rdev_need_serial(rdev) && |
| 217 | !test_bit(CollisionCheck, &rdev->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 218 | return; |
| 219 | |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 220 | if (!is_suspend) |
| 221 | mddev_suspend(mddev); |
| 222 | |
| 223 | if (!rdev) |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 224 | ret = rdevs_init_serial(mddev); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 225 | else |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 226 | ret = rdev_init_serial(rdev); |
| 227 | if (ret) |
| 228 | goto abort; |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 229 | |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 230 | if (mddev->serial_info_pool == NULL) { |
Coly Li | 3024ba2 | 2020-04-09 22:17:23 +0800 | [diff] [blame] | 231 | /* |
| 232 | * already in memalloc noio context by |
| 233 | * mddev_suspend() |
| 234 | */ |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 235 | mddev->serial_info_pool = |
| 236 | mempool_create_kmalloc_pool(NR_SERIAL_INFOS, |
| 237 | sizeof(struct serial_info)); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 238 | if (!mddev->serial_info_pool) { |
| 239 | rdevs_uninit_serial(mddev); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 240 | pr_err("can't alloc memory pool for serialization\n"); |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 241 | } |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 242 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 243 | |
| 244 | abort: |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 245 | if (!is_suspend) |
| 246 | mddev_resume(mddev); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 247 | } |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 248 | |
| 249 | /* |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 250 | * Free resource from rdev(s), and destroy serial_info_pool under conditions: |
| 251 | * 1. rdev is the last device flaged with CollisionCheck. |
| 252 | * 2. when bitmap is destroyed while policy is not enabled. |
| 253 | * 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] | 254 | */ |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 255 | void mddev_destroy_serial_pool(struct mddev *mddev, struct md_rdev *rdev, |
| 256 | bool is_suspend) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 257 | { |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 258 | if (rdev && !test_bit(CollisionCheck, &rdev->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 259 | return; |
| 260 | |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 261 | if (mddev->serial_info_pool) { |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 262 | struct md_rdev *temp; |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 263 | int num = 0; /* used to track if other rdevs need the pool */ |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 264 | |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 265 | if (!is_suspend) |
| 266 | mddev_suspend(mddev); |
| 267 | rdev_for_each(temp, mddev) { |
| 268 | if (!rdev) { |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 269 | if (!mddev->serialize_policy || |
| 270 | !rdev_need_serial(temp)) |
| 271 | rdev_uninit_serial(temp); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 272 | else |
| 273 | num++; |
| 274 | } else if (temp != rdev && |
| 275 | test_bit(CollisionCheck, &temp->flags)) |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 276 | num++; |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (rdev) |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 280 | rdev_uninit_serial(rdev); |
Guoqing Jiang | de31ee9 | 2019-12-23 10:48:57 +0100 | [diff] [blame] | 281 | |
| 282 | if (num) |
| 283 | pr_info("The mempool could be used by other devices\n"); |
| 284 | else { |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 285 | mempool_destroy(mddev->serial_info_pool); |
| 286 | mddev->serial_info_pool = NULL; |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 287 | } |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 288 | if (!is_suspend) |
| 289 | mddev_resume(mddev); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | static struct ctl_table_header *raid_table_header; |
| 294 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 295 | static struct ctl_table raid_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | .procname = "speed_limit_min", |
| 298 | .data = &sysctl_speed_limit_min, |
| 299 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 300 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 301 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | }, |
| 303 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | .procname = "speed_limit_max", |
| 305 | .data = &sysctl_speed_limit_max, |
| 306 | .maxlen = sizeof(int), |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 307 | .mode = S_IRUGO|S_IWUSR, |
Eric W. Biederman | 6d45611 | 2009-11-16 03:11:48 -0800 | [diff] [blame] | 308 | .proc_handler = proc_dointvec, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 310 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | }; |
| 312 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 313 | static struct ctl_table raid_dir_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | .procname = "raid", |
| 316 | .maxlen = 0, |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 317 | .mode = S_IRUGO|S_IXUGO, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | .child = raid_table, |
| 319 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 320 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | }; |
| 322 | |
Joe Perches | 82592c3 | 2013-11-14 15:16:18 +1100 | [diff] [blame] | 323 | static struct ctl_table raid_root_table[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | .procname = "dev", |
| 326 | .maxlen = 0, |
| 327 | .mode = 0555, |
| 328 | .child = raid_dir_table, |
| 329 | }, |
Eric W. Biederman | 894d249 | 2009-11-05 14:34:02 -0800 | [diff] [blame] | 330 | { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | }; |
| 332 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 333 | static int start_readonly; |
| 334 | |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 335 | /* |
| 336 | * The original mechanism for creating an md device is to create |
| 337 | * a device node in /dev and to open it. This causes races with device-close. |
| 338 | * The preferred method is to write to the "new_array" module parameter. |
| 339 | * This can avoid races. |
| 340 | * Setting create_on_open to false disables the original mechanism |
| 341 | * so all the races disappear. |
| 342 | */ |
| 343 | static bool create_on_open = true; |
| 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | /* |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 346 | * We have a system wide 'event count' that is incremented |
| 347 | * on any 'interesting' event, and readers of /proc/mdstat |
| 348 | * can use 'poll' or 'select' to find out when the event |
| 349 | * count increases. |
| 350 | * |
| 351 | * Events are: |
| 352 | * start array, stop array, error, add device, remove device, |
| 353 | * start build, activate spare |
| 354 | */ |
NeilBrown | 2989ddb | 2006-01-06 00:20:43 -0800 | [diff] [blame] | 355 | static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 356 | static atomic_t md_event_count; |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 357 | void md_new_event(void) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 358 | { |
| 359 | atomic_inc(&md_event_count); |
| 360 | wake_up(&md_event_waiters); |
| 361 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 362 | EXPORT_SYMBOL_GPL(md_new_event); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 363 | |
| 364 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * Enables to iterate over all existing md arrays |
| 366 | * all_mddevs_lock protects this list. |
| 367 | */ |
| 368 | static LIST_HEAD(all_mddevs); |
| 369 | static DEFINE_SPINLOCK(all_mddevs_lock); |
| 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | /* |
| 372 | * iterates through all used mddevs in the system. |
| 373 | * We take care to grab the all_mddevs_lock whenever navigating |
| 374 | * the list, and to always hold a refcount when unlocked. |
| 375 | * Any code which breaks out of this loop while own |
| 376 | * a reference to the current mddev and must mddev_put it. |
| 377 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 378 | #define for_each_mddev(_mddev,_tmp) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | \ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 380 | for (({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 381 | _tmp = all_mddevs.next; \ |
| 382 | _mddev = NULL;}); \ |
| 383 | ({ if (_tmp != &all_mddevs) \ |
| 384 | mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | spin_unlock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 386 | if (_mddev) mddev_put(_mddev); \ |
| 387 | _mddev = list_entry(_tmp, struct mddev, all_mddevs); \ |
| 388 | _tmp != &all_mddevs;}); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | ({ spin_lock(&all_mddevs_lock); \ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 390 | _tmp = _tmp->next;}) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | ) |
| 392 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 393 | /* Rather than calling directly into the personality make_request function, |
| 394 | * IO requests come here first so that we can check if the device is |
| 395 | * being suspended pending a reconfiguration. |
| 396 | * We hold a refcount over the call to ->make_request. By the time that |
| 397 | * call has finished, the bio has been linked into some internal structure |
| 398 | * and so is visible to ->quiesce(), so we don't need the refcount any more. |
| 399 | */ |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 400 | static bool is_suspended(struct mddev *mddev, struct bio *bio) |
| 401 | { |
| 402 | if (mddev->suspended) |
| 403 | return true; |
| 404 | if (bio_data_dir(bio) != WRITE) |
| 405 | return false; |
| 406 | if (mddev->suspend_lo >= mddev->suspend_hi) |
| 407 | return false; |
| 408 | if (bio->bi_iter.bi_sector >= mddev->suspend_hi) |
| 409 | return false; |
| 410 | if (bio_end_sector(bio) < mddev->suspend_lo) |
| 411 | return false; |
| 412 | return true; |
| 413 | } |
| 414 | |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 415 | void md_handle_request(struct mddev *mddev, struct bio *bio) |
| 416 | { |
| 417 | check_suspended: |
| 418 | rcu_read_lock(); |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 419 | if (is_suspended(mddev, bio)) { |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 420 | DEFINE_WAIT(__wait); |
| 421 | for (;;) { |
| 422 | prepare_to_wait(&mddev->sb_wait, &__wait, |
| 423 | TASK_UNINTERRUPTIBLE); |
NeilBrown | b3143b9 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 424 | if (!is_suspended(mddev, bio)) |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 425 | break; |
| 426 | rcu_read_unlock(); |
| 427 | schedule(); |
| 428 | rcu_read_lock(); |
| 429 | } |
| 430 | finish_wait(&mddev->sb_wait, &__wait); |
| 431 | } |
| 432 | atomic_inc(&mddev->active_io); |
| 433 | rcu_read_unlock(); |
| 434 | |
| 435 | if (!mddev->pers->make_request(mddev, bio)) { |
| 436 | atomic_dec(&mddev->active_io); |
| 437 | wake_up(&mddev->sb_wait); |
| 438 | goto check_suspended; |
| 439 | } |
| 440 | |
| 441 | if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended) |
| 442 | wake_up(&mddev->sb_wait); |
| 443 | } |
| 444 | EXPORT_SYMBOL(md_handle_request); |
| 445 | |
Christoph Hellwig | 3e08773 | 2021-10-12 13:12:24 +0200 | [diff] [blame] | 446 | static void md_submit_bio(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | { |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 448 | const int rw = bio_data_dir(bio); |
Christoph Hellwig | 309dca30 | 2021-01-24 11:02:34 +0100 | [diff] [blame] | 449 | struct mddev *mddev = bio->bi_bdev->bd_disk->private_data; |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 450 | |
Colin Ian King | 9a5a859 | 2020-07-02 12:35:02 +0100 | [diff] [blame] | 451 | if (mddev == NULL || mddev->pers == NULL) { |
| 452 | bio_io_error(bio); |
Christoph Hellwig | 3e08773 | 2021-10-12 13:12:24 +0200 | [diff] [blame] | 453 | return; |
Colin Ian King | 9a5a859 | 2020-07-02 12:35:02 +0100 | [diff] [blame] | 454 | } |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 455 | |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 456 | if (unlikely(test_bit(MD_BROKEN, &mddev->flags)) && (rw == WRITE)) { |
| 457 | bio_io_error(bio); |
Christoph Hellwig | 3e08773 | 2021-10-12 13:12:24 +0200 | [diff] [blame] | 458 | return; |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 459 | } |
| 460 | |
Christoph Hellwig | f695ca3 | 2020-07-01 10:59:39 +0200 | [diff] [blame] | 461 | blk_queue_split(&bio); |
Kent Overstreet | 54efd50 | 2015-04-23 22:37:18 -0700 | [diff] [blame] | 462 | |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 463 | if (mddev->ro == 1 && unlikely(rw == WRITE)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 464 | if (bio_sectors(bio) != 0) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 465 | bio->bi_status = BLK_STS_IOERR; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 466 | bio_endio(bio); |
Christoph Hellwig | 3e08773 | 2021-10-12 13:12:24 +0200 | [diff] [blame] | 467 | return; |
Sebastian Riemer | bbfa57c | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 468 | } |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 469 | |
Shaohua Li | 9c573de | 2016-04-25 16:52:38 -0700 | [diff] [blame] | 470 | /* bio could be mergeable after passing to underlayer */ |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 471 | bio->bi_opf &= ~REQ_NOMERGE; |
Shaohua Li | 393debc | 2017-09-21 10:23:35 -0700 | [diff] [blame] | 472 | |
| 473 | md_handle_request(mddev, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
| 475 | |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 476 | /* mddev_suspend makes sure no new requests are submitted |
| 477 | * to the device, and that any requests that have been submitted |
| 478 | * are completely handled. |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 479 | * Once mddev_detach() is called and completes, the module will be |
| 480 | * completely unused. |
NeilBrown | 9e35b99 | 2010-04-06 14:23:02 +1000 | [diff] [blame] | 481 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 482 | void mddev_suspend(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 483 | { |
Heinz Mauelshagen | 092398d | 2016-05-03 19:43:57 +0200 | [diff] [blame] | 484 | WARN_ON_ONCE(mddev->thread && current == mddev->thread->tsk); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 485 | lockdep_assert_held(&mddev->reconfig_mutex); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 486 | if (mddev->suspended++) |
| 487 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 488 | synchronize_rcu(); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 489 | wake_up(&mddev->sb_wait); |
NeilBrown | 35bfc52 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 490 | set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags); |
| 491 | smp_mb__after_atomic(); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 492 | wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0); |
| 493 | mddev->pers->quiesce(mddev, 1); |
NeilBrown | 35bfc52 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 494 | clear_bit_unlock(MD_ALLOW_SB_UPDATE, &mddev->flags); |
| 495 | wait_event(mddev->sb_wait, !test_bit(MD_UPDATING_SB, &mddev->flags)); |
Jonathan Brassow | 0d9f4f1 | 2012-05-16 04:06:14 -0500 | [diff] [blame] | 496 | |
| 497 | del_timer_sync(&mddev->safemode_timer); |
Coly Li | 78f57ef | 2020-04-09 22:17:20 +0800 | [diff] [blame] | 498 | /* restrict memory reclaim I/O during raid array is suspend */ |
| 499 | mddev->noio_flag = memalloc_noio_save(); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 500 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 501 | EXPORT_SYMBOL_GPL(mddev_suspend); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 502 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 503 | void mddev_resume(struct mddev *mddev) |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 504 | { |
Coly Li | 78f57ef | 2020-04-09 22:17:20 +0800 | [diff] [blame] | 505 | /* entred the memalloc scope from mddev_suspend() */ |
| 506 | memalloc_noio_restore(mddev->noio_flag); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 507 | lockdep_assert_held(&mddev->reconfig_mutex); |
Mikulas Patocka | 0dc10e5 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 508 | if (--mddev->suspended) |
| 509 | return; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 510 | wake_up(&mddev->sb_wait); |
| 511 | mddev->pers->quiesce(mddev, 0); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 512 | |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 513 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 514 | md_wakeup_thread(mddev->thread); |
| 515 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 516 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 517 | EXPORT_SYMBOL_GPL(mddev_resume); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 518 | |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 519 | /* |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 520 | * Generic flush handling for md |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 521 | */ |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 522 | |
| 523 | static void md_end_flush(struct bio *bio) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 524 | { |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 525 | struct md_rdev *rdev = bio->bi_private; |
| 526 | struct mddev *mddev = rdev->mddev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 527 | |
| 528 | rdev_dec_pending(rdev, mddev); |
| 529 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 530 | if (atomic_dec_and_test(&mddev->flush_pending)) { |
| 531 | /* The pre-request flush has finished */ |
| 532 | queue_work(md_wq, &mddev->flush_work); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 533 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 534 | bio_put(bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 535 | } |
| 536 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 537 | static void md_submit_flush_data(struct work_struct *ws); |
| 538 | |
| 539 | static void submit_flushes(struct work_struct *ws) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 540 | { |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 541 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 542 | struct md_rdev *rdev; |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 543 | |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 544 | mddev->start_flush = ktime_get_boottime(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 545 | INIT_WORK(&mddev->flush_work, md_submit_flush_data); |
| 546 | atomic_set(&mddev->flush_pending, 1); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 547 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 548 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 549 | if (rdev->raid_disk >= 0 && |
| 550 | !test_bit(Faulty, &rdev->flags)) { |
| 551 | /* Take two references, one is dropped |
| 552 | * when request finishes, one after |
| 553 | * we reclaim rcu_read_lock |
| 554 | */ |
| 555 | struct bio *bi; |
| 556 | atomic_inc(&rdev->nr_pending); |
| 557 | atomic_inc(&rdev->nr_pending); |
| 558 | rcu_read_unlock(); |
Christoph Hellwig | a78f18d | 2021-01-26 15:52:41 +0100 | [diff] [blame] | 559 | bi = bio_alloc_bioset(GFP_NOIO, 0, &mddev->bio_set); |
Xiao Ni | 5a409b4 | 2018-05-21 11:49:54 +0800 | [diff] [blame] | 560 | bi->bi_end_io = md_end_flush; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 561 | bi->bi_private = rdev; |
| 562 | bio_set_dev(bi, rdev->bdev); |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 563 | bi->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 564 | atomic_inc(&mddev->flush_pending); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 565 | submit_bio(bi); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 566 | rcu_read_lock(); |
| 567 | rdev_dec_pending(rdev, mddev); |
| 568 | } |
| 569 | rcu_read_unlock(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 570 | if (atomic_dec_and_test(&mddev->flush_pending)) |
| 571 | queue_work(md_wq, &mddev->flush_work); |
| 572 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 573 | |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 574 | static void md_submit_flush_data(struct work_struct *ws) |
| 575 | { |
| 576 | struct mddev *mddev = container_of(ws, struct mddev, flush_work); |
| 577 | struct bio *bio = mddev->flush_bio; |
| 578 | |
| 579 | /* |
| 580 | * must reset flush_bio before calling into md_handle_request to avoid a |
| 581 | * deadlock, because other bios passed md_handle_request suspend check |
| 582 | * could wait for this and below md_handle_request could wait for those |
| 583 | * bios because of suspend check |
| 584 | */ |
Xiao Ni | dc5d17a3 | 2020-12-10 14:33:32 +0800 | [diff] [blame] | 585 | spin_lock_irq(&mddev->lock); |
Pankaj Gupta | 81ba3c2 | 2020-11-11 06:16:56 +0100 | [diff] [blame] | 586 | mddev->prev_flush_start = mddev->start_flush; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 587 | mddev->flush_bio = NULL; |
Xiao Ni | dc5d17a3 | 2020-12-10 14:33:32 +0800 | [diff] [blame] | 588 | spin_unlock_irq(&mddev->lock); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 589 | wake_up(&mddev->sb_wait); |
| 590 | |
| 591 | if (bio->bi_iter.bi_size == 0) { |
| 592 | /* an empty barrier - all done */ |
| 593 | bio_endio(bio); |
| 594 | } else { |
| 595 | bio->bi_opf &= ~REQ_PREFLUSH; |
| 596 | md_handle_request(mddev, bio); |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 597 | } |
NeilBrown | a2826aa | 2009-12-14 12:49:49 +1100 | [diff] [blame] | 598 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 599 | |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 600 | /* |
| 601 | * Manages consolidation of flushes and submitting any flushes needed for |
| 602 | * a bio with REQ_PREFLUSH. Returns true if the bio is finished or is |
| 603 | * being finished in another context. Returns false if the flushing is |
| 604 | * complete but still needs the I/O portion of the bio to be processed. |
| 605 | */ |
| 606 | bool md_flush_request(struct mddev *mddev, struct bio *bio) |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 607 | { |
Pankaj Gupta | 81ba3c2 | 2020-11-11 06:16:56 +0100 | [diff] [blame] | 608 | ktime_t req_start = ktime_get_boottime(); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 609 | spin_lock_irq(&mddev->lock); |
Pankaj Gupta | 204d1a6 | 2020-11-11 06:16:57 +0100 | [diff] [blame] | 610 | /* flush requests wait until ongoing flush completes, |
| 611 | * hence coalescing all the pending requests. |
| 612 | */ |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 613 | wait_event_lock_irq(mddev->sb_wait, |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 614 | !mddev->flush_bio || |
Pankaj Gupta | a23f2aa | 2020-11-11 06:16:58 +0100 | [diff] [blame] | 615 | ktime_before(req_start, mddev->prev_flush_start), |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 616 | mddev->lock); |
Pankaj Gupta | 204d1a6 | 2020-11-11 06:16:57 +0100 | [diff] [blame] | 617 | /* new request after previous flush is completed */ |
Pankaj Gupta | a23f2aa | 2020-11-11 06:16:58 +0100 | [diff] [blame] | 618 | if (ktime_after(req_start, mddev->prev_flush_start)) { |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 619 | WARN_ON(mddev->flush_bio); |
| 620 | mddev->flush_bio = bio; |
| 621 | bio = NULL; |
| 622 | } |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 623 | spin_unlock_irq(&mddev->lock); |
| 624 | |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 625 | if (!bio) { |
| 626 | INIT_WORK(&mddev->flush_work, submit_flushes); |
| 627 | queue_work(md_wq, &mddev->flush_work); |
| 628 | } else { |
| 629 | /* flush was performed for some other bio while we waited. */ |
| 630 | if (bio->bi_iter.bi_size == 0) |
| 631 | /* an empty barrier - all done */ |
| 632 | bio_endio(bio); |
| 633 | else { |
| 634 | bio->bi_opf &= ~REQ_PREFLUSH; |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 635 | return false; |
NeilBrown | 2bc13b8 | 2019-03-29 10:46:17 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 638 | return true; |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 639 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 640 | EXPORT_SYMBOL(md_flush_request); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 641 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 642 | static inline struct mddev *mddev_get(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { |
| 644 | atomic_inc(&mddev->active); |
| 645 | return mddev; |
| 646 | } |
| 647 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 648 | static void mddev_delayed_delete(struct work_struct *ws); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 649 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 650 | static void mddev_put(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | { |
| 652 | if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock)) |
| 653 | return; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 654 | if (!mddev->raid_disks && list_empty(&mddev->disks) && |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 655 | mddev->ctime == 0 && !mddev->hold_active) { |
| 656 | /* Array is not configured at all, and not held active, |
| 657 | * so destroy it */ |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 658 | list_del_init(&mddev->all_mddevs); |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 659 | |
| 660 | /* |
| 661 | * Call queue_work inside the spinlock so that |
| 662 | * flush_workqueue() after mddev_find will succeed in waiting |
| 663 | * for the work to be done. |
| 664 | */ |
| 665 | INIT_WORK(&mddev->del_work, mddev_delayed_delete); |
| 666 | queue_work(md_misc_wq, &mddev->del_work); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 667 | } |
| 668 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 671 | static void md_safemode_timeout(struct timer_list *t); |
Sasha Levin | 25b2edf | 2015-07-24 18:19:58 -0400 | [diff] [blame] | 672 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 673 | void mddev_init(struct mddev *mddev) |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 674 | { |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 675 | kobject_init(&mddev->kobj, &md_ktype); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 676 | mutex_init(&mddev->open_mutex); |
| 677 | mutex_init(&mddev->reconfig_mutex); |
| 678 | mutex_init(&mddev->bitmap_info.mutex); |
| 679 | INIT_LIST_HEAD(&mddev->disks); |
| 680 | INIT_LIST_HEAD(&mddev->all_mddevs); |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 681 | timer_setup(&mddev->safemode_timer, md_safemode_timeout, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 682 | atomic_set(&mddev->active, 1); |
| 683 | atomic_set(&mddev->openers, 0); |
| 684 | atomic_set(&mddev->active_io, 0); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 685 | spin_lock_init(&mddev->lock); |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 686 | atomic_set(&mddev->flush_pending, 0); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 687 | init_waitqueue_head(&mddev->sb_wait); |
| 688 | init_waitqueue_head(&mddev->recovery_wait); |
| 689 | mddev->reshape_position = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 690 | mddev->reshape_backwards = 0; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 691 | mddev->last_sync_action = "none"; |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 692 | mddev->resync_min = 0; |
| 693 | mddev->resync_max = MaxSector; |
| 694 | mddev->level = LEVEL_NONE; |
| 695 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 696 | EXPORT_SYMBOL_GPL(mddev_init); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 697 | |
Christoph Hellwig | 8b57251f | 2021-04-03 18:15:28 +0200 | [diff] [blame] | 698 | static struct mddev *mddev_find_locked(dev_t unit) |
| 699 | { |
| 700 | struct mddev *mddev; |
| 701 | |
| 702 | list_for_each_entry(mddev, &all_mddevs, all_mddevs) |
| 703 | if (mddev->unit == unit) |
| 704 | return mddev; |
| 705 | |
| 706 | return NULL; |
| 707 | } |
| 708 | |
Christoph Hellwig | 85c8c3c | 2021-04-12 10:05:28 +0200 | [diff] [blame] | 709 | /* find an unused unit number */ |
| 710 | static dev_t mddev_alloc_unit(void) |
| 711 | { |
| 712 | static int next_minor = 512; |
| 713 | int start = next_minor; |
| 714 | bool is_free = 0; |
| 715 | dev_t dev = 0; |
| 716 | |
| 717 | while (!is_free) { |
| 718 | dev = MKDEV(MD_MAJOR, next_minor); |
| 719 | next_minor++; |
| 720 | if (next_minor > MINORMASK) |
| 721 | next_minor = 0; |
| 722 | if (next_minor == start) |
| 723 | return 0; /* Oh dear, all in use. */ |
| 724 | is_free = !mddev_find_locked(dev); |
| 725 | } |
| 726 | |
| 727 | return dev; |
| 728 | } |
| 729 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 730 | static struct mddev *mddev_find(dev_t unit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | { |
Christoph Hellwig | 65aa97c | 2021-04-03 18:15:29 +0200 | [diff] [blame] | 732 | struct mddev *mddev; |
| 733 | |
| 734 | if (MAJOR(unit) != MD_MAJOR) |
| 735 | unit &= ~((1 << MdpMinorShift) - 1); |
| 736 | |
| 737 | spin_lock(&all_mddevs_lock); |
| 738 | mddev = mddev_find_locked(unit); |
| 739 | if (mddev) |
| 740 | mddev_get(mddev); |
| 741 | spin_unlock(&all_mddevs_lock); |
| 742 | |
| 743 | return mddev; |
| 744 | } |
| 745 | |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 746 | static struct mddev *mddev_alloc(dev_t unit) |
Christoph Hellwig | 65aa97c | 2021-04-03 18:15:29 +0200 | [diff] [blame] | 747 | { |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 748 | struct mddev *new; |
| 749 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
NeilBrown | 8f5f02c | 2011-02-16 13:58:51 +1100 | [diff] [blame] | 751 | if (unit && MAJOR(unit) != MD_MAJOR) |
Christoph Hellwig | d144fe6 | 2021-04-12 10:05:29 +0200 | [diff] [blame] | 752 | unit &= ~((1 << MdpMinorShift) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 754 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | if (!new) |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 756 | return ERR_PTR(-ENOMEM); |
NeilBrown | fafd7fb | 2010-04-01 15:55:30 +1100 | [diff] [blame] | 757 | mddev_init(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
Christoph Hellwig | d144fe6 | 2021-04-12 10:05:29 +0200 | [diff] [blame] | 759 | spin_lock(&all_mddevs_lock); |
| 760 | if (unit) { |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 761 | error = -EEXIST; |
| 762 | if (mddev_find_locked(unit)) |
Christoph Hellwig | d144fe6 | 2021-04-12 10:05:29 +0200 | [diff] [blame] | 763 | goto out_free_new; |
Christoph Hellwig | d144fe6 | 2021-04-12 10:05:29 +0200 | [diff] [blame] | 764 | new->unit = unit; |
| 765 | if (MAJOR(unit) == MD_MAJOR) |
| 766 | new->md_minor = MINOR(unit); |
| 767 | else |
| 768 | new->md_minor = MINOR(unit) >> MdpMinorShift; |
| 769 | new->hold_active = UNTIL_IOCTL; |
| 770 | } else { |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 771 | error = -ENODEV; |
Christoph Hellwig | d144fe6 | 2021-04-12 10:05:29 +0200 | [diff] [blame] | 772 | new->unit = mddev_alloc_unit(); |
| 773 | if (!new->unit) |
| 774 | goto out_free_new; |
| 775 | new->md_minor = MINOR(new->unit); |
| 776 | new->hold_active = UNTIL_STOP; |
| 777 | } |
| 778 | |
| 779 | list_add(&new->all_mddevs, &all_mddevs); |
| 780 | spin_unlock(&all_mddevs_lock); |
| 781 | return new; |
| 782 | out_free_new: |
| 783 | spin_unlock(&all_mddevs_lock); |
| 784 | kfree(new); |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 785 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Rikard Falkeborn | c32dc04 | 2021-05-29 12:30:49 +0200 | [diff] [blame] | 788 | static const struct attribute_group md_redundancy_group; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 789 | |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 790 | void mddev_unlock(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 792 | if (mddev->to_remove) { |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 793 | /* These cannot be removed under reconfig_mutex as |
| 794 | * an access to the files will try to take reconfig_mutex |
| 795 | * while holding the file unremovable, which leads to |
| 796 | * a deadlock. |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 797 | * So hold set sysfs_active while the remove in happeing, |
| 798 | * and anything else which might set ->to_remove or my |
| 799 | * otherwise change the sysfs namespace will fail with |
| 800 | * -EBUSY if sysfs_active is still set. |
| 801 | * We set sysfs_active under reconfig_mutex and elsewhere |
| 802 | * test it under the same mutex to ensure its correct value |
| 803 | * is seen. |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 804 | */ |
Rikard Falkeborn | c32dc04 | 2021-05-29 12:30:49 +0200 | [diff] [blame] | 805 | const struct attribute_group *to_remove = mddev->to_remove; |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 806 | mddev->to_remove = NULL; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 807 | mddev->sysfs_active = 1; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 808 | mutex_unlock(&mddev->reconfig_mutex); |
| 809 | |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 810 | if (mddev->kobj.sd) { |
| 811 | if (to_remove != &md_redundancy_group) |
| 812 | sysfs_remove_group(&mddev->kobj, to_remove); |
| 813 | if (mddev->pers == NULL || |
| 814 | mddev->pers->sync_request == NULL) { |
| 815 | sysfs_remove_group(&mddev->kobj, &md_redundancy_group); |
| 816 | if (mddev->sysfs_action) |
| 817 | sysfs_put(mddev->sysfs_action); |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 818 | if (mddev->sysfs_completed) |
| 819 | sysfs_put(mddev->sysfs_completed); |
| 820 | if (mddev->sysfs_degraded) |
| 821 | sysfs_put(mddev->sysfs_degraded); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 822 | mddev->sysfs_action = NULL; |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 823 | mddev->sysfs_completed = NULL; |
| 824 | mddev->sysfs_degraded = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 825 | } |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 826 | } |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 827 | mddev->sysfs_active = 0; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 828 | } else |
| 829 | mutex_unlock(&mddev->reconfig_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
Chris Dunlop | 751e67c | 2011-10-19 16:48:26 +1100 | [diff] [blame] | 831 | /* As we've dropped the mutex we need a spinlock to |
| 832 | * make sure the thread doesn't disappear |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 833 | */ |
| 834 | spin_lock(&pers_lock); |
NeilBrown | 005eca5 | 2005-08-22 13:11:08 -0700 | [diff] [blame] | 835 | md_wakeup_thread(mddev->thread); |
NeilBrown | 4d5324f | 2017-10-19 12:17:16 +1100 | [diff] [blame] | 836 | wake_up(&mddev->sb_wait); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 837 | spin_unlock(&pers_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | } |
NeilBrown | 5c47daf | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 839 | EXPORT_SYMBOL_GPL(mddev_unlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 841 | struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 842 | { |
| 843 | struct md_rdev *rdev; |
| 844 | |
| 845 | rdev_for_each_rcu(rdev, mddev) |
| 846 | if (rdev->desc_nr == nr) |
| 847 | return rdev; |
| 848 | |
| 849 | return NULL; |
| 850 | } |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 851 | EXPORT_SYMBOL_GPL(md_find_rdev_nr_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 852 | |
| 853 | static struct md_rdev *find_rdev(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 855 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 857 | rdev_for_each(rdev, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | if (rdev->bdev->bd_dev == dev) |
| 859 | return rdev; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | return NULL; |
| 862 | } |
| 863 | |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 864 | struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 865 | { |
| 866 | struct md_rdev *rdev; |
| 867 | |
| 868 | rdev_for_each_rcu(rdev, mddev) |
| 869 | if (rdev->bdev->bd_dev == dev) |
| 870 | return rdev; |
| 871 | |
| 872 | return NULL; |
| 873 | } |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 874 | EXPORT_SYMBOL_GPL(md_find_rdev_rcu); |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 875 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 876 | static struct md_personality *find_pers(int level, char *clevel) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 877 | { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 878 | struct md_personality *pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 879 | list_for_each_entry(pers, &pers_list, list) { |
| 880 | if (level != LEVEL_NONE && pers->level == level) |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 881 | return pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 882 | if (strcmp(pers->name, clevel)==0) |
| 883 | return pers; |
| 884 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 885 | return NULL; |
| 886 | } |
| 887 | |
Andre Noll | b73df2d | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 888 | /* return the offset of the super block in 512byte sectors */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 889 | static inline sector_t calc_dev_sboffset(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | { |
Christoph Hellwig | 0fe8034 | 2021-10-18 12:11:06 +0200 | [diff] [blame] | 891 | return MD_NEW_SIZE_SECTORS(bdev_nr_sectors(rdev->bdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } |
| 893 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 894 | static int alloc_disk_sb(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | rdev->sb_page = alloc_page(GFP_KERNEL); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 897 | if (!rdev->sb_page) |
Andre Noll | ebc2433 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 898 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | return 0; |
| 900 | } |
| 901 | |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 902 | void md_rdev_clear(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | { |
| 904 | if (rdev->sb_page) { |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 905 | put_page(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | rdev->sb_loaded = 0; |
| 907 | rdev->sb_page = NULL; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 908 | rdev->sb_start = 0; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 909 | rdev->sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 911 | if (rdev->bb_page) { |
| 912 | put_page(rdev->bb_page); |
| 913 | rdev->bb_page = NULL; |
| 914 | } |
Dan Williams | d3b407fb | 2016-01-06 12:19:22 -0800 | [diff] [blame] | 915 | badblocks_exit(&rdev->badblocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 917 | EXPORT_SYMBOL_GPL(md_rdev_clear); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 919 | static void super_written(struct bio *bio) |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 920 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 921 | struct md_rdev *rdev = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 922 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 923 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 924 | if (bio->bi_status) { |
Guoqing Jiang | b3db8a2 | 2020-07-28 12:01:41 +0200 | [diff] [blame] | 925 | pr_err("md: %s gets error=%d\n", __func__, |
| 926 | blk_status_to_errno(bio->bi_status)); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 927 | md_error(mddev, rdev); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 928 | if (!test_bit(Faulty, &rdev->flags) |
| 929 | && (bio->bi_opf & MD_FAILFAST)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 930 | set_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 931 | set_bit(LastDev, &rdev->flags); |
| 932 | } |
| 933 | } else |
| 934 | clear_bit(LastDev, &rdev->flags); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 935 | |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 936 | if (atomic_dec_and_test(&mddev->pending_writes)) |
| 937 | wake_up(&mddev->sb_wait); |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 938 | rdev_dec_pending(rdev, mddev); |
Neil Brown | f8b58ed | 2005-06-27 22:29:34 -0700 | [diff] [blame] | 939 | bio_put(bio); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 940 | } |
| 941 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 942 | void md_super_write(struct mddev *mddev, struct md_rdev *rdev, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 943 | sector_t sector, int size, struct page *page) |
| 944 | { |
| 945 | /* write first size bytes of page to sector of rdev |
| 946 | * Increment mddev->pending_writes before returning |
| 947 | * and decrement it on completion, waking up sb_wait |
| 948 | * if zero is reached. |
| 949 | * If an error occurred, call md_error |
| 950 | */ |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 951 | struct bio *bio; |
| 952 | int ff = 0; |
| 953 | |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 954 | if (!page) |
| 955 | return; |
| 956 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 957 | if (test_bit(Faulty, &rdev->flags)) |
| 958 | return; |
| 959 | |
Christoph Hellwig | 6a59656 | 2021-01-26 15:52:43 +0100 | [diff] [blame] | 960 | bio = bio_alloc_bioset(GFP_NOIO, 1, &mddev->sync_set); |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 961 | |
Shaohua Li | ed3b98c | 2016-03-29 14:00:19 -0700 | [diff] [blame] | 962 | atomic_inc(&rdev->nr_pending); |
| 963 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 964 | bio_set_dev(bio, rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 965 | bio->bi_iter.bi_sector = sector; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 966 | bio_add_page(bio, page, size, 0); |
| 967 | bio->bi_private = rdev; |
| 968 | bio->bi_end_io = super_written; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 969 | |
| 970 | if (test_bit(MD_FAILFAST_SUPPORTED, &mddev->flags) && |
| 971 | test_bit(FailFast, &rdev->flags) && |
| 972 | !test_bit(LastDev, &rdev->flags)) |
| 973 | ff = MD_FAILFAST; |
Jan Kara | 5a8948f | 2017-05-31 09:44:33 +0200 | [diff] [blame] | 974 | bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA | ff; |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 975 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 976 | atomic_inc(&mddev->pending_writes); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 977 | submit_bio(bio); |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 978 | } |
| 979 | |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 980 | int md_super_wait(struct mddev *mddev) |
NeilBrown | a9701a3 | 2005-11-08 21:39:34 -0800 | [diff] [blame] | 981 | { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 982 | /* wait for all superblock writes that were scheduled to complete */ |
NeilBrown | 1967cd5 | 2014-09-09 14:20:28 +1000 | [diff] [blame] | 983 | wait_event(mddev->sb_wait, atomic_read(&mddev->pending_writes)==0); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 984 | if (test_and_clear_bit(MD_SB_NEED_REWRITE, &mddev->sb_flags)) |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 985 | return -EAGAIN; |
| 986 | return 0; |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 987 | } |
| 988 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 989 | 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] | 990 | struct page *page, int op, int op_flags, bool metadata_op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | { |
Christoph Hellwig | 3263738 | 2021-01-26 15:52:42 +0100 | [diff] [blame] | 992 | struct bio bio; |
| 993 | struct bio_vec bvec; |
| 994 | |
| 995 | bio_init(&bio, &bvec, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 997 | if (metadata_op && rdev->meta_bdev) |
Christoph Hellwig | 3263738 | 2021-01-26 15:52:42 +0100 | [diff] [blame] | 998 | bio_set_dev(&bio, rdev->meta_bdev); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 999 | else |
Christoph Hellwig | 3263738 | 2021-01-26 15:52:42 +0100 | [diff] [blame] | 1000 | bio_set_dev(&bio, rdev->bdev); |
| 1001 | bio.bi_opf = op | op_flags; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1002 | if (metadata_op) |
Christoph Hellwig | 3263738 | 2021-01-26 15:52:42 +0100 | [diff] [blame] | 1003 | bio.bi_iter.bi_sector = sector + rdev->sb_start; |
NeilBrown | 1fdd6fc9 | 2012-05-21 09:28:32 +1000 | [diff] [blame] | 1004 | else if (rdev->mddev->reshape_position != MaxSector && |
| 1005 | (rdev->mddev->reshape_backwards == |
| 1006 | (sector >= rdev->mddev->reshape_position))) |
Christoph Hellwig | 3263738 | 2021-01-26 15:52:42 +0100 | [diff] [blame] | 1007 | bio.bi_iter.bi_sector = sector + rdev->new_data_offset; |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1008 | else |
Christoph Hellwig | 3263738 | 2021-01-26 15:52:42 +0100 | [diff] [blame] | 1009 | bio.bi_iter.bi_sector = sector + rdev->data_offset; |
| 1010 | bio_add_page(&bio, page, size, 0); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 1011 | |
Christoph Hellwig | 3263738 | 2021-01-26 15:52:42 +0100 | [diff] [blame] | 1012 | submit_bio_wait(&bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Christoph Hellwig | 3263738 | 2021-01-26 15:52:42 +0100 | [diff] [blame] | 1014 | return !bio.bi_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | } |
NeilBrown | a8745db | 2006-01-06 00:20:34 -0800 | [diff] [blame] | 1016 | EXPORT_SYMBOL_GPL(sync_page_io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1018 | static int read_disk_sb(struct md_rdev *rdev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | { |
| 1020 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | if (rdev->sb_loaded) |
| 1023 | return 0; |
| 1024 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1025 | 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] | 1026 | goto fail; |
| 1027 | rdev->sb_loaded = 1; |
| 1028 | return 0; |
| 1029 | |
| 1030 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1031 | pr_err("md: disabled device %s, could not read superblock.\n", |
| 1032 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | return -EINVAL; |
| 1034 | } |
| 1035 | |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1036 | 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] | 1037 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1038 | return sb1->set_uuid0 == sb2->set_uuid0 && |
Andre Noll | 0571046 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 1039 | sb1->set_uuid1 == sb2->set_uuid1 && |
| 1040 | sb1->set_uuid2 == sb2->set_uuid2 && |
| 1041 | sb1->set_uuid3 == sb2->set_uuid3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1044 | 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] | 1045 | { |
| 1046 | int ret; |
| 1047 | mdp_super_t *tmp1, *tmp2; |
| 1048 | |
| 1049 | tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL); |
| 1050 | tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL); |
| 1051 | |
| 1052 | if (!tmp1 || !tmp2) { |
| 1053 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | goto abort; |
| 1055 | } |
| 1056 | |
| 1057 | *tmp1 = *sb1; |
| 1058 | *tmp2 = *sb2; |
| 1059 | |
| 1060 | /* |
| 1061 | * nr_disks is not constant |
| 1062 | */ |
| 1063 | tmp1->nr_disks = 0; |
| 1064 | tmp2->nr_disks = 0; |
| 1065 | |
Andre Noll | ce0c8e0 | 2008-07-11 22:02:20 +1000 | [diff] [blame] | 1066 | ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | abort: |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 1068 | kfree(tmp1); |
| 1069 | kfree(tmp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | return ret; |
| 1071 | } |
| 1072 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1073 | static u32 md_csum_fold(u32 csum) |
| 1074 | { |
| 1075 | csum = (csum & 0xffff) + (csum >> 16); |
| 1076 | return (csum & 0xffff) + (csum >> 16); |
| 1077 | } |
| 1078 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1079 | static unsigned int calc_sb_csum(mdp_super_t *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | { |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1081 | u64 newcsum = 0; |
| 1082 | u32 *sb32 = (u32*)sb; |
| 1083 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | unsigned int disk_csum, csum; |
| 1085 | |
| 1086 | disk_csum = sb->sb_csum; |
| 1087 | sb->sb_csum = 0; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1088 | |
| 1089 | for (i = 0; i < MD_SB_BYTES/4 ; i++) |
| 1090 | newcsum += sb32[i]; |
| 1091 | csum = (newcsum & 0xffffffff) + (newcsum>>32); |
| 1092 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1093 | #ifdef CONFIG_ALPHA |
| 1094 | /* This used to use csum_partial, which was wrong for several |
| 1095 | * reasons including that different results are returned on |
| 1096 | * different architectures. It isn't critical that we get exactly |
| 1097 | * the same return value as before (we always csum_fold before |
| 1098 | * testing, and that removes any differences). However as we |
| 1099 | * know that csum_partial always returned a 16bit value on |
| 1100 | * alphas, do a fold to maximise conformity to previous behaviour. |
| 1101 | */ |
| 1102 | sb->sb_csum = md_csum_fold(disk_csum); |
| 1103 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | sb->sb_csum = disk_csum; |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1105 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | return csum; |
| 1107 | } |
| 1108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | /* |
| 1110 | * Handle superblock details. |
| 1111 | * We want to be able to handle multiple superblock formats |
| 1112 | * so we have a common interface to them all, and an array of |
| 1113 | * different handlers. |
| 1114 | * We rely on user-space to write the initial superblock, and support |
| 1115 | * reading and updating of superblocks. |
| 1116 | * Interface methods are: |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1117 | * 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] | 1118 | * loads and validates a superblock on dev. |
| 1119 | * if refdev != NULL, compare superblocks on both devices |
| 1120 | * Return: |
| 1121 | * 0 - dev has a superblock that is compatible with refdev |
| 1122 | * 1 - dev has a superblock that is compatible and newer than refdev |
| 1123 | * so dev should be used as the refdev in future |
| 1124 | * -EINVAL superblock incompatible or invalid |
| 1125 | * -othererror e.g. -EIO |
| 1126 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1127 | * int validate_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | * Verify that dev is acceptable into mddev. |
| 1129 | * The first time, mddev->raid_disks will be 0, and data from |
| 1130 | * dev should be merged in. Subsequent calls check that dev |
| 1131 | * is new enough. Return 0 or -EINVAL |
| 1132 | * |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1133 | * void sync_super(struct mddev *mddev, struct md_rdev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | * Update the superblock for rdev with data in mddev |
| 1135 | * This does not write to disc. |
| 1136 | * |
| 1137 | */ |
| 1138 | |
| 1139 | struct super_type { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1140 | char *name; |
| 1141 | struct module *owner; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1142 | int (*load_super)(struct md_rdev *rdev, |
| 1143 | struct md_rdev *refdev, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1144 | int minor_version); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1145 | int (*validate_super)(struct mddev *mddev, |
| 1146 | struct md_rdev *rdev); |
| 1147 | void (*sync_super)(struct mddev *mddev, |
| 1148 | struct md_rdev *rdev); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1149 | unsigned long long (*rdev_size_change)(struct md_rdev *rdev, |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1150 | sector_t num_sectors); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1151 | int (*allow_new_offset)(struct md_rdev *rdev, |
| 1152 | unsigned long long new_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | }; |
| 1154 | |
| 1155 | /* |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1156 | * Check that the given mddev has no bitmap. |
| 1157 | * |
| 1158 | * This function is called from the run method of all personalities that do not |
| 1159 | * support bitmaps. It prints an error message and returns non-zero if mddev |
| 1160 | * has a bitmap. Otherwise, it returns 0. |
| 1161 | * |
| 1162 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1163 | int md_check_no_bitmap(struct mddev *mddev) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1164 | { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1165 | if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset) |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1166 | return 0; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1167 | pr_warn("%s: bitmaps are not supported for %s\n", |
Andre Noll | 0894cc3 | 2009-06-18 08:49:23 +1000 | [diff] [blame] | 1168 | mdname(mddev), mddev->pers->name); |
| 1169 | return 1; |
| 1170 | } |
| 1171 | EXPORT_SYMBOL(md_check_no_bitmap); |
| 1172 | |
| 1173 | /* |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1174 | * load_super for 0.90.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1176 | 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] | 1177 | { |
| 1178 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
| 1179 | mdp_super_t *sb; |
| 1180 | int ret; |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1181 | bool spare_disk = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | |
| 1183 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1184 | * Calculate the position of the superblock (512byte sectors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | * it's at the end of the disk. |
| 1186 | * |
| 1187 | * It also happens to be a multiple of 4Kb. |
| 1188 | */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1189 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1191 | ret = read_disk_sb(rdev, MD_SB_BYTES); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1192 | if (ret) |
| 1193 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
| 1195 | ret = -EINVAL; |
| 1196 | |
| 1197 | bdevname(rdev->bdev, b); |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1198 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
| 1200 | if (sb->md_magic != MD_SB_MAGIC) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1201 | pr_warn("md: invalid raid superblock magic on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | goto abort; |
| 1203 | } |
| 1204 | |
| 1205 | if (sb->major_version != 0 || |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1206 | sb->minor_version < 90 || |
| 1207 | sb->minor_version > 91) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1208 | pr_warn("Bad version number %d.%d on %s\n", |
| 1209 | sb->major_version, sb->minor_version, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | goto abort; |
| 1211 | } |
| 1212 | |
| 1213 | if (sb->raid_disks <= 0) |
| 1214 | goto abort; |
| 1215 | |
NeilBrown | 4d167f0 | 2007-05-09 02:35:37 -0700 | [diff] [blame] | 1216 | 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] | 1217 | pr_warn("md: invalid superblock checksum on %s\n", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | goto abort; |
| 1219 | } |
| 1220 | |
| 1221 | rdev->preferred_minor = sb->md_minor; |
| 1222 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1223 | rdev->new_data_offset = 0; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1224 | rdev->sb_size = MD_SB_BYTES; |
NeilBrown | 9f2f383 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1225 | rdev->badblocks.shift = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
| 1227 | if (sb->level == LEVEL_MULTIPATH) |
| 1228 | rdev->desc_nr = -1; |
| 1229 | else |
| 1230 | rdev->desc_nr = sb->this_disk.number; |
| 1231 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1232 | /* not spare disk, or LEVEL_MULTIPATH */ |
| 1233 | if (sb->level == LEVEL_MULTIPATH || |
| 1234 | (rdev->desc_nr >= 0 && |
Yufen Yu | 3b7436c | 2019-12-10 15:01:29 +0800 | [diff] [blame] | 1235 | rdev->desc_nr < MD_SB_DISKS && |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1236 | sb->disks[rdev->desc_nr].state & |
| 1237 | ((1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))) |
| 1238 | spare_disk = false; |
| 1239 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1240 | if (!refdev) { |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1241 | if (!spare_disk) |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1242 | ret = 1; |
| 1243 | else |
| 1244 | ret = 0; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1245 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1247 | mdp_super_t *refsb = page_address(refdev->sb_page); |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1248 | if (!md_uuid_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1249 | pr_warn("md: %s has different UUID to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | b, bdevname(refdev->bdev,b2)); |
| 1251 | goto abort; |
| 1252 | } |
Amir Goldstein | e6fd209 | 2017-05-04 16:26:20 +0300 | [diff] [blame] | 1253 | if (!md_sb_equal(refsb, sb)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1254 | pr_warn("md: %s has same UUID but different superblock to %s\n", |
| 1255 | b, bdevname(refdev->bdev, b2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | goto abort; |
| 1257 | } |
| 1258 | ev1 = md_event(sb); |
| 1259 | ev2 = md_event(refsb); |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1260 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1261 | if (!spare_disk && ev1 > ev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | ret = 1; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1263 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | ret = 0; |
| 1265 | } |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 1266 | rdev->sectors = rdev->sb_start; |
NeilBrown | 667a531 | 2012-08-16 16:46:12 +1000 | [diff] [blame] | 1267 | /* Limit to 4TB as metadata cannot record more than that. |
| 1268 | * (not needed for Linear and RAID0 as metadata doesn't |
| 1269 | * record this size) |
| 1270 | */ |
Christoph Hellwig | 72deb45 | 2019-04-05 18:08:59 +0200 | [diff] [blame] | 1271 | if ((u64)rdev->sectors >= (2ULL << 32) && sb->level >= 1) |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1272 | rdev->sectors = (sector_t)(2ULL << 32) - 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1274 | if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1) |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 1275 | /* "this cannot possibly happen" ... */ |
| 1276 | ret = -EINVAL; |
| 1277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | abort: |
| 1279 | return ret; |
| 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * validate_super for 0.90.0 |
| 1284 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1285 | static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | { |
| 1287 | mdp_disk_t *desc; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1288 | mdp_super_t *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1289 | __u64 ev1 = md_event(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1291 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1292 | clear_bit(Faulty, &rdev->flags); |
| 1293 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1294 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1295 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | if (mddev->raid_disks == 0) { |
| 1298 | mddev->major_version = 0; |
| 1299 | mddev->minor_version = sb->minor_version; |
| 1300 | mddev->patch_version = sb->patch_version; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1301 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1302 | mddev->chunk_sectors = sb->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | mddev->ctime = sb->ctime; |
| 1304 | mddev->utime = sb->utime; |
| 1305 | mddev->level = sb->level; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1306 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | mddev->layout = sb->layout; |
| 1308 | mddev->raid_disks = sb->raid_disks; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1309 | mddev->dev_sectors = ((sector_t)sb->size) * 2; |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1310 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1311 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1312 | mddev->bitmap_info.space = 0; |
| 1313 | /* bitmap can use 60 K after the 4K superblocks */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1314 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1315 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1316 | mddev->reshape_backwards = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1318 | if (mddev->minor_version >= 91) { |
| 1319 | mddev->reshape_position = sb->reshape_position; |
| 1320 | mddev->delta_disks = sb->delta_disks; |
| 1321 | mddev->new_level = sb->new_level; |
| 1322 | mddev->new_layout = sb->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1323 | mddev->new_chunk_sectors = sb->new_chunk >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1324 | if (mddev->delta_disks < 0) |
| 1325 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1326 | } else { |
| 1327 | mddev->reshape_position = MaxSector; |
| 1328 | mddev->delta_disks = 0; |
| 1329 | mddev->new_level = mddev->level; |
| 1330 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1331 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1332 | } |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1333 | if (mddev->level == 0) |
| 1334 | mddev->layout = -1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | if (sb->state & (1<<MD_SB_CLEAN)) |
| 1337 | mddev->recovery_cp = MaxSector; |
| 1338 | else { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1339 | if (sb->events_hi == sb->cp_events_hi && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | sb->events_lo == sb->cp_events_lo) { |
| 1341 | mddev->recovery_cp = sb->recovery_cp; |
| 1342 | } else |
| 1343 | mddev->recovery_cp = 0; |
| 1344 | } |
| 1345 | |
| 1346 | memcpy(mddev->uuid+0, &sb->set_uuid0, 4); |
| 1347 | memcpy(mddev->uuid+4, &sb->set_uuid1, 4); |
| 1348 | memcpy(mddev->uuid+8, &sb->set_uuid2, 4); |
| 1349 | memcpy(mddev->uuid+12,&sb->set_uuid3, 4); |
| 1350 | |
| 1351 | mddev->max_disks = MD_SB_DISKS; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1352 | |
| 1353 | if (sb->state & (1<<MD_SB_BITMAP_PRESENT) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1354 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1355 | mddev->bitmap_info.offset = |
| 1356 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1357 | mddev->bitmap_info.space = |
Dave Jones | c9ad020 | 2013-08-19 22:26:32 -0400 | [diff] [blame] | 1358 | mddev->bitmap_info.default_space; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1359 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1360 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1361 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1362 | /* Insist on good event counter while assembling, except |
| 1363 | * for spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1365 | if (sb->disks[rdev->desc_nr].state & ( |
| 1366 | (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE))) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1367 | if (ev1 < mddev->events) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1368 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1369 | } else if (mddev->bitmap) { |
| 1370 | /* if adding to array with a bitmap, then we can accept an |
| 1371 | * older device ... but not too old. |
| 1372 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1373 | if (ev1 < mddev->bitmap->events_cleared) |
| 1374 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1375 | if (ev1 < mddev->events) |
| 1376 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1377 | } else { |
| 1378 | if (ev1 < mddev->events) |
| 1379 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1380 | return 0; |
| 1381 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | if (mddev->level != LEVEL_MULTIPATH) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | desc = sb->disks + rdev->desc_nr; |
| 1385 | |
| 1386 | if (desc->state & (1<<MD_DISK_FAULTY)) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1387 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 7c7546c | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1388 | else if (desc->state & (1<<MD_DISK_SYNC) /* && |
| 1389 | desc->raid_disk < mddev->raid_disks */) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1390 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | rdev->raid_disk = desc->raid_disk; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1392 | rdev->saved_raid_disk = desc->raid_disk; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1393 | } else if (desc->state & (1<<MD_DISK_ACTIVE)) { |
| 1394 | /* active but not in sync implies recovery up to |
| 1395 | * reshape position. We don't know exactly where |
| 1396 | * that is, so set to zero for now */ |
| 1397 | if (mddev->minor_version >= 91) { |
| 1398 | rdev->recovery_offset = 0; |
| 1399 | rdev->raid_disk = desc->raid_disk; |
| 1400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1402 | if (desc->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 1403 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1404 | if (desc->state & (1<<MD_DISK_FAILFAST)) |
| 1405 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1406 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1407 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | return 0; |
| 1409 | } |
| 1410 | |
| 1411 | /* |
| 1412 | * sync_super for 0.90.0 |
| 1413 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1414 | static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | { |
| 1416 | mdp_super_t *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1417 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | int next_spare = mddev->raid_disks; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | /* make rdev->sb match mddev data.. |
| 1421 | * |
| 1422 | * 1/ zero out disks |
| 1423 | * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare); |
| 1424 | * 3/ any empty disks < next_spare become removed |
| 1425 | * |
| 1426 | * disks[0] gets initialised to REMOVED because |
| 1427 | * we cannot be sure from other fields if it has |
| 1428 | * been initialised or not. |
| 1429 | */ |
| 1430 | int i; |
| 1431 | int active=0, working=0,failed=0,spare=0,nr_disks=0; |
| 1432 | |
NeilBrown | 6118156 | 2005-09-09 16:24:02 -0700 | [diff] [blame] | 1433 | rdev->sb_size = MD_SB_BYTES; |
| 1434 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1435 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | |
| 1437 | memset(sb, 0, sizeof(*sb)); |
| 1438 | |
| 1439 | sb->md_magic = MD_SB_MAGIC; |
| 1440 | sb->major_version = mddev->major_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | sb->patch_version = mddev->patch_version; |
| 1442 | sb->gvalid_words = 0; /* ignored */ |
| 1443 | memcpy(&sb->set_uuid0, mddev->uuid+0, 4); |
| 1444 | memcpy(&sb->set_uuid1, mddev->uuid+4, 4); |
| 1445 | memcpy(&sb->set_uuid2, mddev->uuid+8, 4); |
| 1446 | memcpy(&sb->set_uuid3, mddev->uuid+12,4); |
| 1447 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1448 | sb->ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | sb->level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1450 | sb->size = mddev->dev_sectors / 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | sb->raid_disks = mddev->raid_disks; |
| 1452 | sb->md_minor = mddev->md_minor; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1453 | sb->not_persistent = 0; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1454 | sb->utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | sb->state = 0; |
| 1456 | sb->events_hi = (mddev->events>>32); |
| 1457 | sb->events_lo = (u32)mddev->events; |
| 1458 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1459 | if (mddev->reshape_position == MaxSector) |
| 1460 | sb->minor_version = 90; |
| 1461 | else { |
| 1462 | sb->minor_version = 91; |
| 1463 | sb->reshape_position = mddev->reshape_position; |
| 1464 | sb->new_level = mddev->new_level; |
| 1465 | sb->delta_disks = mddev->delta_disks; |
| 1466 | sb->new_layout = mddev->new_layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1467 | sb->new_chunk = mddev->new_chunk_sectors << 9; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1468 | } |
| 1469 | mddev->minor_version = sb->minor_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | if (mddev->in_sync) |
| 1471 | { |
| 1472 | sb->recovery_cp = mddev->recovery_cp; |
| 1473 | sb->cp_events_hi = (mddev->events>>32); |
| 1474 | sb->cp_events_lo = (u32)mddev->events; |
| 1475 | if (mddev->recovery_cp == MaxSector) |
| 1476 | sb->state = (1<< MD_SB_CLEAN); |
| 1477 | } else |
| 1478 | sb->recovery_cp = 0; |
| 1479 | |
| 1480 | sb->layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1481 | sb->chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1483 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1484 | sb->state |= (1<<MD_SB_BITMAP_PRESENT); |
| 1485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | sb->disks[0].state = (1<<MD_DISK_REMOVED); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1487 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | mdp_disk_t *d; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1489 | int desc_nr; |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1490 | int is_active = test_bit(In_sync, &rdev2->flags); |
| 1491 | |
| 1492 | if (rdev2->raid_disk >= 0 && |
| 1493 | sb->minor_version >= 91) |
| 1494 | /* we have nowhere to store the recovery_offset, |
| 1495 | * but if it is not below the reshape_position, |
| 1496 | * we can piggy-back on that. |
| 1497 | */ |
| 1498 | is_active = 1; |
| 1499 | if (rdev2->raid_disk < 0 || |
| 1500 | test_bit(Faulty, &rdev2->flags)) |
| 1501 | is_active = 0; |
| 1502 | if (is_active) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1503 | desc_nr = rdev2->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | else |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 1505 | desc_nr = next_spare++; |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 1506 | rdev2->desc_nr = desc_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | d = &sb->disks[rdev2->desc_nr]; |
| 1508 | nr_disks++; |
| 1509 | d->number = rdev2->desc_nr; |
| 1510 | d->major = MAJOR(rdev2->bdev->bd_dev); |
| 1511 | d->minor = MINOR(rdev2->bdev->bd_dev); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1512 | if (is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | d->raid_disk = rdev2->raid_disk; |
| 1514 | else |
| 1515 | d->raid_disk = rdev2->desc_nr; /* compatibility */ |
NeilBrown | 1be7892 | 2006-03-27 01:18:03 -0800 | [diff] [blame] | 1516 | if (test_bit(Faulty, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | d->state = (1<<MD_DISK_FAULTY); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1518 | else if (is_active) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | d->state = (1<<MD_DISK_ACTIVE); |
NeilBrown | 0261cd9f | 2009-11-13 17:40:48 +1100 | [diff] [blame] | 1520 | if (test_bit(In_sync, &rdev2->flags)) |
| 1521 | d->state |= (1<<MD_DISK_SYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | active++; |
| 1523 | working++; |
| 1524 | } else { |
| 1525 | d->state = 0; |
| 1526 | spare++; |
| 1527 | working++; |
| 1528 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1529 | if (test_bit(WriteMostly, &rdev2->flags)) |
| 1530 | d->state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1531 | if (test_bit(FailFast, &rdev2->flags)) |
| 1532 | d->state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | /* now set the "removed" and "faulty" bits on any missing devices */ |
| 1535 | for (i=0 ; i < mddev->raid_disks ; i++) { |
| 1536 | mdp_disk_t *d = &sb->disks[i]; |
| 1537 | if (d->state == 0 && d->number == 0) { |
| 1538 | d->number = i; |
| 1539 | d->raid_disk = i; |
| 1540 | d->state = (1<<MD_DISK_REMOVED); |
| 1541 | d->state |= (1<<MD_DISK_FAULTY); |
| 1542 | failed++; |
| 1543 | } |
| 1544 | } |
| 1545 | sb->nr_disks = nr_disks; |
| 1546 | sb->active_disks = active; |
| 1547 | sb->working_disks = working; |
| 1548 | sb->failed_disks = failed; |
| 1549 | sb->spare_disks = spare; |
| 1550 | |
| 1551 | sb->this_disk = sb->disks[rdev->desc_nr]; |
| 1552 | sb->sb_csum = calc_sb_csum(sb); |
| 1553 | } |
| 1554 | |
| 1555 | /* |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1556 | * rdev_size_change for 0.90.0 |
| 1557 | */ |
| 1558 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1559 | 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] | 1560 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1561 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1562 | return 0; /* component must fit device */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1563 | if (rdev->mddev->bitmap_info.offset) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1564 | return 0; /* can't move bitmap */ |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1565 | rdev->sb_start = calc_dev_sboffset(rdev); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 1566 | if (!num_sectors || num_sectors > rdev->sb_start) |
| 1567 | num_sectors = rdev->sb_start; |
NeilBrown | 27a7b26 | 2011-09-10 17:21:28 +1000 | [diff] [blame] | 1568 | /* Limit to 4TB as metadata cannot record more than that. |
| 1569 | * 4TB == 2^32 KB, or 2*2^32 sectors. |
| 1570 | */ |
Christoph Hellwig | 72deb45 | 2019-04-05 18:08:59 +0200 | [diff] [blame] | 1571 | if ((u64)num_sectors >= (2ULL << 32) && rdev->mddev->level >= 1) |
Arnd Bergmann | 3312c95 | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1572 | num_sectors = (sector_t)(2ULL << 32) - 2; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1573 | do { |
| 1574 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1575 | rdev->sb_page); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1576 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 1577 | return num_sectors; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1578 | } |
| 1579 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1580 | static int |
| 1581 | super_90_allow_new_offset(struct md_rdev *rdev, unsigned long long new_offset) |
| 1582 | { |
| 1583 | /* non-zero offset changes not possible with v0.90 */ |
| 1584 | return new_offset == 0; |
| 1585 | } |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 1586 | |
| 1587 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | * version 1 superblock |
| 1589 | */ |
| 1590 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 1591 | static __le32 calc_sb_1_csum(struct mdp_superblock_1 *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | { |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1593 | __le32 disk_csum; |
| 1594 | u32 csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | unsigned long long newcsum; |
| 1596 | int size = 256 + le32_to_cpu(sb->max_dev)*2; |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1597 | __le32 *isuper = (__le32*)sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | |
| 1599 | disk_csum = sb->sb_csum; |
| 1600 | sb->sb_csum = 0; |
| 1601 | newcsum = 0; |
NeilBrown | 1f3c990 | 2012-12-11 13:09:00 +1100 | [diff] [blame] | 1602 | for (; size >= 4; size -= 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | newcsum += le32_to_cpu(*isuper++); |
| 1604 | |
| 1605 | if (size == 2) |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1606 | newcsum += le16_to_cpu(*(__le16*) isuper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | |
| 1608 | csum = (newcsum & 0xffffffff) + (newcsum >> 32); |
| 1609 | sb->sb_csum = disk_csum; |
| 1610 | return cpu_to_le32(csum); |
| 1611 | } |
| 1612 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1613 | 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] | 1614 | { |
| 1615 | struct mdp_superblock_1 *sb; |
| 1616 | int ret; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1617 | sector_t sb_start; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1618 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1620 | int bmask; |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1621 | bool spare_disk = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
| 1623 | /* |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1624 | * Calculate the position of the superblock in 512byte sectors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | * It is always aligned to a 4K boundary and |
| 1626 | * depeding on minor_version, it can be: |
| 1627 | * 0: At least 8K, but less than 12K, from end of device |
| 1628 | * 1: At start of device |
| 1629 | * 2: 4K from start of device. |
| 1630 | */ |
| 1631 | switch(minor_version) { |
| 1632 | case 0: |
Christoph Hellwig | 0fe8034 | 2021-10-18 12:11:06 +0200 | [diff] [blame] | 1633 | sb_start = bdev_nr_sectors(rdev->bdev) - 8 * 2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1634 | sb_start &= ~(sector_t)(4*2-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | break; |
| 1636 | case 1: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1637 | sb_start = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | break; |
| 1639 | case 2: |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1640 | sb_start = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | break; |
| 1642 | default: |
| 1643 | return -EINVAL; |
| 1644 | } |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1645 | rdev->sb_start = sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1647 | /* superblock is rarely larger than 1K, but it can be larger, |
| 1648 | * and it is safe to read 4k, so we do that |
| 1649 | */ |
| 1650 | ret = read_disk_sb(rdev, 4096); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | if (ret) return ret; |
| 1652 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1653 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | |
| 1655 | if (sb->magic != cpu_to_le32(MD_SB_MAGIC) || |
| 1656 | sb->major_version != cpu_to_le32(1) || |
| 1657 | le32_to_cpu(sb->max_dev) > (4096-256)/2 || |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1658 | le64_to_cpu(sb->super_offset) != rdev->sb_start || |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1659 | (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | return -EINVAL; |
| 1661 | |
| 1662 | if (calc_sb_1_csum(sb) != sb->sb_csum) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1663 | pr_warn("md: invalid superblock checksum on %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | bdevname(rdev->bdev,b)); |
| 1665 | return -EINVAL; |
| 1666 | } |
| 1667 | if (le64_to_cpu(sb->data_size) < 10) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1668 | pr_warn("md: data_size too small on %s\n", |
| 1669 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | return -EINVAL; |
| 1671 | } |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1672 | if (sb->pad0 || |
| 1673 | sb->pad3[0] || |
| 1674 | memcmp(sb->pad3, sb->pad3+1, sizeof(sb->pad3) - sizeof(sb->pad3[1]))) |
| 1675 | /* Some padding is non-zero, might be a new feature */ |
| 1676 | return -EINVAL; |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | rdev->preferred_minor = 0xffff; |
| 1679 | rdev->data_offset = le64_to_cpu(sb->data_offset); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1680 | rdev->new_data_offset = rdev->data_offset; |
| 1681 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE) && |
| 1682 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_NEW_OFFSET)) |
| 1683 | rdev->new_data_offset += (s32)le32_to_cpu(sb->new_offset); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1684 | atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1686 | rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256; |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 1687 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1688 | if (rdev->sb_size & bmask) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1689 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
| 1690 | |
| 1691 | if (minor_version |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 1692 | && rdev->data_offset < sb_start + (rdev->sb_size/512)) |
NeilBrown | a1801f8 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 1693 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1694 | if (minor_version |
| 1695 | && rdev->new_data_offset < sb_start + (rdev->sb_size/512)) |
| 1696 | return -EINVAL; |
NeilBrown | 0002b27 | 2005-09-09 16:23:53 -0700 | [diff] [blame] | 1697 | |
NeilBrown | 31b65a0 | 2006-07-10 04:44:14 -0700 | [diff] [blame] | 1698 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH)) |
| 1699 | rdev->desc_nr = -1; |
| 1700 | else |
| 1701 | rdev->desc_nr = le32_to_cpu(sb->dev_number); |
| 1702 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1703 | if (!rdev->bb_page) { |
| 1704 | rdev->bb_page = alloc_page(GFP_KERNEL); |
| 1705 | if (!rdev->bb_page) |
| 1706 | return -ENOMEM; |
| 1707 | } |
| 1708 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) && |
| 1709 | rdev->badblocks.count == 0) { |
| 1710 | /* need to load the bad block list. |
| 1711 | * Currently we limit it to one page. |
| 1712 | */ |
| 1713 | s32 offset; |
| 1714 | sector_t bb_sector; |
Christoph Hellwig | 00485d0 | 2019-04-04 18:56:12 +0200 | [diff] [blame] | 1715 | __le64 *bbp; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1716 | int i; |
| 1717 | int sectors = le16_to_cpu(sb->bblog_size); |
| 1718 | if (sectors > (PAGE_SIZE / 512)) |
| 1719 | return -EINVAL; |
| 1720 | offset = le32_to_cpu(sb->bblog_offset); |
| 1721 | if (offset == 0) |
| 1722 | return -EINVAL; |
| 1723 | bb_sector = (long long)offset; |
| 1724 | if (!sync_page_io(rdev, bb_sector, sectors << 9, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1725 | rdev->bb_page, REQ_OP_READ, 0, true)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1726 | return -EIO; |
Christoph Hellwig | 00485d0 | 2019-04-04 18:56:12 +0200 | [diff] [blame] | 1727 | bbp = (__le64 *)page_address(rdev->bb_page); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1728 | rdev->badblocks.shift = sb->bblog_shift; |
| 1729 | for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) { |
| 1730 | u64 bb = le64_to_cpu(*bbp); |
| 1731 | int count = bb & (0x3ff); |
| 1732 | u64 sector = bb >> 10; |
| 1733 | sector <<= sb->bblog_shift; |
| 1734 | count <<= sb->bblog_shift; |
| 1735 | if (bb + 1 == 0) |
| 1736 | break; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 1737 | if (badblocks_set(&rdev->badblocks, sector, count, 1)) |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1738 | return -EINVAL; |
| 1739 | } |
NeilBrown | 486adf7 | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 1740 | } else if (sb->bblog_offset != 0) |
| 1741 | rdev->badblocks.shift = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1742 | |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1743 | if ((le32_to_cpu(sb->feature_map) & |
| 1744 | (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS))) { |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1745 | rdev->ppl.offset = (__s16)le16_to_cpu(sb->ppl.offset); |
| 1746 | rdev->ppl.size = le16_to_cpu(sb->ppl.size); |
| 1747 | rdev->ppl.sector = rdev->sb_start + rdev->ppl.offset; |
| 1748 | } |
| 1749 | |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1750 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT) && |
| 1751 | sb->level != 0) |
| 1752 | return -EINVAL; |
| 1753 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1754 | /* not spare disk, or LEVEL_MULTIPATH */ |
| 1755 | if (sb->level == cpu_to_le32(LEVEL_MULTIPATH) || |
| 1756 | (rdev->desc_nr >= 0 && |
| 1757 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
| 1758 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1759 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL))) |
| 1760 | spare_disk = false; |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1761 | |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1762 | if (!refdev) { |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1763 | if (!spare_disk) |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 1764 | ret = 1; |
| 1765 | else |
| 1766 | ret = 0; |
Harvey Harrison | 9a7b2b0 | 2008-04-28 02:15:49 -0700 | [diff] [blame] | 1767 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | __u64 ev1, ev2; |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1769 | struct mdp_superblock_1 *refsb = page_address(refdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | |
| 1771 | if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 || |
| 1772 | sb->level != refsb->level || |
| 1773 | sb->layout != refsb->layout || |
| 1774 | sb->chunksize != refsb->chunksize) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1775 | pr_warn("md: %s has strangely different superblock to %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | bdevname(rdev->bdev,b), |
| 1777 | bdevname(refdev->bdev,b2)); |
| 1778 | return -EINVAL; |
| 1779 | } |
| 1780 | ev1 = le64_to_cpu(sb->events); |
| 1781 | ev2 = le64_to_cpu(refsb->events); |
| 1782 | |
Yufen Yu | 228fc7d | 2019-10-30 18:47:02 +0800 | [diff] [blame] | 1783 | if (!spare_disk && ev1 > ev2) |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1784 | ret = 1; |
| 1785 | else |
| 1786 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | } |
Christoph Hellwig | 0fe8034 | 2021-10-18 12:11:06 +0200 | [diff] [blame] | 1788 | if (minor_version) |
| 1789 | sectors = bdev_nr_sectors(rdev->bdev) - rdev->data_offset; |
| 1790 | else |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1791 | sectors = rdev->sb_start; |
| 1792 | if (sectors < le64_to_cpu(sb->data_size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | return -EINVAL; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1794 | rdev->sectors = le64_to_cpu(sb->data_size); |
NeilBrown | 8ed7546 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1795 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | } |
| 1797 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1798 | static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | { |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1800 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1801 | __u64 ev1 = le64_to_cpu(sb->events); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1803 | rdev->raid_disk = -1; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1804 | clear_bit(Faulty, &rdev->flags); |
| 1805 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1806 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1807 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 1808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | if (mddev->raid_disks == 0) { |
| 1810 | mddev->major_version = 1; |
| 1811 | mddev->patch_version = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 1812 | mddev->external = 0; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1813 | mddev->chunk_sectors = le32_to_cpu(sb->chunksize); |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 1814 | mddev->ctime = le64_to_cpu(sb->ctime); |
| 1815 | mddev->utime = le64_to_cpu(sb->utime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | mddev->level = le32_to_cpu(sb->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 1817 | mddev->clevel[0] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | mddev->layout = le32_to_cpu(sb->layout); |
| 1819 | mddev->raid_disks = le32_to_cpu(sb->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1820 | mddev->dev_sectors = le64_to_cpu(sb->size); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1821 | mddev->events = ev1; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1822 | mddev->bitmap_info.offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1823 | mddev->bitmap_info.space = 0; |
| 1824 | /* Default location for bitmap is 1K after superblock |
| 1825 | * using 3K - total of 4K |
| 1826 | */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1827 | mddev->bitmap_info.default_offset = 1024 >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1828 | mddev->bitmap_info.default_space = (4096-1024) >> 9; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1829 | mddev->reshape_backwards = 0; |
| 1830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | mddev->recovery_cp = le64_to_cpu(sb->resync_offset); |
| 1832 | memcpy(mddev->uuid, sb->set_uuid, 16); |
| 1833 | |
| 1834 | mddev->max_disks = (4096-256)/2; |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 1835 | |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 1836 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) && |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1837 | mddev->bitmap_info.file == NULL) { |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 1838 | mddev->bitmap_info.offset = |
| 1839 | (__s32)le32_to_cpu(sb->bitmap_offset); |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 1840 | /* Metadata doesn't record how much space is available. |
| 1841 | * For 1.0, we assume we can use up to the superblock |
| 1842 | * if before, else to 4K beyond superblock. |
| 1843 | * For others, assume no change is possible. |
| 1844 | */ |
| 1845 | if (mddev->minor_version > 0) |
| 1846 | mddev->bitmap_info.space = 0; |
| 1847 | else if (mddev->bitmap_info.offset > 0) |
| 1848 | mddev->bitmap_info.space = |
| 1849 | 8 - mddev->bitmap_info.offset; |
| 1850 | else |
| 1851 | mddev->bitmap_info.space = |
| 1852 | -mddev->bitmap_info.offset; |
| 1853 | } |
NeilBrown | e11e93f | 2007-05-09 02:35:36 -0700 | [diff] [blame] | 1854 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1855 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 1856 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
| 1857 | mddev->delta_disks = le32_to_cpu(sb->delta_disks); |
| 1858 | mddev->new_level = le32_to_cpu(sb->new_level); |
| 1859 | mddev->new_layout = le32_to_cpu(sb->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1860 | mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1861 | if (mddev->delta_disks < 0 || |
| 1862 | (mddev->delta_disks == 0 && |
| 1863 | (le32_to_cpu(sb->feature_map) |
| 1864 | & MD_FEATURE_RESHAPE_BACKWARDS))) |
| 1865 | mddev->reshape_backwards = 1; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1866 | } else { |
| 1867 | mddev->reshape_position = MaxSector; |
| 1868 | mddev->delta_disks = 0; |
| 1869 | mddev->new_level = mddev->level; |
| 1870 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 1871 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 1872 | } |
| 1873 | |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 1874 | if (mddev->level == 0 && |
| 1875 | !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RAID0_LAYOUT)) |
| 1876 | mddev->layout = -1; |
| 1877 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 1878 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL) |
Shaohua Li | a62ab49 | 2016-01-06 14:37:13 -0800 | [diff] [blame] | 1879 | set_bit(MD_HAS_JOURNAL, &mddev->flags); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1880 | |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1881 | if (le32_to_cpu(sb->feature_map) & |
| 1882 | (MD_FEATURE_PPL | MD_FEATURE_MULTIPLE_PPLS)) { |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1883 | if (le32_to_cpu(sb->feature_map) & |
| 1884 | (MD_FEATURE_BITMAP_OFFSET | MD_FEATURE_JOURNAL)) |
| 1885 | return -EINVAL; |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 1886 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_PPL) && |
| 1887 | (le32_to_cpu(sb->feature_map) & |
| 1888 | MD_FEATURE_MULTIPLE_PPLS)) |
| 1889 | return -EINVAL; |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 1890 | set_bit(MD_HAS_PPL, &mddev->flags); |
| 1891 | } |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1892 | } else if (mddev->pers == NULL) { |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1893 | /* Insist of good event counter while assembling, except for |
| 1894 | * spares (which don't need an event count) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | ++ev1; |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1896 | if (rdev->desc_nr >= 0 && |
| 1897 | rdev->desc_nr < le32_to_cpu(sb->max_dev) && |
Song Liu | a3dfbda | 2015-10-08 21:54:11 -0700 | [diff] [blame] | 1898 | (le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < MD_DISK_ROLE_MAX || |
| 1899 | le16_to_cpu(sb->dev_roles[rdev->desc_nr]) == MD_DISK_ROLE_JOURNAL)) |
NeilBrown | be6800a | 2010-05-18 10:17:09 +1000 | [diff] [blame] | 1900 | if (ev1 < mddev->events) |
| 1901 | return -EINVAL; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1902 | } else if (mddev->bitmap) { |
| 1903 | /* If adding to array with a bitmap, then we can accept an |
| 1904 | * older device, but not too old. |
| 1905 | */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1906 | if (ev1 < mddev->bitmap->events_cleared) |
| 1907 | return 0; |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 1908 | if (ev1 < mddev->events) |
| 1909 | set_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | 07d84d10 | 2006-06-26 00:27:56 -0700 | [diff] [blame] | 1910 | } else { |
| 1911 | if (ev1 < mddev->events) |
| 1912 | /* just a hot-add of a new device, leave raid_disk at -1 */ |
| 1913 | return 0; |
| 1914 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | if (mddev->level != LEVEL_MULTIPATH) { |
| 1916 | int role; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1917 | if (rdev->desc_nr < 0 || |
| 1918 | rdev->desc_nr >= le32_to_cpu(sb->max_dev)) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1919 | role = MD_DISK_ROLE_SPARE; |
NeilBrown | 3673f30 | 2009-08-03 10:59:56 +1000 | [diff] [blame] | 1920 | rdev->desc_nr = -1; |
| 1921 | } else |
| 1922 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | switch(role) { |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1924 | case MD_DISK_ROLE_SPARE: /* spare */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | break; |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 1926 | case MD_DISK_ROLE_FAULTY: /* faulty */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1927 | set_bit(Faulty, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | break; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1929 | case MD_DISK_ROLE_JOURNAL: /* journal device */ |
| 1930 | if (!(le32_to_cpu(sb->feature_map) & MD_FEATURE_JOURNAL)) { |
| 1931 | /* journal device without journal feature */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 1932 | 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] | 1933 | return -EINVAL; |
| 1934 | } |
| 1935 | set_bit(Journal, &rdev->flags); |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 1936 | rdev->journal_tail = le64_to_cpu(sb->journal_tail); |
Shaohua Li | 9b15603 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 1937 | rdev->raid_disk = 0; |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 1938 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | default: |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1940 | rdev->saved_raid_disk = role; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1941 | if ((le32_to_cpu(sb->feature_map) & |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1942 | MD_FEATURE_RECOVERY_OFFSET)) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1943 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 1944 | if (!(le32_to_cpu(sb->feature_map) & |
| 1945 | MD_FEATURE_RECOVERY_BITMAP)) |
| 1946 | rdev->saved_raid_disk = -1; |
Guoqing Jiang | 062f5b2a | 2019-07-24 11:09:20 +0200 | [diff] [blame] | 1947 | } else { |
| 1948 | /* |
| 1949 | * If the array is FROZEN, then the device can't |
| 1950 | * be in_sync with rest of array. |
| 1951 | */ |
| 1952 | if (!test_bit(MD_RECOVERY_FROZEN, |
| 1953 | &mddev->recovery)) |
| 1954 | set_bit(In_sync, &rdev->flags); |
| 1955 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | rdev->raid_disk = role; |
| 1957 | break; |
| 1958 | } |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 1959 | if (sb->devflags & WriteMostly1) |
| 1960 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 1961 | if (sb->devflags & FailFast1) |
| 1962 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1963 | if (le32_to_cpu(sb->feature_map) & MD_FEATURE_REPLACEMENT) |
| 1964 | set_bit(Replacement, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1965 | } else /* MULTIPATH are always insync */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1966 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 1967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | return 0; |
| 1969 | } |
| 1970 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1971 | static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | { |
| 1973 | struct mdp_superblock_1 *sb; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1974 | struct md_rdev *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | int max_dev, i; |
| 1976 | /* make rdev->sb match mddev and rdev data. */ |
| 1977 | |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1978 | sb = page_address(rdev->sb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | |
| 1980 | sb->feature_map = 0; |
| 1981 | sb->pad0 = 0; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 1982 | sb->recovery_offset = cpu_to_le64(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | memset(sb->pad3, 0, sizeof(sb->pad3)); |
| 1984 | |
| 1985 | sb->utime = cpu_to_le64((__u64)mddev->utime); |
| 1986 | sb->events = cpu_to_le64(mddev->events); |
| 1987 | if (mddev->in_sync) |
| 1988 | sb->resync_offset = cpu_to_le64(mddev->recovery_cp); |
Shaohua Li | bd18f64 | 2015-09-02 13:49:50 -0700 | [diff] [blame] | 1989 | else if (test_bit(MD_JOURNAL_CLEAN, &mddev->flags)) |
| 1990 | sb->resync_offset = cpu_to_le64(MaxSector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | else |
| 1992 | sb->resync_offset = cpu_to_le64(0); |
| 1993 | |
NeilBrown | 1c05b4b | 2006-10-21 10:24:08 -0700 | [diff] [blame] | 1994 | sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors)); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1995 | |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 1996 | sb->raid_disks = cpu_to_le32(mddev->raid_disks); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1997 | sb->size = cpu_to_le64(mddev->dev_sectors); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1998 | sb->chunksize = cpu_to_le32(mddev->chunk_sectors); |
NeilBrown | 62e1e38 | 2009-05-26 09:40:59 +1000 | [diff] [blame] | 1999 | sb->level = cpu_to_le32(mddev->level); |
| 2000 | sb->layout = cpu_to_le32(mddev->layout); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2001 | if (test_bit(FailFast, &rdev->flags)) |
| 2002 | sb->devflags |= FailFast1; |
| 2003 | else |
| 2004 | sb->devflags &= ~FailFast1; |
NeilBrown | f0ca340 | 2006-02-02 14:28:04 -0800 | [diff] [blame] | 2005 | |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 2006 | if (test_bit(WriteMostly, &rdev->flags)) |
| 2007 | sb->devflags |= WriteMostly1; |
| 2008 | else |
| 2009 | sb->devflags &= ~WriteMostly1; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2010 | sb->data_offset = cpu_to_le64(rdev->data_offset); |
| 2011 | sb->data_size = cpu_to_le64(rdev->sectors); |
NeilBrown | aeb9b211 | 2011-08-25 14:43:08 +1000 | [diff] [blame] | 2012 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 2013 | if (mddev->bitmap && mddev->bitmap_info.file == NULL) { |
| 2014 | sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset); |
NeilBrown | 71c0805 | 2005-09-09 16:23:51 -0700 | [diff] [blame] | 2015 | sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2016 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2017 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2018 | if (rdev->raid_disk >= 0 && !test_bit(Journal, &rdev->flags) && |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2019 | !test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 2020 | sb->feature_map |= |
| 2021 | cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET); |
| 2022 | sb->recovery_offset = |
| 2023 | cpu_to_le64(rdev->recovery_offset); |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2024 | if (rdev->saved_raid_disk >= 0 && mddev->bitmap) |
| 2025 | sb->feature_map |= |
| 2026 | cpu_to_le32(MD_FEATURE_RECOVERY_BITMAP); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2027 | } |
Shaohua Li | 3069aa8 | 2015-08-13 14:31:56 -0700 | [diff] [blame] | 2028 | /* Note: recovery_offset and journal_tail share space */ |
| 2029 | if (test_bit(Journal, &rdev->flags)) |
| 2030 | sb->journal_tail = cpu_to_le64(rdev->journal_tail); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2031 | if (test_bit(Replacement, &rdev->flags)) |
| 2032 | sb->feature_map |= |
| 2033 | cpu_to_le32(MD_FEATURE_REPLACEMENT); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2034 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2035 | if (mddev->reshape_position != MaxSector) { |
| 2036 | sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE); |
| 2037 | sb->reshape_position = cpu_to_le64(mddev->reshape_position); |
| 2038 | sb->new_layout = cpu_to_le32(mddev->new_layout); |
| 2039 | sb->delta_disks = cpu_to_le32(mddev->delta_disks); |
| 2040 | sb->new_level = cpu_to_le32(mddev->new_level); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 2041 | sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2042 | if (mddev->delta_disks == 0 && |
| 2043 | mddev->reshape_backwards) |
| 2044 | sb->feature_map |
| 2045 | |= cpu_to_le32(MD_FEATURE_RESHAPE_BACKWARDS); |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2046 | if (rdev->new_data_offset != rdev->data_offset) { |
| 2047 | sb->feature_map |
| 2048 | |= cpu_to_le32(MD_FEATURE_NEW_OFFSET); |
| 2049 | sb->new_offset = cpu_to_le32((__u32)(rdev->new_data_offset |
| 2050 | - rdev->data_offset)); |
| 2051 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 2052 | } |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 2053 | |
Goldwyn Rodrigues | 3c462c8 | 2015-08-19 07:35:54 +1000 | [diff] [blame] | 2054 | if (mddev_is_clustered(mddev)) |
| 2055 | sb->feature_map |= cpu_to_le32(MD_FEATURE_CLUSTERED); |
| 2056 | |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2057 | if (rdev->badblocks.count == 0) |
| 2058 | /* Nothing to do for bad blocks*/ ; |
| 2059 | else if (sb->bblog_offset == 0) |
| 2060 | /* Cannot record bad blocks on this device */ |
| 2061 | md_error(mddev, rdev); |
| 2062 | else { |
| 2063 | struct badblocks *bb = &rdev->badblocks; |
Christoph Hellwig | ae50640 | 2019-04-04 18:56:13 +0200 | [diff] [blame] | 2064 | __le64 *bbp = (__le64 *)page_address(rdev->bb_page); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2065 | u64 *p = bb->page; |
| 2066 | sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS); |
| 2067 | if (bb->changed) { |
| 2068 | unsigned seq; |
| 2069 | |
| 2070 | retry: |
| 2071 | seq = read_seqbegin(&bb->lock); |
| 2072 | |
| 2073 | memset(bbp, 0xff, PAGE_SIZE); |
| 2074 | |
| 2075 | for (i = 0 ; i < bb->count ; i++) { |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 2076 | u64 internal_bb = p[i]; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2077 | u64 store_bb = ((BB_OFFSET(internal_bb) << 10) |
| 2078 | | BB_LEN(internal_bb)); |
majianpeng | 35f9ac2 | 2012-11-08 08:56:27 +0800 | [diff] [blame] | 2079 | bbp[i] = cpu_to_le64(store_bb); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2080 | } |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2081 | bb->changed = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2082 | if (read_seqretry(&bb->lock, seq)) |
| 2083 | goto retry; |
| 2084 | |
| 2085 | bb->sector = (rdev->sb_start + |
| 2086 | (int)le32_to_cpu(sb->bblog_offset)); |
| 2087 | bb->size = le16_to_cpu(sb->bblog_size); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2088 | } |
| 2089 | } |
| 2090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | max_dev = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2092 | rdev_for_each(rdev2, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | if (rdev2->desc_nr+1 > max_dev) |
| 2094 | max_dev = rdev2->desc_nr+1; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2095 | |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 2096 | if (max_dev > le32_to_cpu(sb->max_dev)) { |
| 2097 | int bmask; |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2098 | sb->max_dev = cpu_to_le32(max_dev); |
NeilBrown | 70471da | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 2099 | rdev->sb_size = max_dev * 2 + 256; |
| 2100 | bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1; |
| 2101 | if (rdev->sb_size & bmask) |
| 2102 | rdev->sb_size = (rdev->sb_size | bmask) + 1; |
NeilBrown | ddcf352 | 2010-09-08 16:48:17 +1000 | [diff] [blame] | 2103 | } else |
| 2104 | max_dev = le32_to_cpu(sb->max_dev); |
| 2105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | for (i=0; i<max_dev;i++) |
Lidong Zhong | 8df7202 | 2017-06-12 10:45:55 +0800 | [diff] [blame] | 2107 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2108 | |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 2109 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) |
| 2110 | sb->feature_map |= cpu_to_le32(MD_FEATURE_JOURNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 2112 | if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 2113 | if (test_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags)) |
| 2114 | sb->feature_map |= |
| 2115 | cpu_to_le32(MD_FEATURE_MULTIPLE_PPLS); |
| 2116 | else |
| 2117 | sb->feature_map |= cpu_to_le32(MD_FEATURE_PPL); |
Artur Paszkiewicz | ea0213e | 2017-03-09 09:59:57 +0100 | [diff] [blame] | 2118 | sb->ppl.offset = cpu_to_le16(rdev->ppl.offset); |
| 2119 | sb->ppl.size = cpu_to_le16(rdev->ppl.size); |
| 2120 | } |
| 2121 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2122 | rdev_for_each(rdev2, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | i = rdev2->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2124 | if (test_bit(Faulty, &rdev2->flags)) |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 2125 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2126 | else if (test_bit(In_sync, &rdev2->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Song Liu | a97b789 | 2015-10-08 21:54:09 -0700 | [diff] [blame] | 2128 | else if (test_bit(Journal, &rdev2->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 2129 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_JOURNAL); |
NeilBrown | 93be75f | 2009-12-14 12:50:06 +1100 | [diff] [blame] | 2130 | else if (rdev2->raid_disk >= 0) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2131 | sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | else |
Song Liu | c4d4c91 | 2015-08-13 14:31:54 -0700 | [diff] [blame] | 2133 | sb->dev_roles[i] = cpu_to_le16(MD_DISK_ROLE_SPARE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | } |
| 2135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | sb->sb_csum = calc_sb_1_csum(sb); |
| 2137 | } |
| 2138 | |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2139 | static sector_t super_1_choose_bm_space(sector_t dev_size) |
| 2140 | { |
| 2141 | sector_t bm_space; |
| 2142 | |
| 2143 | /* if the device is bigger than 8Gig, save 64k for bitmap |
| 2144 | * usage, if bigger than 200Gig, save 128k |
| 2145 | */ |
| 2146 | if (dev_size < 64*2) |
| 2147 | bm_space = 0; |
| 2148 | else if (dev_size - 64*2 >= 200*1024*1024*2) |
| 2149 | bm_space = 128*2; |
| 2150 | else if (dev_size - 4*2 > 8*1024*1024*2) |
| 2151 | bm_space = 64*2; |
| 2152 | else |
| 2153 | bm_space = 4*2; |
| 2154 | return bm_space; |
| 2155 | } |
| 2156 | |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2157 | static unsigned long long |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2158 | 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] | 2159 | { |
| 2160 | struct mdp_superblock_1 *sb; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2161 | sector_t max_sectors; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2162 | if (num_sectors && num_sectors < rdev->mddev->dev_sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2163 | return 0; /* component must fit device */ |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2164 | if (rdev->data_offset != rdev->new_data_offset) |
| 2165 | return 0; /* too confusing */ |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2166 | if (rdev->sb_start < rdev->data_offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2167 | /* minor versions 1 and 2; superblock before data */ |
Christoph Hellwig | 0fe8034 | 2021-10-18 12:11:06 +0200 | [diff] [blame] | 2168 | max_sectors = bdev_nr_sectors(rdev->bdev) - rdev->data_offset; |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2169 | if (!num_sectors || num_sectors > max_sectors) |
| 2170 | num_sectors = max_sectors; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 2171 | } else if (rdev->mddev->bitmap_info.offset) { |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2172 | /* minor version 0 with bitmap we can't move */ |
| 2173 | return 0; |
| 2174 | } else { |
| 2175 | /* minor version 0; superblock after data */ |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2176 | sector_t sb_start, bm_space; |
Christoph Hellwig | 0fe8034 | 2021-10-18 12:11:06 +0200 | [diff] [blame] | 2177 | sector_t dev_size = bdev_nr_sectors(rdev->bdev); |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2178 | |
| 2179 | /* 8K is for superblock */ |
| 2180 | sb_start = dev_size - 8*2; |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2181 | sb_start &= ~(sector_t)(4*2 - 1); |
Xiao Ni | d9c0fa5 | 2020-06-30 15:55:36 +0800 | [diff] [blame] | 2182 | |
| 2183 | bm_space = super_1_choose_bm_space(dev_size); |
| 2184 | |
| 2185 | /* Space that can be used to store date needs to decrease |
| 2186 | * superblock bitmap space and bad block space(4K) |
| 2187 | */ |
| 2188 | max_sectors = sb_start - bm_space - 4*2; |
| 2189 | |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2190 | if (!num_sectors || num_sectors > max_sectors) |
| 2191 | num_sectors = max_sectors; |
Markus Hochholdinger | 55df1ce | 2021-11-16 10:21:35 +0000 | [diff] [blame] | 2192 | rdev->sb_start = sb_start; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2193 | } |
Namhyung Kim | 65a06f06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2194 | sb = page_address(rdev->sb_page); |
Andre Noll | 15f4a5f | 2008-07-21 14:42:12 +1000 | [diff] [blame] | 2195 | sb->data_size = cpu_to_le64(num_sectors); |
Jason Yan | 3fb632e | 2017-03-10 11:27:23 +0800 | [diff] [blame] | 2196 | sb->super_offset = cpu_to_le64(rdev->sb_start); |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2197 | sb->sb_csum = calc_sb_1_csum(sb); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2198 | do { |
| 2199 | md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size, |
| 2200 | rdev->sb_page); |
| 2201 | } while (md_super_wait(rdev->mddev) < 0); |
Justin Maggard | c26a44e | 2010-11-24 16:36:17 +1100 | [diff] [blame] | 2202 | return num_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2203 | |
| 2204 | } |
| 2205 | |
| 2206 | static int |
| 2207 | super_1_allow_new_offset(struct md_rdev *rdev, |
| 2208 | unsigned long long new_offset) |
| 2209 | { |
| 2210 | /* All necessary checks on new >= old have been done */ |
| 2211 | struct bitmap *bitmap; |
| 2212 | if (new_offset >= rdev->data_offset) |
| 2213 | return 1; |
| 2214 | |
| 2215 | /* with 1.0 metadata, there is no metadata to tread on |
| 2216 | * so we can always move back */ |
| 2217 | if (rdev->mddev->minor_version == 0) |
| 2218 | return 1; |
| 2219 | |
| 2220 | /* otherwise we must be sure not to step on |
| 2221 | * any metadata, so stay: |
| 2222 | * 36K beyond start of superblock |
| 2223 | * beyond end of badblocks |
| 2224 | * beyond write-intent bitmap |
| 2225 | */ |
| 2226 | if (rdev->sb_start + (32+4)*2 > new_offset) |
| 2227 | return 0; |
| 2228 | bitmap = rdev->mddev->bitmap; |
| 2229 | if (bitmap && !rdev->mddev->bitmap_info.file && |
| 2230 | rdev->sb_start + rdev->mddev->bitmap_info.offset + |
NeilBrown | 1ec885c | 2012-05-22 13:55:10 +1000 | [diff] [blame] | 2231 | bitmap->storage.file_pages * (PAGE_SIZE>>9) > new_offset) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2232 | return 0; |
| 2233 | if (rdev->badblocks.sector + rdev->badblocks.size > new_offset) |
| 2234 | return 0; |
| 2235 | |
| 2236 | return 1; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 2239 | static struct super_type super_types[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | [0] = { |
| 2241 | .name = "0.90.0", |
| 2242 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2243 | .load_super = super_90_load, |
| 2244 | .validate_super = super_90_validate, |
| 2245 | .sync_super = super_90_sync, |
| 2246 | .rdev_size_change = super_90_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2247 | .allow_new_offset = super_90_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | }, |
| 2249 | [1] = { |
| 2250 | .name = "md-1", |
| 2251 | .owner = THIS_MODULE, |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 2252 | .load_super = super_1_load, |
| 2253 | .validate_super = super_1_validate, |
| 2254 | .sync_super = super_1_sync, |
| 2255 | .rdev_size_change = super_1_rdev_size_change, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2256 | .allow_new_offset = super_1_allow_new_offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | }, |
| 2258 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2260 | static void sync_super(struct mddev *mddev, struct md_rdev *rdev) |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2261 | { |
| 2262 | if (mddev->sync_super) { |
| 2263 | mddev->sync_super(mddev, rdev); |
| 2264 | return; |
| 2265 | } |
| 2266 | |
| 2267 | BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types)); |
| 2268 | |
| 2269 | super_types[mddev->major_version].sync_super(mddev, rdev); |
| 2270 | } |
| 2271 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2272 | static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2274 | struct md_rdev *rdev, *rdev2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2276 | rcu_read_lock(); |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2277 | rdev_for_each_rcu(rdev, mddev1) { |
| 2278 | if (test_bit(Faulty, &rdev->flags) || |
| 2279 | test_bit(Journal, &rdev->flags) || |
| 2280 | rdev->raid_disk == -1) |
| 2281 | continue; |
| 2282 | rdev_for_each_rcu(rdev2, mddev2) { |
| 2283 | if (test_bit(Faulty, &rdev2->flags) || |
| 2284 | test_bit(Journal, &rdev2->flags) || |
| 2285 | rdev2->raid_disk == -1) |
| 2286 | continue; |
Christoph Hellwig | 61a27e1f | 2020-09-03 07:40:58 +0200 | [diff] [blame] | 2287 | if (rdev->bdev->bd_disk == rdev2->bdev->bd_disk) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2288 | rcu_read_unlock(); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2289 | return 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2290 | } |
Song Liu | 0b020e8 | 2015-09-03 23:00:35 -0700 | [diff] [blame] | 2291 | } |
| 2292 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2293 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | return 0; |
| 2295 | } |
| 2296 | |
| 2297 | static LIST_HEAD(pending_raid_disks); |
| 2298 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2299 | /* |
| 2300 | * Try to register data integrity profile for an mddev |
| 2301 | * |
| 2302 | * This is called when an array is started and after a disk has been kicked |
| 2303 | * from the array. It only succeeds if all working and active component devices |
| 2304 | * are integrity capable with matching profiles. |
| 2305 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2306 | int md_integrity_register(struct mddev *mddev) |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2307 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2308 | struct md_rdev *rdev, *reference = NULL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2309 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2310 | if (list_empty(&mddev->disks)) |
| 2311 | return 0; /* nothing to do */ |
Jonathan Brassow | 629acb6 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 2312 | if (!mddev->gendisk || blk_get_integrity(mddev->gendisk)) |
| 2313 | return 0; /* shouldn't register, or already is */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2314 | rdev_for_each(rdev, mddev) { |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2315 | /* skip spares and non-functional disks */ |
| 2316 | if (test_bit(Faulty, &rdev->flags)) |
| 2317 | continue; |
| 2318 | if (rdev->raid_disk < 0) |
| 2319 | continue; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2320 | if (!reference) { |
| 2321 | /* Use the first rdev as the reference */ |
| 2322 | reference = rdev; |
| 2323 | continue; |
| 2324 | } |
| 2325 | /* does this rdev's profile match the reference profile? */ |
| 2326 | if (blk_integrity_compare(reference->bdev->bd_disk, |
| 2327 | rdev->bdev->bd_disk) < 0) |
| 2328 | return -EINVAL; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2329 | } |
Martin K. Petersen | 89078d5 | 2011-03-28 20:09:12 -0400 | [diff] [blame] | 2330 | if (!reference || !bdev_get_integrity(reference->bdev)) |
| 2331 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2332 | /* |
| 2333 | * All component devices are integrity capable and have matching |
| 2334 | * profiles, register the common profile for the md device. |
| 2335 | */ |
Martin K. Petersen | 25520d5 | 2015-10-21 13:19:49 -0400 | [diff] [blame] | 2336 | blk_integrity_register(mddev->gendisk, |
| 2337 | bdev_get_integrity(reference->bdev)); |
| 2338 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2339 | pr_debug("md: data integrity enabled on %s\n", mdname(mddev)); |
Guoqing Jiang | 1076481 | 2021-05-25 17:46:17 +0800 | [diff] [blame] | 2340 | if (bioset_integrity_create(&mddev->bio_set, BIO_POOL_SIZE) || |
Guoqing Jiang | daee202 | 2021-06-03 17:21:06 +0800 | [diff] [blame] | 2341 | (mddev->level != 1 && mddev->level != 10 && |
| 2342 | bioset_integrity_create(&mddev->io_acct_set, BIO_POOL_SIZE))) { |
Guoqing Jiang | de3ea66 | 2021-06-03 17:21:07 +0800 | [diff] [blame] | 2343 | /* |
| 2344 | * No need to handle the failure of bioset_integrity_create, |
| 2345 | * because the function is called by md_run() -> pers->run(), |
| 2346 | * md_run calls bioset_exit -> bioset_integrity_free in case |
| 2347 | * of failure case. |
| 2348 | */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2349 | pr_err("md: failed to create integrity pool for %s\n", |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2350 | mdname(mddev)); |
| 2351 | return -EINVAL; |
| 2352 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2353 | return 0; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2354 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2355 | EXPORT_SYMBOL(md_integrity_register); |
| 2356 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2357 | /* |
| 2358 | * Attempt to add an rdev, but only if it is consistent with the current |
| 2359 | * integrity profile |
| 2360 | */ |
| 2361 | int md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev) |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2362 | { |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2363 | struct blk_integrity *bi_mddev; |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2364 | char name[BDEVNAME_SIZE]; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2365 | |
| 2366 | if (!mddev->gendisk) |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2367 | return 0; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2368 | |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 2369 | bi_mddev = blk_get_integrity(mddev->gendisk); |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2370 | |
| 2371 | if (!bi_mddev) /* nothing to do */ |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2372 | return 0; |
| 2373 | |
| 2374 | if (blk_integrity_compare(mddev->gendisk, rdev->bdev->bd_disk) != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2375 | pr_err("%s: incompatible integrity profile for %s\n", |
| 2376 | mdname(mddev), bdevname(rdev->bdev, name)); |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 2377 | return -ENXIO; |
| 2378 | } |
| 2379 | |
| 2380 | return 0; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 2381 | } |
| 2382 | EXPORT_SYMBOL(md_integrity_add_rdev); |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2383 | |
Christoph Hellwig | d7a4783 | 2021-02-01 14:17:20 +0100 | [diff] [blame] | 2384 | static bool rdev_read_only(struct md_rdev *rdev) |
| 2385 | { |
| 2386 | return bdev_read_only(rdev->bdev) || |
| 2387 | (rdev->meta_bdev && bdev_read_only(rdev->meta_bdev)); |
| 2388 | } |
| 2389 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2390 | 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] | 2391 | { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 2392 | char b[BDEVNAME_SIZE]; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2393 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | |
Dan Williams | 11e2ede | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 2395 | /* prevent duplicates */ |
| 2396 | if (find_rdev(mddev, rdev->bdev->bd_dev)) |
| 2397 | return -EEXIST; |
| 2398 | |
Christoph Hellwig | d7a4783 | 2021-02-01 14:17:20 +0100 | [diff] [blame] | 2399 | if (rdev_read_only(rdev) && mddev->pers) |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 2400 | return -EROFS; |
| 2401 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2402 | /* make sure rdev->sectors exceeds mddev->dev_sectors */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2403 | if (!test_bit(Journal, &rdev->flags) && |
| 2404 | rdev->sectors && |
| 2405 | (mddev->dev_sectors == 0 || rdev->sectors < mddev->dev_sectors)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 2406 | if (mddev->pers) { |
| 2407 | /* Cannot change size, so fail |
| 2408 | * If mddev->level <= 0, then we don't care |
| 2409 | * about aligning sizes (e.g. linear) |
| 2410 | */ |
| 2411 | if (mddev->level > 0) |
| 2412 | return -ENOSPC; |
| 2413 | } else |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2414 | mddev->dev_sectors = rdev->sectors; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 2415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | |
| 2417 | /* Verify rdev->desc_nr is unique. |
| 2418 | * If it is -1, assign a free number, else |
| 2419 | * check number is not in use |
| 2420 | */ |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2421 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | if (rdev->desc_nr < 0) { |
| 2423 | int choice = 0; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2424 | if (mddev->pers) |
| 2425 | choice = mddev->raid_disks; |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2426 | while (md_find_rdev_nr_rcu(mddev, choice)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2427 | choice++; |
| 2428 | rdev->desc_nr = choice; |
| 2429 | } else { |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 2430 | if (md_find_rdev_nr_rcu(mddev, rdev->desc_nr)) { |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2431 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | return -EBUSY; |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | } |
NeilBrown | 4878e9e | 2014-09-25 17:00:11 +1000 | [diff] [blame] | 2435 | rcu_read_unlock(); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 2436 | if (!test_bit(Journal, &rdev->flags) && |
| 2437 | mddev->max_disks && rdev->desc_nr >= mddev->max_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2438 | pr_warn("md: %s: array is limited to %d devices\n", |
| 2439 | mdname(mddev), mddev->max_disks); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 2440 | return -EBUSY; |
| 2441 | } |
NeilBrown | 19133a4 | 2005-11-08 21:39:35 -0800 | [diff] [blame] | 2442 | bdevname(rdev->bdev,b); |
Rasmus Villemoes | 90a9bef | 2015-06-25 15:02:36 -0700 | [diff] [blame] | 2443 | strreplace(b, '/', '!'); |
Greg Kroah-Hartman | 649316b | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | rdev->mddev = mddev; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2446 | pr_debug("md: bind<%s>\n", b); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2447 | |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 2448 | if (mddev->raid_disks) |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 2449 | mddev_create_serial_pool(mddev, rdev, false); |
Guoqing Jiang | 963c555 | 2019-06-14 17:10:36 +0800 | [diff] [blame] | 2450 | |
Greg Kroah-Hartman | b2d6db5 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 2451 | if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b))) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2452 | goto fail; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2453 | |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 2454 | /* failure here is OK */ |
Christoph Hellwig | 8d65269 | 2020-11-17 08:18:55 +0100 | [diff] [blame] | 2455 | err = sysfs_create_link(&rdev->kobj, bdev_kobj(rdev->bdev), "block"); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 2456 | rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2457 | rdev->sysfs_unack_badblocks = |
| 2458 | sysfs_get_dirent_safe(rdev->kobj.sd, "unacknowledged_bad_blocks"); |
| 2459 | rdev->sysfs_badblocks = |
| 2460 | sysfs_get_dirent_safe(rdev->kobj.sd, "bad_blocks"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2461 | |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2462 | list_add_rcu(&rdev->same_set, &mddev->disks); |
Tejun Heo | e09b457 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2463 | bd_link_disk_holder(rdev->bdev, mddev->gendisk); |
NeilBrown | 4044ba5 | 2009-01-09 08:31:11 +1100 | [diff] [blame] | 2464 | |
| 2465 | /* May as well allow recovery to be retried once */ |
NeilBrown | 5389042 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2466 | mddev->recovery_disabled++; |
Martin K. Petersen | 3f9d99c | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 2467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | return 0; |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2469 | |
| 2470 | fail: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2471 | pr_warn("md: failed to register dev-%s for %s\n", |
| 2472 | b, mdname(mddev)); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 2473 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | } |
| 2475 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2476 | static void rdev_delayed_delete(struct work_struct *ws) |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2477 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2478 | struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2479 | kobject_del(&rdev->kobj); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2480 | kobject_put(&rdev->kobj); |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2481 | } |
| 2482 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2483 | static void unbind_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2484 | { |
| 2485 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2486 | |
Tejun Heo | 49731ba | 2011-01-14 18:43:57 +0100 | [diff] [blame] | 2487 | bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2488 | list_del_rcu(&rdev->same_set); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2489 | pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b)); |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 2490 | mddev_destroy_serial_pool(rdev->mddev, rdev, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | rdev->mddev = NULL; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2492 | sysfs_remove_link(&rdev->kobj, "block"); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2493 | sysfs_put(rdev->sysfs_state); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2494 | sysfs_put(rdev->sysfs_unack_badblocks); |
| 2495 | sysfs_put(rdev->sysfs_badblocks); |
NeilBrown | 3c0ee63 | 2008-10-21 13:25:28 +1100 | [diff] [blame] | 2496 | rdev->sysfs_state = NULL; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2497 | rdev->sysfs_unack_badblocks = NULL; |
| 2498 | rdev->sysfs_badblocks = NULL; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 2499 | rdev->badblocks.count = 0; |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2500 | /* We need to delay this, otherwise we can deadlock when |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2501 | * writing to 'remove' to "dev/state". We also need |
| 2502 | * to delay it due to rcu usage. |
NeilBrown | 5792a28 | 2007-04-04 19:08:18 -0700 | [diff] [blame] | 2503 | */ |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 2504 | synchronize_rcu(); |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2505 | INIT_WORK(&rdev->del_work, rdev_delayed_delete); |
NeilBrown | 177a99b | 2008-02-06 01:39:56 -0800 | [diff] [blame] | 2506 | kobject_get(&rdev->kobj); |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 2507 | queue_work(md_rdev_misc_wq, &rdev->del_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | /* |
| 2511 | * prevent the device from being mounted, repartitioned or |
| 2512 | * otherwise reused by a RAID array (or any other kernel |
| 2513 | * subsystem), by bd_claiming the device. |
| 2514 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2515 | 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] | 2516 | { |
| 2517 | int err = 0; |
| 2518 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | |
Tejun Heo | d4d7762 | 2010-11-13 11:55:18 +0100 | [diff] [blame] | 2520 | bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2521 | shared ? (struct md_rdev *)lock_rdev : rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | if (IS_ERR(bdev)) { |
Christoph Hellwig | ea3edd4 | 2020-03-24 08:25:11 +0100 | [diff] [blame] | 2523 | pr_warn("md: could not open device unknown-block(%u,%u).\n", |
| 2524 | MAJOR(dev), MINOR(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | return PTR_ERR(bdev); |
| 2526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | rdev->bdev = bdev; |
| 2528 | return err; |
| 2529 | } |
| 2530 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2531 | static void unlock_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | { |
| 2533 | struct block_device *bdev = rdev->bdev; |
| 2534 | rdev->bdev = NULL; |
Tejun Heo | e525fd8 | 2010-11-13 11:55:17 +0100 | [diff] [blame] | 2535 | blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | void md_autodetect_dev(dev_t dev); |
| 2539 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2540 | static void export_rdev(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | { |
| 2542 | char b[BDEVNAME_SIZE]; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2543 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 2544 | pr_debug("md: export_rdev(%s)\n", bdevname(rdev->bdev,b)); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 2545 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | #ifndef MODULE |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 2547 | if (test_bit(AutoDetected, &rdev->flags)) |
| 2548 | md_autodetect_dev(rdev->bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | #endif |
| 2550 | unlock_rdev(rdev); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2551 | kobject_put(&rdev->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | } |
| 2553 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2554 | void md_kick_rdev_from_array(struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | { |
| 2556 | unbind_rdev_from_array(rdev); |
| 2557 | export_rdev(rdev); |
| 2558 | } |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2559 | EXPORT_SYMBOL_GPL(md_kick_rdev_from_array); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2561 | static void export_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | { |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2563 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | |
NeilBrown | 0638bb0 | 2014-09-25 17:43:47 +1000 | [diff] [blame] | 2565 | while (!list_empty(&mddev->disks)) { |
| 2566 | rdev = list_first_entry(&mddev->disks, struct md_rdev, |
| 2567 | same_set); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 2568 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | mddev->raid_disks = 0; |
| 2571 | mddev->major_version = 0; |
| 2572 | } |
| 2573 | |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2574 | static bool set_in_sync(struct mddev *mddev) |
| 2575 | { |
Shaohua Li | efa4b77 | 2017-10-18 22:08:13 -0700 | [diff] [blame] | 2576 | lockdep_assert_held(&mddev->lock); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2577 | if (!mddev->in_sync) { |
| 2578 | mddev->sync_checkers++; |
| 2579 | spin_unlock(&mddev->lock); |
| 2580 | percpu_ref_switch_to_atomic_sync(&mddev->writes_pending); |
| 2581 | spin_lock(&mddev->lock); |
| 2582 | if (!mddev->in_sync && |
| 2583 | percpu_ref_is_zero(&mddev->writes_pending)) { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2584 | mddev->in_sync = 1; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2585 | /* |
| 2586 | * Ensure ->in_sync is visible before we clear |
| 2587 | * ->sync_checkers. |
| 2588 | */ |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2589 | smp_mb(); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2590 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2591 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 2592 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2593 | if (--mddev->sync_checkers == 0) |
| 2594 | percpu_ref_switch_to_percpu(&mddev->writes_pending); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 2595 | } |
| 2596 | if (mddev->safemode == 1) |
| 2597 | mddev->safemode = 0; |
| 2598 | return mddev->in_sync; |
| 2599 | } |
| 2600 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2601 | static void sync_sbs(struct mddev *mddev, int nospares) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2603 | /* Update each superblock (in-memory image), but |
| 2604 | * if we are allowed to, skip spares which already |
| 2605 | * have the right event counter, or have one earlier |
| 2606 | * (which would mean they aren't being marked as dirty |
| 2607 | * with the rest of the array) |
| 2608 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2609 | struct md_rdev *rdev; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2610 | rdev_for_each(rdev, mddev) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2611 | if (rdev->sb_events == mddev->events || |
| 2612 | (nospares && |
| 2613 | rdev->raid_disk < 0 && |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2614 | rdev->sb_events+1 == mddev->events)) { |
| 2615 | /* Don't update this superblock */ |
| 2616 | rdev->sb_loaded = 2; |
| 2617 | } else { |
Jonathan Brassow | 076f968 | 2011-06-07 17:51:30 -0500 | [diff] [blame] | 2618 | sync_super(mddev, rdev); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2619 | rdev->sb_loaded = 1; |
| 2620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | } |
| 2622 | } |
| 2623 | |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2624 | static bool does_sb_need_changing(struct mddev *mddev) |
| 2625 | { |
| 2626 | struct md_rdev *rdev; |
| 2627 | struct mdp_superblock_1 *sb; |
| 2628 | int role; |
| 2629 | |
| 2630 | /* Find a good rdev */ |
| 2631 | rdev_for_each(rdev, mddev) |
| 2632 | if ((rdev->raid_disk >= 0) && !test_bit(Faulty, &rdev->flags)) |
| 2633 | break; |
| 2634 | |
| 2635 | /* No good device found. */ |
| 2636 | if (!rdev) |
| 2637 | return false; |
| 2638 | |
| 2639 | sb = page_address(rdev->sb_page); |
| 2640 | /* Check if a device has become faulty or a spare become active */ |
| 2641 | rdev_for_each(rdev, mddev) { |
| 2642 | role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]); |
| 2643 | /* Device activated? */ |
| 2644 | if (role == 0xffff && rdev->raid_disk >=0 && |
| 2645 | !test_bit(Faulty, &rdev->flags)) |
| 2646 | return true; |
| 2647 | /* Device turned faulty? */ |
| 2648 | if (test_bit(Faulty, &rdev->flags) && (role < 0xfffd)) |
| 2649 | return true; |
| 2650 | } |
| 2651 | |
| 2652 | /* Check if any mddev parameters have changed */ |
| 2653 | if ((mddev->dev_sectors != le64_to_cpu(sb->size)) || |
| 2654 | (mddev->reshape_position != le64_to_cpu(sb->reshape_position)) || |
Jason Yan | 1345921 | 2017-03-10 11:49:12 +0800 | [diff] [blame] | 2655 | (mddev->layout != le32_to_cpu(sb->layout)) || |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2656 | (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) || |
| 2657 | (mddev->chunk_sectors != le32_to_cpu(sb->chunksize))) |
| 2658 | return true; |
| 2659 | |
| 2660 | return false; |
| 2661 | } |
| 2662 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2663 | void md_update_sb(struct mddev *mddev, int force_change) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2665 | struct md_rdev *rdev; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2666 | int sync_req; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2667 | int nospares = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2668 | int any_badblocks_changed = 0; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2669 | int ret = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2671 | if (mddev->ro) { |
| 2672 | if (force_change) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2673 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | d87f064 | 2013-04-24 11:42:40 +1000 | [diff] [blame] | 2674 | return; |
| 2675 | } |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2676 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2677 | repeat: |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2678 | if (mddev_is_clustered(mddev)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2679 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2680 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2681 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2682 | nospares = 1; |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2683 | ret = md_cluster_ops->metadata_update_start(mddev); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2684 | /* Has someone else has updated the sb */ |
| 2685 | if (!does_sb_need_changing(mddev)) { |
Guoqing Jiang | 23b63f9 | 2015-10-12 17:21:30 +0800 | [diff] [blame] | 2686 | if (ret == 0) |
| 2687 | md_cluster_ops->metadata_update_cancel(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2688 | bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2689 | BIT(MD_SB_CHANGE_DEVS) | |
| 2690 | BIT(MD_SB_CHANGE_CLEAN)); |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 2691 | return; |
| 2692 | } |
| 2693 | } |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2694 | |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 2695 | /* |
| 2696 | * First make sure individual recovery_offsets are correct |
| 2697 | * curr_resync_completed can only be used during recovery. |
| 2698 | * During reshape/resync it might use array-addresses rather |
| 2699 | * that device addresses. |
| 2700 | */ |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2701 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2702 | if (rdev->raid_disk >= 0 && |
| 2703 | mddev->delta_disks >= 0 && |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 2704 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 2705 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery) && |
| 2706 | !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2707 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2708 | !test_bit(In_sync, &rdev->flags) && |
| 2709 | mddev->curr_resync_completed > rdev->recovery_offset) |
| 2710 | rdev->recovery_offset = mddev->curr_resync_completed; |
| 2711 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 2712 | } |
Dan Williams | bd52b74 | 2010-08-30 17:33:33 +1000 | [diff] [blame] | 2713 | if (!mddev->persistent) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2714 | clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 2715 | clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2716 | if (!mddev->external) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2717 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2718 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2719 | if (rdev->badblocks.changed) { |
NeilBrown | d096293 | 2012-03-19 12:46:41 +1100 | [diff] [blame] | 2720 | rdev->badblocks.changed = 0; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2721 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2722 | md_error(mddev, rdev); |
| 2723 | } |
| 2724 | clear_bit(Blocked, &rdev->flags); |
| 2725 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2726 | wake_up(&rdev->blocked_wait); |
| 2727 | } |
| 2728 | } |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2729 | wake_up(&mddev->sb_wait); |
| 2730 | return; |
| 2731 | } |
| 2732 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2733 | spin_lock(&mddev->lock); |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2734 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 2735 | mddev->utime = ktime_get_real_seconds(); |
NeilBrown | 3a3a5dd | 2010-08-16 18:09:31 +1000 | [diff] [blame] | 2736 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2737 | if (test_and_clear_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2738 | force_change = 1; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2739 | if (test_and_clear_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags)) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2740 | /* just a clean<-> dirty transition, possibly leave spares alone, |
| 2741 | * though if events isn't the right even/odd, we will have to do |
| 2742 | * spares after all |
| 2743 | */ |
| 2744 | nospares = 1; |
| 2745 | if (force_change) |
| 2746 | nospares = 0; |
| 2747 | if (mddev->degraded) |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2748 | /* If the array is degraded, then skipping spares is both |
| 2749 | * dangerous and fairly pointless. |
| 2750 | * Dangerous because a device that was removed from the array |
| 2751 | * might have a event_count that still looks up-to-date, |
| 2752 | * so it can be re-added without a resync. |
| 2753 | * Pointless because if there are any spares to skip, |
| 2754 | * then a recovery will happen and soon that array won't |
| 2755 | * be degraded any more and the spare can go back to sleep then. |
| 2756 | */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2757 | nospares = 0; |
NeilBrown | 8469219 | 2006-08-27 01:23:49 -0700 | [diff] [blame] | 2758 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2759 | sync_req = mddev->in_sync; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2760 | |
| 2761 | /* If this is just a dirty<->clean transition, and the array is clean |
| 2762 | * and 'events' is odd, we can roll back to the previous clean state */ |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2763 | if (nospares |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2764 | && (mddev->in_sync && mddev->recovery_cp == MaxSector) |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2765 | && mddev->can_decrease_events |
| 2766 | && mddev->events != 1) { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2767 | mddev->events--; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2768 | mddev->can_decrease_events = 0; |
| 2769 | } else { |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2770 | /* otherwise we have to go forward and ... */ |
| 2771 | mddev->events ++; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 2772 | mddev->can_decrease_events = nospares; |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2773 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 2775 | /* |
| 2776 | * This 64-bit counter should never wrap. |
| 2777 | * Either we are in around ~1 trillion A.C., assuming |
| 2778 | * 1 reboot per second, or we have a bug... |
| 2779 | */ |
| 2780 | WARN_ON(mddev->events == 0); |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2781 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2782 | rdev_for_each(rdev, mddev) { |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2783 | if (rdev->badblocks.changed) |
| 2784 | any_badblocks_changed++; |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2785 | if (test_bit(Faulty, &rdev->flags)) |
| 2786 | set_bit(FaultRecorded, &rdev->flags); |
| 2787 | } |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2788 | |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 2789 | sync_sbs(mddev, nospares); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 2790 | spin_unlock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2792 | pr_debug("md: updating %s RAID superblock on device (in sync %d)\n", |
| 2793 | mdname(mddev), mddev->in_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | |
Shaohua Li | 504634f | 2016-11-18 09:44:08 -0800 | [diff] [blame] | 2795 | if (mddev->queue) |
| 2796 | blk_add_trace_msg(mddev->queue, "md md_update_sb"); |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2797 | rewrite: |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2798 | md_bitmap_update_sb(mddev->bitmap); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2799 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | char b[BDEVNAME_SIZE]; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2801 | |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2802 | if (rdev->sb_loaded != 1) |
| 2803 | continue; /* no noise on spare devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2805 | if (!test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2806 | md_super_write(mddev,rdev, |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 2807 | rdev->sb_start, rdev->sb_size, |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2808 | rdev->sb_page); |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2809 | pr_debug("md: (write) %s's sb offset: %llu\n", |
| 2810 | bdevname(rdev->bdev, b), |
| 2811 | (unsigned long long)rdev->sb_start); |
NeilBrown | 4254376 | 2006-06-26 00:27:57 -0700 | [diff] [blame] | 2812 | rdev->sb_events = mddev->events; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2813 | if (rdev->badblocks.size) { |
| 2814 | md_super_write(mddev, rdev, |
| 2815 | rdev->badblocks.sector, |
| 2816 | rdev->badblocks.size << 9, |
| 2817 | rdev->bb_page); |
| 2818 | rdev->badblocks.size = 0; |
| 2819 | } |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2820 | |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2821 | } else |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 2822 | pr_debug("md: %s (skipping faulty)\n", |
| 2823 | bdevname(rdev->bdev, b)); |
Andrei Warkentin | d70ed2e | 2011-10-18 12:16:48 +1100 | [diff] [blame] | 2824 | |
NeilBrown | 7bfa19f | 2005-06-21 17:17:28 -0700 | [diff] [blame] | 2825 | if (mddev->level == LEVEL_MULTIPATH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | /* only need to write one superblock... */ |
| 2827 | break; |
| 2828 | } |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2829 | if (md_super_wait(mddev) < 0) |
| 2830 | goto rewrite; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2831 | /* 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] | 2832 | |
Guoqing Jiang | 2c97cf1 | 2016-05-02 11:33:09 -0400 | [diff] [blame] | 2833 | if (mddev_is_clustered(mddev) && ret == 0) |
| 2834 | md_cluster_ops->metadata_update_finish(mddev); |
| 2835 | |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 2836 | if (mddev->in_sync != sync_req || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2837 | !bit_clear_unless(&mddev->sb_flags, BIT(MD_SB_CHANGE_PENDING), |
| 2838 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_CLEAN))) |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2839 | /* have to write it out again */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2840 | goto repeat; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 2841 | wake_up(&mddev->sb_wait); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 2842 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 2843 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 2844 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2845 | rdev_for_each(rdev, mddev) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2846 | if (test_and_clear_bit(FaultRecorded, &rdev->flags)) |
| 2847 | clear_bit(Blocked, &rdev->flags); |
| 2848 | |
| 2849 | if (any_badblocks_changed) |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 2850 | ack_all_badblocks(&rdev->badblocks); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2851 | clear_bit(BlockedBadBlocks, &rdev->flags); |
| 2852 | wake_up(&rdev->blocked_wait); |
| 2853 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 2855 | EXPORT_SYMBOL(md_update_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2857 | static int add_bound_rdev(struct md_rdev *rdev) |
| 2858 | { |
| 2859 | struct mddev *mddev = rdev->mddev; |
| 2860 | int err = 0; |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2861 | bool add_journal = test_bit(Journal, &rdev->flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2862 | |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2863 | if (!mddev->pers->hot_remove_disk || add_journal) { |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2864 | /* If there is hot_add_disk but no hot_remove_disk |
| 2865 | * then added disks for geometry changes, |
| 2866 | * and should be added immediately. |
| 2867 | */ |
| 2868 | super_types[mddev->major_version]. |
| 2869 | validate_super(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2870 | if (add_journal) |
| 2871 | mddev_suspend(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2872 | err = mddev->pers->hot_add_disk(mddev, rdev); |
Shaohua Li | 87d4d91 | 2016-01-06 14:37:14 -0800 | [diff] [blame] | 2873 | if (add_journal) |
| 2874 | mddev_resume(mddev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2875 | if (err) { |
Guoqing Jiang | db76767 | 2016-06-02 23:32:05 -0400 | [diff] [blame] | 2876 | md_kick_rdev_from_array(rdev); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2877 | return err; |
| 2878 | } |
| 2879 | } |
| 2880 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
| 2881 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2882 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2883 | if (mddev->degraded) |
| 2884 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
| 2885 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 2886 | md_new_event(); |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 2887 | md_wakeup_thread(mddev->thread); |
| 2888 | return 0; |
| 2889 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | |
Andre Noll | 7f6ce76 | 2008-03-23 18:34:54 +0100 | [diff] [blame] | 2891 | /* words written to sysfs files may, or may not, be \n terminated. |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 2892 | * We want to accept with case. For this we use cmd_match. |
| 2893 | */ |
| 2894 | static int cmd_match(const char *cmd, const char *str) |
| 2895 | { |
| 2896 | /* See if cmd, written into a sysfs file, matches |
| 2897 | * str. They must either be the same, or cmd can |
| 2898 | * have a trailing newline |
| 2899 | */ |
| 2900 | while (*cmd && *str && *cmd == *str) { |
| 2901 | cmd++; |
| 2902 | str++; |
| 2903 | } |
| 2904 | if (*cmd == '\n') |
| 2905 | cmd++; |
| 2906 | if (*str || *cmd) |
| 2907 | return 0; |
| 2908 | return 1; |
| 2909 | } |
| 2910 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2911 | struct rdev_sysfs_entry { |
| 2912 | struct attribute attr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2913 | ssize_t (*show)(struct md_rdev *, char *); |
| 2914 | ssize_t (*store)(struct md_rdev *, const char *, size_t); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2915 | }; |
| 2916 | |
| 2917 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2918 | state_show(struct md_rdev *rdev, char *page) |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2919 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2920 | char *sep = ","; |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 2921 | size_t len = 0; |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 2922 | unsigned long flags = READ_ONCE(rdev->flags); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2923 | |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2924 | if (test_bit(Faulty, &flags) || |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 2925 | (!test_bit(ExternalBbl, &flags) && |
| 2926 | rdev->badblocks.unacked_exist)) |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2927 | len += sprintf(page+len, "faulty%s", sep); |
| 2928 | if (test_bit(In_sync, &flags)) |
| 2929 | len += sprintf(page+len, "in_sync%s", sep); |
| 2930 | if (test_bit(Journal, &flags)) |
| 2931 | len += sprintf(page+len, "journal%s", sep); |
| 2932 | if (test_bit(WriteMostly, &flags)) |
| 2933 | len += sprintf(page+len, "write_mostly%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2934 | if (test_bit(Blocked, &flags) || |
NeilBrown | 52c6415 | 2011-12-08 16:22:48 +1100 | [diff] [blame] | 2935 | (rdev->badblocks.unacked_exist |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2936 | && !test_bit(Faulty, &flags))) |
| 2937 | len += sprintf(page+len, "blocked%s", sep); |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 2938 | if (!test_bit(Faulty, &flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 2939 | !test_bit(Journal, &flags) && |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2940 | !test_bit(In_sync, &flags)) |
| 2941 | len += sprintf(page+len, "spare%s", sep); |
| 2942 | if (test_bit(WriteErrorSeen, &flags)) |
| 2943 | len += sprintf(page+len, "write_error%s", sep); |
| 2944 | if (test_bit(WantReplacement, &flags)) |
| 2945 | len += sprintf(page+len, "want_replacement%s", sep); |
| 2946 | if (test_bit(Replacement, &flags)) |
| 2947 | len += sprintf(page+len, "replacement%s", sep); |
| 2948 | if (test_bit(ExternalBbl, &flags)) |
| 2949 | len += sprintf(page+len, "external_bbl%s", sep); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2950 | if (test_bit(FailFast, &flags)) |
| 2951 | len += sprintf(page+len, "failfast%s", sep); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 2952 | |
| 2953 | if (len) |
| 2954 | len -= strlen(sep); |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 2955 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 2956 | return len+sprintf(page+len, "\n"); |
| 2957 | } |
| 2958 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2959 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2960 | state_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2961 | { |
| 2962 | /* can write |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2963 | * faulty - simulates an error |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2964 | * remove - disconnects the device |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 2965 | * writemostly - sets write_mostly |
| 2966 | * -writemostly - clears write_mostly |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2967 | * blocked - sets the Blocked flags |
| 2968 | * -blocked - clears the Blocked and possibly simulates an error |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 2969 | * insync - sets Insync providing device isn't active |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 2970 | * -insync - clear Insync for a device with a slot assigned, |
| 2971 | * so that it gets rebuilt based on bitmap |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2972 | * write_error - sets WriteErrorSeen |
| 2973 | * -write_error - clears WriteErrorSeen |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 2974 | * {,-}failfast - set/clear FailFast |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2975 | */ |
Xiao Ni | 8b9e229 | 2021-10-13 22:59:33 +0800 | [diff] [blame] | 2976 | |
| 2977 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2978 | int err = -EINVAL; |
Xiao Ni | 8b9e229 | 2021-10-13 22:59:33 +0800 | [diff] [blame] | 2979 | bool need_update_sb = false; |
| 2980 | |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2981 | if (cmd_match(buf, "faulty") && rdev->mddev->pers) { |
| 2982 | md_error(rdev->mddev, rdev); |
NeilBrown | 5ef56c8 | 2011-08-25 14:42:51 +1000 | [diff] [blame] | 2983 | if (test_bit(Faulty, &rdev->flags)) |
| 2984 | err = 0; |
| 2985 | else |
| 2986 | err = -EBUSY; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2987 | } else if (cmd_match(buf, "remove")) { |
Shaohua Li | 5d88178 | 2016-07-28 09:06:34 -0700 | [diff] [blame] | 2988 | if (rdev->mddev->pers) { |
| 2989 | clear_bit(Blocked, &rdev->flags); |
| 2990 | remove_and_add_spares(rdev->mddev, rdev); |
| 2991 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2992 | if (rdev->raid_disk >= 0) |
| 2993 | err = -EBUSY; |
| 2994 | else { |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 2995 | err = 0; |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 2996 | if (mddev_is_clustered(mddev)) |
| 2997 | err = md_cluster_ops->remove_disk(mddev, rdev); |
| 2998 | |
| 2999 | if (err == 0) { |
| 3000 | md_kick_rdev_from_array(rdev); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 3001 | if (mddev->pers) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 3002 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 3003 | md_wakeup_thread(mddev->thread); |
| 3004 | } |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 3005 | md_new_event(); |
Guoqing Jiang | a972090 | 2015-10-12 17:21:27 +0800 | [diff] [blame] | 3006 | } |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3007 | } |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3008 | } else if (cmd_match(buf, "writemostly")) { |
| 3009 | set_bit(WriteMostly, &rdev->flags); |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 3010 | mddev_create_serial_pool(rdev->mddev, rdev, false); |
Xiao Ni | 8b9e229 | 2021-10-13 22:59:33 +0800 | [diff] [blame] | 3011 | need_update_sb = true; |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3012 | err = 0; |
| 3013 | } else if (cmd_match(buf, "-writemostly")) { |
Guoqing Jiang | 11d3a9f | 2019-12-23 10:48:55 +0100 | [diff] [blame] | 3014 | mddev_destroy_serial_pool(rdev->mddev, rdev, false); |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3015 | clear_bit(WriteMostly, &rdev->flags); |
Xiao Ni | 8b9e229 | 2021-10-13 22:59:33 +0800 | [diff] [blame] | 3016 | need_update_sb = true; |
NeilBrown | f655675 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 3017 | err = 0; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3018 | } else if (cmd_match(buf, "blocked")) { |
| 3019 | set_bit(Blocked, &rdev->flags); |
| 3020 | err = 0; |
| 3021 | } else if (cmd_match(buf, "-blocked")) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3022 | if (!test_bit(Faulty, &rdev->flags) && |
Tomasz Majchrzak | dcbcb48 | 2016-10-21 16:27:08 +0200 | [diff] [blame] | 3023 | !test_bit(ExternalBbl, &rdev->flags) && |
NeilBrown | 7da64a0 | 2011-08-30 16:20:17 +1000 | [diff] [blame] | 3024 | rdev->badblocks.unacked_exist) { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3025 | /* metadata handler doesn't understand badblocks, |
| 3026 | * so we need to fail the device |
| 3027 | */ |
| 3028 | md_error(rdev->mddev, rdev); |
| 3029 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3030 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3031 | clear_bit(BlockedBadBlocks, &rdev->flags); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3032 | wake_up(&rdev->blocked_wait); |
| 3033 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3034 | md_wakeup_thread(rdev->mddev->thread); |
| 3035 | |
| 3036 | err = 0; |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3037 | } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) { |
| 3038 | set_bit(In_sync, &rdev->flags); |
| 3039 | err = 0; |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3040 | } else if (cmd_match(buf, "failfast")) { |
| 3041 | set_bit(FailFast, &rdev->flags); |
Xiao Ni | 8b9e229 | 2021-10-13 22:59:33 +0800 | [diff] [blame] | 3042 | need_update_sb = true; |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3043 | err = 0; |
| 3044 | } else if (cmd_match(buf, "-failfast")) { |
| 3045 | clear_bit(FailFast, &rdev->flags); |
Xiao Ni | 8b9e229 | 2021-10-13 22:59:33 +0800 | [diff] [blame] | 3046 | need_update_sb = true; |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3047 | err = 0; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3048 | } else if (cmd_match(buf, "-insync") && rdev->raid_disk >= 0 && |
| 3049 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | e1960f8 | 2014-09-30 15:24:25 +1000 | [diff] [blame] | 3050 | if (rdev->mddev->pers == NULL) { |
| 3051 | clear_bit(In_sync, &rdev->flags); |
| 3052 | rdev->saved_raid_disk = rdev->raid_disk; |
| 3053 | rdev->raid_disk = -1; |
| 3054 | err = 0; |
| 3055 | } |
NeilBrown | d7a9d44 | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3056 | } else if (cmd_match(buf, "write_error")) { |
| 3057 | set_bit(WriteErrorSeen, &rdev->flags); |
| 3058 | err = 0; |
| 3059 | } else if (cmd_match(buf, "-write_error")) { |
| 3060 | clear_bit(WriteErrorSeen, &rdev->flags); |
| 3061 | err = 0; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3062 | } else if (cmd_match(buf, "want_replacement")) { |
| 3063 | /* Any non-spare device that is not a replacement can |
| 3064 | * become want_replacement at any time, but we then need to |
| 3065 | * check if recovery is needed. |
| 3066 | */ |
| 3067 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3068 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 3069 | !test_bit(Replacement, &rdev->flags)) |
| 3070 | set_bit(WantReplacement, &rdev->flags); |
| 3071 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3072 | md_wakeup_thread(rdev->mddev->thread); |
| 3073 | err = 0; |
| 3074 | } else if (cmd_match(buf, "-want_replacement")) { |
| 3075 | /* Clearing 'want_replacement' is always allowed. |
| 3076 | * Once replacements starts it is too late though. |
| 3077 | */ |
| 3078 | err = 0; |
| 3079 | clear_bit(WantReplacement, &rdev->flags); |
| 3080 | } else if (cmd_match(buf, "replacement")) { |
| 3081 | /* Can only set a device as a replacement when array has not |
| 3082 | * yet been started. Once running, replacement is automatic |
| 3083 | * from spares, or by assigning 'slot'. |
| 3084 | */ |
| 3085 | if (rdev->mddev->pers) |
| 3086 | err = -EBUSY; |
| 3087 | else { |
| 3088 | set_bit(Replacement, &rdev->flags); |
| 3089 | err = 0; |
| 3090 | } |
| 3091 | } else if (cmd_match(buf, "-replacement")) { |
| 3092 | /* Similarly, can only clear Replacement before start */ |
| 3093 | if (rdev->mddev->pers) |
| 3094 | err = -EBUSY; |
| 3095 | else { |
| 3096 | clear_bit(Replacement, &rdev->flags); |
| 3097 | err = 0; |
| 3098 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 3099 | } else if (cmd_match(buf, "re-add")) { |
Yufen Yu | ee37e62 | 2019-04-02 14:22:14 +0800 | [diff] [blame] | 3100 | if (!rdev->mddev->pers) |
| 3101 | err = -EINVAL; |
| 3102 | else if (test_bit(Faulty, &rdev->flags) && (rdev->raid_disk == -1) && |
| 3103 | rdev->saved_raid_disk >= 0) { |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 3104 | /* clear_bit is performed _after_ all the devices |
| 3105 | * have their local Faulty bit cleared. If any writes |
| 3106 | * happen in the meantime in the local node, they |
| 3107 | * will land in the local bitmap, which will be synced |
| 3108 | * by this node eventually |
| 3109 | */ |
| 3110 | if (!mddev_is_clustered(rdev->mddev) || |
| 3111 | (err = md_cluster_ops->gather_bitmaps(rdev)) == 0) { |
| 3112 | clear_bit(Faulty, &rdev->flags); |
| 3113 | err = add_bound_rdev(rdev); |
| 3114 | } |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 3115 | } else |
| 3116 | err = -EBUSY; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 3117 | } else if (cmd_match(buf, "external_bbl") && (rdev->mddev->external)) { |
| 3118 | set_bit(ExternalBbl, &rdev->flags); |
| 3119 | rdev->badblocks.shift = 0; |
| 3120 | err = 0; |
| 3121 | } else if (cmd_match(buf, "-external_bbl") && (rdev->mddev->external)) { |
| 3122 | clear_bit(ExternalBbl, &rdev->flags); |
| 3123 | err = 0; |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3124 | } |
Xiao Ni | 8b9e229 | 2021-10-13 22:59:33 +0800 | [diff] [blame] | 3125 | if (need_update_sb) |
| 3126 | md_update_sb(mddev, 1); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3127 | if (!err) |
| 3128 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | 45dc2de | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 3129 | return err ? err : len; |
| 3130 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3131 | static struct rdev_sysfs_entry rdev_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 3132 | __ATTR_PREALLOC(state, S_IRUGO|S_IWUSR, state_show, state_store); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3133 | |
| 3134 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3135 | errors_show(struct md_rdev *rdev, char *page) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3136 | { |
| 3137 | return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors)); |
| 3138 | } |
| 3139 | |
| 3140 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3141 | errors_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3142 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3143 | unsigned int n; |
| 3144 | int rv; |
| 3145 | |
| 3146 | rv = kstrtouint(buf, 10, &n); |
| 3147 | if (rv < 0) |
| 3148 | return rv; |
| 3149 | atomic_set(&rdev->corrected_errors, n); |
| 3150 | return len; |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3151 | } |
| 3152 | static struct rdev_sysfs_entry rdev_errors = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3153 | __ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3154 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3155 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3156 | slot_show(struct md_rdev *rdev, char *page) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3157 | { |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3158 | if (test_bit(Journal, &rdev->flags)) |
| 3159 | return sprintf(page, "journal\n"); |
| 3160 | else if (rdev->raid_disk < 0) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3161 | return sprintf(page, "none\n"); |
| 3162 | else |
| 3163 | return sprintf(page, "%d\n", rdev->raid_disk); |
| 3164 | } |
| 3165 | |
| 3166 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3167 | slot_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3168 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3169 | int slot; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3170 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3171 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3172 | if (test_bit(Journal, &rdev->flags)) |
| 3173 | return -EBUSY; |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3174 | if (strncmp(buf, "none", 4)==0) |
| 3175 | slot = -1; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 3176 | else { |
| 3177 | err = kstrtouint(buf, 10, (unsigned int *)&slot); |
| 3178 | if (err < 0) |
| 3179 | return err; |
| 3180 | } |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3181 | if (rdev->mddev->pers && slot == -1) { |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3182 | /* Setting 'slot' on an active array requires also |
| 3183 | * updating the 'rd%d' link, and communicating |
| 3184 | * with the personality with ->hot_*_disk. |
| 3185 | * For now we only support removing |
| 3186 | * failed/spare devices. This normally happens automatically, |
| 3187 | * but not when the metadata is externally managed. |
| 3188 | */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3189 | if (rdev->raid_disk == -1) |
| 3190 | return -EEXIST; |
| 3191 | /* personality does all needed checks */ |
Namhyung Kim | 01393f3 | 2011-06-09 11:42:54 +1000 | [diff] [blame] | 3192 | if (rdev->mddev->pers->hot_remove_disk == NULL) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3193 | return -EINVAL; |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 3194 | clear_bit(Blocked, &rdev->flags); |
| 3195 | remove_and_add_spares(rdev->mddev, rdev); |
| 3196 | if (rdev->raid_disk >= 0) |
| 3197 | return -EBUSY; |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3198 | set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery); |
| 3199 | md_wakeup_thread(rdev->mddev->thread); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3200 | } else if (rdev->mddev->pers) { |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3201 | /* Activating a spare .. or possibly reactivating |
NeilBrown | 6d56e27 | 2009-04-14 12:01:57 +1000 | [diff] [blame] | 3202 | * if we ever get bitmaps working here. |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3203 | */ |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 3204 | int err; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3205 | |
| 3206 | if (rdev->raid_disk != -1) |
| 3207 | return -EBUSY; |
| 3208 | |
NeilBrown | c6751b2 | 2011-02-02 11:57:13 +1100 | [diff] [blame] | 3209 | if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery)) |
| 3210 | return -EBUSY; |
| 3211 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3212 | if (rdev->mddev->pers->hot_add_disk == NULL) |
| 3213 | return -EINVAL; |
| 3214 | |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3215 | if (slot >= rdev->mddev->raid_disks && |
| 3216 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
| 3217 | return -ENOSPC; |
| 3218 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3219 | rdev->raid_disk = slot; |
| 3220 | if (test_bit(In_sync, &rdev->flags)) |
| 3221 | rdev->saved_raid_disk = slot; |
| 3222 | else |
| 3223 | rdev->saved_raid_disk = -1; |
NeilBrown | d30519f | 2011-10-18 12:13:47 +1100 | [diff] [blame] | 3224 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 3225 | clear_bit(Bitmap_sync, &rdev->flags); |
Guoqing Jiang | 3f79cc2 | 2020-04-04 23:57:11 +0200 | [diff] [blame] | 3226 | err = rdev->mddev->pers->hot_add_disk(rdev->mddev, rdev); |
Goldwyn Rodrigues | cb01c54 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 3227 | if (err) { |
| 3228 | rdev->raid_disk = -1; |
| 3229 | return err; |
| 3230 | } else |
| 3231 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 3232 | /* failure here is OK */; |
| 3233 | sysfs_link_rdev(rdev->mddev, rdev); |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 3234 | /* don't wakeup anyone, leave that to userspace. */ |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3235 | } else { |
NeilBrown | ba1b41b | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 3236 | if (slot >= rdev->mddev->raid_disks && |
| 3237 | slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks) |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3238 | return -ENOSPC; |
| 3239 | rdev->raid_disk = slot; |
| 3240 | /* assume it is working */ |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3241 | clear_bit(Faulty, &rdev->flags); |
| 3242 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3243 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 3244 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
NeilBrown | c303da6 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 3245 | } |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3246 | return len; |
| 3247 | } |
| 3248 | |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3249 | static struct rdev_sysfs_entry rdev_slot = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3250 | __ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store); |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3251 | |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3252 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3253 | offset_show(struct md_rdev *rdev, char *page) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3254 | { |
Andrew Morton | 6961ece | 2006-01-06 00:20:59 -0800 | [diff] [blame] | 3255 | return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3259 | offset_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3260 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3261 | unsigned long long offset; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3262 | if (kstrtoull(buf, 10, &offset) < 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3263 | return -EINVAL; |
Neil Brown | 8ed0a52 | 2008-06-28 08:31:29 +1000 | [diff] [blame] | 3264 | if (rdev->mddev->pers && rdev->raid_disk >= 0) |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3265 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3266 | if (rdev->sectors && rdev->mddev->external) |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3267 | /* Must set offset before size, so overlap checks |
| 3268 | * can be sane */ |
| 3269 | return -EBUSY; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3270 | rdev->data_offset = offset; |
NeilBrown | 25f7fd4 | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 3271 | rdev->new_data_offset = offset; |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3272 | return len; |
| 3273 | } |
| 3274 | |
| 3275 | static struct rdev_sysfs_entry rdev_offset = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3276 | __ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store); |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3277 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3278 | static ssize_t new_offset_show(struct md_rdev *rdev, char *page) |
| 3279 | { |
| 3280 | return sprintf(page, "%llu\n", |
| 3281 | (unsigned long long)rdev->new_data_offset); |
| 3282 | } |
| 3283 | |
| 3284 | static ssize_t new_offset_store(struct md_rdev *rdev, |
| 3285 | const char *buf, size_t len) |
| 3286 | { |
| 3287 | unsigned long long new_offset; |
| 3288 | struct mddev *mddev = rdev->mddev; |
| 3289 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3290 | if (kstrtoull(buf, 10, &new_offset) < 0) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3291 | return -EINVAL; |
| 3292 | |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3293 | if (mddev->sync_thread || |
| 3294 | test_bit(MD_RECOVERY_RUNNING,&mddev->recovery)) |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3295 | return -EBUSY; |
| 3296 | if (new_offset == rdev->data_offset) |
| 3297 | /* reset is always permitted */ |
| 3298 | ; |
| 3299 | else if (new_offset > rdev->data_offset) { |
| 3300 | /* must not push array size beyond rdev_sectors */ |
| 3301 | if (new_offset - rdev->data_offset |
| 3302 | + mddev->dev_sectors > rdev->sectors) |
| 3303 | return -E2BIG; |
| 3304 | } |
| 3305 | /* Metadata worries about other space details. */ |
| 3306 | |
| 3307 | /* decreasing the offset is inconsistent with a backwards |
| 3308 | * reshape. |
| 3309 | */ |
| 3310 | if (new_offset < rdev->data_offset && |
| 3311 | mddev->reshape_backwards) |
| 3312 | return -EINVAL; |
| 3313 | /* Increasing offset is inconsistent with forwards |
| 3314 | * reshape. reshape_direction should be set to |
| 3315 | * 'backwards' first. |
| 3316 | */ |
| 3317 | if (new_offset > rdev->data_offset && |
| 3318 | !mddev->reshape_backwards) |
| 3319 | return -EINVAL; |
| 3320 | |
| 3321 | if (mddev->pers && mddev->persistent && |
| 3322 | !super_types[mddev->major_version] |
| 3323 | .allow_new_offset(rdev, new_offset)) |
| 3324 | return -E2BIG; |
| 3325 | rdev->new_data_offset = new_offset; |
| 3326 | if (new_offset > rdev->data_offset) |
| 3327 | mddev->reshape_backwards = 1; |
| 3328 | else if (new_offset < rdev->data_offset) |
| 3329 | mddev->reshape_backwards = 0; |
| 3330 | |
| 3331 | return len; |
| 3332 | } |
| 3333 | static struct rdev_sysfs_entry rdev_new_offset = |
| 3334 | __ATTR(new_offset, S_IRUGO|S_IWUSR, new_offset_show, new_offset_store); |
| 3335 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3336 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3337 | rdev_size_show(struct md_rdev *rdev, char *page) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3338 | { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3339 | return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3340 | } |
| 3341 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3342 | static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2) |
| 3343 | { |
| 3344 | /* check if two start/length pairs overlap */ |
| 3345 | if (s1+l1 <= s2) |
| 3346 | return 0; |
| 3347 | if (s2+l2 <= s1) |
| 3348 | return 0; |
| 3349 | return 1; |
| 3350 | } |
| 3351 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3352 | static int strict_blocks_to_sectors(const char *buf, sector_t *sectors) |
| 3353 | { |
| 3354 | unsigned long long blocks; |
| 3355 | sector_t new; |
| 3356 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3357 | if (kstrtoull(buf, 10, &blocks) < 0) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3358 | return -EINVAL; |
| 3359 | |
| 3360 | if (blocks & 1ULL << (8 * sizeof(blocks) - 1)) |
| 3361 | return -EINVAL; /* sector conversion overflow */ |
| 3362 | |
| 3363 | new = blocks * 2; |
| 3364 | if (new != blocks * 2) |
| 3365 | return -EINVAL; /* unsigned long long to sector_t overflow */ |
| 3366 | |
| 3367 | *sectors = new; |
| 3368 | return 0; |
| 3369 | } |
| 3370 | |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3371 | static ssize_t |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3372 | rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3373 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3374 | struct mddev *my_mddev = rdev->mddev; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3375 | sector_t oldsectors = rdev->sectors; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3376 | sector_t sectors; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3377 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3378 | if (test_bit(Journal, &rdev->flags)) |
| 3379 | return -EBUSY; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 3380 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3381 | return -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3382 | if (rdev->data_offset != rdev->new_data_offset) |
| 3383 | return -EINVAL; /* too confusing */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3384 | if (my_mddev->pers && rdev->raid_disk >= 0) { |
Neil Brown | d702745 | 2008-07-12 10:37:50 +1000 | [diff] [blame] | 3385 | if (my_mddev->persistent) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3386 | sectors = super_types[my_mddev->major_version]. |
| 3387 | rdev_size_change(rdev, sectors); |
| 3388 | if (!sectors) |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3389 | return -EBUSY; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3390 | } else if (!sectors) |
Christoph Hellwig | 0fe8034 | 2021-10-18 12:11:06 +0200 | [diff] [blame] | 3391 | sectors = bdev_nr_sectors(rdev->bdev) - |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3392 | rdev->data_offset; |
NeilBrown | a646853 | 2013-02-21 14:33:17 +1100 | [diff] [blame] | 3393 | if (!my_mddev->pers->resize) |
| 3394 | /* Cannot change size for RAID0 or Linear etc */ |
| 3395 | return -EINVAL; |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3396 | } |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3397 | if (sectors < my_mddev->dev_sectors) |
Chris Webb | 7d3c6f8 | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 3398 | return -EINVAL; /* component must fit device */ |
Chris Webb | 0cd17fe | 2008-06-28 08:31:46 +1000 | [diff] [blame] | 3399 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3400 | rdev->sectors = sectors; |
| 3401 | if (sectors > oldsectors && my_mddev->external) { |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3402 | /* Need to check that all other rdevs with the same |
| 3403 | * ->bdev do not overlap. 'rcu' is sufficient to walk |
| 3404 | * the rdev lists safely. |
| 3405 | * This check does not provide a hard guarantee, it |
| 3406 | * just helps avoid dangerous mistakes. |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3407 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3408 | struct mddev *mddev; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3409 | int overlap = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 3410 | struct list_head *tmp; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3411 | |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3412 | rcu_read_lock(); |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 3413 | for_each_mddev(mddev, tmp) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3414 | struct md_rdev *rdev2; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3415 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3416 | rdev_for_each(rdev2, mddev) |
NeilBrown | f21e9ff | 2011-01-31 12:10:09 +1100 | [diff] [blame] | 3417 | if (rdev->bdev == rdev2->bdev && |
| 3418 | rdev != rdev2 && |
| 3419 | overlaps(rdev->data_offset, rdev->sectors, |
| 3420 | rdev2->data_offset, |
| 3421 | rdev2->sectors)) { |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3422 | overlap = 1; |
| 3423 | break; |
| 3424 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3425 | if (overlap) { |
| 3426 | mddev_put(mddev); |
| 3427 | break; |
| 3428 | } |
| 3429 | } |
NeilBrown | 8b1afc3 | 2014-09-29 15:33:20 +1000 | [diff] [blame] | 3430 | rcu_read_unlock(); |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3431 | if (overlap) { |
| 3432 | /* Someone else could have slipped in a size |
| 3433 | * change here, but doing so is just silly. |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3434 | * We put oldsectors back because we *know* it is |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3435 | * safe, and trust userspace not to race with |
| 3436 | * itself |
| 3437 | */ |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3438 | rdev->sectors = oldsectors; |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3439 | return -EBUSY; |
| 3440 | } |
| 3441 | } |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3442 | return len; |
| 3443 | } |
| 3444 | |
| 3445 | static struct rdev_sysfs_entry rdev_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3446 | __ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store); |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3447 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3448 | static ssize_t recovery_start_show(struct md_rdev *rdev, char *page) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3449 | { |
| 3450 | unsigned long long recovery_start = rdev->recovery_offset; |
| 3451 | |
| 3452 | if (test_bit(In_sync, &rdev->flags) || |
| 3453 | recovery_start == MaxSector) |
| 3454 | return sprintf(page, "none\n"); |
| 3455 | |
| 3456 | return sprintf(page, "%llu\n", recovery_start); |
| 3457 | } |
| 3458 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3459 | 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] | 3460 | { |
| 3461 | unsigned long long recovery_start; |
| 3462 | |
| 3463 | if (cmd_match(buf, "none")) |
| 3464 | recovery_start = MaxSector; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3465 | else if (kstrtoull(buf, 10, &recovery_start)) |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3466 | return -EINVAL; |
| 3467 | |
| 3468 | if (rdev->mddev->pers && |
| 3469 | rdev->raid_disk >= 0) |
| 3470 | return -EBUSY; |
| 3471 | |
| 3472 | rdev->recovery_offset = recovery_start; |
| 3473 | if (recovery_start == MaxSector) |
| 3474 | set_bit(In_sync, &rdev->flags); |
| 3475 | else |
| 3476 | clear_bit(In_sync, &rdev->flags); |
| 3477 | return len; |
| 3478 | } |
| 3479 | |
| 3480 | static struct rdev_sysfs_entry rdev_recovery_start = |
| 3481 | __ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store); |
| 3482 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3483 | /* sysfs access to bad-blocks list. |
| 3484 | * We present two files. |
| 3485 | * 'bad-blocks' lists sector numbers and lengths of ranges that |
| 3486 | * are recorded as bad. The list is truncated to fit within |
| 3487 | * the one-page limit of sysfs. |
| 3488 | * Writing "sector length" to this file adds an acknowledged |
| 3489 | * bad block list. |
| 3490 | * 'unacknowledged-bad-blocks' lists bad blocks that have not yet |
| 3491 | * been acknowledged. Writing to this file adds bad blocks |
| 3492 | * without acknowledging them. This is largely for testing. |
| 3493 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3494 | static ssize_t bb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3495 | { |
| 3496 | return badblocks_show(&rdev->badblocks, page, 0); |
| 3497 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3498 | 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] | 3499 | { |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 3500 | int rv = badblocks_store(&rdev->badblocks, page, len, 0); |
| 3501 | /* Maybe that ack was all we needed */ |
| 3502 | if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags)) |
| 3503 | wake_up(&rdev->blocked_wait); |
| 3504 | return rv; |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3505 | } |
| 3506 | static struct rdev_sysfs_entry rdev_bad_blocks = |
| 3507 | __ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store); |
| 3508 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3509 | static ssize_t ubb_show(struct md_rdev *rdev, char *page) |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3510 | { |
| 3511 | return badblocks_show(&rdev->badblocks, page, 1); |
| 3512 | } |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3513 | 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] | 3514 | { |
| 3515 | return badblocks_store(&rdev->badblocks, page, len, 1); |
| 3516 | } |
| 3517 | static struct rdev_sysfs_entry rdev_unack_bad_blocks = |
| 3518 | __ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store); |
| 3519 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3520 | static ssize_t |
| 3521 | ppl_sector_show(struct md_rdev *rdev, char *page) |
| 3522 | { |
| 3523 | return sprintf(page, "%llu\n", (unsigned long long)rdev->ppl.sector); |
| 3524 | } |
| 3525 | |
| 3526 | static ssize_t |
| 3527 | ppl_sector_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3528 | { |
| 3529 | unsigned long long sector; |
| 3530 | |
| 3531 | if (kstrtoull(buf, 10, §or) < 0) |
| 3532 | return -EINVAL; |
| 3533 | if (sector != (sector_t)sector) |
| 3534 | return -EINVAL; |
| 3535 | |
| 3536 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3537 | rdev->raid_disk >= 0) |
| 3538 | return -EBUSY; |
| 3539 | |
| 3540 | if (rdev->mddev->persistent) { |
| 3541 | if (rdev->mddev->major_version == 0) |
| 3542 | return -EINVAL; |
| 3543 | if ((sector > rdev->sb_start && |
| 3544 | sector - rdev->sb_start > S16_MAX) || |
| 3545 | (sector < rdev->sb_start && |
| 3546 | rdev->sb_start - sector > -S16_MIN)) |
| 3547 | return -EINVAL; |
| 3548 | rdev->ppl.offset = sector - rdev->sb_start; |
| 3549 | } else if (!rdev->mddev->external) { |
| 3550 | return -EBUSY; |
| 3551 | } |
| 3552 | rdev->ppl.sector = sector; |
| 3553 | return len; |
| 3554 | } |
| 3555 | |
| 3556 | static struct rdev_sysfs_entry rdev_ppl_sector = |
| 3557 | __ATTR(ppl_sector, S_IRUGO|S_IWUSR, ppl_sector_show, ppl_sector_store); |
| 3558 | |
| 3559 | static ssize_t |
| 3560 | ppl_size_show(struct md_rdev *rdev, char *page) |
| 3561 | { |
| 3562 | return sprintf(page, "%u\n", rdev->ppl.size); |
| 3563 | } |
| 3564 | |
| 3565 | static ssize_t |
| 3566 | ppl_size_store(struct md_rdev *rdev, const char *buf, size_t len) |
| 3567 | { |
| 3568 | unsigned int size; |
| 3569 | |
| 3570 | if (kstrtouint(buf, 10, &size) < 0) |
| 3571 | return -EINVAL; |
| 3572 | |
| 3573 | if (rdev->mddev->pers && test_bit(MD_HAS_PPL, &rdev->mddev->flags) && |
| 3574 | rdev->raid_disk >= 0) |
| 3575 | return -EBUSY; |
| 3576 | |
| 3577 | if (rdev->mddev->persistent) { |
| 3578 | if (rdev->mddev->major_version == 0) |
| 3579 | return -EINVAL; |
| 3580 | if (size > U16_MAX) |
| 3581 | return -EINVAL; |
| 3582 | } else if (!rdev->mddev->external) { |
| 3583 | return -EBUSY; |
| 3584 | } |
| 3585 | rdev->ppl.size = size; |
| 3586 | return len; |
| 3587 | } |
| 3588 | |
| 3589 | static struct rdev_sysfs_entry rdev_ppl_size = |
| 3590 | __ATTR(ppl_size, S_IRUGO|S_IWUSR, ppl_size_show, ppl_size_store); |
| 3591 | |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3592 | static struct attribute *rdev_default_attrs[] = { |
| 3593 | &rdev_state.attr, |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 3594 | &rdev_errors.attr, |
NeilBrown | 014236d | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 3595 | &rdev_slot.attr, |
NeilBrown | 93c8cad | 2006-01-06 00:20:56 -0800 | [diff] [blame] | 3596 | &rdev_offset.attr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3597 | &rdev_new_offset.attr, |
NeilBrown | 83303b6 | 2006-01-06 00:21:06 -0800 | [diff] [blame] | 3598 | &rdev_size.attr, |
Dan Williams | 06e3c81 | 2009-12-12 21:17:12 -0700 | [diff] [blame] | 3599 | &rdev_recovery_start.attr, |
NeilBrown | 16c791a | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3600 | &rdev_bad_blocks.attr, |
| 3601 | &rdev_unack_bad_blocks.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 3602 | &rdev_ppl_sector.attr, |
| 3603 | &rdev_ppl_size.attr, |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3604 | NULL, |
| 3605 | }; |
| 3606 | static ssize_t |
| 3607 | rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 3608 | { |
| 3609 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3610 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3611 | |
| 3612 | if (!entry->show) |
| 3613 | return -EIO; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3614 | if (!rdev->mddev) |
Marcos Paulo de Souza | 168b305 | 2019-06-14 15:41:06 -0700 | [diff] [blame] | 3615 | return -ENODEV; |
NeilBrown | 758bfc8 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 3616 | return entry->show(rdev, page); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | static ssize_t |
| 3620 | rdev_attr_store(struct kobject *kobj, struct attribute *attr, |
| 3621 | const char *page, size_t length) |
| 3622 | { |
| 3623 | struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3624 | struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj); |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3625 | ssize_t rv; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3626 | struct mddev *mddev = rdev->mddev; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3627 | |
| 3628 | if (!entry->store) |
| 3629 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 3630 | if (!capable(CAP_SYS_ADMIN)) |
| 3631 | return -EACCES; |
Pawel Baldysiak | c42d324 | 2019-03-27 13:48:21 +0100 | [diff] [blame] | 3632 | rv = mddev ? mddev_lock(mddev) : -ENODEV; |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3633 | if (!rv) { |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3634 | if (rdev->mddev == NULL) |
Pawel Baldysiak | c42d324 | 2019-03-27 13:48:21 +0100 | [diff] [blame] | 3635 | rv = -ENODEV; |
NeilBrown | 27c529b | 2008-03-04 14:29:33 -0800 | [diff] [blame] | 3636 | else |
| 3637 | rv = entry->store(rdev, page, length); |
Dan Williams | 6a51830 | 2008-04-30 00:52:28 -0700 | [diff] [blame] | 3638 | mddev_unlock(mddev); |
NeilBrown | ca38805 | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 3639 | } |
| 3640 | return rv; |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3641 | } |
| 3642 | |
| 3643 | static void rdev_free(struct kobject *ko) |
| 3644 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3645 | struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3646 | kfree(rdev); |
| 3647 | } |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 3648 | static const struct sysfs_ops rdev_sysfs_ops = { |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3649 | .show = rdev_attr_show, |
| 3650 | .store = rdev_attr_store, |
| 3651 | }; |
| 3652 | static struct kobj_type rdev_ktype = { |
| 3653 | .release = rdev_free, |
| 3654 | .sysfs_ops = &rdev_sysfs_ops, |
| 3655 | .default_attrs = rdev_default_attrs, |
| 3656 | }; |
| 3657 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3658 | int md_rdev_init(struct md_rdev *rdev) |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3659 | { |
| 3660 | rdev->desc_nr = -1; |
| 3661 | rdev->saved_raid_disk = -1; |
| 3662 | rdev->raid_disk = -1; |
| 3663 | rdev->flags = 0; |
| 3664 | rdev->data_offset = 0; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3665 | rdev->new_data_offset = 0; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3666 | rdev->sb_events = 0; |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 3667 | rdev->last_read_error = 0; |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3668 | rdev->sb_loaded = 0; |
| 3669 | rdev->bb_page = NULL; |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3670 | atomic_set(&rdev->nr_pending, 0); |
| 3671 | atomic_set(&rdev->read_errors, 0); |
| 3672 | atomic_set(&rdev->corrected_errors, 0); |
| 3673 | |
| 3674 | INIT_LIST_HEAD(&rdev->same_set); |
| 3675 | init_waitqueue_head(&rdev->blocked_wait); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3676 | |
| 3677 | /* Add space to store bad block list. |
| 3678 | * This reserves the space even on arrays where it cannot |
| 3679 | * be used - I wonder if that matters |
| 3680 | */ |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 3681 | return badblocks_init(&rdev->badblocks, 0); |
NeilBrown | e8bb9a8 | 2010-06-01 19:37:26 +1000 | [diff] [blame] | 3682 | } |
| 3683 | EXPORT_SYMBOL_GPL(md_rdev_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | /* |
| 3685 | * Import a device. If 'super_format' >= 0, then sanity check the superblock |
| 3686 | * |
| 3687 | * mark the device faulty if: |
| 3688 | * |
| 3689 | * - the device is nonexistent (zero size) |
| 3690 | * - the device has no valid superblock |
| 3691 | * |
| 3692 | * a faulty rdev _never_ has rdev->sb set. |
| 3693 | */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3694 | 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] | 3695 | { |
| 3696 | char b[BDEVNAME_SIZE]; |
| 3697 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3698 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3699 | sector_t size; |
| 3700 | |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 3701 | rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3702 | if (!rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3703 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 3705 | err = md_rdev_init(rdev); |
| 3706 | if (err) |
| 3707 | goto abort_free; |
| 3708 | err = alloc_disk_sb(rdev); |
| 3709 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | goto abort_free; |
| 3711 | |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 3712 | err = lock_rdev(rdev, newdev, super_format == -2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | if (err) |
| 3714 | goto abort_free; |
| 3715 | |
Greg Kroah-Hartman | f9cb074 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 3716 | kobject_init(&rdev->kobj, &rdev_ktype); |
NeilBrown | 86e6ffd | 2005-11-08 21:39:24 -0800 | [diff] [blame] | 3717 | |
Christoph Hellwig | 0fe8034 | 2021-10-18 12:11:06 +0200 | [diff] [blame] | 3718 | size = bdev_nr_bytes(rdev->bdev) >> BLOCK_SIZE_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | if (!size) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3720 | pr_warn("md: %s has zero or unknown size, marking faulty!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3721 | bdevname(rdev->bdev,b)); |
| 3722 | err = -EINVAL; |
| 3723 | goto abort_free; |
| 3724 | } |
| 3725 | |
| 3726 | if (super_format >= 0) { |
| 3727 | err = super_types[super_format]. |
| 3728 | load_super(rdev, NULL, super_minor); |
| 3729 | if (err == -EINVAL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3730 | 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] | 3731 | bdevname(rdev->bdev,b), |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3732 | super_format, super_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | goto abort_free; |
| 3734 | } |
| 3735 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3736 | pr_warn("md: could not read %s's sb, not importing!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | bdevname(rdev->bdev,b)); |
| 3738 | goto abort_free; |
| 3739 | } |
| 3740 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | return rdev; |
| 3743 | |
| 3744 | abort_free: |
NeilBrown | 2699b67 | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3745 | if (rdev->bdev) |
| 3746 | unlock_rdev(rdev); |
NeilBrown | 545c879 | 2012-05-22 13:54:30 +1000 | [diff] [blame] | 3747 | md_rdev_clear(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | kfree(rdev); |
| 3749 | return ERR_PTR(err); |
| 3750 | } |
| 3751 | |
| 3752 | /* |
| 3753 | * Check a full RAID array for plausibility |
| 3754 | */ |
| 3755 | |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3756 | static int analyze_sbs(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | { |
| 3758 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3759 | struct md_rdev *rdev, *freshest, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | char b[BDEVNAME_SIZE]; |
| 3761 | |
| 3762 | freshest = NULL; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3763 | rdev_for_each_safe(rdev, tmp, mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | switch (super_types[mddev->major_version]. |
| 3765 | load_super(rdev, freshest, mddev->minor_version)) { |
| 3766 | case 1: |
| 3767 | freshest = rdev; |
| 3768 | break; |
| 3769 | case 0: |
| 3770 | break; |
| 3771 | default: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3772 | pr_warn("md: fatal superblock inconsistency in %s -- removing from array\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3773 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3774 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3775 | } |
| 3776 | |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3777 | /* Cannot find a valid fresh disk */ |
| 3778 | if (!freshest) { |
| 3779 | pr_warn("md: cannot find a valid disk\n"); |
| 3780 | return -EINVAL; |
| 3781 | } |
| 3782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | super_types[mddev->major_version]. |
| 3784 | validate_super(mddev, freshest); |
| 3785 | |
| 3786 | i = 0; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3787 | rdev_for_each_safe(rdev, tmp, mddev) { |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 3788 | if (mddev->max_disks && |
| 3789 | (rdev->desc_nr >= mddev->max_disks || |
| 3790 | i > mddev->max_disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3791 | pr_warn("md: %s: %s: only %d devices permitted\n", |
| 3792 | mdname(mddev), bdevname(rdev->bdev, b), |
| 3793 | mddev->max_disks); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3794 | md_kick_rdev_from_array(rdev); |
NeilBrown | de01dfa | 2009-02-06 18:02:46 +1100 | [diff] [blame] | 3795 | continue; |
| 3796 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3797 | if (rdev != freshest) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3798 | if (super_types[mddev->major_version]. |
| 3799 | validate_super(mddev, rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3800 | pr_warn("md: kicking non-fresh %s from array!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | bdevname(rdev->bdev,b)); |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 3802 | md_kick_rdev_from_array(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | continue; |
| 3804 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 3805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3806 | if (mddev->level == LEVEL_MULTIPATH) { |
| 3807 | rdev->desc_nr = i++; |
| 3808 | rdev->raid_disk = rdev->desc_nr; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3809 | set_bit(In_sync, &rdev->flags); |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 3810 | } else if (rdev->raid_disk >= |
| 3811 | (mddev->raid_disks - min(0, mddev->delta_disks)) && |
| 3812 | !test_bit(Journal, &rdev->flags)) { |
NeilBrown | a778b73 | 2007-05-23 13:58:10 -0700 | [diff] [blame] | 3813 | rdev->raid_disk = -1; |
| 3814 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | } |
| 3816 | } |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 3817 | |
| 3818 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | } |
| 3820 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3821 | /* Read a fixed-point number. |
| 3822 | * Numbers in sysfs attributes should be in "standard" units where |
| 3823 | * possible, so time should be in seconds. |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 3824 | * However we internally use a a much smaller unit such as |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3825 | * milliseconds or jiffies. |
| 3826 | * This function takes a decimal number with a possible fractional |
| 3827 | * component, and produces an integer which is the result of |
| 3828 | * multiplying that number by 10^'scale'. |
| 3829 | * all without any floating-point arithmetic. |
| 3830 | */ |
| 3831 | int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale) |
| 3832 | { |
| 3833 | unsigned long result = 0; |
| 3834 | long decimals = -1; |
| 3835 | while (isdigit(*cp) || (*cp == '.' && decimals < 0)) { |
| 3836 | if (*cp == '.') |
| 3837 | decimals = 0; |
| 3838 | else if (decimals < scale) { |
| 3839 | unsigned int value; |
| 3840 | value = *cp - '0'; |
| 3841 | result = result * 10 + value; |
| 3842 | if (decimals >= 0) |
| 3843 | decimals++; |
| 3844 | } |
| 3845 | cp++; |
| 3846 | } |
| 3847 | if (*cp == '\n') |
| 3848 | cp++; |
| 3849 | if (*cp) |
| 3850 | return -EINVAL; |
| 3851 | if (decimals < 0) |
| 3852 | decimals = 0; |
Andy Shevchenko | cf89160 | 2019-07-23 23:41:55 +0300 | [diff] [blame] | 3853 | *res = result * int_pow(10, scale - decimals); |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3854 | return 0; |
| 3855 | } |
| 3856 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3857 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3858 | safe_delay_show(struct mddev *mddev, char *page) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3859 | { |
| 3860 | int msec = (mddev->safemode_delay*1000)/HZ; |
| 3861 | return sprintf(page, "%d.%03d\n", msec/1000, msec%1000); |
| 3862 | } |
| 3863 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3864 | safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3865 | { |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3866 | unsigned long msec; |
Dan Williams | 97ce0a7 | 2008-09-24 22:48:19 -0700 | [diff] [blame] | 3867 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3868 | if (mddev_is_clustered(mddev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3869 | pr_warn("md: Safemode is disabled for clustered mode\n"); |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 3870 | return -EINVAL; |
| 3871 | } |
| 3872 | |
NeilBrown | 72e0207 | 2009-12-14 12:49:55 +1100 | [diff] [blame] | 3873 | if (strict_strtoul_scaled(cbuf, &msec, 3) < 0) |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3874 | return -EINVAL; |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3875 | if (msec == 0) |
| 3876 | mddev->safemode_delay = 0; |
| 3877 | else { |
NeilBrown | 19052c0 | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 3878 | unsigned long old_delay = mddev->safemode_delay; |
NeilBrown | 1b30e66 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 3879 | unsigned long new_delay = (msec*HZ)/1000; |
| 3880 | |
| 3881 | if (new_delay == 0) |
| 3882 | new_delay = 1; |
| 3883 | mddev->safemode_delay = new_delay; |
| 3884 | if (new_delay < old_delay || old_delay == 0) |
| 3885 | mod_timer(&mddev->safemode_timer, jiffies+1); |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 3886 | } |
| 3887 | return len; |
| 3888 | } |
| 3889 | static struct md_sysfs_entry md_safe_delay = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 3890 | __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] | 3891 | |
| 3892 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3893 | level_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3894 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3895 | struct md_personality *p; |
| 3896 | int ret; |
| 3897 | spin_lock(&mddev->lock); |
| 3898 | p = mddev->pers; |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3899 | if (p) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3900 | ret = sprintf(page, "%s\n", p->name); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3901 | else if (mddev->clevel[0]) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3902 | ret = sprintf(page, "%s\n", mddev->clevel); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3903 | else if (mddev->level != LEVEL_NONE) |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3904 | ret = sprintf(page, "%d\n", mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3905 | else |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3906 | ret = 0; |
| 3907 | spin_unlock(&mddev->lock); |
| 3908 | return ret; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 3909 | } |
| 3910 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3911 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3912 | level_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3913 | { |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3914 | char clevel[16]; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3915 | ssize_t rv; |
| 3916 | size_t slen = len; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3917 | struct md_personality *pers, *oldpers; |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3918 | long level; |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3919 | void *priv, *oldpriv; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3920 | struct md_rdev *rdev; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3921 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3922 | if (slen == 0 || slen >= sizeof(clevel)) |
| 3923 | return -EINVAL; |
| 3924 | |
| 3925 | rv = mddev_lock(mddev); |
| 3926 | if (rv) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3927 | return rv; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3928 | |
| 3929 | if (mddev->pers == NULL) { |
| 3930 | strncpy(mddev->clevel, buf, slen); |
| 3931 | if (mddev->clevel[slen-1] == '\n') |
| 3932 | slen--; |
| 3933 | mddev->clevel[slen] = 0; |
| 3934 | mddev->level = LEVEL_NONE; |
| 3935 | rv = len; |
| 3936 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3937 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3938 | rv = -EROFS; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 3939 | if (mddev->ro) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3940 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3941 | |
| 3942 | /* request to change the personality. Need to ensure: |
| 3943 | * - array is not engaged in resync/recovery/reshape |
| 3944 | * - old personality can be suspended |
| 3945 | * - new personality will access other array. |
| 3946 | */ |
| 3947 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3948 | rv = -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3949 | if (mddev->sync_thread || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 3950 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 3951 | mddev->reshape_position != MaxSector || |
| 3952 | mddev->sysfs_active) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3953 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3954 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3955 | rv = -EINVAL; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3956 | if (!mddev->pers->quiesce) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3957 | pr_warn("md: %s: %s does not support online personality change\n", |
| 3958 | mdname(mddev), mddev->pers->name); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3959 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | /* Now find the new personality */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3963 | strncpy(clevel, buf, slen); |
| 3964 | if (clevel[slen-1] == '\n') |
| 3965 | slen--; |
| 3966 | clevel[slen] = 0; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 3967 | if (kstrtol(clevel, 10, &level)) |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3968 | level = LEVEL_NONE; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3969 | |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3970 | if (request_module("md-%s", clevel) != 0) |
| 3971 | request_module("md-level-%s", clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3972 | spin_lock(&pers_lock); |
Dan Williams | f2859af | 2010-05-02 10:04:16 -0700 | [diff] [blame] | 3973 | pers = find_pers(level, clevel); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3974 | if (!pers || !try_module_get(pers->owner)) { |
| 3975 | spin_unlock(&pers_lock); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3976 | pr_warn("md: personality %s not loaded\n", clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3977 | rv = -EINVAL; |
| 3978 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3979 | } |
| 3980 | spin_unlock(&pers_lock); |
| 3981 | |
| 3982 | if (pers == mddev->pers) { |
| 3983 | /* Nothing to do! */ |
| 3984 | module_put(pers->owner); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3985 | rv = len; |
| 3986 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3987 | } |
| 3988 | if (!pers->takeover) { |
| 3989 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 3990 | pr_warn("md: %s: %s does not support personality takeover\n", |
| 3991 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 3992 | rv = -EINVAL; |
| 3993 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3994 | } |
| 3995 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3996 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3997 | rdev->new_raid_disk = rdev->raid_disk; |
| 3998 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3999 | /* ->takeover must set new_* and/or delta_disks |
| 4000 | * if it succeeds, and may set them when it fails. |
| 4001 | */ |
| 4002 | priv = pers->takeover(mddev); |
| 4003 | if (IS_ERR(priv)) { |
| 4004 | mddev->new_level = mddev->level; |
| 4005 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4006 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4007 | mddev->raid_disks -= mddev->delta_disks; |
| 4008 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4009 | mddev->reshape_backwards = 0; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4010 | module_put(pers->owner); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4011 | pr_warn("md: %s: %s would not accept array\n", |
| 4012 | mdname(mddev), clevel); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4013 | rv = PTR_ERR(priv); |
| 4014 | goto out_unlock; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | /* Looks like we have a winner */ |
| 4018 | mddev_suspend(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 4019 | mddev_detach(mddev); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4020 | |
| 4021 | spin_lock(&mddev->lock); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4022 | oldpers = mddev->pers; |
| 4023 | oldpriv = mddev->private; |
| 4024 | mddev->pers = pers; |
| 4025 | mddev->private = priv; |
| 4026 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
| 4027 | mddev->level = mddev->new_level; |
| 4028 | mddev->layout = mddev->new_layout; |
| 4029 | mddev->chunk_sectors = mddev->new_chunk_sectors; |
| 4030 | mddev->delta_disks = 0; |
| 4031 | mddev->reshape_backwards = 0; |
| 4032 | mddev->degraded = 0; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4033 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4034 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4035 | if (oldpers->sync_request == NULL && |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 4036 | mddev->external) { |
| 4037 | /* We are converting from a no-redundancy array |
| 4038 | * to a redundancy array and metadata is managed |
| 4039 | * externally so we need to be sure that writes |
| 4040 | * won't block due to a need to transition |
| 4041 | * clean->dirty |
| 4042 | * until external management is started. |
| 4043 | */ |
| 4044 | mddev->in_sync = 0; |
| 4045 | mddev->safemode_delay = 0; |
| 4046 | mddev->safemode = 0; |
| 4047 | } |
| 4048 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4049 | oldpers->free(mddev, oldpriv); |
| 4050 | |
| 4051 | if (oldpers->sync_request == NULL && |
| 4052 | pers->sync_request != NULL) { |
| 4053 | /* need to add the md_redundancy_group */ |
| 4054 | if (sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4055 | pr_warn("md: cannot register extra attributes for %s\n", |
| 4056 | mdname(mddev)); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4057 | mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action"); |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 4058 | mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed"); |
| 4059 | mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded"); |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4060 | } |
| 4061 | if (oldpers->sync_request != NULL && |
| 4062 | pers->sync_request == NULL) { |
| 4063 | /* need to remove the md_redundancy_group */ |
| 4064 | if (mddev->to_remove == NULL) |
| 4065 | mddev->to_remove = &md_redundancy_group; |
| 4066 | } |
| 4067 | |
Alexey Obitotskiy | 4cb9da7 | 2016-06-23 12:11:01 +0200 | [diff] [blame] | 4068 | module_put(oldpers->owner); |
| 4069 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4070 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4071 | if (rdev->raid_disk < 0) |
| 4072 | continue; |
NeilBrown | bf2cb0d | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4073 | if (rdev->new_raid_disk >= mddev->raid_disks) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4074 | rdev->new_raid_disk = -1; |
| 4075 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 4076 | continue; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4077 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4078 | } |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4079 | rdev_for_each(rdev, mddev) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4080 | if (rdev->raid_disk < 0) |
| 4081 | continue; |
| 4082 | if (rdev->new_raid_disk == rdev->raid_disk) |
| 4083 | continue; |
| 4084 | rdev->raid_disk = rdev->new_raid_disk; |
| 4085 | if (rdev->raid_disk < 0) |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 4086 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4087 | else { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4088 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 4089 | pr_warn("md: cannot register rd%d for %s after level change\n", |
| 4090 | rdev->raid_disk, mdname(mddev)); |
NeilBrown | 3a981b0 | 2009-08-03 10:59:55 +1000 | [diff] [blame] | 4091 | } |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4092 | } |
| 4093 | |
NeilBrown | db721d3 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4094 | if (pers->sync_request == NULL) { |
Trela, Maciej | 9af204c | 2010-03-08 16:02:44 +1100 | [diff] [blame] | 4095 | /* this is now an array without redundancy, so |
| 4096 | * it must always be in_sync |
| 4097 | */ |
| 4098 | mddev->in_sync = 1; |
| 4099 | del_timer_sync(&mddev->safemode_timer); |
| 4100 | } |
NeilBrown | 02e5f5c | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 4101 | blk_set_stacking_limits(&mddev->queue->limits); |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 4102 | pers->run(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4103 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | 47525e5 | 2012-05-22 13:55:29 +1000 | [diff] [blame] | 4104 | mddev_resume(mddev); |
NeilBrown | 830778a | 2014-01-14 15:17:03 +1100 | [diff] [blame] | 4105 | if (!mddev->thread) |
| 4106 | md_update_sb(mddev, 1); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 4107 | sysfs_notify_dirent_safe(mddev->sysfs_level); |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 4108 | md_new_event(); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4109 | rv = len; |
| 4110 | out_unlock: |
| 4111 | mddev_unlock(mddev); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4112 | return rv; |
| 4113 | } |
| 4114 | |
| 4115 | static struct md_sysfs_entry md_level = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4116 | __ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4117 | |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4118 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4119 | layout_show(struct mddev *mddev, char *page) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4120 | { |
| 4121 | /* just a number, not meaningful for all levels */ |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4122 | if (mddev->reshape_position != MaxSector && |
| 4123 | mddev->layout != mddev->new_layout) |
| 4124 | return sprintf(page, "%d (%d)\n", |
| 4125 | mddev->new_layout, mddev->layout); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4126 | return sprintf(page, "%d\n", mddev->layout); |
| 4127 | } |
| 4128 | |
| 4129 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4130 | layout_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4131 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4132 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4133 | int err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4134 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4135 | err = kstrtouint(buf, 10, &n); |
| 4136 | if (err < 0) |
| 4137 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4138 | err = mddev_lock(mddev); |
| 4139 | if (err) |
| 4140 | return err; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4141 | |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4142 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 4143 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4144 | err = -EBUSY; |
| 4145 | else if (mddev->ro) |
| 4146 | err = -EROFS; |
| 4147 | else { |
| 4148 | mddev->new_layout = n; |
| 4149 | err = mddev->pers->check_reshape(mddev); |
| 4150 | if (err) |
| 4151 | mddev->new_layout = mddev->layout; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 4152 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4153 | } else { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4154 | mddev->new_layout = n; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4155 | if (mddev->reshape_position == MaxSector) |
| 4156 | mddev->layout = n; |
| 4157 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4158 | mddev_unlock(mddev); |
| 4159 | return err ?: len; |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4160 | } |
| 4161 | static struct md_sysfs_entry md_layout = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4162 | __ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store); |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 4163 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4164 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4165 | raid_disks_show(struct mddev *mddev, char *page) |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4166 | { |
NeilBrown | bb63654 | 2005-11-08 21:39:45 -0800 | [diff] [blame] | 4167 | if (mddev->raid_disks == 0) |
| 4168 | return 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4169 | if (mddev->reshape_position != MaxSector && |
| 4170 | mddev->delta_disks != 0) |
| 4171 | return sprintf(page, "%d (%d)\n", mddev->raid_disks, |
| 4172 | mddev->raid_disks - mddev->delta_disks); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4173 | return sprintf(page, "%d\n", mddev->raid_disks); |
| 4174 | } |
| 4175 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4176 | static int update_raid_disks(struct mddev *mddev, int raid_disks); |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4177 | |
| 4178 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4179 | raid_disks_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4180 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4181 | unsigned int n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4182 | int err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4183 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4184 | err = kstrtouint(buf, 10, &n); |
| 4185 | if (err < 0) |
| 4186 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4187 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4188 | err = mddev_lock(mddev); |
| 4189 | if (err) |
| 4190 | return err; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4191 | if (mddev->pers) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4192 | err = update_raid_disks(mddev, n); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4193 | else if (mddev->reshape_position != MaxSector) { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4194 | struct md_rdev *rdev; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4195 | int olddisks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4196 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4197 | err = -EINVAL; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4198 | rdev_for_each(rdev, mddev) { |
| 4199 | if (olddisks < n && |
| 4200 | rdev->data_offset < rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4201 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4202 | if (olddisks > n && |
| 4203 | rdev->data_offset > rdev->new_data_offset) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4204 | goto out_unlock; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4205 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4206 | err = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4207 | mddev->delta_disks = n - olddisks; |
| 4208 | mddev->raid_disks = n; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4209 | mddev->reshape_backwards = (mddev->delta_disks < 0); |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4210 | } else |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4211 | mddev->raid_disks = n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4212 | out_unlock: |
| 4213 | mddev_unlock(mddev); |
| 4214 | return err ? err : len; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 4215 | } |
| 4216 | static struct md_sysfs_entry md_raid_disks = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4217 | __ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 4218 | |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4219 | static ssize_t |
Sebastian Parschauer | ec164d07 | 2020-07-28 12:01:39 +0200 | [diff] [blame] | 4220 | uuid_show(struct mddev *mddev, char *page) |
| 4221 | { |
| 4222 | return sprintf(page, "%pU\n", mddev->uuid); |
| 4223 | } |
| 4224 | static struct md_sysfs_entry md_uuid = |
| 4225 | __ATTR(uuid, S_IRUGO, uuid_show, NULL); |
| 4226 | |
| 4227 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4228 | chunk_size_show(struct mddev *mddev, char *page) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4229 | { |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 4230 | if (mddev->reshape_position != MaxSector && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4231 | mddev->chunk_sectors != mddev->new_chunk_sectors) |
| 4232 | return sprintf(page, "%d (%d)\n", |
| 4233 | mddev->new_chunk_sectors << 9, |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4234 | mddev->chunk_sectors << 9); |
| 4235 | return sprintf(page, "%d\n", mddev->chunk_sectors << 9); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4236 | } |
| 4237 | |
| 4238 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4239 | chunk_size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4240 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4241 | unsigned long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4242 | int err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4243 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4244 | err = kstrtoul(buf, 10, &n); |
| 4245 | if (err < 0) |
| 4246 | return err; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4247 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4248 | err = mddev_lock(mddev); |
| 4249 | if (err) |
| 4250 | return err; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4251 | if (mddev->pers) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 4252 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4253 | err = -EBUSY; |
| 4254 | else if (mddev->ro) |
| 4255 | err = -EROFS; |
| 4256 | else { |
| 4257 | mddev->new_chunk_sectors = n >> 9; |
| 4258 | err = mddev->pers->check_reshape(mddev); |
| 4259 | if (err) |
| 4260 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 4261 | } |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4262 | } else { |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4263 | mddev->new_chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4264 | if (mddev->reshape_position == MaxSector) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4265 | mddev->chunk_sectors = n >> 9; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 4266 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4267 | mddev_unlock(mddev); |
| 4268 | return err ?: len; |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4269 | } |
| 4270 | static struct md_sysfs_entry md_chunk_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4271 | __ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4272 | |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4273 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4274 | resync_start_show(struct mddev *mddev, char *page) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4275 | { |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 4276 | if (mddev->recovery_cp == MaxSector) |
| 4277 | return sprintf(page, "none\n"); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4278 | return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp); |
| 4279 | } |
| 4280 | |
| 4281 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4282 | resync_start_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4283 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4284 | unsigned long long n; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4285 | int err; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4286 | |
| 4287 | if (cmd_match(buf, "none")) |
| 4288 | n = MaxSector; |
| 4289 | else { |
| 4290 | err = kstrtoull(buf, 10, &n); |
| 4291 | if (err < 0) |
| 4292 | return err; |
| 4293 | if (n != (sector_t)n) |
| 4294 | return -EINVAL; |
| 4295 | } |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4296 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4297 | err = mddev_lock(mddev); |
| 4298 | if (err) |
| 4299 | return err; |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 4300 | if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4301 | err = -EBUSY; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4302 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4303 | if (!err) { |
| 4304 | mddev->recovery_cp = n; |
| 4305 | if (mddev->pers) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4306 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4307 | } |
| 4308 | mddev_unlock(mddev); |
| 4309 | return err ?: len; |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4310 | } |
| 4311 | static struct md_sysfs_entry md_resync_start = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4312 | __ATTR_PREALLOC(resync_start, S_IRUGO|S_IWUSR, |
| 4313 | resync_start_show, resync_start_store); |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 4314 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4315 | /* |
| 4316 | * The array state can be: |
| 4317 | * |
| 4318 | * clear |
| 4319 | * No devices, no size, no level |
| 4320 | * Equivalent to STOP_ARRAY ioctl |
| 4321 | * inactive |
| 4322 | * May have some settings, but array is not active |
| 4323 | * all IO results in error |
| 4324 | * When written, doesn't tear down array, but just stops it |
| 4325 | * suspended (not supported yet) |
| 4326 | * All IO requests will block. The array can be reconfigured. |
Andre Noll | 910d8cb | 2008-03-25 21:00:53 +0100 | [diff] [blame] | 4327 | * Writing this, if accepted, will block until array is quiescent |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4328 | * readonly |
| 4329 | * no resync can happen. no superblocks get written. |
| 4330 | * write requests fail |
| 4331 | * read-auto |
| 4332 | * like readonly, but behaves like 'clean' on a write request. |
| 4333 | * |
| 4334 | * clean - no pending writes, but otherwise active. |
| 4335 | * When written to inactive array, starts without resync |
| 4336 | * If a write request arrives then |
| 4337 | * if metadata is known, mark 'dirty' and switch to 'active'. |
| 4338 | * if not known, block and switch to write-pending |
| 4339 | * If written to an active array that has pending writes, then fails. |
| 4340 | * active |
| 4341 | * fully active: IO and resync can be happening. |
| 4342 | * When written to inactive array, starts with resync |
| 4343 | * |
| 4344 | * write-pending |
| 4345 | * clean, but writes are blocked waiting for 'active' to be written. |
| 4346 | * |
| 4347 | * active-idle |
| 4348 | * like active, but no writes have been seen for a while (100msec). |
| 4349 | * |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4350 | * broken |
| 4351 | * RAID0/LINEAR-only: same as clean, but array is missing a member. |
| 4352 | * It's useful because RAID0/LINEAR mounted-arrays aren't stopped |
| 4353 | * when a member is gone, so this state will at least alert the |
| 4354 | * user that something is wrong. |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4355 | */ |
| 4356 | enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active, |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4357 | write_pending, active_idle, broken, bad_word}; |
Adrian Bunk | 0538195 | 2006-06-26 00:28:01 -0700 | [diff] [blame] | 4358 | static char *array_states[] = { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4359 | "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active", |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4360 | "write-pending", "active-idle", "broken", NULL }; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4361 | |
| 4362 | static int match_word(const char *word, char **list) |
| 4363 | { |
| 4364 | int n; |
| 4365 | for (n=0; list[n]; n++) |
| 4366 | if (cmd_match(word, list[n])) |
| 4367 | break; |
| 4368 | return n; |
| 4369 | } |
| 4370 | |
| 4371 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4372 | array_state_show(struct mddev *mddev, char *page) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4373 | { |
| 4374 | enum array_state st = inactive; |
| 4375 | |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4376 | if (mddev->pers && !test_bit(MD_NOT_READY, &mddev->flags)) { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4377 | switch(mddev->ro) { |
| 4378 | case 1: |
| 4379 | st = readonly; |
| 4380 | break; |
| 4381 | case 2: |
| 4382 | st = read_auto; |
| 4383 | break; |
| 4384 | case 0: |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4385 | spin_lock(&mddev->lock); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4386 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4387 | st = write_pending; |
Tomasz Majchrzak | 16f8894 | 2016-10-24 12:47:28 +0200 | [diff] [blame] | 4388 | else if (mddev->in_sync) |
| 4389 | st = clean; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4390 | else if (mddev->safemode) |
| 4391 | st = active_idle; |
| 4392 | else |
| 4393 | st = active; |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4394 | spin_unlock(&mddev->lock); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4395 | } |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4396 | |
| 4397 | if (test_bit(MD_BROKEN, &mddev->flags) && st == clean) |
| 4398 | st = broken; |
| 4399 | } else { |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4400 | if (list_empty(&mddev->disks) && |
| 4401 | mddev->raid_disks == 0 && |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4402 | mddev->dev_sectors == 0) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4403 | st = clear; |
| 4404 | else |
| 4405 | st = inactive; |
| 4406 | } |
| 4407 | return sprintf(page, "%s\n", array_states[st]); |
| 4408 | } |
| 4409 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 4410 | static int do_md_stop(struct mddev *mddev, int ro, struct block_device *bdev); |
| 4411 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4412 | static int restart_array(struct mddev *mddev); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4413 | |
| 4414 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4415 | array_state_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4416 | { |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4417 | int err = 0; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4418 | enum array_state st = match_word(buf, array_states); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4419 | |
| 4420 | if (mddev->pers && (st == active || st == clean) && mddev->ro != 1) { |
| 4421 | /* don't take reconfig_mutex when toggling between |
| 4422 | * clean and active |
| 4423 | */ |
| 4424 | spin_lock(&mddev->lock); |
| 4425 | if (st == active) { |
| 4426 | restart_array(mddev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4427 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
Tomasz Majchrzak | 91a6c4a | 2016-10-25 17:07:08 +0200 | [diff] [blame] | 4428 | md_wakeup_thread(mddev->thread); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4429 | wake_up(&mddev->sb_wait); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4430 | } else /* st == clean */ { |
| 4431 | restart_array(mddev); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4432 | if (!set_in_sync(mddev)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4433 | err = -EBUSY; |
| 4434 | } |
Tomasz Majchrzak | 573275b | 2016-06-30 10:47:09 +0200 | [diff] [blame] | 4435 | if (!err) |
| 4436 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4437 | spin_unlock(&mddev->lock); |
NeilBrown | c008f1d | 2015-06-12 19:46:44 +1000 | [diff] [blame] | 4438 | return err ?: len; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4439 | } |
| 4440 | err = mddev_lock(mddev); |
| 4441 | if (err) |
| 4442 | return err; |
| 4443 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4444 | switch(st) { |
| 4445 | case bad_word: |
| 4446 | break; |
| 4447 | case clear: |
| 4448 | /* stopping an active array */ |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4449 | err = do_md_stop(mddev, 0, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4450 | break; |
| 4451 | case inactive: |
| 4452 | /* stopping an active array */ |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4453 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4454 | err = do_md_stop(mddev, 2, NULL); |
NeilBrown | 90cf195 | 2012-07-31 10:04:55 +1000 | [diff] [blame] | 4455 | else |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4456 | err = 0; /* already inactive */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4457 | break; |
| 4458 | case suspended: |
| 4459 | break; /* not supported yet */ |
| 4460 | case readonly: |
| 4461 | if (mddev->pers) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4462 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4463 | else { |
| 4464 | mddev->ro = 1; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4465 | set_disk_ro(mddev->gendisk, 1); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4466 | err = do_md_run(mddev); |
| 4467 | } |
| 4468 | break; |
| 4469 | case read_auto: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4470 | if (mddev->pers) { |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4471 | if (mddev->ro == 0) |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 4472 | err = md_set_readonly(mddev, NULL); |
NeilBrown | 80268ee | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 4473 | else if (mddev->ro == 1) |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4474 | err = restart_array(mddev); |
| 4475 | if (err == 0) { |
| 4476 | mddev->ro = 2; |
| 4477 | set_disk_ro(mddev->gendisk, 0); |
| 4478 | } |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4479 | } else { |
| 4480 | mddev->ro = 2; |
| 4481 | err = do_md_run(mddev); |
| 4482 | } |
| 4483 | break; |
| 4484 | case clean: |
| 4485 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4486 | err = restart_array(mddev); |
| 4487 | if (err) |
| 4488 | break; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4489 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 4490 | if (!set_in_sync(mddev)) |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4491 | err = -EBUSY; |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4492 | spin_unlock(&mddev->lock); |
NeilBrown | 5bf2959 | 2009-05-07 12:50:57 +1000 | [diff] [blame] | 4493 | } else |
| 4494 | err = -EINVAL; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4495 | break; |
| 4496 | case active: |
| 4497 | if (mddev->pers) { |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 4498 | err = restart_array(mddev); |
| 4499 | if (err) |
| 4500 | break; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4501 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4502 | wake_up(&mddev->sb_wait); |
| 4503 | err = 0; |
| 4504 | } else { |
| 4505 | mddev->ro = 0; |
NeilBrown | 648b629 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 4506 | set_disk_ro(mddev->gendisk, 0); |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4507 | err = do_md_run(mddev); |
| 4508 | } |
| 4509 | break; |
| 4510 | case write_pending: |
| 4511 | case active_idle: |
Guilherme G. Piccoli | 62f7b19 | 2019-09-03 16:49:00 -0300 | [diff] [blame] | 4512 | case broken: |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4513 | /* these cannot be set */ |
| 4514 | break; |
| 4515 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4516 | |
| 4517 | if (!err) { |
NeilBrown | 1d23f17 | 2011-12-08 15:49:12 +1100 | [diff] [blame] | 4518 | if (mddev->hold_active == UNTIL_IOCTL) |
| 4519 | mddev->hold_active = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4520 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 4521 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4522 | mddev_unlock(mddev); |
| 4523 | return err ?: len; |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 4524 | } |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4525 | static struct md_sysfs_entry md_array_state = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4526 | __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] | 4527 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4528 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4529 | max_corrected_read_errors_show(struct mddev *mddev, char *page) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4530 | return sprintf(page, "%d\n", |
| 4531 | atomic_read(&mddev->max_corr_read_errors)); |
| 4532 | } |
| 4533 | |
| 4534 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4535 | 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] | 4536 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4537 | unsigned int n; |
| 4538 | int rv; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4539 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4540 | rv = kstrtouint(buf, 10, &n); |
| 4541 | if (rv < 0) |
| 4542 | return rv; |
| 4543 | atomic_set(&mddev->max_corr_read_errors, n); |
| 4544 | return len; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4545 | } |
| 4546 | |
| 4547 | static struct md_sysfs_entry max_corr_read_errors = |
| 4548 | __ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show, |
| 4549 | max_corrected_read_errors_store); |
| 4550 | |
| 4551 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4552 | null_show(struct mddev *mddev, char *page) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4553 | { |
| 4554 | return -EINVAL; |
| 4555 | } |
| 4556 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4557 | /* need to ensure rdev_delayed_delete() has completed */ |
| 4558 | static void flush_rdev_wq(struct mddev *mddev) |
| 4559 | { |
| 4560 | struct md_rdev *rdev; |
| 4561 | |
| 4562 | rcu_read_lock(); |
| 4563 | rdev_for_each_rcu(rdev, mddev) |
| 4564 | if (work_pending(&rdev->del_work)) { |
| 4565 | flush_workqueue(md_rdev_misc_wq); |
| 4566 | break; |
| 4567 | } |
| 4568 | rcu_read_unlock(); |
| 4569 | } |
| 4570 | |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4571 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4572 | new_dev_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4573 | { |
| 4574 | /* buf must be %d:%d\n? giving major and minor numbers */ |
| 4575 | /* The new device is added to the array. |
| 4576 | * If the array has a persistent superblock, we read the |
| 4577 | * superblock to initialise info and check validity. |
| 4578 | * Otherwise, only checking done is that in bind_rdev_to_array, |
| 4579 | * which mainly checks size. |
| 4580 | */ |
| 4581 | char *e; |
| 4582 | int major = simple_strtoul(buf, &e, 10); |
| 4583 | int minor; |
| 4584 | dev_t dev; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4585 | struct md_rdev *rdev; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4586 | int err; |
| 4587 | |
| 4588 | if (!*buf || *e != ':' || !e[1] || e[1] == '\n') |
| 4589 | return -EINVAL; |
| 4590 | minor = simple_strtoul(e+1, &e, 10); |
| 4591 | if (*e && *e != '\n') |
| 4592 | return -EINVAL; |
| 4593 | dev = MKDEV(major, minor); |
| 4594 | if (major != MAJOR(dev) || |
| 4595 | minor != MINOR(dev)) |
| 4596 | return -EOVERFLOW; |
| 4597 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4598 | flush_rdev_wq(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4599 | err = mddev_lock(mddev); |
| 4600 | if (err) |
| 4601 | return err; |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4602 | if (mddev->persistent) { |
| 4603 | rdev = md_import_device(dev, mddev->major_version, |
| 4604 | mddev->minor_version); |
| 4605 | if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4606 | struct md_rdev *rdev0 |
| 4607 | = list_entry(mddev->disks.next, |
| 4608 | struct md_rdev, same_set); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4609 | err = super_types[mddev->major_version] |
| 4610 | .load_super(rdev, rdev0, mddev->minor_version); |
| 4611 | if (err < 0) |
| 4612 | goto out; |
| 4613 | } |
NeilBrown | c5d79ad | 2008-02-06 01:39:54 -0800 | [diff] [blame] | 4614 | } else if (mddev->external) |
| 4615 | rdev = md_import_device(dev, -2, -1); |
| 4616 | else |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4617 | rdev = md_import_device(dev, -1, -1); |
| 4618 | |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4619 | if (IS_ERR(rdev)) { |
| 4620 | mddev_unlock(mddev); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4621 | return PTR_ERR(rdev); |
NeilBrown | 9a8c0fa | 2015-06-25 17:06:40 +1000 | [diff] [blame] | 4622 | } |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4623 | err = bind_rdev_to_array(rdev, mddev); |
| 4624 | out: |
| 4625 | if (err) |
| 4626 | export_rdev(rdev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4627 | mddev_unlock(mddev); |
Alexey Obitotskiy | 5492c46 | 2017-07-28 15:49:25 +0200 | [diff] [blame] | 4628 | if (!err) |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 4629 | md_new_event(); |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 4630 | return err ? err : len; |
| 4631 | } |
| 4632 | |
| 4633 | static struct md_sysfs_entry md_new_device = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4634 | __ATTR(new_dev, S_IWUSR, null_show, new_dev_store); |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 4635 | |
| 4636 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4637 | bitmap_store(struct mddev *mddev, const char *buf, size_t len) |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4638 | { |
| 4639 | char *end; |
| 4640 | unsigned long chunk, end_chunk; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4641 | int err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4642 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4643 | err = mddev_lock(mddev); |
| 4644 | if (err) |
| 4645 | return err; |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4646 | if (!mddev->bitmap) |
| 4647 | goto out; |
| 4648 | /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */ |
| 4649 | while (*buf) { |
| 4650 | chunk = end_chunk = simple_strtoul(buf, &end, 0); |
| 4651 | if (buf == end) break; |
| 4652 | if (*end == '-') { /* range */ |
| 4653 | buf = end + 1; |
| 4654 | end_chunk = simple_strtoul(buf, &end, 0); |
| 4655 | if (buf == end) break; |
| 4656 | } |
| 4657 | if (*end && !isspace(*end)) break; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4658 | md_bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk); |
André Goddard Rosa | e7d2860 | 2009-12-14 18:01:06 -0800 | [diff] [blame] | 4659 | buf = skip_spaces(end); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4660 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4661 | md_bitmap_unplug(mddev->bitmap); /* flush the bits to disk */ |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4662 | out: |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4663 | mddev_unlock(mddev); |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 4664 | return len; |
| 4665 | } |
| 4666 | |
| 4667 | static struct md_sysfs_entry md_bitmap = |
| 4668 | __ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store); |
| 4669 | |
| 4670 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4671 | size_show(struct mddev *mddev, char *page) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4672 | { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4673 | return sprintf(page, "%llu\n", |
| 4674 | (unsigned long long)mddev->dev_sectors / 2); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4675 | } |
| 4676 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4677 | static int update_size(struct mddev *mddev, sector_t num_sectors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4678 | |
| 4679 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4680 | size_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4681 | { |
| 4682 | /* If array is inactive, we can reduce the component size, but |
| 4683 | * not increase it (except from 0). |
| 4684 | * If array is active, we can try an on-line resize |
| 4685 | */ |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4686 | sector_t sectors; |
| 4687 | int err = strict_blocks_to_sectors(buf, §ors); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4688 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4689 | if (err < 0) |
| 4690 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4691 | err = mddev_lock(mddev); |
| 4692 | if (err) |
| 4693 | return err; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4694 | if (mddev->pers) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4695 | err = update_size(mddev, sectors); |
Xiao Ni | 4ba1e78 | 2016-06-12 17:18:00 +0800 | [diff] [blame] | 4696 | if (err == 0) |
| 4697 | md_update_sb(mddev, 1); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4698 | } else { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4699 | if (mddev->dev_sectors == 0 || |
| 4700 | mddev->dev_sectors > sectors) |
| 4701 | mddev->dev_sectors = sectors; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4702 | else |
| 4703 | err = -ENOSPC; |
| 4704 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4705 | mddev_unlock(mddev); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4706 | return err ? err : len; |
| 4707 | } |
| 4708 | |
| 4709 | static struct md_sysfs_entry md_size = |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4710 | __ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store); |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4711 | |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 4712 | /* Metadata version. |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4713 | * This is one of |
| 4714 | * 'none' for arrays with no metadata (good luck...) |
| 4715 | * 'external' for arrays with externally managed metadata, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4716 | * or N.M for internally known formats |
| 4717 | */ |
| 4718 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4719 | metadata_show(struct mddev *mddev, char *page) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4720 | { |
| 4721 | if (mddev->persistent) |
| 4722 | return sprintf(page, "%d.%d\n", |
| 4723 | mddev->major_version, mddev->minor_version); |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4724 | else if (mddev->external) |
| 4725 | return sprintf(page, "external:%s\n", mddev->metadata_type); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4726 | else |
| 4727 | return sprintf(page, "none\n"); |
| 4728 | } |
| 4729 | |
| 4730 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4731 | metadata_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4732 | { |
| 4733 | int major, minor; |
| 4734 | char *e; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4735 | int err; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4736 | /* Changing the details of 'external' metadata is |
| 4737 | * always permitted. Otherwise there must be |
| 4738 | * no devices attached to the array. |
| 4739 | */ |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4740 | |
| 4741 | err = mddev_lock(mddev); |
| 4742 | if (err) |
| 4743 | return err; |
| 4744 | err = -EBUSY; |
NeilBrown | ea43ddd | 2008-10-13 11:55:11 +1100 | [diff] [blame] | 4745 | if (mddev->external && strncmp(buf, "external:", 9) == 0) |
| 4746 | ; |
| 4747 | else if (!list_empty(&mddev->disks)) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4748 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4749 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4750 | err = 0; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4751 | if (cmd_match(buf, "none")) { |
| 4752 | mddev->persistent = 0; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4753 | mddev->external = 0; |
| 4754 | mddev->major_version = 0; |
| 4755 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4756 | goto out_unlock; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4757 | } |
| 4758 | if (strncmp(buf, "external:", 9) == 0) { |
NeilBrown | 20a49ff | 2008-02-06 01:39:57 -0800 | [diff] [blame] | 4759 | size_t namelen = len-9; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4760 | if (namelen >= sizeof(mddev->metadata_type)) |
| 4761 | namelen = sizeof(mddev->metadata_type)-1; |
| 4762 | strncpy(mddev->metadata_type, buf+9, namelen); |
| 4763 | mddev->metadata_type[namelen] = 0; |
| 4764 | if (namelen && mddev->metadata_type[namelen-1] == '\n') |
| 4765 | mddev->metadata_type[--namelen] = 0; |
| 4766 | mddev->persistent = 0; |
| 4767 | mddev->external = 1; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4768 | mddev->major_version = 0; |
| 4769 | mddev->minor_version = 90; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4770 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4771 | } |
| 4772 | major = simple_strtoul(buf, &e, 10); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4773 | err = -EINVAL; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4774 | if (e==buf || *e != '.') |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4775 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4776 | buf = e+1; |
| 4777 | minor = simple_strtoul(buf, &e, 10); |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 4778 | if (e==buf || (*e && *e != '\n') ) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4779 | goto out_unlock; |
| 4780 | err = -ENOENT; |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 4781 | if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4782 | goto out_unlock; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4783 | mddev->major_version = major; |
| 4784 | mddev->minor_version = minor; |
| 4785 | mddev->persistent = 1; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 4786 | mddev->external = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4787 | err = 0; |
| 4788 | out_unlock: |
| 4789 | mddev_unlock(mddev); |
| 4790 | return err ?: len; |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4791 | } |
| 4792 | |
| 4793 | static struct md_sysfs_entry md_metadata = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4794 | __ATTR_PREALLOC(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store); |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 4795 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 4796 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4797 | action_show(struct mddev *mddev, char *page) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4798 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4799 | char *type = "idle"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4800 | unsigned long recovery = mddev->recovery; |
| 4801 | if (test_bit(MD_RECOVERY_FROZEN, &recovery)) |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4802 | type = "frozen"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4803 | else if (test_bit(MD_RECOVERY_RUNNING, &recovery) || |
| 4804 | (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &recovery))) { |
| 4805 | if (test_bit(MD_RECOVERY_RESHAPE, &recovery)) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4806 | type = "reshape"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4807 | else if (test_bit(MD_RECOVERY_SYNC, &recovery)) { |
| 4808 | if (!test_bit(MD_RECOVERY_REQUESTED, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4809 | type = "resync"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4810 | else if (test_bit(MD_RECOVERY_CHECK, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4811 | type = "check"; |
| 4812 | else |
| 4813 | type = "repair"; |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 4814 | } else if (test_bit(MD_RECOVERY_RECOVER, &recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4815 | type = "recover"; |
NeilBrown | 985ca97 | 2015-07-06 12:26:57 +1000 | [diff] [blame] | 4816 | else if (mddev->reshape_position != MaxSector) |
| 4817 | type = "reshape"; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4818 | } |
| 4819 | return sprintf(page, "%s\n", type); |
| 4820 | } |
| 4821 | |
| 4822 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4823 | action_store(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4824 | { |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4825 | if (!mddev->pers || !mddev->pers->sync_request) |
| 4826 | return -EINVAL; |
| 4827 | |
NeilBrown | b6a9ce6 | 2009-05-26 09:41:17 +1000 | [diff] [blame] | 4828 | |
| 4829 | if (cmd_match(page, "idle") || cmd_match(page, "frozen")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4830 | if (cmd_match(page, "frozen")) |
| 4831 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4832 | else |
| 4833 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4834 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 4835 | mddev_lock(mddev) == 0) { |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 4836 | if (work_pending(&mddev->del_work)) |
| 4837 | flush_workqueue(md_misc_wq); |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4838 | if (mddev->sync_thread) { |
| 4839 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4840 | md_reap_sync_thread(mddev); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4841 | } |
NeilBrown | 8e8e251 | 2015-06-12 19:51:27 +1000 | [diff] [blame] | 4842 | mddev_unlock(mddev); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4843 | } |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4844 | } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4845 | return -EBUSY; |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4846 | else if (cmd_match(page, "resync")) |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4847 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4848 | else if (cmd_match(page, "recover")) { |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4849 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4850 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 4851 | } else if (cmd_match(page, "reshape")) { |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4852 | int err; |
| 4853 | if (mddev->pers->start_reshape == NULL) |
| 4854 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4855 | err = mddev_lock(mddev); |
| 4856 | if (!err) { |
NeilBrown | 312045e | 2015-12-21 11:01:21 +1100 | [diff] [blame] | 4857 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4858 | err = -EBUSY; |
| 4859 | else { |
| 4860 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 4861 | err = mddev->pers->start_reshape(mddev); |
| 4862 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 4863 | mddev_unlock(mddev); |
| 4864 | } |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4865 | if (err) |
| 4866 | return err; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 4867 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | 16484bf | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 4868 | } else { |
NeilBrown | bce74da | 2006-01-06 00:20:41 -0800 | [diff] [blame] | 4869 | if (cmd_match(page, "check")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4870 | set_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | 2adc7d4 | 2006-05-20 14:59:57 -0700 | [diff] [blame] | 4871 | else if (!cmd_match(page, "repair")) |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4872 | return -EINVAL; |
NeilBrown | 56ccc11 | 2015-05-28 17:53:29 +1000 | [diff] [blame] | 4873 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4874 | set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 4875 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
NeilBrown | 7eec314 | 2005-11-08 21:39:44 -0800 | [diff] [blame] | 4876 | } |
NeilBrown | 48c26dd | 2012-10-11 14:19:39 +1100 | [diff] [blame] | 4877 | if (mddev->ro == 2) { |
| 4878 | /* A write to sync_action is enough to justify |
| 4879 | * canceling read-auto mode |
| 4880 | */ |
| 4881 | mddev->ro = 0; |
| 4882 | md_wakeup_thread(mddev->sync_thread); |
| 4883 | } |
NeilBrown | 03c902e | 2006-01-06 00:20:46 -0800 | [diff] [blame] | 4884 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4885 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 4886 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4887 | return len; |
| 4888 | } |
| 4889 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4890 | static struct md_sysfs_entry md_scan_mode = |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 4891 | __ATTR_PREALLOC(sync_action, S_IRUGO|S_IWUSR, action_show, action_store); |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 4892 | |
| 4893 | static ssize_t |
| 4894 | last_sync_action_show(struct mddev *mddev, char *page) |
| 4895 | { |
| 4896 | return sprintf(page, "%s\n", mddev->last_sync_action); |
| 4897 | } |
| 4898 | |
| 4899 | static struct md_sysfs_entry md_last_scan_mode = __ATTR_RO(last_sync_action); |
| 4900 | |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4901 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4902 | mismatch_cnt_show(struct mddev *mddev, char *page) |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4903 | { |
| 4904 | return sprintf(page, "%llu\n", |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4905 | (unsigned long long) |
| 4906 | atomic64_read(&mddev->resync_mismatches)); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4907 | } |
| 4908 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 4909 | static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt); |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 4910 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4911 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4912 | sync_min_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4913 | { |
| 4914 | return sprintf(page, "%d (%s)\n", speed_min(mddev), |
| 4915 | mddev->sync_speed_min ? "local": "system"); |
| 4916 | } |
| 4917 | |
| 4918 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4919 | sync_min_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4920 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4921 | unsigned int min; |
| 4922 | int rv; |
| 4923 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4924 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4925 | min = 0; |
| 4926 | } else { |
| 4927 | rv = kstrtouint(buf, 10, &min); |
| 4928 | if (rv < 0) |
| 4929 | return rv; |
| 4930 | if (min == 0) |
| 4931 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4932 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4933 | mddev->sync_speed_min = min; |
| 4934 | return len; |
| 4935 | } |
| 4936 | |
| 4937 | static struct md_sysfs_entry md_sync_min = |
| 4938 | __ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store); |
| 4939 | |
| 4940 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4941 | sync_max_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4942 | { |
| 4943 | return sprintf(page, "%d (%s)\n", speed_max(mddev), |
| 4944 | mddev->sync_speed_max ? "local": "system"); |
| 4945 | } |
| 4946 | |
| 4947 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4948 | sync_max_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4949 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4950 | unsigned int max; |
| 4951 | int rv; |
| 4952 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4953 | if (strncmp(buf, "system", 6)==0) { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 4954 | max = 0; |
| 4955 | } else { |
| 4956 | rv = kstrtouint(buf, 10, &max); |
| 4957 | if (rv < 0) |
| 4958 | return rv; |
| 4959 | if (max == 0) |
| 4960 | return -EINVAL; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4961 | } |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4962 | mddev->sync_speed_max = max; |
| 4963 | return len; |
| 4964 | } |
| 4965 | |
| 4966 | static struct md_sysfs_entry md_sync_max = |
| 4967 | __ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store); |
| 4968 | |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4969 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4970 | degraded_show(struct mddev *mddev, char *page) |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 4971 | { |
| 4972 | return sprintf(page, "%d\n", mddev->degraded); |
| 4973 | } |
| 4974 | static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 4975 | |
| 4976 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4977 | sync_force_parallel_show(struct mddev *mddev, char *page) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4978 | { |
| 4979 | return sprintf(page, "%d\n", mddev->parallel_resync); |
| 4980 | } |
| 4981 | |
| 4982 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4983 | 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] | 4984 | { |
| 4985 | long n; |
| 4986 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 4987 | if (kstrtol(buf, 10, &n)) |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 4988 | return -EINVAL; |
| 4989 | |
| 4990 | if (n != 0 && n != 1) |
| 4991 | return -EINVAL; |
| 4992 | |
| 4993 | mddev->parallel_resync = n; |
| 4994 | |
| 4995 | if (mddev->sync_thread) |
| 4996 | wake_up(&resync_wait); |
| 4997 | |
| 4998 | return len; |
| 4999 | } |
| 5000 | |
| 5001 | /* force parallel resync, even with shared block devices */ |
| 5002 | static struct md_sysfs_entry md_sync_force_parallel = |
| 5003 | __ATTR(sync_force_parallel, S_IRUGO|S_IWUSR, |
| 5004 | sync_force_parallel_show, sync_force_parallel_store); |
| 5005 | |
| 5006 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5007 | sync_speed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5008 | { |
| 5009 | unsigned long resync, dt, db; |
NeilBrown | d1a7c50 | 2009-03-31 15:24:32 +1100 | [diff] [blame] | 5010 | if (mddev->curr_resync == 0) |
| 5011 | return sprintf(page, "none\n"); |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 5012 | resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active); |
| 5013 | dt = (jiffies - mddev->resync_mark) / HZ; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5014 | if (!dt) dt++; |
Andre Noll | 9687a60 | 2008-03-25 22:24:09 +0100 | [diff] [blame] | 5015 | db = resync - mddev->resync_mark_cnt; |
| 5016 | return sprintf(page, "%lu\n", db/dt/2); /* K/sec */ |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5017 | } |
| 5018 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 5019 | static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5020 | |
| 5021 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5022 | sync_completed_show(struct mddev *mddev, char *page) |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5023 | { |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5024 | unsigned long long max_sectors, resync; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5025 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5026 | if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 5027 | return sprintf(page, "none\n"); |
| 5028 | |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 5029 | if (mddev->curr_resync == 1 || |
| 5030 | mddev->curr_resync == 2) |
| 5031 | return sprintf(page, "delayed\n"); |
| 5032 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 5033 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 5034 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5035 | max_sectors = mddev->resync_max_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5036 | else |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5037 | max_sectors = mddev->dev_sectors; |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5038 | |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5039 | resync = mddev->curr_resync_completed; |
RĂ©mi RĂ©rolle | 13ae864 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5040 | return sprintf(page, "%llu / %llu\n", resync, max_sectors); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5041 | } |
| 5042 | |
NeilBrown | 750f199 | 2014-09-30 08:53:05 +1000 | [diff] [blame] | 5043 | static struct md_sysfs_entry md_sync_completed = |
| 5044 | __ATTR_PREALLOC(sync_completed, S_IRUGO, sync_completed_show, NULL); |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5045 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5046 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5047 | min_sync_show(struct mddev *mddev, char *page) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5048 | { |
| 5049 | return sprintf(page, "%llu\n", |
| 5050 | (unsigned long long)mddev->resync_min); |
| 5051 | } |
| 5052 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5053 | min_sync_store(struct mddev *mddev, const char *buf, size_t len) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5054 | { |
| 5055 | unsigned long long min; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5056 | int err; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5057 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5058 | if (kstrtoull(buf, 10, &min)) |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5059 | return -EINVAL; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5060 | |
| 5061 | spin_lock(&mddev->lock); |
| 5062 | err = -EINVAL; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5063 | if (min > mddev->resync_max) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5064 | goto out_unlock; |
| 5065 | |
| 5066 | err = -EBUSY; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5067 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5068 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5069 | |
NeilBrown | 50c37b1 | 2015-03-23 17:36:38 +1100 | [diff] [blame] | 5070 | /* Round down to multiple of 4K for safety */ |
| 5071 | mddev->resync_min = round_down(min, 8); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5072 | err = 0; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5073 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5074 | out_unlock: |
| 5075 | spin_unlock(&mddev->lock); |
| 5076 | return err ?: len; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | static struct md_sysfs_entry md_min_sync = |
| 5080 | __ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store); |
| 5081 | |
| 5082 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5083 | max_sync_show(struct mddev *mddev, char *page) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5084 | { |
| 5085 | if (mddev->resync_max == MaxSector) |
| 5086 | return sprintf(page, "max\n"); |
| 5087 | else |
| 5088 | return sprintf(page, "%llu\n", |
| 5089 | (unsigned long long)mddev->resync_max); |
| 5090 | } |
| 5091 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5092 | max_sync_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5093 | { |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5094 | int err; |
| 5095 | spin_lock(&mddev->lock); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5096 | if (strncmp(buf, "max", 3) == 0) |
| 5097 | mddev->resync_max = MaxSector; |
| 5098 | else { |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5099 | unsigned long long max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5100 | int chunk; |
| 5101 | |
| 5102 | err = -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5103 | if (kstrtoull(buf, 10, &max)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5104 | goto out_unlock; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5105 | if (max < mddev->resync_min) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5106 | goto out_unlock; |
| 5107 | |
| 5108 | err = -EBUSY; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5109 | if (max < mddev->resync_max && |
NeilBrown | 4d484a4 | 2009-08-13 10:41:50 +1000 | [diff] [blame] | 5110 | mddev->ro == 0 && |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5111 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5112 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5113 | |
| 5114 | /* Must be a multiple of chunk_size */ |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5115 | chunk = mddev->chunk_sectors; |
| 5116 | if (chunk) { |
raz ben yehuda | 2ac06c3 | 2009-06-16 17:01:42 +1000 | [diff] [blame] | 5117 | sector_t temp = max; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5118 | |
| 5119 | err = -EINVAL; |
| 5120 | if (sector_div(temp, chunk)) |
| 5121 | goto out_unlock; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5122 | } |
| 5123 | mddev->resync_max = max; |
| 5124 | } |
| 5125 | wake_up(&mddev->recovery_wait); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5126 | err = 0; |
| 5127 | out_unlock: |
| 5128 | spin_unlock(&mddev->lock); |
| 5129 | return err ?: len; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | static struct md_sysfs_entry md_max_sync = |
| 5133 | __ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store); |
| 5134 | |
| 5135 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5136 | suspend_lo_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5137 | { |
| 5138 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo); |
| 5139 | } |
| 5140 | |
| 5141 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5142 | suspend_lo_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5143 | { |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5144 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5145 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5146 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5147 | err = kstrtoull(buf, 10, &new); |
| 5148 | if (err < 0) |
| 5149 | return err; |
| 5150 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5151 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5152 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5153 | err = mddev_lock(mddev); |
| 5154 | if (err) |
| 5155 | return err; |
| 5156 | err = -EINVAL; |
| 5157 | if (mddev->pers == NULL || |
| 5158 | mddev->pers->quiesce == NULL) |
| 5159 | goto unlock; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5160 | mddev_suspend(mddev); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5161 | mddev->suspend_lo = new; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5162 | mddev_resume(mddev); |
| 5163 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5164 | err = 0; |
| 5165 | unlock: |
| 5166 | mddev_unlock(mddev); |
| 5167 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5168 | } |
| 5169 | static struct md_sysfs_entry md_suspend_lo = |
| 5170 | __ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store); |
| 5171 | |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5172 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5173 | suspend_hi_show(struct mddev *mddev, char *page) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5174 | { |
| 5175 | return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi); |
| 5176 | } |
| 5177 | |
| 5178 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5179 | suspend_hi_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5180 | { |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5181 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5182 | int err; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5183 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5184 | err = kstrtoull(buf, 10, &new); |
| 5185 | if (err < 0) |
| 5186 | return err; |
| 5187 | if (new != (sector_t)new) |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5188 | return -EINVAL; |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5189 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5190 | err = mddev_lock(mddev); |
| 5191 | if (err) |
| 5192 | return err; |
| 5193 | err = -EINVAL; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5194 | if (mddev->pers == NULL) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5195 | goto unlock; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5196 | |
| 5197 | mddev_suspend(mddev); |
NeilBrown | 23ddff3 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5198 | mddev->suspend_hi = new; |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 5199 | mddev_resume(mddev); |
| 5200 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5201 | err = 0; |
| 5202 | unlock: |
| 5203 | mddev_unlock(mddev); |
| 5204 | return err ?: len; |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5205 | } |
| 5206 | static struct md_sysfs_entry md_suspend_hi = |
| 5207 | __ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store); |
| 5208 | |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5209 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5210 | reshape_position_show(struct mddev *mddev, char *page) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5211 | { |
| 5212 | if (mddev->reshape_position != MaxSector) |
| 5213 | return sprintf(page, "%llu\n", |
| 5214 | (unsigned long long)mddev->reshape_position); |
| 5215 | strcpy(page, "none\n"); |
| 5216 | return 5; |
| 5217 | } |
| 5218 | |
| 5219 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5220 | reshape_position_store(struct mddev *mddev, const char *buf, size_t len) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5221 | { |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5222 | struct md_rdev *rdev; |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5223 | unsigned long long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5224 | int err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5225 | |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 5226 | err = kstrtoull(buf, 10, &new); |
| 5227 | if (err < 0) |
| 5228 | return err; |
| 5229 | if (new != (sector_t)new) |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5230 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5231 | err = mddev_lock(mddev); |
| 5232 | if (err) |
| 5233 | return err; |
| 5234 | err = -EBUSY; |
| 5235 | if (mddev->pers) |
| 5236 | goto unlock; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5237 | mddev->reshape_position = new; |
| 5238 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5239 | mddev->reshape_backwards = 0; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5240 | mddev->new_level = mddev->level; |
| 5241 | mddev->new_layout = mddev->layout; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5242 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5243 | rdev_for_each(rdev, mddev) |
| 5244 | rdev->new_data_offset = rdev->data_offset; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5245 | err = 0; |
| 5246 | unlock: |
| 5247 | mddev_unlock(mddev); |
| 5248 | return err ?: len; |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5249 | } |
| 5250 | |
| 5251 | static struct md_sysfs_entry md_reshape_position = |
| 5252 | __ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show, |
| 5253 | reshape_position_store); |
| 5254 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5255 | static ssize_t |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5256 | reshape_direction_show(struct mddev *mddev, char *page) |
| 5257 | { |
| 5258 | return sprintf(page, "%s\n", |
| 5259 | mddev->reshape_backwards ? "backwards" : "forwards"); |
| 5260 | } |
| 5261 | |
| 5262 | static ssize_t |
| 5263 | reshape_direction_store(struct mddev *mddev, const char *buf, size_t len) |
| 5264 | { |
| 5265 | int backwards = 0; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5266 | int err; |
| 5267 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5268 | if (cmd_match(buf, "forwards")) |
| 5269 | backwards = 0; |
| 5270 | else if (cmd_match(buf, "backwards")) |
| 5271 | backwards = 1; |
| 5272 | else |
| 5273 | return -EINVAL; |
| 5274 | if (mddev->reshape_backwards == backwards) |
| 5275 | return len; |
| 5276 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5277 | err = mddev_lock(mddev); |
| 5278 | if (err) |
| 5279 | return err; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5280 | /* check if we are allowed to change */ |
| 5281 | if (mddev->delta_disks) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5282 | err = -EBUSY; |
| 5283 | else if (mddev->persistent && |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5284 | mddev->major_version == 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5285 | err = -EINVAL; |
| 5286 | else |
| 5287 | mddev->reshape_backwards = backwards; |
| 5288 | mddev_unlock(mddev); |
| 5289 | return err ?: len; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5290 | } |
| 5291 | |
| 5292 | static struct md_sysfs_entry md_reshape_direction = |
| 5293 | __ATTR(reshape_direction, S_IRUGO|S_IWUSR, reshape_direction_show, |
| 5294 | reshape_direction_store); |
| 5295 | |
| 5296 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5297 | array_size_show(struct mddev *mddev, char *page) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5298 | { |
| 5299 | if (mddev->external_size) |
| 5300 | return sprintf(page, "%llu\n", |
| 5301 | (unsigned long long)mddev->array_sectors/2); |
| 5302 | else |
| 5303 | return sprintf(page, "default\n"); |
| 5304 | } |
| 5305 | |
| 5306 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5307 | array_size_store(struct mddev *mddev, const char *buf, size_t len) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5308 | { |
| 5309 | sector_t sectors; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5310 | int err; |
| 5311 | |
| 5312 | err = mddev_lock(mddev); |
| 5313 | if (err) |
| 5314 | return err; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5315 | |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5316 | /* cluster raid doesn't support change array_sectors */ |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 5317 | if (mddev_is_clustered(mddev)) { |
| 5318 | mddev_unlock(mddev); |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5319 | return -EINVAL; |
Zhilong Liu | b670883 | 2017-04-10 14:15:55 +0800 | [diff] [blame] | 5320 | } |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 5321 | |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5322 | if (strncmp(buf, "default", 7) == 0) { |
| 5323 | if (mddev->pers) |
| 5324 | sectors = mddev->pers->size(mddev, 0, 0); |
| 5325 | else |
| 5326 | sectors = mddev->array_sectors; |
| 5327 | |
| 5328 | mddev->external_size = 0; |
| 5329 | } else { |
| 5330 | if (strict_blocks_to_sectors(buf, §ors) < 0) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5331 | err = -EINVAL; |
| 5332 | else if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors) |
| 5333 | err = -E2BIG; |
| 5334 | else |
| 5335 | mddev->external_size = 1; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5336 | } |
| 5337 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5338 | if (!err) { |
| 5339 | mddev->array_sectors = sectors; |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 5340 | if (mddev->pers) |
| 5341 | set_capacity_and_notify(mddev->gendisk, |
| 5342 | mddev->array_sectors); |
NeilBrown | cbe6ef1 | 2011-02-16 13:58:38 +1100 | [diff] [blame] | 5343 | } |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5344 | mddev_unlock(mddev); |
| 5345 | return err ?: len; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5346 | } |
| 5347 | |
| 5348 | static struct md_sysfs_entry md_array_size = |
| 5349 | __ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show, |
| 5350 | array_size_store); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5351 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5352 | static ssize_t |
| 5353 | consistency_policy_show(struct mddev *mddev, char *page) |
| 5354 | { |
| 5355 | int ret; |
| 5356 | |
| 5357 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 5358 | ret = sprintf(page, "journal\n"); |
| 5359 | } else if (test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 5360 | ret = sprintf(page, "ppl\n"); |
| 5361 | } else if (mddev->bitmap) { |
| 5362 | ret = sprintf(page, "bitmap\n"); |
| 5363 | } else if (mddev->pers) { |
| 5364 | if (mddev->pers->sync_request) |
| 5365 | ret = sprintf(page, "resync\n"); |
| 5366 | else |
| 5367 | ret = sprintf(page, "none\n"); |
| 5368 | } else { |
| 5369 | ret = sprintf(page, "unknown\n"); |
| 5370 | } |
| 5371 | |
| 5372 | return ret; |
| 5373 | } |
| 5374 | |
| 5375 | static ssize_t |
| 5376 | consistency_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5377 | { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5378 | int err = 0; |
| 5379 | |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5380 | if (mddev->pers) { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5381 | if (mddev->pers->change_consistency_policy) |
| 5382 | err = mddev->pers->change_consistency_policy(mddev, buf); |
| 5383 | else |
| 5384 | err = -EBUSY; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5385 | } else if (mddev->external && strncmp(buf, "ppl", 3) == 0) { |
| 5386 | set_bit(MD_HAS_PPL, &mddev->flags); |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5387 | } else { |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5388 | err = -EINVAL; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5389 | } |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 5390 | |
| 5391 | return err ? err : len; |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | static struct md_sysfs_entry md_consistency_policy = |
| 5395 | __ATTR(consistency_policy, S_IRUGO | S_IWUSR, consistency_policy_show, |
| 5396 | consistency_policy_store); |
| 5397 | |
Guoqing Jiang | 9a56784 | 2019-07-24 11:09:19 +0200 | [diff] [blame] | 5398 | static ssize_t fail_last_dev_show(struct mddev *mddev, char *page) |
| 5399 | { |
| 5400 | return sprintf(page, "%d\n", mddev->fail_last_dev); |
| 5401 | } |
| 5402 | |
| 5403 | /* |
| 5404 | * Setting fail_last_dev to true to allow last device to be forcibly removed |
| 5405 | * from RAID1/RAID10. |
| 5406 | */ |
| 5407 | static ssize_t |
| 5408 | fail_last_dev_store(struct mddev *mddev, const char *buf, size_t len) |
| 5409 | { |
| 5410 | int ret; |
| 5411 | bool value; |
| 5412 | |
| 5413 | ret = kstrtobool(buf, &value); |
| 5414 | if (ret) |
| 5415 | return ret; |
| 5416 | |
| 5417 | if (value != mddev->fail_last_dev) |
| 5418 | mddev->fail_last_dev = value; |
| 5419 | |
| 5420 | return len; |
| 5421 | } |
| 5422 | static struct md_sysfs_entry md_fail_last_dev = |
| 5423 | __ATTR(fail_last_dev, S_IRUGO | S_IWUSR, fail_last_dev_show, |
| 5424 | fail_last_dev_store); |
| 5425 | |
Guoqing Jiang | 3938f5f | 2019-12-23 10:48:56 +0100 | [diff] [blame] | 5426 | static ssize_t serialize_policy_show(struct mddev *mddev, char *page) |
| 5427 | { |
| 5428 | if (mddev->pers == NULL || (mddev->pers->level != 1)) |
| 5429 | return sprintf(page, "n/a\n"); |
| 5430 | else |
| 5431 | return sprintf(page, "%d\n", mddev->serialize_policy); |
| 5432 | } |
| 5433 | |
| 5434 | /* |
| 5435 | * Setting serialize_policy to true to enforce write IO is not reordered |
| 5436 | * for raid1. |
| 5437 | */ |
| 5438 | static ssize_t |
| 5439 | serialize_policy_store(struct mddev *mddev, const char *buf, size_t len) |
| 5440 | { |
| 5441 | int err; |
| 5442 | bool value; |
| 5443 | |
| 5444 | err = kstrtobool(buf, &value); |
| 5445 | if (err) |
| 5446 | return err; |
| 5447 | |
| 5448 | if (value == mddev->serialize_policy) |
| 5449 | return len; |
| 5450 | |
| 5451 | err = mddev_lock(mddev); |
| 5452 | if (err) |
| 5453 | return err; |
| 5454 | if (mddev->pers == NULL || (mddev->pers->level != 1)) { |
| 5455 | pr_err("md: serialize_policy is only effective for raid1\n"); |
| 5456 | err = -EINVAL; |
| 5457 | goto unlock; |
| 5458 | } |
| 5459 | |
| 5460 | mddev_suspend(mddev); |
| 5461 | if (value) |
| 5462 | mddev_create_serial_pool(mddev, NULL, true); |
| 5463 | else |
| 5464 | mddev_destroy_serial_pool(mddev, NULL, true); |
| 5465 | mddev->serialize_policy = value; |
| 5466 | mddev_resume(mddev); |
| 5467 | unlock: |
| 5468 | mddev_unlock(mddev); |
| 5469 | return err ?: len; |
| 5470 | } |
| 5471 | |
| 5472 | static struct md_sysfs_entry md_serialize_policy = |
| 5473 | __ATTR(serialize_policy, S_IRUGO | S_IWUSR, serialize_policy_show, |
| 5474 | serialize_policy_store); |
| 5475 | |
| 5476 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5477 | static struct attribute *md_default_attrs[] = { |
| 5478 | &md_level.attr, |
NeilBrown | d4dbd02 | 2006-06-26 00:27:59 -0700 | [diff] [blame] | 5479 | &md_layout.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5480 | &md_raid_disks.attr, |
Sebastian Parschauer | ec164d07 | 2020-07-28 12:01:39 +0200 | [diff] [blame] | 5481 | &md_uuid.attr, |
NeilBrown | 3b34380 | 2006-01-06 00:20:47 -0800 | [diff] [blame] | 5482 | &md_chunk_size.attr, |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 5483 | &md_size.attr, |
NeilBrown | a94213b | 2006-06-26 00:28:00 -0700 | [diff] [blame] | 5484 | &md_resync_start.attr, |
NeilBrown | 8bb93aa | 2006-01-06 00:20:50 -0800 | [diff] [blame] | 5485 | &md_metadata.attr, |
NeilBrown | 6d7ff738 | 2006-01-06 00:21:16 -0800 | [diff] [blame] | 5486 | &md_new_device.attr, |
NeilBrown | 16f17b3 | 2006-06-26 00:27:37 -0700 | [diff] [blame] | 5487 | &md_safe_delay.attr, |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 5488 | &md_array_state.attr, |
NeilBrown | 08a02ec | 2007-05-09 02:35:38 -0700 | [diff] [blame] | 5489 | &md_reshape_position.attr, |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5490 | &md_reshape_direction.attr, |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5491 | &md_array_size.attr, |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 5492 | &max_corr_read_errors.attr, |
Artur Paszkiewicz | 664aed0 | 2017-03-09 10:00:00 +0100 | [diff] [blame] | 5493 | &md_consistency_policy.attr, |
Guoqing Jiang | 9a56784 | 2019-07-24 11:09:19 +0200 | [diff] [blame] | 5494 | &md_fail_last_dev.attr, |
Guoqing Jiang | 3938f5f | 2019-12-23 10:48:56 +0100 | [diff] [blame] | 5495 | &md_serialize_policy.attr, |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5496 | NULL, |
| 5497 | }; |
| 5498 | |
Christoph Hellwig | 51238e7f | 2021-09-01 13:38:31 +0200 | [diff] [blame] | 5499 | static const struct attribute_group md_default_group = { |
| 5500 | .attrs = md_default_attrs, |
| 5501 | }; |
| 5502 | |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5503 | static struct attribute *md_redundancy_attrs[] = { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5504 | &md_scan_mode.attr, |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 5505 | &md_last_scan_mode.attr, |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 5506 | &md_mismatches.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5507 | &md_sync_min.attr, |
| 5508 | &md_sync_max.attr, |
| 5509 | &md_sync_speed.attr, |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 5510 | &md_sync_force_parallel.attr, |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 5511 | &md_sync_completed.attr, |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 5512 | &md_min_sync.attr, |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5513 | &md_max_sync.attr, |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5514 | &md_suspend_lo.attr, |
| 5515 | &md_suspend_hi.attr, |
Paul Clements | 9b1d1da | 2006-10-03 01:15:49 -0700 | [diff] [blame] | 5516 | &md_bitmap.attr, |
Iustin Pop | d7f3d29 | 2007-10-16 23:30:54 -0700 | [diff] [blame] | 5517 | &md_degraded.attr, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5518 | NULL, |
| 5519 | }; |
Rikard Falkeborn | c32dc04 | 2021-05-29 12:30:49 +0200 | [diff] [blame] | 5520 | static const struct attribute_group md_redundancy_group = { |
NeilBrown | 411036f | 2005-11-08 21:39:40 -0800 | [diff] [blame] | 5521 | .name = NULL, |
| 5522 | .attrs = md_redundancy_attrs, |
| 5523 | }; |
| 5524 | |
Christoph Hellwig | 51238e7f | 2021-09-01 13:38:31 +0200 | [diff] [blame] | 5525 | static const struct attribute_group *md_attr_groups[] = { |
| 5526 | &md_default_group, |
| 5527 | &md_bitmap_group, |
| 5528 | NULL, |
| 5529 | }; |
| 5530 | |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5531 | static ssize_t |
| 5532 | md_attr_show(struct kobject *kobj, struct attribute *attr, char *page) |
| 5533 | { |
| 5534 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5535 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5536 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5537 | |
| 5538 | if (!entry->show) |
| 5539 | return -EIO; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5540 | spin_lock(&all_mddevs_lock); |
| 5541 | if (list_empty(&mddev->all_mddevs)) { |
| 5542 | spin_unlock(&all_mddevs_lock); |
| 5543 | return -EBUSY; |
| 5544 | } |
| 5545 | mddev_get(mddev); |
| 5546 | spin_unlock(&all_mddevs_lock); |
| 5547 | |
NeilBrown | b7b17c9 | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 5548 | rv = entry->show(mddev, page); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5549 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5550 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5551 | } |
| 5552 | |
| 5553 | static ssize_t |
| 5554 | md_attr_store(struct kobject *kobj, struct attribute *attr, |
| 5555 | const char *page, size_t length) |
| 5556 | { |
| 5557 | struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5558 | struct mddev *mddev = container_of(kobj, struct mddev, kobj); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5559 | ssize_t rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5560 | |
| 5561 | if (!entry->store) |
| 5562 | return -EIO; |
NeilBrown | 67463ac | 2006-07-10 04:44:19 -0700 | [diff] [blame] | 5563 | if (!capable(CAP_SYS_ADMIN)) |
| 5564 | return -EACCES; |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5565 | spin_lock(&all_mddevs_lock); |
| 5566 | if (list_empty(&mddev->all_mddevs)) { |
| 5567 | spin_unlock(&all_mddevs_lock); |
| 5568 | return -EBUSY; |
| 5569 | } |
| 5570 | mddev_get(mddev); |
| 5571 | spin_unlock(&all_mddevs_lock); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 5572 | rv = entry->store(mddev, page, length); |
NeilBrown | af8a243 | 2011-12-08 15:49:46 +1100 | [diff] [blame] | 5573 | mddev_put(mddev); |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5574 | return rv; |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5575 | } |
| 5576 | |
| 5577 | static void md_free(struct kobject *ko) |
| 5578 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5579 | struct mddev *mddev = container_of(ko, struct mddev, kobj); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5580 | |
| 5581 | if (mddev->sysfs_state) |
| 5582 | sysfs_put(mddev->sysfs_state); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 5583 | if (mddev->sysfs_level) |
| 5584 | sysfs_put(mddev->sysfs_level); |
| 5585 | |
Christoph Hellwig | 0f1d2e0 | 2021-05-21 07:51:04 +0200 | [diff] [blame] | 5586 | if (mddev->gendisk) { |
Bart Van Assche | d8115c35 | 2018-02-28 10:15:29 -0800 | [diff] [blame] | 5587 | del_gendisk(mddev->gendisk); |
Christoph Hellwig | 0f1d2e0 | 2021-05-21 07:51:04 +0200 | [diff] [blame] | 5588 | blk_cleanup_disk(mddev->gendisk); |
| 5589 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5590 | percpu_ref_exit(&mddev->writes_pending); |
NeilBrown | a21d150 | 2009-01-09 08:31:09 +1100 | [diff] [blame] | 5591 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5592 | bioset_exit(&mddev->bio_set); |
| 5593 | bioset_exit(&mddev->sync_set); |
Guoqing Jiang | daee202 | 2021-06-03 17:21:06 +0800 | [diff] [blame] | 5594 | if (mddev->level != 1 && mddev->level != 10) |
| 5595 | bioset_exit(&mddev->io_acct_set); |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5596 | kfree(mddev); |
| 5597 | } |
| 5598 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 5599 | static const struct sysfs_ops md_sysfs_ops = { |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5600 | .show = md_attr_show, |
| 5601 | .store = md_attr_store, |
| 5602 | }; |
| 5603 | static struct kobj_type md_ktype = { |
| 5604 | .release = md_free, |
| 5605 | .sysfs_ops = &md_sysfs_ops, |
Christoph Hellwig | 51238e7f | 2021-09-01 13:38:31 +0200 | [diff] [blame] | 5606 | .default_groups = md_attr_groups, |
NeilBrown | eae1701 | 2005-11-08 21:39:23 -0800 | [diff] [blame] | 5607 | }; |
| 5608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | int mdp_major = 0; |
| 5610 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5611 | static void mddev_delayed_delete(struct work_struct *ws) |
| 5612 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5613 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5614 | |
Dan Williams | 5fd3a17 | 2009-03-04 00:57:25 -0700 | [diff] [blame] | 5615 | kobject_del(&mddev->kobj); |
| 5616 | kobject_put(&mddev->kobj); |
| 5617 | } |
| 5618 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5619 | static void no_op(struct percpu_ref *r) {} |
| 5620 | |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5621 | int mddev_init_writes_pending(struct mddev *mddev) |
| 5622 | { |
| 5623 | if (mddev->writes_pending.percpu_count_ptr) |
| 5624 | return 0; |
Roman Gushchin | ddde2af | 2019-05-07 10:01:49 -0700 | [diff] [blame] | 5625 | if (percpu_ref_init(&mddev->writes_pending, no_op, |
| 5626 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL) < 0) |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 5627 | return -ENOMEM; |
| 5628 | /* We want to start with the refcount at zero */ |
| 5629 | percpu_ref_put(&mddev->writes_pending); |
| 5630 | return 0; |
| 5631 | } |
| 5632 | EXPORT_SYMBOL_GPL(mddev_init_writes_pending); |
| 5633 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5634 | static int md_alloc(dev_t dev, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5636 | /* |
| 5637 | * If dev is zero, name is the name of a device to allocate with |
| 5638 | * an arbitrary minor number. It will be "md_???" |
| 5639 | * If dev is non-zero it must be a device number with a MAJOR of |
| 5640 | * MD_MAJOR or mdp_major. In this case, if "name" is NULL, then |
| 5641 | * the device is being created by opening a node in /dev. |
| 5642 | * If "name" is not NULL, the device is being created by |
| 5643 | * writing to /sys/module/md_mod/parameters/new_array. |
| 5644 | */ |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5645 | static DEFINE_MUTEX(disks_mutex); |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 5646 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5647 | struct gendisk *disk; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5648 | int partitioned; |
| 5649 | int shift; |
| 5650 | int unit; |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 5651 | int error ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5652 | |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 5653 | /* |
| 5654 | * Wait for any previous instance of this device to be completely |
| 5655 | * removed (mddev_delayed_delete). |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5656 | */ |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 5657 | flush_workqueue(md_misc_wq); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5658 | |
Arjan van de Ven | 48c9c27 | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 5659 | mutex_lock(&disks_mutex); |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 5660 | mddev = mddev_alloc(dev); |
| 5661 | if (IS_ERR(mddev)) { |
| 5662 | mutex_unlock(&disks_mutex); |
| 5663 | return PTR_ERR(mddev); |
| 5664 | } |
| 5665 | |
| 5666 | partitioned = (MAJOR(mddev->unit) != MD_MAJOR); |
| 5667 | shift = partitioned ? MdpMinorShift : 0; |
| 5668 | unit = MINOR(mddev->unit) >> shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5669 | |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5670 | if (name && !dev) { |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5671 | /* Need to ensure that 'name' is not a duplicate. |
| 5672 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5673 | struct mddev *mddev2; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5674 | spin_lock(&all_mddevs_lock); |
| 5675 | |
| 5676 | list_for_each_entry(mddev2, &all_mddevs, all_mddevs) |
| 5677 | if (mddev2->gendisk && |
| 5678 | strcmp(mddev2->gendisk->disk_name, name) == 0) { |
| 5679 | spin_unlock(&all_mddevs_lock); |
Christoph Hellwig | 0d809b3 | 2021-04-12 10:05:30 +0200 | [diff] [blame] | 5680 | error = -EEXIST; |
Christoph Hellwig | 7ad1069 | 2021-09-01 13:38:33 +0200 | [diff] [blame] | 5681 | goto out_unlock_disks_mutex; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5682 | } |
| 5683 | spin_unlock(&all_mddevs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5684 | } |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5685 | if (name && dev) |
| 5686 | /* |
| 5687 | * Creating /dev/mdNNN via "newarray", so adjust hold_active. |
| 5688 | */ |
| 5689 | mddev->hold_active = UNTIL_STOP; |
NeilBrown | 8b76539 | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5690 | |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5691 | error = -ENOMEM; |
Christoph Hellwig | 0f1d2e0 | 2021-05-21 07:51:04 +0200 | [diff] [blame] | 5692 | disk = blk_alloc_disk(NUMA_NO_NODE); |
| 5693 | if (!disk) |
Christoph Hellwig | 7ad1069 | 2021-09-01 13:38:33 +0200 | [diff] [blame] | 5694 | goto out_unlock_disks_mutex; |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5695 | |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5696 | disk->major = MAJOR(mddev->unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | disk->first_minor = unit << shift; |
Christoph Hellwig | 0f1d2e0 | 2021-05-21 07:51:04 +0200 | [diff] [blame] | 5698 | disk->minors = 1 << shift; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5699 | if (name) |
| 5700 | strcpy(disk->disk_name, name); |
| 5701 | else if (partitioned) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | sprintf(disk->disk_name, "md_d%d", unit); |
Greg Kroah-Hartman | ce7b0f46 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 5703 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | sprintf(disk->disk_name, "md%d", unit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | disk->fops = &md_fops; |
| 5706 | disk->private_data = mddev; |
Christoph Hellwig | 0f1d2e0 | 2021-05-21 07:51:04 +0200 | [diff] [blame] | 5707 | |
| 5708 | mddev->queue = disk->queue; |
| 5709 | blk_set_stacking_limits(&mddev->queue->limits); |
Jens Axboe | 56883a7 | 2016-03-30 10:16:53 -0600 | [diff] [blame] | 5710 | blk_queue_write_cache(mddev->queue, true, true); |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5711 | /* Allow extended partitions. This makes the |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5712 | * 'mdp' device redundant, but we can't really |
NeilBrown | 92850bb | 2008-10-21 13:25:32 +1100 | [diff] [blame] | 5713 | * remove it now. |
| 5714 | */ |
| 5715 | disk->flags |= GENHD_FL_EXT_DEVT; |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 5716 | disk->events |= DISK_EVENT_MEDIA_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | mddev->gendisk = disk; |
Luis Chamberlain | 9be68dd | 2021-09-01 13:38:30 +0200 | [diff] [blame] | 5718 | error = add_disk(disk); |
Christoph Hellwig | 7ad1069 | 2021-09-01 13:38:33 +0200 | [diff] [blame] | 5719 | if (error) |
| 5720 | goto out_cleanup_disk; |
NeilBrown | b014089 | 2011-05-10 17:49:01 +1000 | [diff] [blame] | 5721 | |
Kent Overstreet | 28dec87 | 2018-06-07 20:52:54 -0400 | [diff] [blame] | 5722 | error = kobject_add(&mddev->kobj, &disk_to_dev(disk)->kobj, "%s", "md"); |
Christoph Hellwig | 7ad1069 | 2021-09-01 13:38:33 +0200 | [diff] [blame] | 5723 | if (error) |
| 5724 | goto out_del_gendisk; |
| 5725 | |
| 5726 | kobject_uevent(&mddev->kobj, KOBJ_ADD); |
| 5727 | mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state"); |
| 5728 | mddev->sysfs_level = sysfs_get_dirent_safe(mddev->kobj.sd, "level"); |
| 5729 | goto out_unlock_disks_mutex; |
| 5730 | |
| 5731 | out_del_gendisk: |
| 5732 | del_gendisk(disk); |
| 5733 | out_cleanup_disk: |
| 5734 | blk_cleanup_disk(disk); |
| 5735 | out_unlock_disks_mutex: |
Christoph Hellwig | 94f3cd7 | 2021-09-01 13:38:32 +0200 | [diff] [blame] | 5736 | mutex_unlock(&disks_mutex); |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5737 | mddev_put(mddev); |
NeilBrown | 0909dc4 | 2009-07-01 12:27:21 +1000 | [diff] [blame] | 5738 | return error; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5739 | } |
| 5740 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 5741 | static void md_probe(dev_t dev) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5742 | { |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 5743 | if (MAJOR(dev) == MD_MAJOR && MINOR(dev) >= 512) |
| 5744 | return; |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5745 | if (create_on_open) |
| 5746 | md_alloc(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | } |
| 5748 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 5749 | static int add_named_array(const char *val, const struct kernel_param *kp) |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5750 | { |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5751 | /* |
| 5752 | * val must be "md_*" or "mdNNN". |
| 5753 | * For "md_*" we allocate an array with a large free minor number, and |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5754 | * set the name to val. val must not already be an active name. |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5755 | * For "mdNNN" we allocate an array with the minor number NNN |
| 5756 | * which must not already be in use. |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5757 | */ |
| 5758 | int len = strlen(val); |
| 5759 | char buf[DISK_NAME_LEN]; |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5760 | unsigned long devnum; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5761 | |
| 5762 | while (len && val[len-1] == '\n') |
| 5763 | len--; |
| 5764 | if (len >= DISK_NAME_LEN) |
| 5765 | return -E2BIG; |
| 5766 | strlcpy(buf, val, len+1); |
NeilBrown | 039b722 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 5767 | if (strncmp(buf, "md_", 3) == 0) |
| 5768 | return md_alloc(0, buf); |
| 5769 | if (strncmp(buf, "md", 2) == 0 && |
| 5770 | isdigit(buf[2]) && |
| 5771 | kstrtoul(buf+2, 10, &devnum) == 0 && |
| 5772 | devnum <= MINORMASK) |
| 5773 | return md_alloc(MKDEV(MD_MAJOR, devnum), NULL); |
| 5774 | |
| 5775 | return -EINVAL; |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 5776 | } |
| 5777 | |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 5778 | static void md_safemode_timeout(struct timer_list *t) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5779 | { |
Kees Cook | 8376d3c | 2017-10-16 17:01:48 -0700 | [diff] [blame] | 5780 | struct mddev *mddev = from_timer(mddev, t, safemode_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 5782 | mddev->safemode = 1; |
| 5783 | if (mddev->external) |
| 5784 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 5785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5786 | md_wakeup_thread(mddev->thread); |
| 5787 | } |
| 5788 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5789 | static int start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5791 | int md_run(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5792 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5793 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5794 | struct md_rdev *rdev; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 5795 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5796 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 5797 | if (list_empty(&mddev->disks)) |
| 5798 | /* cannot run an array with no devices.. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5799 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5800 | |
| 5801 | if (mddev->pers) |
| 5802 | return -EBUSY; |
NeilBrown | bb4f1e9 | 2010-08-08 21:18:03 +1000 | [diff] [blame] | 5803 | /* Cannot run until previous stop completes properly */ |
| 5804 | if (mddev->sysfs_active) |
| 5805 | return -EBUSY; |
NeilBrown | b6eb127 | 2010-04-15 10:13:47 +1000 | [diff] [blame] | 5806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5807 | /* |
| 5808 | * Analyze all RAID superblock(s) |
| 5809 | */ |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5810 | if (!mddev->raid_disks) { |
| 5811 | if (!mddev->persistent) |
| 5812 | return -EINVAL; |
Yufen Yu | 6a5cb53 | 2019-10-16 16:00:03 +0800 | [diff] [blame] | 5813 | err = analyze_sbs(mddev); |
| 5814 | if (err) |
| 5815 | return -EINVAL; |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 5816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5818 | if (mddev->level != LEVEL_NONE) |
| 5819 | request_module("md-level-%d", mddev->level); |
| 5820 | else if (mddev->clevel[0]) |
| 5821 | request_module("md-%s", mddev->clevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5822 | |
| 5823 | /* |
| 5824 | * Drop all container device buffers, from now on |
| 5825 | * the only valid external interface is through the md |
| 5826 | * device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | */ |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 5828 | mddev->has_superblocks = false; |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5829 | rdev_for_each(rdev, mddev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5830 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | continue; |
| 5832 | sync_blockdev(rdev->bdev); |
Peter Zijlstra | f98393a | 2007-05-06 14:49:54 -0700 | [diff] [blame] | 5833 | invalidate_bdev(rdev->bdev); |
Christoph Hellwig | d7a4783 | 2021-02-01 14:17:20 +0100 | [diff] [blame] | 5834 | if (mddev->ro != 1 && rdev_read_only(rdev)) { |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 5835 | mddev->ro = 1; |
| 5836 | if (mddev->gendisk) |
| 5837 | set_disk_ro(mddev->gendisk, 1); |
| 5838 | } |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5839 | |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 5840 | if (rdev->sb_page) |
| 5841 | mddev->has_superblocks = true; |
| 5842 | |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5843 | /* perform some consistency tests on the device. |
| 5844 | * We don't want the data to overlap the metadata, |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5845 | * Internal Bitmap issues have been handled elsewhere. |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5846 | */ |
Jonathan Brassow | a6ff7e0 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5847 | if (rdev->meta_bdev) { |
| 5848 | /* Nothing to check */; |
| 5849 | } else if (rdev->data_offset < rdev->sb_start) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5850 | if (mddev->dev_sectors && |
| 5851 | rdev->data_offset + mddev->dev_sectors |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5852 | > rdev->sb_start) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5853 | pr_warn("md: %s: data overlaps metadata\n", |
| 5854 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5855 | return -EINVAL; |
| 5856 | } |
| 5857 | } else { |
Andre Noll | 0f42035 | 2008-07-11 22:02:23 +1000 | [diff] [blame] | 5858 | if (rdev->sb_start + rdev->sb_size/512 |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5859 | > rdev->data_offset) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5860 | pr_warn("md: %s: metadata overlaps data\n", |
| 5861 | mdname(mddev)); |
NeilBrown | f0d76d7 | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 5862 | return -EINVAL; |
| 5863 | } |
| 5864 | } |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5865 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5866 | } |
| 5867 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5868 | if (!bioset_initialized(&mddev->bio_set)) { |
| 5869 | err = bioset_init(&mddev->bio_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); |
| 5870 | if (err) |
| 5871 | return err; |
Ming Lei | 1027317 | 2017-02-14 23:29:00 +0800 | [diff] [blame] | 5872 | } |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5873 | if (!bioset_initialized(&mddev->sync_set)) { |
| 5874 | err = bioset_init(&mddev->sync_set, BIO_POOL_SIZE, 0, BIOSET_NEED_BVECS); |
| 5875 | if (err) |
Guoqing Jiang | 1076481 | 2021-05-25 17:46:17 +0800 | [diff] [blame] | 5876 | goto exit_bio_set; |
| 5877 | } |
Guoqing Jiang | daee202 | 2021-06-03 17:21:06 +0800 | [diff] [blame] | 5878 | if (mddev->level != 1 && mddev->level != 10 && |
| 5879 | !bioset_initialized(&mddev->io_acct_set)) { |
Guoqing Jiang | 1076481 | 2021-05-25 17:46:17 +0800 | [diff] [blame] | 5880 | err = bioset_init(&mddev->io_acct_set, BIO_POOL_SIZE, |
| 5881 | offsetof(struct md_io_acct, bio_clone), 0); |
| 5882 | if (err) |
| 5883 | goto exit_sync_set; |
NeilBrown | 5a85071 | 2017-06-21 09:12:21 +1000 | [diff] [blame] | 5884 | } |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 5885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | spin_lock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5887 | pers = find_pers(mddev->level, mddev->clevel); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 5888 | if (!pers || !try_module_get(pers->owner)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | spin_unlock(&pers_lock); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5890 | if (mddev->level != LEVEL_NONE) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5891 | pr_warn("md: personality for level %d is not loaded!\n", |
| 5892 | mddev->level); |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5893 | else |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5894 | pr_warn("md: personality for level %s is not loaded!\n", |
| 5895 | mddev->clevel); |
Shaohua Li | bfc9dfd | 2018-06-13 08:39:49 -0700 | [diff] [blame] | 5896 | err = -EINVAL; |
| 5897 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5899 | spin_unlock(&pers_lock); |
NeilBrown | 34817e8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5900 | if (mddev->level != pers->level) { |
| 5901 | mddev->level = pers->level; |
| 5902 | mddev->new_level = pers->level; |
| 5903 | } |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 5904 | strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5906 | if (mddev->reshape_position != MaxSector && |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5907 | pers->start_reshape == NULL) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5908 | /* This personality cannot handle reshaping... */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5909 | module_put(pers->owner); |
Shaohua Li | bfc9dfd | 2018-06-13 08:39:49 -0700 | [diff] [blame] | 5910 | err = -EINVAL; |
| 5911 | goto abort; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5912 | } |
| 5913 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5914 | if (pers->sync_request) { |
| 5915 | /* Warn if this is a potentially silly |
| 5916 | * configuration. |
| 5917 | */ |
| 5918 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5919 | struct md_rdev *rdev2; |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5920 | int warned = 0; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5921 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5922 | rdev_for_each(rdev, mddev) |
| 5923 | rdev_for_each(rdev2, mddev) { |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5924 | if (rdev < rdev2 && |
Christoph Hellwig | 61a27e1f | 2020-09-03 07:40:58 +0200 | [diff] [blame] | 5925 | rdev->bdev->bd_disk == |
| 5926 | rdev2->bdev->bd_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5927 | pr_warn("%s: WARNING: %s appears to be on the same physical disk as %s.\n", |
| 5928 | mdname(mddev), |
| 5929 | bdevname(rdev->bdev,b), |
| 5930 | bdevname(rdev2->bdev,b2)); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5931 | warned = 1; |
| 5932 | } |
| 5933 | } |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 5934 | |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5935 | if (warned) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5936 | pr_warn("True protection against single-disk failure might be compromised.\n"); |
NeilBrown | 7dd5e7c3 | 2007-02-28 20:11:35 -0800 | [diff] [blame] | 5937 | } |
| 5938 | |
NeilBrown | 657390d | 2005-08-26 18:34:16 -0700 | [diff] [blame] | 5939 | mddev->recovery = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5940 | /* may be over-ridden by personality */ |
| 5941 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5942 | |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5943 | mddev->ok_start_degraded = start_dirty_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | |
NeilBrown | 0f9552b5 | 2009-12-30 12:08:50 +1100 | [diff] [blame] | 5945 | if (start_readonly && mddev->ro == 0) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 5946 | mddev->ro = 2; /* read-only, but switch on first write */ |
| 5947 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5948 | err = pers->run(mddev); |
Andre Noll | 13e53df | 2008-03-26 00:07:03 +0100 | [diff] [blame] | 5949 | if (err) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5950 | pr_warn("md: pers->run() failed ...\n"); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5951 | else if (pers->size(mddev, 0, 0) < mddev->array_sectors) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5952 | WARN_ONCE(!mddev->external_size, |
| 5953 | "%s: default size too small, but 'external_size' not in effect?\n", |
| 5954 | __func__); |
| 5955 | pr_warn("md: invalid array_size %llu > default size %llu\n", |
| 5956 | (unsigned long long)mddev->array_sectors / 2, |
| 5957 | (unsigned long long)pers->size(mddev, 0, 0) / 2); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5958 | err = -EINVAL; |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5959 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 5960 | if (err == 0 && pers->sync_request && |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 5961 | (mddev->bitmap_info.file || mddev->bitmap_info.offset)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5962 | struct bitmap *bitmap; |
| 5963 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 5964 | bitmap = md_bitmap_create(mddev, -1); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5965 | if (IS_ERR(bitmap)) { |
| 5966 | err = PTR_ERR(bitmap); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 5967 | pr_warn("%s: failed to create bitmap (%d)\n", |
| 5968 | mdname(mddev), err); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 5969 | } else |
| 5970 | mddev->bitmap = bitmap; |
| 5971 | |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 5972 | } |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 5973 | if (err) |
| 5974 | goto bitmap_abort; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5975 | |
| 5976 | if (mddev->bitmap_info.max_write_behind > 0) { |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 5977 | bool create_pool = false; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5978 | |
| 5979 | rdev_for_each(rdev, mddev) { |
| 5980 | if (test_bit(WriteMostly, &rdev->flags) && |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 5981 | rdev_init_serial(rdev)) |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 5982 | create_pool = true; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5983 | } |
Guoqing Jiang | 3e173ab | 2019-12-23 10:48:54 +0100 | [diff] [blame] | 5984 | if (create_pool && mddev->serial_info_pool == NULL) { |
Guoqing Jiang | 404659c | 2019-12-23 10:48:53 +0100 | [diff] [blame] | 5985 | mddev->serial_info_pool = |
| 5986 | mempool_create_kmalloc_pool(NR_SERIAL_INFOS, |
| 5987 | sizeof(struct serial_info)); |
| 5988 | if (!mddev->serial_info_pool) { |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5989 | err = -ENOMEM; |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 5990 | goto bitmap_abort; |
Guoqing Jiang | 3e148a3 | 2019-06-19 17:30:46 +0800 | [diff] [blame] | 5991 | } |
| 5992 | } |
| 5993 | } |
| 5994 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5995 | if (mddev->queue) { |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 5996 | bool nonrot = true; |
| 5997 | |
| 5998 | rdev_for_each(rdev, mddev) { |
| 5999 | if (rdev->raid_disk >= 0 && |
| 6000 | !blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 6001 | nonrot = false; |
| 6002 | break; |
| 6003 | } |
| 6004 | } |
| 6005 | if (mddev->degraded) |
| 6006 | nonrot = false; |
| 6007 | if (nonrot) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 6008 | blk_queue_flag_set(QUEUE_FLAG_NONROT, mddev->queue); |
Shaohua Li | bb086a8 | 2016-09-30 09:45:40 -0700 | [diff] [blame] | 6009 | else |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 6010 | blk_queue_flag_clear(QUEUE_FLAG_NONROT, mddev->queue); |
Guoqing Jiang | 1076481 | 2021-05-25 17:46:17 +0800 | [diff] [blame] | 6011 | blk_queue_flag_set(QUEUE_FLAG_IO_STAT, mddev->queue); |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 6012 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6013 | if (pers->sync_request) { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6014 | if (mddev->kobj.sd && |
| 6015 | sysfs_create_group(&mddev->kobj, &md_redundancy_group)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6016 | pr_warn("md: cannot register extra attributes for %s\n", |
| 6017 | mdname(mddev)); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6018 | mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action"); |
Junxiao Bi | e8efa9b | 2020-08-04 17:27:18 -0700 | [diff] [blame] | 6019 | mddev->sysfs_completed = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_completed"); |
| 6020 | mddev->sysfs_degraded = sysfs_get_dirent_safe(mddev->kobj.sd, "degraded"); |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6021 | } else if (mddev->ro == 2) /* auto-readonly not meaningful */ |
NeilBrown | fd9d49c | 2005-11-08 21:39:42 -0800 | [diff] [blame] | 6022 | mddev->ro = 0; |
| 6023 | |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6024 | atomic_set(&mddev->max_corr_read_errors, |
| 6025 | MD_DEFAULT_MAX_CORRECTED_READ_ERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | mddev->safemode = 0; |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6027 | if (mddev_is_clustered(mddev)) |
| 6028 | mddev->safemode_delay = 0; |
| 6029 | else |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 6030 | mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6031 | mddev->in_sync = 1; |
NeilBrown | 0ca6988 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6032 | smp_wmb(); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6033 | spin_lock(&mddev->lock); |
| 6034 | mddev->pers = pers; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6035 | spin_unlock(&mddev->lock); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6036 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 6037 | if (rdev->raid_disk >= 0) |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 6038 | sysfs_link_rdev(mddev, rdev); /* failure here is OK */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6039 | |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 6040 | if (mddev->degraded && !mddev->ro) |
| 6041 | /* This ensures that recovering status is reported immediately |
| 6042 | * via sysfs - until a lack of spares is confirmed. |
| 6043 | */ |
| 6044 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6045 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6046 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6047 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6048 | md_update_sb(mddev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6049 | |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 6050 | md_new_event(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | return 0; |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6052 | |
Guoqing Jiang | d494549 | 2019-06-14 17:10:39 +0800 | [diff] [blame] | 6053 | bitmap_abort: |
| 6054 | mddev_detach(mddev); |
| 6055 | if (mddev->private) |
| 6056 | pers->free(mddev, mddev->private); |
| 6057 | mddev->private = NULL; |
| 6058 | module_put(pers->owner); |
| 6059 | md_bitmap_destroy(mddev); |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6060 | abort: |
Guoqing Jiang | daee202 | 2021-06-03 17:21:06 +0800 | [diff] [blame] | 6061 | if (mddev->level != 1 && mddev->level != 10) |
| 6062 | bioset_exit(&mddev->io_acct_set); |
Guoqing Jiang | 1076481 | 2021-05-25 17:46:17 +0800 | [diff] [blame] | 6063 | exit_sync_set: |
NeilBrown | 4bc034d | 2019-03-29 10:46:16 -0700 | [diff] [blame] | 6064 | bioset_exit(&mddev->sync_set); |
Guoqing Jiang | 1076481 | 2021-05-25 17:46:17 +0800 | [diff] [blame] | 6065 | exit_bio_set: |
| 6066 | bioset_exit(&mddev->bio_set); |
Xiao Ni | b126194 | 2018-01-24 12:17:38 +0800 | [diff] [blame] | 6067 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6069 | EXPORT_SYMBOL_GPL(md_run); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6071 | int do_md_run(struct mddev *mddev) |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6072 | { |
| 6073 | int err; |
| 6074 | |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6075 | set_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6076 | err = md_run(mddev); |
| 6077 | if (err) |
| 6078 | goto out; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6079 | err = md_bitmap_load(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6080 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6081 | md_bitmap_destroy(mddev); |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 6082 | goto out; |
| 6083 | } |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 6084 | |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6085 | if (mddev_is_clustered(mddev)) |
| 6086 | md_allow_write(mddev); |
| 6087 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 6088 | /* run start up tasks that require md_thread */ |
| 6089 | md_start(mddev); |
| 6090 | |
Jonathan Brassow | 0fd018a | 2011-06-07 17:49:36 -0500 | [diff] [blame] | 6091 | md_wakeup_thread(mddev->thread); |
| 6092 | md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */ |
| 6093 | |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 6094 | set_capacity_and_notify(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6095 | clear_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6096 | mddev->changed = 1; |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6097 | kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE); |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6098 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
| 6099 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 6100 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6101 | out: |
NeilBrown | 9d4b45d | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 6102 | clear_bit(MD_NOT_READY, &mddev->flags); |
NeilBrown | fe60b01 | 2010-03-29 11:10:42 +1100 | [diff] [blame] | 6103 | return err; |
| 6104 | } |
| 6105 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 6106 | int md_start(struct mddev *mddev) |
| 6107 | { |
| 6108 | int ret = 0; |
| 6109 | |
| 6110 | if (mddev->pers->start) { |
| 6111 | set_bit(MD_RECOVERY_WAIT, &mddev->recovery); |
| 6112 | md_wakeup_thread(mddev->thread); |
| 6113 | ret = mddev->pers->start(mddev); |
| 6114 | clear_bit(MD_RECOVERY_WAIT, &mddev->recovery); |
| 6115 | md_wakeup_thread(mddev->sync_thread); |
| 6116 | } |
| 6117 | return ret; |
| 6118 | } |
| 6119 | EXPORT_SYMBOL_GPL(md_start); |
| 6120 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6121 | static int restart_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6122 | { |
| 6123 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6124 | struct md_rdev *rdev; |
| 6125 | bool has_journal = false; |
| 6126 | bool has_readonly = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6127 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6128 | /* Complain if it has no devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6129 | if (list_empty(&mddev->disks)) |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6130 | return -ENXIO; |
| 6131 | if (!mddev->pers) |
| 6132 | return -EINVAL; |
| 6133 | if (!mddev->ro) |
| 6134 | return -EBUSY; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6135 | |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6136 | rcu_read_lock(); |
| 6137 | rdev_for_each_rcu(rdev, mddev) { |
| 6138 | if (test_bit(Journal, &rdev->flags) && |
| 6139 | !test_bit(Faulty, &rdev->flags)) |
| 6140 | has_journal = true; |
Christoph Hellwig | a42e0d7 | 2021-02-01 14:17:21 +0100 | [diff] [blame] | 6141 | if (rdev_read_only(rdev)) |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6142 | has_readonly = true; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6143 | } |
NeilBrown | 97b20ef | 2017-04-13 08:53:48 +1000 | [diff] [blame] | 6144 | rcu_read_unlock(); |
| 6145 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && !has_journal) |
| 6146 | /* Don't restart rw with journal missing/faulty */ |
| 6147 | return -EINVAL; |
| 6148 | if (has_readonly) |
| 6149 | return -EROFS; |
Song Liu | 339421d | 2015-10-08 21:54:13 -0700 | [diff] [blame] | 6150 | |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6151 | mddev->safemode = 0; |
| 6152 | mddev->ro = 0; |
| 6153 | set_disk_ro(disk, 0); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6154 | pr_debug("md: %s switched to read-write mode.\n", mdname(mddev)); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6155 | /* Kick recovery or resync if necessary */ |
| 6156 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6157 | md_wakeup_thread(mddev->thread); |
| 6158 | md_wakeup_thread(mddev->sync_thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6159 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Andre Noll | 80fab1d | 2008-07-11 22:02:21 +1000 | [diff] [blame] | 6160 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6161 | } |
| 6162 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6163 | static void md_clean(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6164 | { |
| 6165 | mddev->array_sectors = 0; |
| 6166 | mddev->external_size = 0; |
| 6167 | mddev->dev_sectors = 0; |
| 6168 | mddev->raid_disks = 0; |
| 6169 | mddev->recovery_cp = 0; |
| 6170 | mddev->resync_min = 0; |
| 6171 | mddev->resync_max = MaxSector; |
| 6172 | mddev->reshape_position = MaxSector; |
| 6173 | mddev->external = 0; |
| 6174 | mddev->persistent = 0; |
| 6175 | mddev->level = LEVEL_NONE; |
| 6176 | mddev->clevel[0] = 0; |
| 6177 | mddev->flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6178 | mddev->sb_flags = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6179 | mddev->ro = 0; |
| 6180 | mddev->metadata_type[0] = 0; |
| 6181 | mddev->chunk_sectors = 0; |
| 6182 | mddev->ctime = mddev->utime = 0; |
| 6183 | mddev->layout = 0; |
| 6184 | mddev->max_disks = 0; |
| 6185 | mddev->events = 0; |
NeilBrown | a8707c0 | 2010-05-18 09:28:43 +1000 | [diff] [blame] | 6186 | mddev->can_decrease_events = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6187 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6188 | mddev->reshape_backwards = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6189 | mddev->new_level = LEVEL_NONE; |
| 6190 | mddev->new_layout = 0; |
| 6191 | mddev->new_chunk_sectors = 0; |
| 6192 | mddev->curr_resync = 0; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 6193 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6194 | mddev->suspend_lo = mddev->suspend_hi = 0; |
| 6195 | mddev->sync_speed_min = mddev->sync_speed_max = 0; |
| 6196 | mddev->recovery = 0; |
| 6197 | mddev->in_sync = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6198 | mddev->changed = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6199 | mddev->degraded = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6200 | mddev->safemode = 0; |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 6201 | mddev->private = NULL; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 6202 | mddev->cluster_info = NULL; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6203 | mddev->bitmap_info.offset = 0; |
| 6204 | mddev->bitmap_info.default_offset = 0; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 6205 | mddev->bitmap_info.default_space = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6206 | mddev->bitmap_info.chunksize = 0; |
| 6207 | mddev->bitmap_info.daemon_sleep = 0; |
| 6208 | mddev->bitmap_info.max_write_behind = 0; |
Guoqing Jiang | c20c33f | 2016-08-12 13:42:38 +0800 | [diff] [blame] | 6209 | mddev->bitmap_info.nodes = 0; |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6210 | } |
| 6211 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6212 | static void __md_stop_writes(struct mddev *mddev) |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6213 | { |
NeilBrown | 6b6204e | 2013-05-09 09:48:30 +1000 | [diff] [blame] | 6214 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Guoqing Jiang | 21e0958 | 2020-04-04 23:57:07 +0200 | [diff] [blame] | 6215 | if (work_pending(&mddev->del_work)) |
| 6216 | flush_workqueue(md_misc_wq); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6217 | if (mddev->sync_thread) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6218 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 6219 | md_reap_sync_thread(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6220 | } |
| 6221 | |
| 6222 | del_timer_sync(&mddev->safemode_timer); |
| 6223 | |
Shaohua Li | 034e33f | 2016-11-21 10:29:19 -0800 | [diff] [blame] | 6224 | if (mddev->pers && mddev->pers->quiesce) { |
| 6225 | mddev->pers->quiesce(mddev, 1); |
| 6226 | mddev->pers->quiesce(mddev, 0); |
| 6227 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6228 | md_bitmap_flush(mddev); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6229 | |
NeilBrown | b6d428c | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 6230 | if (mddev->ro == 0 && |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6231 | ((!mddev->in_sync && !mddev_is_clustered(mddev)) || |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6232 | mddev->sb_flags)) { |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6233 | /* mark array as shutdown cleanly */ |
Goldwyn Rodrigues | 28c1b9f | 2015-10-22 16:01:25 +1100 | [diff] [blame] | 6234 | if (!mddev_is_clustered(mddev)) |
| 6235 | mddev->in_sync = 1; |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6236 | md_update_sb(mddev, 1); |
| 6237 | } |
Guoqing Jiang | 69b00b5 | 2019-12-23 10:49:00 +0100 | [diff] [blame] | 6238 | /* disable policy to guarantee rdevs free resources for serialization */ |
| 6239 | mddev->serialize_policy = 0; |
| 6240 | mddev_destroy_serial_pool(mddev, NULL, true); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6241 | } |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6242 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6243 | void md_stop_writes(struct mddev *mddev) |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6244 | { |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 6245 | mddev_lock_nointr(mddev); |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6246 | __md_stop_writes(mddev); |
| 6247 | mddev_unlock(mddev); |
| 6248 | } |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6249 | EXPORT_SYMBOL_GPL(md_stop_writes); |
NeilBrown | a047e12 | 2010-03-29 12:07:53 +1100 | [diff] [blame] | 6250 | |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6251 | static void mddev_detach(struct mddev *mddev) |
| 6252 | { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6253 | md_bitmap_wait_behind_writes(mddev); |
Guoqing Jiang | 6b40bec | 2020-02-11 11:10:04 +0100 | [diff] [blame] | 6254 | if (mddev->pers && mddev->pers->quiesce && !mddev->suspended) { |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6255 | mddev->pers->quiesce(mddev, 1); |
| 6256 | mddev->pers->quiesce(mddev, 0); |
| 6257 | } |
| 6258 | md_unregister_thread(&mddev->thread); |
| 6259 | if (mddev->queue) |
| 6260 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 6261 | } |
| 6262 | |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6263 | static void __md_stop(struct mddev *mddev) |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6264 | { |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6265 | struct md_personality *pers = mddev->pers; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6266 | md_bitmap_destroy(mddev); |
NeilBrown | 5aa61f4 | 2014-12-15 12:56:57 +1100 | [diff] [blame] | 6267 | mddev_detach(mddev); |
NeilBrown | ee5d004 | 2015-07-22 10:20:07 +1000 | [diff] [blame] | 6268 | /* Ensure ->event_work is done */ |
Guoqing Jiang | 21e0958 | 2020-04-04 23:57:07 +0200 | [diff] [blame] | 6269 | if (mddev->event_work.func) |
| 6270 | flush_workqueue(md_misc_wq); |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6271 | spin_lock(&mddev->lock); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6272 | mddev->pers = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6273 | spin_unlock(&mddev->lock); |
zhangyue | 07641b5 | 2021-11-16 10:35:26 +0800 | [diff] [blame^] | 6274 | if (mddev->private) |
| 6275 | pers->free(mddev, mddev->private); |
NeilBrown | bd69192 | 2015-06-25 17:01:40 +1000 | [diff] [blame] | 6276 | mddev->private = NULL; |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 6277 | if (pers->sync_request && mddev->to_remove == NULL) |
| 6278 | mddev->to_remove = &md_redundancy_group; |
| 6279 | module_put(pers->owner); |
NeilBrown | cca9cf9 | 2010-04-01 12:08:16 +1100 | [diff] [blame] | 6280 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
Jack Wang | 6aaa58c | 2018-10-19 16:21:31 +0200 | [diff] [blame] | 6281 | } |
| 6282 | |
| 6283 | void md_stop(struct mddev *mddev) |
| 6284 | { |
| 6285 | /* stop the array and free an attached data structures. |
| 6286 | * This is called from dm-raid |
| 6287 | */ |
| 6288 | __md_stop(mddev); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 6289 | bioset_exit(&mddev->bio_set); |
| 6290 | bioset_exit(&mddev->sync_set); |
Guoqing Jiang | daee202 | 2021-06-03 17:21:06 +0800 | [diff] [blame] | 6291 | if (mddev->level != 1 && mddev->level != 10) |
| 6292 | bioset_exit(&mddev->io_acct_set); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6293 | } |
| 6294 | |
NeilBrown | 390ee60 | 2010-06-01 19:37:27 +1000 | [diff] [blame] | 6295 | EXPORT_SYMBOL_GPL(md_stop); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6296 | |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6297 | static int md_set_readonly(struct mddev *mddev, struct block_device *bdev) |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6298 | { |
| 6299 | int err = 0; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6300 | int did_freeze = 0; |
| 6301 | |
| 6302 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 6303 | did_freeze = 1; |
| 6304 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 6305 | md_wakeup_thread(mddev->thread); |
| 6306 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6307 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6308 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6309 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6310 | /* Thread might be blocked waiting for metadata update |
| 6311 | * which will now never happen */ |
| 6312 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6313 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6314 | if (mddev->external && test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 6315 | return -EBUSY; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6316 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6317 | wait_event(resync_wait, !test_bit(MD_RECOVERY_RUNNING, |
| 6318 | &mddev->recovery)); |
NeilBrown | 88724bf | 2015-09-24 14:00:51 +1000 | [diff] [blame] | 6319 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6320 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6321 | mddev_lock_nointr(mddev); |
| 6322 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6323 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6324 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6325 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6326 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6327 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6328 | if (did_freeze) { |
| 6329 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6330 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6331 | md_wakeup_thread(mddev->thread); |
| 6332 | } |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6333 | err = -EBUSY; |
| 6334 | goto out; |
| 6335 | } |
| 6336 | if (mddev->pers) { |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6337 | __md_stop_writes(mddev); |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6338 | |
| 6339 | err = -ENXIO; |
| 6340 | if (mddev->ro==1) |
| 6341 | goto out; |
| 6342 | mddev->ro = 1; |
| 6343 | set_disk_ro(mddev->gendisk, 1); |
| 6344 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6345 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6346 | md_wakeup_thread(mddev->thread); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6347 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6348 | err = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6349 | } |
| 6350 | out: |
| 6351 | mutex_unlock(&mddev->open_mutex); |
| 6352 | return err; |
| 6353 | } |
| 6354 | |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6355 | /* mode: |
| 6356 | * 0 - completely stop and dis-assemble array |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6357 | * 2 - stop but do not disassemble array |
| 6358 | */ |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6359 | static int do_md_stop(struct mddev *mddev, int mode, |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6360 | struct block_device *bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6362 | struct gendisk *disk = mddev->gendisk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6363 | struct md_rdev *rdev; |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6364 | int did_freeze = 0; |
| 6365 | |
| 6366 | if (!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) { |
| 6367 | did_freeze = 1; |
| 6368 | set_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
| 6369 | md_wakeup_thread(mddev->thread); |
| 6370 | } |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6371 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6372 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6373 | if (mddev->sync_thread) |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6374 | /* Thread might be blocked waiting for metadata update |
| 6375 | * which will now never happen */ |
| 6376 | wake_up_process(mddev->sync_thread->tsk); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6377 | |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6378 | mddev_unlock(mddev); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 6379 | wait_event(resync_wait, (mddev->sync_thread == NULL && |
| 6380 | !test_bit(MD_RECOVERY_RUNNING, |
| 6381 | &mddev->recovery))); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6382 | mddev_lock_nointr(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 6384 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 6385 | if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) || |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6386 | mddev->sysfs_active || |
| 6387 | mddev->sync_thread || |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 6388 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6389 | pr_warn("md: %s still in use.\n",mdname(mddev)); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6390 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6391 | if (did_freeze) { |
| 6392 | clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery); |
NeilBrown | 45eaf45 | 2014-10-29 08:49:50 +1100 | [diff] [blame] | 6393 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | 30b8feb | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6394 | md_wakeup_thread(mddev->thread); |
| 6395 | } |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 6396 | return -EBUSY; |
| 6397 | } |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6398 | if (mddev->pers) { |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6399 | if (mddev->ro) |
| 6400 | set_disk_ro(disk, 0); |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6401 | |
NeilBrown | defad61 | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6402 | __md_stop_writes(mddev); |
NeilBrown | 5eff3c4 | 2012-11-19 10:47:48 +1100 | [diff] [blame] | 6403 | __md_stop(mddev); |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6404 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6405 | /* tell userspace to handle 'inactive' */ |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6406 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 6407 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6408 | rdev_for_each(rdev, mddev) |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 6409 | if (rdev->raid_disk >= 0) |
| 6410 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | c464729 | 2009-05-07 12:51:06 +1000 | [diff] [blame] | 6411 | |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 6412 | set_capacity_and_notify(disk, 0); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6413 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 6414 | mddev->changed = 1; |
NeilBrown | 0d4ca60 | 2006-12-10 02:20:44 -0800 | [diff] [blame] | 6415 | |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6416 | if (mddev->ro) |
| 6417 | mddev->ro = 0; |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6418 | } else |
| 6419 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | /* |
| 6421 | * Free resources if final stop |
| 6422 | */ |
NeilBrown | 9e653b6 | 2006-06-26 00:27:58 -0700 | [diff] [blame] | 6423 | if (mode == 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6424 | pr_info("md: %s stopped.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6426 | if (mddev->bitmap_info.file) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6427 | struct file *f = mddev->bitmap_info.file; |
| 6428 | spin_lock(&mddev->lock); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6429 | mddev->bitmap_info.file = NULL; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6430 | spin_unlock(&mddev->lock); |
| 6431 | fput(f); |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 6432 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6433 | mddev->bitmap_info.offset = 0; |
NeilBrown | 978f946 | 2006-02-02 14:28:05 -0800 | [diff] [blame] | 6434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | export_array(mddev); |
| 6436 | |
NeilBrown | 6177b47 | 2010-03-29 11:37:13 +1100 | [diff] [blame] | 6437 | md_clean(mddev); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 6438 | if (mddev->hold_active == UNTIL_STOP) |
| 6439 | mddev->hold_active = 0; |
NeilBrown | a4bd82d | 2010-03-29 13:23:10 +1100 | [diff] [blame] | 6440 | } |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 6441 | md_new_event(); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6442 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
NeilBrown | 6e17b02 | 2010-08-07 21:41:19 +1000 | [diff] [blame] | 6443 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6444 | } |
| 6445 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6446 | #ifndef MODULE |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6447 | static void autorun_array(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6448 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6449 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | int err; |
| 6451 | |
NeilBrown | a757e64 | 2005-04-16 15:26:42 -0700 | [diff] [blame] | 6452 | if (list_empty(&mddev->disks)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6455 | pr_info("md: running: "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6456 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6457 | rdev_for_each(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6458 | char b[BDEVNAME_SIZE]; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6459 | pr_cont("<%s>", bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6461 | pr_cont("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6462 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6463 | err = do_md_run(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6464 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6465 | pr_warn("md: do_md_run() returned %d\n", err); |
NeilBrown | a05b7ea | 2012-07-19 15:59:18 +1000 | [diff] [blame] | 6466 | do_md_stop(mddev, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | } |
| 6468 | } |
| 6469 | |
| 6470 | /* |
| 6471 | * lets try to run arrays based on all disks that have arrived |
| 6472 | * until now. (those are in pending_raid_disks) |
| 6473 | * |
| 6474 | * the method: pick the first pending disk, collect all disks with |
| 6475 | * the same UUID, remove all from the pending list and put them into |
| 6476 | * the 'same_array' list. Then order this list based on superblock |
| 6477 | * update time (freshest comes first), kick out 'old' disks and |
| 6478 | * compare superblocks. If everything's fine then run it. |
| 6479 | * |
| 6480 | * If "unit" is allocated, then bump its reference count |
| 6481 | */ |
| 6482 | static void autorun_devices(int part) |
| 6483 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6484 | struct md_rdev *rdev0, *rdev, *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6485 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6486 | char b[BDEVNAME_SIZE]; |
| 6487 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6488 | pr_info("md: autorun ...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | while (!list_empty(&pending_raid_disks)) { |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6490 | int unit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6491 | dev_t dev; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 6492 | LIST_HEAD(candidates); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6493 | rdev0 = list_entry(pending_raid_disks.next, |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6494 | struct md_rdev, same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6496 | pr_debug("md: considering %s ...\n", bdevname(rdev0->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6497 | INIT_LIST_HEAD(&candidates); |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6498 | rdev_for_each_list(rdev, tmp, &pending_raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6499 | if (super_90_load(rdev, rdev0, 0) >= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6500 | pr_debug("md: adding %s ...\n", |
| 6501 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6502 | list_move(&rdev->same_set, &candidates); |
| 6503 | } |
| 6504 | /* |
| 6505 | * now we have a set of devices, with all of them having |
| 6506 | * mostly sane superblocks. It's time to allocate the |
| 6507 | * mddev. |
| 6508 | */ |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 6509 | if (part) { |
| 6510 | dev = MKDEV(mdp_major, |
| 6511 | rdev0->preferred_minor << MdpMinorShift); |
| 6512 | unit = MINOR(dev) >> MdpMinorShift; |
| 6513 | } else { |
| 6514 | dev = MKDEV(MD_MAJOR, rdev0->preferred_minor); |
| 6515 | unit = MINOR(dev); |
| 6516 | } |
| 6517 | if (rdev0->preferred_minor != unit) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6518 | pr_warn("md: unit number in %s is bad: %d\n", |
| 6519 | bdevname(rdev0->bdev, b), rdev0->preferred_minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | break; |
| 6521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6522 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 6523 | md_probe(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6524 | mddev = mddev_find(dev); |
Christoph Hellwig | 65aa97c | 2021-04-03 18:15:29 +0200 | [diff] [blame] | 6525 | if (!mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | break; |
Christoph Hellwig | 65aa97c | 2021-04-03 18:15:29 +0200 | [diff] [blame] | 6527 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6528 | if (mddev_lock(mddev)) |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6529 | pr_warn("md: %s locked, cannot run\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6530 | else if (mddev->raid_disks || mddev->major_version |
| 6531 | || !list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6532 | pr_warn("md: %s already running, cannot run %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6533 | mdname(mddev), bdevname(rdev0->bdev,b)); |
| 6534 | mddev_unlock(mddev); |
| 6535 | } else { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6536 | pr_debug("md: created %s\n", mdname(mddev)); |
NeilBrown | 1ec4a93 | 2008-02-06 01:39:53 -0800 | [diff] [blame] | 6537 | mddev->persistent = 1; |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6538 | rdev_for_each_list(rdev, tmp, &candidates) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | list_del_init(&rdev->same_set); |
| 6540 | if (bind_rdev_to_array(rdev, mddev)) |
| 6541 | export_rdev(rdev); |
| 6542 | } |
| 6543 | autorun_array(mddev); |
| 6544 | mddev_unlock(mddev); |
| 6545 | } |
| 6546 | /* on success, candidates will be empty, on error |
| 6547 | * it won't... |
| 6548 | */ |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 6549 | rdev_for_each_list(rdev, tmp, &candidates) { |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6550 | list_del_init(&rdev->same_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6551 | export_rdev(rdev); |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 6552 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | mddev_put(mddev); |
| 6554 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6555 | pr_info("md: ... autorun DONE.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | } |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 6557 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6558 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6559 | static int get_version(void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6560 | { |
| 6561 | mdu_version_t ver; |
| 6562 | |
| 6563 | ver.major = MD_MAJOR_VERSION; |
| 6564 | ver.minor = MD_MINOR_VERSION; |
| 6565 | ver.patchlevel = MD_PATCHLEVEL_VERSION; |
| 6566 | |
| 6567 | if (copy_to_user(arg, &ver, sizeof(ver))) |
| 6568 | return -EFAULT; |
| 6569 | |
| 6570 | return 0; |
| 6571 | } |
| 6572 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6573 | static int get_array_info(struct mddev *mddev, void __user *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | { |
| 6575 | mdu_array_info_t info; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6576 | int nr,working,insync,failed,spare; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6577 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6579 | nr = working = insync = failed = spare = 0; |
| 6580 | rcu_read_lock(); |
| 6581 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6582 | nr++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6583 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6584 | failed++; |
| 6585 | else { |
| 6586 | working++; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6587 | if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6588 | insync++; |
Song Liu | b347af8 | 2016-08-11 17:14:45 -0700 | [diff] [blame] | 6589 | else if (test_bit(Journal, &rdev->flags)) |
| 6590 | /* TODO: add journal count to md_u.h */ |
| 6591 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | else |
| 6593 | spare++; |
| 6594 | } |
| 6595 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6596 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6597 | |
| 6598 | info.major_version = mddev->major_version; |
| 6599 | info.minor_version = mddev->minor_version; |
| 6600 | info.patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6601 | info.ctime = clamp_t(time64_t, mddev->ctime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6602 | info.level = mddev->level; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6603 | info.size = mddev->dev_sectors / 2; |
| 6604 | if (info.size != mddev->dev_sectors / 2) /* overflow */ |
NeilBrown | 284ae7c | 2006-02-03 03:03:40 -0800 | [diff] [blame] | 6605 | info.size = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6606 | info.nr_disks = nr; |
| 6607 | info.raid_disks = mddev->raid_disks; |
| 6608 | info.md_minor = mddev->md_minor; |
| 6609 | info.not_persistent= !mddev->persistent; |
| 6610 | |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 6611 | info.utime = clamp_t(time64_t, mddev->utime, 0, U32_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6612 | info.state = 0; |
| 6613 | if (mddev->in_sync) |
| 6614 | info.state = (1<<MD_SB_CLEAN); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 6615 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 9bd3592 | 2014-07-02 11:35:06 +1000 | [diff] [blame] | 6616 | info.state |= (1<<MD_SB_BITMAP_PRESENT); |
Goldwyn Rodrigues | ca8895d | 2014-11-26 12:22:03 -0600 | [diff] [blame] | 6617 | if (mddev_is_clustered(mddev)) |
| 6618 | info.state |= (1<<MD_SB_CLUSTERED); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6619 | info.active_disks = insync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6620 | info.working_disks = working; |
| 6621 | info.failed_disks = failed; |
| 6622 | info.spare_disks = spare; |
| 6623 | |
| 6624 | info.layout = mddev->layout; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6625 | info.chunk_size = mddev->chunk_sectors << 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6626 | |
| 6627 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6628 | return -EFAULT; |
| 6629 | |
| 6630 | return 0; |
| 6631 | } |
| 6632 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6633 | static int get_bitmap_file(struct mddev *mddev, void __user * arg) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6634 | { |
| 6635 | mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */ |
NeilBrown | f4ad3d3 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6636 | char *ptr; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6637 | int err; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6638 | |
Benjamin Randazzo | b6878d9 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6639 | file = kzalloc(sizeof(*file), GFP_NOIO); |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6640 | if (!file) |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6641 | return -ENOMEM; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6642 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6643 | err = 0; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6644 | spin_lock(&mddev->lock); |
Benjamin Randazzo | 25eafe1 | 2015-07-25 16:36:50 +0200 | [diff] [blame] | 6645 | /* bitmap enabled */ |
| 6646 | if (mddev->bitmap_info.file) { |
| 6647 | ptr = file_path(mddev->bitmap_info.file, file->pathname, |
| 6648 | sizeof(file->pathname)); |
| 6649 | if (IS_ERR(ptr)) |
| 6650 | err = PTR_ERR(ptr); |
| 6651 | else |
| 6652 | memmove(file->pathname, ptr, |
| 6653 | sizeof(file->pathname)-(ptr-file->pathname)); |
| 6654 | } |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6655 | spin_unlock(&mddev->lock); |
| 6656 | |
| 6657 | if (err == 0 && |
| 6658 | copy_to_user(arg, file, sizeof(*file))) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6659 | err = -EFAULT; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 6660 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 6661 | kfree(file); |
| 6662 | return err; |
| 6663 | } |
| 6664 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6665 | static int get_disk_info(struct mddev *mddev, void __user * arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6666 | { |
| 6667 | mdu_disk_info_t info; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6668 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6669 | |
| 6670 | if (copy_from_user(&info, arg, sizeof(info))) |
| 6671 | return -EFAULT; |
| 6672 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6673 | rcu_read_lock(); |
Goldwyn Rodrigues | 57d051d | 2015-04-14 10:43:55 -0500 | [diff] [blame] | 6674 | rdev = md_find_rdev_nr_rcu(mddev, info.number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6675 | if (rdev) { |
| 6676 | info.major = MAJOR(rdev->bdev->bd_dev); |
| 6677 | info.minor = MINOR(rdev->bdev->bd_dev); |
| 6678 | info.raid_disk = rdev->raid_disk; |
| 6679 | info.state = 0; |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6680 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6681 | info.state |= (1<<MD_DISK_FAULTY); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6682 | else if (test_bit(In_sync, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6683 | info.state |= (1<<MD_DISK_ACTIVE); |
| 6684 | info.state |= (1<<MD_DISK_SYNC); |
| 6685 | } |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 6686 | if (test_bit(Journal, &rdev->flags)) |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6687 | info.state |= (1<<MD_DISK_JOURNAL); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6688 | if (test_bit(WriteMostly, &rdev->flags)) |
| 6689 | info.state |= (1<<MD_DISK_WRITEMOSTLY); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6690 | if (test_bit(FailFast, &rdev->flags)) |
| 6691 | info.state |= (1<<MD_DISK_FAILFAST); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6692 | } else { |
| 6693 | info.major = info.minor = 0; |
| 6694 | info.raid_disk = -1; |
| 6695 | info.state = (1<<MD_DISK_REMOVED); |
| 6696 | } |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 6697 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6698 | |
| 6699 | if (copy_to_user(arg, &info, sizeof(info))) |
| 6700 | return -EFAULT; |
| 6701 | |
| 6702 | return 0; |
| 6703 | } |
| 6704 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6705 | int md_add_new_disk(struct mddev *mddev, struct mdu_disk_info_s *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | { |
| 6707 | char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6708 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | dev_t dev = MKDEV(info->major,info->minor); |
| 6710 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6711 | if (mddev_is_clustered(mddev) && |
| 6712 | !(info->state & ((1 << MD_DISK_CLUSTER_ADD) | (1 << MD_DISK_CANDIDATE)))) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6713 | pr_warn("%s: Cannot add to clustered mddev.\n", |
| 6714 | mdname(mddev)); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6715 | return -EINVAL; |
| 6716 | } |
| 6717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6718 | if (info->major != MAJOR(dev) || info->minor != MINOR(dev)) |
| 6719 | return -EOVERFLOW; |
| 6720 | |
| 6721 | if (!mddev->raid_disks) { |
| 6722 | int err; |
| 6723 | /* expecting a device which has a superblock */ |
| 6724 | rdev = md_import_device(dev, mddev->major_version, mddev->minor_version); |
| 6725 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6726 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | PTR_ERR(rdev)); |
| 6728 | return PTR_ERR(rdev); |
| 6729 | } |
| 6730 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6731 | struct md_rdev *rdev0 |
| 6732 | = list_entry(mddev->disks.next, |
| 6733 | struct md_rdev, same_set); |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6734 | err = super_types[mddev->major_version] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | .load_super(rdev, rdev0, mddev->minor_version); |
| 6736 | if (err < 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6737 | pr_warn("md: %s has different UUID to %s\n", |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6738 | bdevname(rdev->bdev,b), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6739 | bdevname(rdev0->bdev,b2)); |
| 6740 | export_rdev(rdev); |
| 6741 | return -EINVAL; |
| 6742 | } |
| 6743 | } |
| 6744 | err = bind_rdev_to_array(rdev, mddev); |
| 6745 | if (err) |
| 6746 | export_rdev(rdev); |
| 6747 | return err; |
| 6748 | } |
| 6749 | |
| 6750 | /* |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6751 | * md_add_new_disk can be used once the array is assembled |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | * to add "hot spares". They must already have a superblock |
| 6753 | * written |
| 6754 | */ |
| 6755 | if (mddev->pers) { |
| 6756 | int err; |
| 6757 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6758 | pr_warn("%s: personality does not support diskops!\n", |
| 6759 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | return -EINVAL; |
| 6761 | } |
NeilBrown | 7b1e35f | 2005-09-09 16:23:50 -0700 | [diff] [blame] | 6762 | if (mddev->persistent) |
| 6763 | rdev = md_import_device(dev, mddev->major_version, |
| 6764 | mddev->minor_version); |
| 6765 | else |
| 6766 | rdev = md_import_device(dev, -1, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6768 | pr_warn("md: md_import_device returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | PTR_ERR(rdev)); |
| 6770 | return PTR_ERR(rdev); |
| 6771 | } |
NeilBrown | 1a855a0 | 2010-12-09 16:36:28 +1100 | [diff] [blame] | 6772 | /* set saved_raid_disk if appropriate */ |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6773 | if (!mddev->persistent) { |
| 6774 | if (info->state & (1<<MD_DISK_SYNC) && |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6775 | info->raid_disk < mddev->raid_disks) { |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6776 | rdev->raid_disk = info->raid_disk; |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6777 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 6778 | clear_bit(Bitmap_sync, &rdev->flags); |
NeilBrown | bf57254 | 2011-01-12 09:03:35 +1100 | [diff] [blame] | 6779 | } else |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6780 | rdev->raid_disk = -1; |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 6781 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | 41158c7 | 2005-06-21 17:17:25 -0700 | [diff] [blame] | 6782 | } else |
| 6783 | super_types[mddev->major_version]. |
| 6784 | validate_super(mddev, rdev); |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6785 | if ((info->state & (1<<MD_DISK_SYNC)) && |
NeilBrown | f456309 | 2012-07-03 15:59:06 +1000 | [diff] [blame] | 6786 | rdev->raid_disk != info->raid_disk) { |
NeilBrown | bedd86b | 2011-05-11 14:26:20 +1000 | [diff] [blame] | 6787 | /* This was a hot-add request, but events doesn't |
| 6788 | * match, so reject it. |
| 6789 | */ |
| 6790 | export_rdev(rdev); |
| 6791 | return -EINVAL; |
| 6792 | } |
| 6793 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6794 | clear_bit(In_sync, &rdev->flags); /* just to be sure */ |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6795 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6796 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 575a80f | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6797 | else |
| 6798 | clear_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6799 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6800 | set_bit(FailFast, &rdev->flags); |
| 6801 | else |
| 6802 | clear_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6803 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6804 | if (info->state & (1<<MD_DISK_JOURNAL)) { |
| 6805 | struct md_rdev *rdev2; |
| 6806 | bool has_journal = false; |
| 6807 | |
| 6808 | /* make sure no existing journal disk */ |
| 6809 | rdev_for_each(rdev2, mddev) { |
| 6810 | if (test_bit(Journal, &rdev2->flags)) { |
| 6811 | has_journal = true; |
| 6812 | break; |
| 6813 | } |
| 6814 | } |
NeilBrown | 230b55f | 2017-10-17 14:24:09 +1100 | [diff] [blame] | 6815 | if (has_journal || mddev->bitmap) { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6816 | export_rdev(rdev); |
| 6817 | return -EBUSY; |
| 6818 | } |
Song Liu | bac624f | 2015-08-13 14:31:55 -0700 | [diff] [blame] | 6819 | set_bit(Journal, &rdev->flags); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 6820 | } |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6821 | /* |
| 6822 | * check whether the device shows up in other nodes |
| 6823 | */ |
| 6824 | if (mddev_is_clustered(mddev)) { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6825 | if (info->state & (1 << MD_DISK_CANDIDATE)) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6826 | set_bit(Candidate, &rdev->flags); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6827 | else if (info->state & (1 << MD_DISK_CLUSTER_ADD)) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6828 | /* --add initiated by this node */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6829 | err = md_cluster_ops->add_new_disk(mddev, rdev); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6830 | if (err) { |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 6831 | export_rdev(rdev); |
| 6832 | return err; |
| 6833 | } |
| 6834 | } |
| 6835 | } |
| 6836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | rdev->raid_disk = -1; |
| 6838 | err = bind_rdev_to_array(rdev, mddev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | if (err) |
| 6841 | export_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6842 | |
| 6843 | if (mddev_is_clustered(mddev)) { |
Guoqing Jiang | e566aef | 2016-08-12 13:42:34 +0800 | [diff] [blame] | 6844 | if (info->state & (1 << MD_DISK_CANDIDATE)) { |
| 6845 | if (!err) { |
| 6846 | err = md_cluster_ops->new_disk_ack(mddev, |
| 6847 | err == 0); |
| 6848 | if (err) |
| 6849 | md_kick_rdev_from_array(rdev); |
| 6850 | } |
| 6851 | } else { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6852 | if (err) |
| 6853 | md_cluster_ops->add_new_disk_cancel(mddev); |
| 6854 | else |
| 6855 | err = add_bound_rdev(rdev); |
| 6856 | } |
| 6857 | |
| 6858 | } else if (!err) |
Goldwyn Rodrigues | a6da4ef | 2015-04-14 10:45:22 -0500 | [diff] [blame] | 6859 | err = add_bound_rdev(rdev); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 6860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | return err; |
| 6862 | } |
| 6863 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 6864 | /* otherwise, md_add_new_disk is only allowed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | * for major_version==0 superblocks |
| 6866 | */ |
| 6867 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6868 | pr_warn("%s: ADD_NEW_DISK not supported\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | return -EINVAL; |
| 6870 | } |
| 6871 | |
| 6872 | if (!(info->state & (1<<MD_DISK_FAULTY))) { |
| 6873 | int err; |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6874 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6876 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | PTR_ERR(rdev)); |
| 6878 | return PTR_ERR(rdev); |
| 6879 | } |
| 6880 | rdev->desc_nr = info->number; |
| 6881 | if (info->raid_disk < mddev->raid_disks) |
| 6882 | rdev->raid_disk = info->raid_disk; |
| 6883 | else |
| 6884 | rdev->raid_disk = -1; |
| 6885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6886 | if (rdev->raid_disk < mddev->raid_disks) |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6887 | if (info->state & (1<<MD_DISK_SYNC)) |
| 6888 | set_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6889 | |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6890 | if (info->state & (1<<MD_DISK_WRITEMOSTLY)) |
| 6891 | set_bit(WriteMostly, &rdev->flags); |
NeilBrown | 688834e | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 6892 | if (info->state & (1<<MD_DISK_FAILFAST)) |
| 6893 | set_bit(FailFast, &rdev->flags); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 6894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6895 | if (!mddev->persistent) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6896 | pr_debug("md: nonpersistent superblock ...\n"); |
Christoph Hellwig | 0fe8034 | 2021-10-18 12:11:06 +0200 | [diff] [blame] | 6897 | rdev->sb_start = bdev_nr_sectors(rdev->bdev); |
Mike Snitzer | 77304d2 | 2010-11-08 14:39:12 +0100 | [diff] [blame] | 6898 | } else |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6899 | rdev->sb_start = calc_dev_sboffset(rdev); |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6900 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6901 | |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6902 | err = bind_rdev_to_array(rdev, mddev); |
| 6903 | if (err) { |
| 6904 | export_rdev(rdev); |
| 6905 | return err; |
| 6906 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6907 | } |
| 6908 | |
| 6909 | return 0; |
| 6910 | } |
| 6911 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6912 | static int hot_remove_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6913 | { |
| 6914 | char b[BDEVNAME_SIZE]; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6915 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | |
Yufen Yu | c42a0e2 | 2018-05-04 18:08:10 +0800 | [diff] [blame] | 6917 | if (!mddev->pers) |
| 6918 | return -ENODEV; |
| 6919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6920 | rdev = find_rdev(mddev, dev); |
| 6921 | if (!rdev) |
| 6922 | return -ENXIO; |
| 6923 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6924 | if (rdev->raid_disk < 0) |
| 6925 | goto kick_rdev; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6926 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6927 | clear_bit(Blocked, &rdev->flags); |
| 6928 | remove_and_add_spares(mddev, rdev); |
| 6929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6930 | if (rdev->raid_disk >= 0) |
| 6931 | goto busy; |
| 6932 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 6933 | kick_rdev: |
Zhao Heming | bca5b06 | 2020-11-19 19:41:34 +0800 | [diff] [blame] | 6934 | if (mddev_is_clustered(mddev)) { |
| 6935 | if (md_cluster_ops->remove_disk(mddev, rdev)) |
| 6936 | goto busy; |
| 6937 | } |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 6938 | |
Goldwyn Rodrigues | fb56dfe | 2015-04-14 10:43:24 -0500 | [diff] [blame] | 6939 | md_kick_rdev_from_array(rdev); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6940 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 6941 | if (mddev->thread) |
| 6942 | md_wakeup_thread(mddev->thread); |
| 6943 | else |
| 6944 | md_update_sb(mddev, 1); |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 6945 | md_new_event(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | |
| 6947 | return 0; |
| 6948 | busy: |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6949 | pr_debug("md: cannot remove active disk %s from %s ...\n", |
| 6950 | bdevname(rdev->bdev,b), mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | return -EBUSY; |
| 6952 | } |
| 6953 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6954 | static int hot_add_disk(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | { |
| 6956 | char b[BDEVNAME_SIZE]; |
| 6957 | int err; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6958 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6959 | |
| 6960 | if (!mddev->pers) |
| 6961 | return -ENODEV; |
| 6962 | |
| 6963 | if (mddev->major_version != 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6964 | 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] | 6965 | mdname(mddev)); |
| 6966 | return -EINVAL; |
| 6967 | } |
| 6968 | if (!mddev->pers->hot_add_disk) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6969 | pr_warn("%s: personality does not support diskops!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | mdname(mddev)); |
| 6971 | return -EINVAL; |
| 6972 | } |
| 6973 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6974 | rdev = md_import_device(dev, -1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | if (IS_ERR(rdev)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6976 | pr_warn("md: error, md_import_device() returned %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6977 | PTR_ERR(rdev)); |
| 6978 | return -EINVAL; |
| 6979 | } |
| 6980 | |
| 6981 | if (mddev->persistent) |
Jonathan Brassow | 57b2caa | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6982 | rdev->sb_start = calc_dev_sboffset(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6983 | else |
Christoph Hellwig | 0fe8034 | 2021-10-18 12:11:06 +0200 | [diff] [blame] | 6984 | rdev->sb_start = bdev_nr_sectors(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6985 | |
NeilBrown | 8190e75 | 2009-06-18 08:48:58 +1000 | [diff] [blame] | 6986 | rdev->sectors = rdev->sb_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6987 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6988 | if (test_bit(Faulty, &rdev->flags)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 6989 | 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] | 6990 | bdevname(rdev->bdev,b), mdname(mddev)); |
| 6991 | err = -EINVAL; |
| 6992 | goto abort_export; |
| 6993 | } |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 6994 | |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6995 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | rdev->desc_nr = -1; |
NeilBrown | 5842730 | 2006-10-06 00:44:04 -0700 | [diff] [blame] | 6997 | rdev->saved_raid_disk = -1; |
NeilBrown | 2bf071b | 2006-01-06 00:20:55 -0800 | [diff] [blame] | 6998 | err = bind_rdev_to_array(rdev, mddev); |
| 6999 | if (err) |
Goldwyn Rodrigues | 2aa8219 | 2015-09-28 19:21:35 -0500 | [diff] [blame] | 7000 | goto abort_export; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | |
| 7002 | /* |
| 7003 | * The rest should better be atomic, we can have disk failures |
| 7004 | * noticed in interrupt contexts ... |
| 7005 | */ |
| 7006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | rdev->raid_disk = -1; |
| 7008 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7009 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 060b068 | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 7010 | if (!mddev->thread) |
| 7011 | md_update_sb(mddev, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7012 | /* |
| 7013 | * Kick recovery, maybe this spare has to be added to the |
| 7014 | * array immediately. |
| 7015 | */ |
| 7016 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7017 | md_wakeup_thread(mddev->thread); |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 7018 | md_new_event(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7019 | return 0; |
| 7020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7021 | abort_export: |
| 7022 | export_rdev(rdev); |
| 7023 | return err; |
| 7024 | } |
| 7025 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7026 | static int set_bitmap_file(struct mddev *mddev, int fd) |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7027 | { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7028 | int err = 0; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7029 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7030 | if (mddev->pers) { |
NeilBrown | d66b1b3 | 2014-08-08 15:40:24 +1000 | [diff] [blame] | 7031 | if (!mddev->pers->quiesce || !mddev->thread) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7032 | return -EBUSY; |
| 7033 | if (mddev->recovery || mddev->sync_thread) |
| 7034 | return -EBUSY; |
| 7035 | /* we should be able to change the bitmap.. */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7036 | } |
| 7037 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7038 | if (fd >= 0) { |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7039 | struct inode *inode; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7040 | struct file *f; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7041 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7042 | if (mddev->bitmap || mddev->bitmap_info.file) |
| 7043 | return -EEXIST; /* cannot add when bitmap is present */ |
| 7044 | f = fget(fd); |
| 7045 | |
| 7046 | if (f == NULL) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7047 | pr_warn("%s: error: failed to get bitmap file\n", |
| 7048 | mdname(mddev)); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7049 | return -EBADF; |
| 7050 | } |
| 7051 | |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7052 | inode = f->f_mapping->host; |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7053 | if (!S_ISREG(inode->i_mode)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7054 | pr_warn("%s: error: bitmap file must be a regular file\n", |
| 7055 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7056 | err = -EBADF; |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7057 | } else if (!(f->f_mode & FMODE_WRITE)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7058 | pr_warn("%s: error: bitmap file must open for write\n", |
| 7059 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7060 | err = -EBADF; |
| 7061 | } else if (atomic_read(&inode->i_writecount) != 1) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7062 | pr_warn("%s: error: bitmap file is already in use\n", |
| 7063 | mdname(mddev)); |
NeilBrown | 035328c | 2014-04-09 12:25:40 +1000 | [diff] [blame] | 7064 | err = -EBUSY; |
| 7065 | } |
| 7066 | if (err) { |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7067 | fput(f); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7068 | return err; |
| 7069 | } |
NeilBrown | 1e594bb | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7070 | mddev->bitmap_info.file = f; |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7071 | mddev->bitmap_info.offset = 0; /* file overrides offset */ |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7072 | } else if (mddev->bitmap == NULL) |
| 7073 | return -ENOENT; /* cannot remove what isn't there */ |
| 7074 | err = 0; |
| 7075 | if (mddev->pers) { |
NeilBrown | 69e51b4 | 2010-06-01 19:37:35 +1000 | [diff] [blame] | 7076 | if (fd >= 0) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7077 | struct bitmap *bitmap; |
| 7078 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7079 | bitmap = md_bitmap_create(mddev, -1); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7080 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7081 | if (!IS_ERR(bitmap)) { |
| 7082 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7083 | err = md_bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 7084 | } else |
| 7085 | err = PTR_ERR(bitmap); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7086 | if (err) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7087 | md_bitmap_destroy(mddev); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7088 | fd = -1; |
| 7089 | } |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7090 | mddev_resume(mddev); |
NeilBrown | 52a0d49 | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7091 | } else if (fd < 0) { |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7092 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7093 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7094 | mddev_resume(mddev); |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 7095 | } |
NeilBrown | d7375ab | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 7096 | } |
| 7097 | if (fd < 0) { |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7098 | struct file *f = mddev->bitmap_info.file; |
| 7099 | if (f) { |
| 7100 | spin_lock(&mddev->lock); |
| 7101 | mddev->bitmap_info.file = NULL; |
| 7102 | spin_unlock(&mddev->lock); |
| 7103 | fput(f); |
| 7104 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7105 | } |
| 7106 | |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7107 | return err; |
| 7108 | } |
| 7109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7110 | /* |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7111 | * md_set_array_info is used two different ways |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | * The original usage is when creating a new array. |
| 7113 | * In this usage, raid_disks is > 0 and it together with |
| 7114 | * level, size, not_persistent,layout,chunksize determine the |
| 7115 | * shape of the array. |
| 7116 | * This will always create an array with a type-0.90.0 superblock. |
| 7117 | * The newer usage is when assembling an array. |
| 7118 | * In this case raid_disks will be 0, and the major_version field is |
| 7119 | * use to determine which style super-blocks are to be found on the devices. |
| 7120 | * The minor and patch _version numbers are also kept incase the |
| 7121 | * super_block handler wishes to interpret them. |
| 7122 | */ |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7123 | int md_set_array_info(struct mddev *mddev, struct mdu_array_info_s *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7124 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7125 | if (info->raid_disks == 0) { |
| 7126 | /* just setting version number for superblock loading */ |
| 7127 | if (info->major_version < 0 || |
Ahmed S. Darwish | 50511da | 2007-05-09 02:35:34 -0700 | [diff] [blame] | 7128 | info->major_version >= ARRAY_SIZE(super_types) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7129 | super_types[info->major_version].name == NULL) { |
| 7130 | /* maybe try to auto-load a module? */ |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7131 | pr_warn("md: superblock version %d not known\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7132 | info->major_version); |
| 7133 | return -EINVAL; |
| 7134 | } |
| 7135 | mddev->major_version = info->major_version; |
| 7136 | mddev->minor_version = info->minor_version; |
| 7137 | mddev->patch_version = info->patch_version; |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7138 | mddev->persistent = !info->not_persistent; |
NeilBrown | cbd1998 | 2009-12-30 12:08:49 +1100 | [diff] [blame] | 7139 | /* ensure mddev_put doesn't delete this now that there |
| 7140 | * is some minimal configuration. |
| 7141 | */ |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7142 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | return 0; |
| 7144 | } |
| 7145 | mddev->major_version = MD_MAJOR_VERSION; |
| 7146 | mddev->minor_version = MD_MINOR_VERSION; |
| 7147 | mddev->patch_version = MD_PATCHLEVEL_VERSION; |
Deepa Dinamani | 9ebc6ef | 2015-12-21 10:51:01 +1100 | [diff] [blame] | 7148 | mddev->ctime = ktime_get_real_seconds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7149 | |
| 7150 | mddev->level = info->level; |
NeilBrown | 17115e0 | 2006-01-16 22:14:57 -0800 | [diff] [blame] | 7151 | mddev->clevel[0] = 0; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7152 | mddev->dev_sectors = 2 * (sector_t)info->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7153 | mddev->raid_disks = info->raid_disks; |
| 7154 | /* don't set md_minor, it is determined by which /dev/md* was |
| 7155 | * openned |
| 7156 | */ |
| 7157 | if (info->state & (1<<MD_SB_CLEAN)) |
| 7158 | mddev->recovery_cp = MaxSector; |
| 7159 | else |
| 7160 | mddev->recovery_cp = 0; |
| 7161 | mddev->persistent = ! info->not_persistent; |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 7162 | mddev->external = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | |
| 7164 | mddev->layout = info->layout; |
NeilBrown | 33f2c35 | 2019-09-09 16:52:29 +1000 | [diff] [blame] | 7165 | if (mddev->level == 0) |
| 7166 | /* Cannot trust RAID0 layout info here */ |
| 7167 | mddev->layout = -1; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7168 | mddev->chunk_sectors = info->chunk_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7169 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7170 | if (mddev->persistent) { |
NeilBrown | 1b3bae4 | 2017-03-01 07:31:28 +1100 | [diff] [blame] | 7171 | mddev->max_disks = MD_SB_DISKS; |
| 7172 | mddev->flags = 0; |
| 7173 | mddev->sb_flags = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7174 | } |
| 7175 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7176 | |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7177 | mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 7178 | mddev->bitmap_info.default_space = 64*2 - (MD_SB_BYTES >> 9); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7179 | mddev->bitmap_info.offset = 0; |
NeilBrown | b2a2703 | 2005-11-28 13:44:12 -0800 | [diff] [blame] | 7180 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7181 | mddev->reshape_position = MaxSector; |
| 7182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7183 | /* |
| 7184 | * Generate a 128 bit UUID |
| 7185 | */ |
| 7186 | get_random_bytes(mddev->uuid, 16); |
| 7187 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7188 | mddev->new_level = mddev->level; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7189 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7190 | mddev->new_layout = mddev->layout; |
| 7191 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7192 | mddev->reshape_backwards = 0; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7194 | return 0; |
| 7195 | } |
| 7196 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7197 | void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors) |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 7198 | { |
Shaohua Li | efa4b77 | 2017-10-18 22:08:13 -0700 | [diff] [blame] | 7199 | lockdep_assert_held(&mddev->reconfig_mutex); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 7200 | |
| 7201 | if (mddev->external_size) |
| 7202 | return; |
| 7203 | |
Dan Williams | 1f40362 | 2009-03-31 14:59:03 +1100 | [diff] [blame] | 7204 | mddev->array_sectors = array_sectors; |
| 7205 | } |
| 7206 | EXPORT_SYMBOL(md_set_array_sectors); |
| 7207 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7208 | static int update_size(struct mddev *mddev, sector_t num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7209 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7210 | struct md_rdev *rdev; |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7211 | int rv; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7212 | int fit = (num_sectors == 0); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 7213 | sector_t old_dev_sectors = mddev->dev_sectors; |
Guoqing Jiang | ab5a98b | 2016-05-02 11:33:13 -0400 | [diff] [blame] | 7214 | |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7215 | if (mddev->pers->resize == NULL) |
| 7216 | return -EINVAL; |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7217 | /* The "num_sectors" is the number of sectors of each device that |
| 7218 | * is used. This can only make sense for arrays with redundancy. |
| 7219 | * linear and raid0 always use whatever space is available. We can only |
| 7220 | * consider changing this number if no resync or reconstruction is |
| 7221 | * 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] | 7222 | * 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] | 7223 | * of each device. If num_sectors is zero, we find the largest size |
| 7224 | * that fits. |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7225 | */ |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7226 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 7227 | mddev->sync_thread) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7228 | return -EBUSY; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 7229 | if (mddev->ro) |
| 7230 | return -EROFS; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7231 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7232 | rdev_for_each(rdev, mddev) { |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7233 | sector_t avail = rdev->sectors; |
NeilBrown | 01ab566 | 2006-10-28 10:38:30 -0700 | [diff] [blame] | 7234 | |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7235 | if (fit && (num_sectors == 0 || num_sectors > avail)) |
| 7236 | num_sectors = avail; |
| 7237 | if (avail < num_sectors) |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7238 | return -ENOSPC; |
| 7239 | } |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7240 | rv = mddev->pers->resize(mddev, num_sectors); |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 7241 | if (!rv) { |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 7242 | if (mddev_is_clustered(mddev)) |
| 7243 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
| 7244 | else if (mddev->queue) { |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 7245 | set_capacity_and_notify(mddev->gendisk, |
| 7246 | mddev->array_sectors); |
Guoqing Jiang | c948363 | 2017-02-24 11:15:23 +0800 | [diff] [blame] | 7247 | } |
| 7248 | } |
NeilBrown | a35b0d6 | 2006-01-06 00:20:49 -0800 | [diff] [blame] | 7249 | return rv; |
| 7250 | } |
| 7251 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7252 | static int update_raid_disks(struct mddev *mddev, int raid_disks) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7253 | { |
| 7254 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7255 | struct md_rdev *rdev; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7256 | /* change the number of raid disks */ |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7257 | if (mddev->pers->check_reshape == NULL) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7258 | return -EINVAL; |
NeilBrown | bd8839e | 2014-05-28 13:39:21 +1000 | [diff] [blame] | 7259 | if (mddev->ro) |
| 7260 | return -EROFS; |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7261 | if (raid_disks <= 0 || |
NeilBrown | 233fca3 | 2010-04-14 17:02:09 +1000 | [diff] [blame] | 7262 | (mddev->max_disks && raid_disks >= mddev->max_disks)) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7263 | return -EINVAL; |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7264 | if (mddev->sync_thread || |
| 7265 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
Zhao Heming | a8da01f | 2020-11-19 19:41:33 +0800 | [diff] [blame] | 7266 | test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) || |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 7267 | mddev->reshape_position != MaxSector) |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7268 | return -EBUSY; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7269 | |
| 7270 | rdev_for_each(rdev, mddev) { |
| 7271 | if (mddev->raid_disks < raid_disks && |
| 7272 | rdev->data_offset < rdev->new_data_offset) |
| 7273 | return -EINVAL; |
| 7274 | if (mddev->raid_disks > raid_disks && |
| 7275 | rdev->data_offset > rdev->new_data_offset) |
| 7276 | return -EINVAL; |
| 7277 | } |
| 7278 | |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7279 | mddev->delta_disks = raid_disks - mddev->raid_disks; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7280 | if (mddev->delta_disks < 0) |
| 7281 | mddev->reshape_backwards = 1; |
| 7282 | else if (mddev->delta_disks > 0) |
| 7283 | mddev->reshape_backwards = 0; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7284 | |
| 7285 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7286 | if (rv < 0) { |
NeilBrown | de171cb | 2011-01-31 11:57:42 +1100 | [diff] [blame] | 7287 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7288 | mddev->reshape_backwards = 0; |
| 7289 | } |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7290 | return rv; |
| 7291 | } |
| 7292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7293 | /* |
| 7294 | * update_array_info is used to change the configuration of an |
| 7295 | * on-line array. |
| 7296 | * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size |
| 7297 | * fields in the info are checked against the array. |
| 7298 | * Any differences that cannot be handled will cause an error. |
| 7299 | * Normally, only one change can be managed at a time. |
| 7300 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7301 | 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] | 7302 | { |
| 7303 | int rv = 0; |
| 7304 | int cnt = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7305 | int state = 0; |
| 7306 | |
| 7307 | /* calculate expected state,ignoring low bits */ |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7308 | if (mddev->bitmap && mddev->bitmap_info.offset) |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7309 | state |= (1 << MD_SB_BITMAP_PRESENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7310 | |
| 7311 | if (mddev->major_version != info->major_version || |
| 7312 | mddev->minor_version != info->minor_version || |
| 7313 | /* mddev->patch_version != info->patch_version || */ |
| 7314 | mddev->ctime != info->ctime || |
| 7315 | mddev->level != info->level || |
| 7316 | /* mddev->layout != info->layout || */ |
Firo Yang | 4e02361 | 2015-06-11 09:41:10 +0800 | [diff] [blame] | 7317 | mddev->persistent != !info->not_persistent || |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7318 | mddev->chunk_sectors != info->chunk_size >> 9 || |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7319 | /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */ |
| 7320 | ((state^info->state) & 0xfffffe00) |
| 7321 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7322 | return -EINVAL; |
| 7323 | /* Check there is only one change */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7324 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
| 7325 | cnt++; |
| 7326 | if (mddev->raid_disks != info->raid_disks) |
| 7327 | cnt++; |
| 7328 | if (mddev->layout != info->layout) |
| 7329 | cnt++; |
| 7330 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) |
| 7331 | cnt++; |
| 7332 | if (cnt == 0) |
| 7333 | return 0; |
| 7334 | if (cnt > 1) |
| 7335 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7336 | |
| 7337 | if (mddev->layout != info->layout) { |
| 7338 | /* Change layout |
| 7339 | * we don't need to do anything at the md level, the |
| 7340 | * personality will take care of it all. |
| 7341 | */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7342 | if (mddev->pers->check_reshape == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7344 | else { |
| 7345 | mddev->new_layout = info->layout; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7346 | rv = mddev->pers->check_reshape(mddev); |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7347 | if (rv) |
| 7348 | mddev->new_layout = mddev->layout; |
| 7349 | return rv; |
| 7350 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7351 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7352 | if (info->size >= 0 && mddev->dev_sectors / 2 != info->size) |
Andre Noll | d71f9f8 | 2008-07-11 22:02:22 +1000 | [diff] [blame] | 7353 | rv = update_size(mddev, (sector_t)info->size * 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7354 | |
NeilBrown | da943b99 | 2006-01-06 00:20:54 -0800 | [diff] [blame] | 7355 | if (mddev->raid_disks != info->raid_disks) |
| 7356 | rv = update_raid_disks(mddev, info->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7357 | |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7358 | if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7359 | if (mddev->pers->quiesce == NULL || mddev->thread == NULL) { |
| 7360 | rv = -EINVAL; |
| 7361 | goto err; |
| 7362 | } |
| 7363 | if (mddev->recovery || mddev->sync_thread) { |
| 7364 | rv = -EBUSY; |
| 7365 | goto err; |
| 7366 | } |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7367 | if (info->state & (1<<MD_SB_BITMAP_PRESENT)) { |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7368 | struct bitmap *bitmap; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7369 | /* add the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7370 | if (mddev->bitmap) { |
| 7371 | rv = -EEXIST; |
| 7372 | goto err; |
| 7373 | } |
| 7374 | if (mddev->bitmap_info.default_offset == 0) { |
| 7375 | rv = -EINVAL; |
| 7376 | goto err; |
| 7377 | } |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7378 | mddev->bitmap_info.offset = |
| 7379 | mddev->bitmap_info.default_offset; |
NeilBrown | 6409bb0 | 2012-05-22 13:55:07 +1000 | [diff] [blame] | 7380 | mddev->bitmap_info.space = |
| 7381 | mddev->bitmap_info.default_space; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7382 | bitmap = md_bitmap_create(mddev, -1); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7383 | mddev_suspend(mddev); |
Goldwyn Rodrigues | f9209a3 | 2014-06-06 12:43:49 -0500 | [diff] [blame] | 7384 | if (!IS_ERR(bitmap)) { |
| 7385 | mddev->bitmap = bitmap; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7386 | rv = md_bitmap_load(mddev); |
NeilBrown | ba599ac | 2015-02-25 11:44:11 +1100 | [diff] [blame] | 7387 | } else |
| 7388 | rv = PTR_ERR(bitmap); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7389 | if (rv) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7390 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7391 | mddev_resume(mddev); |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7392 | } else { |
| 7393 | /* remove the bitmap */ |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7394 | if (!mddev->bitmap) { |
| 7395 | rv = -ENOENT; |
| 7396 | goto err; |
| 7397 | } |
| 7398 | if (mddev->bitmap->storage.file) { |
| 7399 | rv = -EINVAL; |
| 7400 | goto err; |
| 7401 | } |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7402 | if (mddev->bitmap_info.nodes) { |
| 7403 | /* hold PW on all the bitmap lock */ |
| 7404 | if (md_cluster_ops->lock_all_bitmaps(mddev) <= 0) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7405 | 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] | 7406 | rv = -EPERM; |
| 7407 | md_cluster_ops->unlock_all_bitmaps(mddev); |
| 7408 | goto err; |
| 7409 | } |
| 7410 | |
| 7411 | mddev->bitmap_info.nodes = 0; |
| 7412 | md_cluster_ops->leave(mddev); |
Zhao Heming | edee9df | 2020-07-21 02:08:53 +0800 | [diff] [blame] | 7413 | module_put(md_cluster_mod); |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 7414 | mddev->safemode_delay = DEFAULT_SAFEMODE_DELAY; |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 7415 | } |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7416 | mddev_suspend(mddev); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 7417 | md_bitmap_destroy(mddev); |
NeilBrown | 9e1cc0a | 2017-10-17 13:46:43 +1100 | [diff] [blame] | 7418 | mddev_resume(mddev); |
NeilBrown | c3d9714 | 2009-12-14 12:49:52 +1100 | [diff] [blame] | 7419 | mddev->bitmap_info.offset = 0; |
NeilBrown | 36fa306 | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 7420 | } |
| 7421 | } |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7422 | md_update_sb(mddev, 1); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 7423 | return rv; |
| 7424 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7425 | return rv; |
| 7426 | } |
| 7427 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7428 | static int set_disk_faulty(struct mddev *mddev, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7429 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7430 | struct md_rdev *rdev; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7431 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7432 | |
| 7433 | if (mddev->pers == NULL) |
| 7434 | return -ENODEV; |
| 7435 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7436 | rcu_read_lock(); |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 7437 | rdev = md_find_rdev_rcu(mddev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7438 | if (!rdev) |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7439 | err = -ENODEV; |
| 7440 | else { |
| 7441 | md_error(mddev, rdev); |
| 7442 | if (!test_bit(Faulty, &rdev->flags)) |
| 7443 | err = -EBUSY; |
| 7444 | } |
| 7445 | rcu_read_unlock(); |
| 7446 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7447 | } |
| 7448 | |
Andre Noll | 2f9618c | 2008-04-25 18:57:58 +0200 | [diff] [blame] | 7449 | /* |
| 7450 | * We have a problem here : there is no easy way to give a CHS |
| 7451 | * virtual geometry. We currently pretend that we have a 2 heads |
| 7452 | * 4 sectors (with a BIG number of cylinders...). This drives |
| 7453 | * dosfs just mad... ;-) |
| 7454 | */ |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7455 | static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 7456 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7457 | struct mddev *mddev = bdev->bd_disk->private_data; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7458 | |
| 7459 | geo->heads = 2; |
| 7460 | geo->sectors = 4; |
NeilBrown | 49ce6ce | 2010-03-29 10:51:42 +1100 | [diff] [blame] | 7461 | geo->cylinders = mddev->array_sectors / 8; |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7462 | return 0; |
| 7463 | } |
| 7464 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7465 | static inline bool md_ioctl_valid(unsigned int cmd) |
| 7466 | { |
| 7467 | switch (cmd) { |
| 7468 | case ADD_NEW_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7469 | case GET_ARRAY_INFO: |
| 7470 | case GET_BITMAP_FILE: |
| 7471 | case GET_DISK_INFO: |
| 7472 | case HOT_ADD_DISK: |
| 7473 | case HOT_REMOVE_DISK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7474 | case RAID_VERSION: |
| 7475 | case RESTART_ARRAY_RW: |
| 7476 | case RUN_ARRAY: |
| 7477 | case SET_ARRAY_INFO: |
| 7478 | case SET_BITMAP_FILE: |
| 7479 | case SET_DISK_FAULTY: |
| 7480 | case STOP_ARRAY: |
| 7481 | case STOP_ARRAY_RO: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7482 | case CLUSTERED_DISK_NACK: |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7483 | return true; |
| 7484 | default: |
| 7485 | return false; |
| 7486 | } |
| 7487 | } |
| 7488 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7489 | static int md_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7490 | unsigned int cmd, unsigned long arg) |
| 7491 | { |
| 7492 | int err = 0; |
| 7493 | void __user *argp = (void __user *)arg; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7494 | struct mddev *mddev = NULL; |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7495 | bool did_set_md_closing = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7496 | |
Nicolas Schichan | cb335f8 | 2014-01-15 16:58:52 +0100 | [diff] [blame] | 7497 | if (!md_ioctl_valid(cmd)) |
| 7498 | return -ENOTTY; |
| 7499 | |
NeilBrown | 506c9e4 | 2011-12-23 10:17:26 +1100 | [diff] [blame] | 7500 | switch (cmd) { |
| 7501 | case RAID_VERSION: |
| 7502 | case GET_ARRAY_INFO: |
| 7503 | case GET_DISK_INFO: |
| 7504 | break; |
| 7505 | default: |
| 7506 | if (!capable(CAP_SYS_ADMIN)) |
| 7507 | return -EACCES; |
| 7508 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7509 | |
| 7510 | /* |
| 7511 | * Commands dealing with the RAID driver but not any |
| 7512 | * particular array: |
| 7513 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7514 | switch (cmd) { |
| 7515 | case RAID_VERSION: |
| 7516 | err = get_version(argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7517 | goto out; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7518 | default:; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7519 | } |
| 7520 | |
| 7521 | /* |
| 7522 | * Commands creating/starting a new array: |
| 7523 | */ |
| 7524 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7525 | mddev = bdev->bd_disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7526 | |
| 7527 | if (!mddev) { |
| 7528 | BUG(); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7529 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7530 | } |
| 7531 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7532 | /* Some actions do not requires the mutex */ |
| 7533 | switch (cmd) { |
| 7534 | case GET_ARRAY_INFO: |
| 7535 | if (!mddev->raid_disks && !mddev->external) |
| 7536 | err = -ENODEV; |
| 7537 | else |
| 7538 | err = get_array_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7539 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7540 | |
| 7541 | case GET_DISK_INFO: |
| 7542 | if (!mddev->raid_disks && !mddev->external) |
| 7543 | err = -ENODEV; |
| 7544 | else |
| 7545 | err = get_disk_info(mddev, argp); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7546 | goto out; |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7547 | |
| 7548 | case SET_DISK_FAULTY: |
| 7549 | err = set_disk_faulty(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7550 | goto out; |
NeilBrown | 4af1a04 | 2014-12-15 12:57:00 +1100 | [diff] [blame] | 7551 | |
| 7552 | case GET_BITMAP_FILE: |
| 7553 | err = get_bitmap_file(mddev, argp); |
| 7554 | goto out; |
| 7555 | |
NeilBrown | 1ca69c4 | 2012-10-11 13:37:33 +1100 | [diff] [blame] | 7556 | } |
| 7557 | |
Guoqing Jiang | 78b990c | 2020-04-04 23:57:10 +0200 | [diff] [blame] | 7558 | if (cmd == ADD_NEW_DISK || cmd == HOT_ADD_DISK) |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 7559 | flush_rdev_wq(mddev); |
NeilBrown | a7a3f08 | 2012-12-11 13:35:54 +1100 | [diff] [blame] | 7560 | |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7561 | if (cmd == HOT_REMOVE_DISK) |
| 7562 | /* need to ensure recovery thread has run */ |
| 7563 | wait_event_interruptible_timeout(mddev->sb_wait, |
| 7564 | !test_bit(MD_RECOVERY_NEEDED, |
Shaohua Li | 82a301c | 2016-12-08 15:48:18 -0800 | [diff] [blame] | 7565 | &mddev->recovery), |
Hannes Reinecke | 90f5f7a | 2013-04-02 08:38:55 +0200 | [diff] [blame] | 7566 | msecs_to_jiffies(5000)); |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7567 | if (cmd == STOP_ARRAY || cmd == STOP_ARRAY_RO) { |
| 7568 | /* Need to flush page cache, and ensure no-one else opens |
| 7569 | * and writes |
| 7570 | */ |
| 7571 | mutex_lock(&mddev->open_mutex); |
NeilBrown | 9ba3b7f | 2014-09-09 14:00:15 +1000 | [diff] [blame] | 7572 | if (mddev->pers && atomic_read(&mddev->openers) > 1) { |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7573 | mutex_unlock(&mddev->open_mutex); |
| 7574 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7575 | goto out; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7576 | } |
Dae R. Jeong | c731b84 | 2020-10-22 10:21:28 +0900 | [diff] [blame] | 7577 | if (test_and_set_bit(MD_CLOSING, &mddev->flags)) { |
| 7578 | mutex_unlock(&mddev->open_mutex); |
| 7579 | err = -EBUSY; |
| 7580 | goto out; |
| 7581 | } |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7582 | did_set_md_closing = true; |
NeilBrown | 260fa03 | 2013-08-27 16:44:13 +1000 | [diff] [blame] | 7583 | mutex_unlock(&mddev->open_mutex); |
| 7584 | sync_blockdev(bdev); |
| 7585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7586 | err = mddev_lock(mddev); |
| 7587 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7588 | pr_debug("md: ioctl lock interrupted, reason %d, cmd %d\n", |
| 7589 | err, cmd); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7590 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7591 | } |
| 7592 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7593 | if (cmd == SET_ARRAY_INFO) { |
| 7594 | mdu_array_info_t info; |
| 7595 | if (!arg) |
| 7596 | memset(&info, 0, sizeof(info)); |
| 7597 | else if (copy_from_user(&info, argp, sizeof(info))) { |
| 7598 | err = -EFAULT; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7599 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7600 | } |
| 7601 | if (mddev->pers) { |
| 7602 | err = update_array_info(mddev, &info); |
| 7603 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7604 | pr_warn("md: couldn't update array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7605 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7606 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7607 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7608 | } |
| 7609 | if (!list_empty(&mddev->disks)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7610 | pr_warn("md: array %s already has disks!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7611 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7612 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7613 | } |
| 7614 | if (mddev->raid_disks) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7615 | pr_warn("md: array %s already initialised!\n", mdname(mddev)); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7616 | err = -EBUSY; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7617 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7618 | } |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7619 | err = md_set_array_info(mddev, &info); |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7620 | if (err) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 7621 | pr_warn("md: couldn't set array info. %d\n", err); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7622 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7623 | } |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7624 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7625 | } |
| 7626 | |
| 7627 | /* |
| 7628 | * Commands querying/configuring an existing array: |
| 7629 | */ |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7630 | /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY, |
NeilBrown | 3f9d7b0 | 2006-12-22 01:11:41 -0800 | [diff] [blame] | 7631 | * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */ |
NeilBrown | a17184a | 2008-02-06 01:39:55 -0800 | [diff] [blame] | 7632 | if ((!mddev->raid_disks && !mddev->external) |
| 7633 | && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY |
| 7634 | && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE |
| 7635 | && cmd != GET_BITMAP_FILE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7636 | err = -ENODEV; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7637 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7638 | } |
| 7639 | |
| 7640 | /* |
| 7641 | * Commands even a read-only array can execute: |
| 7642 | */ |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7643 | switch (cmd) { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7644 | case RESTART_ARRAY_RW: |
| 7645 | err = restart_array(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7646 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7647 | |
| 7648 | case STOP_ARRAY: |
| 7649 | err = do_md_stop(mddev, 0, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7650 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7651 | |
| 7652 | case STOP_ARRAY_RO: |
| 7653 | err = md_set_readonly(mddev, bdev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7654 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7655 | |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7656 | case HOT_REMOVE_DISK: |
| 7657 | err = hot_remove_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7658 | goto unlock; |
NeilBrown | 3ea8929d | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 7659 | |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7660 | case ADD_NEW_DISK: |
| 7661 | /* We can support ADD_NEW_DISK on read-only arrays |
Wei Fang | 466ad29 | 2016-03-21 19:19:30 +0800 | [diff] [blame] | 7662 | * only if we are re-adding a preexisting device. |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7663 | * So require mddev->pers and MD_DISK_SYNC. |
| 7664 | */ |
| 7665 | if (mddev->pers) { |
| 7666 | mdu_disk_info_t info; |
| 7667 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7668 | err = -EFAULT; |
| 7669 | else if (!(info.state & (1<<MD_DISK_SYNC))) |
| 7670 | /* Need to clear read-only for this */ |
| 7671 | break; |
| 7672 | else |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7673 | err = md_add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7674 | goto unlock; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 7675 | } |
| 7676 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7677 | } |
| 7678 | |
| 7679 | /* |
| 7680 | * The remaining ioctls are changing the state of the |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7681 | * superblock, so we do not allow them on read-only arrays. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7682 | */ |
NeilBrown | 326eb17 | 2014-09-30 15:36:28 +1000 | [diff] [blame] | 7683 | if (mddev->ro && mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7684 | if (mddev->ro == 2) { |
| 7685 | mddev->ro = 0; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7686 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 7687 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7688 | /* mddev_unlock will wake thread */ |
| 7689 | /* If a device failed while we were read-only, we |
| 7690 | * need to make sure the metadata is updated now. |
| 7691 | */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7692 | if (test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)) { |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7693 | mddev_unlock(mddev); |
| 7694 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7695 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) && |
| 7696 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 29f097c | 2013-11-14 17:54:51 +1100 | [diff] [blame] | 7697 | mddev_lock_nointr(mddev); |
NeilBrown | f3378b4 | 2013-02-28 11:59:03 +1100 | [diff] [blame] | 7698 | } |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7699 | } else { |
| 7700 | err = -EROFS; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7701 | goto unlock; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 7702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7703 | } |
| 7704 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7705 | switch (cmd) { |
| 7706 | case ADD_NEW_DISK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7707 | { |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7708 | mdu_disk_info_t info; |
| 7709 | if (copy_from_user(&info, argp, sizeof(info))) |
| 7710 | err = -EFAULT; |
| 7711 | else |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7712 | err = md_add_new_disk(mddev, &info); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7713 | goto unlock; |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7715 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 7716 | case CLUSTERED_DISK_NACK: |
| 7717 | if (mddev_is_clustered(mddev)) |
| 7718 | md_cluster_ops->new_disk_ack(mddev, false); |
| 7719 | else |
| 7720 | err = -EINVAL; |
| 7721 | goto unlock; |
| 7722 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7723 | case HOT_ADD_DISK: |
| 7724 | err = hot_add_disk(mddev, new_decode_dev(arg)); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7725 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7726 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7727 | case RUN_ARRAY: |
| 7728 | err = do_md_run(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7729 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7730 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7731 | case SET_BITMAP_FILE: |
| 7732 | err = set_bitmap_file(mddev, (int)arg); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7733 | goto unlock; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7734 | |
NeilBrown | c02c0ae | 2012-12-11 13:39:21 +1100 | [diff] [blame] | 7735 | default: |
| 7736 | err = -EINVAL; |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7737 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7738 | } |
| 7739 | |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7740 | unlock: |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7741 | if (mddev->hold_active == UNTIL_IOCTL && |
| 7742 | err != -EINVAL) |
| 7743 | mddev->hold_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7744 | mddev_unlock(mddev); |
NeilBrown | 3adc28d | 2014-09-30 15:46:41 +1000 | [diff] [blame] | 7745 | out: |
NeilBrown | 065e519 | 2017-04-06 11:16:33 +0800 | [diff] [blame] | 7746 | if(did_set_md_closing) |
| 7747 | clear_bit(MD_CLOSING, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7748 | return err; |
| 7749 | } |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7750 | #ifdef CONFIG_COMPAT |
| 7751 | static int md_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 7752 | unsigned int cmd, unsigned long arg) |
| 7753 | { |
| 7754 | switch (cmd) { |
| 7755 | case HOT_REMOVE_DISK: |
| 7756 | case HOT_ADD_DISK: |
| 7757 | case SET_DISK_FAULTY: |
| 7758 | case SET_BITMAP_FILE: |
| 7759 | /* These take in integer arg, do not convert */ |
| 7760 | break; |
| 7761 | default: |
| 7762 | arg = (unsigned long)compat_ptr(arg); |
| 7763 | break; |
| 7764 | } |
| 7765 | |
| 7766 | return md_ioctl(bdev, mode, cmd, arg); |
| 7767 | } |
| 7768 | #endif /* CONFIG_COMPAT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7769 | |
Christoph Hellwig | 118cf08 | 2020-11-03 11:00:13 +0100 | [diff] [blame] | 7770 | static int md_set_read_only(struct block_device *bdev, bool ro) |
| 7771 | { |
| 7772 | struct mddev *mddev = bdev->bd_disk->private_data; |
| 7773 | int err; |
| 7774 | |
| 7775 | err = mddev_lock(mddev); |
| 7776 | if (err) |
| 7777 | return err; |
| 7778 | |
| 7779 | if (!mddev->raid_disks && !mddev->external) { |
| 7780 | err = -ENODEV; |
| 7781 | goto out_unlock; |
| 7782 | } |
| 7783 | |
| 7784 | /* |
| 7785 | * Transitioning to read-auto need only happen for arrays that call |
| 7786 | * md_write_start and which are not ready for writes yet. |
| 7787 | */ |
| 7788 | if (!ro && mddev->ro == 1 && mddev->pers) { |
| 7789 | err = restart_array(mddev); |
| 7790 | if (err) |
| 7791 | goto out_unlock; |
| 7792 | mddev->ro = 2; |
| 7793 | } |
| 7794 | |
| 7795 | out_unlock: |
| 7796 | mddev_unlock(mddev); |
| 7797 | return err; |
| 7798 | } |
| 7799 | |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7800 | static int md_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7801 | { |
| 7802 | /* |
| 7803 | * Succeed if we can lock the mddev, which confirms that |
| 7804 | * it isn't being stopped right now. |
| 7805 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7806 | struct mddev *mddev = mddev_find(bdev->bd_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7807 | int err; |
| 7808 | |
Yuanhan Liu | 0c09822 | 2012-05-22 13:55:32 +1000 | [diff] [blame] | 7809 | if (!mddev) |
| 7810 | return -ENODEV; |
| 7811 | |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7812 | if (mddev->gendisk != bdev->bd_disk) { |
| 7813 | /* we are racing with mddev_put which is discarding this |
| 7814 | * bd_disk. |
| 7815 | */ |
| 7816 | mddev_put(mddev); |
| 7817 | /* Wait until bdev->bd_disk is definitely gone */ |
Guoqing Jiang | f6766ff | 2020-04-04 23:57:09 +0200 | [diff] [blame] | 7818 | if (work_pending(&mddev->del_work)) |
| 7819 | flush_workqueue(md_misc_wq); |
Zhao Heming | 6a4db2a | 2021-04-03 11:01:25 +0800 | [diff] [blame] | 7820 | return -EBUSY; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 7821 | } |
| 7822 | BUG_ON(mddev != bdev->bd_disk->private_data); |
| 7823 | |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7824 | if ((err = mutex_lock_interruptible(&mddev->open_mutex))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7825 | goto out; |
| 7826 | |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7827 | if (test_bit(MD_CLOSING, &mddev->flags)) { |
| 7828 | mutex_unlock(&mddev->open_mutex); |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7829 | err = -ENODEV; |
| 7830 | goto out; |
Guoqing Jiang | af8d8e6 | 2016-08-12 13:42:37 +0800 | [diff] [blame] | 7831 | } |
| 7832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7833 | err = 0; |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7834 | atomic_inc(&mddev->openers); |
NeilBrown | c8c00a6 | 2009-08-10 12:50:52 +1000 | [diff] [blame] | 7835 | mutex_unlock(&mddev->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7836 | |
Christoph Hellwig | 818077d | 2020-09-08 16:53:43 +0200 | [diff] [blame] | 7837 | bdev_check_media_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7838 | out: |
NeilBrown | e2342ca | 2016-12-05 16:40:50 +1100 | [diff] [blame] | 7839 | if (err) |
| 7840 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7841 | return err; |
| 7842 | } |
| 7843 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 7844 | static void md_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7845 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7846 | struct mddev *mddev = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7847 | |
Eric Sesterhenn | 52e5f9d | 2006-10-03 23:33:23 +0200 | [diff] [blame] | 7848 | BUG_ON(!mddev); |
NeilBrown | f2ea68c | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 7849 | atomic_dec(&mddev->openers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7850 | mddev_put(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7851 | } |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7852 | |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7853 | static unsigned int md_check_events(struct gendisk *disk, unsigned int clearing) |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7854 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7855 | struct mddev *mddev = disk->private_data; |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7856 | unsigned int ret = 0; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7857 | |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7858 | if (mddev->changed) |
| 7859 | ret = DISK_EVENT_MEDIA_CHANGE; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7860 | mddev->changed = 0; |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7861 | return ret; |
NeilBrown | f0b4f7e | 2011-02-24 17:26:41 +1100 | [diff] [blame] | 7862 | } |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7863 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 7864 | const struct block_device_operations md_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7865 | { |
| 7866 | .owner = THIS_MODULE, |
Christoph Hellwig | c62b37d | 2020-07-01 10:59:43 +0200 | [diff] [blame] | 7867 | .submit_bio = md_submit_bio, |
Al Viro | a39907f | 2008-03-02 10:31:15 -0500 | [diff] [blame] | 7868 | .open = md_open, |
| 7869 | .release = md_release, |
NeilBrown | b492b85 | 2009-05-26 12:57:36 +1000 | [diff] [blame] | 7870 | .ioctl = md_ioctl, |
Arnd Bergmann | aa98aa3 | 2009-12-14 12:50:05 +1100 | [diff] [blame] | 7871 | #ifdef CONFIG_COMPAT |
| 7872 | .compat_ioctl = md_compat_ioctl, |
| 7873 | #endif |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 7874 | .getgeo = md_getgeo, |
Christoph Hellwig | a564e23 | 2020-07-08 14:25:41 +0200 | [diff] [blame] | 7875 | .check_events = md_check_events, |
Christoph Hellwig | 118cf08 | 2020-11-03 11:00:13 +0100 | [diff] [blame] | 7876 | .set_read_only = md_set_read_only, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7877 | }; |
| 7878 | |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7879 | static int md_thread(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7880 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7881 | struct md_thread *thread = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7883 | /* |
| 7884 | * md_thread is a 'system-thread', it's priority should be very |
| 7885 | * high. We avoid resource deadlocks individually in each |
| 7886 | * raid personality. (RAID5 does preallocation) We also use RR and |
| 7887 | * the very same RT priority as kswapd, thus we will never get |
| 7888 | * into a priority inversion deadlock. |
| 7889 | * |
| 7890 | * we definitely have to have equal or higher priority than |
| 7891 | * bdflush, otherwise bdflush will deadlock if there are too |
| 7892 | * many dirty RAID5 blocks. |
| 7893 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7894 | |
NeilBrown | 6985c43 | 2005-10-19 21:23:47 -0700 | [diff] [blame] | 7895 | allow_signal(SIGKILL); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7896 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7897 | |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7898 | /* We need to wait INTERRUPTIBLE so that |
| 7899 | * we don't add to the load-average. |
| 7900 | * That means we need to be sure no signals are |
| 7901 | * pending |
| 7902 | */ |
| 7903 | if (signal_pending(current)) |
| 7904 | flush_signals(current); |
| 7905 | |
| 7906 | wait_event_interruptible_timeout |
| 7907 | (thread->wqueue, |
| 7908 | test_bit(THREAD_WAKEUP, &thread->flags) |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7909 | || kthread_should_stop() || kthread_should_park(), |
NeilBrown | 93588e2 | 2005-11-15 00:09:12 -0800 | [diff] [blame] | 7910 | thread->timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7911 | |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7912 | clear_bit(THREAD_WAKEUP, &thread->flags); |
Shaohua Li | ce1ccd0 | 2016-11-21 10:29:18 -0800 | [diff] [blame] | 7913 | if (kthread_should_park()) |
| 7914 | kthread_parkme(); |
NeilBrown | 6c98791 | 2011-01-14 09:13:53 +1100 | [diff] [blame] | 7915 | if (!kthread_should_stop()) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7916 | thread->run(thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7917 | } |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7919 | return 0; |
| 7920 | } |
| 7921 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7922 | void md_wakeup_thread(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7923 | { |
| 7924 | if (thread) { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7925 | pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm); |
Guoqing Jiang | d1d9014 | 2017-10-09 10:32:48 +0800 | [diff] [blame] | 7926 | set_bit(THREAD_WAKEUP, &thread->flags); |
| 7927 | wake_up(&thread->wqueue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7928 | } |
| 7929 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7930 | EXPORT_SYMBOL(md_wakeup_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7931 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 7932 | struct md_thread *md_register_thread(void (*run) (struct md_thread *), |
| 7933 | struct mddev *mddev, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7934 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7935 | struct md_thread *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7936 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7937 | thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7938 | if (!thread) |
| 7939 | return NULL; |
| 7940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7941 | init_waitqueue_head(&thread->wqueue); |
| 7942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7943 | thread->run = run; |
| 7944 | thread->mddev = mddev; |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 7945 | thread->timeout = MAX_SCHEDULE_TIMEOUT; |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7946 | thread->tsk = kthread_run(md_thread, thread, |
| 7947 | "%s_%s", |
| 7948 | mdname(thread->mddev), |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7949 | name); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7950 | if (IS_ERR(thread->tsk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7951 | kfree(thread); |
| 7952 | return NULL; |
| 7953 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7954 | return thread; |
| 7955 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7956 | EXPORT_SYMBOL(md_register_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7957 | |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7958 | void md_unregister_thread(struct md_thread **threadp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7959 | { |
NeilBrown | 2b8bf34 | 2011-10-11 16:48:23 +1100 | [diff] [blame] | 7960 | struct md_thread *thread = *threadp; |
NeilBrown | e0cf8f0 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7961 | if (!thread) |
| 7962 | return; |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 7963 | pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk)); |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7964 | /* Locking ensures that mddev_unlock does not wake_up a |
| 7965 | * non-existent thread |
| 7966 | */ |
| 7967 | spin_lock(&pers_lock); |
| 7968 | *threadp = NULL; |
| 7969 | spin_unlock(&pers_lock); |
NeilBrown | a6fb093 | 2005-09-09 16:23:56 -0700 | [diff] [blame] | 7970 | |
| 7971 | kthread_stop(thread->tsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7972 | kfree(thread); |
| 7973 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7974 | EXPORT_SYMBOL(md_unregister_thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7975 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7976 | void md_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7977 | { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7978 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7979 | return; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 7980 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 7981 | if (!mddev->pers || !mddev->pers->error_handler) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7982 | return; |
| 7983 | mddev->pers->error_handler(mddev,rdev); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 7984 | if (mddev->degraded) |
| 7985 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7986 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7987 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 7988 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7989 | md_wakeup_thread(mddev->thread); |
NeilBrown | 768a418 | 2010-07-26 11:49:55 +1000 | [diff] [blame] | 7990 | if (mddev->event_work.func) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 7991 | queue_work(md_misc_wq, &mddev->event_work); |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 7992 | md_new_event(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7993 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 7994 | EXPORT_SYMBOL(md_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7995 | |
| 7996 | /* seq_file implementation /proc/mdstat */ |
| 7997 | |
| 7998 | static void status_unused(struct seq_file *seq) |
| 7999 | { |
| 8000 | int i = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8001 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8002 | |
| 8003 | seq_printf(seq, "unused devices: "); |
| 8004 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 8005 | list_for_each_entry(rdev, &pending_raid_disks, same_set) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8006 | char b[BDEVNAME_SIZE]; |
| 8007 | i++; |
| 8008 | seq_printf(seq, "%s ", |
| 8009 | bdevname(rdev->bdev,b)); |
| 8010 | } |
| 8011 | if (!i) |
| 8012 | seq_printf(seq, "<none>"); |
| 8013 | |
| 8014 | seq_printf(seq, "\n"); |
| 8015 | } |
| 8016 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8017 | static int status_resync(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8018 | { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8019 | sector_t max_sectors, resync, res; |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8020 | unsigned long dt, db = 0; |
| 8021 | sector_t rt, curr_mark_cnt, resync_mark_cnt; |
| 8022 | int scale, recovery_active; |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8023 | unsigned int per_milli; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8024 | |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8025 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8026 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8027 | max_sectors = mddev->resync_max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8028 | else |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8029 | max_sectors = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8030 | |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8031 | resync = mddev->curr_resync; |
| 8032 | if (resync <= 3) { |
| 8033 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery)) |
| 8034 | /* Still cleaning up */ |
| 8035 | resync = max_sectors; |
Nate Dailey | d2e2ec8 | 2017-11-30 11:33:30 -0500 | [diff] [blame] | 8036 | } else if (resync > max_sectors) |
| 8037 | resync = max_sectors; |
| 8038 | else |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8039 | resync -= atomic_read(&mddev->recovery_active); |
| 8040 | |
| 8041 | if (resync == 0) { |
Guoqing Jiang | 0357ba2 | 2018-07-02 16:26:25 +0800 | [diff] [blame] | 8042 | if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery)) { |
| 8043 | struct md_rdev *rdev; |
| 8044 | |
| 8045 | rdev_for_each(rdev, mddev) |
| 8046 | if (rdev->raid_disk >= 0 && |
| 8047 | !test_bit(Faulty, &rdev->flags) && |
| 8048 | rdev->recovery_offset != MaxSector && |
| 8049 | rdev->recovery_offset) { |
| 8050 | seq_printf(seq, "\trecover=REMOTE"); |
| 8051 | return 1; |
| 8052 | } |
| 8053 | if (mddev->reshape_position != MaxSector) |
| 8054 | seq_printf(seq, "\treshape=REMOTE"); |
| 8055 | else |
| 8056 | seq_printf(seq, "\tresync=REMOTE"); |
| 8057 | return 1; |
| 8058 | } |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8059 | if (mddev->recovery_cp < MaxSector) { |
| 8060 | seq_printf(seq, "\tresync=PENDING"); |
| 8061 | return 1; |
| 8062 | } |
| 8063 | return 0; |
| 8064 | } |
| 8065 | if (resync < 3) { |
| 8066 | seq_printf(seq, "\tresync=DELAYED"); |
| 8067 | return 1; |
| 8068 | } |
| 8069 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 8070 | WARN_ON(max_sectors == 0); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8071 | /* Pick 'scale' such that (resync>>scale)*1000 will fit |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8072 | * in a sector_t, and (max_sectors>>scale) will fit in a |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8073 | * u32, as those are the requirements for sector_div. |
| 8074 | * Thus 'scale' must be at least 10 |
| 8075 | */ |
| 8076 | scale = 10; |
| 8077 | if (sizeof(sector_t) > sizeof(unsigned long)) { |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8078 | while ( max_sectors/2 > (1ULL<<(scale+32))) |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8079 | scale++; |
| 8080 | } |
| 8081 | res = (resync>>scale)*1000; |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8082 | sector_div(res, (u32)((max_sectors>>scale)+1)); |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8083 | |
| 8084 | per_milli = res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8085 | { |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8086 | int i, x = per_milli/50, y = 20-x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8087 | seq_printf(seq, "["); |
| 8088 | for (i = 0; i < x; i++) |
| 8089 | seq_printf(seq, "="); |
| 8090 | seq_printf(seq, ">"); |
| 8091 | for (i = 0; i < y; i++) |
| 8092 | seq_printf(seq, "."); |
| 8093 | seq_printf(seq, "] "); |
| 8094 | } |
NeilBrown | 4588b42 | 2006-03-27 01:18:04 -0800 | [diff] [blame] | 8095 | seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)", |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 8096 | (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)? |
| 8097 | "reshape" : |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8098 | (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)? |
| 8099 | "check" : |
| 8100 | (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ? |
| 8101 | "resync" : "recovery"))), |
| 8102 | per_milli/10, per_milli % 10, |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8103 | (unsigned long long) resync/2, |
| 8104 | (unsigned long long) max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8105 | |
| 8106 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8107 | * dt: time from mark until now |
| 8108 | * db: blocks written from mark until now |
| 8109 | * rt: remaining time |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8110 | * |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8111 | * rt is a sector_t, which is always 64bit now. We are keeping |
| 8112 | * the original algorithm, but it is not really necessary. |
| 8113 | * |
| 8114 | * Original algorithm: |
| 8115 | * So we divide before multiply in case it is 32bit and close |
| 8116 | * to the limit. |
| 8117 | * We scale the divisor (db) by 32 to avoid losing precision |
| 8118 | * near the end of resync when the number of remaining sectors |
| 8119 | * is close to 'db'. |
| 8120 | * We then divide rt by 32 after multiplying by db to compensate. |
| 8121 | * The '+1' avoids division by zero if db is very small. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8122 | */ |
| 8123 | dt = ((jiffies - mddev->resync_mark) / HZ); |
| 8124 | if (!dt) dt++; |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8125 | |
| 8126 | curr_mark_cnt = mddev->curr_mark_cnt; |
| 8127 | recovery_active = atomic_read(&mddev->recovery_active); |
| 8128 | resync_mark_cnt = mddev->resync_mark_cnt; |
| 8129 | |
| 8130 | if (curr_mark_cnt >= (recovery_active + resync_mark_cnt)) |
| 8131 | db = curr_mark_cnt - (recovery_active + resync_mark_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8132 | |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8133 | rt = max_sectors - resync; /* number of remaining sectors */ |
Mariusz Tkaczyk | 9642fa7 | 2019-06-13 16:11:41 +0200 | [diff] [blame] | 8134 | rt = div64_u64(rt, db/32+1); |
NeilBrown | dd71cf6 | 2009-05-07 12:49:35 +1000 | [diff] [blame] | 8135 | rt *= dt; |
| 8136 | rt >>= 5; |
| 8137 | |
| 8138 | seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60, |
| 8139 | ((unsigned long)rt % 60)/6); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8140 | |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8141 | seq_printf(seq, " speed=%ldK/sec", db/2/dt); |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8142 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8143 | } |
| 8144 | |
| 8145 | static void *md_seq_start(struct seq_file *seq, loff_t *pos) |
| 8146 | { |
| 8147 | struct list_head *tmp; |
| 8148 | loff_t l = *pos; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8149 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8150 | |
Jan Glauber | 7abfaba | 2021-03-17 15:04:39 +0100 | [diff] [blame] | 8151 | if (l == 0x10000) { |
| 8152 | ++*pos; |
| 8153 | return (void *)2; |
| 8154 | } |
| 8155 | if (l > 0x10000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8156 | return NULL; |
| 8157 | if (!l--) |
| 8158 | /* header */ |
| 8159 | return (void*)1; |
| 8160 | |
| 8161 | spin_lock(&all_mddevs_lock); |
| 8162 | list_for_each(tmp,&all_mddevs) |
| 8163 | if (!l--) { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8164 | mddev = list_entry(tmp, struct mddev, all_mddevs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8165 | mddev_get(mddev); |
| 8166 | spin_unlock(&all_mddevs_lock); |
| 8167 | return mddev; |
| 8168 | } |
| 8169 | spin_unlock(&all_mddevs_lock); |
| 8170 | if (!l--) |
| 8171 | return (void*)2;/* tail */ |
| 8172 | return NULL; |
| 8173 | } |
| 8174 | |
| 8175 | static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 8176 | { |
| 8177 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8178 | struct mddev *next_mddev, *mddev = v; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8180 | ++*pos; |
| 8181 | if (v == (void*)2) |
| 8182 | return NULL; |
| 8183 | |
| 8184 | spin_lock(&all_mddevs_lock); |
| 8185 | if (v == (void*)1) |
| 8186 | tmp = all_mddevs.next; |
| 8187 | else |
| 8188 | tmp = mddev->all_mddevs.next; |
| 8189 | if (tmp != &all_mddevs) |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8190 | next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8191 | else { |
| 8192 | next_mddev = (void*)2; |
| 8193 | *pos = 0x10000; |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8195 | spin_unlock(&all_mddevs_lock); |
| 8196 | |
| 8197 | if (v != (void*)1) |
| 8198 | mddev_put(mddev); |
| 8199 | return next_mddev; |
| 8200 | |
| 8201 | } |
| 8202 | |
| 8203 | static void md_seq_stop(struct seq_file *seq, void *v) |
| 8204 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8205 | struct mddev *mddev = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8206 | |
| 8207 | if (mddev && v != (void*)1 && v != (void*)2) |
| 8208 | mddev_put(mddev); |
| 8209 | } |
| 8210 | |
| 8211 | static int md_seq_show(struct seq_file *seq, void *v) |
| 8212 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8213 | struct mddev *mddev = v; |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8214 | sector_t sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8215 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8216 | |
| 8217 | if (v == (void*)1) { |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8218 | struct md_personality *pers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8219 | seq_printf(seq, "Personalities : "); |
| 8220 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8221 | list_for_each_entry(pers, &pers_list, list) |
| 8222 | seq_printf(seq, "[%s] ", pers->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8223 | |
| 8224 | spin_unlock(&pers_lock); |
| 8225 | seq_printf(seq, "\n"); |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8226 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8227 | return 0; |
| 8228 | } |
| 8229 | if (v == (void*)2) { |
| 8230 | status_unused(seq); |
| 8231 | return 0; |
| 8232 | } |
| 8233 | |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8234 | spin_lock(&mddev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8235 | if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) { |
| 8236 | seq_printf(seq, "%s : %sactive", mdname(mddev), |
| 8237 | mddev->pers ? "" : "in"); |
| 8238 | if (mddev->pers) { |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8239 | if (mddev->ro==1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8240 | seq_printf(seq, " (read-only)"); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8241 | if (mddev->ro==2) |
NeilBrown | 52720ae | 2008-03-10 11:43:47 -0700 | [diff] [blame] | 8242 | seq_printf(seq, " (auto-read-only)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8243 | seq_printf(seq, " %s", mddev->pers->name); |
| 8244 | } |
| 8245 | |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8246 | sectors = 0; |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 8247 | rcu_read_lock(); |
| 8248 | rdev_for_each_rcu(rdev, mddev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8249 | char b[BDEVNAME_SIZE]; |
| 8250 | seq_printf(seq, " %s[%d]", |
| 8251 | bdevname(rdev->bdev,b), rdev->desc_nr); |
NeilBrown | 8ddf9ef | 2005-09-09 16:23:45 -0700 | [diff] [blame] | 8252 | if (test_bit(WriteMostly, &rdev->flags)) |
| 8253 | seq_printf(seq, "(W)"); |
Shaohua Li | 9efdca1 | 2015-10-12 16:59:50 -0700 | [diff] [blame] | 8254 | if (test_bit(Journal, &rdev->flags)) |
| 8255 | seq_printf(seq, "(J)"); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8256 | if (test_bit(Faulty, &rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8257 | seq_printf(seq, "(F)"); |
| 8258 | continue; |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8259 | } |
| 8260 | if (rdev->raid_disk < 0) |
NeilBrown | b325a32 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8261 | seq_printf(seq, "(S)"); /* spare */ |
NeilBrown | 2d78f8c | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 8262 | if (test_bit(Replacement, &rdev->flags)) |
| 8263 | seq_printf(seq, "(R)"); |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8264 | sectors += rdev->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8265 | } |
NeilBrown | f97fcad | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 8266 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8267 | |
| 8268 | if (!list_empty(&mddev->disks)) { |
| 8269 | if (mddev->pers) |
| 8270 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 8271 | (unsigned long long) |
| 8272 | mddev->array_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8273 | else |
| 8274 | seq_printf(seq, "\n %llu blocks", |
Andre Noll | dd8ac33 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8275 | (unsigned long long)sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8276 | } |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8277 | if (mddev->persistent) { |
| 8278 | if (mddev->major_version != 0 || |
| 8279 | mddev->minor_version != 90) { |
| 8280 | seq_printf(seq," super %d.%d", |
| 8281 | mddev->major_version, |
| 8282 | mddev->minor_version); |
| 8283 | } |
NeilBrown | e691063 | 2008-02-06 01:39:51 -0800 | [diff] [blame] | 8284 | } else if (mddev->external) |
| 8285 | seq_printf(seq, " super external:%s", |
| 8286 | mddev->metadata_type); |
| 8287 | else |
NeilBrown | 1cd6bf1 | 2005-09-09 16:24:00 -0700 | [diff] [blame] | 8288 | seq_printf(seq, " super non-persistent"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8289 | |
| 8290 | if (mddev->pers) { |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 8291 | mddev->pers->status(seq, mddev); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8292 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8293 | if (mddev->pers->sync_request) { |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 8294 | if (status_resync(seq, mddev)) |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8295 | seq_printf(seq, "\n "); |
NeilBrown | 8e1b39d | 2005-11-08 21:39:41 -0800 | [diff] [blame] | 8296 | } |
NeilBrown | 32a7627 | 2005-06-21 17:17:14 -0700 | [diff] [blame] | 8297 | } else |
| 8298 | seq_printf(seq, "\n "); |
| 8299 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 8300 | md_bitmap_status(seq, mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8301 | |
| 8302 | seq_printf(seq, "\n"); |
| 8303 | } |
NeilBrown | 36d091f | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8304 | spin_unlock(&mddev->lock); |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8306 | return 0; |
| 8307 | } |
| 8308 | |
Jan Engelhardt | 110518b | 2009-05-07 12:49:37 +1000 | [diff] [blame] | 8309 | static const struct seq_operations md_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8310 | .start = md_seq_start, |
| 8311 | .next = md_seq_next, |
| 8312 | .stop = md_seq_stop, |
| 8313 | .show = md_seq_show, |
| 8314 | }; |
| 8315 | |
| 8316 | static int md_seq_open(struct inode *inode, struct file *file) |
| 8317 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8318 | struct seq_file *seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8319 | int error; |
| 8320 | |
| 8321 | error = seq_open(file, &md_seq_ops); |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8322 | if (error) |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8323 | return error; |
| 8324 | |
| 8325 | seq = file->private_data; |
| 8326 | seq->poll_event = atomic_read(&md_event_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8327 | return error; |
| 8328 | } |
| 8329 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8330 | static int md_unloading; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 8331 | static __poll_t mdstat_poll(struct file *filp, poll_table *wait) |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8332 | { |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8333 | struct seq_file *seq = filp->private_data; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 8334 | __poll_t mask; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8335 | |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 8336 | if (md_unloading) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8337 | return EPOLLIN|EPOLLRDNORM|EPOLLERR|EPOLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8338 | poll_wait(filp, &md_event_waiters, wait); |
| 8339 | |
| 8340 | /* always allow read */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8341 | mask = EPOLLIN | EPOLLRDNORM; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8342 | |
Kay Sievers | f151463 | 2011-07-12 20:48:39 +0200 | [diff] [blame] | 8343 | if (seq->poll_event != atomic_read(&md_event_count)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 8344 | mask |= EPOLLERR | EPOLLPRI; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8345 | return mask; |
| 8346 | } |
| 8347 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 8348 | static const struct proc_ops mdstat_proc_ops = { |
| 8349 | .proc_open = md_seq_open, |
| 8350 | .proc_read = seq_read, |
| 8351 | .proc_lseek = seq_lseek, |
| 8352 | .proc_release = seq_release, |
| 8353 | .proc_poll = mdstat_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8354 | }; |
| 8355 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8356 | int register_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8357 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8358 | pr_debug("md: %s personality registered for level %d\n", |
| 8359 | p->name, p->level); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8360 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8361 | list_add_tail(&p->list, &pers_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8362 | spin_unlock(&pers_lock); |
| 8363 | return 0; |
| 8364 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8365 | EXPORT_SYMBOL(register_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8366 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8367 | int unregister_md_personality(struct md_personality *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8368 | { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8369 | pr_debug("md: %s personality unregistered\n", p->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8370 | spin_lock(&pers_lock); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8371 | list_del_init(&p->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8372 | spin_unlock(&pers_lock); |
| 8373 | return 0; |
| 8374 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8375 | EXPORT_SYMBOL(unregister_md_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8376 | |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8377 | int register_md_cluster_operations(struct md_cluster_operations *ops, |
| 8378 | struct module *module) |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8379 | { |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8380 | int ret = 0; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8381 | spin_lock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8382 | if (md_cluster_ops != NULL) |
| 8383 | ret = -EALREADY; |
| 8384 | else { |
| 8385 | md_cluster_ops = ops; |
| 8386 | md_cluster_mod = module; |
| 8387 | } |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8388 | spin_unlock(&pers_lock); |
NeilBrown | 6022e75 | 2015-08-13 12:32:55 +1000 | [diff] [blame] | 8389 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8390 | } |
| 8391 | EXPORT_SYMBOL(register_md_cluster_operations); |
| 8392 | |
| 8393 | int unregister_md_cluster_operations(void) |
| 8394 | { |
| 8395 | spin_lock(&pers_lock); |
| 8396 | md_cluster_ops = NULL; |
| 8397 | spin_unlock(&pers_lock); |
| 8398 | return 0; |
| 8399 | } |
| 8400 | EXPORT_SYMBOL(unregister_md_cluster_operations); |
| 8401 | |
| 8402 | int md_setup_cluster(struct mddev *mddev, int nodes) |
| 8403 | { |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 8404 | int ret; |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 8405 | if (!md_cluster_ops) |
| 8406 | request_module("md-cluster"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8407 | spin_lock(&pers_lock); |
Guoqing Jiang | 47a7b0d | 2016-09-04 22:17:28 -0400 | [diff] [blame] | 8408 | /* ensure module won't be unloaded */ |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8409 | if (!md_cluster_ops || !try_module_get(md_cluster_mod)) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8410 | 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] | 8411 | spin_unlock(&pers_lock); |
| 8412 | return -ENOENT; |
| 8413 | } |
| 8414 | spin_unlock(&pers_lock); |
| 8415 | |
Zhao Heming | 7c9d5c5 | 2020-07-21 02:08:52 +0800 | [diff] [blame] | 8416 | ret = md_cluster_ops->join(mddev, nodes); |
| 8417 | if (!ret) |
| 8418 | mddev->safemode_delay = 0; |
| 8419 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8420 | } |
| 8421 | |
| 8422 | void md_cluster_stop(struct mddev *mddev) |
| 8423 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 8424 | if (!md_cluster_ops) |
| 8425 | return; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 8426 | md_cluster_ops->leave(mddev); |
| 8427 | module_put(md_cluster_mod); |
| 8428 | } |
| 8429 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8430 | static int is_mddev_idle(struct mddev *mddev, int init) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8431 | { |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 8432 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8433 | int idle; |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8434 | int curr_events; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8435 | |
| 8436 | idle = 1; |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 8437 | rcu_read_lock(); |
| 8438 | rdev_for_each_rcu(rdev, mddev) { |
Christoph Hellwig | 4245e52 | 2020-09-03 07:40:59 +0200 | [diff] [blame] | 8439 | struct gendisk *disk = rdev->bdev->bd_disk; |
Christoph Hellwig | 8446fe9 | 2020-11-24 09:36:54 +0100 | [diff] [blame] | 8440 | curr_events = (int)part_stat_read_accum(disk->part0, sectors) - |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8441 | atomic_read(&disk->sync_io); |
NeilBrown | 713f6ab | 2007-07-17 04:06:12 -0700 | [diff] [blame] | 8442 | /* sync IO will cause sync_io to increase before the disk_stats |
| 8443 | * as sync_io is counted when a request starts, and |
| 8444 | * disk_stats is counted when it completes. |
| 8445 | * So resync activity will cause curr_events to be smaller than |
| 8446 | * when there was no such activity. |
| 8447 | * non-sync IO will cause disk_stat to increase without |
| 8448 | * increasing sync_io so curr_events will (eventually) |
| 8449 | * be larger than it was before. Once it becomes |
| 8450 | * substantially larger, the test below will cause |
| 8451 | * the array to appear non-idle, and resync will slow |
| 8452 | * down. |
| 8453 | * If there is a lot of outstanding resync activity when |
| 8454 | * we set last_event to curr_events, then all that activity |
| 8455 | * completing might cause the array to appear non-idle |
| 8456 | * and resync will be slowed down even though there might |
| 8457 | * not have been non-resync activity. This will only |
| 8458 | * happen once though. 'last_events' will soon reflect |
| 8459 | * the state where there is little or no outstanding |
| 8460 | * resync requests, and further resync activity will |
| 8461 | * always make curr_events less than last_events. |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8462 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8463 | */ |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8464 | if (init || curr_events - rdev->last_events > 64) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8465 | rdev->last_events = curr_events; |
| 8466 | idle = 0; |
| 8467 | } |
| 8468 | } |
NeilBrown | 4b80991 | 2008-07-21 17:05:25 +1000 | [diff] [blame] | 8469 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8470 | return idle; |
| 8471 | } |
| 8472 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8473 | void md_done_sync(struct mddev *mddev, int blocks, int ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8474 | { |
| 8475 | /* another "blocks" (512byte) blocks have been synced */ |
| 8476 | atomic_sub(blocks, &mddev->recovery_active); |
| 8477 | wake_up(&mddev->recovery_wait); |
| 8478 | if (!ok) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8479 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 8480 | set_bit(MD_RECOVERY_ERROR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8481 | md_wakeup_thread(mddev->thread); |
| 8482 | // stop recovery, signal do_sync .... |
| 8483 | } |
| 8484 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8485 | EXPORT_SYMBOL(md_done_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8486 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8487 | /* md_write_start(mddev, bi) |
| 8488 | * If we need to update some array metadata (e.g. 'active' flag |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8489 | * in superblock) before writing, schedule a superblock update |
| 8490 | * and wait for it to complete. |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8491 | * A return value of 'false' means that the write wasn't recorded |
| 8492 | * and cannot proceed as the array is being suspend. |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8493 | */ |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8494 | bool md_write_start(struct mddev *mddev, struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8495 | { |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8496 | int did_change = 0; |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 8497 | |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8498 | if (bio_data_dir(bi) != WRITE) |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8499 | return true; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8500 | |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8501 | BUG_ON(mddev->ro == 1); |
| 8502 | if (mddev->ro == 2) { |
| 8503 | /* need to switch to read/write */ |
| 8504 | mddev->ro = 0; |
| 8505 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8506 | md_wakeup_thread(mddev->thread); |
NeilBrown | 2515619 | 2008-03-04 14:29:32 -0800 | [diff] [blame] | 8507 | md_wakeup_thread(mddev->sync_thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8508 | did_change = 1; |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 8509 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8510 | rcu_read_lock(); |
| 8511 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 55cc39f | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8512 | smp_mb(); /* Match smp_mb in set_in_sync() */ |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 8513 | if (mddev->safemode == 1) |
| 8514 | mddev->safemode = 0; |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8515 | /* sync_checkers is always 0 when writes_pending is in per-cpu mode */ |
NeilBrown | 81fe48e | 2017-08-08 16:56:36 +1000 | [diff] [blame] | 8516 | if (mddev->in_sync || mddev->sync_checkers) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8517 | spin_lock(&mddev->lock); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8518 | if (mddev->in_sync) { |
| 8519 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8520 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8521 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8522 | md_wakeup_thread(mddev->thread); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8523 | did_change = 1; |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 8524 | } |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8525 | spin_unlock(&mddev->lock); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 8526 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8527 | rcu_read_unlock(); |
Neil Brown | 0fd62b8 | 2008-06-28 08:31:36 +1000 | [diff] [blame] | 8528 | if (did_change) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8529 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Heinz Mauelshagen | 4b6c106 | 2018-02-02 23:13:19 +0100 | [diff] [blame] | 8530 | if (!mddev->has_superblocks) |
| 8531 | return true; |
NeilBrown | 09a44cc | 2008-05-23 13:04:36 -0700 | [diff] [blame] | 8532 | wait_event(mddev->sb_wait, |
NeilBrown | d47c8ad | 2017-10-05 16:23:16 +1100 | [diff] [blame] | 8533 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags) || |
| 8534 | mddev->suspended); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 8535 | if (test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
| 8536 | percpu_ref_put(&mddev->writes_pending); |
| 8537 | return false; |
| 8538 | } |
| 8539 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8540 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8541 | EXPORT_SYMBOL(md_write_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8542 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8543 | /* md_write_inc can only be called when md_write_start() has |
| 8544 | * already been called at least once of the current request. |
| 8545 | * It increments the counter and is useful when a single request |
| 8546 | * is split into several parts. Each part causes an increment and |
| 8547 | * so needs a matching md_write_end(). |
| 8548 | * Unlike md_write_start(), it is safe to call md_write_inc() inside |
| 8549 | * a spinlocked region. |
| 8550 | */ |
| 8551 | void md_write_inc(struct mddev *mddev, struct bio *bi) |
| 8552 | { |
| 8553 | if (bio_data_dir(bi) != WRITE) |
| 8554 | return; |
| 8555 | WARN_ON_ONCE(mddev->in_sync || mddev->ro); |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8556 | percpu_ref_get(&mddev->writes_pending); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 8557 | } |
| 8558 | EXPORT_SYMBOL(md_write_inc); |
| 8559 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8560 | void md_write_end(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8561 | { |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8562 | percpu_ref_put(&mddev->writes_pending); |
| 8563 | |
| 8564 | if (mddev->safemode == 2) |
| 8565 | md_wakeup_thread(mddev->thread); |
| 8566 | else if (mddev->safemode_delay) |
| 8567 | /* The roundup() ensures this only performs locking once |
| 8568 | * every ->safemode_delay jiffies |
| 8569 | */ |
| 8570 | mod_timer(&mddev->safemode_timer, |
| 8571 | roundup(jiffies, mddev->safemode_delay) + |
| 8572 | mddev->safemode_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8573 | } |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 8574 | |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 8575 | EXPORT_SYMBOL(md_write_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8576 | |
Xiao Ni | cf78408 | 2021-02-04 15:50:43 +0800 | [diff] [blame] | 8577 | /* This is used by raid0 and raid10 */ |
| 8578 | void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev, |
| 8579 | struct bio *bio, sector_t start, sector_t size) |
| 8580 | { |
| 8581 | struct bio *discard_bio = NULL; |
| 8582 | |
| 8583 | if (__blkdev_issue_discard(rdev->bdev, start, size, GFP_NOIO, 0, |
| 8584 | &discard_bio) || !discard_bio) |
| 8585 | return; |
| 8586 | |
| 8587 | bio_chain(discard_bio, bio); |
| 8588 | bio_clone_blkg_association(discard_bio, bio); |
| 8589 | if (mddev->gendisk) |
| 8590 | trace_block_bio_remap(discard_bio, |
| 8591 | disk_devt(mddev->gendisk), |
| 8592 | bio->bi_iter.bi_sector); |
| 8593 | submit_bio_noacct(discard_bio); |
| 8594 | } |
| 8595 | EXPORT_SYMBOL_GPL(md_submit_discard_bio); |
| 8596 | |
Guoqing Jiang | 1076481 | 2021-05-25 17:46:17 +0800 | [diff] [blame] | 8597 | static void md_end_io_acct(struct bio *bio) |
| 8598 | { |
| 8599 | struct md_io_acct *md_io_acct = bio->bi_private; |
| 8600 | struct bio *orig_bio = md_io_acct->orig_bio; |
| 8601 | |
| 8602 | orig_bio->bi_status = bio->bi_status; |
| 8603 | |
| 8604 | bio_end_io_acct(orig_bio, md_io_acct->start_time); |
| 8605 | bio_put(bio); |
| 8606 | bio_endio(orig_bio); |
| 8607 | } |
| 8608 | |
Guoqing Jiang | daee202 | 2021-06-03 17:21:06 +0800 | [diff] [blame] | 8609 | /* |
| 8610 | * Used by personalities that don't already clone the bio and thus can't |
| 8611 | * easily add the timestamp to their extended bio structure. |
| 8612 | */ |
Guoqing Jiang | 1076481 | 2021-05-25 17:46:17 +0800 | [diff] [blame] | 8613 | void md_account_bio(struct mddev *mddev, struct bio **bio) |
| 8614 | { |
| 8615 | struct md_io_acct *md_io_acct; |
| 8616 | struct bio *clone; |
| 8617 | |
| 8618 | if (!blk_queue_io_stat((*bio)->bi_bdev->bd_disk->queue)) |
| 8619 | return; |
| 8620 | |
| 8621 | clone = bio_clone_fast(*bio, GFP_NOIO, &mddev->io_acct_set); |
| 8622 | md_io_acct = container_of(clone, struct md_io_acct, bio_clone); |
| 8623 | md_io_acct->orig_bio = *bio; |
| 8624 | md_io_acct->start_time = bio_start_io_acct(*bio); |
| 8625 | |
| 8626 | clone->bi_end_io = md_end_io_acct; |
| 8627 | clone->bi_private = md_io_acct; |
| 8628 | *bio = clone; |
| 8629 | } |
| 8630 | EXPORT_SYMBOL_GPL(md_account_bio); |
| 8631 | |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8632 | /* md_allow_write(mddev) |
| 8633 | * Calling this ensures that the array is marked 'active' so that writes |
| 8634 | * may proceed without blocking. It is important to call this before |
| 8635 | * attempting a GFP_KERNEL allocation while holding the mddev lock. |
| 8636 | * Must be called with mddev_lock held. |
| 8637 | */ |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8638 | void md_allow_write(struct mddev *mddev) |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8639 | { |
| 8640 | if (!mddev->pers) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8641 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8642 | if (mddev->ro) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8643 | return; |
Neil Brown | 1a0fd49 | 2008-06-28 08:31:27 +1000 | [diff] [blame] | 8644 | if (!mddev->pers->sync_request) |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8645 | return; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8646 | |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8647 | spin_lock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8648 | if (mddev->in_sync) { |
| 8649 | mddev->in_sync = 0; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8650 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
| 8651 | set_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8652 | if (mddev->safemode_delay && |
| 8653 | mddev->safemode == 0) |
| 8654 | mddev->safemode = 1; |
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 | md_update_sb(mddev, 0); |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 8657 | sysfs_notify_dirent_safe(mddev->sysfs_state); |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8658 | /* wait for the dirty state to be recorded in the metadata */ |
| 8659 | wait_event(mddev->sb_wait, |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 8660 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8661 | } else |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8662 | spin_unlock(&mddev->lock); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 8663 | } |
| 8664 | EXPORT_SYMBOL_GPL(md_allow_write); |
| 8665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8666 | #define SYNC_MARKS 10 |
| 8667 | #define SYNC_MARK_STEP (3*HZ) |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8668 | #define UPDATE_FREQUENCY (5*60*HZ) |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8669 | void md_do_sync(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8670 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 8671 | struct mddev *mddev = thread->mddev; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8672 | struct mddev *mddev2; |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8673 | unsigned int currspeed = 0, window; |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8674 | sector_t max_sectors,j, io_sectors, recovery_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8675 | unsigned long mark[SYNC_MARKS]; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8676 | unsigned long update_time; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8677 | sector_t mark_cnt[SYNC_MARKS]; |
| 8678 | int last_mark,m; |
| 8679 | struct list_head *tmp; |
| 8680 | sector_t last_check; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8681 | int skipped = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8682 | struct md_rdev *rdev; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8683 | char *desc, *action = NULL; |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8684 | struct blk_plug plug; |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8685 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8686 | |
| 8687 | /* just incase thread restarts... */ |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8688 | if (test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
| 8689 | test_bit(MD_RECOVERY_WAIT, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8690 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8691 | if (mddev->ro) {/* never try to sync a read-only array */ |
| 8692 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8693 | return; |
NeilBrown | 3991b31 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 8694 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8695 | |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8696 | if (mddev_is_clustered(mddev)) { |
| 8697 | ret = md_cluster_ops->resync_start(mddev); |
| 8698 | if (ret) |
| 8699 | goto skip; |
| 8700 | |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 8701 | set_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags); |
Guoqing Jiang | 41a9a0d | 2016-05-02 11:33:08 -0400 | [diff] [blame] | 8702 | if (!(test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 8703 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) || |
| 8704 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) |
| 8705 | && ((unsigned long long)mddev->curr_resync_completed |
| 8706 | < (unsigned long long)mddev->resync_max_sectors)) |
| 8707 | goto skip; |
| 8708 | } |
| 8709 | |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8710 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8711 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8712 | desc = "data-check"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8713 | action = "check"; |
| 8714 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) { |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8715 | desc = "requested-resync"; |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8716 | action = "repair"; |
| 8717 | } else |
NeilBrown | 61df9d9 | 2006-10-03 01:15:57 -0700 | [diff] [blame] | 8718 | desc = "resync"; |
| 8719 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 8720 | desc = "reshape"; |
| 8721 | else |
| 8722 | desc = "recovery"; |
| 8723 | |
Jonathan Brassow | c4a3955 | 2013-06-25 01:23:59 -0500 | [diff] [blame] | 8724 | mddev->last_sync_action = action ?: desc; |
| 8725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8726 | /* we overload curr_resync somewhat here. |
| 8727 | * 0 == not engaged in resync at all |
| 8728 | * 2 == checking that there is no conflict with another sync |
| 8729 | * 1 == like 2, but have yielded to allow conflicting resync to |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8730 | * commence |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8731 | * other == active in resync - this many blocks |
| 8732 | * |
| 8733 | * Before starting a resync we must have set curr_resync to |
| 8734 | * 2, and then checked that every "conflicting" array has curr_resync |
| 8735 | * less than ours. When we find one that is the same or higher |
| 8736 | * we wait on resync_wait. To avoid deadlock, we reduce curr_resync |
| 8737 | * to 1 if we choose to yield (based arbitrarily on address of mddev structure). |
| 8738 | * This will mean we have to start checking from the beginning again. |
| 8739 | * |
| 8740 | */ |
| 8741 | |
| 8742 | do { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8743 | int mddev2_minor = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8744 | mddev->curr_resync = 2; |
| 8745 | |
| 8746 | try_again: |
NeilBrown | 404e4b4 | 2009-12-30 15:25:23 +1100 | [diff] [blame] | 8747 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8748 | goto skip; |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 8749 | for_each_mddev(mddev2, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8750 | if (mddev2 == mddev) |
| 8751 | continue; |
Bernd Schubert | 90b0871 | 2008-05-23 13:04:38 -0700 | [diff] [blame] | 8752 | if (!mddev->parallel_resync |
| 8753 | && mddev2->curr_resync |
| 8754 | && match_mddev_units(mddev, mddev2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8755 | DEFINE_WAIT(wq); |
| 8756 | if (mddev < mddev2 && mddev->curr_resync == 2) { |
| 8757 | /* arbitrarily yield */ |
| 8758 | mddev->curr_resync = 1; |
| 8759 | wake_up(&resync_wait); |
| 8760 | } |
| 8761 | if (mddev > mddev2 && mddev->curr_resync == 1) |
| 8762 | /* no need to wait here, we can wait the next |
| 8763 | * time 'round when curr_resync == 2 |
| 8764 | */ |
| 8765 | continue; |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8766 | /* We need to wait 'interruptible' so as not to |
| 8767 | * contribute to the load average, and not to |
| 8768 | * be caught by 'softlockup' |
| 8769 | */ |
| 8770 | prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8771 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 8712e55 | 2005-10-26 01:58:58 -0700 | [diff] [blame] | 8772 | mddev2->curr_resync >= mddev->curr_resync) { |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8773 | if (mddev2_minor != mddev2->md_minor) { |
| 8774 | mddev2_minor = mddev2->md_minor; |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8775 | pr_info("md: delaying %s of %s until %s has finished (they share one or more physical units)\n", |
| 8776 | desc, mdname(mddev), |
| 8777 | mdname(mddev2)); |
Artur Paszkiewicz | c622ca5 | 2016-08-16 14:26:08 +0200 | [diff] [blame] | 8778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8779 | mddev_put(mddev2); |
NeilBrown | 9744197 | 2008-09-19 11:49:54 +1000 | [diff] [blame] | 8780 | if (signal_pending(current)) |
| 8781 | flush_signals(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8782 | schedule(); |
| 8783 | finish_wait(&resync_wait, &wq); |
| 8784 | goto try_again; |
| 8785 | } |
| 8786 | finish_wait(&resync_wait, &wq); |
| 8787 | } |
| 8788 | } |
| 8789 | } while (mddev->curr_resync < 2); |
| 8790 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8791 | j = 0; |
NeilBrown | 9d88883 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 8792 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8793 | /* resync follows the size requested by the personality, |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8794 | * which defaults to physical size, but can be virtual size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8795 | */ |
| 8796 | max_sectors = mddev->resync_max_sectors; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 8797 | atomic64_set(&mddev->resync_mismatches, 0); |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8798 | /* we don't use the checkpoint if there's a bitmap */ |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8799 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 8800 | j = mddev->resync_min; |
| 8801 | else if (!mddev->bitmap) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8802 | j = mddev->recovery_cp; |
Neil Brown | 5e96ee6 | 2008-06-28 08:31:24 +1000 | [diff] [blame] | 8803 | |
Guoqing Jiang | cb9ee15 | 2018-10-18 16:37:47 +0800 | [diff] [blame] | 8804 | } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
NeilBrown | c804cde | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 8805 | max_sectors = mddev->resync_max_sectors; |
Guoqing Jiang | cb9ee15 | 2018-10-18 16:37:47 +0800 | [diff] [blame] | 8806 | /* |
| 8807 | * If the original node aborts reshaping then we continue the |
| 8808 | * reshaping, so set j again to avoid restart reshape from the |
| 8809 | * first beginning |
| 8810 | */ |
| 8811 | if (mddev_is_clustered(mddev) && |
| 8812 | mddev->reshape_position != MaxSector) |
| 8813 | j = mddev->reshape_position; |
| 8814 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8815 | /* recovery follows the physical size of devices */ |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8816 | max_sectors = mddev->dev_sectors; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8817 | j = MaxSector; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8818 | rcu_read_lock(); |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8819 | rdev_for_each_rcu(rdev, mddev) |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8820 | if (rdev->raid_disk >= 0 && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 8821 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8822 | !test_bit(Faulty, &rdev->flags) && |
| 8823 | !test_bit(In_sync, &rdev->flags) && |
| 8824 | rdev->recovery_offset < j) |
| 8825 | j = rdev->recovery_offset; |
Dan Williams | 4e59ca7 | 2009-12-12 21:17:06 -0700 | [diff] [blame] | 8826 | rcu_read_unlock(); |
NeilBrown | 133d452 | 2014-07-02 12:04:14 +1000 | [diff] [blame] | 8827 | |
| 8828 | /* If there is a bitmap, we need to make sure all |
| 8829 | * writes that started before we added a spare |
| 8830 | * complete before we start doing a recovery. |
| 8831 | * Otherwise the write might complete and (via |
| 8832 | * bitmap_endwrite) set a bit in the bitmap after the |
| 8833 | * recovery has checked that bit and skipped that |
| 8834 | * region. |
| 8835 | */ |
| 8836 | if (mddev->bitmap) { |
| 8837 | mddev->pers->quiesce(mddev, 1); |
| 8838 | mddev->pers->quiesce(mddev, 0); |
| 8839 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8840 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8841 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8842 | pr_info("md: %s of RAID array %s\n", desc, mdname(mddev)); |
| 8843 | pr_debug("md: minimum _guaranteed_ speed: %d KB/sec/disk.\n", speed_min(mddev)); |
| 8844 | pr_debug("md: using maximum available idle IO bandwidth (but not more than %d KB/sec) for %s.\n", |
| 8845 | speed_max(mddev), desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8846 | |
NeilBrown | eea1bf3 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 8847 | is_mddev_idle(mddev, 1); /* this initializes IO event counters */ |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 8848 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8849 | io_sectors = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8850 | for (m = 0; m < SYNC_MARKS; m++) { |
| 8851 | mark[m] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8852 | mark_cnt[m] = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8853 | } |
| 8854 | last_mark = 0; |
| 8855 | mddev->resync_mark = mark[last_mark]; |
| 8856 | mddev->resync_mark_cnt = mark_cnt[last_mark]; |
| 8857 | |
| 8858 | /* |
| 8859 | * Tune reconstruction: |
| 8860 | */ |
Yufen Yu | e5b521e | 2019-06-14 15:41:07 -0700 | [diff] [blame] | 8861 | window = 32 * (PAGE_SIZE / 512); |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8862 | pr_debug("md: using %dk window, over a total of %lluk.\n", |
| 8863 | window/2, (unsigned long long)max_sectors/2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8864 | |
| 8865 | atomic_set(&mddev->recovery_active, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8866 | last_check = 0; |
| 8867 | |
| 8868 | if (j>2) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8869 | pr_debug("md: resuming %s of %s from checkpoint.\n", |
| 8870 | desc, mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8871 | mddev->curr_resync = j; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8872 | } else |
| 8873 | mddev->curr_resync = 3; /* no longer delayed */ |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8874 | mddev->curr_resync_completed = j; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 8875 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 8876 | md_new_event(); |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8877 | update_time = jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8878 | |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 8879 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8880 | while (j < max_sectors) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8881 | sector_t sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8882 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8883 | skipped = 0; |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8884 | |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8885 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 8886 | ((mddev->curr_resync > mddev->curr_resync_completed && |
| 8887 | (mddev->curr_resync - mddev->curr_resync_completed) |
| 8888 | > (max_sectors >> 4)) || |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8889 | time_after_eq(jiffies, update_time + UPDATE_FREQUENCY) || |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8890 | (j - mddev->curr_resync_completed)*2 |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 8891 | >= mddev->resync_max - mddev->curr_resync_completed || |
| 8892 | mddev->curr_resync_completed > mddev->resync_max |
NeilBrown | 7a91ee1 | 2009-05-26 12:57:21 +1000 | [diff] [blame] | 8893 | )) { |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8894 | /* time to update curr_resync_completed */ |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8895 | wait_event(mddev->recovery_wait, |
| 8896 | atomic_read(&mddev->recovery_active) == 0); |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 8897 | mddev->curr_resync_completed = j; |
kernelmail | 35d78c6 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8898 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
| 8899 | j > mddev->recovery_cp) |
| 8900 | mddev->recovery_cp = j; |
majianpeng | 54f8934 | 2012-10-31 11:59:10 +1100 | [diff] [blame] | 8901 | update_time = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8902 | set_bit(MD_SB_CHANGE_CLEAN, &mddev->sb_flags); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 8903 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 97e4f42 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8904 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8905 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8906 | while (j >= mddev->resync_max && |
| 8907 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8908 | /* As this condition is controlled by user-space, |
| 8909 | * we can block indefinitely, so use '_interruptible' |
| 8910 | * to avoid triggering warnings. |
| 8911 | */ |
| 8912 | flush_signals(current); /* just in case */ |
| 8913 | wait_event_interruptible(mddev->recovery_wait, |
| 8914 | mddev->resync_max > j |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8915 | || test_bit(MD_RECOVERY_INTR, |
| 8916 | &mddev->recovery)); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 8917 | } |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8918 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8919 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8920 | break; |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 8921 | |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 8922 | sectors = mddev->pers->sync_request(mddev, j, &skipped); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8923 | if (sectors == 0) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 8924 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8925 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8926 | } |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8927 | |
| 8928 | if (!skipped) { /* actual IO requested */ |
| 8929 | io_sectors += sectors; |
| 8930 | atomic_add(sectors, &mddev->recovery_active); |
| 8931 | } |
| 8932 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8933 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8934 | break; |
| 8935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8936 | j += sectors; |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 8937 | if (j > max_sectors) |
| 8938 | /* when skipping, extra large numbers can be returned. */ |
| 8939 | j = max_sectors; |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 8940 | if (j > 2) |
| 8941 | mddev->curr_resync = j; |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 8942 | mddev->curr_mark_cnt = io_sectors; |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8943 | if (last_check == 0) |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 8944 | /* this is the earliest that rebuild will be |
NeilBrown | d7603b7 | 2006-01-06 00:20:30 -0800 | [diff] [blame] | 8945 | * visible in /proc/mdstat |
| 8946 | */ |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 8947 | md_new_event(); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8948 | |
| 8949 | if (last_check + window > io_sectors || j == max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8950 | continue; |
| 8951 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8952 | last_check = io_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8953 | repeat: |
| 8954 | if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) { |
| 8955 | /* step marks */ |
| 8956 | int next = (last_mark+1) % SYNC_MARKS; |
| 8957 | |
| 8958 | mddev->resync_mark = mark[next]; |
| 8959 | mddev->resync_mark_cnt = mark_cnt[next]; |
| 8960 | mark[next] = jiffies; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8961 | mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8962 | last_mark = next; |
| 8963 | } |
| 8964 | |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 8965 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 8966 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8967 | |
| 8968 | /* |
| 8969 | * this loop exits only if either when we are slower than |
| 8970 | * the 'hard' speed limit, or the system was IO-idle for |
| 8971 | * a jiffy. |
| 8972 | * the system might be non-idle CPU-wise, but we only care |
| 8973 | * about not overloading the IO subsystem. (things like an |
| 8974 | * e2fsck being done on the RAID array should execute fast) |
| 8975 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8976 | cond_resched(); |
| 8977 | |
Xiao Ni | ac7e50a | 2014-08-07 09:37:41 -0400 | [diff] [blame] | 8978 | recovery_done = io_sectors - atomic_read(&mddev->recovery_active); |
| 8979 | currspeed = ((unsigned long)(recovery_done - mddev->resync_mark_cnt))/2 |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 8980 | /((jiffies-mddev->resync_mark)/HZ +1) +1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8981 | |
NeilBrown | 88202a0 | 2006-01-06 00:21:36 -0800 | [diff] [blame] | 8982 | if (currspeed > speed_min(mddev)) { |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8983 | if (currspeed > speed_max(mddev)) { |
NeilBrown | c0e4852 | 2005-11-18 01:11:01 -0800 | [diff] [blame] | 8984 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8985 | goto repeat; |
| 8986 | } |
NeilBrown | ac8fa41 | 2015-02-19 16:55:00 +1100 | [diff] [blame] | 8987 | if (!is_mddev_idle(mddev, 0)) { |
| 8988 | /* |
| 8989 | * Give other IO more of a chance. |
| 8990 | * The faster the devices, the less we wait. |
| 8991 | */ |
| 8992 | wait_event(mddev->recovery_wait, |
| 8993 | !atomic_read(&mddev->recovery_active)); |
| 8994 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8995 | } |
| 8996 | } |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 8997 | pr_info("md: %s: %s %s.\n",mdname(mddev), desc, |
| 8998 | test_bit(MD_RECOVERY_INTR, &mddev->recovery) |
| 8999 | ? "interrupted" : "done"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9000 | /* |
| 9001 | * this also signals 'finished resyncing' to md_stop |
| 9002 | */ |
majianpeng | 7c2c57c | 2012-07-03 12:12:26 +1000 | [diff] [blame] | 9003 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9004 | wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active)); |
| 9005 | |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 9006 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 9007 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 9008 | mddev->curr_resync > 3) { |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 9009 | mddev->curr_resync_completed = mddev->curr_resync; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9010 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 5ed1df2 | 2015-07-24 13:27:08 +1000 | [diff] [blame] | 9011 | } |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 9012 | mddev->pers->sync_request(mddev, max_sectors, &skipped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9013 | |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9014 | if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) && |
NeilBrown | 1217e1d | 2016-10-28 15:59:41 +1100 | [diff] [blame] | 9015 | mddev->curr_resync > 3) { |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 9016 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 9017 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 9018 | if (mddev->curr_resync >= mddev->recovery_cp) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9019 | pr_debug("md: checkpointing %s of %s.\n", |
| 9020 | desc, mdname(mddev)); |
majianpeng | 0a19caa | 2012-11-19 19:57:34 +0800 | [diff] [blame] | 9021 | if (test_bit(MD_RECOVERY_ERROR, |
| 9022 | &mddev->recovery)) |
| 9023 | mddev->recovery_cp = |
| 9024 | mddev->curr_resync_completed; |
| 9025 | else |
| 9026 | mddev->recovery_cp = |
| 9027 | mddev->curr_resync; |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 9028 | } |
| 9029 | } else |
| 9030 | mddev->recovery_cp = MaxSector; |
| 9031 | } else { |
| 9032 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 9033 | mddev->curr_resync = MaxSector; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 9034 | if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 9035 | test_bit(MD_RECOVERY_RECOVER, &mddev->recovery)) { |
| 9036 | rcu_read_lock(); |
| 9037 | rdev_for_each_rcu(rdev, mddev) |
| 9038 | if (rdev->raid_disk >= 0 && |
| 9039 | mddev->delta_disks >= 0 && |
| 9040 | !test_bit(Journal, &rdev->flags) && |
| 9041 | !test_bit(Faulty, &rdev->flags) && |
| 9042 | !test_bit(In_sync, &rdev->flags) && |
| 9043 | rdev->recovery_offset < mddev->curr_resync) |
| 9044 | rdev->recovery_offset = mddev->curr_resync; |
| 9045 | rcu_read_unlock(); |
| 9046 | } |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 9047 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9048 | } |
NeilBrown | db91ff5 | 2012-02-07 12:01:51 +1100 | [diff] [blame] | 9049 | skip: |
Guoqing Jiang | bb8bf15 | 2016-06-02 23:32:04 -0400 | [diff] [blame] | 9050 | /* set CHANGE_PENDING here since maybe another update is needed, |
| 9051 | * so other nodes are informed. It should be harmless for normal |
| 9052 | * raid */ |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9053 | set_mask_bits(&mddev->sb_flags, 0, |
| 9054 | BIT(MD_SB_CHANGE_PENDING) | BIT(MD_SB_CHANGE_DEVS)); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 9055 | |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 9056 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 9057 | !test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
| 9058 | mddev->delta_disks > 0 && |
| 9059 | mddev->pers->finish_reshape && |
| 9060 | mddev->pers->size && |
| 9061 | mddev->queue) { |
| 9062 | mddev_lock_nointr(mddev); |
| 9063 | md_set_array_sectors(mddev, mddev->pers->size(mddev, 0, 0)); |
| 9064 | mddev_unlock(mddev); |
Christoph Hellwig | 2c247c5 | 2020-11-16 15:57:11 +0100 | [diff] [blame] | 9065 | if (!mddev_is_clustered(mddev)) |
| 9066 | set_capacity_and_notify(mddev->gendisk, |
| 9067 | mddev->array_sectors); |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 9068 | } |
| 9069 | |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9070 | spin_lock(&mddev->lock); |
NeilBrown | c07b70a | 2009-12-14 12:49:48 +1100 | [diff] [blame] | 9071 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 9072 | /* We completed so min/max setting can be forgotten if used. */ |
| 9073 | if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 9074 | mddev->resync_min = 0; |
| 9075 | mddev->resync_max = MaxSector; |
| 9076 | } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) |
| 9077 | mddev->resync_min = mddev->curr_resync_completed; |
NeilBrown | f7851be | 2015-07-02 17:12:58 +1000 | [diff] [blame] | 9078 | set_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9079 | mddev->curr_resync = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9080 | spin_unlock(&mddev->lock); |
| 9081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9082 | wake_up(&resync_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9083 | md_wakeup_thread(mddev->thread); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 9084 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9085 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 9086 | EXPORT_SYMBOL_GPL(md_do_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9087 | |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9088 | static int remove_and_add_spares(struct mddev *mddev, |
| 9089 | struct md_rdev *this) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9090 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9091 | struct md_rdev *rdev; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9092 | int spares = 0; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 9093 | int removed = 0; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9094 | bool remove_some = false; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9095 | |
NeilBrown | 39772f0 | 2018-02-03 09:19:30 +1100 | [diff] [blame] | 9096 | if (this && test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 9097 | /* Mustn't remove devices when resync thread is running */ |
| 9098 | return 0; |
| 9099 | |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9100 | rdev_for_each(rdev, mddev) { |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9101 | if ((this == NULL || rdev == this) && |
| 9102 | rdev->raid_disk >= 0 && |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9103 | !test_bit(Blocked, &rdev->flags) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9104 | test_bit(Faulty, &rdev->flags) && |
| 9105 | atomic_read(&rdev->nr_pending)==0) { |
| 9106 | /* Faulty non-Blocked devices with nr_pending == 0 |
| 9107 | * never get nr_pending incremented, |
| 9108 | * never get Faulty cleared, and never get Blocked set. |
| 9109 | * So we can synchronize_rcu now rather than once per device |
| 9110 | */ |
| 9111 | remove_some = true; |
| 9112 | set_bit(RemoveSynchronized, &rdev->flags); |
| 9113 | } |
| 9114 | } |
| 9115 | |
| 9116 | if (remove_some) |
| 9117 | synchronize_rcu(); |
| 9118 | rdev_for_each(rdev, mddev) { |
| 9119 | if ((this == NULL || rdev == this) && |
| 9120 | rdev->raid_disk >= 0 && |
| 9121 | !test_bit(Blocked, &rdev->flags) && |
| 9122 | ((test_bit(RemoveSynchronized, &rdev->flags) || |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 9123 | (!test_bit(In_sync, &rdev->flags) && |
| 9124 | !test_bit(Journal, &rdev->flags))) && |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9125 | atomic_read(&rdev->nr_pending)==0)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9126 | if (mddev->pers->hot_remove_disk( |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 9127 | mddev, rdev) == 0) { |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 9128 | sysfs_unlink_rdev(mddev, rdev); |
NeilBrown | 011abdc | 2018-04-26 14:46:29 +1000 | [diff] [blame] | 9129 | rdev->saved_raid_disk = rdev->raid_disk; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9130 | rdev->raid_disk = -1; |
NeilBrown | f2a371c | 2012-01-09 00:46:41 +1100 | [diff] [blame] | 9131 | removed++; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9132 | } |
| 9133 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 9134 | if (remove_some && test_bit(RemoveSynchronized, &rdev->flags)) |
| 9135 | clear_bit(RemoveSynchronized, &rdev->flags); |
| 9136 | } |
| 9137 | |
Jonathan Brassow | 90584fc | 2013-03-07 16:24:26 -0600 | [diff] [blame] | 9138 | if (removed && mddev->kobj.sd) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9139 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9140 | |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 9141 | if (this && removed) |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9142 | goto no_add; |
| 9143 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 9144 | rdev_for_each(rdev, mddev) { |
Goldwyn Rodrigues | 2910ff1 | 2015-09-28 10:27:26 -0500 | [diff] [blame] | 9145 | if (this && this != rdev) |
| 9146 | continue; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9147 | if (test_bit(Candidate, &rdev->flags)) |
| 9148 | continue; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 9149 | if (rdev->raid_disk >= 0 && |
| 9150 | !test_bit(In_sync, &rdev->flags) && |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 9151 | !test_bit(Journal, &rdev->flags) && |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 9152 | !test_bit(Faulty, &rdev->flags)) |
| 9153 | spares++; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9154 | if (rdev->raid_disk >= 0) |
| 9155 | continue; |
| 9156 | if (test_bit(Faulty, &rdev->flags)) |
| 9157 | continue; |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9158 | if (!test_bit(Journal, &rdev->flags)) { |
| 9159 | if (mddev->ro && |
| 9160 | ! (rdev->saved_raid_disk >= 0 && |
| 9161 | !test_bit(Bitmap_sync, &rdev->flags))) |
| 9162 | continue; |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9163 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9164 | rdev->recovery_offset = 0; |
| 9165 | } |
Guoqing Jiang | 3f79cc2 | 2020-04-04 23:57:11 +0200 | [diff] [blame] | 9166 | if (mddev->pers->hot_add_disk(mddev, rdev) == 0) { |
Damien Le Moal | 5e3b8a8 | 2020-07-16 13:54:40 +0900 | [diff] [blame] | 9167 | /* failure here is OK */ |
| 9168 | sysfs_link_rdev(mddev, rdev); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 9169 | if (!test_bit(Journal, &rdev->flags)) |
| 9170 | spares++; |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 9171 | md_new_event(); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9172 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9173 | } |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9174 | } |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9175 | no_add: |
NeilBrown | 6dafab6 | 2012-09-19 12:54:22 +1000 | [diff] [blame] | 9176 | if (removed) |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9177 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9178 | return spares; |
| 9179 | } |
NeilBrown | 7ebc0be | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 9180 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9181 | static void md_start_sync(struct work_struct *ws) |
| 9182 | { |
| 9183 | struct mddev *mddev = container_of(ws, struct mddev, del_work); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 9184 | |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9185 | mddev->sync_thread = md_register_thread(md_do_sync, |
| 9186 | mddev, |
| 9187 | "resync"); |
| 9188 | if (!mddev->sync_thread) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9189 | pr_warn("%s: could not start resync thread...\n", |
| 9190 | mdname(mddev)); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9191 | /* leave the spares where they are, it shouldn't hurt */ |
| 9192 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9193 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 9194 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 9195 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 9196 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9197 | wake_up(&resync_wait); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9198 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 9199 | &mddev->recovery)) |
| 9200 | if (mddev->sysfs_action) |
| 9201 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
| 9202 | } else |
| 9203 | md_wakeup_thread(mddev->sync_thread); |
| 9204 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 9205 | md_new_event(); |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9206 | } |
| 9207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9208 | /* |
| 9209 | * This routine is regularly called by all per-raid-array threads to |
| 9210 | * deal with generic issues like resync and super-block update. |
| 9211 | * Raid personalities that don't have a thread (linear/raid0) do not |
| 9212 | * need this as they never do any recovery or update the superblock. |
| 9213 | * |
| 9214 | * It does not do any resync itself, but rather "forks" off other threads |
| 9215 | * to do that as needed. |
| 9216 | * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in |
| 9217 | * "->recovery" and create a thread at ->sync_thread. |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 9218 | * When the thread finishes it sets MD_RECOVERY_DONE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9219 | * and wakeups up this thread which will reap the thread and finish up. |
| 9220 | * This thread also removes any faulty devices (with nr_pending == 0). |
| 9221 | * |
| 9222 | * The overall approach is: |
| 9223 | * 1/ if the superblock needs updating, update it. |
| 9224 | * 2/ If a recovery thread is running, don't do anything else. |
| 9225 | * 3/ If recovery has finished, clean up, possibly marking spares active. |
| 9226 | * 4/ If there are any faulty devices, remove them. |
| 9227 | * 5/ If array is degraded, try to add spares devices |
| 9228 | * 6/ If array has spares or is not in-sync, start a resync thread. |
| 9229 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9230 | void md_check_recovery(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9231 | { |
NeilBrown | 059421e | 2018-10-03 15:04:41 +1000 | [diff] [blame] | 9232 | if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags) && mddev->sb_flags) { |
| 9233 | /* Write superblock - thread that called mddev_suspend() |
| 9234 | * holds reconfig_mutex for us. |
| 9235 | */ |
| 9236 | set_bit(MD_UPDATING_SB, &mddev->flags); |
| 9237 | smp_mb__after_atomic(); |
| 9238 | if (test_bit(MD_ALLOW_SB_UPDATE, &mddev->flags)) |
| 9239 | md_update_sb(mddev, 0); |
| 9240 | clear_bit_unlock(MD_UPDATING_SB, &mddev->flags); |
| 9241 | wake_up(&mddev->sb_wait); |
| 9242 | } |
| 9243 | |
Jonathan Brassow | 68866e4 | 2011-06-08 15:10:08 +1000 | [diff] [blame] | 9244 | if (mddev->suspended) |
| 9245 | return; |
| 9246 | |
NeilBrown | 5f40402 | 2005-06-21 17:17:16 -0700 | [diff] [blame] | 9247 | if (mddev->bitmap) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9248 | md_bitmap_daemon_work(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9249 | |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9250 | if (signal_pending(current)) { |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 9251 | if (mddev->pers->sync_request && !mddev->external) { |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9252 | pr_debug("md: %s in immediate safe mode\n", |
| 9253 | mdname(mddev)); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9254 | mddev->safemode = 2; |
| 9255 | } |
| 9256 | flush_signals(current); |
| 9257 | } |
| 9258 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9259 | if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) |
| 9260 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9261 | if ( ! ( |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9262 | (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9263 | test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9264 | test_bit(MD_RECOVERY_DONE, &mddev->recovery) || |
NeilBrown | 31a59e3 | 2008-04-30 00:52:30 -0700 | [diff] [blame] | 9265 | (mddev->external == 0 && mddev->safemode == 1) || |
NeilBrown | 4ad23a97 | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 9266 | (mddev->safemode == 2 |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9267 | && !mddev->in_sync && mddev->recovery_cp == MaxSector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9268 | )) |
| 9269 | return; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9270 | |
NeilBrown | df5b89b | 2006-03-27 01:18:20 -0800 | [diff] [blame] | 9271 | if (mddev_trylock(mddev)) { |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9272 | int spares = 0; |
NeilBrown | 480523f | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 9273 | bool try_set_sync = mddev->safemode != 0; |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9274 | |
Shaohua Li | afc1f55 | 2017-08-11 20:34:45 -0700 | [diff] [blame] | 9275 | if (!mddev->external && mddev->safemode == 1) |
NeilBrown | 33182d1 | 2017-08-08 16:56:36 +1000 | [diff] [blame] | 9276 | mddev->safemode = 0; |
| 9277 | |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9278 | if (mddev->ro) { |
Neil Brown | ab16bfc | 2015-06-17 12:31:46 +1000 | [diff] [blame] | 9279 | struct md_rdev *rdev; |
| 9280 | if (!mddev->external && mddev->in_sync) |
| 9281 | /* 'Blocked' flag not needed as failed devices |
| 9282 | * will be recorded if array switched to read/write. |
| 9283 | * Leaving it set will prevent the device |
| 9284 | * from being removed. |
| 9285 | */ |
| 9286 | rdev_for_each(rdev, mddev) |
| 9287 | clear_bit(Blocked, &rdev->flags); |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9288 | /* On a read-only array we can: |
| 9289 | * - remove failed devices |
| 9290 | * - add already-in_sync devices if the array itself |
| 9291 | * is in-sync. |
| 9292 | * As we only add devices that are already in-sync, |
| 9293 | * we can activate the spares immediately. |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9294 | */ |
NeilBrown | 7ceb17e | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 9295 | remove_and_add_spares(mddev, NULL); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9296 | /* There is no thread, but we need to call |
| 9297 | * ->spare_active and clear saved_raid_disk |
| 9298 | */ |
NeilBrown | 2ac295a | 2014-05-29 11:40:03 +1000 | [diff] [blame] | 9299 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9300 | md_reap_sync_thread(mddev); |
NeilBrown | a4a3d26 | 2015-07-17 11:57:30 +1000 | [diff] [blame] | 9301 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 8313b8e | 2013-12-12 10:13:33 +1100 | [diff] [blame] | 9302 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9303 | clear_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags); |
NeilBrown | c89a8ee | 2008-08-05 15:54:13 +1000 | [diff] [blame] | 9304 | goto unlock; |
| 9305 | } |
| 9306 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 9307 | if (mddev_is_clustered(mddev)) { |
Heming Zhao | f7c7a2f | 2021-04-08 15:44:15 +0800 | [diff] [blame] | 9308 | struct md_rdev *rdev, *tmp; |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 9309 | /* kick the device if another node issued a |
| 9310 | * remove disk. |
| 9311 | */ |
Heming Zhao | f7c7a2f | 2021-04-08 15:44:15 +0800 | [diff] [blame] | 9312 | rdev_for_each_safe(rdev, tmp, mddev) { |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 9313 | if (test_and_clear_bit(ClusterRemove, &rdev->flags) && |
| 9314 | rdev->raid_disk < 0) |
| 9315 | md_kick_rdev_from_array(rdev); |
| 9316 | } |
| 9317 | } |
| 9318 | |
NeilBrown | 480523f | 2019-08-20 10:21:09 +1000 | [diff] [blame] | 9319 | if (try_set_sync && !mddev->external && !mddev->in_sync) { |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 9320 | spin_lock(&mddev->lock); |
NeilBrown | 6497709b | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 9321 | set_in_sync(mddev); |
NeilBrown | 85572d7 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 9322 | spin_unlock(&mddev->lock); |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9323 | } |
NeilBrown | fca4d84 | 2005-06-21 17:17:11 -0700 | [diff] [blame] | 9324 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9325 | if (mddev->sb_flags) |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 9326 | md_update_sb(mddev, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 9327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9328 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) && |
| 9329 | !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) { |
| 9330 | /* resync/recovery still happening */ |
| 9331 | clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9332 | goto unlock; |
| 9333 | } |
| 9334 | if (mddev->sync_thread) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9335 | md_reap_sync_thread(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9336 | goto unlock; |
| 9337 | } |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9338 | /* Set RUNNING before clearing NEEDED to avoid |
| 9339 | * any transients in the value of "sync_action". |
| 9340 | */ |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 9341 | mddev->curr_resync_completed = 0; |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9342 | spin_lock(&mddev->lock); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9343 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | 23da422 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 9344 | spin_unlock(&mddev->lock); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9345 | /* Clear some bits that don't mean anything, but |
| 9346 | * might be left set |
| 9347 | */ |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9348 | clear_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 9349 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9350 | |
NeilBrown | ed20958 | 2012-04-24 10:23:14 +1000 | [diff] [blame] | 9351 | if (!test_and_clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery) || |
| 9352 | test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9353 | goto not_running; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9354 | /* no recovery is running. |
| 9355 | * remove any failed drives, then |
| 9356 | * add spares if possible. |
NeilBrown | 72f36d5 | 2012-10-11 14:25:57 +1100 | [diff] [blame] | 9357 | * Spares are also removed and re-added, to allow |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9358 | * the personality to fail the re-add. |
| 9359 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9360 | |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9361 | if (mddev->reshape_position != MaxSector) { |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 9362 | if (mddev->pers->check_reshape == NULL || |
| 9363 | mddev->pers->check_reshape(mddev) != 0) |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9364 | /* Cannot proceed */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9365 | goto not_running; |
NeilBrown | b4c4c7b | 2007-02-28 20:11:48 -0800 | [diff] [blame] | 9366 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9367 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 746d320 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 9368 | } else if ((spares = remove_and_add_spares(mddev, NULL))) { |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9369 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9370 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Dan Williams | 56ac36d | 2008-08-07 10:02:47 -0700 | [diff] [blame] | 9371 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9372 | set_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9373 | } else if (mddev->recovery_cp < MaxSector) { |
| 9374 | set_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9375 | clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery); |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9376 | } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 9377 | /* nothing to be done ... */ |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9378 | goto not_running; |
NeilBrown | 24dd469 | 2005-11-08 21:39:26 -0800 | [diff] [blame] | 9379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9380 | if (mddev->pers->sync_request) { |
NeilBrown | ef99bf4 | 2012-05-22 13:55:08 +1000 | [diff] [blame] | 9381 | if (spares) { |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 9382 | /* We are adding a device or devices to an array |
| 9383 | * which has the bitmap stored on all devices. |
| 9384 | * So make sure all bitmap pages get written |
| 9385 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9386 | md_bitmap_write_all(mddev->bitmap); |
NeilBrown | a654b9d8 | 2005-06-21 17:17:27 -0700 | [diff] [blame] | 9387 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9388 | INIT_WORK(&mddev->del_work, md_start_sync); |
| 9389 | queue_work(md_misc_wq, &mddev->del_work); |
| 9390 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9391 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9392 | not_running: |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9393 | if (!mddev->sync_thread) { |
| 9394 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9395 | wake_up(&resync_wait); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9396 | if (test_and_clear_bit(MD_RECOVERY_RECOVER, |
| 9397 | &mddev->recovery)) |
NeilBrown | 0c3573f | 2009-01-09 08:31:05 +1100 | [diff] [blame] | 9398 | if (mddev->sysfs_action) |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 9399 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Neil Brown | 72a23c2 | 2008-06-28 08:31:41 +1000 | [diff] [blame] | 9400 | } |
NeilBrown | ac05f25 | 2014-09-30 08:10:42 +1000 | [diff] [blame] | 9401 | unlock: |
| 9402 | wake_up(&mddev->sb_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9403 | mddev_unlock(mddev); |
| 9404 | } |
| 9405 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 9406 | EXPORT_SYMBOL(md_check_recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9407 | |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9408 | void md_reap_sync_thread(struct mddev *mddev) |
| 9409 | { |
| 9410 | struct md_rdev *rdev; |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9411 | sector_t old_dev_sectors = mddev->dev_sectors; |
| 9412 | bool is_reshaped = false; |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9413 | |
| 9414 | /* resync has finished, collect result */ |
| 9415 | md_unregister_thread(&mddev->sync_thread); |
| 9416 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) && |
Guoqing Jiang | 0d8ed0e9 | 2019-07-24 11:09:21 +0200 | [diff] [blame] | 9417 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 9418 | mddev->degraded != mddev->raid_disks) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9419 | /* success...*/ |
| 9420 | /* activate any spares */ |
| 9421 | if (mddev->pers->spare_active(mddev)) { |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9422 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9423 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9424 | } |
| 9425 | } |
| 9426 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9427 | mddev->pers->finish_reshape) { |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9428 | mddev->pers->finish_reshape(mddev); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9429 | if (mddev_is_clustered(mddev)) |
| 9430 | is_reshaped = true; |
| 9431 | } |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9432 | |
| 9433 | /* If array is no-longer degraded, then any saved_raid_disk |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 9434 | * information must be scrapped. |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9435 | */ |
NeilBrown | f466722 | 2013-12-09 12:04:56 +1100 | [diff] [blame] | 9436 | if (!mddev->degraded) |
| 9437 | rdev_for_each(rdev, mddev) |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9438 | rdev->saved_raid_disk = -1; |
| 9439 | |
| 9440 | md_update_sb(mddev, 1); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9441 | /* 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] | 9442 | * call resync_finish here if MD_CLUSTER_RESYNC_LOCKED is set by |
| 9443 | * clustered raid */ |
| 9444 | if (test_and_clear_bit(MD_CLUSTER_RESYNC_LOCKED, &mddev->flags)) |
| 9445 | md_cluster_ops->resync_finish(mddev); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9446 | clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 9447 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9448 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 9449 | clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 9450 | clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery); |
| 9451 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
Guoqing Jiang | aefb2e5 | 2018-10-18 16:37:44 +0800 | [diff] [blame] | 9452 | /* |
| 9453 | * We call md_cluster_ops->update_size here because sync_size could |
| 9454 | * be changed by md_update_sb, and MD_RECOVERY_RESHAPE is cleared, |
| 9455 | * so it is time to update size across cluster. |
| 9456 | */ |
| 9457 | if (mddev_is_clustered(mddev) && is_reshaped |
| 9458 | && !test_bit(MD_CLOSING, &mddev->flags)) |
| 9459 | md_cluster_ops->update_size(mddev, old_dev_sectors); |
NeilBrown | f851b60 | 2014-12-11 10:02:10 +1100 | [diff] [blame] | 9460 | wake_up(&resync_wait); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9461 | /* flag recovery needed just to double check */ |
| 9462 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9463 | sysfs_notify_dirent_safe(mddev->sysfs_action); |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 9464 | md_new_event(); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9465 | if (mddev->event_work.func) |
| 9466 | queue_work(md_misc_wq, &mddev->event_work); |
| 9467 | } |
NeilBrown | 6c144d3 | 2014-09-30 16:15:38 +1000 | [diff] [blame] | 9468 | EXPORT_SYMBOL(md_reap_sync_thread); |
Jonathan Brassow | a91d5ac | 2013-04-24 11:42:43 +1000 | [diff] [blame] | 9469 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9470 | 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] | 9471 | { |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 9472 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9473 | wait_event_timeout(rdev->blocked_wait, |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 9474 | !test_bit(Blocked, &rdev->flags) && |
| 9475 | !test_bit(BlockedBadBlocks, &rdev->flags), |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 9476 | msecs_to_jiffies(5000)); |
| 9477 | rdev_dec_pending(rdev, mddev); |
| 9478 | } |
| 9479 | EXPORT_SYMBOL(md_wait_for_blocked_rdev); |
| 9480 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9481 | void md_finish_reshape(struct mddev *mddev) |
| 9482 | { |
| 9483 | /* called be personality module when reshape completes. */ |
| 9484 | struct md_rdev *rdev; |
| 9485 | |
| 9486 | rdev_for_each(rdev, mddev) { |
| 9487 | if (rdev->data_offset > rdev->new_data_offset) |
| 9488 | rdev->sectors += rdev->data_offset - rdev->new_data_offset; |
| 9489 | else |
| 9490 | rdev->sectors -= rdev->new_data_offset - rdev->data_offset; |
| 9491 | rdev->data_offset = rdev->new_data_offset; |
| 9492 | } |
| 9493 | } |
| 9494 | EXPORT_SYMBOL(md_finish_reshape); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9495 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9496 | /* Bad block management */ |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9497 | |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9498 | /* Returns 1 on success, 0 on failure */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9499 | int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9500 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9501 | { |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 9502 | struct mddev *mddev = rdev->mddev; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9503 | int rv; |
| 9504 | if (is_new) |
| 9505 | s += rdev->new_data_offset; |
| 9506 | else |
| 9507 | s += rdev->data_offset; |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9508 | rv = badblocks_set(&rdev->badblocks, s, sectors, 0); |
| 9509 | if (rv == 0) { |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9510 | /* Make sure they get written out promptly */ |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9511 | if (test_bit(ExternalBbl, &rdev->flags)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9512 | sysfs_notify_dirent_safe(rdev->sysfs_unack_badblocks); |
NeilBrown | 8bd2f0a | 2011-12-08 16:26:08 +1100 | [diff] [blame] | 9513 | sysfs_notify_dirent_safe(rdev->sysfs_state); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 9514 | set_mask_bits(&mddev->sb_flags, 0, |
| 9515 | BIT(MD_SB_CHANGE_CLEAN) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9516 | md_wakeup_thread(rdev->mddev->thread); |
Vishal Verma | fc974ee | 2015-12-24 19:20:34 -0700 | [diff] [blame] | 9517 | return 1; |
| 9518 | } else |
| 9519 | return 0; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9520 | } |
| 9521 | EXPORT_SYMBOL_GPL(rdev_set_badblocks); |
| 9522 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9523 | int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors, |
| 9524 | int is_new) |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9525 | { |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9526 | int rv; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 9527 | if (is_new) |
| 9528 | s += rdev->new_data_offset; |
| 9529 | else |
| 9530 | s += rdev->data_offset; |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9531 | rv = badblocks_clear(&rdev->badblocks, s, sectors); |
| 9532 | if ((rv == 0) && test_bit(ExternalBbl, &rdev->flags)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9533 | sysfs_notify_dirent_safe(rdev->sysfs_badblocks); |
Tomasz Majchrzak | 35b785f | 2016-10-21 16:26:57 +0200 | [diff] [blame] | 9534 | return rv; |
NeilBrown | 2230dfe | 2011-07-28 11:31:46 +1000 | [diff] [blame] | 9535 | } |
| 9536 | EXPORT_SYMBOL_GPL(rdev_clear_badblocks); |
| 9537 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9538 | static int md_notify_reboot(struct notifier_block *this, |
| 9539 | unsigned long code, void *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9540 | { |
| 9541 | struct list_head *tmp; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9542 | struct mddev *mddev; |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9543 | int need_delay = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9544 | |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9545 | for_each_mddev(mddev, tmp) { |
| 9546 | if (mddev_trylock(mddev)) { |
NeilBrown | 30b8aa9 | 2012-04-24 10:23:16 +1000 | [diff] [blame] | 9547 | if (mddev->pers) |
| 9548 | __md_stop_writes(mddev); |
NeilBrown | 0f62fb2 | 2014-05-06 09:36:08 +1000 | [diff] [blame] | 9549 | if (mddev->persistent) |
| 9550 | mddev->safemode = 2; |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9551 | mddev_unlock(mddev); |
Daniel P. Berrange | 2dba6a9 | 2011-09-23 10:40:45 +0100 | [diff] [blame] | 9552 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9553 | need_delay = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9554 | } |
NeilBrown | c744a65 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 9555 | /* |
| 9556 | * certain more exotic SCSI devices are known to be |
| 9557 | * volatile wrt too early system reboots. While the |
| 9558 | * right place to handle this issue is the given |
| 9559 | * driver, we do want to have a safe RAID driver ... |
| 9560 | */ |
| 9561 | if (need_delay) |
| 9562 | mdelay(1000*1); |
| 9563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9564 | return NOTIFY_DONE; |
| 9565 | } |
| 9566 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9567 | static struct notifier_block md_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9568 | .notifier_call = md_notify_reboot, |
| 9569 | .next = NULL, |
| 9570 | .priority = INT_MAX, /* before any real devices */ |
| 9571 | }; |
| 9572 | |
| 9573 | static void md_geninit(void) |
| 9574 | { |
NeilBrown | 36a4e1f | 2011-10-07 14:23:17 +1100 | [diff] [blame] | 9575 | 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] | 9576 | |
Alexey Dobriyan | 97a3253 | 2020-02-03 17:37:17 -0800 | [diff] [blame] | 9577 | proc_create("mdstat", S_IRUGO, NULL, &mdstat_proc_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9578 | } |
| 9579 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 9580 | static int __init md_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9581 | { |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9582 | int ret = -ENOMEM; |
| 9583 | |
Tejun Heo | ada609e | 2011-01-25 14:35:54 +0100 | [diff] [blame] | 9584 | md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9585 | if (!md_wq) |
| 9586 | goto err_wq; |
| 9587 | |
| 9588 | md_misc_wq = alloc_workqueue("md_misc", 0, 0); |
| 9589 | if (!md_misc_wq) |
| 9590 | goto err_misc_wq; |
| 9591 | |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9592 | md_rdev_misc_wq = alloc_workqueue("md_rdev_misc", 0, 0); |
Guoqing Jiang | cf0b9b4 | 2020-10-08 05:19:09 +0200 | [diff] [blame] | 9593 | if (!md_rdev_misc_wq) |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9594 | goto err_rdev_misc_wq; |
| 9595 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 9596 | ret = __register_blkdev(MD_MAJOR, "md", md_probe); |
| 9597 | if (ret < 0) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9598 | goto err_md; |
| 9599 | |
Christoph Hellwig | 28144f9 | 2020-10-29 15:58:34 +0100 | [diff] [blame] | 9600 | ret = __register_blkdev(0, "mdp", md_probe); |
| 9601 | if (ret < 0) |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9602 | goto err_mdp; |
| 9603 | mdp_major = ret; |
| 9604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9605 | register_reboot_notifier(&md_notifier); |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 9606 | raid_table_header = register_sysctl_table(raid_root_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9607 | |
| 9608 | md_geninit(); |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 9609 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9610 | |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9611 | err_mdp: |
| 9612 | unregister_blkdev(MD_MAJOR, "md"); |
| 9613 | err_md: |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9614 | destroy_workqueue(md_rdev_misc_wq); |
| 9615 | err_rdev_misc_wq: |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9616 | destroy_workqueue(md_misc_wq); |
| 9617 | err_misc_wq: |
| 9618 | destroy_workqueue(md_wq); |
| 9619 | err_wq: |
| 9620 | return ret; |
| 9621 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9622 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9623 | static void check_sb_changes(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9624 | { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9625 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
Heming Zhao | f7c7a2f | 2021-04-08 15:44:15 +0800 | [diff] [blame] | 9626 | struct md_rdev *rdev2, *tmp; |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9627 | int role, ret; |
| 9628 | char b[BDEVNAME_SIZE]; |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9629 | |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 9630 | /* |
| 9631 | * If size is changed in another node then we need to |
| 9632 | * do resize as well. |
| 9633 | */ |
| 9634 | if (mddev->dev_sectors != le64_to_cpu(sb->size)) { |
| 9635 | ret = mddev->pers->resize(mddev, le64_to_cpu(sb->size)); |
| 9636 | if (ret) |
| 9637 | pr_info("md-cluster: resize failed\n"); |
| 9638 | else |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 9639 | md_bitmap_update_sb(mddev->bitmap); |
Guoqing Jiang | 818da59 | 2017-03-01 16:42:40 +0800 | [diff] [blame] | 9640 | } |
| 9641 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9642 | /* Check for change of roles in the active devices */ |
Heming Zhao | f7c7a2f | 2021-04-08 15:44:15 +0800 | [diff] [blame] | 9643 | rdev_for_each_safe(rdev2, tmp, mddev) { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9644 | if (test_bit(Faulty, &rdev2->flags)) |
| 9645 | continue; |
| 9646 | |
| 9647 | /* Check if the roles changed */ |
| 9648 | role = le16_to_cpu(sb->dev_roles[rdev2->desc_nr]); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 9649 | |
| 9650 | if (test_bit(Candidate, &rdev2->flags)) { |
| 9651 | if (role == 0xfffe) { |
| 9652 | pr_info("md: Removing Candidate device %s because add failed\n", bdevname(rdev2->bdev,b)); |
| 9653 | md_kick_rdev_from_array(rdev2); |
| 9654 | continue; |
| 9655 | } |
| 9656 | else |
| 9657 | clear_bit(Candidate, &rdev2->flags); |
| 9658 | } |
| 9659 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9660 | if (role != rdev2->raid_disk) { |
Guoqing Jiang | ca1e98e | 2018-10-18 16:37:45 +0800 | [diff] [blame] | 9661 | /* |
| 9662 | * got activated except reshape is happening. |
| 9663 | */ |
| 9664 | if (rdev2->raid_disk == -1 && role != 0xffff && |
| 9665 | !(le32_to_cpu(sb->feature_map) & |
| 9666 | MD_FEATURE_RESHAPE_ACTIVE)) { |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9667 | rdev2->saved_raid_disk = role; |
| 9668 | ret = remove_and_add_spares(mddev, rdev2); |
| 9669 | pr_info("Activated spare: %s\n", |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9670 | bdevname(rdev2->bdev,b)); |
Guoqing Jiang | a578183 | 2016-05-02 11:33:14 -0400 | [diff] [blame] | 9671 | /* wakeup mddev->thread here, so array could |
| 9672 | * perform resync with the new activated disk */ |
| 9673 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 9674 | md_wakeup_thread(mddev->thread); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9675 | } |
| 9676 | /* device faulty |
| 9677 | * We just want to do the minimum to mark the disk |
| 9678 | * as faulty. The recovery is performed by the |
| 9679 | * one who initiated the error. |
| 9680 | */ |
| 9681 | if ((role == 0xfffe) || (role == 0xfffd)) { |
| 9682 | md_error(mddev, rdev2); |
| 9683 | clear_bit(Blocked, &rdev2->flags); |
| 9684 | } |
| 9685 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9686 | } |
| 9687 | |
Zhao Heming | a8da01f | 2020-11-19 19:41:33 +0800 | [diff] [blame] | 9688 | if (mddev->raid_disks != le32_to_cpu(sb->raid_disks)) { |
| 9689 | ret = update_raid_disks(mddev, le32_to_cpu(sb->raid_disks)); |
| 9690 | if (ret) |
| 9691 | pr_warn("md: updating array disks failed. %d\n", ret); |
| 9692 | } |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9693 | |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 9694 | /* |
| 9695 | * Since mddev->delta_disks has already updated in update_raid_disks, |
| 9696 | * so it is time to check reshape. |
| 9697 | */ |
| 9698 | if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) && |
| 9699 | (le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 9700 | /* |
| 9701 | * reshape is happening in the remote node, we need to |
| 9702 | * update reshape_position and call start_reshape. |
| 9703 | */ |
Christoph Hellwig | ed4d0a4e | 2019-04-04 18:56:10 +0200 | [diff] [blame] | 9704 | mddev->reshape_position = le64_to_cpu(sb->reshape_position); |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 9705 | if (mddev->pers->update_reshape_pos) |
| 9706 | mddev->pers->update_reshape_pos(mddev); |
| 9707 | if (mddev->pers->start_reshape) |
| 9708 | mddev->pers->start_reshape(mddev); |
| 9709 | } else if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) && |
| 9710 | mddev->reshape_position != MaxSector && |
| 9711 | !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) { |
| 9712 | /* reshape is just done in another node. */ |
| 9713 | mddev->reshape_position = MaxSector; |
| 9714 | if (mddev->pers->update_reshape_pos) |
| 9715 | mddev->pers->update_reshape_pos(mddev); |
| 9716 | } |
| 9717 | |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9718 | /* Finally set the event to be up to date */ |
| 9719 | mddev->events = le64_to_cpu(sb->events); |
| 9720 | } |
| 9721 | |
| 9722 | static int read_rdev(struct mddev *mddev, struct md_rdev *rdev) |
| 9723 | { |
| 9724 | int err; |
| 9725 | struct page *swapout = rdev->sb_page; |
| 9726 | struct mdp_superblock_1 *sb; |
| 9727 | |
| 9728 | /* Store the sb page of the rdev in the swapout temporary |
| 9729 | * variable in case we err in the future |
| 9730 | */ |
| 9731 | rdev->sb_page = NULL; |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9732 | err = alloc_disk_sb(rdev); |
| 9733 | if (err == 0) { |
| 9734 | ClearPageUptodate(rdev->sb_page); |
| 9735 | rdev->sb_loaded = 0; |
| 9736 | err = super_types[mddev->major_version]. |
| 9737 | load_super(rdev, NULL, mddev->minor_version); |
| 9738 | } |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9739 | if (err < 0) { |
| 9740 | pr_warn("%s: %d Could not reload rdev(%d) err: %d. Restoring old values\n", |
| 9741 | __func__, __LINE__, rdev->desc_nr, err); |
NeilBrown | 7f0f0d8 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9742 | if (rdev->sb_page) |
| 9743 | put_page(rdev->sb_page); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9744 | rdev->sb_page = swapout; |
| 9745 | rdev->sb_loaded = 1; |
| 9746 | return err; |
| 9747 | } |
| 9748 | |
| 9749 | sb = page_address(rdev->sb_page); |
| 9750 | /* Read the offset unconditionally, even if MD_FEATURE_RECOVERY_OFFSET |
| 9751 | * is not set |
| 9752 | */ |
| 9753 | |
| 9754 | if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RECOVERY_OFFSET)) |
| 9755 | rdev->recovery_offset = le64_to_cpu(sb->recovery_offset); |
| 9756 | |
| 9757 | /* The other node finished recovery, call spare_active to set |
| 9758 | * device In_sync and mddev->degraded |
| 9759 | */ |
| 9760 | if (rdev->recovery_offset == MaxSector && |
| 9761 | !test_bit(In_sync, &rdev->flags) && |
| 9762 | mddev->pers->spare_active(mddev)) |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 9763 | sysfs_notify_dirent_safe(mddev->sysfs_degraded); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 9764 | |
| 9765 | put_page(swapout); |
| 9766 | return 0; |
| 9767 | } |
| 9768 | |
| 9769 | void md_reload_sb(struct mddev *mddev, int nr) |
| 9770 | { |
| 9771 | struct md_rdev *rdev; |
| 9772 | int err; |
| 9773 | |
| 9774 | /* Find the rdev */ |
| 9775 | rdev_for_each_rcu(rdev, mddev) { |
| 9776 | if (rdev->desc_nr == nr) |
| 9777 | break; |
| 9778 | } |
| 9779 | |
| 9780 | if (!rdev || rdev->desc_nr != nr) { |
| 9781 | pr_warn("%s: %d Could not find rdev with nr %d\n", __func__, __LINE__, nr); |
| 9782 | return; |
| 9783 | } |
| 9784 | |
| 9785 | err = read_rdev(mddev, rdev); |
| 9786 | if (err < 0) |
| 9787 | return; |
| 9788 | |
| 9789 | check_sb_changes(mddev, rdev); |
| 9790 | |
| 9791 | /* Read all rdev's to update recovery_offset */ |
Guoqing Jiang | 0ea9924 | 2018-04-09 17:01:21 +0800 | [diff] [blame] | 9792 | rdev_for_each_rcu(rdev, mddev) { |
| 9793 | if (!test_bit(Faulty, &rdev->flags)) |
| 9794 | read_rdev(mddev, rdev); |
| 9795 | } |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 9796 | } |
| 9797 | EXPORT_SYMBOL(md_reload_sb); |
| 9798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9799 | #ifndef MODULE |
| 9800 | |
| 9801 | /* |
| 9802 | * Searches all registered partitions for autorun RAID arrays |
| 9803 | * at boot time. |
| 9804 | */ |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9805 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9806 | static DEFINE_MUTEX(detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9807 | static LIST_HEAD(all_detected_devices); |
| 9808 | struct detected_devices_node { |
| 9809 | struct list_head list; |
| 9810 | dev_t dev; |
| 9811 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9812 | |
| 9813 | void md_autodetect_dev(dev_t dev) |
| 9814 | { |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9815 | struct detected_devices_node *node_detected_dev; |
| 9816 | |
| 9817 | node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL); |
| 9818 | if (node_detected_dev) { |
| 9819 | node_detected_dev->dev = dev; |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9820 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9821 | list_add_tail(&node_detected_dev->list, &all_detected_devices); |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9822 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9824 | } |
| 9825 | |
Christoph Hellwig | d82fa81 | 2020-06-06 15:00:24 +0200 | [diff] [blame] | 9826 | void md_autostart_arrays(int part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9827 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 9828 | struct md_rdev *rdev; |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9829 | struct detected_devices_node *node_detected_dev; |
| 9830 | dev_t dev; |
| 9831 | int i_scanned, i_passed; |
| 9832 | |
| 9833 | i_scanned = 0; |
| 9834 | i_passed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9835 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9836 | pr_info("md: Autodetecting RAID arrays.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9837 | |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9838 | mutex_lock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9839 | while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) { |
| 9840 | i_scanned++; |
| 9841 | node_detected_dev = list_entry(all_detected_devices.next, |
| 9842 | struct detected_devices_node, list); |
| 9843 | list_del(&node_detected_dev->list); |
| 9844 | dev = node_detected_dev->dev; |
| 9845 | kfree(node_detected_dev); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9846 | mutex_unlock(&detected_devices_mutex); |
NeilBrown | df968c4 | 2007-07-17 04:06:11 -0700 | [diff] [blame] | 9847 | rdev = md_import_device(dev,0, 90); |
Shaohua Li | 90bcf133 | 2016-09-14 14:26:54 -0700 | [diff] [blame] | 9848 | mutex_lock(&detected_devices_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9849 | if (IS_ERR(rdev)) |
| 9850 | continue; |
| 9851 | |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9852 | if (test_bit(Faulty, &rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9853 | continue; |
NeilBrown | 403df47 | 2014-09-30 15:52:29 +1000 | [diff] [blame] | 9854 | |
NeilBrown | d0fae18 | 2008-03-04 14:29:31 -0800 | [diff] [blame] | 9855 | set_bit(AutoDetected, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9856 | list_add(&rdev->same_set, &pending_raid_disks); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9857 | i_passed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9858 | } |
Cong Wang | 5b1f5bc3 | 2016-06-08 09:20:16 -0700 | [diff] [blame] | 9859 | mutex_unlock(&detected_devices_mutex); |
Michael J. Evans | 4d936ec | 2007-10-16 23:30:52 -0700 | [diff] [blame] | 9860 | |
NeilBrown | 9d48739 | 2016-11-02 14:16:49 +1100 | [diff] [blame] | 9861 | 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] | 9862 | |
| 9863 | autorun_devices(part); |
| 9864 | } |
| 9865 | |
Jeff Garzik | fdee8ae | 2006-12-10 02:20:50 -0800 | [diff] [blame] | 9866 | #endif /* !MODULE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9867 | |
| 9868 | static __exit void md_exit(void) |
| 9869 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 9870 | struct mddev *mddev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9871 | struct list_head *tmp; |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9872 | int delay = 1; |
Greg Kroah-Hartman | 8ab5e4c | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 9873 | |
Christoph Hellwig | 3dbd8c2 | 2009-03-31 14:27:02 +1100 | [diff] [blame] | 9874 | unregister_blkdev(MD_MAJOR,"md"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9875 | unregister_blkdev(mdp_major, "mdp"); |
| 9876 | unregister_reboot_notifier(&md_notifier); |
| 9877 | unregister_sysctl_table(raid_table_header); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9878 | |
| 9879 | /* We cannot unload the modules while some process is |
| 9880 | * waiting for us in select() or poll() - wake them up |
| 9881 | */ |
| 9882 | md_unloading = 1; |
| 9883 | while (waitqueue_active(&md_event_waiters)) { |
| 9884 | /* not safe to leave yet */ |
| 9885 | wake_up(&md_event_waiters); |
| 9886 | msleep(delay); |
| 9887 | delay += delay; |
| 9888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9889 | remove_proc_entry("mdstat", NULL); |
NeilBrown | e2f23b6 | 2014-04-09 14:33:51 +1000 | [diff] [blame] | 9890 | |
NeilBrown | 29ac4aa | 2008-02-06 01:39:58 -0800 | [diff] [blame] | 9891 | for_each_mddev(mddev, tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9892 | export_array(mddev); |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9893 | mddev->ctime = 0; |
NeilBrown | d337482 | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9894 | mddev->hold_active = 0; |
NeilBrown | 9356863 | 2017-02-06 13:41:39 +1100 | [diff] [blame] | 9895 | /* |
| 9896 | * for_each_mddev() will call mddev_put() at the end of each |
| 9897 | * iteration. As the mddev is now fully clear, this will |
| 9898 | * schedule the mddev for destruction by a workqueue, and the |
| 9899 | * destroy_workqueue() below will wait for that to complete. |
| 9900 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9901 | } |
Guoqing Jiang | cc1ffe6 | 2020-04-04 23:57:08 +0200 | [diff] [blame] | 9902 | destroy_workqueue(md_rdev_misc_wq); |
Tejun Heo | e804ac7 | 2010-10-15 15:36:08 +0200 | [diff] [blame] | 9903 | destroy_workqueue(md_misc_wq); |
| 9904 | destroy_workqueue(md_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9905 | } |
| 9906 | |
Dan Williams | 685784a | 2007-07-09 11:56:42 -0700 | [diff] [blame] | 9907 | subsys_initcall(md_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9908 | module_exit(md_exit) |
| 9909 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9910 | static int get_ro(char *buffer, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9911 | { |
Xiongfeng Wang | 3f99980 | 2020-05-11 16:23:25 +0800 | [diff] [blame] | 9912 | return sprintf(buffer, "%d\n", start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9913 | } |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 9914 | static int set_ro(const char *val, const struct kernel_param *kp) |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9915 | { |
Alexey Dobriyan | 4c9309c | 2015-05-16 14:02:38 +0300 | [diff] [blame] | 9916 | return kstrtouint(val, 10, (unsigned int *)&start_readonly); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9917 | } |
| 9918 | |
NeilBrown | 80ca3a4 | 2006-07-10 04:44:18 -0700 | [diff] [blame] | 9919 | module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR); |
| 9920 | module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR); |
NeilBrown | efeb53c | 2009-01-09 08:31:10 +1100 | [diff] [blame] | 9921 | module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR); |
NeilBrown | 78b6350 | 2017-04-12 16:26:13 +1000 | [diff] [blame] | 9922 | module_param(create_on_open, bool, S_IRUSR|S_IWUSR); |
NeilBrown | f91de92 | 2005-11-08 21:39:36 -0800 | [diff] [blame] | 9923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9924 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 9925 | MODULE_DESCRIPTION("MD RAID framework"); |
NeilBrown | aa1595e | 2005-08-04 12:53:32 -0700 | [diff] [blame] | 9926 | MODULE_ALIAS("md"); |
NeilBrown | 7200865 | 2005-08-26 18:34:15 -0700 | [diff] [blame] | 9927 | MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR); |