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