md-cluster.c 35 KB

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