md-cluster.c 28 KB

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