md-cluster.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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(sizeof(struct dlm_lock_resource), GFP_KERNEL);
  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->recovery_cp)
  300. mddev->recovery_cp = lo;
  301. /* wake up thread to continue resync in case resync
  302. * is not finished */
  303. if (mddev->recovery_cp != 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. /* daemaon thread must exist */
  490. thread = rcu_dereference_protected(mddev->thread, true);
  491. wait_event(thread->wqueue,
  492. (got_lock = mddev_trylock(mddev)) ||
  493. test_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state));
  494. md_reload_sb(mddev, mddev->good_device_nr);
  495. if (got_lock)
  496. mddev_unlock(mddev);
  497. }
  498. static void process_remove_disk(struct mddev *mddev, struct cluster_msg *msg)
  499. {
  500. struct md_rdev *rdev;
  501. rcu_read_lock();
  502. rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot));
  503. if (rdev) {
  504. set_bit(ClusterRemove, &rdev->flags);
  505. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  506. md_wakeup_thread(mddev->thread);
  507. }
  508. else
  509. pr_warn("%s: %d Could not find disk(%d) to REMOVE\n",
  510. __func__, __LINE__, le32_to_cpu(msg->raid_slot));
  511. rcu_read_unlock();
  512. }
  513. static void process_readd_disk(struct mddev *mddev, struct cluster_msg *msg)
  514. {
  515. struct md_rdev *rdev;
  516. rcu_read_lock();
  517. rdev = md_find_rdev_nr_rcu(mddev, le32_to_cpu(msg->raid_slot));
  518. if (rdev && test_bit(Faulty, &rdev->flags))
  519. clear_bit(Faulty, &rdev->flags);
  520. else
  521. pr_warn("%s: %d Could not find disk(%d) which is faulty",
  522. __func__, __LINE__, le32_to_cpu(msg->raid_slot));
  523. rcu_read_unlock();
  524. }
  525. static int process_recvd_msg(struct mddev *mddev, struct cluster_msg *msg)
  526. {
  527. int ret = 0;
  528. if (WARN(mddev->cluster_info->slot_number - 1 == le32_to_cpu(msg->slot),
  529. "node %d received its own msg\n", le32_to_cpu(msg->slot)))
  530. return -1;
  531. switch (le32_to_cpu(msg->type)) {
  532. case METADATA_UPDATED:
  533. process_metadata_update(mddev, msg);
  534. break;
  535. case CHANGE_CAPACITY:
  536. set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
  537. break;
  538. case RESYNCING_START:
  539. clear_bit(MD_CLUSTER_WAITING_FOR_SYNC, &mddev->cluster_info->state);
  540. break;
  541. case RESYNCING:
  542. set_bit(MD_RESYNCING_REMOTE, &mddev->recovery);
  543. process_suspend_info(mddev, le32_to_cpu(msg->slot),
  544. le64_to_cpu(msg->low),
  545. le64_to_cpu(msg->high));
  546. break;
  547. case NEWDISK:
  548. if (process_add_new_disk(mddev, msg))
  549. ret = -1;
  550. break;
  551. case REMOVE:
  552. process_remove_disk(mddev, msg);
  553. break;
  554. case RE_ADD:
  555. process_readd_disk(mddev, msg);
  556. break;
  557. case BITMAP_NEEDS_SYNC:
  558. __recover_slot(mddev, le32_to_cpu(msg->slot));
  559. break;
  560. case BITMAP_RESIZE:
  561. if (le64_to_cpu(msg->high) != mddev->pers->size(mddev, 0, 0))
  562. ret = mddev->bitmap_ops->resize(mddev,
  563. le64_to_cpu(msg->high),
  564. 0, false);
  565. break;
  566. default:
  567. ret = -1;
  568. pr_warn("%s:%d Received unknown message from %d\n",
  569. __func__, __LINE__, msg->slot);
  570. }
  571. return ret;
  572. }
  573. /*
  574. * thread for receiving message
  575. */
  576. static void recv_daemon(struct md_thread *thread)
  577. {
  578. struct md_cluster_info *cinfo = thread->mddev->cluster_info;
  579. struct dlm_lock_resource *ack_lockres = cinfo->ack_lockres;
  580. struct dlm_lock_resource *message_lockres = cinfo->message_lockres;
  581. struct cluster_msg msg;
  582. int ret;
  583. mutex_lock(&cinfo->recv_mutex);
  584. /*get CR on Message*/
  585. if (dlm_lock_sync(message_lockres, DLM_LOCK_CR)) {
  586. pr_err("md/raid1:failed to get CR on MESSAGE\n");
  587. mutex_unlock(&cinfo->recv_mutex);
  588. return;
  589. }
  590. /* read lvb and wake up thread to process this message_lockres */
  591. memcpy(&msg, message_lockres->lksb.sb_lvbptr, sizeof(struct cluster_msg));
  592. ret = process_recvd_msg(thread->mddev, &msg);
  593. if (ret)
  594. goto out;
  595. /*release CR on ack_lockres*/
  596. ret = dlm_unlock_sync(ack_lockres);
  597. if (unlikely(ret != 0))
  598. pr_info("unlock ack failed return %d\n", ret);
  599. /*up-convert to PR on message_lockres*/
  600. ret = dlm_lock_sync(message_lockres, DLM_LOCK_PR);
  601. if (unlikely(ret != 0))
  602. pr_info("lock PR on msg failed return %d\n", ret);
  603. /*get CR on ack_lockres again*/
  604. ret = dlm_lock_sync(ack_lockres, DLM_LOCK_CR);
  605. if (unlikely(ret != 0))
  606. pr_info("lock CR on ack failed return %d\n", ret);
  607. out:
  608. /*release CR on message_lockres*/
  609. ret = dlm_unlock_sync(message_lockres);
  610. if (unlikely(ret != 0))
  611. pr_info("unlock msg failed return %d\n", ret);
  612. mutex_unlock(&cinfo->recv_mutex);
  613. }
  614. /* lock_token()
  615. * Takes the lock on the TOKEN lock resource so no other
  616. * node can communicate while the operation is underway.
  617. */
  618. static int lock_token(struct md_cluster_info *cinfo)
  619. {
  620. int error;
  621. error = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
  622. if (error) {
  623. pr_err("md-cluster(%s:%d): failed to get EX on TOKEN (%d)\n",
  624. __func__, __LINE__, error);
  625. } else {
  626. /* Lock the receive sequence */
  627. mutex_lock(&cinfo->recv_mutex);
  628. }
  629. return error;
  630. }
  631. /* lock_comm()
  632. * Sets the MD_CLUSTER_SEND_LOCK bit to lock the send channel.
  633. */
  634. static int lock_comm(struct md_cluster_info *cinfo, bool mddev_locked)
  635. {
  636. int rv, set_bit = 0;
  637. struct mddev *mddev = cinfo->mddev;
  638. /*
  639. * If resync thread run after raid1d thread, then process_metadata_update
  640. * could not continue if raid1d held reconfig_mutex (and raid1d is blocked
  641. * since another node already got EX on Token and waiting the EX of Ack),
  642. * so let resync wake up thread in case flag is set.
  643. */
  644. if (mddev_locked && !test_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
  645. &cinfo->state)) {
  646. rv = test_and_set_bit_lock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
  647. &cinfo->state);
  648. WARN_ON_ONCE(rv);
  649. md_wakeup_thread(mddev->thread);
  650. set_bit = 1;
  651. }
  652. wait_event(cinfo->wait,
  653. !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state));
  654. rv = lock_token(cinfo);
  655. if (set_bit)
  656. clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
  657. return rv;
  658. }
  659. static void unlock_comm(struct md_cluster_info *cinfo)
  660. {
  661. WARN_ON(cinfo->token_lockres->mode != DLM_LOCK_EX);
  662. mutex_unlock(&cinfo->recv_mutex);
  663. dlm_unlock_sync(cinfo->token_lockres);
  664. clear_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state);
  665. wake_up(&cinfo->wait);
  666. }
  667. /* __sendmsg()
  668. * This function performs the actual sending of the message. This function is
  669. * usually called after performing the encompassing operation
  670. * The function:
  671. * 1. Grabs the message lockresource in EX mode
  672. * 2. Copies the message to the message LVB
  673. * 3. Downconverts message lockresource to CW
  674. * 4. Upconverts ack lock resource from CR to EX. This forces the BAST on other nodes
  675. * and the other nodes read the message. The thread will wait here until all other
  676. * nodes have released ack lock resource.
  677. * 5. Downconvert ack lockresource to CR
  678. */
  679. static int __sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg)
  680. {
  681. int error, unlock_error;
  682. int slot = cinfo->slot_number - 1;
  683. cmsg->slot = cpu_to_le32(slot);
  684. /*get EX on Message*/
  685. error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_EX);
  686. if (error) {
  687. pr_err("md-cluster: failed to get EX on MESSAGE (%d)\n", error);
  688. return error;
  689. }
  690. memcpy(cinfo->message_lockres->lksb.sb_lvbptr, (void *)cmsg,
  691. sizeof(struct cluster_msg));
  692. /*down-convert EX to CW on Message*/
  693. error = dlm_lock_sync(cinfo->message_lockres, DLM_LOCK_CW);
  694. if (error) {
  695. pr_err("md-cluster: failed to convert EX to CW on MESSAGE(%d)\n",
  696. error);
  697. goto failed_ack;
  698. }
  699. /*up-convert CR to EX on Ack*/
  700. error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_EX);
  701. if (error) {
  702. pr_err("md-cluster: failed to convert CR to EX on ACK(%d)\n",
  703. error);
  704. goto failed_ack;
  705. }
  706. /*down-convert EX to CR on Ack*/
  707. error = dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR);
  708. if (error) {
  709. pr_err("md-cluster: failed to convert EX to CR on ACK(%d)\n",
  710. error);
  711. goto failed_ack;
  712. }
  713. failed_ack:
  714. while ((unlock_error = dlm_unlock_sync(cinfo->message_lockres)))
  715. pr_err("md-cluster: failed convert to NL on MESSAGE(%d)\n",
  716. unlock_error);
  717. return error;
  718. }
  719. static int sendmsg(struct md_cluster_info *cinfo, struct cluster_msg *cmsg,
  720. bool mddev_locked)
  721. {
  722. int ret;
  723. ret = lock_comm(cinfo, mddev_locked);
  724. if (!ret) {
  725. ret = __sendmsg(cinfo, cmsg);
  726. unlock_comm(cinfo);
  727. }
  728. return ret;
  729. }
  730. static int gather_all_resync_info(struct mddev *mddev, int total_slots)
  731. {
  732. struct md_cluster_info *cinfo = mddev->cluster_info;
  733. int i, ret = 0;
  734. struct dlm_lock_resource *bm_lockres;
  735. char str[64];
  736. sector_t lo, hi;
  737. for (i = 0; i < total_slots; i++) {
  738. memset(str, '\0', 64);
  739. snprintf(str, 64, "bitmap%04d", i);
  740. bm_lockres = lockres_init(mddev, str, NULL, 1);
  741. if (!bm_lockres)
  742. return -ENOMEM;
  743. if (i == (cinfo->slot_number - 1)) {
  744. lockres_free(bm_lockres);
  745. continue;
  746. }
  747. bm_lockres->flags |= DLM_LKF_NOQUEUE;
  748. ret = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
  749. if (ret == -EAGAIN) {
  750. if (read_resync_info(mddev, bm_lockres)) {
  751. pr_info("%s:%d Resync[%llu..%llu] in progress on %d\n",
  752. __func__, __LINE__,
  753. (unsigned long long) cinfo->suspend_lo,
  754. (unsigned long long) cinfo->suspend_hi,
  755. i);
  756. cinfo->suspend_from = i;
  757. }
  758. ret = 0;
  759. lockres_free(bm_lockres);
  760. continue;
  761. }
  762. if (ret) {
  763. lockres_free(bm_lockres);
  764. goto out;
  765. }
  766. /* Read the disk bitmap sb and check if it needs recovery */
  767. ret = mddev->bitmap_ops->copy_from_slot(mddev, i, &lo, &hi, false);
  768. if (ret) {
  769. pr_warn("md-cluster: Could not gather bitmaps from slot %d", i);
  770. lockres_free(bm_lockres);
  771. continue;
  772. }
  773. if ((hi > 0) && (lo < mddev->recovery_cp)) {
  774. set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
  775. mddev->recovery_cp = lo;
  776. md_check_recovery(mddev);
  777. }
  778. lockres_free(bm_lockres);
  779. }
  780. out:
  781. return ret;
  782. }
  783. static int join(struct mddev *mddev, int nodes)
  784. {
  785. struct md_cluster_info *cinfo;
  786. int ret, ops_rv;
  787. char str[64];
  788. cinfo = kzalloc(sizeof(struct md_cluster_info), GFP_KERNEL);
  789. if (!cinfo)
  790. return -ENOMEM;
  791. INIT_LIST_HEAD(&cinfo->suspend_list);
  792. spin_lock_init(&cinfo->suspend_lock);
  793. init_completion(&cinfo->completion);
  794. set_bit(MD_CLUSTER_BEGIN_JOIN_CLUSTER, &cinfo->state);
  795. init_waitqueue_head(&cinfo->wait);
  796. mutex_init(&cinfo->recv_mutex);
  797. mddev->cluster_info = cinfo;
  798. cinfo->mddev = mddev;
  799. memset(str, 0, 64);
  800. sprintf(str, "%pU", mddev->uuid);
  801. ret = dlm_new_lockspace(str, mddev->bitmap_info.cluster_name,
  802. DLM_LSFL_SOFTIRQ, LVB_SIZE, &md_ls_ops, mddev,
  803. &ops_rv, &cinfo->lockspace);
  804. if (ret)
  805. goto err;
  806. wait_for_completion(&cinfo->completion);
  807. if (nodes < cinfo->slot_number) {
  808. pr_err("md-cluster: Slot allotted(%d) is greater than available slots(%d).",
  809. cinfo->slot_number, nodes);
  810. ret = -ERANGE;
  811. goto err;
  812. }
  813. /* Initiate the communication resources */
  814. ret = -ENOMEM;
  815. rcu_assign_pointer(cinfo->recv_thread,
  816. md_register_thread(recv_daemon, mddev, "cluster_recv"));
  817. if (!cinfo->recv_thread) {
  818. pr_err("md-cluster: cannot allocate memory for recv_thread!\n");
  819. goto err;
  820. }
  821. cinfo->message_lockres = lockres_init(mddev, "message", NULL, 1);
  822. if (!cinfo->message_lockres)
  823. goto err;
  824. cinfo->token_lockres = lockres_init(mddev, "token", NULL, 0);
  825. if (!cinfo->token_lockres)
  826. goto err;
  827. cinfo->no_new_dev_lockres = lockres_init(mddev, "no-new-dev", NULL, 0);
  828. if (!cinfo->no_new_dev_lockres)
  829. goto err;
  830. ret = dlm_lock_sync(cinfo->token_lockres, DLM_LOCK_EX);
  831. if (ret) {
  832. ret = -EAGAIN;
  833. pr_err("md-cluster: can't join cluster to avoid lock issue\n");
  834. goto err;
  835. }
  836. cinfo->ack_lockres = lockres_init(mddev, "ack", ack_bast, 0);
  837. if (!cinfo->ack_lockres) {
  838. ret = -ENOMEM;
  839. goto err;
  840. }
  841. /* get sync CR lock on ACK. */
  842. if (dlm_lock_sync(cinfo->ack_lockres, DLM_LOCK_CR))
  843. pr_err("md-cluster: failed to get a sync CR lock on ACK!(%d)\n",
  844. ret);
  845. dlm_unlock_sync(cinfo->token_lockres);
  846. /* get sync CR lock on no-new-dev. */
  847. if (dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR))
  848. pr_err("md-cluster: failed to get a sync CR lock on no-new-dev!(%d)\n", ret);
  849. pr_info("md-cluster: Joined cluster %s slot %d\n", str, cinfo->slot_number);
  850. snprintf(str, 64, "bitmap%04d", cinfo->slot_number - 1);
  851. cinfo->bitmap_lockres = lockres_init(mddev, str, NULL, 1);
  852. if (!cinfo->bitmap_lockres) {
  853. ret = -ENOMEM;
  854. goto err;
  855. }
  856. if (dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW)) {
  857. pr_err("Failed to get bitmap lock\n");
  858. ret = -EINVAL;
  859. goto err;
  860. }
  861. cinfo->resync_lockres = lockres_init(mddev, "resync", NULL, 0);
  862. if (!cinfo->resync_lockres) {
  863. ret = -ENOMEM;
  864. goto err;
  865. }
  866. return 0;
  867. err:
  868. set_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
  869. md_unregister_thread(mddev, &cinfo->recovery_thread);
  870. md_unregister_thread(mddev, &cinfo->recv_thread);
  871. lockres_free(cinfo->message_lockres);
  872. lockres_free(cinfo->token_lockres);
  873. lockres_free(cinfo->ack_lockres);
  874. lockres_free(cinfo->no_new_dev_lockres);
  875. lockres_free(cinfo->resync_lockres);
  876. lockres_free(cinfo->bitmap_lockres);
  877. if (cinfo->lockspace)
  878. dlm_release_lockspace(cinfo->lockspace, 2);
  879. mddev->cluster_info = NULL;
  880. kfree(cinfo);
  881. return ret;
  882. }
  883. static void load_bitmaps(struct mddev *mddev, int total_slots)
  884. {
  885. struct md_cluster_info *cinfo = mddev->cluster_info;
  886. /* load all the node's bitmap info for resync */
  887. if (gather_all_resync_info(mddev, total_slots))
  888. pr_err("md-cluster: failed to gather all resyn infos\n");
  889. set_bit(MD_CLUSTER_ALREADY_IN_CLUSTER, &cinfo->state);
  890. /* wake up recv thread in case something need to be handled */
  891. if (test_and_clear_bit(MD_CLUSTER_PENDING_RECV_EVENT, &cinfo->state))
  892. md_wakeup_thread(cinfo->recv_thread);
  893. }
  894. static void resync_bitmap(struct mddev *mddev)
  895. {
  896. struct md_cluster_info *cinfo = mddev->cluster_info;
  897. struct cluster_msg cmsg = {0};
  898. int err;
  899. cmsg.type = cpu_to_le32(BITMAP_NEEDS_SYNC);
  900. err = sendmsg(cinfo, &cmsg, 1);
  901. if (err)
  902. pr_err("%s:%d: failed to send BITMAP_NEEDS_SYNC message (%d)\n",
  903. __func__, __LINE__, err);
  904. }
  905. static void unlock_all_bitmaps(struct mddev *mddev);
  906. static int leave(struct mddev *mddev)
  907. {
  908. struct md_cluster_info *cinfo = mddev->cluster_info;
  909. if (!cinfo)
  910. return 0;
  911. /*
  912. * BITMAP_NEEDS_SYNC message should be sent when node
  913. * is leaving the cluster with dirty bitmap, also we
  914. * can only deliver it when dlm connection is available.
  915. *
  916. * Also, we should send BITMAP_NEEDS_SYNC message in
  917. * case reshaping is interrupted.
  918. */
  919. if ((cinfo->slot_number > 0 && mddev->recovery_cp != MaxSector) ||
  920. (mddev->reshape_position != MaxSector &&
  921. test_bit(MD_CLOSING, &mddev->flags)))
  922. resync_bitmap(mddev);
  923. set_bit(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
  924. md_unregister_thread(mddev, &cinfo->recovery_thread);
  925. md_unregister_thread(mddev, &cinfo->recv_thread);
  926. lockres_free(cinfo->message_lockres);
  927. lockres_free(cinfo->token_lockres);
  928. lockres_free(cinfo->ack_lockres);
  929. lockres_free(cinfo->no_new_dev_lockres);
  930. lockres_free(cinfo->resync_lockres);
  931. lockres_free(cinfo->bitmap_lockres);
  932. unlock_all_bitmaps(mddev);
  933. dlm_release_lockspace(cinfo->lockspace, 2);
  934. kfree(cinfo);
  935. return 0;
  936. }
  937. /* slot_number(): Returns the MD slot number to use
  938. * DLM starts the slot numbers from 1, wheras cluster-md
  939. * wants the number to be from zero, so we deduct one
  940. */
  941. static int slot_number(struct mddev *mddev)
  942. {
  943. struct md_cluster_info *cinfo = mddev->cluster_info;
  944. return cinfo->slot_number - 1;
  945. }
  946. /*
  947. * Check if the communication is already locked, else lock the communication
  948. * channel.
  949. * If it is already locked, token is in EX mode, and hence lock_token()
  950. * should not be called.
  951. */
  952. static int metadata_update_start(struct mddev *mddev)
  953. {
  954. struct md_cluster_info *cinfo = mddev->cluster_info;
  955. int ret;
  956. /*
  957. * metadata_update_start is always called with the protection of
  958. * reconfig_mutex, so set WAITING_FOR_TOKEN here.
  959. */
  960. ret = test_and_set_bit_lock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD,
  961. &cinfo->state);
  962. WARN_ON_ONCE(ret);
  963. md_wakeup_thread(mddev->thread);
  964. wait_event(cinfo->wait,
  965. !test_and_set_bit(MD_CLUSTER_SEND_LOCK, &cinfo->state) ||
  966. test_and_clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state));
  967. /* If token is already locked, return 0 */
  968. if (cinfo->token_lockres->mode == DLM_LOCK_EX) {
  969. clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
  970. return 0;
  971. }
  972. ret = lock_token(cinfo);
  973. clear_bit_unlock(MD_CLUSTER_HOLDING_MUTEX_FOR_RECVD, &cinfo->state);
  974. return ret;
  975. }
  976. static int metadata_update_finish(struct mddev *mddev)
  977. {
  978. struct md_cluster_info *cinfo = mddev->cluster_info;
  979. struct cluster_msg cmsg;
  980. struct md_rdev *rdev;
  981. int ret = 0;
  982. int raid_slot = -1;
  983. memset(&cmsg, 0, sizeof(cmsg));
  984. cmsg.type = cpu_to_le32(METADATA_UPDATED);
  985. /* Pick up a good active device number to send.
  986. */
  987. rdev_for_each(rdev, mddev)
  988. if (rdev->raid_disk > -1 && !test_bit(Faulty, &rdev->flags)) {
  989. raid_slot = rdev->desc_nr;
  990. break;
  991. }
  992. if (raid_slot >= 0) {
  993. cmsg.raid_slot = cpu_to_le32(raid_slot);
  994. ret = __sendmsg(cinfo, &cmsg);
  995. } else
  996. pr_warn("md-cluster: No good device id found to send\n");
  997. clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  998. unlock_comm(cinfo);
  999. return ret;
  1000. }
  1001. static void metadata_update_cancel(struct mddev *mddev)
  1002. {
  1003. struct md_cluster_info *cinfo = mddev->cluster_info;
  1004. clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  1005. unlock_comm(cinfo);
  1006. }
  1007. static int update_bitmap_size(struct mddev *mddev, sector_t size)
  1008. {
  1009. struct md_cluster_info *cinfo = mddev->cluster_info;
  1010. struct cluster_msg cmsg = {0};
  1011. int ret;
  1012. cmsg.type = cpu_to_le32(BITMAP_RESIZE);
  1013. cmsg.high = cpu_to_le64(size);
  1014. ret = sendmsg(cinfo, &cmsg, 0);
  1015. if (ret)
  1016. pr_err("%s:%d: failed to send BITMAP_RESIZE message (%d)\n",
  1017. __func__, __LINE__, ret);
  1018. return ret;
  1019. }
  1020. static int resize_bitmaps(struct mddev *mddev, sector_t newsize, sector_t oldsize)
  1021. {
  1022. void *bitmap = mddev->bitmap;
  1023. struct md_bitmap_stats stats;
  1024. unsigned long my_pages;
  1025. int i, rv;
  1026. rv = mddev->bitmap_ops->get_stats(bitmap, &stats);
  1027. if (rv)
  1028. return rv;
  1029. my_pages = stats.pages;
  1030. /*
  1031. * We need to ensure all the nodes can grow to a larger
  1032. * bitmap size before make the reshaping.
  1033. */
  1034. rv = update_bitmap_size(mddev, newsize);
  1035. if (rv)
  1036. return rv;
  1037. for (i = 0; i < mddev->bitmap_info.nodes; i++) {
  1038. struct dlm_lock_resource *bm_lockres;
  1039. char str[64];
  1040. if (i == md_cluster_ops->slot_number(mddev))
  1041. continue;
  1042. bitmap = mddev->bitmap_ops->get_from_slot(mddev, i);
  1043. if (IS_ERR(bitmap)) {
  1044. pr_err("can't get bitmap from slot %d\n", i);
  1045. bitmap = NULL;
  1046. goto out;
  1047. }
  1048. rv = mddev->bitmap_ops->get_stats(bitmap, &stats);
  1049. if (rv)
  1050. goto out;
  1051. /*
  1052. * If we can hold the bitmap lock of one node then
  1053. * the slot is not occupied, update the pages.
  1054. */
  1055. snprintf(str, 64, "bitmap%04d", i);
  1056. bm_lockres = lockres_init(mddev, str, NULL, 1);
  1057. if (!bm_lockres) {
  1058. pr_err("Cannot initialize %s lock\n", str);
  1059. goto out;
  1060. }
  1061. bm_lockres->flags |= DLM_LKF_NOQUEUE;
  1062. rv = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
  1063. if (!rv)
  1064. mddev->bitmap_ops->set_pages(bitmap, my_pages);
  1065. lockres_free(bm_lockres);
  1066. if (my_pages != stats.pages)
  1067. /*
  1068. * Let's revert the bitmap size if one node
  1069. * can't resize bitmap
  1070. */
  1071. goto out;
  1072. mddev->bitmap_ops->free(bitmap);
  1073. }
  1074. return 0;
  1075. out:
  1076. mddev->bitmap_ops->free(bitmap);
  1077. update_bitmap_size(mddev, oldsize);
  1078. return -1;
  1079. }
  1080. /*
  1081. * return 0 if all the bitmaps have the same sync_size
  1082. */
  1083. static int cluster_check_sync_size(struct mddev *mddev)
  1084. {
  1085. int current_slot = md_cluster_ops->slot_number(mddev);
  1086. int node_num = mddev->bitmap_info.nodes;
  1087. struct dlm_lock_resource *bm_lockres;
  1088. struct md_bitmap_stats stats;
  1089. void *bitmap = mddev->bitmap;
  1090. unsigned long sync_size = 0;
  1091. unsigned long my_sync_size;
  1092. char str[64];
  1093. int i, rv;
  1094. rv = mddev->bitmap_ops->get_stats(bitmap, &stats);
  1095. if (rv)
  1096. return rv;
  1097. my_sync_size = stats.sync_size;
  1098. for (i = 0; i < node_num; i++) {
  1099. if (i == current_slot)
  1100. continue;
  1101. bitmap = mddev->bitmap_ops->get_from_slot(mddev, i);
  1102. if (IS_ERR(bitmap)) {
  1103. pr_err("can't get bitmap from slot %d\n", i);
  1104. return -1;
  1105. }
  1106. /*
  1107. * If we can hold the bitmap lock of one node then
  1108. * the slot is not occupied, update the sb.
  1109. */
  1110. snprintf(str, 64, "bitmap%04d", i);
  1111. bm_lockres = lockres_init(mddev, str, NULL, 1);
  1112. if (!bm_lockres) {
  1113. pr_err("md-cluster: Cannot initialize %s\n", str);
  1114. mddev->bitmap_ops->free(bitmap);
  1115. return -1;
  1116. }
  1117. bm_lockres->flags |= DLM_LKF_NOQUEUE;
  1118. rv = dlm_lock_sync(bm_lockres, DLM_LOCK_PW);
  1119. if (!rv)
  1120. mddev->bitmap_ops->update_sb(bitmap);
  1121. lockres_free(bm_lockres);
  1122. rv = mddev->bitmap_ops->get_stats(bitmap, &stats);
  1123. if (rv) {
  1124. mddev->bitmap_ops->free(bitmap);
  1125. return rv;
  1126. }
  1127. if (sync_size == 0) {
  1128. sync_size = stats.sync_size;
  1129. } else if (sync_size != stats.sync_size) {
  1130. mddev->bitmap_ops->free(bitmap);
  1131. return -1;
  1132. }
  1133. mddev->bitmap_ops->free(bitmap);
  1134. }
  1135. return (my_sync_size == sync_size) ? 0 : -1;
  1136. }
  1137. /*
  1138. * Update the size for cluster raid is a little more complex, we perform it
  1139. * by the steps:
  1140. * 1. hold token lock and update superblock in initiator node.
  1141. * 2. send METADATA_UPDATED msg to other nodes.
  1142. * 3. The initiator node continues to check each bitmap's sync_size, if all
  1143. * bitmaps have the same value of sync_size, then we can set capacity and
  1144. * let other nodes to perform it. If one node can't update sync_size
  1145. * accordingly, we need to revert to previous value.
  1146. */
  1147. static void update_size(struct mddev *mddev, sector_t old_dev_sectors)
  1148. {
  1149. struct md_cluster_info *cinfo = mddev->cluster_info;
  1150. struct cluster_msg cmsg;
  1151. struct md_rdev *rdev;
  1152. int ret = 0;
  1153. int raid_slot = -1;
  1154. md_update_sb(mddev, 1);
  1155. if (lock_comm(cinfo, 1)) {
  1156. pr_err("%s: lock_comm failed\n", __func__);
  1157. return;
  1158. }
  1159. memset(&cmsg, 0, sizeof(cmsg));
  1160. cmsg.type = cpu_to_le32(METADATA_UPDATED);
  1161. rdev_for_each(rdev, mddev)
  1162. if (rdev->raid_disk >= 0 && !test_bit(Faulty, &rdev->flags)) {
  1163. raid_slot = rdev->desc_nr;
  1164. break;
  1165. }
  1166. if (raid_slot >= 0) {
  1167. cmsg.raid_slot = cpu_to_le32(raid_slot);
  1168. /*
  1169. * We can only change capiticy after all the nodes can do it,
  1170. * so need to wait after other nodes already received the msg
  1171. * and handled the change
  1172. */
  1173. ret = __sendmsg(cinfo, &cmsg);
  1174. if (ret) {
  1175. pr_err("%s:%d: failed to send METADATA_UPDATED msg\n",
  1176. __func__, __LINE__);
  1177. unlock_comm(cinfo);
  1178. return;
  1179. }
  1180. } else {
  1181. pr_err("md-cluster: No good device id found to send\n");
  1182. unlock_comm(cinfo);
  1183. return;
  1184. }
  1185. /*
  1186. * check the sync_size from other node's bitmap, if sync_size
  1187. * have already updated in other nodes as expected, send an
  1188. * empty metadata msg to permit the change of capacity
  1189. */
  1190. if (cluster_check_sync_size(mddev) == 0) {
  1191. memset(&cmsg, 0, sizeof(cmsg));
  1192. cmsg.type = cpu_to_le32(CHANGE_CAPACITY);
  1193. ret = __sendmsg(cinfo, &cmsg);
  1194. if (ret)
  1195. pr_err("%s:%d: failed to send CHANGE_CAPACITY msg\n",
  1196. __func__, __LINE__);
  1197. set_capacity_and_notify(mddev->gendisk, mddev->array_sectors);
  1198. } else {
  1199. /* revert to previous sectors */
  1200. ret = mddev->pers->resize(mddev, old_dev_sectors);
  1201. ret = __sendmsg(cinfo, &cmsg);
  1202. if (ret)
  1203. pr_err("%s:%d: failed to send METADATA_UPDATED msg\n",
  1204. __func__, __LINE__);
  1205. }
  1206. unlock_comm(cinfo);
  1207. }
  1208. static int resync_start(struct mddev *mddev)
  1209. {
  1210. struct md_cluster_info *cinfo = mddev->cluster_info;
  1211. return dlm_lock_sync_interruptible(cinfo->resync_lockres, DLM_LOCK_EX, mddev);
  1212. }
  1213. static void resync_info_get(struct mddev *mddev, sector_t *lo, sector_t *hi)
  1214. {
  1215. struct md_cluster_info *cinfo = mddev->cluster_info;
  1216. spin_lock_irq(&cinfo->suspend_lock);
  1217. *lo = cinfo->suspend_lo;
  1218. *hi = cinfo->suspend_hi;
  1219. spin_unlock_irq(&cinfo->suspend_lock);
  1220. }
  1221. static int resync_status_get(struct mddev *mddev)
  1222. {
  1223. struct md_cluster_info *cinfo = mddev->cluster_info;
  1224. return test_bit(MD_CLUSTER_WAITING_FOR_SYNC, &cinfo->state);
  1225. }
  1226. static int resync_start_notify(struct mddev *mddev)
  1227. {
  1228. struct md_cluster_info *cinfo = mddev->cluster_info;
  1229. struct cluster_msg cmsg = {0};
  1230. cmsg.type = cpu_to_le32(RESYNCING_START);
  1231. return sendmsg(cinfo, &cmsg, 0);
  1232. }
  1233. static int resync_info_update(struct mddev *mddev, sector_t lo, sector_t hi)
  1234. {
  1235. struct md_cluster_info *cinfo = mddev->cluster_info;
  1236. struct resync_info ri;
  1237. struct cluster_msg cmsg = {0};
  1238. /* do not send zero again, if we have sent before */
  1239. if (hi == 0) {
  1240. memcpy(&ri, cinfo->bitmap_lockres->lksb.sb_lvbptr, sizeof(struct resync_info));
  1241. if (le64_to_cpu(ri.hi) == 0)
  1242. return 0;
  1243. }
  1244. add_resync_info(cinfo->bitmap_lockres, lo, hi);
  1245. /* Re-acquire the lock to refresh LVB */
  1246. dlm_lock_sync(cinfo->bitmap_lockres, DLM_LOCK_PW);
  1247. cmsg.type = cpu_to_le32(RESYNCING);
  1248. cmsg.low = cpu_to_le64(lo);
  1249. cmsg.high = cpu_to_le64(hi);
  1250. /*
  1251. * mddev_lock is held if resync_info_update is called from
  1252. * resync_finish (md_reap_sync_thread -> resync_finish)
  1253. */
  1254. if (lo == 0 && hi == 0)
  1255. return sendmsg(cinfo, &cmsg, 1);
  1256. else
  1257. return sendmsg(cinfo, &cmsg, 0);
  1258. }
  1259. static int resync_finish(struct mddev *mddev)
  1260. {
  1261. struct md_cluster_info *cinfo = mddev->cluster_info;
  1262. int ret = 0;
  1263. clear_bit(MD_RESYNCING_REMOTE, &mddev->recovery);
  1264. /*
  1265. * If resync thread is interrupted so we can't say resync is finished,
  1266. * another node will launch resync thread to continue.
  1267. */
  1268. if (!test_bit(MD_CLOSING, &mddev->flags))
  1269. ret = resync_info_update(mddev, 0, 0);
  1270. dlm_unlock_sync(cinfo->resync_lockres);
  1271. return ret;
  1272. }
  1273. static int area_resyncing(struct mddev *mddev, int direction,
  1274. sector_t lo, sector_t hi)
  1275. {
  1276. struct md_cluster_info *cinfo = mddev->cluster_info;
  1277. int ret = 0;
  1278. if ((direction == READ) &&
  1279. test_bit(MD_CLUSTER_SUSPEND_READ_BALANCING, &cinfo->state))
  1280. return 1;
  1281. spin_lock_irq(&cinfo->suspend_lock);
  1282. if (hi > cinfo->suspend_lo && lo < cinfo->suspend_hi)
  1283. ret = 1;
  1284. spin_unlock_irq(&cinfo->suspend_lock);
  1285. return ret;
  1286. }
  1287. /* add_new_disk() - initiates a disk add
  1288. * However, if this fails before writing md_update_sb(),
  1289. * add_new_disk_cancel() must be called to release token lock
  1290. */
  1291. static int add_new_disk(struct mddev *mddev, struct md_rdev *rdev)
  1292. {
  1293. struct md_cluster_info *cinfo = mddev->cluster_info;
  1294. struct cluster_msg cmsg;
  1295. int ret = 0;
  1296. struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
  1297. char *uuid = sb->device_uuid;
  1298. memset(&cmsg, 0, sizeof(cmsg));
  1299. cmsg.type = cpu_to_le32(NEWDISK);
  1300. memcpy(cmsg.uuid, uuid, 16);
  1301. cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
  1302. if (lock_comm(cinfo, 1))
  1303. return -EAGAIN;
  1304. ret = __sendmsg(cinfo, &cmsg);
  1305. if (ret) {
  1306. unlock_comm(cinfo);
  1307. return ret;
  1308. }
  1309. cinfo->no_new_dev_lockres->flags |= DLM_LKF_NOQUEUE;
  1310. ret = dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_EX);
  1311. cinfo->no_new_dev_lockres->flags &= ~DLM_LKF_NOQUEUE;
  1312. /* Some node does not "see" the device */
  1313. if (ret == -EAGAIN)
  1314. ret = -ENOENT;
  1315. if (ret)
  1316. unlock_comm(cinfo);
  1317. else {
  1318. dlm_lock_sync(cinfo->no_new_dev_lockres, DLM_LOCK_CR);
  1319. /* Since MD_CHANGE_DEVS will be set in add_bound_rdev which
  1320. * will run soon after add_new_disk, the below path will be
  1321. * invoked:
  1322. * md_wakeup_thread(mddev->thread)
  1323. * -> conf->thread (raid1d)
  1324. * -> md_check_recovery -> md_update_sb
  1325. * -> metadata_update_start/finish
  1326. * MD_CLUSTER_SEND_LOCKED_ALREADY will be cleared eventually.
  1327. *
  1328. * For other failure cases, metadata_update_cancel and
  1329. * add_new_disk_cancel also clear below bit as well.
  1330. * */
  1331. set_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  1332. wake_up(&cinfo->wait);
  1333. }
  1334. return ret;
  1335. }
  1336. static void add_new_disk_cancel(struct mddev *mddev)
  1337. {
  1338. struct md_cluster_info *cinfo = mddev->cluster_info;
  1339. clear_bit(MD_CLUSTER_SEND_LOCKED_ALREADY, &cinfo->state);
  1340. unlock_comm(cinfo);
  1341. }
  1342. static int new_disk_ack(struct mddev *mddev, bool ack)
  1343. {
  1344. struct md_cluster_info *cinfo = mddev->cluster_info;
  1345. if (!test_bit(MD_CLUSTER_WAITING_FOR_NEWDISK, &cinfo->state)) {
  1346. pr_warn("md-cluster(%s): Spurious cluster confirmation\n", mdname(mddev));
  1347. return -EINVAL;
  1348. }
  1349. if (ack)
  1350. dlm_unlock_sync(cinfo->no_new_dev_lockres);
  1351. complete(&cinfo->newdisk_completion);
  1352. return 0;
  1353. }
  1354. static int remove_disk(struct mddev *mddev, struct md_rdev *rdev)
  1355. {
  1356. struct cluster_msg cmsg = {0};
  1357. struct md_cluster_info *cinfo = mddev->cluster_info;
  1358. cmsg.type = cpu_to_le32(REMOVE);
  1359. cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
  1360. return sendmsg(cinfo, &cmsg, 1);
  1361. }
  1362. static int lock_all_bitmaps(struct mddev *mddev)
  1363. {
  1364. int slot, my_slot, ret, held = 1, i = 0;
  1365. char str[64];
  1366. struct md_cluster_info *cinfo = mddev->cluster_info;
  1367. cinfo->other_bitmap_lockres =
  1368. kcalloc(mddev->bitmap_info.nodes - 1,
  1369. sizeof(struct dlm_lock_resource *), GFP_KERNEL);
  1370. if (!cinfo->other_bitmap_lockres) {
  1371. pr_err("md: can't alloc mem for other bitmap locks\n");
  1372. return 0;
  1373. }
  1374. my_slot = slot_number(mddev);
  1375. for (slot = 0; slot < mddev->bitmap_info.nodes; slot++) {
  1376. if (slot == my_slot)
  1377. continue;
  1378. memset(str, '\0', 64);
  1379. snprintf(str, 64, "bitmap%04d", slot);
  1380. cinfo->other_bitmap_lockres[i] = lockres_init(mddev, str, NULL, 1);
  1381. if (!cinfo->other_bitmap_lockres[i])
  1382. return -ENOMEM;
  1383. cinfo->other_bitmap_lockres[i]->flags |= DLM_LKF_NOQUEUE;
  1384. ret = dlm_lock_sync(cinfo->other_bitmap_lockres[i], DLM_LOCK_PW);
  1385. if (ret)
  1386. held = -1;
  1387. i++;
  1388. }
  1389. return held;
  1390. }
  1391. static void unlock_all_bitmaps(struct mddev *mddev)
  1392. {
  1393. struct md_cluster_info *cinfo = mddev->cluster_info;
  1394. int i;
  1395. /* release other node's bitmap lock if they are existed */
  1396. if (cinfo->other_bitmap_lockres) {
  1397. for (i = 0; i < mddev->bitmap_info.nodes - 1; i++) {
  1398. if (cinfo->other_bitmap_lockres[i]) {
  1399. lockres_free(cinfo->other_bitmap_lockres[i]);
  1400. }
  1401. }
  1402. kfree(cinfo->other_bitmap_lockres);
  1403. cinfo->other_bitmap_lockres = NULL;
  1404. }
  1405. }
  1406. static int gather_bitmaps(struct md_rdev *rdev)
  1407. {
  1408. int sn, err;
  1409. sector_t lo, hi;
  1410. struct cluster_msg cmsg = {0};
  1411. struct mddev *mddev = rdev->mddev;
  1412. struct md_cluster_info *cinfo = mddev->cluster_info;
  1413. cmsg.type = cpu_to_le32(RE_ADD);
  1414. cmsg.raid_slot = cpu_to_le32(rdev->desc_nr);
  1415. err = sendmsg(cinfo, &cmsg, 1);
  1416. if (err)
  1417. goto out;
  1418. for (sn = 0; sn < mddev->bitmap_info.nodes; sn++) {
  1419. if (sn == (cinfo->slot_number - 1))
  1420. continue;
  1421. err = mddev->bitmap_ops->copy_from_slot(mddev, sn, &lo, &hi, false);
  1422. if (err) {
  1423. pr_warn("md-cluster: Could not gather bitmaps from slot %d", sn);
  1424. goto out;
  1425. }
  1426. if ((hi > 0) && (lo < mddev->recovery_cp))
  1427. mddev->recovery_cp = lo;
  1428. }
  1429. out:
  1430. return err;
  1431. }
  1432. static const struct md_cluster_operations cluster_ops = {
  1433. .join = join,
  1434. .leave = leave,
  1435. .slot_number = slot_number,
  1436. .resync_start = resync_start,
  1437. .resync_finish = resync_finish,
  1438. .resync_info_update = resync_info_update,
  1439. .resync_start_notify = resync_start_notify,
  1440. .resync_status_get = resync_status_get,
  1441. .resync_info_get = resync_info_get,
  1442. .metadata_update_start = metadata_update_start,
  1443. .metadata_update_finish = metadata_update_finish,
  1444. .metadata_update_cancel = metadata_update_cancel,
  1445. .area_resyncing = area_resyncing,
  1446. .add_new_disk = add_new_disk,
  1447. .add_new_disk_cancel = add_new_disk_cancel,
  1448. .new_disk_ack = new_disk_ack,
  1449. .remove_disk = remove_disk,
  1450. .load_bitmaps = load_bitmaps,
  1451. .gather_bitmaps = gather_bitmaps,
  1452. .resize_bitmaps = resize_bitmaps,
  1453. .lock_all_bitmaps = lock_all_bitmaps,
  1454. .unlock_all_bitmaps = unlock_all_bitmaps,
  1455. .update_size = update_size,
  1456. };
  1457. static int __init cluster_init(void)
  1458. {
  1459. pr_warn("md-cluster: support raid1 and raid10 (limited support)\n");
  1460. pr_info("Registering Cluster MD functions\n");
  1461. register_md_cluster_operations(&cluster_ops, THIS_MODULE);
  1462. return 0;
  1463. }
  1464. static void cluster_exit(void)
  1465. {
  1466. unregister_md_cluster_operations();
  1467. }
  1468. module_init(cluster_init);
  1469. module_exit(cluster_exit);
  1470. MODULE_AUTHOR("SUSE");
  1471. MODULE_LICENSE("GPL");
  1472. MODULE_DESCRIPTION("Clustering support for MD");