md-cluster.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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. kfree(cinfo);
  869. return 0;
  870. }
  871. /* slot_number(): Returns the MD slot number to use
  872. * DLM starts the slot numbers from 1, wheras cluster-md
  873. * wants the number to be from zero, so we deduct one
  874. */
  875. static int slot_number(struct mddev *mddev)
  876. {
  877. struct md_cluster_info *cinfo = mddev->cluster_info;
  878. return cinfo->slot_number - 1;
  879. }
  880. /*
  881. * Check if the communication is already locked, else lock the communication
  882. * channel.
  883. * If it is already locked, token is in EX mode, and hence lock_token()
  884. * should not be called.
  885. */
  886. static int metadata_update_start(struct mddev *mddev)
  887. {
  888. struct md_cluster_info *cinfo = mddev->cluster_info;
  889. wait_event(cinfo->wait,
  890. !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state) ||
  891. test_and_clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state));
  892. /* If token is already locked, return 0 */
  893. if (cinfo->token_lockres->mode == DLM_LOCK_EX)
  894. return 0;
  895. return lock_token(cinfo);
  896. }
  897. static int metadata_update_finish(struct mddev *mddev)
  898. {
  899. struct md_cluster_info *cinfo = mddev->cluster_info;
  900. struct cluster_msg cmsg;
  901. struct md_rdev *rdev;
  902. int ret = 0;
  903. int raid_slot = -1;
  904. memset(&cmsg, 0, sizeof(cmsg));
  905. cmsg.type = cpu_to_le32(METADATA_UPDATED);
  906. /* Pick up a good active device number to send.
  907. */
  908. rdev_for_each(rdev, mddev)
  909. if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
  910. raid_slot = rdev->desc_nr;
  911. break;
  912. }
  913. if (raid_slot >= 0) {
  914. cmsg.raid_slot = cpu_to_le32(raid_slot);
  915. ret = __sendmsg(cinfo, &cmsg);
  916. } else
  917. pr_warn("md-cluster: No good device id found to send\n");
  918. clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  919. unlock_comm(cinfo);
  920. return ret;
  921. }
  922. static void metadata_update_cancel(struct mddev *mddev)
  923. {
  924. struct md_cluster_info *cinfo = mddev->cluster_info;
  925. clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  926. unlock_comm(cinfo);
  927. }
  928. static int resync_start(struct mddev *mddev)
  929. {
  930. struct md_cluster_info *cinfo = mddev->cluster_info;
  931. return dlm_lock_sync_interruptible(cinfo->resync_lockres, DLM_LOCK_EX, mddev);
  932. }
  933. static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
  934. {
  935. struct md_cluster_info *cinfo = mddev->cluster_info;
  936. struct resync_info ri;
  937. struct cluster_msg cmsg = {0};
  938. /* do not send zero again, if we have sent before */
  939. if (hi == 0) {
  940. memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
  941. if (le64_to_cpu(ri.hi) == 0)
  942. return 0;
  943. }
  944. add_resync_info(cinfo->bitmap_lockres, lo, hi);
  945. /* Re-acquire the lock to refresh LVB */
  946. dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
  947. cmsg.type = cpu_to_le32(RESYNCING);
  948. cmsg.low = cpu_to_le64(lo);
  949. cmsg.high = cpu_to_le64(hi);
  950. return sendmsg(cinfo, &cmsg);
  951. }
  952. static int resync_finish(struct mddev *mddev)
  953. {
  954. struct md_cluster_info *cinfo = mddev->cluster_info;
  955. dlm_unlock_sync(cinfo->resync_lockres);
  956. return resync_info_update(mddev, 0, 0);
  957. }
  958. static int area_resyncing(struct mddev *mddev, int direction,
  959. sector_t lo, sector_t hi)
  960. {
  961. struct md_cluster_info *cinfo = mddev->cluster_info;
  962. int ret = 0;
  963. struct suspend_info *s;
  964. if ((direction == READ) &&
  965. test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
  966. return 1;
  967. spin_lock_irq(&cinfo->suspend_lock);
  968. if (list_empty(&cinfo->suspend_list))
  969. goto out;
  970. list_for_each_entry(s, &cinfo->suspend_list, list)
  971. if (hi > s->lo && lo < s->hi) {
  972. ret = 1;
  973. break;
  974. }
  975. out:
  976. spin_unlock_irq(&cinfo->suspend_lock);
  977. return ret;
  978. }
  979. /* add_new_disk() - initiates a disk add
  980. * However, if this fails before writing md_update_sb(),
  981. * add_new_disk_cancel() must be called to release token lock
  982. */
  983. static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev)
  984. {
  985. struct md_cluster_info *cinfo = mddev->cluster_info;
  986. struct cluster_msg cmsg;
  987. int ret = 0;
  988. struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
  989. char *uuid = sb->device_uuid;
  990. memset(&cmsg, 0, sizeof(cmsg));
  991. cmsg.type = cpu_to_le32(NEWDISK);
  992. memcpy(cmsg.uuid, uuid, 16);
  993. cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
  994. lock_comm(cinfo);
  995. ret = __sendmsg(cinfo, &cmsg);
  996. if (ret) {
  997. unlock_comm(cinfo);
  998. return ret;
  999. }
  1000. cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
  1001. ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
  1002. cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
  1003. /* Some node does not "see" the device */
  1004. if (ret == -EAGAIN)
  1005. ret = -ENOENT;
  1006. if (ret)
  1007. unlock_comm(cinfo);
  1008. else {
  1009. dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
  1010. /* Since MD_CHANGE_DEVS will be set in add_bound_rdev which
  1011. * will run soon after add_new_disk, the below path will be
  1012. * invoked:
  1013. * md_wakeup_thread(mddev->thread)
  1014. * -> conf->thread (raid1d)
  1015. * -> md_check_recovery -> md_update_sb
  1016. * -> metadata_update_start/finish
  1017. * MD_CLUSTER_SEND_LOCKED_ALREADY will be cleared eventually.
  1018. *
  1019. * For other failure cases, metadata_update_cancel and
  1020. * add_new_disk_cancel also clear below bit as well.
  1021. * */
  1022. set_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  1023. wake_up(&cinfo->wait);
  1024. }
  1025. return ret;
  1026. }
  1027. static void add_new_disk_cancel(struct mddev *mddev)
  1028. {
  1029. struct md_cluster_info *cinfo = mddev->cluster_info;
  1030. clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  1031. unlock_comm(cinfo);
  1032. }
  1033. static int new_disk_ack(struct mddev *mddev, bool ack)
  1034. {
  1035. struct md_cluster_info *cinfo = mddev->cluster_info;
  1036. if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
  1037. pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
  1038. return -EINVAL;
  1039. }
  1040. if (ack)
  1041. dlm_unlock_sync(cinfo->no_new_dev_lockres);
  1042. complete(&cinfo->newdisk_completion);
  1043. return 0;
  1044. }
  1045. static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1046. {
  1047. struct cluster_msg cmsg = {0};
  1048. struct md_cluster_info *cinfo = mddev->cluster_info;
  1049. cmsg.type = cpu_to_le32(REMOVE);
  1050. cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
  1051. return sendmsg(cinfo, &cmsg);
  1052. }
  1053. static int lock_all_bitmaps(struct mddev *mddev)
  1054. {
  1055. int slot, my_slot, ret, held = 1, i = 0;
  1056. char str[64];
  1057. struct md_cluster_info *cinfo = mddev->cluster_info;
  1058. cinfo->other_bitmap_lockres = kzalloc((mddev->bitmap_info.nodes - 1) *
  1059. sizeof(struct dlm_lock_resource *),
  1060. GFP_KERNEL);
  1061. if (!cinfo->other_bitmap_lockres) {
  1062. pr_err("md: can't alloc mem for other bitmap locks\n");
  1063. return 0;
  1064. }
  1065. my_slot = slot_number(mddev);
  1066. for (slot = 0; slot < mddev->bitmap_info.nodes; slot++) {
  1067. if (slot == my_slot)
  1068. continue;
  1069. memset(str, '\0', 64);
  1070. snprintf(str, 64, "bitmap%04d", slot);
  1071. cinfo->other_bitmap_lockres[i] = lockres_init(mddev, str, NULL, 1);
  1072. if (!cinfo->other_bitmap_lockres[i])
  1073. return -ENOMEM;
  1074. cinfo->other_bitmap_lockres[i]->flags |= DLM_LKF_NOQUEUE;
  1075. ret = dlm_lock_sync(cinfo->other_bitmap_lockres[i], DLM_LOCK_PW);
  1076. if (ret)
  1077. held = -1;
  1078. i++;
  1079. }
  1080. return held;
  1081. }
  1082. static void unlock_all_bitmaps(struct mddev *mddev)
  1083. {
  1084. struct md_cluster_info *cinfo = mddev->cluster_info;
  1085. int i;
  1086. /* release other node's bitmap lock if they are existed */
  1087. if (cinfo->other_bitmap_lockres) {
  1088. for (i = 0; i < mddev->bitmap_info.nodes - 1; i++) {
  1089. if (cinfo->other_bitmap_lockres[i]) {
  1090. lockres_free(cinfo->other_bitmap_lockres[i]);
  1091. }
  1092. }
  1093. kfree(cinfo->other_bitmap_lockres);
  1094. }
  1095. }
  1096. static int gather_bitmaps(struct md_rdev *rdev)
  1097. {
  1098. int sn, err;
  1099. sector_t lo, hi;
  1100. struct cluster_msg cmsg = {0};
  1101. struct mddev *mddev = rdev->mddev;
  1102. struct md_cluster_info *cinfo = mddev->cluster_info;
  1103. cmsg.type = cpu_to_le32(RE_ADD);
  1104. cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
  1105. err = sendmsg(cinfo, &cmsg);
  1106. if (err)
  1107. goto out;
  1108. for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
  1109. if (sn == (cinfo->slot_number - 1))
  1110. continue;
  1111. err = bitmap_copy_from_slot(mddev, sn, &lo, &hi, false);
  1112. if (err) {
  1113. pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
  1114. goto out;
  1115. }
  1116. if ((hi > 0) && (lo < mddev->recovery_cp))
  1117. mddev->recovery_cp = lo;
  1118. }
  1119. out:
  1120. return err;
  1121. }
  1122. static struct md_cluster_operations cluster_ops = {
  1123. .join = join,
  1124. .leave = leave,
  1125. .slot_number = slot_number,
  1126. .resync_start = resync_start,
  1127. .resync_finish = resync_finish,
  1128. .resync_info_update = resync_info_update,
  1129. .metadata_update_start = metadata_update_start,
  1130. .metadata_update_finish = metadata_update_finish,
  1131. .metadata_update_cancel = metadata_update_cancel,
  1132. .area_resyncing = area_resyncing,
  1133. .add_new_disk = add_new_disk,
  1134. .add_new_disk_cancel = add_new_disk_cancel,
  1135. .new_disk_ack = new_disk_ack,
  1136. .remove_disk = remove_disk,
  1137. .load_bitmaps = load_bitmaps,
  1138. .gather_bitmaps = gather_bitmaps,
  1139. .lock_all_bitmaps = lock_all_bitmaps,
  1140. .unlock_all_bitmaps = unlock_all_bitmaps,
  1141. };
  1142. static int __init cluster_init(void)
  1143. {
  1144. pr_warn("md-cluster: EXPERIMENTAL. Use with caution\n");
  1145. pr_info("Registering Cluster MD functions\n");
  1146. register_md_cluster_operations(&cluster_ops, THIS_MODULE);
  1147. return 0;
  1148. }
  1149. static void cluster_exit(void)
  1150. {
  1151. unregister_md_cluster_operations();
  1152. }
  1153. module_init(cluster_init);
  1154. module_exit(cluster_exit);
  1155. MODULE_AUTHOR("SUSE");
  1156. MODULE_LICENSE("GPL");
  1157. MODULE_DESCRIPTION("Clustering support for MD");