Goldwyn Rodrigues | 8e854e9 | 2014-03-07 11:21:15 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015, SUSE |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | |
| 12 | #include <linux/module.h> |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 13 | #include <linux/dlm.h> |
| 14 | #include <linux/sched.h> |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 15 | #include <linux/raid/md_p.h> |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 16 | #include "md.h" |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 17 | #include "bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 18 | #include "md-cluster.h" |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 19 | |
| 20 | #define LVB_SIZE 64 |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 21 | #define NEW_DEV_TIMEOUT 5000 |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 22 | |
| 23 | struct dlm_lock_resource { |
| 24 | dlm_lockspace_t *ls; |
| 25 | struct dlm_lksb lksb; |
| 26 | char *name; /* lock name. */ |
| 27 | uint32_t flags; /* flags to pass to dlm_lock() */ |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 28 | struct completion completion; /* completion for synchronized locking */ |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 29 | void (*bast)(void *arg, int mode); /* blocking AST function pointer*/ |
| 30 | struct mddev *mddev; /* pointing back to mddev. */ |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 31 | int mode; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 32 | }; |
| 33 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 34 | struct suspend_info { |
| 35 | int slot; |
| 36 | sector_t lo; |
| 37 | sector_t hi; |
| 38 | struct list_head list; |
| 39 | }; |
| 40 | |
| 41 | struct resync_info { |
| 42 | __le64 lo; |
| 43 | __le64 hi; |
| 44 | }; |
| 45 | |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 46 | /* md_cluster_info flags */ |
| 47 | #define MD_CLUSTER_WAITING_FOR_NEWDISK 1 |
Goldwyn Rodrigues | 90382ed | 2015-06-24 09:30:32 -0500 | [diff] [blame] | 48 | #define MD_CLUSTER_SUSPEND_READ_BALANCING 2 |
Guoqing Jiang | eece075 | 2015-07-10 17:01:21 +0800 | [diff] [blame] | 49 | #define MD_CLUSTER_BEGIN_JOIN_CLUSTER 3 |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 50 | |
| 51 | |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 52 | struct md_cluster_info { |
| 53 | /* dlm lock space and resources for clustered raid. */ |
| 54 | dlm_lockspace_t *lockspace; |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 55 | int slot_number; |
| 56 | struct completion completion; |
Goldwyn Rodrigues | 54519c5 | 2014-06-06 12:12:32 -0500 | [diff] [blame] | 57 | struct dlm_lock_resource *bitmap_lockres; |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame^] | 58 | struct dlm_lock_resource **other_bitmap_lockres; |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 59 | struct dlm_lock_resource *resync_lockres; |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 60 | struct list_head suspend_list; |
| 61 | spinlock_t suspend_lock; |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 62 | struct md_thread *recovery_thread; |
| 63 | unsigned long recovery_map; |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 64 | /* communication loc resources */ |
| 65 | struct dlm_lock_resource *ack_lockres; |
| 66 | struct dlm_lock_resource *message_lockres; |
| 67 | struct dlm_lock_resource *token_lockres; |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 68 | struct dlm_lock_resource *no_new_dev_lockres; |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 69 | struct md_thread *recv_thread; |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 70 | struct completion newdisk_completion; |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 71 | unsigned long state; |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | enum msg_type { |
| 75 | METADATA_UPDATED = 0, |
| 76 | RESYNCING, |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 77 | NEWDISK, |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 78 | REMOVE, |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 79 | RE_ADD, |
Guoqing Jiang | dc737d7 | 2015-07-10 16:54:04 +0800 | [diff] [blame] | 80 | BITMAP_NEEDS_SYNC, |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | struct cluster_msg { |
Guoqing Jiang | cf97a34 | 2015-10-16 15:40:22 +0800 | [diff] [blame] | 84 | __le32 type; |
| 85 | __le32 slot; |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 86 | /* TODO: Unionize this for smaller footprint */ |
Guoqing Jiang | cf97a34 | 2015-10-16 15:40:22 +0800 | [diff] [blame] | 87 | __le64 low; |
| 88 | __le64 high; |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 89 | char uuid[16]; |
Guoqing Jiang | cf97a34 | 2015-10-16 15:40:22 +0800 | [diff] [blame] | 90 | __le32 raid_slot; |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static void sync_ast(void *arg) |
| 94 | { |
| 95 | struct dlm_lock_resource *res; |
| 96 | |
NeilBrown | 2e2a7cd | 2015-10-19 15:42:18 +1100 | [diff] [blame] | 97 | res = arg; |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 98 | complete(&res->completion); |
| 99 | } |
| 100 | |
| 101 | static int dlm_lock_sync(struct dlm_lock_resource *res, int mode) |
| 102 | { |
| 103 | int ret = 0; |
| 104 | |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 105 | ret = dlm_lock(res->ls, mode, &res->lksb, |
| 106 | res->flags, res->name, strlen(res->name), |
| 107 | 0, sync_ast, res, res->bast); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | wait_for_completion(&res->completion); |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 111 | if (res->lksb.sb_status == 0) |
| 112 | res->mode = mode; |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 113 | return res->lksb.sb_status; |
| 114 | } |
| 115 | |
| 116 | static int dlm_unlock_sync(struct dlm_lock_resource *res) |
| 117 | { |
| 118 | return dlm_lock_sync(res, DLM_LOCK_NL); |
| 119 | } |
| 120 | |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 121 | static struct dlm_lock_resource *lockres_init(struct mddev *mddev, |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 122 | char *name, void (*bastfn)(void *arg, int mode), int with_lvb) |
| 123 | { |
| 124 | struct dlm_lock_resource *res = NULL; |
| 125 | int ret, namelen; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 126 | struct md_cluster_info *cinfo = mddev->cluster_info; |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 127 | |
| 128 | res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL); |
| 129 | if (!res) |
| 130 | return NULL; |
Guoqing Jiang | b83d51c | 2015-07-10 17:01:16 +0800 | [diff] [blame] | 131 | init_completion(&res->completion); |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 132 | res->ls = cinfo->lockspace; |
| 133 | res->mddev = mddev; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 134 | res->mode = DLM_LOCK_IV; |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 135 | namelen = strlen(name); |
| 136 | res->name = kzalloc(namelen + 1, GFP_KERNEL); |
| 137 | if (!res->name) { |
| 138 | pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name); |
| 139 | goto out_err; |
| 140 | } |
| 141 | strlcpy(res->name, name, namelen + 1); |
| 142 | if (with_lvb) { |
| 143 | res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL); |
| 144 | if (!res->lksb.sb_lvbptr) { |
| 145 | pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name); |
| 146 | goto out_err; |
| 147 | } |
| 148 | res->flags = DLM_LKF_VALBLK; |
| 149 | } |
| 150 | |
| 151 | if (bastfn) |
| 152 | res->bast = bastfn; |
| 153 | |
| 154 | res->flags |= DLM_LKF_EXPEDITE; |
| 155 | |
| 156 | ret = dlm_lock_sync(res, DLM_LOCK_NL); |
| 157 | if (ret) { |
| 158 | pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name); |
| 159 | goto out_err; |
| 160 | } |
| 161 | res->flags &= ~DLM_LKF_EXPEDITE; |
| 162 | res->flags |= DLM_LKF_CONVERT; |
| 163 | |
| 164 | return res; |
| 165 | out_err: |
| 166 | kfree(res->lksb.sb_lvbptr); |
| 167 | kfree(res->name); |
| 168 | kfree(res); |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | static void lockres_free(struct dlm_lock_resource *res) |
| 173 | { |
Guoqing Jiang | b5ef567 | 2015-07-10 17:01:17 +0800 | [diff] [blame] | 174 | int ret; |
| 175 | |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 176 | if (!res) |
| 177 | return; |
| 178 | |
Guoqing Jiang | b5ef567 | 2015-07-10 17:01:17 +0800 | [diff] [blame] | 179 | /* cancel a lock request or a conversion request that is blocked */ |
| 180 | res->flags |= DLM_LKF_CANCEL; |
| 181 | retry: |
| 182 | ret = dlm_unlock(res->ls, res->lksb.sb_lkid, 0, &res->lksb, res); |
| 183 | if (unlikely(ret != 0)) { |
| 184 | pr_info("%s: failed to unlock %s return %d\n", __func__, res->name, ret); |
| 185 | |
| 186 | /* if a lock conversion is cancelled, then the lock is put |
| 187 | * back to grant queue, need to ensure it is unlocked */ |
| 188 | if (ret == -DLM_ECANCEL) |
| 189 | goto retry; |
| 190 | } |
| 191 | res->flags &= ~DLM_LKF_CANCEL; |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 192 | wait_for_completion(&res->completion); |
| 193 | |
| 194 | kfree(res->name); |
| 195 | kfree(res->lksb.sb_lvbptr); |
| 196 | kfree(res); |
| 197 | } |
Goldwyn Rodrigues | 8e854e9 | 2014-03-07 11:21:15 -0600 | [diff] [blame] | 198 | |
NeilBrown | 30661b4 | 2015-10-19 15:44:00 +1100 | [diff] [blame] | 199 | static void add_resync_info(struct dlm_lock_resource *lockres, |
| 200 | sector_t lo, sector_t hi) |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 201 | { |
| 202 | struct resync_info *ri; |
| 203 | |
| 204 | ri = (struct resync_info *)lockres->lksb.sb_lvbptr; |
| 205 | ri->lo = cpu_to_le64(lo); |
| 206 | ri->hi = cpu_to_le64(hi); |
| 207 | } |
| 208 | |
| 209 | static struct suspend_info *read_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres) |
| 210 | { |
| 211 | struct resync_info ri; |
| 212 | struct suspend_info *s = NULL; |
| 213 | sector_t hi = 0; |
| 214 | |
| 215 | dlm_lock_sync(lockres, DLM_LOCK_CR); |
| 216 | memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info)); |
| 217 | hi = le64_to_cpu(ri.hi); |
Guoqing Jiang | cf97a34 | 2015-10-16 15:40:22 +0800 | [diff] [blame] | 218 | if (hi > 0) { |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 219 | s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL); |
| 220 | if (!s) |
| 221 | goto out; |
| 222 | s->hi = hi; |
| 223 | s->lo = le64_to_cpu(ri.lo); |
| 224 | } |
| 225 | dlm_unlock_sync(lockres); |
| 226 | out: |
| 227 | return s; |
| 228 | } |
| 229 | |
kbuild test robot | 6dc69c9 | 2015-02-28 07:04:37 +0800 | [diff] [blame] | 230 | static void recover_bitmaps(struct md_thread *thread) |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 231 | { |
| 232 | struct mddev *mddev = thread->mddev; |
| 233 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 234 | struct dlm_lock_resource *bm_lockres; |
| 235 | char str[64]; |
| 236 | int slot, ret; |
| 237 | struct suspend_info *s, *tmp; |
| 238 | sector_t lo, hi; |
| 239 | |
| 240 | while (cinfo->recovery_map) { |
| 241 | slot = fls64((u64)cinfo->recovery_map) - 1; |
| 242 | |
| 243 | /* Clear suspend_area associated with the bitmap */ |
| 244 | spin_lock_irq(&cinfo->suspend_lock); |
| 245 | list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list) |
| 246 | if (slot == s->slot) { |
| 247 | list_del(&s->list); |
| 248 | kfree(s); |
| 249 | } |
| 250 | spin_unlock_irq(&cinfo->suspend_lock); |
| 251 | |
| 252 | snprintf(str, 64, "bitmap%04d", slot); |
| 253 | bm_lockres = lockres_init(mddev, str, NULL, 1); |
| 254 | if (!bm_lockres) { |
| 255 | pr_err("md-cluster: Cannot initialize bitmaps\n"); |
| 256 | goto clear_bit; |
| 257 | } |
| 258 | |
| 259 | ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW); |
| 260 | if (ret) { |
| 261 | pr_err("md-cluster: Could not DLM lock %s: %d\n", |
| 262 | str, ret); |
| 263 | goto clear_bit; |
| 264 | } |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 265 | ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi, true); |
Goldwyn Rodrigues | 4b26a08 | 2014-06-07 00:52:29 -0500 | [diff] [blame] | 266 | if (ret) { |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 267 | pr_err("md-cluster: Could not copy data from bitmap %d\n", slot); |
Goldwyn Rodrigues | 4b26a08 | 2014-06-07 00:52:29 -0500 | [diff] [blame] | 268 | goto dlm_unlock; |
| 269 | } |
| 270 | if (hi > 0) { |
| 271 | /* TODO:Wait for current resync to get over */ |
| 272 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 273 | if (lo < mddev->recovery_cp) |
| 274 | mddev->recovery_cp = lo; |
| 275 | md_check_recovery(mddev); |
| 276 | } |
| 277 | dlm_unlock: |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 278 | dlm_unlock_sync(bm_lockres); |
| 279 | clear_bit: |
| 280 | clear_bit(slot, &cinfo->recovery_map); |
| 281 | } |
| 282 | } |
| 283 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 284 | static void recover_prep(void *arg) |
| 285 | { |
Goldwyn Rodrigues | 90382ed | 2015-06-24 09:30:32 -0500 | [diff] [blame] | 286 | struct mddev *mddev = arg; |
| 287 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 288 | set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Guoqing Jiang | 05cd0e5 | 2015-07-10 16:54:03 +0800 | [diff] [blame] | 291 | static void __recover_slot(struct mddev *mddev, int slot) |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 292 | { |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 293 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 294 | |
Guoqing Jiang | 05cd0e5 | 2015-07-10 16:54:03 +0800 | [diff] [blame] | 295 | set_bit(slot, &cinfo->recovery_map); |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 296 | if (!cinfo->recovery_thread) { |
| 297 | cinfo->recovery_thread = md_register_thread(recover_bitmaps, |
| 298 | mddev, "recover"); |
| 299 | if (!cinfo->recovery_thread) { |
| 300 | pr_warn("md-cluster: Could not create recovery thread\n"); |
| 301 | return; |
| 302 | } |
| 303 | } |
| 304 | md_wakeup_thread(cinfo->recovery_thread); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Guoqing Jiang | 05cd0e5 | 2015-07-10 16:54:03 +0800 | [diff] [blame] | 307 | static void recover_slot(void *arg, struct dlm_slot *slot) |
| 308 | { |
| 309 | struct mddev *mddev = arg; |
| 310 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 311 | |
| 312 | pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n", |
| 313 | mddev->bitmap_info.cluster_name, |
| 314 | slot->nodeid, slot->slot, |
| 315 | cinfo->slot_number); |
| 316 | /* deduct one since dlm slot starts from one while the num of |
| 317 | * cluster-md begins with 0 */ |
| 318 | __recover_slot(mddev, slot->slot - 1); |
| 319 | } |
| 320 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 321 | static void recover_done(void *arg, struct dlm_slot *slots, |
| 322 | int num_slots, int our_slot, |
| 323 | uint32_t generation) |
| 324 | { |
| 325 | struct mddev *mddev = arg; |
| 326 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 327 | |
| 328 | cinfo->slot_number = our_slot; |
Guoqing Jiang | eece075 | 2015-07-10 17:01:21 +0800 | [diff] [blame] | 329 | /* completion is only need to be complete when node join cluster, |
| 330 | * it doesn't need to run during another node's failure */ |
| 331 | if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) { |
| 332 | complete(&cinfo->completion); |
| 333 | clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state); |
| 334 | } |
Goldwyn Rodrigues | 90382ed | 2015-06-24 09:30:32 -0500 | [diff] [blame] | 335 | clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 336 | } |
| 337 | |
Guoqing Jiang | eece075 | 2015-07-10 17:01:21 +0800 | [diff] [blame] | 338 | /* the ops is called when node join the cluster, and do lock recovery |
| 339 | * if node failure occurs */ |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 340 | static const struct dlm_lockspace_ops md_ls_ops = { |
| 341 | .recover_prep = recover_prep, |
| 342 | .recover_slot = recover_slot, |
| 343 | .recover_done = recover_done, |
| 344 | }; |
| 345 | |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 346 | /* |
| 347 | * The BAST function for the ack lock resource |
| 348 | * This function wakes up the receive thread in |
| 349 | * order to receive and process the message. |
| 350 | */ |
| 351 | static void ack_bast(void *arg, int mode) |
| 352 | { |
NeilBrown | 2e2a7cd | 2015-10-19 15:42:18 +1100 | [diff] [blame] | 353 | struct dlm_lock_resource *res = arg; |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 354 | struct md_cluster_info *cinfo = res->mddev->cluster_info; |
| 355 | |
| 356 | if (mode == DLM_LOCK_EX) |
| 357 | md_wakeup_thread(cinfo->recv_thread); |
| 358 | } |
| 359 | |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame] | 360 | static void __remove_suspend_info(struct md_cluster_info *cinfo, int slot) |
| 361 | { |
| 362 | struct suspend_info *s, *tmp; |
| 363 | |
| 364 | list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list) |
| 365 | if (slot == s->slot) { |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame] | 366 | list_del(&s->list); |
| 367 | kfree(s); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | |
Goldwyn Rodrigues | b8ca846 | 2015-10-09 11:27:01 -0500 | [diff] [blame] | 372 | static void remove_suspend_info(struct mddev *mddev, int slot) |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame] | 373 | { |
Goldwyn Rodrigues | b8ca846 | 2015-10-09 11:27:01 -0500 | [diff] [blame] | 374 | struct md_cluster_info *cinfo = mddev->cluster_info; |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame] | 375 | spin_lock_irq(&cinfo->suspend_lock); |
| 376 | __remove_suspend_info(cinfo, slot); |
| 377 | spin_unlock_irq(&cinfo->suspend_lock); |
Goldwyn Rodrigues | b8ca846 | 2015-10-09 11:27:01 -0500 | [diff] [blame] | 378 | mddev->pers->quiesce(mddev, 2); |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | |
Goldwyn Rodrigues | 9ed38ff5 | 2015-08-14 12:19:40 -0500 | [diff] [blame] | 382 | static void process_suspend_info(struct mddev *mddev, |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame] | 383 | int slot, sector_t lo, sector_t hi) |
| 384 | { |
Goldwyn Rodrigues | 9ed38ff5 | 2015-08-14 12:19:40 -0500 | [diff] [blame] | 385 | struct md_cluster_info *cinfo = mddev->cluster_info; |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame] | 386 | struct suspend_info *s; |
| 387 | |
| 388 | if (!hi) { |
Goldwyn Rodrigues | b8ca846 | 2015-10-09 11:27:01 -0500 | [diff] [blame] | 389 | remove_suspend_info(mddev, slot); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 390 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 391 | md_wakeup_thread(mddev->thread); |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL); |
| 395 | if (!s) |
| 396 | return; |
| 397 | s->slot = slot; |
| 398 | s->lo = lo; |
| 399 | s->hi = hi; |
Goldwyn Rodrigues | 9ed38ff5 | 2015-08-14 12:19:40 -0500 | [diff] [blame] | 400 | mddev->pers->quiesce(mddev, 1); |
| 401 | mddev->pers->quiesce(mddev, 0); |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame] | 402 | spin_lock_irq(&cinfo->suspend_lock); |
| 403 | /* Remove existing entry (if exists) before adding */ |
| 404 | __remove_suspend_info(cinfo, slot); |
| 405 | list_add(&s->list, &cinfo->suspend_list); |
| 406 | spin_unlock_irq(&cinfo->suspend_lock); |
Goldwyn Rodrigues | b8ca846 | 2015-10-09 11:27:01 -0500 | [diff] [blame] | 407 | mddev->pers->quiesce(mddev, 2); |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 410 | static void process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg) |
| 411 | { |
| 412 | char disk_uuid[64]; |
| 413 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 414 | char event_name[] = "EVENT=ADD_DEVICE"; |
| 415 | char raid_slot[16]; |
| 416 | char *envp[] = {event_name, disk_uuid, raid_slot, NULL}; |
| 417 | int len; |
| 418 | |
| 419 | len = snprintf(disk_uuid, 64, "DEVICE_UUID="); |
Guoqing Jiang | b89f704 | 2015-07-10 16:54:02 +0800 | [diff] [blame] | 420 | sprintf(disk_uuid + len, "%pU", cmsg->uuid); |
Guoqing Jiang | faeff83 | 2015-10-12 17:21:21 +0800 | [diff] [blame] | 421 | snprintf(raid_slot, 16, "RAID_DISK=%d", le32_to_cpu(cmsg->raid_slot)); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 422 | pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot); |
| 423 | init_completion(&cinfo->newdisk_completion); |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 424 | set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 425 | kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp); |
| 426 | wait_for_completion_timeout(&cinfo->newdisk_completion, |
| 427 | NEW_DEV_TIMEOUT); |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 428 | clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
| 432 | static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg) |
| 433 | { |
| 434 | struct md_cluster_info *cinfo = mddev->cluster_info; |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 435 | md_reload_sb(mddev, le32_to_cpu(msg->raid_slot)); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 436 | dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR); |
| 437 | } |
| 438 | |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 439 | static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg) |
| 440 | { |
Guoqing Jiang | faeff83 | 2015-10-12 17:21:21 +0800 | [diff] [blame] | 441 | struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev, |
| 442 | le32_to_cpu(msg->raid_slot)); |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 443 | |
Guoqing Jiang | 659b254 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 444 | if (rdev) { |
| 445 | set_bit(ClusterRemove, &rdev->flags); |
| 446 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 447 | md_wakeup_thread(mddev->thread); |
| 448 | } |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 449 | else |
Guoqing Jiang | faeff83 | 2015-10-12 17:21:21 +0800 | [diff] [blame] | 450 | pr_warn("%s: %d Could not find disk(%d) to REMOVE\n", |
| 451 | __func__, __LINE__, le32_to_cpu(msg->raid_slot)); |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 452 | } |
| 453 | |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 454 | static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg) |
| 455 | { |
Guoqing Jiang | faeff83 | 2015-10-12 17:21:21 +0800 | [diff] [blame] | 456 | struct md_rdev *rdev = md_find_rdev_nr_rcu(mddev, |
| 457 | le32_to_cpu(msg->raid_slot)); |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 458 | |
| 459 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 460 | clear_bit(Faulty, &rdev->flags); |
| 461 | else |
Guoqing Jiang | faeff83 | 2015-10-12 17:21:21 +0800 | [diff] [blame] | 462 | pr_warn("%s: %d Could not find disk(%d) which is faulty", |
| 463 | __func__, __LINE__, le32_to_cpu(msg->raid_slot)); |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 464 | } |
| 465 | |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 466 | static void process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg) |
| 467 | { |
Guoqing Jiang | 256f5b2 | 2015-10-12 17:21:23 +0800 | [diff] [blame] | 468 | if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot), |
| 469 | "node %d received it's own msg\n", le32_to_cpu(msg->slot))) |
| 470 | return; |
Guoqing Jiang | cf97a34 | 2015-10-16 15:40:22 +0800 | [diff] [blame] | 471 | switch (le32_to_cpu(msg->type)) { |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 472 | case METADATA_UPDATED: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 473 | process_metadata_update(mddev, msg); |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 474 | break; |
| 475 | case RESYNCING: |
Guoqing Jiang | cf97a34 | 2015-10-16 15:40:22 +0800 | [diff] [blame] | 476 | process_suspend_info(mddev, le32_to_cpu(msg->slot), |
| 477 | le64_to_cpu(msg->low), |
| 478 | le64_to_cpu(msg->high)); |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 479 | break; |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 480 | case NEWDISK: |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 481 | process_add_new_disk(mddev, msg); |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 482 | break; |
| 483 | case REMOVE: |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 484 | process_remove_disk(mddev, msg); |
| 485 | break; |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 486 | case RE_ADD: |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 487 | process_readd_disk(mddev, msg); |
| 488 | break; |
Guoqing Jiang | dc737d7 | 2015-07-10 16:54:04 +0800 | [diff] [blame] | 489 | case BITMAP_NEEDS_SYNC: |
Guoqing Jiang | cf97a34 | 2015-10-16 15:40:22 +0800 | [diff] [blame] | 490 | __recover_slot(mddev, le32_to_cpu(msg->slot)); |
Guoqing Jiang | dc737d7 | 2015-07-10 16:54:04 +0800 | [diff] [blame] | 491 | break; |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 492 | default: |
| 493 | pr_warn("%s:%d Received unknown message from %d\n", |
| 494 | __func__, __LINE__, msg->slot); |
kbuild test robot | 09dd1af | 2015-02-28 09:16:08 +0800 | [diff] [blame] | 495 | } |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /* |
| 499 | * thread for receiving message |
| 500 | */ |
| 501 | static void recv_daemon(struct md_thread *thread) |
| 502 | { |
| 503 | struct md_cluster_info *cinfo = thread->mddev->cluster_info; |
| 504 | struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres; |
| 505 | struct dlm_lock_resource *message_lockres = cinfo->message_lockres; |
| 506 | struct cluster_msg msg; |
Guoqing Jiang | b5ef567 | 2015-07-10 17:01:17 +0800 | [diff] [blame] | 507 | int ret; |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 508 | |
| 509 | /*get CR on Message*/ |
| 510 | if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) { |
| 511 | pr_err("md/raid1:failed to get CR on MESSAGE\n"); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | /* read lvb and wake up thread to process this message_lockres */ |
| 516 | memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg)); |
| 517 | process_recvd_msg(thread->mddev, &msg); |
| 518 | |
| 519 | /*release CR on ack_lockres*/ |
Guoqing Jiang | b5ef567 | 2015-07-10 17:01:17 +0800 | [diff] [blame] | 520 | ret = dlm_unlock_sync(ack_lockres); |
| 521 | if (unlikely(ret != 0)) |
| 522 | pr_info("unlock ack failed return %d\n", ret); |
Guoqing Jiang | 66099bb | 2015-07-10 17:01:15 +0800 | [diff] [blame] | 523 | /*up-convert to PR on message_lockres*/ |
Guoqing Jiang | b5ef567 | 2015-07-10 17:01:17 +0800 | [diff] [blame] | 524 | ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR); |
| 525 | if (unlikely(ret != 0)) |
| 526 | pr_info("lock PR on msg failed return %d\n", ret); |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 527 | /*get CR on ack_lockres again*/ |
Guoqing Jiang | b5ef567 | 2015-07-10 17:01:17 +0800 | [diff] [blame] | 528 | ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR); |
| 529 | if (unlikely(ret != 0)) |
| 530 | pr_info("lock CR on ack failed return %d\n", ret); |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 531 | /*release CR on message_lockres*/ |
Guoqing Jiang | b5ef567 | 2015-07-10 17:01:17 +0800 | [diff] [blame] | 532 | ret = dlm_unlock_sync(message_lockres); |
| 533 | if (unlikely(ret != 0)) |
| 534 | pr_info("unlock msg failed return %d\n", ret); |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 535 | } |
| 536 | |
Goldwyn Rodrigues | 601b515 | 2014-06-07 01:28:53 -0500 | [diff] [blame] | 537 | /* lock_comm() |
| 538 | * Takes the lock on the TOKEN lock resource so no other |
| 539 | * node can communicate while the operation is underway. |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 540 | * If called again, and the TOKEN lock is alread in EX mode |
| 541 | * return success. However, care must be taken that unlock_comm() |
| 542 | * is called only once. |
Goldwyn Rodrigues | 601b515 | 2014-06-07 01:28:53 -0500 | [diff] [blame] | 543 | */ |
| 544 | static int lock_comm(struct md_cluster_info *cinfo) |
| 545 | { |
| 546 | int error; |
| 547 | |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 548 | if (cinfo->token_lockres->mode == DLM_LOCK_EX) |
| 549 | return 0; |
| 550 | |
Goldwyn Rodrigues | 601b515 | 2014-06-07 01:28:53 -0500 | [diff] [blame] | 551 | error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX); |
| 552 | if (error) |
| 553 | pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n", |
| 554 | __func__, __LINE__, error); |
| 555 | return error; |
| 556 | } |
| 557 | |
| 558 | static void unlock_comm(struct md_cluster_info *cinfo) |
| 559 | { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 560 | WARN_ON(cinfo->token_lockres->mode != DLM_LOCK_EX); |
Goldwyn Rodrigues | 601b515 | 2014-06-07 01:28:53 -0500 | [diff] [blame] | 561 | dlm_unlock_sync(cinfo->token_lockres); |
| 562 | } |
| 563 | |
| 564 | /* __sendmsg() |
| 565 | * This function performs the actual sending of the message. This function is |
| 566 | * usually called after performing the encompassing operation |
| 567 | * The function: |
| 568 | * 1. Grabs the message lockresource in EX mode |
| 569 | * 2. Copies the message to the message LVB |
Guoqing Jiang | 66099bb | 2015-07-10 17:01:15 +0800 | [diff] [blame] | 570 | * 3. Downconverts message lockresource to CW |
Goldwyn Rodrigues | 601b515 | 2014-06-07 01:28:53 -0500 | [diff] [blame] | 571 | * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes |
| 572 | * and the other nodes read the message. The thread will wait here until all other |
| 573 | * nodes have released ack lock resource. |
| 574 | * 5. Downconvert ack lockresource to CR |
| 575 | */ |
| 576 | static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg) |
| 577 | { |
| 578 | int error; |
| 579 | int slot = cinfo->slot_number - 1; |
| 580 | |
| 581 | cmsg->slot = cpu_to_le32(slot); |
| 582 | /*get EX on Message*/ |
| 583 | error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX); |
| 584 | if (error) { |
| 585 | pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error); |
| 586 | goto failed_message; |
| 587 | } |
| 588 | |
| 589 | memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg, |
| 590 | sizeof(struct cluster_msg)); |
Guoqing Jiang | 66099bb | 2015-07-10 17:01:15 +0800 | [diff] [blame] | 591 | /*down-convert EX to CW on Message*/ |
| 592 | error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW); |
Goldwyn Rodrigues | 601b515 | 2014-06-07 01:28:53 -0500 | [diff] [blame] | 593 | if (error) { |
Guoqing Jiang | 66099bb | 2015-07-10 17:01:15 +0800 | [diff] [blame] | 594 | pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n", |
Goldwyn Rodrigues | 601b515 | 2014-06-07 01:28:53 -0500 | [diff] [blame] | 595 | error); |
Guoqing Jiang | 66099bb | 2015-07-10 17:01:15 +0800 | [diff] [blame] | 596 | goto failed_ack; |
Goldwyn Rodrigues | 601b515 | 2014-06-07 01:28:53 -0500 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /*up-convert CR to EX on Ack*/ |
| 600 | error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX); |
| 601 | if (error) { |
| 602 | pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n", |
| 603 | error); |
| 604 | goto failed_ack; |
| 605 | } |
| 606 | |
| 607 | /*down-convert EX to CR on Ack*/ |
| 608 | error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR); |
| 609 | if (error) { |
| 610 | pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n", |
| 611 | error); |
| 612 | goto failed_ack; |
| 613 | } |
| 614 | |
| 615 | failed_ack: |
Guoqing Jiang | b5ef567 | 2015-07-10 17:01:17 +0800 | [diff] [blame] | 616 | error = dlm_unlock_sync(cinfo->message_lockres); |
| 617 | if (unlikely(error != 0)) { |
| 618 | pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n", |
| 619 | error); |
| 620 | /* in case the message can't be released due to some reason */ |
| 621 | goto failed_ack; |
| 622 | } |
Goldwyn Rodrigues | 601b515 | 2014-06-07 01:28:53 -0500 | [diff] [blame] | 623 | failed_message: |
| 624 | return error; |
| 625 | } |
| 626 | |
| 627 | static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg) |
| 628 | { |
| 629 | int ret; |
| 630 | |
| 631 | lock_comm(cinfo); |
| 632 | ret = __sendmsg(cinfo, cmsg); |
| 633 | unlock_comm(cinfo); |
| 634 | return ret; |
| 635 | } |
| 636 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 637 | static int gather_all_resync_info(struct mddev *mddev, int total_slots) |
| 638 | { |
| 639 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 640 | int i, ret = 0; |
| 641 | struct dlm_lock_resource *bm_lockres; |
| 642 | struct suspend_info *s; |
| 643 | char str[64]; |
Guoqing Jiang | abb9b22 | 2015-07-10 17:01:22 +0800 | [diff] [blame] | 644 | sector_t lo, hi; |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 645 | |
| 646 | |
| 647 | for (i = 0; i < total_slots; i++) { |
| 648 | memset(str, '\0', 64); |
| 649 | snprintf(str, 64, "bitmap%04d", i); |
| 650 | bm_lockres = lockres_init(mddev, str, NULL, 1); |
| 651 | if (!bm_lockres) |
| 652 | return -ENOMEM; |
| 653 | if (i == (cinfo->slot_number - 1)) |
| 654 | continue; |
| 655 | |
| 656 | bm_lockres->flags |= DLM_LKF_NOQUEUE; |
| 657 | ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW); |
| 658 | if (ret == -EAGAIN) { |
| 659 | memset(bm_lockres->lksb.sb_lvbptr, '\0', LVB_SIZE); |
| 660 | s = read_resync_info(mddev, bm_lockres); |
| 661 | if (s) { |
| 662 | pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n", |
| 663 | __func__, __LINE__, |
| 664 | (unsigned long long) s->lo, |
| 665 | (unsigned long long) s->hi, i); |
| 666 | spin_lock_irq(&cinfo->suspend_lock); |
| 667 | s->slot = i; |
| 668 | list_add(&s->list, &cinfo->suspend_list); |
| 669 | spin_unlock_irq(&cinfo->suspend_lock); |
| 670 | } |
| 671 | ret = 0; |
| 672 | lockres_free(bm_lockres); |
| 673 | continue; |
| 674 | } |
Guoqing Jiang | 6e6d9f2 | 2015-07-10 17:01:20 +0800 | [diff] [blame] | 675 | if (ret) { |
| 676 | lockres_free(bm_lockres); |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 677 | goto out; |
Guoqing Jiang | 6e6d9f2 | 2015-07-10 17:01:20 +0800 | [diff] [blame] | 678 | } |
Guoqing Jiang | abb9b22 | 2015-07-10 17:01:22 +0800 | [diff] [blame] | 679 | |
| 680 | /* Read the disk bitmap sb and check if it needs recovery */ |
| 681 | ret = bitmap_copy_from_slot(mddev, i, &lo, &hi, false); |
| 682 | if (ret) { |
| 683 | pr_warn("md-cluster: Could not gather bitmaps from slot %d", i); |
| 684 | lockres_free(bm_lockres); |
| 685 | continue; |
| 686 | } |
| 687 | if ((hi > 0) && (lo < mddev->recovery_cp)) { |
| 688 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 689 | mddev->recovery_cp = lo; |
| 690 | md_check_recovery(mddev); |
| 691 | } |
| 692 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 693 | dlm_unlock_sync(bm_lockres); |
| 694 | lockres_free(bm_lockres); |
| 695 | } |
| 696 | out: |
| 697 | return ret; |
| 698 | } |
| 699 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 700 | static int join(struct mddev *mddev, int nodes) |
| 701 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 702 | struct md_cluster_info *cinfo; |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 703 | int ret, ops_rv; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 704 | char str[64]; |
| 705 | |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 706 | cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL); |
| 707 | if (!cinfo) |
| 708 | return -ENOMEM; |
| 709 | |
Guoqing Jiang | 9e3072e | 2015-07-10 17:01:18 +0800 | [diff] [blame] | 710 | INIT_LIST_HEAD(&cinfo->suspend_list); |
| 711 | spin_lock_init(&cinfo->suspend_lock); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 712 | init_completion(&cinfo->completion); |
Guoqing Jiang | eece075 | 2015-07-10 17:01:21 +0800 | [diff] [blame] | 713 | set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 714 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 715 | mddev->cluster_info = cinfo; |
| 716 | |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 717 | memset(str, 0, 64); |
Guoqing Jiang | b89f704 | 2015-07-10 16:54:02 +0800 | [diff] [blame] | 718 | sprintf(str, "%pU", mddev->uuid); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 719 | ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name, |
| 720 | DLM_LSFL_FS, LVB_SIZE, |
| 721 | &md_ls_ops, mddev, &ops_rv, &cinfo->lockspace); |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 722 | if (ret) |
| 723 | goto err; |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 724 | wait_for_completion(&cinfo->completion); |
Guoqing Jiang | 8c58f02 | 2015-04-21 11:25:52 -0500 | [diff] [blame] | 725 | if (nodes < cinfo->slot_number) { |
| 726 | pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).", |
| 727 | cinfo->slot_number, nodes); |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 728 | ret = -ERANGE; |
| 729 | goto err; |
| 730 | } |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 731 | /* Initiate the communication resources */ |
| 732 | ret = -ENOMEM; |
| 733 | cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv"); |
| 734 | if (!cinfo->recv_thread) { |
| 735 | pr_err("md-cluster: cannot allocate memory for recv_thread!\n"); |
| 736 | goto err; |
| 737 | } |
| 738 | cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1); |
| 739 | if (!cinfo->message_lockres) |
| 740 | goto err; |
| 741 | cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0); |
| 742 | if (!cinfo->token_lockres) |
| 743 | goto err; |
| 744 | cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0); |
| 745 | if (!cinfo->ack_lockres) |
| 746 | goto err; |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 747 | cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0); |
| 748 | if (!cinfo->no_new_dev_lockres) |
| 749 | goto err; |
| 750 | |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 751 | /* get sync CR lock on ACK. */ |
| 752 | if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR)) |
| 753 | pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n", |
| 754 | ret); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 755 | /* get sync CR lock on no-new-dev. */ |
| 756 | if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR)) |
| 757 | pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret); |
| 758 | |
Goldwyn Rodrigues | 54519c5 | 2014-06-06 12:12:32 -0500 | [diff] [blame] | 759 | |
| 760 | pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number); |
| 761 | snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1); |
| 762 | cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1); |
| 763 | if (!cinfo->bitmap_lockres) |
| 764 | goto err; |
| 765 | if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) { |
| 766 | pr_err("Failed to get bitmap lock\n"); |
| 767 | ret = -EINVAL; |
| 768 | goto err; |
| 769 | } |
| 770 | |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 771 | cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0); |
| 772 | if (!cinfo->resync_lockres) |
| 773 | goto err; |
| 774 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 775 | ret = gather_all_resync_info(mddev, nodes); |
| 776 | if (ret) |
| 777 | goto err; |
| 778 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 779 | return 0; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 780 | err: |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 781 | lockres_free(cinfo->message_lockres); |
| 782 | lockres_free(cinfo->token_lockres); |
| 783 | lockres_free(cinfo->ack_lockres); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 784 | lockres_free(cinfo->no_new_dev_lockres); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 785 | lockres_free(cinfo->resync_lockres); |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 786 | lockres_free(cinfo->bitmap_lockres); |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 787 | if (cinfo->lockspace) |
| 788 | dlm_release_lockspace(cinfo->lockspace, 2); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 789 | mddev->cluster_info = NULL; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 790 | kfree(cinfo); |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 791 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 792 | } |
| 793 | |
Guoqing Jiang | 0999541 | 2015-10-01 00:09:18 +0800 | [diff] [blame] | 794 | static void resync_bitmap(struct mddev *mddev) |
| 795 | { |
| 796 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 797 | struct cluster_msg cmsg = {0}; |
| 798 | int err; |
| 799 | |
| 800 | cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC); |
| 801 | err = sendmsg(cinfo, &cmsg); |
| 802 | if (err) |
| 803 | pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n", |
| 804 | __func__, __LINE__, err); |
| 805 | } |
| 806 | |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame^] | 807 | static void unlock_all_bitmaps(struct mddev *mddev); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 808 | static int leave(struct mddev *mddev) |
| 809 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 810 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 811 | |
| 812 | if (!cinfo) |
| 813 | return 0; |
Guoqing Jiang | 0999541 | 2015-10-01 00:09:18 +0800 | [diff] [blame] | 814 | |
| 815 | /* BITMAP_NEEDS_SYNC message should be sent when node |
| 816 | * is leaving the cluster with dirty bitmap, also we |
| 817 | * can only deliver it when dlm connection is available */ |
| 818 | if (cinfo->slot_number > 0 && mddev->recovery_cp != MaxSector) |
| 819 | resync_bitmap(mddev); |
| 820 | |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 821 | md_unregister_thread(&cinfo->recovery_thread); |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 822 | md_unregister_thread(&cinfo->recv_thread); |
| 823 | lockres_free(cinfo->message_lockres); |
| 824 | lockres_free(cinfo->token_lockres); |
| 825 | lockres_free(cinfo->ack_lockres); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 826 | lockres_free(cinfo->no_new_dev_lockres); |
Goldwyn Rodrigues | 54519c5 | 2014-06-06 12:12:32 -0500 | [diff] [blame] | 827 | lockres_free(cinfo->bitmap_lockres); |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame^] | 828 | unlock_all_bitmaps(mddev); |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 829 | dlm_release_lockspace(cinfo->lockspace, 2); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 830 | return 0; |
| 831 | } |
| 832 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 833 | /* slot_number(): Returns the MD slot number to use |
| 834 | * DLM starts the slot numbers from 1, wheras cluster-md |
| 835 | * wants the number to be from zero, so we deduct one |
| 836 | */ |
| 837 | static int slot_number(struct mddev *mddev) |
| 838 | { |
| 839 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 840 | |
| 841 | return cinfo->slot_number - 1; |
| 842 | } |
| 843 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 844 | static int metadata_update_start(struct mddev *mddev) |
| 845 | { |
| 846 | return lock_comm(mddev->cluster_info); |
| 847 | } |
| 848 | |
| 849 | static int metadata_update_finish(struct mddev *mddev) |
| 850 | { |
| 851 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 852 | struct cluster_msg cmsg; |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 853 | struct md_rdev *rdev; |
| 854 | int ret = 0; |
NeilBrown | ba2746b | 2015-10-16 13:48:35 +1100 | [diff] [blame] | 855 | int raid_slot = -1; |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 856 | |
| 857 | memset(&cmsg, 0, sizeof(cmsg)); |
| 858 | cmsg.type = cpu_to_le32(METADATA_UPDATED); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 859 | /* Pick up a good active device number to send. |
| 860 | */ |
| 861 | rdev_for_each(rdev, mddev) |
| 862 | if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) { |
NeilBrown | ba2746b | 2015-10-16 13:48:35 +1100 | [diff] [blame] | 863 | raid_slot = rdev->desc_nr; |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 864 | break; |
| 865 | } |
NeilBrown | ba2746b | 2015-10-16 13:48:35 +1100 | [diff] [blame] | 866 | if (raid_slot >= 0) { |
| 867 | cmsg.raid_slot = cpu_to_le32(raid_slot); |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 868 | ret = __sendmsg(cinfo, &cmsg); |
NeilBrown | ba2746b | 2015-10-16 13:48:35 +1100 | [diff] [blame] | 869 | } else |
Goldwyn Rodrigues | 70bcecd | 2015-08-21 10:33:39 -0500 | [diff] [blame] | 870 | pr_warn("md-cluster: No good device id found to send\n"); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 871 | unlock_comm(cinfo); |
| 872 | return ret; |
| 873 | } |
| 874 | |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 875 | static void metadata_update_cancel(struct mddev *mddev) |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 876 | { |
| 877 | struct md_cluster_info *cinfo = mddev->cluster_info; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 878 | unlock_comm(cinfo); |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 879 | } |
| 880 | |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 881 | static int resync_start(struct mddev *mddev) |
| 882 | { |
| 883 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 884 | cinfo->resync_lockres->flags |= DLM_LKF_NOQUEUE; |
| 885 | return dlm_lock_sync(cinfo->resync_lockres, DLM_LOCK_EX); |
| 886 | } |
| 887 | |
Goldwyn Rodrigues | c40f341 | 2015-08-19 08:14:42 +1000 | [diff] [blame] | 888 | static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi) |
Goldwyn Rodrigues | 965400e | 2014-06-07 02:16:58 -0500 | [diff] [blame] | 889 | { |
| 890 | struct md_cluster_info *cinfo = mddev->cluster_info; |
Goldwyn Rodrigues | ac277c6 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 891 | struct resync_info ri; |
Guoqing Jiang | aee177a | 2015-10-12 17:21:24 +0800 | [diff] [blame] | 892 | struct cluster_msg cmsg = {0}; |
Goldwyn Rodrigues | 965400e | 2014-06-07 02:16:58 -0500 | [diff] [blame] | 893 | |
Goldwyn Rodrigues | ac277c6 | 2015-12-21 10:50:59 +1100 | [diff] [blame] | 894 | /* do not send zero again, if we have sent before */ |
| 895 | if (hi == 0) { |
| 896 | memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info)); |
| 897 | if (le64_to_cpu(ri.hi) == 0) |
| 898 | return 0; |
| 899 | } |
| 900 | |
NeilBrown | 30661b4 | 2015-10-19 15:44:00 +1100 | [diff] [blame] | 901 | add_resync_info(cinfo->bitmap_lockres, lo, hi); |
Goldwyn Rodrigues | c40f341 | 2015-08-19 08:14:42 +1000 | [diff] [blame] | 902 | /* Re-acquire the lock to refresh LVB */ |
| 903 | dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW); |
Goldwyn Rodrigues | c40f341 | 2015-08-19 08:14:42 +1000 | [diff] [blame] | 904 | cmsg.type = cpu_to_le32(RESYNCING); |
Goldwyn Rodrigues | 965400e | 2014-06-07 02:16:58 -0500 | [diff] [blame] | 905 | cmsg.low = cpu_to_le64(lo); |
| 906 | cmsg.high = cpu_to_le64(hi); |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 907 | |
Goldwyn Rodrigues | 965400e | 2014-06-07 02:16:58 -0500 | [diff] [blame] | 908 | return sendmsg(cinfo, &cmsg); |
| 909 | } |
| 910 | |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 911 | static int resync_finish(struct mddev *mddev) |
| 912 | { |
| 913 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 914 | cinfo->resync_lockres->flags &= ~DLM_LKF_NOQUEUE; |
| 915 | dlm_unlock_sync(cinfo->resync_lockres); |
| 916 | return resync_info_update(mddev, 0, 0); |
| 917 | } |
| 918 | |
Goldwyn Rodrigues | 90382ed | 2015-06-24 09:30:32 -0500 | [diff] [blame] | 919 | static int area_resyncing(struct mddev *mddev, int direction, |
| 920 | sector_t lo, sector_t hi) |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 921 | { |
| 922 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 923 | int ret = 0; |
| 924 | struct suspend_info *s; |
| 925 | |
Goldwyn Rodrigues | 90382ed | 2015-06-24 09:30:32 -0500 | [diff] [blame] | 926 | if ((direction == READ) && |
| 927 | test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state)) |
| 928 | return 1; |
| 929 | |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 930 | spin_lock_irq(&cinfo->suspend_lock); |
| 931 | if (list_empty(&cinfo->suspend_list)) |
| 932 | goto out; |
| 933 | list_for_each_entry(s, &cinfo->suspend_list, list) |
| 934 | if (hi > s->lo && lo < s->hi) { |
| 935 | ret = 1; |
| 936 | break; |
| 937 | } |
| 938 | out: |
| 939 | spin_unlock_irq(&cinfo->suspend_lock); |
| 940 | return ret; |
| 941 | } |
| 942 | |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 943 | /* add_new_disk() - initiates a disk add |
| 944 | * However, if this fails before writing md_update_sb(), |
| 945 | * add_new_disk_cancel() must be called to release token lock |
| 946 | */ |
| 947 | static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 948 | { |
| 949 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 950 | struct cluster_msg cmsg; |
| 951 | int ret = 0; |
| 952 | struct mdp_superblock_1 *sb = page_address(rdev->sb_page); |
| 953 | char *uuid = sb->device_uuid; |
| 954 | |
| 955 | memset(&cmsg, 0, sizeof(cmsg)); |
| 956 | cmsg.type = cpu_to_le32(NEWDISK); |
| 957 | memcpy(cmsg.uuid, uuid, 16); |
Guoqing Jiang | faeff83 | 2015-10-12 17:21:21 +0800 | [diff] [blame] | 958 | cmsg.raid_slot = cpu_to_le32(rdev->desc_nr); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 959 | lock_comm(cinfo); |
| 960 | ret = __sendmsg(cinfo, &cmsg); |
| 961 | if (ret) |
| 962 | return ret; |
| 963 | cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE; |
| 964 | ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX); |
| 965 | cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE; |
| 966 | /* Some node does not "see" the device */ |
| 967 | if (ret == -EAGAIN) |
| 968 | ret = -ENOENT; |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 969 | if (ret) |
| 970 | unlock_comm(cinfo); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 971 | else |
| 972 | dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR); |
| 973 | return ret; |
| 974 | } |
| 975 | |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 976 | static void add_new_disk_cancel(struct mddev *mddev) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 977 | { |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 978 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 979 | unlock_comm(cinfo); |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 980 | } |
| 981 | |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 982 | static int new_disk_ack(struct mddev *mddev, bool ack) |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 983 | { |
| 984 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 985 | |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 986 | if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) { |
| 987 | pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev)); |
| 988 | return -EINVAL; |
| 989 | } |
| 990 | |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 991 | if (ack) |
| 992 | dlm_unlock_sync(cinfo->no_new_dev_lockres); |
| 993 | complete(&cinfo->newdisk_completion); |
Goldwyn Rodrigues | fa8259d | 2015-03-02 10:55:49 -0600 | [diff] [blame] | 994 | return 0; |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 995 | } |
| 996 | |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 997 | static int remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
| 998 | { |
Guoqing Jiang | aee177a | 2015-10-12 17:21:24 +0800 | [diff] [blame] | 999 | struct cluster_msg cmsg = {0}; |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 1000 | struct md_cluster_info *cinfo = mddev->cluster_info; |
Guoqing Jiang | faeff83 | 2015-10-12 17:21:21 +0800 | [diff] [blame] | 1001 | cmsg.type = cpu_to_le32(REMOVE); |
| 1002 | cmsg.raid_slot = cpu_to_le32(rdev->desc_nr); |
Goldwyn Rodrigues | 54a8839 | 2015-12-21 10:51:00 +1100 | [diff] [blame] | 1003 | return sendmsg(cinfo, &cmsg); |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 1004 | } |
| 1005 | |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame^] | 1006 | static int lock_all_bitmaps(struct mddev *mddev) |
| 1007 | { |
| 1008 | int slot, my_slot, ret, held = 1, i = 0; |
| 1009 | char str[64]; |
| 1010 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 1011 | |
| 1012 | cinfo->other_bitmap_lockres = kzalloc((mddev->bitmap_info.nodes - 1) * |
| 1013 | sizeof(struct dlm_lock_resource *), |
| 1014 | GFP_KERNEL); |
| 1015 | if (!cinfo->other_bitmap_lockres) { |
| 1016 | pr_err("md: can't alloc mem for other bitmap locks\n"); |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | my_slot = slot_number(mddev); |
| 1021 | for (slot = 0; slot < mddev->bitmap_info.nodes; slot++) { |
| 1022 | if (slot == my_slot) |
| 1023 | continue; |
| 1024 | |
| 1025 | memset(str, '\0', 64); |
| 1026 | snprintf(str, 64, "bitmap%04d", slot); |
| 1027 | cinfo->other_bitmap_lockres[i] = lockres_init(mddev, str, NULL, 1); |
| 1028 | if (!cinfo->other_bitmap_lockres[i]) |
| 1029 | return -ENOMEM; |
| 1030 | |
| 1031 | cinfo->other_bitmap_lockres[i]->flags |= DLM_LKF_NOQUEUE; |
| 1032 | ret = dlm_lock_sync(cinfo->other_bitmap_lockres[i], DLM_LOCK_PW); |
| 1033 | if (ret) |
| 1034 | held = -1; |
| 1035 | i++; |
| 1036 | } |
| 1037 | |
| 1038 | return held; |
| 1039 | } |
| 1040 | |
| 1041 | static void unlock_all_bitmaps(struct mddev *mddev) |
| 1042 | { |
| 1043 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 1044 | int i; |
| 1045 | |
| 1046 | /* release other node's bitmap lock if they are existed */ |
| 1047 | if (cinfo->other_bitmap_lockres) { |
| 1048 | for (i = 0; i < mddev->bitmap_info.nodes - 1; i++) { |
| 1049 | if (cinfo->other_bitmap_lockres[i]) { |
| 1050 | dlm_unlock_sync(cinfo->other_bitmap_lockres[i]); |
| 1051 | lockres_free(cinfo->other_bitmap_lockres[i]); |
| 1052 | } |
| 1053 | } |
| 1054 | kfree(cinfo->other_bitmap_lockres); |
| 1055 | } |
| 1056 | } |
| 1057 | |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 1058 | static int gather_bitmaps(struct md_rdev *rdev) |
| 1059 | { |
| 1060 | int sn, err; |
| 1061 | sector_t lo, hi; |
Guoqing Jiang | aee177a | 2015-10-12 17:21:24 +0800 | [diff] [blame] | 1062 | struct cluster_msg cmsg = {0}; |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 1063 | struct mddev *mddev = rdev->mddev; |
| 1064 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 1065 | |
Guoqing Jiang | faeff83 | 2015-10-12 17:21:21 +0800 | [diff] [blame] | 1066 | cmsg.type = cpu_to_le32(RE_ADD); |
| 1067 | cmsg.raid_slot = cpu_to_le32(rdev->desc_nr); |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 1068 | err = sendmsg(cinfo, &cmsg); |
| 1069 | if (err) |
| 1070 | goto out; |
| 1071 | |
| 1072 | for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) { |
| 1073 | if (sn == (cinfo->slot_number - 1)) |
| 1074 | continue; |
| 1075 | err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false); |
| 1076 | if (err) { |
| 1077 | pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn); |
| 1078 | goto out; |
| 1079 | } |
| 1080 | if ((hi > 0) && (lo < mddev->recovery_cp)) |
| 1081 | mddev->recovery_cp = lo; |
| 1082 | } |
| 1083 | out: |
| 1084 | return err; |
| 1085 | } |
| 1086 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 1087 | static struct md_cluster_operations cluster_ops = { |
| 1088 | .join = join, |
| 1089 | .leave = leave, |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 1090 | .slot_number = slot_number, |
Goldwyn Rodrigues | c186b12 | 2015-09-30 13:20:35 -0500 | [diff] [blame] | 1091 | .resync_start = resync_start, |
| 1092 | .resync_finish = resync_finish, |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 1093 | .resync_info_update = resync_info_update, |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 1094 | .metadata_update_start = metadata_update_start, |
| 1095 | .metadata_update_finish = metadata_update_finish, |
| 1096 | .metadata_update_cancel = metadata_update_cancel, |
Goldwyn Rodrigues | 589a1c4 | 2014-06-07 02:39:37 -0500 | [diff] [blame] | 1097 | .area_resyncing = area_resyncing, |
Goldwyn Rodrigues | dbb64f8 | 2015-10-01 13:20:27 -0500 | [diff] [blame] | 1098 | .add_new_disk = add_new_disk, |
| 1099 | .add_new_disk_cancel = add_new_disk_cancel, |
Goldwyn Rodrigues | 1aee41f | 2014-10-29 18:51:31 -0500 | [diff] [blame] | 1100 | .new_disk_ack = new_disk_ack, |
Goldwyn Rodrigues | 88bcfef | 2015-04-14 10:44:44 -0500 | [diff] [blame] | 1101 | .remove_disk = remove_disk, |
Goldwyn Rodrigues | 97f6cd3 | 2015-04-14 10:45:42 -0500 | [diff] [blame] | 1102 | .gather_bitmaps = gather_bitmaps, |
Guoqing Jiang | f6a2dc6 | 2015-12-21 10:51:00 +1100 | [diff] [blame^] | 1103 | .lock_all_bitmaps = lock_all_bitmaps, |
| 1104 | .unlock_all_bitmaps = unlock_all_bitmaps, |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 1105 | }; |
| 1106 | |
Goldwyn Rodrigues | 8e854e9 | 2014-03-07 11:21:15 -0600 | [diff] [blame] | 1107 | static int __init cluster_init(void) |
| 1108 | { |
| 1109 | pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n"); |
| 1110 | pr_info("Registering Cluster MD functions\n"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 1111 | register_md_cluster_operations(&cluster_ops, THIS_MODULE); |
Goldwyn Rodrigues | 8e854e9 | 2014-03-07 11:21:15 -0600 | [diff] [blame] | 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | static void cluster_exit(void) |
| 1116 | { |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 1117 | unregister_md_cluster_operations(); |
Goldwyn Rodrigues | 8e854e9 | 2014-03-07 11:21:15 -0600 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | module_init(cluster_init); |
| 1121 | module_exit(cluster_exit); |
Guoqing Jiang | 86b5727 | 2015-10-12 17:21:25 +0800 | [diff] [blame] | 1122 | MODULE_AUTHOR("SUSE"); |
Goldwyn Rodrigues | 8e854e9 | 2014-03-07 11:21:15 -0600 | [diff] [blame] | 1123 | MODULE_LICENSE("GPL"); |
| 1124 | MODULE_DESCRIPTION("Clustering support for MD"); |