dlmdomain.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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. hlist_del_init(&lockres->hash_node);
  69. dlm_lockres_put(lockres);
  70. }
  71. void __dlm_insert_lockres(struct dlm_ctxt *dlm,
  72. struct dlm_lock_resource *res)
  73. {
  74. struct hlist_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->lockres_hash[q->hash % DLM_HASH_BUCKETS]);
  80. /* get a reference for our hashtable */
  81. dlm_lockres_get(res);
  82. hlist_add_head(&res->hash_node, 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 hlist_node *iter;
  90. struct dlm_lock_resource *tmpres=NULL;
  91. struct hlist_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->lockres_hash[hash % DLM_HASH_BUCKETS]);
  96. /* check for pre-existing lock */
  97. hlist_for_each(iter, bucket) {
  98. tmpres = hlist_entry(iter, struct dlm_lock_resource, hash_node);
  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->lockres_hash)
  159. free_page((unsigned long) dlm->lockres_hash);
  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_BUCKETS; i++) {
  241. while (!hlist_empty(&dlm->lockres_hash[i])) {
  242. res = hlist_entry(dlm->lockres_hash[i].first,
  243. struct dlm_lock_resource, hash_node);
  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. * Also, explicitly disallow joining at certain troublesome
  452. * times (ie. during recovery). */
  453. if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
  454. int bit = query->node_idx;
  455. spin_lock(&dlm->spinlock);
  456. if (dlm->dlm_state == DLM_CTXT_NEW &&
  457. dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
  458. /*If this is a brand new context and we
  459. * haven't started our join process yet, then
  460. * the other node won the race. */
  461. response = JOIN_OK_NO_MAP;
  462. } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  463. /* Disallow parallel joins. */
  464. response = JOIN_DISALLOW;
  465. } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
  466. mlog(ML_NOTICE, "node %u trying to join, but recovery "
  467. "is ongoing.\n", bit);
  468. response = JOIN_DISALLOW;
  469. } else if (test_bit(bit, dlm->recovery_map)) {
  470. mlog(ML_NOTICE, "node %u trying to join, but it "
  471. "still needs recovery.\n", bit);
  472. response = JOIN_DISALLOW;
  473. } else if (test_bit(bit, dlm->domain_map)) {
  474. mlog(ML_NOTICE, "node %u trying to join, but it "
  475. "is still in the domain! needs recovery?\n",
  476. bit);
  477. response = JOIN_DISALLOW;
  478. } else {
  479. /* Alright we're fully a part of this domain
  480. * so we keep some state as to who's joining
  481. * and indicate to him that needs to be fixed
  482. * up. */
  483. response = JOIN_OK;
  484. __dlm_set_joining_node(dlm, query->node_idx);
  485. }
  486. spin_unlock(&dlm->spinlock);
  487. }
  488. spin_unlock(&dlm_domain_lock);
  489. respond:
  490. mlog(0, "We respond with %u\n", response);
  491. return response;
  492. }
  493. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data)
  494. {
  495. struct dlm_assert_joined *assert;
  496. struct dlm_ctxt *dlm = NULL;
  497. assert = (struct dlm_assert_joined *) msg->buf;
  498. mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
  499. assert->domain);
  500. spin_lock(&dlm_domain_lock);
  501. dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
  502. /* XXX should we consider no dlm ctxt an error? */
  503. if (dlm) {
  504. spin_lock(&dlm->spinlock);
  505. /* Alright, this node has officially joined our
  506. * domain. Set him in the map and clean up our
  507. * leftover join state. */
  508. BUG_ON(dlm->joining_node != assert->node_idx);
  509. set_bit(assert->node_idx, dlm->domain_map);
  510. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  511. __dlm_print_nodes(dlm);
  512. /* notify anything attached to the heartbeat events */
  513. dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
  514. spin_unlock(&dlm->spinlock);
  515. }
  516. spin_unlock(&dlm_domain_lock);
  517. return 0;
  518. }
  519. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data)
  520. {
  521. struct dlm_cancel_join *cancel;
  522. struct dlm_ctxt *dlm = NULL;
  523. cancel = (struct dlm_cancel_join *) msg->buf;
  524. mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
  525. cancel->domain);
  526. spin_lock(&dlm_domain_lock);
  527. dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
  528. if (dlm) {
  529. spin_lock(&dlm->spinlock);
  530. /* Yikes, this guy wants to cancel his join. No
  531. * problem, we simply cleanup our join state. */
  532. BUG_ON(dlm->joining_node != cancel->node_idx);
  533. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  534. spin_unlock(&dlm->spinlock);
  535. }
  536. spin_unlock(&dlm_domain_lock);
  537. return 0;
  538. }
  539. static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
  540. unsigned int node)
  541. {
  542. int status;
  543. struct dlm_cancel_join cancel_msg;
  544. memset(&cancel_msg, 0, sizeof(cancel_msg));
  545. cancel_msg.node_idx = dlm->node_num;
  546. cancel_msg.name_len = strlen(dlm->name);
  547. memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
  548. status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  549. &cancel_msg, sizeof(cancel_msg), node,
  550. NULL);
  551. if (status < 0) {
  552. mlog_errno(status);
  553. goto bail;
  554. }
  555. bail:
  556. return status;
  557. }
  558. /* map_size should be in bytes. */
  559. static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
  560. unsigned long *node_map,
  561. unsigned int map_size)
  562. {
  563. int status, tmpstat;
  564. unsigned int node;
  565. if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
  566. sizeof(unsigned long))) {
  567. mlog(ML_ERROR,
  568. "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
  569. map_size, BITS_TO_LONGS(O2NM_MAX_NODES));
  570. return -EINVAL;
  571. }
  572. status = 0;
  573. node = -1;
  574. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  575. node + 1)) < O2NM_MAX_NODES) {
  576. if (node == dlm->node_num)
  577. continue;
  578. tmpstat = dlm_send_one_join_cancel(dlm, node);
  579. if (tmpstat) {
  580. mlog(ML_ERROR, "Error return %d cancelling join on "
  581. "node %d\n", tmpstat, node);
  582. if (!status)
  583. status = tmpstat;
  584. }
  585. }
  586. if (status)
  587. mlog_errno(status);
  588. return status;
  589. }
  590. static int dlm_request_join(struct dlm_ctxt *dlm,
  591. int node,
  592. enum dlm_query_join_response *response)
  593. {
  594. int status, retval;
  595. struct dlm_query_join_request join_msg;
  596. mlog(0, "querying node %d\n", node);
  597. memset(&join_msg, 0, sizeof(join_msg));
  598. join_msg.node_idx = dlm->node_num;
  599. join_msg.name_len = strlen(dlm->name);
  600. memcpy(join_msg.domain, dlm->name, join_msg.name_len);
  601. status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
  602. sizeof(join_msg), node, &retval);
  603. if (status < 0 && status != -ENOPROTOOPT) {
  604. mlog_errno(status);
  605. goto bail;
  606. }
  607. /* -ENOPROTOOPT from the net code means the other side isn't
  608. listening for our message type -- that's fine, it means
  609. his dlm isn't up, so we can consider him a 'yes' but not
  610. joined into the domain. */
  611. if (status == -ENOPROTOOPT) {
  612. status = 0;
  613. *response = JOIN_OK_NO_MAP;
  614. } else if (retval == JOIN_DISALLOW ||
  615. retval == JOIN_OK ||
  616. retval == JOIN_OK_NO_MAP) {
  617. *response = retval;
  618. } else {
  619. status = -EINVAL;
  620. mlog(ML_ERROR, "invalid response %d from node %u\n", retval,
  621. node);
  622. }
  623. mlog(0, "status %d, node %d response is %d\n", status, node,
  624. *response);
  625. bail:
  626. return status;
  627. }
  628. static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
  629. unsigned int node)
  630. {
  631. int status;
  632. struct dlm_assert_joined assert_msg;
  633. mlog(0, "Sending join assert to node %u\n", node);
  634. memset(&assert_msg, 0, sizeof(assert_msg));
  635. assert_msg.node_idx = dlm->node_num;
  636. assert_msg.name_len = strlen(dlm->name);
  637. memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
  638. status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  639. &assert_msg, sizeof(assert_msg), node,
  640. NULL);
  641. if (status < 0)
  642. mlog_errno(status);
  643. return status;
  644. }
  645. static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
  646. unsigned long *node_map)
  647. {
  648. int status, node, live;
  649. status = 0;
  650. node = -1;
  651. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  652. node + 1)) < O2NM_MAX_NODES) {
  653. if (node == dlm->node_num)
  654. continue;
  655. do {
  656. /* It is very important that this message be
  657. * received so we spin until either the node
  658. * has died or it gets the message. */
  659. status = dlm_send_one_join_assert(dlm, node);
  660. spin_lock(&dlm->spinlock);
  661. live = test_bit(node, dlm->live_nodes_map);
  662. spin_unlock(&dlm->spinlock);
  663. if (status) {
  664. mlog(ML_ERROR, "Error return %d asserting "
  665. "join on node %d\n", status, node);
  666. /* give us some time between errors... */
  667. if (live)
  668. msleep(DLM_DOMAIN_BACKOFF_MS);
  669. }
  670. } while (status && live);
  671. }
  672. }
  673. struct domain_join_ctxt {
  674. unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  675. unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  676. };
  677. static int dlm_should_restart_join(struct dlm_ctxt *dlm,
  678. struct domain_join_ctxt *ctxt,
  679. enum dlm_query_join_response response)
  680. {
  681. int ret;
  682. if (response == JOIN_DISALLOW) {
  683. mlog(0, "Latest response of disallow -- should restart\n");
  684. return 1;
  685. }
  686. spin_lock(&dlm->spinlock);
  687. /* For now, we restart the process if the node maps have
  688. * changed at all */
  689. ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
  690. sizeof(dlm->live_nodes_map));
  691. spin_unlock(&dlm->spinlock);
  692. if (ret)
  693. mlog(0, "Node maps changed -- should restart\n");
  694. return ret;
  695. }
  696. static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
  697. {
  698. int status = 0, tmpstat, node;
  699. struct domain_join_ctxt *ctxt;
  700. enum dlm_query_join_response response;
  701. mlog_entry("%p", dlm);
  702. ctxt = kcalloc(1, sizeof(*ctxt), GFP_KERNEL);
  703. if (!ctxt) {
  704. status = -ENOMEM;
  705. mlog_errno(status);
  706. goto bail;
  707. }
  708. /* group sem locking should work for us here -- we're already
  709. * registered for heartbeat events so filling this should be
  710. * atomic wrt getting those handlers called. */
  711. o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
  712. spin_lock(&dlm->spinlock);
  713. memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
  714. __dlm_set_joining_node(dlm, dlm->node_num);
  715. spin_unlock(&dlm->spinlock);
  716. node = -1;
  717. while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
  718. node + 1)) < O2NM_MAX_NODES) {
  719. if (node == dlm->node_num)
  720. continue;
  721. status = dlm_request_join(dlm, node, &response);
  722. if (status < 0) {
  723. mlog_errno(status);
  724. goto bail;
  725. }
  726. /* Ok, either we got a response or the node doesn't have a
  727. * dlm up. */
  728. if (response == JOIN_OK)
  729. set_bit(node, ctxt->yes_resp_map);
  730. if (dlm_should_restart_join(dlm, ctxt, response)) {
  731. status = -EAGAIN;
  732. goto bail;
  733. }
  734. }
  735. mlog(0, "Yay, done querying nodes!\n");
  736. /* Yay, everyone agree's we can join the domain. My domain is
  737. * comprised of all nodes who were put in the
  738. * yes_resp_map. Copy that into our domain map and send a join
  739. * assert message to clean up everyone elses state. */
  740. spin_lock(&dlm->spinlock);
  741. memcpy(dlm->domain_map, ctxt->yes_resp_map,
  742. sizeof(ctxt->yes_resp_map));
  743. set_bit(dlm->node_num, dlm->domain_map);
  744. spin_unlock(&dlm->spinlock);
  745. dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
  746. /* Joined state *must* be set before the joining node
  747. * information, otherwise the query_join handler may read no
  748. * current joiner but a state of NEW and tell joining nodes
  749. * we're not in the domain. */
  750. spin_lock(&dlm_domain_lock);
  751. dlm->dlm_state = DLM_CTXT_JOINED;
  752. dlm->num_joins++;
  753. spin_unlock(&dlm_domain_lock);
  754. bail:
  755. spin_lock(&dlm->spinlock);
  756. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  757. if (!status)
  758. __dlm_print_nodes(dlm);
  759. spin_unlock(&dlm->spinlock);
  760. if (ctxt) {
  761. /* Do we need to send a cancel message to any nodes? */
  762. if (status < 0) {
  763. tmpstat = dlm_send_join_cancels(dlm,
  764. ctxt->yes_resp_map,
  765. sizeof(ctxt->yes_resp_map));
  766. if (tmpstat < 0)
  767. mlog_errno(tmpstat);
  768. }
  769. kfree(ctxt);
  770. }
  771. mlog(0, "returning %d\n", status);
  772. return status;
  773. }
  774. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
  775. {
  776. o2hb_unregister_callback(&dlm->dlm_hb_up);
  777. o2hb_unregister_callback(&dlm->dlm_hb_down);
  778. o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
  779. }
  780. static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
  781. {
  782. int status;
  783. mlog(0, "registering handlers.\n");
  784. o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
  785. dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
  786. status = o2hb_register_callback(&dlm->dlm_hb_down);
  787. if (status)
  788. goto bail;
  789. o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
  790. dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
  791. status = o2hb_register_callback(&dlm->dlm_hb_up);
  792. if (status)
  793. goto bail;
  794. status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
  795. sizeof(struct dlm_master_request),
  796. dlm_master_request_handler,
  797. dlm, &dlm->dlm_domain_handlers);
  798. if (status)
  799. goto bail;
  800. status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
  801. sizeof(struct dlm_assert_master),
  802. dlm_assert_master_handler,
  803. dlm, &dlm->dlm_domain_handlers);
  804. if (status)
  805. goto bail;
  806. status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
  807. sizeof(struct dlm_create_lock),
  808. dlm_create_lock_handler,
  809. dlm, &dlm->dlm_domain_handlers);
  810. if (status)
  811. goto bail;
  812. status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
  813. DLM_CONVERT_LOCK_MAX_LEN,
  814. dlm_convert_lock_handler,
  815. dlm, &dlm->dlm_domain_handlers);
  816. if (status)
  817. goto bail;
  818. status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
  819. DLM_UNLOCK_LOCK_MAX_LEN,
  820. dlm_unlock_lock_handler,
  821. dlm, &dlm->dlm_domain_handlers);
  822. if (status)
  823. goto bail;
  824. status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
  825. DLM_PROXY_AST_MAX_LEN,
  826. dlm_proxy_ast_handler,
  827. dlm, &dlm->dlm_domain_handlers);
  828. if (status)
  829. goto bail;
  830. status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
  831. sizeof(struct dlm_exit_domain),
  832. dlm_exit_domain_handler,
  833. dlm, &dlm->dlm_domain_handlers);
  834. if (status)
  835. goto bail;
  836. status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  837. sizeof(struct dlm_migrate_request),
  838. dlm_migrate_request_handler,
  839. dlm, &dlm->dlm_domain_handlers);
  840. if (status)
  841. goto bail;
  842. status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
  843. DLM_MIG_LOCKRES_MAX_LEN,
  844. dlm_mig_lockres_handler,
  845. dlm, &dlm->dlm_domain_handlers);
  846. if (status)
  847. goto bail;
  848. status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
  849. sizeof(struct dlm_master_requery),
  850. dlm_master_requery_handler,
  851. dlm, &dlm->dlm_domain_handlers);
  852. if (status)
  853. goto bail;
  854. status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
  855. sizeof(struct dlm_lock_request),
  856. dlm_request_all_locks_handler,
  857. dlm, &dlm->dlm_domain_handlers);
  858. if (status)
  859. goto bail;
  860. status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
  861. sizeof(struct dlm_reco_data_done),
  862. dlm_reco_data_done_handler,
  863. dlm, &dlm->dlm_domain_handlers);
  864. if (status)
  865. goto bail;
  866. status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
  867. sizeof(struct dlm_begin_reco),
  868. dlm_begin_reco_handler,
  869. dlm, &dlm->dlm_domain_handlers);
  870. if (status)
  871. goto bail;
  872. status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
  873. sizeof(struct dlm_finalize_reco),
  874. dlm_finalize_reco_handler,
  875. dlm, &dlm->dlm_domain_handlers);
  876. if (status)
  877. goto bail;
  878. bail:
  879. if (status)
  880. dlm_unregister_domain_handlers(dlm);
  881. return status;
  882. }
  883. static int dlm_join_domain(struct dlm_ctxt *dlm)
  884. {
  885. int status;
  886. BUG_ON(!dlm);
  887. mlog(0, "Join domain %s\n", dlm->name);
  888. status = dlm_register_domain_handlers(dlm);
  889. if (status) {
  890. mlog_errno(status);
  891. goto bail;
  892. }
  893. status = dlm_launch_thread(dlm);
  894. if (status < 0) {
  895. mlog_errno(status);
  896. goto bail;
  897. }
  898. status = dlm_launch_recovery_thread(dlm);
  899. if (status < 0) {
  900. mlog_errno(status);
  901. goto bail;
  902. }
  903. do {
  904. unsigned int backoff;
  905. status = dlm_try_to_join_domain(dlm);
  906. /* If we're racing another node to the join, then we
  907. * need to back off temporarily and let them
  908. * complete. */
  909. if (status == -EAGAIN) {
  910. if (signal_pending(current)) {
  911. status = -ERESTARTSYS;
  912. goto bail;
  913. }
  914. /*
  915. * <chip> After you!
  916. * <dale> No, after you!
  917. * <chip> I insist!
  918. * <dale> But you first!
  919. * ...
  920. */
  921. backoff = (unsigned int)(jiffies & 0x3);
  922. backoff *= DLM_DOMAIN_BACKOFF_MS;
  923. mlog(0, "backoff %d\n", backoff);
  924. msleep(backoff);
  925. }
  926. } while (status == -EAGAIN);
  927. if (status < 0) {
  928. mlog_errno(status);
  929. goto bail;
  930. }
  931. status = 0;
  932. bail:
  933. wake_up(&dlm_domain_events);
  934. if (status) {
  935. dlm_unregister_domain_handlers(dlm);
  936. dlm_complete_thread(dlm);
  937. dlm_complete_recovery_thread(dlm);
  938. }
  939. return status;
  940. }
  941. static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
  942. u32 key)
  943. {
  944. int i;
  945. struct dlm_ctxt *dlm = NULL;
  946. dlm = kcalloc(1, sizeof(*dlm), GFP_KERNEL);
  947. if (!dlm) {
  948. mlog_errno(-ENOMEM);
  949. goto leave;
  950. }
  951. dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
  952. if (dlm->name == NULL) {
  953. mlog_errno(-ENOMEM);
  954. kfree(dlm);
  955. dlm = NULL;
  956. goto leave;
  957. }
  958. dlm->lockres_hash = (struct hlist_head *) __get_free_page(GFP_KERNEL);
  959. if (!dlm->lockres_hash) {
  960. mlog_errno(-ENOMEM);
  961. kfree(dlm->name);
  962. kfree(dlm);
  963. dlm = NULL;
  964. goto leave;
  965. }
  966. for (i=0; i<DLM_HASH_BUCKETS; i++)
  967. INIT_HLIST_HEAD(&dlm->lockres_hash[i]);
  968. strcpy(dlm->name, domain);
  969. dlm->key = key;
  970. dlm->node_num = o2nm_this_node();
  971. spin_lock_init(&dlm->spinlock);
  972. spin_lock_init(&dlm->master_lock);
  973. spin_lock_init(&dlm->ast_lock);
  974. INIT_LIST_HEAD(&dlm->list);
  975. INIT_LIST_HEAD(&dlm->dirty_list);
  976. INIT_LIST_HEAD(&dlm->reco.resources);
  977. INIT_LIST_HEAD(&dlm->reco.received);
  978. INIT_LIST_HEAD(&dlm->reco.node_data);
  979. INIT_LIST_HEAD(&dlm->purge_list);
  980. INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
  981. dlm->reco.state = 0;
  982. INIT_LIST_HEAD(&dlm->pending_asts);
  983. INIT_LIST_HEAD(&dlm->pending_basts);
  984. mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
  985. dlm->recovery_map, &(dlm->recovery_map[0]));
  986. memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
  987. memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
  988. memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
  989. dlm->dlm_thread_task = NULL;
  990. dlm->dlm_reco_thread_task = NULL;
  991. init_waitqueue_head(&dlm->dlm_thread_wq);
  992. init_waitqueue_head(&dlm->dlm_reco_thread_wq);
  993. init_waitqueue_head(&dlm->reco.event);
  994. init_waitqueue_head(&dlm->ast_wq);
  995. init_waitqueue_head(&dlm->migration_wq);
  996. INIT_LIST_HEAD(&dlm->master_list);
  997. INIT_LIST_HEAD(&dlm->mle_hb_events);
  998. dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
  999. init_waitqueue_head(&dlm->dlm_join_events);
  1000. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  1001. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  1002. atomic_set(&dlm->local_resources, 0);
  1003. atomic_set(&dlm->remote_resources, 0);
  1004. atomic_set(&dlm->unknown_resources, 0);
  1005. spin_lock_init(&dlm->work_lock);
  1006. INIT_LIST_HEAD(&dlm->work_list);
  1007. INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work, dlm);
  1008. kref_init(&dlm->dlm_refs);
  1009. dlm->dlm_state = DLM_CTXT_NEW;
  1010. INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
  1011. mlog(0, "context init: refcount %u\n",
  1012. atomic_read(&dlm->dlm_refs.refcount));
  1013. leave:
  1014. return dlm;
  1015. }
  1016. /*
  1017. * dlm_register_domain: one-time setup per "domain"
  1018. */
  1019. struct dlm_ctxt * dlm_register_domain(const char *domain,
  1020. u32 key)
  1021. {
  1022. int ret;
  1023. struct dlm_ctxt *dlm = NULL;
  1024. struct dlm_ctxt *new_ctxt = NULL;
  1025. if (strlen(domain) > O2NM_MAX_NAME_LEN) {
  1026. ret = -ENAMETOOLONG;
  1027. mlog(ML_ERROR, "domain name length too long\n");
  1028. goto leave;
  1029. }
  1030. if (!o2hb_check_local_node_heartbeating()) {
  1031. mlog(ML_ERROR, "the local node has not been configured, or is "
  1032. "not heartbeating\n");
  1033. ret = -EPROTO;
  1034. goto leave;
  1035. }
  1036. mlog(0, "register called for domain \"%s\"\n", domain);
  1037. retry:
  1038. dlm = NULL;
  1039. if (signal_pending(current)) {
  1040. ret = -ERESTARTSYS;
  1041. mlog_errno(ret);
  1042. goto leave;
  1043. }
  1044. spin_lock(&dlm_domain_lock);
  1045. dlm = __dlm_lookup_domain(domain);
  1046. if (dlm) {
  1047. if (dlm->dlm_state != DLM_CTXT_JOINED) {
  1048. spin_unlock(&dlm_domain_lock);
  1049. mlog(0, "This ctxt is not joined yet!\n");
  1050. wait_event_interruptible(dlm_domain_events,
  1051. dlm_wait_on_domain_helper(
  1052. domain));
  1053. goto retry;
  1054. }
  1055. __dlm_get(dlm);
  1056. dlm->num_joins++;
  1057. spin_unlock(&dlm_domain_lock);
  1058. ret = 0;
  1059. goto leave;
  1060. }
  1061. /* doesn't exist */
  1062. if (!new_ctxt) {
  1063. spin_unlock(&dlm_domain_lock);
  1064. new_ctxt = dlm_alloc_ctxt(domain, key);
  1065. if (new_ctxt)
  1066. goto retry;
  1067. ret = -ENOMEM;
  1068. mlog_errno(ret);
  1069. goto leave;
  1070. }
  1071. /* a little variable switch-a-roo here... */
  1072. dlm = new_ctxt;
  1073. new_ctxt = NULL;
  1074. /* add the new domain */
  1075. list_add_tail(&dlm->list, &dlm_domains);
  1076. spin_unlock(&dlm_domain_lock);
  1077. ret = dlm_join_domain(dlm);
  1078. if (ret) {
  1079. mlog_errno(ret);
  1080. dlm_put(dlm);
  1081. goto leave;
  1082. }
  1083. ret = 0;
  1084. leave:
  1085. if (new_ctxt)
  1086. dlm_free_ctxt_mem(new_ctxt);
  1087. if (ret < 0)
  1088. dlm = ERR_PTR(ret);
  1089. return dlm;
  1090. }
  1091. EXPORT_SYMBOL_GPL(dlm_register_domain);
  1092. static LIST_HEAD(dlm_join_handlers);
  1093. static void dlm_unregister_net_handlers(void)
  1094. {
  1095. o2net_unregister_handler_list(&dlm_join_handlers);
  1096. }
  1097. static int dlm_register_net_handlers(void)
  1098. {
  1099. int status = 0;
  1100. status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1101. sizeof(struct dlm_query_join_request),
  1102. dlm_query_join_handler,
  1103. NULL, &dlm_join_handlers);
  1104. if (status)
  1105. goto bail;
  1106. status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1107. sizeof(struct dlm_assert_joined),
  1108. dlm_assert_joined_handler,
  1109. NULL, &dlm_join_handlers);
  1110. if (status)
  1111. goto bail;
  1112. status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1113. sizeof(struct dlm_cancel_join),
  1114. dlm_cancel_join_handler,
  1115. NULL, &dlm_join_handlers);
  1116. bail:
  1117. if (status < 0)
  1118. dlm_unregister_net_handlers();
  1119. return status;
  1120. }
  1121. /* Domain eviction callback handling.
  1122. *
  1123. * The file system requires notification of node death *before* the
  1124. * dlm completes it's recovery work, otherwise it may be able to
  1125. * acquire locks on resources requiring recovery. Since the dlm can
  1126. * evict a node from it's domain *before* heartbeat fires, a similar
  1127. * mechanism is required. */
  1128. /* Eviction is not expected to happen often, so a per-domain lock is
  1129. * not necessary. Eviction callbacks are allowed to sleep for short
  1130. * periods of time. */
  1131. static DECLARE_RWSEM(dlm_callback_sem);
  1132. void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
  1133. int node_num)
  1134. {
  1135. struct list_head *iter;
  1136. struct dlm_eviction_cb *cb;
  1137. down_read(&dlm_callback_sem);
  1138. list_for_each(iter, &dlm->dlm_eviction_callbacks) {
  1139. cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
  1140. cb->ec_func(node_num, cb->ec_data);
  1141. }
  1142. up_read(&dlm_callback_sem);
  1143. }
  1144. void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
  1145. dlm_eviction_func *f,
  1146. void *data)
  1147. {
  1148. INIT_LIST_HEAD(&cb->ec_item);
  1149. cb->ec_func = f;
  1150. cb->ec_data = data;
  1151. }
  1152. EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
  1153. void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
  1154. struct dlm_eviction_cb *cb)
  1155. {
  1156. down_write(&dlm_callback_sem);
  1157. list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
  1158. up_write(&dlm_callback_sem);
  1159. }
  1160. EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
  1161. void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
  1162. {
  1163. down_write(&dlm_callback_sem);
  1164. list_del_init(&cb->ec_item);
  1165. up_write(&dlm_callback_sem);
  1166. }
  1167. EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
  1168. static int __init dlm_init(void)
  1169. {
  1170. int status;
  1171. dlm_print_version();
  1172. status = dlm_init_mle_cache();
  1173. if (status)
  1174. return -1;
  1175. status = dlm_register_net_handlers();
  1176. if (status) {
  1177. dlm_destroy_mle_cache();
  1178. return -1;
  1179. }
  1180. return 0;
  1181. }
  1182. static void __exit dlm_exit (void)
  1183. {
  1184. dlm_unregister_net_handlers();
  1185. dlm_destroy_mle_cache();
  1186. }
  1187. MODULE_AUTHOR("Oracle");
  1188. MODULE_LICENSE("GPL");
  1189. module_init(dlm_init);
  1190. module_exit(dlm_exit);