dlmdomain.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmdomain.c
  5. *
  6. * defines domain join / leave apis
  7. *
  8. * Copyright (C) 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/utsname.h>
  31. #include <linux/init.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/delay.h>
  34. #include <linux/err.h>
  35. #include "cluster/heartbeat.h"
  36. #include "cluster/nodemanager.h"
  37. #include "cluster/tcp.h"
  38. #include "dlmapi.h"
  39. #include "dlmcommon.h"
  40. #include "dlmdebug.h"
  41. #include "dlmdomain.h"
  42. #include "dlmver.h"
  43. #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
  44. #include "cluster/masklog.h"
  45. /*
  46. *
  47. * spinlock lock ordering: if multiple locks are needed, obey this ordering:
  48. * dlm_domain_lock
  49. * struct dlm_ctxt->spinlock
  50. * struct dlm_lock_resource->spinlock
  51. * struct dlm_ctxt->master_lock
  52. * struct dlm_ctxt->ast_lock
  53. * dlm_master_list_entry->spinlock
  54. * dlm_lock->spinlock
  55. *
  56. */
  57. spinlock_t dlm_domain_lock = SPIN_LOCK_UNLOCKED;
  58. LIST_HEAD(dlm_domains);
  59. static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
  60. #define DLM_DOMAIN_BACKOFF_MS 200
  61. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data);
  62. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data);
  63. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data);
  64. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data);
  65. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
  66. void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
  67. {
  68. list_del_init(&lockres->list);
  69. dlm_lockres_put(lockres);
  70. }
  71. void __dlm_insert_lockres(struct dlm_ctxt *dlm,
  72. struct dlm_lock_resource *res)
  73. {
  74. struct list_head *bucket;
  75. struct qstr *q;
  76. assert_spin_locked(&dlm->spinlock);
  77. q = &res->lockname;
  78. q->hash = full_name_hash(q->name, q->len);
  79. bucket = &(dlm->resources[q->hash & DLM_HASH_MASK]);
  80. /* get a reference for our hashtable */
  81. dlm_lockres_get(res);
  82. list_add_tail(&res->list, bucket);
  83. }
  84. struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
  85. const char *name,
  86. unsigned int len)
  87. {
  88. unsigned int hash;
  89. struct list_head *iter;
  90. struct dlm_lock_resource *tmpres=NULL;
  91. struct list_head *bucket;
  92. mlog_entry("%.*s\n", len, name);
  93. assert_spin_locked(&dlm->spinlock);
  94. hash = full_name_hash(name, len);
  95. bucket = &(dlm->resources[hash & DLM_HASH_MASK]);
  96. /* check for pre-existing lock */
  97. list_for_each(iter, bucket) {
  98. tmpres = list_entry(iter, struct dlm_lock_resource, list);
  99. if (tmpres->lockname.len == len &&
  100. memcmp(tmpres->lockname.name, name, len) == 0) {
  101. dlm_lockres_get(tmpres);
  102. break;
  103. }
  104. tmpres = NULL;
  105. }
  106. return tmpres;
  107. }
  108. struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
  109. const char *name,
  110. unsigned int len)
  111. {
  112. struct dlm_lock_resource *res;
  113. spin_lock(&dlm->spinlock);
  114. res = __dlm_lookup_lockres(dlm, name, len);
  115. spin_unlock(&dlm->spinlock);
  116. return res;
  117. }
  118. static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
  119. {
  120. struct dlm_ctxt *tmp = NULL;
  121. struct list_head *iter;
  122. assert_spin_locked(&dlm_domain_lock);
  123. /* tmp->name here is always NULL terminated,
  124. * but domain may not be! */
  125. list_for_each(iter, &dlm_domains) {
  126. tmp = list_entry (iter, struct dlm_ctxt, list);
  127. if (strlen(tmp->name) == len &&
  128. memcmp(tmp->name, domain, len)==0)
  129. break;
  130. tmp = NULL;
  131. }
  132. return tmp;
  133. }
  134. /* For null terminated domain strings ONLY */
  135. static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
  136. {
  137. assert_spin_locked(&dlm_domain_lock);
  138. return __dlm_lookup_domain_full(domain, strlen(domain));
  139. }
  140. /* returns true on one of two conditions:
  141. * 1) the domain does not exist
  142. * 2) the domain exists and it's state is "joined" */
  143. static int dlm_wait_on_domain_helper(const char *domain)
  144. {
  145. int ret = 0;
  146. struct dlm_ctxt *tmp = NULL;
  147. spin_lock(&dlm_domain_lock);
  148. tmp = __dlm_lookup_domain(domain);
  149. if (!tmp)
  150. ret = 1;
  151. else if (tmp->dlm_state == DLM_CTXT_JOINED)
  152. ret = 1;
  153. spin_unlock(&dlm_domain_lock);
  154. return ret;
  155. }
  156. static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
  157. {
  158. if (dlm->resources)
  159. free_page((unsigned long) dlm->resources);
  160. if (dlm->name)
  161. kfree(dlm->name);
  162. kfree(dlm);
  163. }
  164. /* A little strange - this function will be called while holding
  165. * dlm_domain_lock and is expected to be holding it on the way out. We
  166. * will however drop and reacquire it multiple times */
  167. static void dlm_ctxt_release(struct kref *kref)
  168. {
  169. struct dlm_ctxt *dlm;
  170. dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
  171. BUG_ON(dlm->num_joins);
  172. BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
  173. /* we may still be in the list if we hit an error during join. */
  174. list_del_init(&dlm->list);
  175. spin_unlock(&dlm_domain_lock);
  176. mlog(0, "freeing memory from domain %s\n", dlm->name);
  177. wake_up(&dlm_domain_events);
  178. dlm_free_ctxt_mem(dlm);
  179. spin_lock(&dlm_domain_lock);
  180. }
  181. void dlm_put(struct dlm_ctxt *dlm)
  182. {
  183. spin_lock(&dlm_domain_lock);
  184. kref_put(&dlm->dlm_refs, dlm_ctxt_release);
  185. spin_unlock(&dlm_domain_lock);
  186. }
  187. static void __dlm_get(struct dlm_ctxt *dlm)
  188. {
  189. kref_get(&dlm->dlm_refs);
  190. }
  191. /* given a questionable reference to a dlm object, gets a reference if
  192. * it can find it in the list, otherwise returns NULL in which case
  193. * you shouldn't trust your pointer. */
  194. struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
  195. {
  196. struct list_head *iter;
  197. struct dlm_ctxt *target = NULL;
  198. spin_lock(&dlm_domain_lock);
  199. list_for_each(iter, &dlm_domains) {
  200. target = list_entry (iter, struct dlm_ctxt, list);
  201. if (target == dlm) {
  202. __dlm_get(target);
  203. break;
  204. }
  205. target = NULL;
  206. }
  207. spin_unlock(&dlm_domain_lock);
  208. return target;
  209. }
  210. int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
  211. {
  212. int ret;
  213. spin_lock(&dlm_domain_lock);
  214. ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
  215. (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
  216. spin_unlock(&dlm_domain_lock);
  217. return ret;
  218. }
  219. static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
  220. {
  221. dlm_unregister_domain_handlers(dlm);
  222. dlm_complete_thread(dlm);
  223. dlm_complete_recovery_thread(dlm);
  224. /* We've left the domain. Now we can take ourselves out of the
  225. * list and allow the kref stuff to help us free the
  226. * memory. */
  227. spin_lock(&dlm_domain_lock);
  228. list_del_init(&dlm->list);
  229. spin_unlock(&dlm_domain_lock);
  230. /* Wake up anyone waiting for us to remove this domain */
  231. wake_up(&dlm_domain_events);
  232. }
  233. static void dlm_migrate_all_locks(struct dlm_ctxt *dlm)
  234. {
  235. int i;
  236. struct dlm_lock_resource *res;
  237. mlog(0, "Migrating locks from domain %s\n", dlm->name);
  238. restart:
  239. spin_lock(&dlm->spinlock);
  240. for (i=0; i<DLM_HASH_SIZE; i++) {
  241. while (!list_empty(&dlm->resources[i])) {
  242. res = list_entry(dlm->resources[i].next,
  243. struct dlm_lock_resource, list);
  244. /* need reference when manually grabbing lockres */
  245. dlm_lockres_get(res);
  246. /* this should unhash the lockres
  247. * and exit with dlm->spinlock */
  248. mlog(0, "purging res=%p\n", res);
  249. if (dlm_lockres_is_dirty(dlm, res)) {
  250. /* HACK! this should absolutely go.
  251. * need to figure out why some empty
  252. * lockreses are still marked dirty */
  253. mlog(ML_ERROR, "lockres %.*s dirty!\n",
  254. res->lockname.len, res->lockname.name);
  255. spin_unlock(&dlm->spinlock);
  256. dlm_kick_thread(dlm, res);
  257. wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
  258. dlm_lockres_put(res);
  259. goto restart;
  260. }
  261. dlm_purge_lockres(dlm, res);
  262. dlm_lockres_put(res);
  263. }
  264. }
  265. spin_unlock(&dlm->spinlock);
  266. mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
  267. }
  268. static int dlm_no_joining_node(struct dlm_ctxt *dlm)
  269. {
  270. int ret;
  271. spin_lock(&dlm->spinlock);
  272. ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
  273. spin_unlock(&dlm->spinlock);
  274. return ret;
  275. }
  276. static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
  277. {
  278. /* Yikes, a double spinlock! I need domain_lock for the dlm
  279. * state and the dlm spinlock for join state... Sorry! */
  280. again:
  281. spin_lock(&dlm_domain_lock);
  282. spin_lock(&dlm->spinlock);
  283. if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  284. mlog(0, "Node %d is joining, we wait on it.\n",
  285. dlm->joining_node);
  286. spin_unlock(&dlm->spinlock);
  287. spin_unlock(&dlm_domain_lock);
  288. wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
  289. goto again;
  290. }
  291. dlm->dlm_state = DLM_CTXT_LEAVING;
  292. spin_unlock(&dlm->spinlock);
  293. spin_unlock(&dlm_domain_lock);
  294. }
  295. static void __dlm_print_nodes(struct dlm_ctxt *dlm)
  296. {
  297. int node = -1;
  298. assert_spin_locked(&dlm->spinlock);
  299. mlog(ML_NOTICE, "Nodes in my domain (\"%s\"):\n", dlm->name);
  300. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  301. node + 1)) < O2NM_MAX_NODES) {
  302. mlog(ML_NOTICE, " node %d\n", node);
  303. }
  304. }
  305. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data)
  306. {
  307. struct dlm_ctxt *dlm = data;
  308. unsigned int node;
  309. struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
  310. mlog_entry("%p %u %p", msg, len, data);
  311. if (!dlm_grab(dlm))
  312. return 0;
  313. node = exit_msg->node_idx;
  314. mlog(0, "Node %u leaves domain %s\n", node, dlm->name);
  315. spin_lock(&dlm->spinlock);
  316. clear_bit(node, dlm->domain_map);
  317. __dlm_print_nodes(dlm);
  318. /* notify anything attached to the heartbeat events */
  319. dlm_hb_event_notify_attached(dlm, node, 0);
  320. spin_unlock(&dlm->spinlock);
  321. dlm_put(dlm);
  322. return 0;
  323. }
  324. static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
  325. unsigned int node)
  326. {
  327. int status;
  328. struct dlm_exit_domain leave_msg;
  329. mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
  330. node, dlm->name, dlm->node_num);
  331. memset(&leave_msg, 0, sizeof(leave_msg));
  332. leave_msg.node_idx = dlm->node_num;
  333. status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
  334. &leave_msg, sizeof(leave_msg), node,
  335. NULL);
  336. mlog(0, "status return %d from o2net_send_message\n", status);
  337. return status;
  338. }
  339. static void dlm_leave_domain(struct dlm_ctxt *dlm)
  340. {
  341. int node, clear_node, status;
  342. /* At this point we've migrated away all our locks and won't
  343. * accept mastership of new ones. The dlm is responsible for
  344. * almost nothing now. We make sure not to confuse any joining
  345. * nodes and then commence shutdown procedure. */
  346. spin_lock(&dlm->spinlock);
  347. /* Clear ourselves from the domain map */
  348. clear_bit(dlm->node_num, dlm->domain_map);
  349. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  350. 0)) < O2NM_MAX_NODES) {
  351. /* Drop the dlm spinlock. This is safe wrt the domain_map.
  352. * -nodes cannot be added now as the
  353. * query_join_handlers knows to respond with OK_NO_MAP
  354. * -we catch the right network errors if a node is
  355. * removed from the map while we're sending him the
  356. * exit message. */
  357. spin_unlock(&dlm->spinlock);
  358. clear_node = 1;
  359. status = dlm_send_one_domain_exit(dlm, node);
  360. if (status < 0 &&
  361. status != -ENOPROTOOPT &&
  362. status != -ENOTCONN) {
  363. mlog(ML_NOTICE, "Error %d sending domain exit message "
  364. "to node %d\n", status, node);
  365. /* Not sure what to do here but lets sleep for
  366. * a bit in case this was a transient
  367. * error... */
  368. msleep(DLM_DOMAIN_BACKOFF_MS);
  369. clear_node = 0;
  370. }
  371. spin_lock(&dlm->spinlock);
  372. /* If we're not clearing the node bit then we intend
  373. * to loop back around to try again. */
  374. if (clear_node)
  375. clear_bit(node, dlm->domain_map);
  376. }
  377. spin_unlock(&dlm->spinlock);
  378. }
  379. int dlm_joined(struct dlm_ctxt *dlm)
  380. {
  381. int ret = 0;
  382. spin_lock(&dlm_domain_lock);
  383. if (dlm->dlm_state == DLM_CTXT_JOINED)
  384. ret = 1;
  385. spin_unlock(&dlm_domain_lock);
  386. return ret;
  387. }
  388. int dlm_shutting_down(struct dlm_ctxt *dlm)
  389. {
  390. int ret = 0;
  391. spin_lock(&dlm_domain_lock);
  392. if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
  393. ret = 1;
  394. spin_unlock(&dlm_domain_lock);
  395. return ret;
  396. }
  397. void dlm_unregister_domain(struct dlm_ctxt *dlm)
  398. {
  399. int leave = 0;
  400. spin_lock(&dlm_domain_lock);
  401. BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
  402. BUG_ON(!dlm->num_joins);
  403. dlm->num_joins--;
  404. if (!dlm->num_joins) {
  405. /* We mark it "in shutdown" now so new register
  406. * requests wait until we've completely left the
  407. * domain. Don't use DLM_CTXT_LEAVING yet as we still
  408. * want new domain joins to communicate with us at
  409. * least until we've completed migration of our
  410. * resources. */
  411. dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
  412. leave = 1;
  413. }
  414. spin_unlock(&dlm_domain_lock);
  415. if (leave) {
  416. mlog(0, "shutting down domain %s\n", dlm->name);
  417. /* We changed dlm state, notify the thread */
  418. dlm_kick_thread(dlm, NULL);
  419. dlm_migrate_all_locks(dlm);
  420. dlm_mark_domain_leaving(dlm);
  421. dlm_leave_domain(dlm);
  422. dlm_complete_dlm_shutdown(dlm);
  423. }
  424. dlm_put(dlm);
  425. }
  426. EXPORT_SYMBOL_GPL(dlm_unregister_domain);
  427. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data)
  428. {
  429. struct dlm_query_join_request *query;
  430. enum dlm_query_join_response response;
  431. struct dlm_ctxt *dlm = NULL;
  432. query = (struct dlm_query_join_request *) msg->buf;
  433. mlog(0, "node %u wants to join domain %s\n", query->node_idx,
  434. query->domain);
  435. /*
  436. * If heartbeat doesn't consider the node live, tell it
  437. * to back off and try again. This gives heartbeat a chance
  438. * to catch up.
  439. */
  440. if (!o2hb_check_node_heartbeating(query->node_idx)) {
  441. mlog(0, "node %u is not in our live map yet\n",
  442. query->node_idx);
  443. response = JOIN_DISALLOW;
  444. goto respond;
  445. }
  446. response = JOIN_OK_NO_MAP;
  447. spin_lock(&dlm_domain_lock);
  448. dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
  449. /* Once the dlm ctxt is marked as leaving then we don't want
  450. * to be put in someone's domain map. */
  451. if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
  452. spin_lock(&dlm->spinlock);
  453. if (dlm->dlm_state == DLM_CTXT_NEW &&
  454. dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
  455. /*If this is a brand new context and we
  456. * haven't started our join process yet, then
  457. * the other node won the race. */
  458. response = JOIN_OK_NO_MAP;
  459. } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  460. /* Disallow parallel joins. */
  461. response = JOIN_DISALLOW;
  462. } else {
  463. /* Alright we're fully a part of this domain
  464. * so we keep some state as to who's joining
  465. * and indicate to him that needs to be fixed
  466. * up. */
  467. response = JOIN_OK;
  468. __dlm_set_joining_node(dlm, query->node_idx);
  469. }
  470. spin_unlock(&dlm->spinlock);
  471. }
  472. spin_unlock(&dlm_domain_lock);
  473. respond:
  474. mlog(0, "We respond with %u\n", response);
  475. return response;
  476. }
  477. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data)
  478. {
  479. struct dlm_assert_joined *assert;
  480. struct dlm_ctxt *dlm = NULL;
  481. assert = (struct dlm_assert_joined *) msg->buf;
  482. mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
  483. assert->domain);
  484. spin_lock(&dlm_domain_lock);
  485. dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
  486. /* XXX should we consider no dlm ctxt an error? */
  487. if (dlm) {
  488. spin_lock(&dlm->spinlock);
  489. /* Alright, this node has officially joined our
  490. * domain. Set him in the map and clean up our
  491. * leftover join state. */
  492. BUG_ON(dlm->joining_node != assert->node_idx);
  493. set_bit(assert->node_idx, dlm->domain_map);
  494. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  495. __dlm_print_nodes(dlm);
  496. /* notify anything attached to the heartbeat events */
  497. dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
  498. spin_unlock(&dlm->spinlock);
  499. }
  500. spin_unlock(&dlm_domain_lock);
  501. return 0;
  502. }
  503. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data)
  504. {
  505. struct dlm_cancel_join *cancel;
  506. struct dlm_ctxt *dlm = NULL;
  507. cancel = (struct dlm_cancel_join *) msg->buf;
  508. mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
  509. cancel->domain);
  510. spin_lock(&dlm_domain_lock);
  511. dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
  512. if (dlm) {
  513. spin_lock(&dlm->spinlock);
  514. /* Yikes, this guy wants to cancel his join. No
  515. * problem, we simply cleanup our join state. */
  516. BUG_ON(dlm->joining_node != cancel->node_idx);
  517. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  518. spin_unlock(&dlm->spinlock);
  519. }
  520. spin_unlock(&dlm_domain_lock);
  521. return 0;
  522. }
  523. static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
  524. unsigned int node)
  525. {
  526. int status;
  527. struct dlm_cancel_join cancel_msg;
  528. memset(&cancel_msg, 0, sizeof(cancel_msg));
  529. cancel_msg.node_idx = dlm->node_num;
  530. cancel_msg.name_len = strlen(dlm->name);
  531. memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
  532. status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  533. &cancel_msg, sizeof(cancel_msg), node,
  534. NULL);
  535. if (status < 0) {
  536. mlog_errno(status);
  537. goto bail;
  538. }
  539. bail:
  540. return status;
  541. }
  542. /* map_size should be in bytes. */
  543. static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
  544. unsigned long *node_map,
  545. unsigned int map_size)
  546. {
  547. int status, tmpstat;
  548. unsigned int node;
  549. if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
  550. sizeof(unsigned long))) {
  551. mlog(ML_ERROR,
  552. "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
  553. map_size, BITS_TO_LONGS(O2NM_MAX_NODES));
  554. return -EINVAL;
  555. }
  556. status = 0;
  557. node = -1;
  558. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  559. node + 1)) < O2NM_MAX_NODES) {
  560. if (node == dlm->node_num)
  561. continue;
  562. tmpstat = dlm_send_one_join_cancel(dlm, node);
  563. if (tmpstat) {
  564. mlog(ML_ERROR, "Error return %d cancelling join on "
  565. "node %d\n", tmpstat, node);
  566. if (!status)
  567. status = tmpstat;
  568. }
  569. }
  570. if (status)
  571. mlog_errno(status);
  572. return status;
  573. }
  574. static int dlm_request_join(struct dlm_ctxt *dlm,
  575. int node,
  576. enum dlm_query_join_response *response)
  577. {
  578. int status, retval;
  579. struct dlm_query_join_request join_msg;
  580. mlog(0, "querying node %d\n", node);
  581. memset(&join_msg, 0, sizeof(join_msg));
  582. join_msg.node_idx = dlm->node_num;
  583. join_msg.name_len = strlen(dlm->name);
  584. memcpy(join_msg.domain, dlm->name, join_msg.name_len);
  585. status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
  586. sizeof(join_msg), node, &retval);
  587. if (status < 0 && status != -ENOPROTOOPT) {
  588. mlog_errno(status);
  589. goto bail;
  590. }
  591. /* -ENOPROTOOPT from the net code means the other side isn't
  592. listening for our message type -- that's fine, it means
  593. his dlm isn't up, so we can consider him a 'yes' but not
  594. joined into the domain. */
  595. if (status == -ENOPROTOOPT) {
  596. status = 0;
  597. *response = JOIN_OK_NO_MAP;
  598. } else if (retval == JOIN_DISALLOW ||
  599. retval == JOIN_OK ||
  600. retval == JOIN_OK_NO_MAP) {
  601. *response = retval;
  602. } else {
  603. status = -EINVAL;
  604. mlog(ML_ERROR, "invalid response %d from node %u\n", retval,
  605. node);
  606. }
  607. mlog(0, "status %d, node %d response is %d\n", status, node,
  608. *response);
  609. bail:
  610. return status;
  611. }
  612. static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
  613. unsigned int node)
  614. {
  615. int status;
  616. struct dlm_assert_joined assert_msg;
  617. mlog(0, "Sending join assert to node %u\n", node);
  618. memset(&assert_msg, 0, sizeof(assert_msg));
  619. assert_msg.node_idx = dlm->node_num;
  620. assert_msg.name_len = strlen(dlm->name);
  621. memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
  622. status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  623. &assert_msg, sizeof(assert_msg), node,
  624. NULL);
  625. if (status < 0)
  626. mlog_errno(status);
  627. return status;
  628. }
  629. static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
  630. unsigned long *node_map)
  631. {
  632. int status, node, live;
  633. status = 0;
  634. node = -1;
  635. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  636. node + 1)) < O2NM_MAX_NODES) {
  637. if (node == dlm->node_num)
  638. continue;
  639. do {
  640. /* It is very important that this message be
  641. * received so we spin until either the node
  642. * has died or it gets the message. */
  643. status = dlm_send_one_join_assert(dlm, node);
  644. spin_lock(&dlm->spinlock);
  645. live = test_bit(node, dlm->live_nodes_map);
  646. spin_unlock(&dlm->spinlock);
  647. if (status) {
  648. mlog(ML_ERROR, "Error return %d asserting "
  649. "join on node %d\n", status, node);
  650. /* give us some time between errors... */
  651. if (live)
  652. msleep(DLM_DOMAIN_BACKOFF_MS);
  653. }
  654. } while (status && live);
  655. }
  656. }
  657. struct domain_join_ctxt {
  658. unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  659. unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  660. };
  661. static int dlm_should_restart_join(struct dlm_ctxt *dlm,
  662. struct domain_join_ctxt *ctxt,
  663. enum dlm_query_join_response response)
  664. {
  665. int ret;
  666. if (response == JOIN_DISALLOW) {
  667. mlog(0, "Latest response of disallow -- should restart\n");
  668. return 1;
  669. }
  670. spin_lock(&dlm->spinlock);
  671. /* For now, we restart the process if the node maps have
  672. * changed at all */
  673. ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
  674. sizeof(dlm->live_nodes_map));
  675. spin_unlock(&dlm->spinlock);
  676. if (ret)
  677. mlog(0, "Node maps changed -- should restart\n");
  678. return ret;
  679. }
  680. static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
  681. {
  682. int status = 0, tmpstat, node;
  683. struct domain_join_ctxt *ctxt;
  684. enum dlm_query_join_response response;
  685. mlog_entry("%p", dlm);
  686. ctxt = kcalloc(1, sizeof(*ctxt), GFP_KERNEL);
  687. if (!ctxt) {
  688. status = -ENOMEM;
  689. mlog_errno(status);
  690. goto bail;
  691. }
  692. /* group sem locking should work for us here -- we're already
  693. * registered for heartbeat events so filling this should be
  694. * atomic wrt getting those handlers called. */
  695. o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
  696. spin_lock(&dlm->spinlock);
  697. memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
  698. __dlm_set_joining_node(dlm, dlm->node_num);
  699. spin_unlock(&dlm->spinlock);
  700. node = -1;
  701. while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
  702. node + 1)) < O2NM_MAX_NODES) {
  703. if (node == dlm->node_num)
  704. continue;
  705. status = dlm_request_join(dlm, node, &response);
  706. if (status < 0) {
  707. mlog_errno(status);
  708. goto bail;
  709. }
  710. /* Ok, either we got a response or the node doesn't have a
  711. * dlm up. */
  712. if (response == JOIN_OK)
  713. set_bit(node, ctxt->yes_resp_map);
  714. if (dlm_should_restart_join(dlm, ctxt, response)) {
  715. status = -EAGAIN;
  716. goto bail;
  717. }
  718. }
  719. mlog(0, "Yay, done querying nodes!\n");
  720. /* Yay, everyone agree's we can join the domain. My domain is
  721. * comprised of all nodes who were put in the
  722. * yes_resp_map. Copy that into our domain map and send a join
  723. * assert message to clean up everyone elses state. */
  724. spin_lock(&dlm->spinlock);
  725. memcpy(dlm->domain_map, ctxt->yes_resp_map,
  726. sizeof(ctxt->yes_resp_map));
  727. set_bit(dlm->node_num, dlm->domain_map);
  728. spin_unlock(&dlm->spinlock);
  729. dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
  730. /* Joined state *must* be set before the joining node
  731. * information, otherwise the query_join handler may read no
  732. * current joiner but a state of NEW and tell joining nodes
  733. * we're not in the domain. */
  734. spin_lock(&dlm_domain_lock);
  735. dlm->dlm_state = DLM_CTXT_JOINED;
  736. dlm->num_joins++;
  737. spin_unlock(&dlm_domain_lock);
  738. bail:
  739. spin_lock(&dlm->spinlock);
  740. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  741. if (!status)
  742. __dlm_print_nodes(dlm);
  743. spin_unlock(&dlm->spinlock);
  744. if (ctxt) {
  745. /* Do we need to send a cancel message to any nodes? */
  746. if (status < 0) {
  747. tmpstat = dlm_send_join_cancels(dlm,
  748. ctxt->yes_resp_map,
  749. sizeof(ctxt->yes_resp_map));
  750. if (tmpstat < 0)
  751. mlog_errno(tmpstat);
  752. }
  753. kfree(ctxt);
  754. }
  755. mlog(0, "returning %d\n", status);
  756. return status;
  757. }
  758. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
  759. {
  760. o2hb_unregister_callback(&dlm->dlm_hb_up);
  761. o2hb_unregister_callback(&dlm->dlm_hb_down);
  762. o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
  763. }
  764. static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
  765. {
  766. int status;
  767. mlog(0, "registering handlers.\n");
  768. o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
  769. dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
  770. status = o2hb_register_callback(&dlm->dlm_hb_down);
  771. if (status)
  772. goto bail;
  773. o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
  774. dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
  775. status = o2hb_register_callback(&dlm->dlm_hb_up);
  776. if (status)
  777. goto bail;
  778. status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
  779. sizeof(struct dlm_master_request),
  780. dlm_master_request_handler,
  781. dlm, &dlm->dlm_domain_handlers);
  782. if (status)
  783. goto bail;
  784. status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
  785. sizeof(struct dlm_assert_master),
  786. dlm_assert_master_handler,
  787. dlm, &dlm->dlm_domain_handlers);
  788. if (status)
  789. goto bail;
  790. status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
  791. sizeof(struct dlm_create_lock),
  792. dlm_create_lock_handler,
  793. dlm, &dlm->dlm_domain_handlers);
  794. if (status)
  795. goto bail;
  796. status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
  797. DLM_CONVERT_LOCK_MAX_LEN,
  798. dlm_convert_lock_handler,
  799. dlm, &dlm->dlm_domain_handlers);
  800. if (status)
  801. goto bail;
  802. status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
  803. DLM_UNLOCK_LOCK_MAX_LEN,
  804. dlm_unlock_lock_handler,
  805. dlm, &dlm->dlm_domain_handlers);
  806. if (status)
  807. goto bail;
  808. status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
  809. DLM_PROXY_AST_MAX_LEN,
  810. dlm_proxy_ast_handler,
  811. dlm, &dlm->dlm_domain_handlers);
  812. if (status)
  813. goto bail;
  814. status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
  815. sizeof(struct dlm_exit_domain),
  816. dlm_exit_domain_handler,
  817. dlm, &dlm->dlm_domain_handlers);
  818. if (status)
  819. goto bail;
  820. status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  821. sizeof(struct dlm_migrate_request),
  822. dlm_migrate_request_handler,
  823. dlm, &dlm->dlm_domain_handlers);
  824. if (status)
  825. goto bail;
  826. status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
  827. DLM_MIG_LOCKRES_MAX_LEN,
  828. dlm_mig_lockres_handler,
  829. dlm, &dlm->dlm_domain_handlers);
  830. if (status)
  831. goto bail;
  832. status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
  833. sizeof(struct dlm_master_requery),
  834. dlm_master_requery_handler,
  835. dlm, &dlm->dlm_domain_handlers);
  836. if (status)
  837. goto bail;
  838. status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
  839. sizeof(struct dlm_lock_request),
  840. dlm_request_all_locks_handler,
  841. dlm, &dlm->dlm_domain_handlers);
  842. if (status)
  843. goto bail;
  844. status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
  845. sizeof(struct dlm_reco_data_done),
  846. dlm_reco_data_done_handler,
  847. dlm, &dlm->dlm_domain_handlers);
  848. if (status)
  849. goto bail;
  850. status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
  851. sizeof(struct dlm_begin_reco),
  852. dlm_begin_reco_handler,
  853. dlm, &dlm->dlm_domain_handlers);
  854. if (status)
  855. goto bail;
  856. status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
  857. sizeof(struct dlm_finalize_reco),
  858. dlm_finalize_reco_handler,
  859. dlm, &dlm->dlm_domain_handlers);
  860. if (status)
  861. goto bail;
  862. bail:
  863. if (status)
  864. dlm_unregister_domain_handlers(dlm);
  865. return status;
  866. }
  867. static int dlm_join_domain(struct dlm_ctxt *dlm)
  868. {
  869. int status;
  870. BUG_ON(!dlm);
  871. mlog(0, "Join domain %s\n", dlm->name);
  872. status = dlm_register_domain_handlers(dlm);
  873. if (status) {
  874. mlog_errno(status);
  875. goto bail;
  876. }
  877. status = dlm_launch_thread(dlm);
  878. if (status < 0) {
  879. mlog_errno(status);
  880. goto bail;
  881. }
  882. status = dlm_launch_recovery_thread(dlm);
  883. if (status < 0) {
  884. mlog_errno(status);
  885. goto bail;
  886. }
  887. do {
  888. unsigned int backoff;
  889. status = dlm_try_to_join_domain(dlm);
  890. /* If we're racing another node to the join, then we
  891. * need to back off temporarily and let them
  892. * complete. */
  893. if (status == -EAGAIN) {
  894. if (signal_pending(current)) {
  895. status = -ERESTARTSYS;
  896. goto bail;
  897. }
  898. /*
  899. * <chip> After you!
  900. * <dale> No, after you!
  901. * <chip> I insist!
  902. * <dale> But you first!
  903. * ...
  904. */
  905. backoff = (unsigned int)(jiffies & 0x3);
  906. backoff *= DLM_DOMAIN_BACKOFF_MS;
  907. mlog(0, "backoff %d\n", backoff);
  908. msleep(backoff);
  909. }
  910. } while (status == -EAGAIN);
  911. if (status < 0) {
  912. mlog_errno(status);
  913. goto bail;
  914. }
  915. status = 0;
  916. bail:
  917. wake_up(&dlm_domain_events);
  918. if (status) {
  919. dlm_unregister_domain_handlers(dlm);
  920. dlm_complete_thread(dlm);
  921. dlm_complete_recovery_thread(dlm);
  922. }
  923. return status;
  924. }
  925. static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
  926. u32 key)
  927. {
  928. int i;
  929. struct dlm_ctxt *dlm = NULL;
  930. dlm = kcalloc(1, sizeof(*dlm), GFP_KERNEL);
  931. if (!dlm) {
  932. mlog_errno(-ENOMEM);
  933. goto leave;
  934. }
  935. dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
  936. if (dlm->name == NULL) {
  937. mlog_errno(-ENOMEM);
  938. kfree(dlm);
  939. dlm = NULL;
  940. goto leave;
  941. }
  942. dlm->resources = (struct list_head *) __get_free_page(GFP_KERNEL);
  943. if (!dlm->resources) {
  944. mlog_errno(-ENOMEM);
  945. kfree(dlm->name);
  946. kfree(dlm);
  947. dlm = NULL;
  948. goto leave;
  949. }
  950. memset(dlm->resources, 0, PAGE_SIZE);
  951. for (i=0; i<DLM_HASH_SIZE; i++)
  952. INIT_LIST_HEAD(&dlm->resources[i]);
  953. strcpy(dlm->name, domain);
  954. dlm->key = key;
  955. dlm->node_num = o2nm_this_node();
  956. spin_lock_init(&dlm->spinlock);
  957. spin_lock_init(&dlm->master_lock);
  958. spin_lock_init(&dlm->ast_lock);
  959. INIT_LIST_HEAD(&dlm->list);
  960. INIT_LIST_HEAD(&dlm->dirty_list);
  961. INIT_LIST_HEAD(&dlm->reco.resources);
  962. INIT_LIST_HEAD(&dlm->reco.received);
  963. INIT_LIST_HEAD(&dlm->reco.node_data);
  964. INIT_LIST_HEAD(&dlm->purge_list);
  965. INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
  966. dlm->reco.state = 0;
  967. INIT_LIST_HEAD(&dlm->pending_asts);
  968. INIT_LIST_HEAD(&dlm->pending_basts);
  969. mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
  970. dlm->recovery_map, &(dlm->recovery_map[0]));
  971. memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
  972. memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
  973. memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
  974. dlm->dlm_thread_task = NULL;
  975. dlm->dlm_reco_thread_task = NULL;
  976. init_waitqueue_head(&dlm->dlm_thread_wq);
  977. init_waitqueue_head(&dlm->dlm_reco_thread_wq);
  978. init_waitqueue_head(&dlm->reco.event);
  979. init_waitqueue_head(&dlm->ast_wq);
  980. init_waitqueue_head(&dlm->migration_wq);
  981. INIT_LIST_HEAD(&dlm->master_list);
  982. INIT_LIST_HEAD(&dlm->mle_hb_events);
  983. dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
  984. init_waitqueue_head(&dlm->dlm_join_events);
  985. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  986. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  987. atomic_set(&dlm->local_resources, 0);
  988. atomic_set(&dlm->remote_resources, 0);
  989. atomic_set(&dlm->unknown_resources, 0);
  990. spin_lock_init(&dlm->work_lock);
  991. INIT_LIST_HEAD(&dlm->work_list);
  992. INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work, dlm);
  993. kref_init(&dlm->dlm_refs);
  994. dlm->dlm_state = DLM_CTXT_NEW;
  995. INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
  996. mlog(0, "context init: refcount %u\n",
  997. atomic_read(&dlm->dlm_refs.refcount));
  998. leave:
  999. return dlm;
  1000. }
  1001. /*
  1002. * dlm_register_domain: one-time setup per "domain"
  1003. */
  1004. struct dlm_ctxt * dlm_register_domain(const char *domain,
  1005. u32 key)
  1006. {
  1007. int ret;
  1008. struct dlm_ctxt *dlm = NULL;
  1009. struct dlm_ctxt *new_ctxt = NULL;
  1010. if (strlen(domain) > O2NM_MAX_NAME_LEN) {
  1011. ret = -ENAMETOOLONG;
  1012. mlog(ML_ERROR, "domain name length too long\n");
  1013. goto leave;
  1014. }
  1015. if (!o2hb_check_local_node_heartbeating()) {
  1016. mlog(ML_ERROR, "the local node has not been configured, or is "
  1017. "not heartbeating\n");
  1018. ret = -EPROTO;
  1019. goto leave;
  1020. }
  1021. mlog(0, "register called for domain \"%s\"\n", domain);
  1022. retry:
  1023. dlm = NULL;
  1024. if (signal_pending(current)) {
  1025. ret = -ERESTARTSYS;
  1026. mlog_errno(ret);
  1027. goto leave;
  1028. }
  1029. spin_lock(&dlm_domain_lock);
  1030. dlm = __dlm_lookup_domain(domain);
  1031. if (dlm) {
  1032. if (dlm->dlm_state != DLM_CTXT_JOINED) {
  1033. spin_unlock(&dlm_domain_lock);
  1034. mlog(0, "This ctxt is not joined yet!\n");
  1035. wait_event_interruptible(dlm_domain_events,
  1036. dlm_wait_on_domain_helper(
  1037. domain));
  1038. goto retry;
  1039. }
  1040. __dlm_get(dlm);
  1041. dlm->num_joins++;
  1042. spin_unlock(&dlm_domain_lock);
  1043. ret = 0;
  1044. goto leave;
  1045. }
  1046. /* doesn't exist */
  1047. if (!new_ctxt) {
  1048. spin_unlock(&dlm_domain_lock);
  1049. new_ctxt = dlm_alloc_ctxt(domain, key);
  1050. if (new_ctxt)
  1051. goto retry;
  1052. ret = -ENOMEM;
  1053. mlog_errno(ret);
  1054. goto leave;
  1055. }
  1056. /* a little variable switch-a-roo here... */
  1057. dlm = new_ctxt;
  1058. new_ctxt = NULL;
  1059. /* add the new domain */
  1060. list_add_tail(&dlm->list, &dlm_domains);
  1061. spin_unlock(&dlm_domain_lock);
  1062. ret = dlm_join_domain(dlm);
  1063. if (ret) {
  1064. mlog_errno(ret);
  1065. dlm_put(dlm);
  1066. goto leave;
  1067. }
  1068. ret = 0;
  1069. leave:
  1070. if (new_ctxt)
  1071. dlm_free_ctxt_mem(new_ctxt);
  1072. if (ret < 0)
  1073. dlm = ERR_PTR(ret);
  1074. return dlm;
  1075. }
  1076. EXPORT_SYMBOL_GPL(dlm_register_domain);
  1077. static LIST_HEAD(dlm_join_handlers);
  1078. static void dlm_unregister_net_handlers(void)
  1079. {
  1080. o2net_unregister_handler_list(&dlm_join_handlers);
  1081. }
  1082. static int dlm_register_net_handlers(void)
  1083. {
  1084. int status = 0;
  1085. status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1086. sizeof(struct dlm_query_join_request),
  1087. dlm_query_join_handler,
  1088. NULL, &dlm_join_handlers);
  1089. if (status)
  1090. goto bail;
  1091. status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1092. sizeof(struct dlm_assert_joined),
  1093. dlm_assert_joined_handler,
  1094. NULL, &dlm_join_handlers);
  1095. if (status)
  1096. goto bail;
  1097. status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1098. sizeof(struct dlm_cancel_join),
  1099. dlm_cancel_join_handler,
  1100. NULL, &dlm_join_handlers);
  1101. bail:
  1102. if (status < 0)
  1103. dlm_unregister_net_handlers();
  1104. return status;
  1105. }
  1106. /* Domain eviction callback handling.
  1107. *
  1108. * The file system requires notification of node death *before* the
  1109. * dlm completes it's recovery work, otherwise it may be able to
  1110. * acquire locks on resources requiring recovery. Since the dlm can
  1111. * evict a node from it's domain *before* heartbeat fires, a similar
  1112. * mechanism is required. */
  1113. /* Eviction is not expected to happen often, so a per-domain lock is
  1114. * not necessary. Eviction callbacks are allowed to sleep for short
  1115. * periods of time. */
  1116. static DECLARE_RWSEM(dlm_callback_sem);
  1117. void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
  1118. int node_num)
  1119. {
  1120. struct list_head *iter;
  1121. struct dlm_eviction_cb *cb;
  1122. down_read(&dlm_callback_sem);
  1123. list_for_each(iter, &dlm->dlm_eviction_callbacks) {
  1124. cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
  1125. cb->ec_func(node_num, cb->ec_data);
  1126. }
  1127. up_read(&dlm_callback_sem);
  1128. }
  1129. void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
  1130. dlm_eviction_func *f,
  1131. void *data)
  1132. {
  1133. INIT_LIST_HEAD(&cb->ec_item);
  1134. cb->ec_func = f;
  1135. cb->ec_data = data;
  1136. }
  1137. EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
  1138. void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
  1139. struct dlm_eviction_cb *cb)
  1140. {
  1141. down_write(&dlm_callback_sem);
  1142. list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
  1143. up_write(&dlm_callback_sem);
  1144. }
  1145. EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
  1146. void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
  1147. {
  1148. down_write(&dlm_callback_sem);
  1149. list_del_init(&cb->ec_item);
  1150. up_write(&dlm_callback_sem);
  1151. }
  1152. EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
  1153. static int __init dlm_init(void)
  1154. {
  1155. int status;
  1156. dlm_print_version();
  1157. status = dlm_init_mle_cache();
  1158. if (status)
  1159. return -1;
  1160. status = dlm_register_net_handlers();
  1161. if (status) {
  1162. dlm_destroy_mle_cache();
  1163. return -1;
  1164. }
  1165. return 0;
  1166. }
  1167. static void __exit dlm_exit (void)
  1168. {
  1169. dlm_unregister_net_handlers();
  1170. dlm_destroy_mle_cache();
  1171. }
  1172. MODULE_AUTHOR("Oracle");
  1173. MODULE_LICENSE("GPL");
  1174. module_init(dlm_init);
  1175. module_exit(dlm_exit);