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> |
| 15 | #include "md.h" |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 16 | #include "bitmap.h" |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 17 | #include "md-cluster.h" |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 18 | |
| 19 | #define LVB_SIZE 64 |
| 20 | |
| 21 | struct dlm_lock_resource { |
| 22 | dlm_lockspace_t *ls; |
| 23 | struct dlm_lksb lksb; |
| 24 | char *name; /* lock name. */ |
| 25 | uint32_t flags; /* flags to pass to dlm_lock() */ |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 26 | struct completion completion; /* completion for synchronized locking */ |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 27 | void (*bast)(void *arg, int mode); /* blocking AST function pointer*/ |
| 28 | struct mddev *mddev; /* pointing back to mddev. */ |
| 29 | }; |
| 30 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 31 | struct suspend_info { |
| 32 | int slot; |
| 33 | sector_t lo; |
| 34 | sector_t hi; |
| 35 | struct list_head list; |
| 36 | }; |
| 37 | |
| 38 | struct resync_info { |
| 39 | __le64 lo; |
| 40 | __le64 hi; |
| 41 | }; |
| 42 | |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 43 | struct md_cluster_info { |
| 44 | /* dlm lock space and resources for clustered raid. */ |
| 45 | dlm_lockspace_t *lockspace; |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 46 | int slot_number; |
| 47 | struct completion completion; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 48 | struct dlm_lock_resource *sb_lock; |
| 49 | struct mutex sb_mutex; |
Goldwyn Rodrigues | 54519c5 | 2014-06-06 12:12:32 -0500 | [diff] [blame] | 50 | struct dlm_lock_resource *bitmap_lockres; |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 51 | struct list_head suspend_list; |
| 52 | spinlock_t suspend_lock; |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 53 | struct md_thread *recovery_thread; |
| 54 | unsigned long recovery_map; |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 55 | /* communication loc resources */ |
| 56 | struct dlm_lock_resource *ack_lockres; |
| 57 | struct dlm_lock_resource *message_lockres; |
| 58 | struct dlm_lock_resource *token_lockres; |
| 59 | struct md_thread *recv_thread; |
| 60 | }; |
| 61 | |
| 62 | enum msg_type { |
| 63 | METADATA_UPDATED = 0, |
| 64 | RESYNCING, |
| 65 | }; |
| 66 | |
| 67 | struct cluster_msg { |
| 68 | int type; |
| 69 | int slot; |
| 70 | sector_t low; |
| 71 | sector_t high; |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | static void sync_ast(void *arg) |
| 75 | { |
| 76 | struct dlm_lock_resource *res; |
| 77 | |
| 78 | res = (struct dlm_lock_resource *) arg; |
| 79 | complete(&res->completion); |
| 80 | } |
| 81 | |
| 82 | static int dlm_lock_sync(struct dlm_lock_resource *res, int mode) |
| 83 | { |
| 84 | int ret = 0; |
| 85 | |
| 86 | init_completion(&res->completion); |
| 87 | ret = dlm_lock(res->ls, mode, &res->lksb, |
| 88 | res->flags, res->name, strlen(res->name), |
| 89 | 0, sync_ast, res, res->bast); |
| 90 | if (ret) |
| 91 | return ret; |
| 92 | wait_for_completion(&res->completion); |
| 93 | return res->lksb.sb_status; |
| 94 | } |
| 95 | |
| 96 | static int dlm_unlock_sync(struct dlm_lock_resource *res) |
| 97 | { |
| 98 | return dlm_lock_sync(res, DLM_LOCK_NL); |
| 99 | } |
| 100 | |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 101 | static struct dlm_lock_resource *lockres_init(struct mddev *mddev, |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 102 | char *name, void (*bastfn)(void *arg, int mode), int with_lvb) |
| 103 | { |
| 104 | struct dlm_lock_resource *res = NULL; |
| 105 | int ret, namelen; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 106 | struct md_cluster_info *cinfo = mddev->cluster_info; |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 107 | |
| 108 | res = kzalloc(sizeof(struct dlm_lock_resource), GFP_KERNEL); |
| 109 | if (!res) |
| 110 | return NULL; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 111 | res->ls = cinfo->lockspace; |
| 112 | res->mddev = mddev; |
Goldwyn Rodrigues | 47741b7 | 2014-03-07 13:49:26 -0600 | [diff] [blame] | 113 | namelen = strlen(name); |
| 114 | res->name = kzalloc(namelen + 1, GFP_KERNEL); |
| 115 | if (!res->name) { |
| 116 | pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name); |
| 117 | goto out_err; |
| 118 | } |
| 119 | strlcpy(res->name, name, namelen + 1); |
| 120 | if (with_lvb) { |
| 121 | res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL); |
| 122 | if (!res->lksb.sb_lvbptr) { |
| 123 | pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name); |
| 124 | goto out_err; |
| 125 | } |
| 126 | res->flags = DLM_LKF_VALBLK; |
| 127 | } |
| 128 | |
| 129 | if (bastfn) |
| 130 | res->bast = bastfn; |
| 131 | |
| 132 | res->flags |= DLM_LKF_EXPEDITE; |
| 133 | |
| 134 | ret = dlm_lock_sync(res, DLM_LOCK_NL); |
| 135 | if (ret) { |
| 136 | pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name); |
| 137 | goto out_err; |
| 138 | } |
| 139 | res->flags &= ~DLM_LKF_EXPEDITE; |
| 140 | res->flags |= DLM_LKF_CONVERT; |
| 141 | |
| 142 | return res; |
| 143 | out_err: |
| 144 | kfree(res->lksb.sb_lvbptr); |
| 145 | kfree(res->name); |
| 146 | kfree(res); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | static void lockres_free(struct dlm_lock_resource *res) |
| 151 | { |
| 152 | if (!res) |
| 153 | return; |
| 154 | |
| 155 | init_completion(&res->completion); |
| 156 | dlm_unlock(res->ls, res->lksb.sb_lkid, 0, &res->lksb, res); |
| 157 | wait_for_completion(&res->completion); |
| 158 | |
| 159 | kfree(res->name); |
| 160 | kfree(res->lksb.sb_lvbptr); |
| 161 | kfree(res); |
| 162 | } |
Goldwyn Rodrigues | 8e854e9 | 2014-03-07 11:21:15 -0600 | [diff] [blame] | 163 | |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 164 | static char *pretty_uuid(char *dest, char *src) |
| 165 | { |
| 166 | int i, len = 0; |
| 167 | |
| 168 | for (i = 0; i < 16; i++) { |
| 169 | if (i == 4 || i == 6 || i == 8 || i == 10) |
| 170 | len += sprintf(dest + len, "-"); |
| 171 | len += sprintf(dest + len, "%02x", (__u8)src[i]); |
| 172 | } |
| 173 | return dest; |
| 174 | } |
| 175 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 176 | static void add_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres, |
| 177 | sector_t lo, sector_t hi) |
| 178 | { |
| 179 | struct resync_info *ri; |
| 180 | |
| 181 | ri = (struct resync_info *)lockres->lksb.sb_lvbptr; |
| 182 | ri->lo = cpu_to_le64(lo); |
| 183 | ri->hi = cpu_to_le64(hi); |
| 184 | } |
| 185 | |
| 186 | static struct suspend_info *read_resync_info(struct mddev *mddev, struct dlm_lock_resource *lockres) |
| 187 | { |
| 188 | struct resync_info ri; |
| 189 | struct suspend_info *s = NULL; |
| 190 | sector_t hi = 0; |
| 191 | |
| 192 | dlm_lock_sync(lockres, DLM_LOCK_CR); |
| 193 | memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info)); |
| 194 | hi = le64_to_cpu(ri.hi); |
| 195 | if (ri.hi > 0) { |
| 196 | s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL); |
| 197 | if (!s) |
| 198 | goto out; |
| 199 | s->hi = hi; |
| 200 | s->lo = le64_to_cpu(ri.lo); |
| 201 | } |
| 202 | dlm_unlock_sync(lockres); |
| 203 | out: |
| 204 | return s; |
| 205 | } |
| 206 | |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 207 | void recover_bitmaps(struct md_thread *thread) |
| 208 | { |
| 209 | struct mddev *mddev = thread->mddev; |
| 210 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 211 | struct dlm_lock_resource *bm_lockres; |
| 212 | char str[64]; |
| 213 | int slot, ret; |
| 214 | struct suspend_info *s, *tmp; |
| 215 | sector_t lo, hi; |
| 216 | |
| 217 | while (cinfo->recovery_map) { |
| 218 | slot = fls64((u64)cinfo->recovery_map) - 1; |
| 219 | |
| 220 | /* Clear suspend_area associated with the bitmap */ |
| 221 | spin_lock_irq(&cinfo->suspend_lock); |
| 222 | list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list) |
| 223 | if (slot == s->slot) { |
| 224 | list_del(&s->list); |
| 225 | kfree(s); |
| 226 | } |
| 227 | spin_unlock_irq(&cinfo->suspend_lock); |
| 228 | |
| 229 | snprintf(str, 64, "bitmap%04d", slot); |
| 230 | bm_lockres = lockres_init(mddev, str, NULL, 1); |
| 231 | if (!bm_lockres) { |
| 232 | pr_err("md-cluster: Cannot initialize bitmaps\n"); |
| 233 | goto clear_bit; |
| 234 | } |
| 235 | |
| 236 | ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW); |
| 237 | if (ret) { |
| 238 | pr_err("md-cluster: Could not DLM lock %s: %d\n", |
| 239 | str, ret); |
| 240 | goto clear_bit; |
| 241 | } |
| 242 | ret = bitmap_copy_from_slot(mddev, slot, &lo, &hi); |
Goldwyn Rodrigues | 4b26a08 | 2014-06-07 00:52:29 -0500 | [diff] [blame] | 243 | if (ret) { |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 244 | 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] | 245 | goto dlm_unlock; |
| 246 | } |
| 247 | if (hi > 0) { |
| 248 | /* TODO:Wait for current resync to get over */ |
| 249 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 250 | if (lo < mddev->recovery_cp) |
| 251 | mddev->recovery_cp = lo; |
| 252 | md_check_recovery(mddev); |
| 253 | } |
| 254 | dlm_unlock: |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 255 | dlm_unlock_sync(bm_lockres); |
| 256 | clear_bit: |
| 257 | clear_bit(slot, &cinfo->recovery_map); |
| 258 | } |
| 259 | } |
| 260 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 261 | static void recover_prep(void *arg) |
| 262 | { |
| 263 | } |
| 264 | |
| 265 | static void recover_slot(void *arg, struct dlm_slot *slot) |
| 266 | { |
| 267 | struct mddev *mddev = arg; |
| 268 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 269 | |
| 270 | pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n", |
| 271 | mddev->bitmap_info.cluster_name, |
| 272 | slot->nodeid, slot->slot, |
| 273 | cinfo->slot_number); |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 274 | set_bit(slot->slot - 1, &cinfo->recovery_map); |
| 275 | if (!cinfo->recovery_thread) { |
| 276 | cinfo->recovery_thread = md_register_thread(recover_bitmaps, |
| 277 | mddev, "recover"); |
| 278 | if (!cinfo->recovery_thread) { |
| 279 | pr_warn("md-cluster: Could not create recovery thread\n"); |
| 280 | return; |
| 281 | } |
| 282 | } |
| 283 | md_wakeup_thread(cinfo->recovery_thread); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | static void recover_done(void *arg, struct dlm_slot *slots, |
| 287 | int num_slots, int our_slot, |
| 288 | uint32_t generation) |
| 289 | { |
| 290 | struct mddev *mddev = arg; |
| 291 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 292 | |
| 293 | cinfo->slot_number = our_slot; |
| 294 | complete(&cinfo->completion); |
| 295 | } |
| 296 | |
| 297 | static const struct dlm_lockspace_ops md_ls_ops = { |
| 298 | .recover_prep = recover_prep, |
| 299 | .recover_slot = recover_slot, |
| 300 | .recover_done = recover_done, |
| 301 | }; |
| 302 | |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 303 | /* |
| 304 | * The BAST function for the ack lock resource |
| 305 | * This function wakes up the receive thread in |
| 306 | * order to receive and process the message. |
| 307 | */ |
| 308 | static void ack_bast(void *arg, int mode) |
| 309 | { |
| 310 | struct dlm_lock_resource *res = (struct dlm_lock_resource *)arg; |
| 311 | struct md_cluster_info *cinfo = res->mddev->cluster_info; |
| 312 | |
| 313 | if (mode == DLM_LOCK_EX) |
| 314 | md_wakeup_thread(cinfo->recv_thread); |
| 315 | } |
| 316 | |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame^] | 317 | static void __remove_suspend_info(struct md_cluster_info *cinfo, int slot) |
| 318 | { |
| 319 | struct suspend_info *s, *tmp; |
| 320 | |
| 321 | list_for_each_entry_safe(s, tmp, &cinfo->suspend_list, list) |
| 322 | if (slot == s->slot) { |
| 323 | pr_info("%s:%d Deleting suspend_info: %d\n", |
| 324 | __func__, __LINE__, slot); |
| 325 | list_del(&s->list); |
| 326 | kfree(s); |
| 327 | break; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static void remove_suspend_info(struct md_cluster_info *cinfo, int slot) |
| 332 | { |
| 333 | spin_lock_irq(&cinfo->suspend_lock); |
| 334 | __remove_suspend_info(cinfo, slot); |
| 335 | spin_unlock_irq(&cinfo->suspend_lock); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | static void process_suspend_info(struct md_cluster_info *cinfo, |
| 340 | int slot, sector_t lo, sector_t hi) |
| 341 | { |
| 342 | struct suspend_info *s; |
| 343 | |
| 344 | if (!hi) { |
| 345 | remove_suspend_info(cinfo, slot); |
| 346 | return; |
| 347 | } |
| 348 | s = kzalloc(sizeof(struct suspend_info), GFP_KERNEL); |
| 349 | if (!s) |
| 350 | return; |
| 351 | s->slot = slot; |
| 352 | s->lo = lo; |
| 353 | s->hi = hi; |
| 354 | spin_lock_irq(&cinfo->suspend_lock); |
| 355 | /* Remove existing entry (if exists) before adding */ |
| 356 | __remove_suspend_info(cinfo, slot); |
| 357 | list_add(&s->list, &cinfo->suspend_list); |
| 358 | spin_unlock_irq(&cinfo->suspend_lock); |
| 359 | } |
| 360 | |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 361 | static void process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg) |
| 362 | { |
| 363 | switch (msg->type) { |
| 364 | case METADATA_UPDATED: |
| 365 | pr_info("%s: %d Received message: METADATA_UPDATE from %d\n", |
| 366 | __func__, __LINE__, msg->slot); |
Goldwyn Rodrigues | 1d7e3e9 | 2014-06-07 01:53:00 -0500 | [diff] [blame] | 367 | md_reload_sb(mddev); |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 368 | break; |
| 369 | case RESYNCING: |
| 370 | pr_info("%s: %d Received message: RESYNCING from %d\n", |
| 371 | __func__, __LINE__, msg->slot); |
Goldwyn Rodrigues | e59721c | 2014-06-07 02:30:30 -0500 | [diff] [blame^] | 372 | process_suspend_info(mddev->cluster_info, msg->slot, |
| 373 | msg->low, msg->high); |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 374 | break; |
| 375 | }; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * thread for receiving message |
| 380 | */ |
| 381 | static void recv_daemon(struct md_thread *thread) |
| 382 | { |
| 383 | struct md_cluster_info *cinfo = thread->mddev->cluster_info; |
| 384 | struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres; |
| 385 | struct dlm_lock_resource *message_lockres = cinfo->message_lockres; |
| 386 | struct cluster_msg msg; |
| 387 | |
| 388 | /*get CR on Message*/ |
| 389 | if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) { |
| 390 | pr_err("md/raid1:failed to get CR on MESSAGE\n"); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | /* read lvb and wake up thread to process this message_lockres */ |
| 395 | memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg)); |
| 396 | process_recvd_msg(thread->mddev, &msg); |
| 397 | |
| 398 | /*release CR on ack_lockres*/ |
| 399 | dlm_unlock_sync(ack_lockres); |
| 400 | /*up-convert to EX on message_lockres*/ |
| 401 | dlm_lock_sync(message_lockres, DLM_LOCK_EX); |
| 402 | /*get CR on ack_lockres again*/ |
| 403 | dlm_lock_sync(ack_lockres, DLM_LOCK_CR); |
| 404 | /*release CR on message_lockres*/ |
| 405 | dlm_unlock_sync(message_lockres); |
| 406 | } |
| 407 | |
Goldwyn Rodrigues | 601b515 | 2014-06-07 01:28:53 -0500 | [diff] [blame] | 408 | /* lock_comm() |
| 409 | * Takes the lock on the TOKEN lock resource so no other |
| 410 | * node can communicate while the operation is underway. |
| 411 | */ |
| 412 | static int lock_comm(struct md_cluster_info *cinfo) |
| 413 | { |
| 414 | int error; |
| 415 | |
| 416 | error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX); |
| 417 | if (error) |
| 418 | pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n", |
| 419 | __func__, __LINE__, error); |
| 420 | return error; |
| 421 | } |
| 422 | |
| 423 | static void unlock_comm(struct md_cluster_info *cinfo) |
| 424 | { |
| 425 | dlm_unlock_sync(cinfo->token_lockres); |
| 426 | } |
| 427 | |
| 428 | /* __sendmsg() |
| 429 | * This function performs the actual sending of the message. This function is |
| 430 | * usually called after performing the encompassing operation |
| 431 | * The function: |
| 432 | * 1. Grabs the message lockresource in EX mode |
| 433 | * 2. Copies the message to the message LVB |
| 434 | * 3. Downconverts message lockresource to CR |
| 435 | * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes |
| 436 | * and the other nodes read the message. The thread will wait here until all other |
| 437 | * nodes have released ack lock resource. |
| 438 | * 5. Downconvert ack lockresource to CR |
| 439 | */ |
| 440 | static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg) |
| 441 | { |
| 442 | int error; |
| 443 | int slot = cinfo->slot_number - 1; |
| 444 | |
| 445 | cmsg->slot = cpu_to_le32(slot); |
| 446 | /*get EX on Message*/ |
| 447 | error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX); |
| 448 | if (error) { |
| 449 | pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error); |
| 450 | goto failed_message; |
| 451 | } |
| 452 | |
| 453 | memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg, |
| 454 | sizeof(struct cluster_msg)); |
| 455 | /*down-convert EX to CR on Message*/ |
| 456 | error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CR); |
| 457 | if (error) { |
| 458 | pr_err("md-cluster: failed to convert EX to CR on MESSAGE(%d)\n", |
| 459 | error); |
| 460 | goto failed_message; |
| 461 | } |
| 462 | |
| 463 | /*up-convert CR to EX on Ack*/ |
| 464 | error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX); |
| 465 | if (error) { |
| 466 | pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n", |
| 467 | error); |
| 468 | goto failed_ack; |
| 469 | } |
| 470 | |
| 471 | /*down-convert EX to CR on Ack*/ |
| 472 | error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR); |
| 473 | if (error) { |
| 474 | pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n", |
| 475 | error); |
| 476 | goto failed_ack; |
| 477 | } |
| 478 | |
| 479 | failed_ack: |
| 480 | dlm_unlock_sync(cinfo->message_lockres); |
| 481 | failed_message: |
| 482 | return error; |
| 483 | } |
| 484 | |
| 485 | static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg) |
| 486 | { |
| 487 | int ret; |
| 488 | |
| 489 | lock_comm(cinfo); |
| 490 | ret = __sendmsg(cinfo, cmsg); |
| 491 | unlock_comm(cinfo); |
| 492 | return ret; |
| 493 | } |
| 494 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 495 | static int gather_all_resync_info(struct mddev *mddev, int total_slots) |
| 496 | { |
| 497 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 498 | int i, ret = 0; |
| 499 | struct dlm_lock_resource *bm_lockres; |
| 500 | struct suspend_info *s; |
| 501 | char str[64]; |
| 502 | |
| 503 | |
| 504 | for (i = 0; i < total_slots; i++) { |
| 505 | memset(str, '\0', 64); |
| 506 | snprintf(str, 64, "bitmap%04d", i); |
| 507 | bm_lockres = lockres_init(mddev, str, NULL, 1); |
| 508 | if (!bm_lockres) |
| 509 | return -ENOMEM; |
| 510 | if (i == (cinfo->slot_number - 1)) |
| 511 | continue; |
| 512 | |
| 513 | bm_lockres->flags |= DLM_LKF_NOQUEUE; |
| 514 | ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW); |
| 515 | if (ret == -EAGAIN) { |
| 516 | memset(bm_lockres->lksb.sb_lvbptr, '\0', LVB_SIZE); |
| 517 | s = read_resync_info(mddev, bm_lockres); |
| 518 | if (s) { |
| 519 | pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n", |
| 520 | __func__, __LINE__, |
| 521 | (unsigned long long) s->lo, |
| 522 | (unsigned long long) s->hi, i); |
| 523 | spin_lock_irq(&cinfo->suspend_lock); |
| 524 | s->slot = i; |
| 525 | list_add(&s->list, &cinfo->suspend_list); |
| 526 | spin_unlock_irq(&cinfo->suspend_lock); |
| 527 | } |
| 528 | ret = 0; |
| 529 | lockres_free(bm_lockres); |
| 530 | continue; |
| 531 | } |
| 532 | if (ret) |
| 533 | goto out; |
| 534 | /* TODO: Read the disk bitmap sb and check if it needs recovery */ |
| 535 | dlm_unlock_sync(bm_lockres); |
| 536 | lockres_free(bm_lockres); |
| 537 | } |
| 538 | out: |
| 539 | return ret; |
| 540 | } |
| 541 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 542 | static int join(struct mddev *mddev, int nodes) |
| 543 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 544 | struct md_cluster_info *cinfo; |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 545 | int ret, ops_rv; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 546 | char str[64]; |
| 547 | |
| 548 | if (!try_module_get(THIS_MODULE)) |
| 549 | return -ENOENT; |
| 550 | |
| 551 | cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL); |
| 552 | if (!cinfo) |
| 553 | return -ENOMEM; |
| 554 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 555 | init_completion(&cinfo->completion); |
| 556 | |
| 557 | mutex_init(&cinfo->sb_mutex); |
| 558 | mddev->cluster_info = cinfo; |
| 559 | |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 560 | memset(str, 0, 64); |
| 561 | pretty_uuid(str, mddev->uuid); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 562 | ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name, |
| 563 | DLM_LSFL_FS, LVB_SIZE, |
| 564 | &md_ls_ops, mddev, &ops_rv, &cinfo->lockspace); |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 565 | if (ret) |
| 566 | goto err; |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 567 | wait_for_completion(&cinfo->completion); |
Goldwyn Rodrigues | b97e9257 | 2014-06-06 11:50:56 -0500 | [diff] [blame] | 568 | if (nodes <= cinfo->slot_number) { |
| 569 | pr_err("md-cluster: Slot allotted(%d) greater than available slots(%d)", cinfo->slot_number - 1, |
| 570 | nodes); |
| 571 | ret = -ERANGE; |
| 572 | goto err; |
| 573 | } |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 574 | cinfo->sb_lock = lockres_init(mddev, "cmd-super", |
| 575 | NULL, 0); |
| 576 | if (!cinfo->sb_lock) { |
| 577 | ret = -ENOMEM; |
| 578 | goto err; |
| 579 | } |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 580 | /* Initiate the communication resources */ |
| 581 | ret = -ENOMEM; |
| 582 | cinfo->recv_thread = md_register_thread(recv_daemon, mddev, "cluster_recv"); |
| 583 | if (!cinfo->recv_thread) { |
| 584 | pr_err("md-cluster: cannot allocate memory for recv_thread!\n"); |
| 585 | goto err; |
| 586 | } |
| 587 | cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1); |
| 588 | if (!cinfo->message_lockres) |
| 589 | goto err; |
| 590 | cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0); |
| 591 | if (!cinfo->token_lockres) |
| 592 | goto err; |
| 593 | cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0); |
| 594 | if (!cinfo->ack_lockres) |
| 595 | goto err; |
| 596 | /* get sync CR lock on ACK. */ |
| 597 | if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR)) |
| 598 | pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n", |
| 599 | ret); |
Goldwyn Rodrigues | 54519c5 | 2014-06-06 12:12:32 -0500 | [diff] [blame] | 600 | |
| 601 | pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number); |
| 602 | snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1); |
| 603 | cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1); |
| 604 | if (!cinfo->bitmap_lockres) |
| 605 | goto err; |
| 606 | if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) { |
| 607 | pr_err("Failed to get bitmap lock\n"); |
| 608 | ret = -EINVAL; |
| 609 | goto err; |
| 610 | } |
| 611 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 612 | INIT_LIST_HEAD(&cinfo->suspend_list); |
| 613 | spin_lock_init(&cinfo->suspend_lock); |
| 614 | |
| 615 | ret = gather_all_resync_info(mddev, nodes); |
| 616 | if (ret) |
| 617 | goto err; |
| 618 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 619 | return 0; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 620 | err: |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 621 | lockres_free(cinfo->message_lockres); |
| 622 | lockres_free(cinfo->token_lockres); |
| 623 | lockres_free(cinfo->ack_lockres); |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 624 | lockres_free(cinfo->bitmap_lockres); |
| 625 | lockres_free(cinfo->sb_lock); |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 626 | if (cinfo->lockspace) |
| 627 | dlm_release_lockspace(cinfo->lockspace, 2); |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 628 | mddev->cluster_info = NULL; |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 629 | kfree(cinfo); |
| 630 | module_put(THIS_MODULE); |
| 631 | return ret; |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static int leave(struct mddev *mddev) |
| 635 | { |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 636 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 637 | |
| 638 | if (!cinfo) |
| 639 | return 0; |
Goldwyn Rodrigues | e94987d | 2014-06-07 00:45:22 -0500 | [diff] [blame] | 640 | md_unregister_thread(&cinfo->recovery_thread); |
Goldwyn Rodrigues | 4664680 | 2014-06-07 01:08:29 -0500 | [diff] [blame] | 641 | md_unregister_thread(&cinfo->recv_thread); |
| 642 | lockres_free(cinfo->message_lockres); |
| 643 | lockres_free(cinfo->token_lockres); |
| 644 | lockres_free(cinfo->ack_lockres); |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 645 | lockres_free(cinfo->sb_lock); |
Goldwyn Rodrigues | 54519c5 | 2014-06-06 12:12:32 -0500 | [diff] [blame] | 646 | lockres_free(cinfo->bitmap_lockres); |
Goldwyn Rodrigues | c4ce867 | 2014-03-29 10:20:02 -0500 | [diff] [blame] | 647 | dlm_release_lockspace(cinfo->lockspace, 2); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 651 | /* slot_number(): Returns the MD slot number to use |
| 652 | * DLM starts the slot numbers from 1, wheras cluster-md |
| 653 | * wants the number to be from zero, so we deduct one |
| 654 | */ |
| 655 | static int slot_number(struct mddev *mddev) |
| 656 | { |
| 657 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 658 | |
| 659 | return cinfo->slot_number - 1; |
| 660 | } |
| 661 | |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 662 | static void resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi) |
| 663 | { |
| 664 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 665 | |
| 666 | add_resync_info(mddev, cinfo->bitmap_lockres, lo, hi); |
| 667 | /* Re-acquire the lock to refresh LVB */ |
| 668 | dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW); |
| 669 | } |
| 670 | |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 671 | static int metadata_update_start(struct mddev *mddev) |
| 672 | { |
| 673 | return lock_comm(mddev->cluster_info); |
| 674 | } |
| 675 | |
| 676 | static int metadata_update_finish(struct mddev *mddev) |
| 677 | { |
| 678 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 679 | struct cluster_msg cmsg; |
| 680 | int ret; |
| 681 | |
| 682 | memset(&cmsg, 0, sizeof(cmsg)); |
| 683 | cmsg.type = cpu_to_le32(METADATA_UPDATED); |
| 684 | ret = __sendmsg(cinfo, &cmsg); |
| 685 | unlock_comm(cinfo); |
| 686 | return ret; |
| 687 | } |
| 688 | |
| 689 | static int metadata_update_cancel(struct mddev *mddev) |
| 690 | { |
| 691 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 692 | |
| 693 | return dlm_unlock_sync(cinfo->token_lockres); |
| 694 | } |
| 695 | |
Goldwyn Rodrigues | 965400e | 2014-06-07 02:16:58 -0500 | [diff] [blame] | 696 | static int resync_send(struct mddev *mddev, enum msg_type type, |
| 697 | sector_t lo, sector_t hi) |
| 698 | { |
| 699 | struct md_cluster_info *cinfo = mddev->cluster_info; |
| 700 | struct cluster_msg cmsg; |
| 701 | int slot = cinfo->slot_number - 1; |
| 702 | |
| 703 | pr_info("%s:%d lo: %llu hi: %llu\n", __func__, __LINE__, |
| 704 | (unsigned long long)lo, |
| 705 | (unsigned long long)hi); |
| 706 | resync_info_update(mddev, lo, hi); |
| 707 | cmsg.type = cpu_to_le32(type); |
| 708 | cmsg.slot = cpu_to_le32(slot); |
| 709 | cmsg.low = cpu_to_le64(lo); |
| 710 | cmsg.high = cpu_to_le64(hi); |
| 711 | return sendmsg(cinfo, &cmsg); |
| 712 | } |
| 713 | |
| 714 | static int resync_start(struct mddev *mddev, sector_t lo, sector_t hi) |
| 715 | { |
| 716 | pr_info("%s:%d\n", __func__, __LINE__); |
| 717 | return resync_send(mddev, RESYNCING, lo, hi); |
| 718 | } |
| 719 | |
| 720 | static void resync_finish(struct mddev *mddev) |
| 721 | { |
| 722 | pr_info("%s:%d\n", __func__, __LINE__); |
| 723 | resync_send(mddev, RESYNCING, 0, 0); |
| 724 | } |
| 725 | |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 726 | static struct md_cluster_operations cluster_ops = { |
| 727 | .join = join, |
| 728 | .leave = leave, |
Goldwyn Rodrigues | cf921cc | 2014-03-30 00:42:49 -0500 | [diff] [blame] | 729 | .slot_number = slot_number, |
Goldwyn Rodrigues | 96ae923 | 2014-06-06 12:35:34 -0500 | [diff] [blame] | 730 | .resync_info_update = resync_info_update, |
Goldwyn Rodrigues | 965400e | 2014-06-07 02:16:58 -0500 | [diff] [blame] | 731 | .resync_start = resync_start, |
| 732 | .resync_finish = resync_finish, |
Goldwyn Rodrigues | 293467a | 2014-06-07 01:44:51 -0500 | [diff] [blame] | 733 | .metadata_update_start = metadata_update_start, |
| 734 | .metadata_update_finish = metadata_update_finish, |
| 735 | .metadata_update_cancel = metadata_update_cancel, |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 736 | }; |
| 737 | |
Goldwyn Rodrigues | 8e854e9 | 2014-03-07 11:21:15 -0600 | [diff] [blame] | 738 | static int __init cluster_init(void) |
| 739 | { |
| 740 | pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n"); |
| 741 | pr_info("Registering Cluster MD functions\n"); |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 742 | register_md_cluster_operations(&cluster_ops, THIS_MODULE); |
Goldwyn Rodrigues | 8e854e9 | 2014-03-07 11:21:15 -0600 | [diff] [blame] | 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static void cluster_exit(void) |
| 747 | { |
Goldwyn Rodrigues | edb39c9 | 2014-03-29 10:01:53 -0500 | [diff] [blame] | 748 | unregister_md_cluster_operations(); |
Goldwyn Rodrigues | 8e854e9 | 2014-03-07 11:21:15 -0600 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | module_init(cluster_init); |
| 752 | module_exit(cluster_exit); |
| 753 | MODULE_LICENSE("GPL"); |
| 754 | MODULE_DESCRIPTION("Clustering support for MD"); |