md-cluster.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015, SUSE
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kthread.h>
  7. #include <linux/dlm.h>
  8. #include <linux/sched.h>
  9. #include <linux/raid/md_p.h>
  10. #include "md.h"
  11. #include "md-bitmap.h"
  12. #include "md-cluster.h"
  13. #define LVB_SIZE 64
  14. #define NEW_DEV_TIMEOUT 5000
  15. #define WAIT_DLM_LOCK_TIMEOUT (30 * HZ)
  16. struct dlm_lock_resource {
  17. dlm_lockspace_t *ls;
  18. struct dlm_lksb lksb;
  19. char *name; /* lock name. */
  20. uint32_t flags; /* flags to pass to dlm_lock() */
  21. wait_queue_head_t sync_locking; /* wait queue for synchronized locking */
  22. bool sync_locking_done;
  23. void (*bast)(void *arg, int mode); /* blocking AST function pointer*/
  24. struct mddev *mddev; /* pointing back to mddev. */
  25. int mode;
  26. };
  27. struct resync_info {
  28. __le64 lo;
  29. __le64 hi;
  30. };
  31. /* md_cluster_info flags */
  32. #define MD_CLUSTER_WAITING_FOR_NEWDISK 1
  33. #define MD_CLUSTER_SUSPEND_READ_BALANCING 2
  34. #define MD_CLUSTER_BEGIN_JOIN_CLUSTER 3
  35. /* Lock the send communication. This is done through
  36. * bit manipulation as opposed to a mutex in order to
  37. * accommodate lock and hold. See next comment.
  38. */
  39. #define MD_CLUSTER_SEND_LOCK 4
  40. /* If cluster operations (such as adding a disk) must lock the
  41. * communication channel, so as to perform extra operations
  42. * (update metadata) and no other operation is allowed on the
  43. * MD. Token needs to be locked and held until the operation
  44. * completes witha md_update_sb(), which would eventually release
  45. * the lock.
  46. */
  47. #define MD_CLUSTER_SEND_LOCKED_ALREADY 5
  48. /* We should receive message after node joined cluster and
  49. * set up all the related infos such as bitmap and personality */
  50. #define MD_CLUSTER_ALREADY_IN_CLUSTER 6
  51. #define MD_CLUSTER_PENDING_RECV_EVENT 7
  52. #define MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD 8
  53. #define MD_CLUSTER_WAITING_FOR_SYNC 9
  54. struct md_cluster_info {
  55. struct mddev *mddev; /* the md device which md_cluster_info belongs to */
  56. /* dlm lock space and resources for clustered raid. */
  57. dlm_lockspace_t *lockspace;
  58. int slot_number;
  59. struct completion completion;
  60. struct mutex recv_mutex;
  61. struct dlm_lock_resource *bitmap_lockres;
  62. struct dlm_lock_resource **other_bitmap_lockres;
  63. struct dlm_lock_resource *resync_lockres;
  64. struct list_head suspend_list;
  65. spinlock_t suspend_lock;
  66. /* record the region which write should be suspended */
  67. sector_t suspend_lo;
  68. sector_t suspend_hi;
  69. int suspend_from; /* the slot which broadcast suspend_lo/hi */
  70. struct md_thread __rcu *recovery_thread;
  71. unsigned long recovery_map;
  72. /* communication loc resources */
  73. struct dlm_lock_resource *ack_lockres;
  74. struct dlm_lock_resource *message_lockres;
  75. struct dlm_lock_resource *token_lockres;
  76. struct dlm_lock_resource *no_new_dev_lockres;
  77. struct md_thread __rcu *recv_thread;
  78. struct completion newdisk_completion;
  79. wait_queue_head_t wait;
  80. unsigned long state;
  81. /* record the region in RESYNCING message */
  82. sector_t sync_low;
  83. sector_t sync_hi;
  84. };
  85. /* For compatibility, add the new msg_type at the end. */
  86. enum msg_type {
  87. METADATA_UPDATED = 0,
  88. RESYNCING,
  89. NEWDISK,
  90. REMOVE,
  91. RE_ADD,
  92. BITMAP_NEEDS_SYNC,
  93. CHANGE_CAPACITY,
  94. BITMAP_RESIZE,
  95. RESYNCING_START,
  96. };
  97. struct cluster_msg {
  98. __le32 type;
  99. __le32 slot;
  100. /* TODO: Unionize this for smaller footprint */
  101. __le64 low;
  102. __le64 high;
  103. char uuid[16];
  104. __le32 raid_slot;
  105. };
  106. static void sync_ast(void *arg)
  107. {
  108. struct dlm_lock_resource *res;
  109. res = arg;
  110. res->sync_locking_done = true;
  111. wake_up(&res->sync_locking);
  112. }
  113. static int dlm_lock_sync(struct dlm_lock_resource *res, int mode)
  114. {
  115. int ret = 0;
  116. ret = dlm_lock(res->ls, mode, &res->lksb,
  117. res->flags, res->name, strlen(res->name),
  118. 0, sync_ast, res, res->bast);
  119. if (ret)
  120. return ret;
  121. ret = wait_event_timeout(res->sync_locking, res->sync_locking_done,
  122. WAIT_DLM_LOCK_TIMEOUT);
  123. res->sync_locking_done = false;
  124. if (!ret) {
  125. pr_err("locking DLM '%s' timeout!\n", res->name);
  126. return -EBUSY;
  127. }
  128. if (res->lksb.sb_status == 0)
  129. res->mode = mode;
  130. return res->lksb.sb_status;
  131. }
  132. static int dlm_unlock_sync(struct dlm_lock_resource *res)
  133. {
  134. return dlm_lock_sync(res, DLM_LOCK_NL);
  135. }
  136. /*
  137. * An variation of dlm_lock_sync, which make lock request could
  138. * be interrupted
  139. */
  140. static int dlm_lock_sync_interruptible(struct dlm_lock_resource *res, int mode,
  141. struct mddev *mddev)
  142. {
  143. int ret = 0;
  144. ret = dlm_lock(res->ls, mode, &res->lksb,
  145. res->flags, res->name, strlen(res->name),
  146. 0, sync_ast, res, res->bast);
  147. if (ret)
  148. return ret;
  149. wait_event(res->sync_locking, res->sync_locking_done
  150. || kthread_should_stop()
  151. || test_bit(MD_CLOSING, &mddev->flags));
  152. if (!res->sync_locking_done) {
  153. /*
  154. * the convert queue contains the lock request when request is
  155. * interrupted, and sync_ast could still be run, so need to
  156. * cancel the request and reset completion
  157. */
  158. ret = dlm_unlock(res->ls, res->lksb.sb_lkid, DLM_LKF_CANCEL,
  159. &res->lksb, res);
  160. res->sync_locking_done = false;
  161. if (unlikely(ret != 0))
  162. pr_info("failed to cancel previous lock request "
  163. "%s return %d\n", res->name, ret);
  164. return -EPERM;
  165. } else
  166. res->sync_locking_done = false;
  167. if (res->lksb.sb_status == 0)
  168. res->mode = mode;
  169. return res->lksb.sb_status;
  170. }
  171. static struct dlm_lock_resource *lockres_init(struct mddev *mddev,
  172. char *name, void (*bastfn)(void *arg, int mode), int with_lvb)
  173. {
  174. struct dlm_lock_resource *res = NULL;
  175. int ret, namelen;
  176. struct md_cluster_info *cinfo = mddev->cluster_info;
  177. res = kzalloc_obj(struct dlm_lock_resource);
  178. if (!res)
  179. return NULL;
  180. init_waitqueue_head(&res->sync_locking);
  181. res->sync_locking_done = false;
  182. res->ls = cinfo->lockspace;
  183. res->mddev = mddev;
  184. res->mode = DLM_LOCK_IV;
  185. namelen = strlen(name);
  186. res->name = kzalloc(namelen + 1, GFP_KERNEL);
  187. if (!res->name) {
  188. pr_err("md-cluster: Unable to allocate resource name for resource %s\n", name);
  189. goto out_err;
  190. }
  191. strscpy(res->name, name, namelen + 1);
  192. if (with_lvb) {
  193. res->lksb.sb_lvbptr = kzalloc(LVB_SIZE, GFP_KERNEL);
  194. if (!res->lksb.sb_lvbptr) {
  195. pr_err("md-cluster: Unable to allocate LVB for resource %s\n", name);
  196. goto out_err;
  197. }
  198. res->flags = DLM_LKF_VALBLK;
  199. }
  200. if (bastfn)
  201. res->bast = bastfn;
  202. res->flags |= DLM_LKF_EXPEDITE;
  203. ret = dlm_lock_sync(res, DLM_LOCK_NL);
  204. if (ret) {
  205. pr_err("md-cluster: Unable to lock NL on new lock resource %s\n", name);
  206. goto out_err;
  207. }
  208. res->flags &= ~DLM_LKF_EXPEDITE;
  209. res->flags |= DLM_LKF_CONVERT;
  210. return res;
  211. out_err:
  212. kfree(res->lksb.sb_lvbptr);
  213. kfree(res->name);
  214. kfree(res);
  215. return NULL;
  216. }
  217. static void lockres_free(struct dlm_lock_resource *res)
  218. {
  219. int ret = 0;
  220. if (!res)
  221. return;
  222. /*
  223. * use FORCEUNLOCK flag, so we can unlock even the lock is on the
  224. * waiting or convert queue
  225. */
  226. ret = dlm_unlock(res->ls, res->lksb.sb_lkid, DLM_LKF_FORCEUNLOCK,
  227. &res->lksb, res);
  228. if (unlikely(ret != 0))
  229. pr_err("failed to unlock %s return %d\n", res->name, ret);
  230. else
  231. wait_event(res->sync_locking, res->sync_locking_done);
  232. kfree(res->name);
  233. kfree(res->lksb.sb_lvbptr);
  234. kfree(res);
  235. }
  236. static void add_resync_info(struct dlm_lock_resource *lockres,
  237. sector_t lo, sector_t hi)
  238. {
  239. struct resync_info *ri;
  240. ri = (struct resync_info *)lockres->lksb.sb_lvbptr;
  241. ri->lo = cpu_to_le64(lo);
  242. ri->hi = cpu_to_le64(hi);
  243. }
  244. static int read_resync_info(struct mddev *mddev,
  245. struct dlm_lock_resource *lockres)
  246. {
  247. struct resync_info ri;
  248. struct md_cluster_info *cinfo = mddev->cluster_info;
  249. int ret = 0;
  250. dlm_lock_sync(lockres, DLM_LOCK_CR);
  251. memcpy(&ri, lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
  252. if (le64_to_cpu(ri.hi) > 0) {
  253. cinfo->suspend_hi = le64_to_cpu(ri.hi);
  254. cinfo->suspend_lo = le64_to_cpu(ri.lo);
  255. ret = 1;
  256. }
  257. dlm_unlock_sync(lockres);
  258. return ret;
  259. }
  260. static void recover_bitmaps(struct md_thread *thread)
  261. {
  262. struct mddev *mddev = thread->mddev;
  263. struct md_cluster_info *cinfo = mddev->cluster_info;
  264. struct dlm_lock_resource *bm_lockres;
  265. char str[64];
  266. int slot, ret;
  267. sector_t lo, hi;
  268. while (cinfo->recovery_map) {
  269. slot = fls64((u64)cinfo->recovery_map) - 1;
  270. snprintf(str, 64, "bitmap%04d", slot);
  271. bm_lockres = lockres_init(mddev, str, NULL, 1);
  272. if (!bm_lockres) {
  273. pr_err("md-cluster: Cannot initialize bitmaps\n");
  274. goto clear_bit;
  275. }
  276. ret = dlm_lock_sync_interruptible(bm_lockres, DLM_LOCK_PW, mddev);
  277. if (ret) {
  278. pr_err("md-cluster: Could not DLM lock %s: %d\n",
  279. str, ret);
  280. goto clear_bit;
  281. }
  282. ret = mddev->bitmap_ops->copy_from_slot(mddev, slot, &lo, &hi, true);
  283. if (ret) {
  284. pr_err("md-cluster: Could not copy data from bitmap %d\n", slot);
  285. goto clear_bit;
  286. }
  287. /* Clear suspend_area associated with the bitmap */
  288. spin_lock_irq(&cinfo->suspend_lock);
  289. cinfo->suspend_hi = 0;
  290. cinfo->suspend_lo = 0;
  291. cinfo->suspend_from = -1;
  292. spin_unlock_irq(&cinfo->suspend_lock);
  293. /* Kick off a reshape if needed */
  294. if (test_bit(MD_RESYNCING_REMOTE, &mddev->recovery) &&
  295. test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
  296. mddev->reshape_position != MaxSector)
  297. md_wakeup_thread(mddev->sync_thread);
  298. if (hi > 0) {
  299. if (lo < mddev->resync_offset)
  300. mddev->resync_offset = lo;
  301. /* wake up thread to continue resync in case resync
  302. * is not finished */
  303. if (mddev->resync_offset != MaxSector) {
  304. /*
  305. * clear the REMOTE flag since we will launch
  306. * resync thread in current node.
  307. */
  308. clear_bit(MD_RESYNCING_REMOTE,
  309. &mddev->recovery);
  310. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  311. md_wakeup_thread(mddev->thread);
  312. }
  313. }
  314. clear_bit:
  315. lockres_free(bm_lockres);
  316. clear_bit(slot, &cinfo->recovery_map);
  317. }
  318. }
  319. static void recover_prep(void *arg)
  320. {
  321. struct mddev *mddev = arg;
  322. struct md_cluster_info *cinfo = mddev->cluster_info;
  323. set_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
  324. }
  325. static void __recover_slot(struct mddev *mddev, int slot)
  326. {
  327. struct md_cluster_info *cinfo = mddev->cluster_info;
  328. set_bit(slot, &cinfo->recovery_map);
  329. if (!cinfo->recovery_thread) {
  330. rcu_assign_pointer(cinfo->recovery_thread,
  331. md_register_thread(recover_bitmaps, mddev, "recover"));
  332. if (!cinfo->recovery_thread) {
  333. pr_warn("md-cluster: Could not create recovery thread\n");
  334. return;
  335. }
  336. }
  337. md_wakeup_thread(cinfo->recovery_thread);
  338. }
  339. static void recover_slot(void *arg, struct dlm_slot *slot)
  340. {
  341. struct mddev *mddev = arg;
  342. struct md_cluster_info *cinfo = mddev->cluster_info;
  343. pr_info("md-cluster: %s Node %d/%d down. My slot: %d. Initiating recovery.\n",
  344. mddev->bitmap_info.cluster_name,
  345. slot->nodeid, slot->slot,
  346. cinfo->slot_number);
  347. /* deduct one since dlm slot starts from one while the num of
  348. * cluster-md begins with 0 */
  349. __recover_slot(mddev, slot->slot - 1);
  350. }
  351. static void recover_done(void *arg, struct dlm_slot *slots,
  352. int num_slots, int our_slot,
  353. uint32_t generation)
  354. {
  355. struct mddev *mddev = arg;
  356. struct md_cluster_info *cinfo = mddev->cluster_info;
  357. cinfo->slot_number = our_slot;
  358. /* completion is only need to be complete when node join cluster,
  359. * it doesn't need to run during another node's failure */
  360. if (test_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state)) {
  361. complete(&cinfo->completion);
  362. clear_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
  363. }
  364. clear_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state);
  365. }
  366. /* the ops is called when node join the cluster, and do lock recovery
  367. * if node failure occurs */
  368. static const struct dlm_lockspace_ops md_ls_ops = {
  369. .recover_prep = recover_prep,
  370. .recover_slot = recover_slot,
  371. .recover_done = recover_done,
  372. };
  373. /*
  374. * The BAST function for the ack lock resource
  375. * This function wakes up the receive thread in
  376. * order to receive and process the message.
  377. */
  378. static void ack_bast(void *arg, int mode)
  379. {
  380. struct dlm_lock_resource *res = arg;
  381. struct md_cluster_info *cinfo = res->mddev->cluster_info;
  382. if (mode == DLM_LOCK_EX) {
  383. if (test_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state))
  384. md_wakeup_thread(cinfo->recv_thread);
  385. else
  386. set_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state);
  387. }
  388. }
  389. static void remove_suspend_info(struct mddev *mddev, int slot)
  390. {
  391. struct md_cluster_info *cinfo = mddev->cluster_info;
  392. mddev->pers->quiesce(mddev, 1);
  393. spin_lock_irq(&cinfo->suspend_lock);
  394. cinfo->suspend_hi = 0;
  395. cinfo->suspend_lo = 0;
  396. spin_unlock_irq(&cinfo->suspend_lock);
  397. mddev->pers->quiesce(mddev, 0);
  398. }
  399. static void process_suspend_info(struct mddev *mddev,
  400. int slot, sector_t lo, sector_t hi)
  401. {
  402. struct md_cluster_info *cinfo = mddev->cluster_info;
  403. struct mdp_superblock_1 *sb = NULL;
  404. struct md_rdev *rdev;
  405. if (!hi) {
  406. /*
  407. * clear the REMOTE flag since resync or recovery is finished
  408. * in remote node.
  409. */
  410. clear_bit(MD_RESYNCING_REMOTE, &mddev->recovery);
  411. remove_suspend_info(mddev, slot);
  412. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  413. clear_bit(MD_CLUSTER_WAITING_FOR_SYNC, &cinfo->state);
  414. md_wakeup_thread(mddev->thread);
  415. return;
  416. }
  417. rdev_for_each(rdev, mddev)
  418. if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
  419. sb = page_address(rdev->sb_page);
  420. break;
  421. }
  422. /*
  423. * The bitmaps are not same for different nodes
  424. * if RESYNCING is happening in one node, then
  425. * the node which received the RESYNCING message
  426. * probably will perform resync with the region
  427. * [lo, hi] again, so we could reduce resync time
  428. * a lot if we can ensure that the bitmaps among
  429. * different nodes are match up well.
  430. *
  431. * sync_low/hi is used to record the region which
  432. * arrived in the previous RESYNCING message,
  433. *
  434. * Call md_bitmap_sync_with_cluster to clear NEEDED_MASK
  435. * and set RESYNC_MASK since resync thread is running
  436. * in another node, so we don't need to do the resync
  437. * again with the same section.
  438. *
  439. * Skip md_bitmap_sync_with_cluster in case reshape
  440. * happening, because reshaping region is small and
  441. * we don't want to trigger lots of WARN.
  442. */
  443. if (sb && !(le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE))
  444. mddev->bitmap_ops->sync_with_cluster(mddev, cinfo->sync_low,
  445. cinfo->sync_hi, lo, hi);
  446. cinfo->sync_low = lo;
  447. cinfo->sync_hi = hi;
  448. mddev->pers->quiesce(mddev, 1);
  449. spin_lock_irq(&cinfo->suspend_lock);
  450. cinfo->suspend_from = slot;
  451. cinfo->suspend_lo = lo;
  452. cinfo->suspend_hi = hi;
  453. spin_unlock_irq(&cinfo->suspend_lock);
  454. mddev->pers->quiesce(mddev, 0);
  455. }
  456. static int process_add_new_disk(struct mddev *mddev, struct cluster_msg *cmsg)
  457. {
  458. char disk_uuid[64];
  459. struct md_cluster_info *cinfo = mddev->cluster_info;
  460. char event_name[] = "EVENT=ADD_DEVICE";
  461. char raid_slot[16];
  462. char *envp[] = {event_name, disk_uuid, raid_slot, NULL};
  463. int len;
  464. int res = 0;
  465. len = snprintf(disk_uuid, 64, "DEVICE_UUID=");
  466. sprintf(disk_uuid + len, "%pU", cmsg->uuid);
  467. snprintf(raid_slot, 16, "RAID_DISK=%d", le32_to_cpu(cmsg->raid_slot));
  468. pr_info("%s:%d Sending kobject change with %s and %s\n", __func__, __LINE__, disk_uuid, raid_slot);
  469. init_completion(&cinfo->newdisk_completion);
  470. set_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
  471. kobject_uevent_env(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE, envp);
  472. if (!wait_for_completion_timeout(&cinfo->newdisk_completion,
  473. NEW_DEV_TIMEOUT)) {
  474. pr_err("md-cluster(%s:%d): timeout on a new disk adding\n",
  475. __func__, __LINE__);
  476. res = -1;
  477. }
  478. clear_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state);
  479. set_bit(MD_CLUSTER_WAITING_FOR_SYNC, &cinfo->state);
  480. return res;
  481. }
  482. static void process_metadata_update(struct mddev *mddev, struct cluster_msg *msg)
  483. {
  484. int got_lock = 0;
  485. struct md_thread *thread;
  486. struct md_cluster_info *cinfo = mddev->cluster_info;
  487. mddev->good_device_nr = le32_to_cpu(msg->raid_slot);
  488. dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
  489. thread = rcu_dereference_protected(mddev->thread, true);
  490. if (!thread) {
  491. pr_warn("md-cluster: Received metadata update but MD thread is not ready\n");
  492. dlm_unlock_sync(cinfo->no_new_dev_lockres);
  493. return;
  494. }
  495. wait_event(thread->wqueue,
  496. (got_lock = mddev_trylock(mddev)) ||
  497. test_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state));
  498. md_reload_sb(mddev, mddev->good_device_nr);
  499. if (got_lock)
  500. mddev_unlock(mddev);
  501. }
  502. static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
  503. {
  504. struct md_rdev *rdev;
  505. rcu_read_lock();
  506. rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot));
  507. if (rdev) {
  508. set_bit(ClusterRemove, &rdev->flags);
  509. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  510. md_wakeup_thread(mddev->thread);
  511. }
  512. else
  513. pr_warn("%s: %d Could not find disk(%d) to REMOVE\n",
  514. __func__, __LINE__, le32_to_cpu(msg->raid_slot));
  515. rcu_read_unlock();
  516. }
  517. static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
  518. {
  519. struct md_rdev *rdev;
  520. rcu_read_lock();
  521. rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot));
  522. if (rdev && test_bit(Faulty, &rdev->flags))
  523. clear_bit(Faulty, &rdev->flags);
  524. else
  525. pr_warn("%s: %d Could not find disk(%d) which is faulty",
  526. __func__, __LINE__, le32_to_cpu(msg->raid_slot));
  527. rcu_read_unlock();
  528. }
  529. static int process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
  530. {
  531. int ret = 0;
  532. if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot),
  533. "node %d received its own msg\n", le32_to_cpu(msg->slot)))
  534. return -1;
  535. switch (le32_to_cpu(msg->type)) {
  536. case METADATA_UPDATED:
  537. process_metadata_update(mddev, msg);
  538. break;
  539. case CHANGE_CAPACITY:
  540. set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
  541. break;
  542. case RESYNCING_START:
  543. clear_bit(MD_CLUSTER_WAITING_FOR_SYNC, &mddev->cluster_info->state);
  544. break;
  545. case RESYNCING:
  546. set_bit(MD_RESYNCING_REMOTE, &mddev->recovery);
  547. process_suspend_info(mddev, le32_to_cpu(msg->slot),
  548. le64_to_cpu(msg->low),
  549. le64_to_cpu(msg->high));
  550. break;
  551. case NEWDISK:
  552. if (process_add_new_disk(mddev, msg))
  553. ret = -1;
  554. break;
  555. case REMOVE:
  556. process_remove_disk(mddev, msg);
  557. break;
  558. case RE_ADD:
  559. process_readd_disk(mddev, msg);
  560. break;
  561. case BITMAP_NEEDS_SYNC:
  562. __recover_slot(mddev, le32_to_cpu(msg->slot));
  563. break;
  564. case BITMAP_RESIZE:
  565. if (le64_to_cpu(msg->high) != mddev->pers->size(mddev, 0, 0))
  566. ret = mddev->bitmap_ops->resize(mddev,
  567. le64_to_cpu(msg->high),
  568. 0);
  569. break;
  570. default:
  571. ret = -1;
  572. pr_warn("%s:%d Received unknown message from %d\n",
  573. __func__, __LINE__, msg->slot);
  574. }
  575. return ret;
  576. }
  577. /*
  578. * thread for receiving message
  579. */
  580. static void recv_daemon(struct md_thread *thread)
  581. {
  582. struct md_cluster_info *cinfo = thread->mddev->cluster_info;
  583. struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
  584. struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
  585. struct cluster_msg msg;
  586. int ret;
  587. mutex_lock(&cinfo->recv_mutex);
  588. /*get CR on Message*/
  589. if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
  590. pr_err("md/raid1:failed to get CR on MESSAGE\n");
  591. mutex_unlock(&cinfo->recv_mutex);
  592. return;
  593. }
  594. /* read lvb and wake up thread to process this message_lockres */
  595. memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
  596. ret = process_recvd_msg(thread->mddev, &msg);
  597. if (ret)
  598. goto out;
  599. /*release CR on ack_lockres*/
  600. ret = dlm_unlock_sync(ack_lockres);
  601. if (unlikely(ret != 0))
  602. pr_info("unlock ack failed return %d\n", ret);
  603. /*up-convert to PR on message_lockres*/
  604. ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
  605. if (unlikely(ret != 0))
  606. pr_info("lock PR on msg failed return %d\n", ret);
  607. /*get CR on ack_lockres again*/
  608. ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
  609. if (unlikely(ret != 0))
  610. pr_info("lock CR on ack failed return %d\n", ret);
  611. out:
  612. /*release CR on message_lockres*/
  613. ret = dlm_unlock_sync(message_lockres);
  614. if (unlikely(ret != 0))
  615. pr_info("unlock msg failed return %d\n", ret);
  616. mutex_unlock(&cinfo->recv_mutex);
  617. }
  618. /* lock_token()
  619. * Takes the lock on the TOKEN lock resource so no other
  620. * node can communicate while the operation is underway.
  621. */
  622. static int lock_token(struct md_cluster_info *cinfo)
  623. {
  624. int error;
  625. error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
  626. if (error) {
  627. pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
  628. __func__, __LINE__, error);
  629. } else {
  630. /* Lock the receive sequence */
  631. mutex_lock(&cinfo->recv_mutex);
  632. }
  633. return error;
  634. }
  635. /* lock_comm()
  636. * Sets the MD_CLUSTER_SEND_LOCK bit to lock the send channel.
  637. */
  638. static int lock_comm(struct md_cluster_info *cinfo, bool mddev_locked)
  639. {
  640. int rv, set_bit = 0;
  641. struct mddev *mddev = cinfo->mddev;
  642. /*
  643. * If resync thread run after raid1d thread, then process_metadata_update
  644. * could not continue if raid1d held reconfig_mutex (and raid1d is blocked
  645. * since another node already got EX on Token and waiting the EX of Ack),
  646. * so let resync wake up thread in case flag is set.
  647. */
  648. if (mddev_locked && !test_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
  649. &cinfo->state)) {
  650. rv = test_and_set_bit_lock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
  651. &cinfo->state);
  652. WARN_ON_ONCE(rv);
  653. md_wakeup_thread(mddev->thread);
  654. set_bit = 1;
  655. }
  656. wait_event(cinfo->wait,
  657. !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state));
  658. rv = lock_token(cinfo);
  659. if (set_bit)
  660. clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
  661. return rv;
  662. }
  663. static void unlock_comm(struct md_cluster_info *cinfo)
  664. {
  665. WARN_ON(cinfo->token_lockres->mode != DLM_LOCK_EX);
  666. mutex_unlock(&cinfo->recv_mutex);
  667. dlm_unlock_sync(cinfo->token_lockres);
  668. clear_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state);
  669. wake_up(&cinfo->wait);
  670. }
  671. /* __sendmsg()
  672. * This function performs the actual sending of the message. This function is
  673. * usually called after performing the encompassing operation
  674. * The function:
  675. * 1. Grabs the message lockresource in EX mode
  676. * 2. Copies the message to the message LVB
  677. * 3. Downconverts message lockresource to CW
  678. * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
  679. * and the other nodes read the message. The thread will wait here until all other
  680. * nodes have released ack lock resource.
  681. * 5. Downconvert ack lockresource to CR
  682. */
  683. static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
  684. {
  685. int error, unlock_error;
  686. int slot = cinfo->slot_number - 1;
  687. cmsg->slot = cpu_to_le32(slot);
  688. /*get EX on Message*/
  689. error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
  690. if (error) {
  691. pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
  692. return error;
  693. }
  694. memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
  695. sizeof(struct cluster_msg));
  696. /*down-convert EX to CW on Message*/
  697. error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
  698. if (error) {
  699. pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
  700. error);
  701. goto failed_ack;
  702. }
  703. /*up-convert CR to EX on Ack*/
  704. error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
  705. if (error) {
  706. pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
  707. error);
  708. goto failed_ack;
  709. }
  710. /*down-convert EX to CR on Ack*/
  711. error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
  712. if (error) {
  713. pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
  714. error);
  715. goto failed_ack;
  716. }
  717. failed_ack:
  718. while ((unlock_error = dlm_unlock_sync(cinfo->message_lockres)))
  719. pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
  720. unlock_error);
  721. return error;
  722. }
  723. static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg,
  724. bool mddev_locked)
  725. {
  726. int ret;
  727. ret = lock_comm(cinfo, mddev_locked);
  728. if (!ret) {
  729. ret = __sendmsg(cinfo, cmsg);
  730. unlock_comm(cinfo);
  731. }
  732. return ret;
  733. }
  734. static int gather_all_resync_info(struct mddev *mddev, int total_slots)
  735. {
  736. struct md_cluster_info *cinfo = mddev->cluster_info;
  737. int i, ret = 0;
  738. struct dlm_lock_resource *bm_lockres;
  739. char str[64];
  740. sector_t lo, hi;
  741. for (i = 0; i < total_slots; i++) {
  742. memset(str, '\0', 64);
  743. snprintf(str, 64, "bitmap%04d", i);
  744. bm_lockres = lockres_init(mddev, str, NULL, 1);
  745. if (!bm_lockres)
  746. return -ENOMEM;
  747. if (i == (cinfo->slot_number - 1)) {
  748. lockres_free(bm_lockres);
  749. continue;
  750. }
  751. bm_lockres->flags |= DLM_LKF_NOQUEUE;
  752. ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
  753. if (ret == -EAGAIN) {
  754. if (read_resync_info(mddev, bm_lockres)) {
  755. pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
  756. __func__, __LINE__,
  757. (unsigned long long) cinfo->suspend_lo,
  758. (unsigned long long) cinfo->suspend_hi,
  759. i);
  760. cinfo->suspend_from = i;
  761. }
  762. ret = 0;
  763. lockres_free(bm_lockres);
  764. continue;
  765. }
  766. if (ret) {
  767. lockres_free(bm_lockres);
  768. goto out;
  769. }
  770. /* Read the disk bitmap sb and check if it needs recovery */
  771. ret = mddev->bitmap_ops->copy_from_slot(mddev, i, &lo, &hi, false);
  772. if (ret) {
  773. pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
  774. lockres_free(bm_lockres);
  775. continue;
  776. }
  777. if ((hi > 0) && (lo < mddev->resync_offset)) {
  778. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  779. mddev->resync_offset = lo;
  780. md_check_recovery(mddev);
  781. }
  782. lockres_free(bm_lockres);
  783. }
  784. out:
  785. return ret;
  786. }
  787. static int join(struct mddev *mddev, int nodes)
  788. {
  789. struct md_cluster_info *cinfo;
  790. int ret, ops_rv;
  791. char str[64];
  792. cinfo = kzalloc_obj(struct md_cluster_info);
  793. if (!cinfo)
  794. return -ENOMEM;
  795. INIT_LIST_HEAD(&cinfo->suspend_list);
  796. spin_lock_init(&cinfo->suspend_lock);
  797. init_completion(&cinfo->completion);
  798. set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
  799. init_waitqueue_head(&cinfo->wait);
  800. mutex_init(&cinfo->recv_mutex);
  801. mddev->cluster_info = cinfo;
  802. cinfo->mddev = mddev;
  803. memset(str, 0, 64);
  804. sprintf(str, "%pU", mddev->uuid);
  805. ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
  806. DLM_LSFL_SOFTIRQ, LVB_SIZE, &md_ls_ops, mddev,
  807. &ops_rv, &cinfo->lockspace);
  808. if (ret)
  809. goto err;
  810. wait_for_completion(&cinfo->completion);
  811. if (nodes < cinfo->slot_number) {
  812. pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
  813. cinfo->slot_number, nodes);
  814. ret = -ERANGE;
  815. goto err;
  816. }
  817. /* Initiate the communication resources */
  818. ret = -ENOMEM;
  819. rcu_assign_pointer(cinfo->recv_thread,
  820. md_register_thread(recv_daemon, mddev, "cluster_recv"));
  821. if (!cinfo->recv_thread) {
  822. pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
  823. goto err;
  824. }
  825. cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
  826. if (!cinfo->message_lockres)
  827. goto err;
  828. cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
  829. if (!cinfo->token_lockres)
  830. goto err;
  831. cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
  832. if (!cinfo->no_new_dev_lockres)
  833. goto err;
  834. ret = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
  835. if (ret) {
  836. ret = -EAGAIN;
  837. pr_err("md-cluster: can't join cluster to avoid lock issue\n");
  838. goto err;
  839. }
  840. cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
  841. if (!cinfo->ack_lockres) {
  842. ret = -ENOMEM;
  843. goto err;
  844. }
  845. /* get sync CR lock on ACK. */
  846. if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
  847. pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
  848. ret);
  849. dlm_unlock_sync(cinfo->token_lockres);
  850. /* get sync CR lock on no-new-dev. */
  851. if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
  852. pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
  853. pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
  854. snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
  855. cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
  856. if (!cinfo->bitmap_lockres) {
  857. ret = -ENOMEM;
  858. goto err;
  859. }
  860. if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
  861. pr_err("Failed to get bitmap lock\n");
  862. ret = -EINVAL;
  863. goto err;
  864. }
  865. cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0);
  866. if (!cinfo->resync_lockres) {
  867. ret = -ENOMEM;
  868. goto err;
  869. }
  870. return 0;
  871. err:
  872. set_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
  873. md_unregister_thread(mddev, &cinfo->recovery_thread);
  874. md_unregister_thread(mddev, &cinfo->recv_thread);
  875. lockres_free(cinfo->message_lockres);
  876. lockres_free(cinfo->token_lockres);
  877. lockres_free(cinfo->ack_lockres);
  878. lockres_free(cinfo->no_new_dev_lockres);
  879. lockres_free(cinfo->resync_lockres);
  880. lockres_free(cinfo->bitmap_lockres);
  881. if (cinfo->lockspace)
  882. dlm_release_lockspace(cinfo->lockspace, DLM_RELEASE_NORMAL);
  883. mddev->cluster_info = NULL;
  884. kfree(cinfo);
  885. return ret;
  886. }
  887. static void load_bitmaps(struct mddev *mddev, int total_slots)
  888. {
  889. struct md_cluster_info *cinfo = mddev->cluster_info;
  890. /* load all the node's bitmap info for resync */
  891. if (gather_all_resync_info(mddev, total_slots))
  892. pr_err("md-cluster: failed to gather all resyn infos\n");
  893. set_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state);
  894. /* wake up recv thread in case something need to be handled */
  895. if (test_and_clear_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state))
  896. md_wakeup_thread(cinfo->recv_thread);
  897. }
  898. static void resync_bitmap(struct mddev *mddev)
  899. {
  900. struct md_cluster_info *cinfo = mddev->cluster_info;
  901. struct cluster_msg cmsg = {0};
  902. int err;
  903. cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
  904. err = sendmsg(cinfo, &cmsg, 1);
  905. if (err)
  906. pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n",
  907. __func__, __LINE__, err);
  908. }
  909. static void unlock_all_bitmaps(struct mddev *mddev);
  910. static int leave(struct mddev *mddev)
  911. {
  912. struct md_cluster_info *cinfo = mddev->cluster_info;
  913. if (!cinfo)
  914. return 0;
  915. /*
  916. * BITMAP_NEEDS_SYNC message should be sent when node
  917. * is leaving the cluster with dirty bitmap, also we
  918. * can only deliver it when dlm connection is available.
  919. *
  920. * Also, we should send BITMAP_NEEDS_SYNC message in
  921. * case reshaping is interrupted.
  922. */
  923. if ((cinfo->slot_number > 0 && mddev->resync_offset != MaxSector) ||
  924. (mddev->reshape_position != MaxSector &&
  925. test_bit(MD_CLOSING, &mddev->flags)))
  926. resync_bitmap(mddev);
  927. set_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
  928. md_unregister_thread(mddev, &cinfo->recovery_thread);
  929. md_unregister_thread(mddev, &cinfo->recv_thread);
  930. lockres_free(cinfo->message_lockres);
  931. lockres_free(cinfo->token_lockres);
  932. lockres_free(cinfo->ack_lockres);
  933. lockres_free(cinfo->no_new_dev_lockres);
  934. lockres_free(cinfo->resync_lockres);
  935. lockres_free(cinfo->bitmap_lockres);
  936. unlock_all_bitmaps(mddev);
  937. dlm_release_lockspace(cinfo->lockspace, DLM_RELEASE_NORMAL);
  938. kfree(cinfo);
  939. return 0;
  940. }
  941. /* slot_number(): Returns the MD slot number to use
  942. * DLM starts the slot numbers from 1, wheras cluster-md
  943. * wants the number to be from zero, so we deduct one
  944. */
  945. static int slot_number(struct mddev *mddev)
  946. {
  947. struct md_cluster_info *cinfo = mddev->cluster_info;
  948. return cinfo->slot_number - 1;
  949. }
  950. /*
  951. * Check if the communication is already locked, else lock the communication
  952. * channel.
  953. * If it is already locked, token is in EX mode, and hence lock_token()
  954. * should not be called.
  955. */
  956. static int metadata_update_start(struct mddev *mddev)
  957. {
  958. struct md_cluster_info *cinfo = mddev->cluster_info;
  959. int ret;
  960. /*
  961. * metadata_update_start is always called with the protection of
  962. * reconfig_mutex, so set WAITING_FOR_TOKEN here.
  963. */
  964. ret = test_and_set_bit_lock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
  965. &cinfo->state);
  966. WARN_ON_ONCE(ret);
  967. md_wakeup_thread(mddev->thread);
  968. wait_event(cinfo->wait,
  969. !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state) ||
  970. test_and_clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state));
  971. /* If token is already locked, return 0 */
  972. if (cinfo->token_lockres->mode == DLM_LOCK_EX) {
  973. clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
  974. return 0;
  975. }
  976. ret = lock_token(cinfo);
  977. clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
  978. return ret;
  979. }
  980. static int metadata_update_finish(struct mddev *mddev)
  981. {
  982. struct md_cluster_info *cinfo = mddev->cluster_info;
  983. struct cluster_msg cmsg;
  984. struct md_rdev *rdev;
  985. int ret = 0;
  986. int raid_slot = -1;
  987. memset(&cmsg, 0, sizeof(cmsg));
  988. cmsg.type = cpu_to_le32(METADATA_UPDATED);
  989. /* Pick up a good active device number to send.
  990. */
  991. rdev_for_each(rdev, mddev)
  992. if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
  993. raid_slot = rdev->desc_nr;
  994. break;
  995. }
  996. if (raid_slot >= 0) {
  997. cmsg.raid_slot = cpu_to_le32(raid_slot);
  998. ret = __sendmsg(cinfo, &cmsg);
  999. } else
  1000. pr_warn("md-cluster: No good device id found to send\n");
  1001. clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  1002. unlock_comm(cinfo);
  1003. return ret;
  1004. }
  1005. static void metadata_update_cancel(struct mddev *mddev)
  1006. {
  1007. struct md_cluster_info *cinfo = mddev->cluster_info;
  1008. clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  1009. unlock_comm(cinfo);
  1010. }
  1011. static int update_bitmap_size(struct mddev *mddev, sector_t size)
  1012. {
  1013. struct md_cluster_info *cinfo = mddev->cluster_info;
  1014. struct cluster_msg cmsg = {0};
  1015. int ret;
  1016. cmsg.type = cpu_to_le32(BITMAP_RESIZE);
  1017. cmsg.high = cpu_to_le64(size);
  1018. ret = sendmsg(cinfo, &cmsg, 0);
  1019. if (ret)
  1020. pr_err("%s:%d: failed to send BITMAP_RESIZE message (%d)\n",
  1021. __func__, __LINE__, ret);
  1022. return ret;
  1023. }
  1024. static int resize_bitmaps(struct mddev *mddev, sector_t newsize, sector_t oldsize)
  1025. {
  1026. void *bitmap = mddev->bitmap;
  1027. struct md_bitmap_stats stats;
  1028. unsigned long my_pages;
  1029. int i, rv;
  1030. rv = mddev->bitmap_ops->get_stats(bitmap, &stats);
  1031. if (rv)
  1032. return rv;
  1033. my_pages = stats.pages;
  1034. /*
  1035. * We need to ensure all the nodes can grow to a larger
  1036. * bitmap size before make the reshaping.
  1037. */
  1038. rv = update_bitmap_size(mddev, newsize);
  1039. if (rv)
  1040. return rv;
  1041. for (i = 0; i < mddev->bitmap_info.nodes; i++) {
  1042. struct dlm_lock_resource *bm_lockres;
  1043. char str[64];
  1044. if (i == slot_number(mddev))
  1045. continue;
  1046. bitmap = mddev->bitmap_ops->get_from_slot(mddev, i);
  1047. if (IS_ERR(bitmap)) {
  1048. pr_err("can't get bitmap from slot %d\n", i);
  1049. bitmap = NULL;
  1050. goto out;
  1051. }
  1052. rv = mddev->bitmap_ops->get_stats(bitmap, &stats);
  1053. if (rv)
  1054. goto out;
  1055. /*
  1056. * If we can hold the bitmap lock of one node then
  1057. * the slot is not occupied, update the pages.
  1058. */
  1059. snprintf(str, 64, "bitmap%04d", i);
  1060. bm_lockres = lockres_init(mddev, str, NULL, 1);
  1061. if (!bm_lockres) {
  1062. pr_err("Cannot initialize %s lock\n", str);
  1063. goto out;
  1064. }
  1065. bm_lockres->flags |= DLM_LKF_NOQUEUE;
  1066. rv = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
  1067. if (!rv)
  1068. mddev->bitmap_ops->set_pages(bitmap, my_pages);
  1069. lockres_free(bm_lockres);
  1070. if (my_pages != stats.pages)
  1071. /*
  1072. * Let's revert the bitmap size if one node
  1073. * can't resize bitmap
  1074. */
  1075. goto out;
  1076. mddev->bitmap_ops->free(bitmap);
  1077. }
  1078. return 0;
  1079. out:
  1080. mddev->bitmap_ops->free(bitmap);
  1081. update_bitmap_size(mddev, oldsize);
  1082. return -1;
  1083. }
  1084. /*
  1085. * return 0 if all the bitmaps have the same sync_size
  1086. */
  1087. static int cluster_check_sync_size(struct mddev *mddev)
  1088. {
  1089. int current_slot = slot_number(mddev);
  1090. int node_num = mddev->bitmap_info.nodes;
  1091. struct dlm_lock_resource *bm_lockres;
  1092. struct md_bitmap_stats stats;
  1093. void *bitmap = mddev->bitmap;
  1094. unsigned long sync_size = 0;
  1095. unsigned long my_sync_size;
  1096. char str[64];
  1097. int i, rv;
  1098. rv = mddev->bitmap_ops->get_stats(bitmap, &stats);
  1099. if (rv)
  1100. return rv;
  1101. my_sync_size = stats.sync_size;
  1102. for (i = 0; i < node_num; i++) {
  1103. if (i == current_slot)
  1104. continue;
  1105. bitmap = mddev->bitmap_ops->get_from_slot(mddev, i);
  1106. if (IS_ERR(bitmap)) {
  1107. pr_err("can't get bitmap from slot %d\n", i);
  1108. return -1;
  1109. }
  1110. /*
  1111. * If we can hold the bitmap lock of one node then
  1112. * the slot is not occupied, update the sb.
  1113. */
  1114. snprintf(str, 64, "bitmap%04d", i);
  1115. bm_lockres = lockres_init(mddev, str, NULL, 1);
  1116. if (!bm_lockres) {
  1117. pr_err("md-cluster: Cannot initialize %s\n", str);
  1118. mddev->bitmap_ops->free(bitmap);
  1119. return -1;
  1120. }
  1121. bm_lockres->flags |= DLM_LKF_NOQUEUE;
  1122. rv = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
  1123. if (!rv)
  1124. mddev->bitmap_ops->update_sb(bitmap);
  1125. lockres_free(bm_lockres);
  1126. rv = mddev->bitmap_ops->get_stats(bitmap, &stats);
  1127. if (rv) {
  1128. mddev->bitmap_ops->free(bitmap);
  1129. return rv;
  1130. }
  1131. if (sync_size == 0) {
  1132. sync_size = stats.sync_size;
  1133. } else if (sync_size != stats.sync_size) {
  1134. mddev->bitmap_ops->free(bitmap);
  1135. return -1;
  1136. }
  1137. mddev->bitmap_ops->free(bitmap);
  1138. }
  1139. return (my_sync_size == sync_size) ? 0 : -1;
  1140. }
  1141. /*
  1142. * Update the size for cluster raid is a little more complex, we perform it
  1143. * by the steps:
  1144. * 1. hold token lock and update superblock in initiator node.
  1145. * 2. send METADATA_UPDATED msg to other nodes.
  1146. * 3. The initiator node continues to check each bitmap's sync_size, if all
  1147. * bitmaps have the same value of sync_size, then we can set capacity and
  1148. * let other nodes to perform it. If one node can't update sync_size
  1149. * accordingly, we need to revert to previous value.
  1150. */
  1151. static void update_size(struct mddev *mddev, sector_t old_dev_sectors)
  1152. {
  1153. struct md_cluster_info *cinfo = mddev->cluster_info;
  1154. struct cluster_msg cmsg;
  1155. struct md_rdev *rdev;
  1156. int ret = 0;
  1157. int raid_slot = -1;
  1158. md_update_sb(mddev, 1);
  1159. if (lock_comm(cinfo, 1)) {
  1160. pr_err("%s: lock_comm failed\n", __func__);
  1161. return;
  1162. }
  1163. memset(&cmsg, 0, sizeof(cmsg));
  1164. cmsg.type = cpu_to_le32(METADATA_UPDATED);
  1165. rdev_for_each(rdev, mddev)
  1166. if (rdev->raid_disk >= 0 && !test_bit(Faulty, &rdev->flags)) {
  1167. raid_slot = rdev->desc_nr;
  1168. break;
  1169. }
  1170. if (raid_slot >= 0) {
  1171. cmsg.raid_slot = cpu_to_le32(raid_slot);
  1172. /*
  1173. * We can only change capiticy after all the nodes can do it,
  1174. * so need to wait after other nodes already received the msg
  1175. * and handled the change
  1176. */
  1177. ret = __sendmsg(cinfo, &cmsg);
  1178. if (ret) {
  1179. pr_err("%s:%d: failed to send METADATA_UPDATED msg\n",
  1180. __func__, __LINE__);
  1181. unlock_comm(cinfo);
  1182. return;
  1183. }
  1184. } else {
  1185. pr_err("md-cluster: No good device id found to send\n");
  1186. unlock_comm(cinfo);
  1187. return;
  1188. }
  1189. /*
  1190. * check the sync_size from other node's bitmap, if sync_size
  1191. * have already updated in other nodes as expected, send an
  1192. * empty metadata msg to permit the change of capacity
  1193. */
  1194. if (cluster_check_sync_size(mddev) == 0) {
  1195. memset(&cmsg, 0, sizeof(cmsg));
  1196. cmsg.type = cpu_to_le32(CHANGE_CAPACITY);
  1197. ret = __sendmsg(cinfo, &cmsg);
  1198. if (ret)
  1199. pr_err("%s:%d: failed to send CHANGE_CAPACITY msg\n",
  1200. __func__, __LINE__);
  1201. set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
  1202. } else {
  1203. /* revert to previous sectors */
  1204. ret = mddev->pers->resize(mddev, old_dev_sectors);
  1205. ret = __sendmsg(cinfo, &cmsg);
  1206. if (ret)
  1207. pr_err("%s:%d: failed to send METADATA_UPDATED msg\n",
  1208. __func__, __LINE__);
  1209. }
  1210. unlock_comm(cinfo);
  1211. }
  1212. static int resync_start(struct mddev *mddev)
  1213. {
  1214. struct md_cluster_info *cinfo = mddev->cluster_info;
  1215. return dlm_lock_sync_interruptible(cinfo->resync_lockres, DLM_LOCK_EX, mddev);
  1216. }
  1217. static void resync_info_get(struct mddev *mddev, sector_t *lo, sector_t *hi)
  1218. {
  1219. struct md_cluster_info *cinfo = mddev->cluster_info;
  1220. spin_lock_irq(&cinfo->suspend_lock);
  1221. *lo = cinfo->suspend_lo;
  1222. *hi = cinfo->suspend_hi;
  1223. spin_unlock_irq(&cinfo->suspend_lock);
  1224. }
  1225. static int resync_status_get(struct mddev *mddev)
  1226. {
  1227. struct md_cluster_info *cinfo = mddev->cluster_info;
  1228. return test_bit(MD_CLUSTER_WAITING_FOR_SYNC, &cinfo->state);
  1229. }
  1230. static int resync_start_notify(struct mddev *mddev)
  1231. {
  1232. struct md_cluster_info *cinfo = mddev->cluster_info;
  1233. struct cluster_msg cmsg = {0};
  1234. cmsg.type = cpu_to_le32(RESYNCING_START);
  1235. return sendmsg(cinfo, &cmsg, 0);
  1236. }
  1237. static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
  1238. {
  1239. struct md_cluster_info *cinfo = mddev->cluster_info;
  1240. struct resync_info ri;
  1241. struct cluster_msg cmsg = {0};
  1242. /* do not send zero again, if we have sent before */
  1243. if (hi == 0) {
  1244. memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
  1245. if (le64_to_cpu(ri.hi) == 0)
  1246. return 0;
  1247. }
  1248. add_resync_info(cinfo->bitmap_lockres, lo, hi);
  1249. /* Re-acquire the lock to refresh LVB */
  1250. dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
  1251. cmsg.type = cpu_to_le32(RESYNCING);
  1252. cmsg.low = cpu_to_le64(lo);
  1253. cmsg.high = cpu_to_le64(hi);
  1254. /*
  1255. * mddev_lock is held if resync_info_update is called from
  1256. * resync_finish (md_reap_sync_thread -> resync_finish)
  1257. */
  1258. if (lo == 0 && hi == 0)
  1259. return sendmsg(cinfo, &cmsg, 1);
  1260. else
  1261. return sendmsg(cinfo, &cmsg, 0);
  1262. }
  1263. static int resync_finish(struct mddev *mddev)
  1264. {
  1265. struct md_cluster_info *cinfo = mddev->cluster_info;
  1266. int ret = 0;
  1267. clear_bit(MD_RESYNCING_REMOTE, &mddev->recovery);
  1268. /*
  1269. * If resync thread is interrupted so we can't say resync is finished,
  1270. * another node will launch resync thread to continue.
  1271. */
  1272. if (!test_bit(MD_CLOSING, &mddev->flags))
  1273. ret = resync_info_update(mddev, 0, 0);
  1274. dlm_unlock_sync(cinfo->resync_lockres);
  1275. return ret;
  1276. }
  1277. static int area_resyncing(struct mddev *mddev, int direction,
  1278. sector_t lo, sector_t hi)
  1279. {
  1280. struct md_cluster_info *cinfo = mddev->cluster_info;
  1281. int ret = 0;
  1282. if ((direction == READ) &&
  1283. test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
  1284. return 1;
  1285. spin_lock_irq(&cinfo->suspend_lock);
  1286. if (hi > cinfo->suspend_lo && lo < cinfo->suspend_hi)
  1287. ret = 1;
  1288. spin_unlock_irq(&cinfo->suspend_lock);
  1289. return ret;
  1290. }
  1291. /* add_new_disk() - initiates a disk add
  1292. * However, if this fails before writing md_update_sb(),
  1293. * add_new_disk_cancel() must be called to release token lock
  1294. */
  1295. static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev)
  1296. {
  1297. struct md_cluster_info *cinfo = mddev->cluster_info;
  1298. struct cluster_msg cmsg;
  1299. int ret = 0;
  1300. struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
  1301. char *uuid = sb->device_uuid;
  1302. memset(&cmsg, 0, sizeof(cmsg));
  1303. cmsg.type = cpu_to_le32(NEWDISK);
  1304. memcpy(cmsg.uuid, uuid, 16);
  1305. cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
  1306. if (lock_comm(cinfo, 1))
  1307. return -EAGAIN;
  1308. ret = __sendmsg(cinfo, &cmsg);
  1309. if (ret) {
  1310. unlock_comm(cinfo);
  1311. return ret;
  1312. }
  1313. cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
  1314. ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
  1315. cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
  1316. /* Some node does not "see" the device */
  1317. if (ret == -EAGAIN)
  1318. ret = -ENOENT;
  1319. if (ret)
  1320. unlock_comm(cinfo);
  1321. else {
  1322. dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
  1323. /* Since MD_CHANGE_DEVS will be set in add_bound_rdev which
  1324. * will run soon after add_new_disk, the below path will be
  1325. * invoked:
  1326. * md_wakeup_thread(mddev->thread)
  1327. * -> conf->thread (raid1d)
  1328. * -> md_check_recovery -> md_update_sb
  1329. * -> metadata_update_start/finish
  1330. * MD_CLUSTER_SEND_LOCKED_ALREADY will be cleared eventually.
  1331. *
  1332. * For other failure cases, metadata_update_cancel and
  1333. * add_new_disk_cancel also clear below bit as well.
  1334. * */
  1335. set_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  1336. wake_up(&cinfo->wait);
  1337. }
  1338. return ret;
  1339. }
  1340. static void add_new_disk_cancel(struct mddev *mddev)
  1341. {
  1342. struct md_cluster_info *cinfo = mddev->cluster_info;
  1343. clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  1344. unlock_comm(cinfo);
  1345. }
  1346. static int new_disk_ack(struct mddev *mddev, bool ack)
  1347. {
  1348. struct md_cluster_info *cinfo = mddev->cluster_info;
  1349. if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
  1350. pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
  1351. return -EINVAL;
  1352. }
  1353. if (ack)
  1354. dlm_unlock_sync(cinfo->no_new_dev_lockres);
  1355. complete(&cinfo->newdisk_completion);
  1356. return 0;
  1357. }
  1358. static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1359. {
  1360. struct cluster_msg cmsg = {0};
  1361. struct md_cluster_info *cinfo = mddev->cluster_info;
  1362. cmsg.type = cpu_to_le32(REMOVE);
  1363. cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
  1364. return sendmsg(cinfo, &cmsg, 1);
  1365. }
  1366. static int lock_all_bitmaps(struct mddev *mddev)
  1367. {
  1368. int slot, my_slot, ret, held = 1, i = 0;
  1369. char str[64];
  1370. struct md_cluster_info *cinfo = mddev->cluster_info;
  1371. cinfo->other_bitmap_lockres =
  1372. kzalloc_objs(struct dlm_lock_resource *,
  1373. mddev->bitmap_info.nodes - 1);
  1374. if (!cinfo->other_bitmap_lockres) {
  1375. pr_err("md: can't alloc mem for other bitmap locks\n");
  1376. return 0;
  1377. }
  1378. my_slot = slot_number(mddev);
  1379. for (slot = 0; slot < mddev->bitmap_info.nodes; slot++) {
  1380. if (slot == my_slot)
  1381. continue;
  1382. memset(str, '\0', 64);
  1383. snprintf(str, 64, "bitmap%04d", slot);
  1384. cinfo->other_bitmap_lockres[i] = lockres_init(mddev, str, NULL, 1);
  1385. if (!cinfo->other_bitmap_lockres[i])
  1386. return -ENOMEM;
  1387. cinfo->other_bitmap_lockres[i]->flags |= DLM_LKF_NOQUEUE;
  1388. ret = dlm_lock_sync(cinfo->other_bitmap_lockres[i], DLM_LOCK_PW);
  1389. if (ret)
  1390. held = -1;
  1391. i++;
  1392. }
  1393. return held;
  1394. }
  1395. static void unlock_all_bitmaps(struct mddev *mddev)
  1396. {
  1397. struct md_cluster_info *cinfo = mddev->cluster_info;
  1398. int i;
  1399. /* release other node's bitmap lock if they are existed */
  1400. if (cinfo->other_bitmap_lockres) {
  1401. for (i = 0; i < mddev->bitmap_info.nodes - 1; i++) {
  1402. if (cinfo->other_bitmap_lockres[i]) {
  1403. lockres_free(cinfo->other_bitmap_lockres[i]);
  1404. }
  1405. }
  1406. kfree(cinfo->other_bitmap_lockres);
  1407. cinfo->other_bitmap_lockres = NULL;
  1408. }
  1409. }
  1410. static int gather_bitmaps(struct md_rdev *rdev)
  1411. {
  1412. int sn, err;
  1413. sector_t lo, hi;
  1414. struct cluster_msg cmsg = {0};
  1415. struct mddev *mddev = rdev->mddev;
  1416. struct md_cluster_info *cinfo = mddev->cluster_info;
  1417. cmsg.type = cpu_to_le32(RE_ADD);
  1418. cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
  1419. err = sendmsg(cinfo, &cmsg, 1);
  1420. if (err)
  1421. goto out;
  1422. for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
  1423. if (sn == (cinfo->slot_number - 1))
  1424. continue;
  1425. err = mddev->bitmap_ops->copy_from_slot(mddev, sn, &lo, &hi, false);
  1426. if (err) {
  1427. pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
  1428. goto out;
  1429. }
  1430. if ((hi > 0) && (lo < mddev->resync_offset))
  1431. mddev->resync_offset = lo;
  1432. }
  1433. out:
  1434. return err;
  1435. }
  1436. static struct md_cluster_operations cluster_ops = {
  1437. .head = {
  1438. .type = MD_CLUSTER,
  1439. .id = ID_CLUSTER,
  1440. .name = "cluster",
  1441. .owner = THIS_MODULE,
  1442. },
  1443. .join = join,
  1444. .leave = leave,
  1445. .slot_number = slot_number,
  1446. .resync_start = resync_start,
  1447. .resync_finish = resync_finish,
  1448. .resync_info_update = resync_info_update,
  1449. .resync_start_notify = resync_start_notify,
  1450. .resync_status_get = resync_status_get,
  1451. .resync_info_get = resync_info_get,
  1452. .metadata_update_start = metadata_update_start,
  1453. .metadata_update_finish = metadata_update_finish,
  1454. .metadata_update_cancel = metadata_update_cancel,
  1455. .area_resyncing = area_resyncing,
  1456. .add_new_disk = add_new_disk,
  1457. .add_new_disk_cancel = add_new_disk_cancel,
  1458. .new_disk_ack = new_disk_ack,
  1459. .remove_disk = remove_disk,
  1460. .load_bitmaps = load_bitmaps,
  1461. .gather_bitmaps = gather_bitmaps,
  1462. .resize_bitmaps = resize_bitmaps,
  1463. .lock_all_bitmaps = lock_all_bitmaps,
  1464. .unlock_all_bitmaps = unlock_all_bitmaps,
  1465. .update_size = update_size,
  1466. };
  1467. static int __init cluster_init(void)
  1468. {
  1469. pr_warn("md-cluster: support raid1 and raid10 (limited support)\n");
  1470. pr_info("Registering Cluster MD functions\n");
  1471. return register_md_submodule(&cluster_ops.head);
  1472. }
  1473. static void cluster_exit(void)
  1474. {
  1475. unregister_md_submodule(&cluster_ops.head);
  1476. }
  1477. module_init(cluster_init);
  1478. module_exit(cluster_exit);
  1479. MODULE_AUTHOR("SUSE");
  1480. MODULE_LICENSE("GPL");
  1481. MODULE_DESCRIPTION("Clustering support for MD");