dlmdomain.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  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 "dlmdomain.h"
  41. #include "dlmver.h"
  42. #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
  43. #include "cluster/masklog.h"
  44. /*
  45. * ocfs2 node maps are array of long int, which limits to send them freely
  46. * across the wire due to endianness issues. To workaround this, we convert
  47. * long ints to byte arrays. Following 3 routines are helper functions to
  48. * set/test/copy bits within those array of bytes
  49. */
  50. static inline void byte_set_bit(u8 nr, u8 map[])
  51. {
  52. map[nr >> 3] |= (1UL << (nr & 7));
  53. }
  54. static inline int byte_test_bit(u8 nr, u8 map[])
  55. {
  56. return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
  57. }
  58. static inline void byte_copymap(u8 dmap[], unsigned long smap[],
  59. unsigned int sz)
  60. {
  61. unsigned int nn;
  62. if (!sz)
  63. return;
  64. memset(dmap, 0, ((sz + 7) >> 3));
  65. for (nn = 0 ; nn < sz; nn++)
  66. if (test_bit(nn, smap))
  67. byte_set_bit(nn, dmap);
  68. }
  69. static void dlm_free_pagevec(void **vec, int pages)
  70. {
  71. while (pages--)
  72. free_page((unsigned long)vec[pages]);
  73. kfree(vec);
  74. }
  75. static void **dlm_alloc_pagevec(int pages)
  76. {
  77. void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
  78. int i;
  79. if (!vec)
  80. return NULL;
  81. for (i = 0; i < pages; i++)
  82. if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
  83. goto out_free;
  84. mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
  85. pages, (unsigned long)DLM_HASH_PAGES,
  86. (unsigned long)DLM_BUCKETS_PER_PAGE);
  87. return vec;
  88. out_free:
  89. dlm_free_pagevec(vec, i);
  90. return NULL;
  91. }
  92. /*
  93. *
  94. * spinlock lock ordering: if multiple locks are needed, obey this ordering:
  95. * dlm_domain_lock
  96. * struct dlm_ctxt->spinlock
  97. * struct dlm_lock_resource->spinlock
  98. * struct dlm_ctxt->master_lock
  99. * struct dlm_ctxt->ast_lock
  100. * dlm_master_list_entry->spinlock
  101. * dlm_lock->spinlock
  102. *
  103. */
  104. DEFINE_SPINLOCK(dlm_domain_lock);
  105. LIST_HEAD(dlm_domains);
  106. static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
  107. /*
  108. * The supported protocol version for DLM communication. Running domains
  109. * will have a negotiated version with the same major number and a minor
  110. * number equal or smaller. The dlm_ctxt->dlm_locking_proto field should
  111. * be used to determine what a running domain is actually using.
  112. */
  113. static const struct dlm_protocol_version dlm_protocol = {
  114. .pv_major = 1,
  115. .pv_minor = 0,
  116. };
  117. #define DLM_DOMAIN_BACKOFF_MS 200
  118. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  119. void **ret_data);
  120. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  121. void **ret_data);
  122. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  123. void **ret_data);
  124. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
  125. void **ret_data);
  126. static int dlm_protocol_compare(struct dlm_protocol_version *existing,
  127. struct dlm_protocol_version *request);
  128. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
  129. void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
  130. {
  131. if (!hlist_unhashed(&lockres->hash_node)) {
  132. hlist_del_init(&lockres->hash_node);
  133. dlm_lockres_put(lockres);
  134. }
  135. }
  136. void __dlm_insert_lockres(struct dlm_ctxt *dlm,
  137. struct dlm_lock_resource *res)
  138. {
  139. struct hlist_head *bucket;
  140. struct qstr *q;
  141. assert_spin_locked(&dlm->spinlock);
  142. q = &res->lockname;
  143. bucket = dlm_lockres_hash(dlm, q->hash);
  144. /* get a reference for our hashtable */
  145. dlm_lockres_get(res);
  146. hlist_add_head(&res->hash_node, bucket);
  147. }
  148. struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
  149. const char *name,
  150. unsigned int len,
  151. unsigned int hash)
  152. {
  153. struct hlist_head *bucket;
  154. struct hlist_node *list;
  155. mlog_entry("%.*s\n", len, name);
  156. assert_spin_locked(&dlm->spinlock);
  157. bucket = dlm_lockres_hash(dlm, hash);
  158. hlist_for_each(list, bucket) {
  159. struct dlm_lock_resource *res = hlist_entry(list,
  160. struct dlm_lock_resource, hash_node);
  161. if (res->lockname.name[0] != name[0])
  162. continue;
  163. if (unlikely(res->lockname.len != len))
  164. continue;
  165. if (memcmp(res->lockname.name + 1, name + 1, len - 1))
  166. continue;
  167. dlm_lockres_get(res);
  168. return res;
  169. }
  170. return NULL;
  171. }
  172. /* intended to be called by functions which do not care about lock
  173. * resources which are being purged (most net _handler functions).
  174. * this will return NULL for any lock resource which is found but
  175. * currently in the process of dropping its mastery reference.
  176. * use __dlm_lookup_lockres_full when you need the lock resource
  177. * regardless (e.g. dlm_get_lock_resource) */
  178. struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
  179. const char *name,
  180. unsigned int len,
  181. unsigned int hash)
  182. {
  183. struct dlm_lock_resource *res = NULL;
  184. mlog_entry("%.*s\n", len, name);
  185. assert_spin_locked(&dlm->spinlock);
  186. res = __dlm_lookup_lockres_full(dlm, name, len, hash);
  187. if (res) {
  188. spin_lock(&res->spinlock);
  189. if (res->state & DLM_LOCK_RES_DROPPING_REF) {
  190. spin_unlock(&res->spinlock);
  191. dlm_lockres_put(res);
  192. return NULL;
  193. }
  194. spin_unlock(&res->spinlock);
  195. }
  196. return res;
  197. }
  198. struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
  199. const char *name,
  200. unsigned int len)
  201. {
  202. struct dlm_lock_resource *res;
  203. unsigned int hash = dlm_lockid_hash(name, len);
  204. spin_lock(&dlm->spinlock);
  205. res = __dlm_lookup_lockres(dlm, name, len, hash);
  206. spin_unlock(&dlm->spinlock);
  207. return res;
  208. }
  209. static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
  210. {
  211. struct dlm_ctxt *tmp = NULL;
  212. struct list_head *iter;
  213. assert_spin_locked(&dlm_domain_lock);
  214. /* tmp->name here is always NULL terminated,
  215. * but domain may not be! */
  216. list_for_each(iter, &dlm_domains) {
  217. tmp = list_entry (iter, struct dlm_ctxt, list);
  218. if (strlen(tmp->name) == len &&
  219. memcmp(tmp->name, domain, len)==0)
  220. break;
  221. tmp = NULL;
  222. }
  223. return tmp;
  224. }
  225. /* For null terminated domain strings ONLY */
  226. static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
  227. {
  228. assert_spin_locked(&dlm_domain_lock);
  229. return __dlm_lookup_domain_full(domain, strlen(domain));
  230. }
  231. /* returns true on one of two conditions:
  232. * 1) the domain does not exist
  233. * 2) the domain exists and it's state is "joined" */
  234. static int dlm_wait_on_domain_helper(const char *domain)
  235. {
  236. int ret = 0;
  237. struct dlm_ctxt *tmp = NULL;
  238. spin_lock(&dlm_domain_lock);
  239. tmp = __dlm_lookup_domain(domain);
  240. if (!tmp)
  241. ret = 1;
  242. else if (tmp->dlm_state == DLM_CTXT_JOINED)
  243. ret = 1;
  244. spin_unlock(&dlm_domain_lock);
  245. return ret;
  246. }
  247. static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
  248. {
  249. if (dlm->lockres_hash)
  250. dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
  251. if (dlm->name)
  252. kfree(dlm->name);
  253. kfree(dlm);
  254. }
  255. /* A little strange - this function will be called while holding
  256. * dlm_domain_lock and is expected to be holding it on the way out. We
  257. * will however drop and reacquire it multiple times */
  258. static void dlm_ctxt_release(struct kref *kref)
  259. {
  260. struct dlm_ctxt *dlm;
  261. dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
  262. BUG_ON(dlm->num_joins);
  263. BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
  264. /* we may still be in the list if we hit an error during join. */
  265. list_del_init(&dlm->list);
  266. spin_unlock(&dlm_domain_lock);
  267. mlog(0, "freeing memory from domain %s\n", dlm->name);
  268. wake_up(&dlm_domain_events);
  269. dlm_free_ctxt_mem(dlm);
  270. spin_lock(&dlm_domain_lock);
  271. }
  272. void dlm_put(struct dlm_ctxt *dlm)
  273. {
  274. spin_lock(&dlm_domain_lock);
  275. kref_put(&dlm->dlm_refs, dlm_ctxt_release);
  276. spin_unlock(&dlm_domain_lock);
  277. }
  278. static void __dlm_get(struct dlm_ctxt *dlm)
  279. {
  280. kref_get(&dlm->dlm_refs);
  281. }
  282. /* given a questionable reference to a dlm object, gets a reference if
  283. * it can find it in the list, otherwise returns NULL in which case
  284. * you shouldn't trust your pointer. */
  285. struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
  286. {
  287. struct list_head *iter;
  288. struct dlm_ctxt *target = NULL;
  289. spin_lock(&dlm_domain_lock);
  290. list_for_each(iter, &dlm_domains) {
  291. target = list_entry (iter, struct dlm_ctxt, list);
  292. if (target == dlm) {
  293. __dlm_get(target);
  294. break;
  295. }
  296. target = NULL;
  297. }
  298. spin_unlock(&dlm_domain_lock);
  299. return target;
  300. }
  301. int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
  302. {
  303. int ret;
  304. spin_lock(&dlm_domain_lock);
  305. ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
  306. (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
  307. spin_unlock(&dlm_domain_lock);
  308. return ret;
  309. }
  310. static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
  311. {
  312. if (dlm->dlm_worker) {
  313. flush_workqueue(dlm->dlm_worker);
  314. destroy_workqueue(dlm->dlm_worker);
  315. dlm->dlm_worker = NULL;
  316. }
  317. }
  318. static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
  319. {
  320. dlm_unregister_domain_handlers(dlm);
  321. dlm_complete_thread(dlm);
  322. dlm_complete_recovery_thread(dlm);
  323. dlm_destroy_dlm_worker(dlm);
  324. /* We've left the domain. Now we can take ourselves out of the
  325. * list and allow the kref stuff to help us free the
  326. * memory. */
  327. spin_lock(&dlm_domain_lock);
  328. list_del_init(&dlm->list);
  329. spin_unlock(&dlm_domain_lock);
  330. /* Wake up anyone waiting for us to remove this domain */
  331. wake_up(&dlm_domain_events);
  332. }
  333. static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
  334. {
  335. int i, num, n, ret = 0;
  336. struct dlm_lock_resource *res;
  337. struct hlist_node *iter;
  338. struct hlist_head *bucket;
  339. int dropped;
  340. mlog(0, "Migrating locks from domain %s\n", dlm->name);
  341. num = 0;
  342. spin_lock(&dlm->spinlock);
  343. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  344. redo_bucket:
  345. n = 0;
  346. bucket = dlm_lockres_hash(dlm, i);
  347. iter = bucket->first;
  348. while (iter) {
  349. n++;
  350. res = hlist_entry(iter, struct dlm_lock_resource,
  351. hash_node);
  352. dlm_lockres_get(res);
  353. /* migrate, if necessary. this will drop the dlm
  354. * spinlock and retake it if it does migration. */
  355. dropped = dlm_empty_lockres(dlm, res);
  356. spin_lock(&res->spinlock);
  357. __dlm_lockres_calc_usage(dlm, res);
  358. iter = res->hash_node.next;
  359. spin_unlock(&res->spinlock);
  360. dlm_lockres_put(res);
  361. if (dropped)
  362. goto redo_bucket;
  363. }
  364. cond_resched_lock(&dlm->spinlock);
  365. num += n;
  366. mlog(0, "%s: touched %d lockreses in bucket %d "
  367. "(tot=%d)\n", dlm->name, n, i, num);
  368. }
  369. spin_unlock(&dlm->spinlock);
  370. wake_up(&dlm->dlm_thread_wq);
  371. /* let the dlm thread take care of purging, keep scanning until
  372. * nothing remains in the hash */
  373. if (num) {
  374. mlog(0, "%s: %d lock resources in hash last pass\n",
  375. dlm->name, num);
  376. ret = -EAGAIN;
  377. }
  378. mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
  379. return ret;
  380. }
  381. static int dlm_no_joining_node(struct dlm_ctxt *dlm)
  382. {
  383. int ret;
  384. spin_lock(&dlm->spinlock);
  385. ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
  386. spin_unlock(&dlm->spinlock);
  387. return ret;
  388. }
  389. static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
  390. {
  391. /* Yikes, a double spinlock! I need domain_lock for the dlm
  392. * state and the dlm spinlock for join state... Sorry! */
  393. again:
  394. spin_lock(&dlm_domain_lock);
  395. spin_lock(&dlm->spinlock);
  396. if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  397. mlog(0, "Node %d is joining, we wait on it.\n",
  398. dlm->joining_node);
  399. spin_unlock(&dlm->spinlock);
  400. spin_unlock(&dlm_domain_lock);
  401. wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
  402. goto again;
  403. }
  404. dlm->dlm_state = DLM_CTXT_LEAVING;
  405. spin_unlock(&dlm->spinlock);
  406. spin_unlock(&dlm_domain_lock);
  407. }
  408. static void __dlm_print_nodes(struct dlm_ctxt *dlm)
  409. {
  410. int node = -1;
  411. assert_spin_locked(&dlm->spinlock);
  412. printk(KERN_INFO "ocfs2_dlm: Nodes in domain (\"%s\"): ", dlm->name);
  413. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  414. node + 1)) < O2NM_MAX_NODES) {
  415. printk("%d ", node);
  416. }
  417. printk("\n");
  418. }
  419. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
  420. void **ret_data)
  421. {
  422. struct dlm_ctxt *dlm = data;
  423. unsigned int node;
  424. struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
  425. mlog_entry("%p %u %p", msg, len, data);
  426. if (!dlm_grab(dlm))
  427. return 0;
  428. node = exit_msg->node_idx;
  429. printk(KERN_INFO "ocfs2_dlm: Node %u leaves domain %s\n", node, dlm->name);
  430. spin_lock(&dlm->spinlock);
  431. clear_bit(node, dlm->domain_map);
  432. __dlm_print_nodes(dlm);
  433. /* notify anything attached to the heartbeat events */
  434. dlm_hb_event_notify_attached(dlm, node, 0);
  435. spin_unlock(&dlm->spinlock);
  436. dlm_put(dlm);
  437. return 0;
  438. }
  439. static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm,
  440. unsigned int node)
  441. {
  442. int status;
  443. struct dlm_exit_domain leave_msg;
  444. mlog(0, "Asking node %u if we can leave the domain %s me = %u\n",
  445. node, dlm->name, dlm->node_num);
  446. memset(&leave_msg, 0, sizeof(leave_msg));
  447. leave_msg.node_idx = dlm->node_num;
  448. status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key,
  449. &leave_msg, sizeof(leave_msg), node,
  450. NULL);
  451. mlog(0, "status return %d from o2net_send_message\n", status);
  452. return status;
  453. }
  454. static void dlm_leave_domain(struct dlm_ctxt *dlm)
  455. {
  456. int node, clear_node, status;
  457. /* At this point we've migrated away all our locks and won't
  458. * accept mastership of new ones. The dlm is responsible for
  459. * almost nothing now. We make sure not to confuse any joining
  460. * nodes and then commence shutdown procedure. */
  461. spin_lock(&dlm->spinlock);
  462. /* Clear ourselves from the domain map */
  463. clear_bit(dlm->node_num, dlm->domain_map);
  464. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  465. 0)) < O2NM_MAX_NODES) {
  466. /* Drop the dlm spinlock. This is safe wrt the domain_map.
  467. * -nodes cannot be added now as the
  468. * query_join_handlers knows to respond with OK_NO_MAP
  469. * -we catch the right network errors if a node is
  470. * removed from the map while we're sending him the
  471. * exit message. */
  472. spin_unlock(&dlm->spinlock);
  473. clear_node = 1;
  474. status = dlm_send_one_domain_exit(dlm, node);
  475. if (status < 0 &&
  476. status != -ENOPROTOOPT &&
  477. status != -ENOTCONN) {
  478. mlog(ML_NOTICE, "Error %d sending domain exit message "
  479. "to node %d\n", status, node);
  480. /* Not sure what to do here but lets sleep for
  481. * a bit in case this was a transient
  482. * error... */
  483. msleep(DLM_DOMAIN_BACKOFF_MS);
  484. clear_node = 0;
  485. }
  486. spin_lock(&dlm->spinlock);
  487. /* If we're not clearing the node bit then we intend
  488. * to loop back around to try again. */
  489. if (clear_node)
  490. clear_bit(node, dlm->domain_map);
  491. }
  492. spin_unlock(&dlm->spinlock);
  493. }
  494. int dlm_joined(struct dlm_ctxt *dlm)
  495. {
  496. int ret = 0;
  497. spin_lock(&dlm_domain_lock);
  498. if (dlm->dlm_state == DLM_CTXT_JOINED)
  499. ret = 1;
  500. spin_unlock(&dlm_domain_lock);
  501. return ret;
  502. }
  503. int dlm_shutting_down(struct dlm_ctxt *dlm)
  504. {
  505. int ret = 0;
  506. spin_lock(&dlm_domain_lock);
  507. if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
  508. ret = 1;
  509. spin_unlock(&dlm_domain_lock);
  510. return ret;
  511. }
  512. void dlm_unregister_domain(struct dlm_ctxt *dlm)
  513. {
  514. int leave = 0;
  515. spin_lock(&dlm_domain_lock);
  516. BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
  517. BUG_ON(!dlm->num_joins);
  518. dlm->num_joins--;
  519. if (!dlm->num_joins) {
  520. /* We mark it "in shutdown" now so new register
  521. * requests wait until we've completely left the
  522. * domain. Don't use DLM_CTXT_LEAVING yet as we still
  523. * want new domain joins to communicate with us at
  524. * least until we've completed migration of our
  525. * resources. */
  526. dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
  527. leave = 1;
  528. }
  529. spin_unlock(&dlm_domain_lock);
  530. if (leave) {
  531. mlog(0, "shutting down domain %s\n", dlm->name);
  532. /* We changed dlm state, notify the thread */
  533. dlm_kick_thread(dlm, NULL);
  534. while (dlm_migrate_all_locks(dlm)) {
  535. /* Give dlm_thread time to purge the lockres' */
  536. msleep(500);
  537. mlog(0, "%s: more migration to do\n", dlm->name);
  538. }
  539. dlm_mark_domain_leaving(dlm);
  540. dlm_leave_domain(dlm);
  541. dlm_complete_dlm_shutdown(dlm);
  542. }
  543. dlm_put(dlm);
  544. }
  545. EXPORT_SYMBOL_GPL(dlm_unregister_domain);
  546. static int dlm_query_join_proto_check(char *proto_type, int node,
  547. struct dlm_protocol_version *ours,
  548. struct dlm_protocol_version *request)
  549. {
  550. int rc;
  551. struct dlm_protocol_version proto = *request;
  552. if (!dlm_protocol_compare(ours, &proto)) {
  553. mlog(0,
  554. "node %u wanted to join with %s locking protocol "
  555. "%u.%u, we respond with %u.%u\n",
  556. node, proto_type,
  557. request->pv_major,
  558. request->pv_minor,
  559. proto.pv_major, proto.pv_minor);
  560. request->pv_minor = proto.pv_minor;
  561. rc = 0;
  562. } else {
  563. mlog(ML_NOTICE,
  564. "Node %u wanted to join with %s locking "
  565. "protocol %u.%u, but we have %u.%u, disallowing\n",
  566. node, proto_type,
  567. request->pv_major,
  568. request->pv_minor,
  569. ours->pv_major,
  570. ours->pv_minor);
  571. rc = 1;
  572. }
  573. return rc;
  574. }
  575. /*
  576. * struct dlm_query_join_packet is made up of four one-byte fields. They
  577. * are effectively in big-endian order already. However, little-endian
  578. * machines swap them before putting the packet on the wire (because
  579. * query_join's response is a status, and that status is treated as a u32
  580. * on the wire). Thus, a big-endian and little-endian machines will treat
  581. * this structure differently.
  582. *
  583. * The solution is to have little-endian machines swap the structure when
  584. * converting from the structure to the u32 representation. This will
  585. * result in the structure having the correct format on the wire no matter
  586. * the host endian format.
  587. */
  588. static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
  589. u32 *wire)
  590. {
  591. union dlm_query_join_response response;
  592. response.packet = *packet;
  593. *wire = cpu_to_be32(response.intval);
  594. }
  595. static void dlm_query_join_wire_to_packet(u32 wire,
  596. struct dlm_query_join_packet *packet)
  597. {
  598. union dlm_query_join_response response;
  599. response.intval = cpu_to_be32(wire);
  600. *packet = response.packet;
  601. }
  602. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  603. void **ret_data)
  604. {
  605. struct dlm_query_join_request *query;
  606. struct dlm_query_join_packet packet = {
  607. .code = JOIN_DISALLOW,
  608. };
  609. struct dlm_ctxt *dlm = NULL;
  610. u32 response;
  611. u8 nodenum;
  612. query = (struct dlm_query_join_request *) msg->buf;
  613. mlog(0, "node %u wants to join domain %s\n", query->node_idx,
  614. query->domain);
  615. /*
  616. * If heartbeat doesn't consider the node live, tell it
  617. * to back off and try again. This gives heartbeat a chance
  618. * to catch up.
  619. */
  620. if (!o2hb_check_node_heartbeating(query->node_idx)) {
  621. mlog(0, "node %u is not in our live map yet\n",
  622. query->node_idx);
  623. packet.code = JOIN_DISALLOW;
  624. goto respond;
  625. }
  626. packet.code = JOIN_OK_NO_MAP;
  627. spin_lock(&dlm_domain_lock);
  628. dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
  629. if (!dlm)
  630. goto unlock_respond;
  631. /*
  632. * There is a small window where the joining node may not see the
  633. * node(s) that just left but still part of the cluster. DISALLOW
  634. * join request if joining node has different node map.
  635. */
  636. nodenum=0;
  637. while (nodenum < O2NM_MAX_NODES) {
  638. if (test_bit(nodenum, dlm->domain_map)) {
  639. if (!byte_test_bit(nodenum, query->node_map)) {
  640. mlog(0, "disallow join as node %u does not "
  641. "have node %u in its nodemap\n",
  642. query->node_idx, nodenum);
  643. packet.code = JOIN_DISALLOW;
  644. goto unlock_respond;
  645. }
  646. }
  647. nodenum++;
  648. }
  649. /* Once the dlm ctxt is marked as leaving then we don't want
  650. * to be put in someone's domain map.
  651. * Also, explicitly disallow joining at certain troublesome
  652. * times (ie. during recovery). */
  653. if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
  654. int bit = query->node_idx;
  655. spin_lock(&dlm->spinlock);
  656. if (dlm->dlm_state == DLM_CTXT_NEW &&
  657. dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
  658. /*If this is a brand new context and we
  659. * haven't started our join process yet, then
  660. * the other node won the race. */
  661. packet.code = JOIN_OK_NO_MAP;
  662. } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  663. /* Disallow parallel joins. */
  664. packet.code = JOIN_DISALLOW;
  665. } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
  666. mlog(0, "node %u trying to join, but recovery "
  667. "is ongoing.\n", bit);
  668. packet.code = JOIN_DISALLOW;
  669. } else if (test_bit(bit, dlm->recovery_map)) {
  670. mlog(0, "node %u trying to join, but it "
  671. "still needs recovery.\n", bit);
  672. packet.code = JOIN_DISALLOW;
  673. } else if (test_bit(bit, dlm->domain_map)) {
  674. mlog(0, "node %u trying to join, but it "
  675. "is still in the domain! needs recovery?\n",
  676. bit);
  677. packet.code = JOIN_DISALLOW;
  678. } else {
  679. /* Alright we're fully a part of this domain
  680. * so we keep some state as to who's joining
  681. * and indicate to him that needs to be fixed
  682. * up. */
  683. /* Make sure we speak compatible locking protocols. */
  684. if (dlm_query_join_proto_check("DLM", bit,
  685. &dlm->dlm_locking_proto,
  686. &query->dlm_proto)) {
  687. packet.code = JOIN_PROTOCOL_MISMATCH;
  688. } else if (dlm_query_join_proto_check("fs", bit,
  689. &dlm->fs_locking_proto,
  690. &query->fs_proto)) {
  691. packet.code = JOIN_PROTOCOL_MISMATCH;
  692. } else {
  693. packet.dlm_minor = query->dlm_proto.pv_minor;
  694. packet.fs_minor = query->fs_proto.pv_minor;
  695. packet.code = JOIN_OK;
  696. __dlm_set_joining_node(dlm, query->node_idx);
  697. }
  698. }
  699. spin_unlock(&dlm->spinlock);
  700. }
  701. unlock_respond:
  702. spin_unlock(&dlm_domain_lock);
  703. respond:
  704. mlog(0, "We respond with %u\n", packet.code);
  705. dlm_query_join_packet_to_wire(&packet, &response);
  706. return response;
  707. }
  708. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  709. void **ret_data)
  710. {
  711. struct dlm_assert_joined *assert;
  712. struct dlm_ctxt *dlm = NULL;
  713. assert = (struct dlm_assert_joined *) msg->buf;
  714. mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
  715. assert->domain);
  716. spin_lock(&dlm_domain_lock);
  717. dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
  718. /* XXX should we consider no dlm ctxt an error? */
  719. if (dlm) {
  720. spin_lock(&dlm->spinlock);
  721. /* Alright, this node has officially joined our
  722. * domain. Set him in the map and clean up our
  723. * leftover join state. */
  724. BUG_ON(dlm->joining_node != assert->node_idx);
  725. set_bit(assert->node_idx, dlm->domain_map);
  726. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  727. printk(KERN_INFO "ocfs2_dlm: Node %u joins domain %s\n",
  728. assert->node_idx, dlm->name);
  729. __dlm_print_nodes(dlm);
  730. /* notify anything attached to the heartbeat events */
  731. dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
  732. spin_unlock(&dlm->spinlock);
  733. }
  734. spin_unlock(&dlm_domain_lock);
  735. return 0;
  736. }
  737. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  738. void **ret_data)
  739. {
  740. struct dlm_cancel_join *cancel;
  741. struct dlm_ctxt *dlm = NULL;
  742. cancel = (struct dlm_cancel_join *) msg->buf;
  743. mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
  744. cancel->domain);
  745. spin_lock(&dlm_domain_lock);
  746. dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
  747. if (dlm) {
  748. spin_lock(&dlm->spinlock);
  749. /* Yikes, this guy wants to cancel his join. No
  750. * problem, we simply cleanup our join state. */
  751. BUG_ON(dlm->joining_node != cancel->node_idx);
  752. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  753. spin_unlock(&dlm->spinlock);
  754. }
  755. spin_unlock(&dlm_domain_lock);
  756. return 0;
  757. }
  758. static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
  759. unsigned int node)
  760. {
  761. int status;
  762. struct dlm_cancel_join cancel_msg;
  763. memset(&cancel_msg, 0, sizeof(cancel_msg));
  764. cancel_msg.node_idx = dlm->node_num;
  765. cancel_msg.name_len = strlen(dlm->name);
  766. memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
  767. status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  768. &cancel_msg, sizeof(cancel_msg), node,
  769. NULL);
  770. if (status < 0) {
  771. mlog_errno(status);
  772. goto bail;
  773. }
  774. bail:
  775. return status;
  776. }
  777. /* map_size should be in bytes. */
  778. static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
  779. unsigned long *node_map,
  780. unsigned int map_size)
  781. {
  782. int status, tmpstat;
  783. unsigned int node;
  784. if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
  785. sizeof(unsigned long))) {
  786. mlog(ML_ERROR,
  787. "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
  788. map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
  789. return -EINVAL;
  790. }
  791. status = 0;
  792. node = -1;
  793. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  794. node + 1)) < O2NM_MAX_NODES) {
  795. if (node == dlm->node_num)
  796. continue;
  797. tmpstat = dlm_send_one_join_cancel(dlm, node);
  798. if (tmpstat) {
  799. mlog(ML_ERROR, "Error return %d cancelling join on "
  800. "node %d\n", tmpstat, node);
  801. if (!status)
  802. status = tmpstat;
  803. }
  804. }
  805. if (status)
  806. mlog_errno(status);
  807. return status;
  808. }
  809. static int dlm_request_join(struct dlm_ctxt *dlm,
  810. int node,
  811. enum dlm_query_join_response_code *response)
  812. {
  813. int status;
  814. struct dlm_query_join_request join_msg;
  815. struct dlm_query_join_packet packet;
  816. u32 join_resp;
  817. mlog(0, "querying node %d\n", node);
  818. memset(&join_msg, 0, sizeof(join_msg));
  819. join_msg.node_idx = dlm->node_num;
  820. join_msg.name_len = strlen(dlm->name);
  821. memcpy(join_msg.domain, dlm->name, join_msg.name_len);
  822. join_msg.dlm_proto = dlm->dlm_locking_proto;
  823. join_msg.fs_proto = dlm->fs_locking_proto;
  824. /* copy live node map to join message */
  825. byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
  826. status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
  827. sizeof(join_msg), node,
  828. &join_resp);
  829. if (status < 0 && status != -ENOPROTOOPT) {
  830. mlog_errno(status);
  831. goto bail;
  832. }
  833. dlm_query_join_wire_to_packet(join_resp, &packet);
  834. /* -ENOPROTOOPT from the net code means the other side isn't
  835. listening for our message type -- that's fine, it means
  836. his dlm isn't up, so we can consider him a 'yes' but not
  837. joined into the domain. */
  838. if (status == -ENOPROTOOPT) {
  839. status = 0;
  840. *response = JOIN_OK_NO_MAP;
  841. } else if (packet.code == JOIN_DISALLOW ||
  842. packet.code == JOIN_OK_NO_MAP) {
  843. *response = packet.code;
  844. } else if (packet.code == JOIN_PROTOCOL_MISMATCH) {
  845. mlog(ML_NOTICE,
  846. "This node requested DLM locking protocol %u.%u and "
  847. "filesystem locking protocol %u.%u. At least one of "
  848. "the protocol versions on node %d is not compatible, "
  849. "disconnecting\n",
  850. dlm->dlm_locking_proto.pv_major,
  851. dlm->dlm_locking_proto.pv_minor,
  852. dlm->fs_locking_proto.pv_major,
  853. dlm->fs_locking_proto.pv_minor,
  854. node);
  855. status = -EPROTO;
  856. *response = packet.code;
  857. } else if (packet.code == JOIN_OK) {
  858. *response = packet.code;
  859. /* Use the same locking protocol as the remote node */
  860. dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
  861. dlm->fs_locking_proto.pv_minor = packet.fs_minor;
  862. mlog(0,
  863. "Node %d responds JOIN_OK with DLM locking protocol "
  864. "%u.%u and fs locking protocol %u.%u\n",
  865. node,
  866. dlm->dlm_locking_proto.pv_major,
  867. dlm->dlm_locking_proto.pv_minor,
  868. dlm->fs_locking_proto.pv_major,
  869. dlm->fs_locking_proto.pv_minor);
  870. } else {
  871. status = -EINVAL;
  872. mlog(ML_ERROR, "invalid response %d from node %u\n",
  873. packet.code, node);
  874. }
  875. mlog(0, "status %d, node %d response is %d\n", status, node,
  876. *response);
  877. bail:
  878. return status;
  879. }
  880. static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
  881. unsigned int node)
  882. {
  883. int status;
  884. struct dlm_assert_joined assert_msg;
  885. mlog(0, "Sending join assert to node %u\n", node);
  886. memset(&assert_msg, 0, sizeof(assert_msg));
  887. assert_msg.node_idx = dlm->node_num;
  888. assert_msg.name_len = strlen(dlm->name);
  889. memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
  890. status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  891. &assert_msg, sizeof(assert_msg), node,
  892. NULL);
  893. if (status < 0)
  894. mlog_errno(status);
  895. return status;
  896. }
  897. static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
  898. unsigned long *node_map)
  899. {
  900. int status, node, live;
  901. status = 0;
  902. node = -1;
  903. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  904. node + 1)) < O2NM_MAX_NODES) {
  905. if (node == dlm->node_num)
  906. continue;
  907. do {
  908. /* It is very important that this message be
  909. * received so we spin until either the node
  910. * has died or it gets the message. */
  911. status = dlm_send_one_join_assert(dlm, node);
  912. spin_lock(&dlm->spinlock);
  913. live = test_bit(node, dlm->live_nodes_map);
  914. spin_unlock(&dlm->spinlock);
  915. if (status) {
  916. mlog(ML_ERROR, "Error return %d asserting "
  917. "join on node %d\n", status, node);
  918. /* give us some time between errors... */
  919. if (live)
  920. msleep(DLM_DOMAIN_BACKOFF_MS);
  921. }
  922. } while (status && live);
  923. }
  924. }
  925. struct domain_join_ctxt {
  926. unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  927. unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  928. };
  929. static int dlm_should_restart_join(struct dlm_ctxt *dlm,
  930. struct domain_join_ctxt *ctxt,
  931. enum dlm_query_join_response_code response)
  932. {
  933. int ret;
  934. if (response == JOIN_DISALLOW) {
  935. mlog(0, "Latest response of disallow -- should restart\n");
  936. return 1;
  937. }
  938. spin_lock(&dlm->spinlock);
  939. /* For now, we restart the process if the node maps have
  940. * changed at all */
  941. ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
  942. sizeof(dlm->live_nodes_map));
  943. spin_unlock(&dlm->spinlock);
  944. if (ret)
  945. mlog(0, "Node maps changed -- should restart\n");
  946. return ret;
  947. }
  948. static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
  949. {
  950. int status = 0, tmpstat, node;
  951. struct domain_join_ctxt *ctxt;
  952. enum dlm_query_join_response_code response = JOIN_DISALLOW;
  953. mlog_entry("%p", dlm);
  954. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  955. if (!ctxt) {
  956. status = -ENOMEM;
  957. mlog_errno(status);
  958. goto bail;
  959. }
  960. /* group sem locking should work for us here -- we're already
  961. * registered for heartbeat events so filling this should be
  962. * atomic wrt getting those handlers called. */
  963. o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
  964. spin_lock(&dlm->spinlock);
  965. memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
  966. __dlm_set_joining_node(dlm, dlm->node_num);
  967. spin_unlock(&dlm->spinlock);
  968. node = -1;
  969. while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
  970. node + 1)) < O2NM_MAX_NODES) {
  971. if (node == dlm->node_num)
  972. continue;
  973. status = dlm_request_join(dlm, node, &response);
  974. if (status < 0) {
  975. mlog_errno(status);
  976. goto bail;
  977. }
  978. /* Ok, either we got a response or the node doesn't have a
  979. * dlm up. */
  980. if (response == JOIN_OK)
  981. set_bit(node, ctxt->yes_resp_map);
  982. if (dlm_should_restart_join(dlm, ctxt, response)) {
  983. status = -EAGAIN;
  984. goto bail;
  985. }
  986. }
  987. mlog(0, "Yay, done querying nodes!\n");
  988. /* Yay, everyone agree's we can join the domain. My domain is
  989. * comprised of all nodes who were put in the
  990. * yes_resp_map. Copy that into our domain map and send a join
  991. * assert message to clean up everyone elses state. */
  992. spin_lock(&dlm->spinlock);
  993. memcpy(dlm->domain_map, ctxt->yes_resp_map,
  994. sizeof(ctxt->yes_resp_map));
  995. set_bit(dlm->node_num, dlm->domain_map);
  996. spin_unlock(&dlm->spinlock);
  997. dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
  998. /* Joined state *must* be set before the joining node
  999. * information, otherwise the query_join handler may read no
  1000. * current joiner but a state of NEW and tell joining nodes
  1001. * we're not in the domain. */
  1002. spin_lock(&dlm_domain_lock);
  1003. dlm->dlm_state = DLM_CTXT_JOINED;
  1004. dlm->num_joins++;
  1005. spin_unlock(&dlm_domain_lock);
  1006. bail:
  1007. spin_lock(&dlm->spinlock);
  1008. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  1009. if (!status)
  1010. __dlm_print_nodes(dlm);
  1011. spin_unlock(&dlm->spinlock);
  1012. if (ctxt) {
  1013. /* Do we need to send a cancel message to any nodes? */
  1014. if (status < 0) {
  1015. tmpstat = dlm_send_join_cancels(dlm,
  1016. ctxt->yes_resp_map,
  1017. sizeof(ctxt->yes_resp_map));
  1018. if (tmpstat < 0)
  1019. mlog_errno(tmpstat);
  1020. }
  1021. kfree(ctxt);
  1022. }
  1023. mlog(0, "returning %d\n", status);
  1024. return status;
  1025. }
  1026. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
  1027. {
  1028. o2hb_unregister_callback(NULL, &dlm->dlm_hb_up);
  1029. o2hb_unregister_callback(NULL, &dlm->dlm_hb_down);
  1030. o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
  1031. }
  1032. static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
  1033. {
  1034. int status;
  1035. mlog(0, "registering handlers.\n");
  1036. o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
  1037. dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
  1038. status = o2hb_register_callback(NULL, &dlm->dlm_hb_down);
  1039. if (status)
  1040. goto bail;
  1041. o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
  1042. dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
  1043. status = o2hb_register_callback(NULL, &dlm->dlm_hb_up);
  1044. if (status)
  1045. goto bail;
  1046. status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
  1047. sizeof(struct dlm_master_request),
  1048. dlm_master_request_handler,
  1049. dlm, NULL, &dlm->dlm_domain_handlers);
  1050. if (status)
  1051. goto bail;
  1052. status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
  1053. sizeof(struct dlm_assert_master),
  1054. dlm_assert_master_handler,
  1055. dlm, dlm_assert_master_post_handler,
  1056. &dlm->dlm_domain_handlers);
  1057. if (status)
  1058. goto bail;
  1059. status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
  1060. sizeof(struct dlm_create_lock),
  1061. dlm_create_lock_handler,
  1062. dlm, NULL, &dlm->dlm_domain_handlers);
  1063. if (status)
  1064. goto bail;
  1065. status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
  1066. DLM_CONVERT_LOCK_MAX_LEN,
  1067. dlm_convert_lock_handler,
  1068. dlm, NULL, &dlm->dlm_domain_handlers);
  1069. if (status)
  1070. goto bail;
  1071. status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
  1072. DLM_UNLOCK_LOCK_MAX_LEN,
  1073. dlm_unlock_lock_handler,
  1074. dlm, NULL, &dlm->dlm_domain_handlers);
  1075. if (status)
  1076. goto bail;
  1077. status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
  1078. DLM_PROXY_AST_MAX_LEN,
  1079. dlm_proxy_ast_handler,
  1080. dlm, NULL, &dlm->dlm_domain_handlers);
  1081. if (status)
  1082. goto bail;
  1083. status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
  1084. sizeof(struct dlm_exit_domain),
  1085. dlm_exit_domain_handler,
  1086. dlm, NULL, &dlm->dlm_domain_handlers);
  1087. if (status)
  1088. goto bail;
  1089. status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
  1090. sizeof(struct dlm_deref_lockres),
  1091. dlm_deref_lockres_handler,
  1092. dlm, NULL, &dlm->dlm_domain_handlers);
  1093. if (status)
  1094. goto bail;
  1095. status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  1096. sizeof(struct dlm_migrate_request),
  1097. dlm_migrate_request_handler,
  1098. dlm, NULL, &dlm->dlm_domain_handlers);
  1099. if (status)
  1100. goto bail;
  1101. status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
  1102. DLM_MIG_LOCKRES_MAX_LEN,
  1103. dlm_mig_lockres_handler,
  1104. dlm, NULL, &dlm->dlm_domain_handlers);
  1105. if (status)
  1106. goto bail;
  1107. status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
  1108. sizeof(struct dlm_master_requery),
  1109. dlm_master_requery_handler,
  1110. dlm, NULL, &dlm->dlm_domain_handlers);
  1111. if (status)
  1112. goto bail;
  1113. status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
  1114. sizeof(struct dlm_lock_request),
  1115. dlm_request_all_locks_handler,
  1116. dlm, NULL, &dlm->dlm_domain_handlers);
  1117. if (status)
  1118. goto bail;
  1119. status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
  1120. sizeof(struct dlm_reco_data_done),
  1121. dlm_reco_data_done_handler,
  1122. dlm, NULL, &dlm->dlm_domain_handlers);
  1123. if (status)
  1124. goto bail;
  1125. status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
  1126. sizeof(struct dlm_begin_reco),
  1127. dlm_begin_reco_handler,
  1128. dlm, NULL, &dlm->dlm_domain_handlers);
  1129. if (status)
  1130. goto bail;
  1131. status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
  1132. sizeof(struct dlm_finalize_reco),
  1133. dlm_finalize_reco_handler,
  1134. dlm, NULL, &dlm->dlm_domain_handlers);
  1135. if (status)
  1136. goto bail;
  1137. bail:
  1138. if (status)
  1139. dlm_unregister_domain_handlers(dlm);
  1140. return status;
  1141. }
  1142. static int dlm_join_domain(struct dlm_ctxt *dlm)
  1143. {
  1144. int status;
  1145. unsigned int backoff;
  1146. unsigned int total_backoff = 0;
  1147. BUG_ON(!dlm);
  1148. mlog(0, "Join domain %s\n", dlm->name);
  1149. status = dlm_register_domain_handlers(dlm);
  1150. if (status) {
  1151. mlog_errno(status);
  1152. goto bail;
  1153. }
  1154. status = dlm_launch_thread(dlm);
  1155. if (status < 0) {
  1156. mlog_errno(status);
  1157. goto bail;
  1158. }
  1159. status = dlm_launch_recovery_thread(dlm);
  1160. if (status < 0) {
  1161. mlog_errno(status);
  1162. goto bail;
  1163. }
  1164. dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
  1165. if (!dlm->dlm_worker) {
  1166. status = -ENOMEM;
  1167. mlog_errno(status);
  1168. goto bail;
  1169. }
  1170. do {
  1171. status = dlm_try_to_join_domain(dlm);
  1172. /* If we're racing another node to the join, then we
  1173. * need to back off temporarily and let them
  1174. * complete. */
  1175. #define DLM_JOIN_TIMEOUT_MSECS 90000
  1176. if (status == -EAGAIN) {
  1177. if (signal_pending(current)) {
  1178. status = -ERESTARTSYS;
  1179. goto bail;
  1180. }
  1181. if (total_backoff >
  1182. msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
  1183. status = -ERESTARTSYS;
  1184. mlog(ML_NOTICE, "Timed out joining dlm domain "
  1185. "%s after %u msecs\n", dlm->name,
  1186. jiffies_to_msecs(total_backoff));
  1187. goto bail;
  1188. }
  1189. /*
  1190. * <chip> After you!
  1191. * <dale> No, after you!
  1192. * <chip> I insist!
  1193. * <dale> But you first!
  1194. * ...
  1195. */
  1196. backoff = (unsigned int)(jiffies & 0x3);
  1197. backoff *= DLM_DOMAIN_BACKOFF_MS;
  1198. total_backoff += backoff;
  1199. mlog(0, "backoff %d\n", backoff);
  1200. msleep(backoff);
  1201. }
  1202. } while (status == -EAGAIN);
  1203. if (status < 0) {
  1204. mlog_errno(status);
  1205. goto bail;
  1206. }
  1207. status = 0;
  1208. bail:
  1209. wake_up(&dlm_domain_events);
  1210. if (status) {
  1211. dlm_unregister_domain_handlers(dlm);
  1212. dlm_complete_thread(dlm);
  1213. dlm_complete_recovery_thread(dlm);
  1214. dlm_destroy_dlm_worker(dlm);
  1215. }
  1216. return status;
  1217. }
  1218. static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
  1219. u32 key)
  1220. {
  1221. int i;
  1222. struct dlm_ctxt *dlm = NULL;
  1223. dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
  1224. if (!dlm) {
  1225. mlog_errno(-ENOMEM);
  1226. goto leave;
  1227. }
  1228. dlm->name = kmalloc(strlen(domain) + 1, GFP_KERNEL);
  1229. if (dlm->name == NULL) {
  1230. mlog_errno(-ENOMEM);
  1231. kfree(dlm);
  1232. dlm = NULL;
  1233. goto leave;
  1234. }
  1235. dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
  1236. if (!dlm->lockres_hash) {
  1237. mlog_errno(-ENOMEM);
  1238. kfree(dlm->name);
  1239. kfree(dlm);
  1240. dlm = NULL;
  1241. goto leave;
  1242. }
  1243. for (i = 0; i < DLM_HASH_BUCKETS; i++)
  1244. INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
  1245. strcpy(dlm->name, domain);
  1246. dlm->key = key;
  1247. dlm->node_num = o2nm_this_node();
  1248. spin_lock_init(&dlm->spinlock);
  1249. spin_lock_init(&dlm->master_lock);
  1250. spin_lock_init(&dlm->ast_lock);
  1251. INIT_LIST_HEAD(&dlm->list);
  1252. INIT_LIST_HEAD(&dlm->dirty_list);
  1253. INIT_LIST_HEAD(&dlm->reco.resources);
  1254. INIT_LIST_HEAD(&dlm->reco.received);
  1255. INIT_LIST_HEAD(&dlm->reco.node_data);
  1256. INIT_LIST_HEAD(&dlm->purge_list);
  1257. INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
  1258. dlm->reco.state = 0;
  1259. INIT_LIST_HEAD(&dlm->pending_asts);
  1260. INIT_LIST_HEAD(&dlm->pending_basts);
  1261. mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
  1262. dlm->recovery_map, &(dlm->recovery_map[0]));
  1263. memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
  1264. memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
  1265. memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
  1266. dlm->dlm_thread_task = NULL;
  1267. dlm->dlm_reco_thread_task = NULL;
  1268. dlm->dlm_worker = NULL;
  1269. init_waitqueue_head(&dlm->dlm_thread_wq);
  1270. init_waitqueue_head(&dlm->dlm_reco_thread_wq);
  1271. init_waitqueue_head(&dlm->reco.event);
  1272. init_waitqueue_head(&dlm->ast_wq);
  1273. init_waitqueue_head(&dlm->migration_wq);
  1274. INIT_LIST_HEAD(&dlm->master_list);
  1275. INIT_LIST_HEAD(&dlm->mle_hb_events);
  1276. dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
  1277. init_waitqueue_head(&dlm->dlm_join_events);
  1278. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  1279. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  1280. atomic_set(&dlm->local_resources, 0);
  1281. atomic_set(&dlm->remote_resources, 0);
  1282. atomic_set(&dlm->unknown_resources, 0);
  1283. spin_lock_init(&dlm->work_lock);
  1284. INIT_LIST_HEAD(&dlm->work_list);
  1285. INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
  1286. kref_init(&dlm->dlm_refs);
  1287. dlm->dlm_state = DLM_CTXT_NEW;
  1288. INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
  1289. mlog(0, "context init: refcount %u\n",
  1290. atomic_read(&dlm->dlm_refs.refcount));
  1291. leave:
  1292. return dlm;
  1293. }
  1294. /*
  1295. * Compare a requested locking protocol version against the current one.
  1296. *
  1297. * If the major numbers are different, they are incompatible.
  1298. * If the current minor is greater than the request, they are incompatible.
  1299. * If the current minor is less than or equal to the request, they are
  1300. * compatible, and the requester should run at the current minor version.
  1301. */
  1302. static int dlm_protocol_compare(struct dlm_protocol_version *existing,
  1303. struct dlm_protocol_version *request)
  1304. {
  1305. if (existing->pv_major != request->pv_major)
  1306. return 1;
  1307. if (existing->pv_minor > request->pv_minor)
  1308. return 1;
  1309. if (existing->pv_minor < request->pv_minor)
  1310. request->pv_minor = existing->pv_minor;
  1311. return 0;
  1312. }
  1313. /*
  1314. * dlm_register_domain: one-time setup per "domain".
  1315. *
  1316. * The filesystem passes in the requested locking version via proto.
  1317. * If registration was successful, proto will contain the negotiated
  1318. * locking protocol.
  1319. */
  1320. struct dlm_ctxt * dlm_register_domain(const char *domain,
  1321. u32 key,
  1322. struct dlm_protocol_version *fs_proto)
  1323. {
  1324. int ret;
  1325. struct dlm_ctxt *dlm = NULL;
  1326. struct dlm_ctxt *new_ctxt = NULL;
  1327. if (strlen(domain) > O2NM_MAX_NAME_LEN) {
  1328. ret = -ENAMETOOLONG;
  1329. mlog(ML_ERROR, "domain name length too long\n");
  1330. goto leave;
  1331. }
  1332. if (!o2hb_check_local_node_heartbeating()) {
  1333. mlog(ML_ERROR, "the local node has not been configured, or is "
  1334. "not heartbeating\n");
  1335. ret = -EPROTO;
  1336. goto leave;
  1337. }
  1338. mlog(0, "register called for domain \"%s\"\n", domain);
  1339. retry:
  1340. dlm = NULL;
  1341. if (signal_pending(current)) {
  1342. ret = -ERESTARTSYS;
  1343. mlog_errno(ret);
  1344. goto leave;
  1345. }
  1346. spin_lock(&dlm_domain_lock);
  1347. dlm = __dlm_lookup_domain(domain);
  1348. if (dlm) {
  1349. if (dlm->dlm_state != DLM_CTXT_JOINED) {
  1350. spin_unlock(&dlm_domain_lock);
  1351. mlog(0, "This ctxt is not joined yet!\n");
  1352. wait_event_interruptible(dlm_domain_events,
  1353. dlm_wait_on_domain_helper(
  1354. domain));
  1355. goto retry;
  1356. }
  1357. if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
  1358. mlog(ML_ERROR,
  1359. "Requested locking protocol version is not "
  1360. "compatible with already registered domain "
  1361. "\"%s\"\n", domain);
  1362. ret = -EPROTO;
  1363. goto leave;
  1364. }
  1365. __dlm_get(dlm);
  1366. dlm->num_joins++;
  1367. spin_unlock(&dlm_domain_lock);
  1368. ret = 0;
  1369. goto leave;
  1370. }
  1371. /* doesn't exist */
  1372. if (!new_ctxt) {
  1373. spin_unlock(&dlm_domain_lock);
  1374. new_ctxt = dlm_alloc_ctxt(domain, key);
  1375. if (new_ctxt)
  1376. goto retry;
  1377. ret = -ENOMEM;
  1378. mlog_errno(ret);
  1379. goto leave;
  1380. }
  1381. /* a little variable switch-a-roo here... */
  1382. dlm = new_ctxt;
  1383. new_ctxt = NULL;
  1384. /* add the new domain */
  1385. list_add_tail(&dlm->list, &dlm_domains);
  1386. spin_unlock(&dlm_domain_lock);
  1387. /*
  1388. * Pass the locking protocol version into the join. If the join
  1389. * succeeds, it will have the negotiated protocol set.
  1390. */
  1391. dlm->dlm_locking_proto = dlm_protocol;
  1392. dlm->fs_locking_proto = *fs_proto;
  1393. ret = dlm_join_domain(dlm);
  1394. if (ret) {
  1395. mlog_errno(ret);
  1396. dlm_put(dlm);
  1397. goto leave;
  1398. }
  1399. /* Tell the caller what locking protocol we negotiated */
  1400. *fs_proto = dlm->fs_locking_proto;
  1401. ret = 0;
  1402. leave:
  1403. if (new_ctxt)
  1404. dlm_free_ctxt_mem(new_ctxt);
  1405. if (ret < 0)
  1406. dlm = ERR_PTR(ret);
  1407. return dlm;
  1408. }
  1409. EXPORT_SYMBOL_GPL(dlm_register_domain);
  1410. static LIST_HEAD(dlm_join_handlers);
  1411. static void dlm_unregister_net_handlers(void)
  1412. {
  1413. o2net_unregister_handler_list(&dlm_join_handlers);
  1414. }
  1415. static int dlm_register_net_handlers(void)
  1416. {
  1417. int status = 0;
  1418. status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1419. sizeof(struct dlm_query_join_request),
  1420. dlm_query_join_handler,
  1421. NULL, NULL, &dlm_join_handlers);
  1422. if (status)
  1423. goto bail;
  1424. status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1425. sizeof(struct dlm_assert_joined),
  1426. dlm_assert_joined_handler,
  1427. NULL, NULL, &dlm_join_handlers);
  1428. if (status)
  1429. goto bail;
  1430. status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1431. sizeof(struct dlm_cancel_join),
  1432. dlm_cancel_join_handler,
  1433. NULL, NULL, &dlm_join_handlers);
  1434. bail:
  1435. if (status < 0)
  1436. dlm_unregister_net_handlers();
  1437. return status;
  1438. }
  1439. /* Domain eviction callback handling.
  1440. *
  1441. * The file system requires notification of node death *before* the
  1442. * dlm completes it's recovery work, otherwise it may be able to
  1443. * acquire locks on resources requiring recovery. Since the dlm can
  1444. * evict a node from it's domain *before* heartbeat fires, a similar
  1445. * mechanism is required. */
  1446. /* Eviction is not expected to happen often, so a per-domain lock is
  1447. * not necessary. Eviction callbacks are allowed to sleep for short
  1448. * periods of time. */
  1449. static DECLARE_RWSEM(dlm_callback_sem);
  1450. void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
  1451. int node_num)
  1452. {
  1453. struct list_head *iter;
  1454. struct dlm_eviction_cb *cb;
  1455. down_read(&dlm_callback_sem);
  1456. list_for_each(iter, &dlm->dlm_eviction_callbacks) {
  1457. cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
  1458. cb->ec_func(node_num, cb->ec_data);
  1459. }
  1460. up_read(&dlm_callback_sem);
  1461. }
  1462. void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
  1463. dlm_eviction_func *f,
  1464. void *data)
  1465. {
  1466. INIT_LIST_HEAD(&cb->ec_item);
  1467. cb->ec_func = f;
  1468. cb->ec_data = data;
  1469. }
  1470. EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
  1471. void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
  1472. struct dlm_eviction_cb *cb)
  1473. {
  1474. down_write(&dlm_callback_sem);
  1475. list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
  1476. up_write(&dlm_callback_sem);
  1477. }
  1478. EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
  1479. void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
  1480. {
  1481. down_write(&dlm_callback_sem);
  1482. list_del_init(&cb->ec_item);
  1483. up_write(&dlm_callback_sem);
  1484. }
  1485. EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
  1486. static int __init dlm_init(void)
  1487. {
  1488. int status;
  1489. dlm_print_version();
  1490. status = dlm_init_mle_cache();
  1491. if (status)
  1492. return -1;
  1493. status = dlm_register_net_handlers();
  1494. if (status) {
  1495. dlm_destroy_mle_cache();
  1496. return -1;
  1497. }
  1498. return 0;
  1499. }
  1500. static void __exit dlm_exit (void)
  1501. {
  1502. dlm_unregister_net_handlers();
  1503. dlm_destroy_mle_cache();
  1504. }
  1505. MODULE_AUTHOR("Oracle");
  1506. MODULE_LICENSE("GPL");
  1507. module_init(dlm_init);
  1508. module_exit(dlm_exit);