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