dlmdomain.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399
  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/init.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/delay.h>
  33. #include <linux/err.h>
  34. #include <linux/debugfs.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 "dlmdebug.h"
  42. #include "dlmver.h"
  43. #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)
  44. #include "cluster/masklog.h"
  45. /*
  46. * ocfs2 node maps are array of long int, which limits to send them freely
  47. * across the wire due to endianness issues. To workaround this, we convert
  48. * long ints to byte arrays. Following 3 routines are helper functions to
  49. * set/test/copy bits within those array of bytes
  50. */
  51. static inline void byte_set_bit(u8 nr, u8 map[])
  52. {
  53. map[nr >> 3] |= (1UL << (nr & 7));
  54. }
  55. static inline int byte_test_bit(u8 nr, u8 map[])
  56. {
  57. return ((1UL << (nr & 7)) & (map[nr >> 3])) != 0;
  58. }
  59. static inline void byte_copymap(u8 dmap[], unsigned long smap[],
  60. unsigned int sz)
  61. {
  62. unsigned int nn;
  63. if (!sz)
  64. return;
  65. memset(dmap, 0, ((sz + 7) >> 3));
  66. for (nn = 0 ; nn < sz; nn++)
  67. if (test_bit(nn, smap))
  68. byte_set_bit(nn, dmap);
  69. }
  70. static void dlm_free_pagevec(void **vec, int pages)
  71. {
  72. while (pages--)
  73. free_page((unsigned long)vec[pages]);
  74. kfree(vec);
  75. }
  76. static void **dlm_alloc_pagevec(int pages)
  77. {
  78. void **vec = kmalloc(pages * sizeof(void *), GFP_KERNEL);
  79. int i;
  80. if (!vec)
  81. return NULL;
  82. for (i = 0; i < pages; i++)
  83. if (!(vec[i] = (void *)__get_free_page(GFP_KERNEL)))
  84. goto out_free;
  85. mlog(0, "Allocated DLM hash pagevec; %d pages (%lu expected), %lu buckets per page\n",
  86. pages, (unsigned long)DLM_HASH_PAGES,
  87. (unsigned long)DLM_BUCKETS_PER_PAGE);
  88. return vec;
  89. out_free:
  90. dlm_free_pagevec(vec, i);
  91. return NULL;
  92. }
  93. /*
  94. *
  95. * spinlock lock ordering: if multiple locks are needed, obey this ordering:
  96. * dlm_domain_lock
  97. * struct dlm_ctxt->spinlock
  98. * struct dlm_lock_resource->spinlock
  99. * struct dlm_ctxt->master_lock
  100. * struct dlm_ctxt->ast_lock
  101. * dlm_master_list_entry->spinlock
  102. * dlm_lock->spinlock
  103. *
  104. */
  105. DEFINE_SPINLOCK(dlm_domain_lock);
  106. LIST_HEAD(dlm_domains);
  107. static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);
  108. /*
  109. * The supported protocol version for DLM communication. Running domains
  110. * will have a negotiated version with the same major number and a minor
  111. * number equal or smaller. The dlm_ctxt->dlm_locking_proto field should
  112. * be used to determine what a running domain is actually using.
  113. *
  114. * New in version 1.1:
  115. * - Message DLM_QUERY_REGION added to support global heartbeat
  116. * - Message DLM_QUERY_NODEINFO added to allow online node removes
  117. * New in version 1.2:
  118. * - Message DLM_BEGIN_EXIT_DOMAIN_MSG added to mark start of exit domain
  119. */
  120. static const struct dlm_protocol_version dlm_protocol = {
  121. .pv_major = 1,
  122. .pv_minor = 2,
  123. };
  124. #define DLM_DOMAIN_BACKOFF_MS 200
  125. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  126. void **ret_data);
  127. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  128. void **ret_data);
  129. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  130. void **ret_data);
  131. static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
  132. void *data, void **ret_data);
  133. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
  134. void **ret_data);
  135. static int dlm_protocol_compare(struct dlm_protocol_version *existing,
  136. struct dlm_protocol_version *request);
  137. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);
  138. void __dlm_unhash_lockres(struct dlm_lock_resource *lockres)
  139. {
  140. if (!hlist_unhashed(&lockres->hash_node)) {
  141. hlist_del_init(&lockres->hash_node);
  142. dlm_lockres_put(lockres);
  143. }
  144. }
  145. void __dlm_insert_lockres(struct dlm_ctxt *dlm,
  146. struct dlm_lock_resource *res)
  147. {
  148. struct hlist_head *bucket;
  149. struct qstr *q;
  150. assert_spin_locked(&dlm->spinlock);
  151. q = &res->lockname;
  152. bucket = dlm_lockres_hash(dlm, q->hash);
  153. /* get a reference for our hashtable */
  154. dlm_lockres_get(res);
  155. hlist_add_head(&res->hash_node, bucket);
  156. }
  157. struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
  158. const char *name,
  159. unsigned int len,
  160. unsigned int hash)
  161. {
  162. struct hlist_head *bucket;
  163. struct hlist_node *list;
  164. mlog(0, "%.*s\n", len, name);
  165. assert_spin_locked(&dlm->spinlock);
  166. bucket = dlm_lockres_hash(dlm, hash);
  167. hlist_for_each(list, bucket) {
  168. struct dlm_lock_resource *res = hlist_entry(list,
  169. struct dlm_lock_resource, hash_node);
  170. if (res->lockname.name[0] != name[0])
  171. continue;
  172. if (unlikely(res->lockname.len != len))
  173. continue;
  174. if (memcmp(res->lockname.name + 1, name + 1, len - 1))
  175. continue;
  176. dlm_lockres_get(res);
  177. return res;
  178. }
  179. return NULL;
  180. }
  181. /* intended to be called by functions which do not care about lock
  182. * resources which are being purged (most net _handler functions).
  183. * this will return NULL for any lock resource which is found but
  184. * currently in the process of dropping its mastery reference.
  185. * use __dlm_lookup_lockres_full when you need the lock resource
  186. * regardless (e.g. dlm_get_lock_resource) */
  187. struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
  188. const char *name,
  189. unsigned int len,
  190. unsigned int hash)
  191. {
  192. struct dlm_lock_resource *res = NULL;
  193. mlog(0, "%.*s\n", len, name);
  194. assert_spin_locked(&dlm->spinlock);
  195. res = __dlm_lookup_lockres_full(dlm, name, len, hash);
  196. if (res) {
  197. spin_lock(&res->spinlock);
  198. if (res->state & DLM_LOCK_RES_DROPPING_REF) {
  199. spin_unlock(&res->spinlock);
  200. dlm_lockres_put(res);
  201. return NULL;
  202. }
  203. spin_unlock(&res->spinlock);
  204. }
  205. return res;
  206. }
  207. struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
  208. const char *name,
  209. unsigned int len)
  210. {
  211. struct dlm_lock_resource *res;
  212. unsigned int hash = dlm_lockid_hash(name, len);
  213. spin_lock(&dlm->spinlock);
  214. res = __dlm_lookup_lockres(dlm, name, len, hash);
  215. spin_unlock(&dlm->spinlock);
  216. return res;
  217. }
  218. static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len)
  219. {
  220. struct dlm_ctxt *tmp = NULL;
  221. struct list_head *iter;
  222. assert_spin_locked(&dlm_domain_lock);
  223. /* tmp->name here is always NULL terminated,
  224. * but domain may not be! */
  225. list_for_each(iter, &dlm_domains) {
  226. tmp = list_entry (iter, struct dlm_ctxt, list);
  227. if (strlen(tmp->name) == len &&
  228. memcmp(tmp->name, domain, len)==0)
  229. break;
  230. tmp = NULL;
  231. }
  232. return tmp;
  233. }
  234. /* For null terminated domain strings ONLY */
  235. static struct dlm_ctxt * __dlm_lookup_domain(const char *domain)
  236. {
  237. assert_spin_locked(&dlm_domain_lock);
  238. return __dlm_lookup_domain_full(domain, strlen(domain));
  239. }
  240. /* returns true on one of two conditions:
  241. * 1) the domain does not exist
  242. * 2) the domain exists and it's state is "joined" */
  243. static int dlm_wait_on_domain_helper(const char *domain)
  244. {
  245. int ret = 0;
  246. struct dlm_ctxt *tmp = NULL;
  247. spin_lock(&dlm_domain_lock);
  248. tmp = __dlm_lookup_domain(domain);
  249. if (!tmp)
  250. ret = 1;
  251. else if (tmp->dlm_state == DLM_CTXT_JOINED)
  252. ret = 1;
  253. spin_unlock(&dlm_domain_lock);
  254. return ret;
  255. }
  256. static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm)
  257. {
  258. dlm_destroy_debugfs_subroot(dlm);
  259. if (dlm->lockres_hash)
  260. dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
  261. if (dlm->master_hash)
  262. dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
  263. if (dlm->name)
  264. kfree(dlm->name);
  265. kfree(dlm);
  266. }
  267. /* A little strange - this function will be called while holding
  268. * dlm_domain_lock and is expected to be holding it on the way out. We
  269. * will however drop and reacquire it multiple times */
  270. static void dlm_ctxt_release(struct kref *kref)
  271. {
  272. struct dlm_ctxt *dlm;
  273. dlm = container_of(kref, struct dlm_ctxt, dlm_refs);
  274. BUG_ON(dlm->num_joins);
  275. BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED);
  276. /* we may still be in the list if we hit an error during join. */
  277. list_del_init(&dlm->list);
  278. spin_unlock(&dlm_domain_lock);
  279. mlog(0, "freeing memory from domain %s\n", dlm->name);
  280. wake_up(&dlm_domain_events);
  281. dlm_free_ctxt_mem(dlm);
  282. spin_lock(&dlm_domain_lock);
  283. }
  284. void dlm_put(struct dlm_ctxt *dlm)
  285. {
  286. spin_lock(&dlm_domain_lock);
  287. kref_put(&dlm->dlm_refs, dlm_ctxt_release);
  288. spin_unlock(&dlm_domain_lock);
  289. }
  290. static void __dlm_get(struct dlm_ctxt *dlm)
  291. {
  292. kref_get(&dlm->dlm_refs);
  293. }
  294. /* given a questionable reference to a dlm object, gets a reference if
  295. * it can find it in the list, otherwise returns NULL in which case
  296. * you shouldn't trust your pointer. */
  297. struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm)
  298. {
  299. struct list_head *iter;
  300. struct dlm_ctxt *target = NULL;
  301. spin_lock(&dlm_domain_lock);
  302. list_for_each(iter, &dlm_domains) {
  303. target = list_entry (iter, struct dlm_ctxt, list);
  304. if (target == dlm) {
  305. __dlm_get(target);
  306. break;
  307. }
  308. target = NULL;
  309. }
  310. spin_unlock(&dlm_domain_lock);
  311. return target;
  312. }
  313. int dlm_domain_fully_joined(struct dlm_ctxt *dlm)
  314. {
  315. int ret;
  316. spin_lock(&dlm_domain_lock);
  317. ret = (dlm->dlm_state == DLM_CTXT_JOINED) ||
  318. (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN);
  319. spin_unlock(&dlm_domain_lock);
  320. return ret;
  321. }
  322. static void dlm_destroy_dlm_worker(struct dlm_ctxt *dlm)
  323. {
  324. if (dlm->dlm_worker) {
  325. flush_workqueue(dlm->dlm_worker);
  326. destroy_workqueue(dlm->dlm_worker);
  327. dlm->dlm_worker = NULL;
  328. }
  329. }
  330. static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm)
  331. {
  332. dlm_unregister_domain_handlers(dlm);
  333. dlm_debug_shutdown(dlm);
  334. dlm_complete_thread(dlm);
  335. dlm_complete_recovery_thread(dlm);
  336. dlm_destroy_dlm_worker(dlm);
  337. /* We've left the domain. Now we can take ourselves out of the
  338. * list and allow the kref stuff to help us free the
  339. * memory. */
  340. spin_lock(&dlm_domain_lock);
  341. list_del_init(&dlm->list);
  342. spin_unlock(&dlm_domain_lock);
  343. /* Wake up anyone waiting for us to remove this domain */
  344. wake_up(&dlm_domain_events);
  345. }
  346. static int dlm_migrate_all_locks(struct dlm_ctxt *dlm)
  347. {
  348. int i, num, n, ret = 0;
  349. struct dlm_lock_resource *res;
  350. struct hlist_node *iter;
  351. struct hlist_head *bucket;
  352. int dropped;
  353. mlog(0, "Migrating locks from domain %s\n", dlm->name);
  354. num = 0;
  355. spin_lock(&dlm->spinlock);
  356. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  357. redo_bucket:
  358. n = 0;
  359. bucket = dlm_lockres_hash(dlm, i);
  360. iter = bucket->first;
  361. while (iter) {
  362. n++;
  363. res = hlist_entry(iter, struct dlm_lock_resource,
  364. hash_node);
  365. dlm_lockres_get(res);
  366. /* migrate, if necessary. this will drop the dlm
  367. * spinlock and retake it if it does migration. */
  368. dropped = dlm_empty_lockres(dlm, res);
  369. spin_lock(&res->spinlock);
  370. if (dropped)
  371. __dlm_lockres_calc_usage(dlm, res);
  372. else
  373. iter = res->hash_node.next;
  374. spin_unlock(&res->spinlock);
  375. dlm_lockres_put(res);
  376. if (dropped) {
  377. cond_resched_lock(&dlm->spinlock);
  378. goto redo_bucket;
  379. }
  380. }
  381. cond_resched_lock(&dlm->spinlock);
  382. num += n;
  383. }
  384. spin_unlock(&dlm->spinlock);
  385. wake_up(&dlm->dlm_thread_wq);
  386. /* let the dlm thread take care of purging, keep scanning until
  387. * nothing remains in the hash */
  388. if (num) {
  389. mlog(0, "%s: %d lock resources in hash last pass\n",
  390. dlm->name, num);
  391. ret = -EAGAIN;
  392. }
  393. mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);
  394. return ret;
  395. }
  396. static int dlm_no_joining_node(struct dlm_ctxt *dlm)
  397. {
  398. int ret;
  399. spin_lock(&dlm->spinlock);
  400. ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN;
  401. spin_unlock(&dlm->spinlock);
  402. return ret;
  403. }
  404. static int dlm_begin_exit_domain_handler(struct o2net_msg *msg, u32 len,
  405. void *data, void **ret_data)
  406. {
  407. struct dlm_ctxt *dlm = data;
  408. unsigned int node;
  409. struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
  410. if (!dlm_grab(dlm))
  411. return 0;
  412. node = exit_msg->node_idx;
  413. mlog(0, "%s: Node %u sent a begin exit domain message\n", dlm->name, node);
  414. spin_lock(&dlm->spinlock);
  415. set_bit(node, dlm->exit_domain_map);
  416. spin_unlock(&dlm->spinlock);
  417. dlm_put(dlm);
  418. return 0;
  419. }
  420. static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm)
  421. {
  422. /* Yikes, a double spinlock! I need domain_lock for the dlm
  423. * state and the dlm spinlock for join state... Sorry! */
  424. again:
  425. spin_lock(&dlm_domain_lock);
  426. spin_lock(&dlm->spinlock);
  427. if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  428. mlog(0, "Node %d is joining, we wait on it.\n",
  429. dlm->joining_node);
  430. spin_unlock(&dlm->spinlock);
  431. spin_unlock(&dlm_domain_lock);
  432. wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm));
  433. goto again;
  434. }
  435. dlm->dlm_state = DLM_CTXT_LEAVING;
  436. spin_unlock(&dlm->spinlock);
  437. spin_unlock(&dlm_domain_lock);
  438. }
  439. static void __dlm_print_nodes(struct dlm_ctxt *dlm)
  440. {
  441. int node = -1, num = 0;
  442. assert_spin_locked(&dlm->spinlock);
  443. printk("( ");
  444. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  445. node + 1)) < O2NM_MAX_NODES) {
  446. printk("%d ", node);
  447. ++num;
  448. }
  449. printk(") %u nodes\n", num);
  450. }
  451. static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data,
  452. void **ret_data)
  453. {
  454. struct dlm_ctxt *dlm = data;
  455. unsigned int node;
  456. struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf;
  457. mlog(0, "%p %u %p", msg, len, data);
  458. if (!dlm_grab(dlm))
  459. return 0;
  460. node = exit_msg->node_idx;
  461. spin_lock(&dlm->spinlock);
  462. clear_bit(node, dlm->domain_map);
  463. clear_bit(node, dlm->exit_domain_map);
  464. printk(KERN_NOTICE "o2dlm: Node %u leaves domain %s ", node, dlm->name);
  465. __dlm_print_nodes(dlm);
  466. /* notify anything attached to the heartbeat events */
  467. dlm_hb_event_notify_attached(dlm, node, 0);
  468. spin_unlock(&dlm->spinlock);
  469. dlm_put(dlm);
  470. return 0;
  471. }
  472. static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm, u32 msg_type,
  473. unsigned int node)
  474. {
  475. int status;
  476. struct dlm_exit_domain leave_msg;
  477. mlog(0, "%s: Sending domain exit message %u to node %u\n", dlm->name,
  478. msg_type, node);
  479. memset(&leave_msg, 0, sizeof(leave_msg));
  480. leave_msg.node_idx = dlm->node_num;
  481. status = o2net_send_message(msg_type, dlm->key, &leave_msg,
  482. sizeof(leave_msg), node, NULL);
  483. if (status < 0)
  484. mlog(ML_ERROR, "Error %d sending domain exit message %u "
  485. "to node %u on domain %s\n", status, msg_type, node,
  486. dlm->name);
  487. return status;
  488. }
  489. static void dlm_begin_exit_domain(struct dlm_ctxt *dlm)
  490. {
  491. int node = -1;
  492. /* Support for begin exit domain was added in 1.2 */
  493. if (dlm->dlm_locking_proto.pv_major == 1 &&
  494. dlm->dlm_locking_proto.pv_minor < 2)
  495. return;
  496. /*
  497. * Unlike DLM_EXIT_DOMAIN_MSG, DLM_BEGIN_EXIT_DOMAIN_MSG is purely
  498. * informational. Meaning if a node does not receive the message,
  499. * so be it.
  500. */
  501. spin_lock(&dlm->spinlock);
  502. while (1) {
  503. node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, node + 1);
  504. if (node >= O2NM_MAX_NODES)
  505. break;
  506. if (node == dlm->node_num)
  507. continue;
  508. spin_unlock(&dlm->spinlock);
  509. dlm_send_one_domain_exit(dlm, DLM_BEGIN_EXIT_DOMAIN_MSG, node);
  510. spin_lock(&dlm->spinlock);
  511. }
  512. spin_unlock(&dlm->spinlock);
  513. }
  514. static void dlm_leave_domain(struct dlm_ctxt *dlm)
  515. {
  516. int node, clear_node, status;
  517. /* At this point we've migrated away all our locks and won't
  518. * accept mastership of new ones. The dlm is responsible for
  519. * almost nothing now. We make sure not to confuse any joining
  520. * nodes and then commence shutdown procedure. */
  521. spin_lock(&dlm->spinlock);
  522. /* Clear ourselves from the domain map */
  523. clear_bit(dlm->node_num, dlm->domain_map);
  524. while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES,
  525. 0)) < O2NM_MAX_NODES) {
  526. /* Drop the dlm spinlock. This is safe wrt the domain_map.
  527. * -nodes cannot be added now as the
  528. * query_join_handlers knows to respond with OK_NO_MAP
  529. * -we catch the right network errors if a node is
  530. * removed from the map while we're sending him the
  531. * exit message. */
  532. spin_unlock(&dlm->spinlock);
  533. clear_node = 1;
  534. status = dlm_send_one_domain_exit(dlm, DLM_EXIT_DOMAIN_MSG,
  535. node);
  536. if (status < 0 &&
  537. status != -ENOPROTOOPT &&
  538. status != -ENOTCONN) {
  539. mlog(ML_NOTICE, "Error %d sending domain exit message "
  540. "to node %d\n", status, node);
  541. /* Not sure what to do here but lets sleep for
  542. * a bit in case this was a transient
  543. * error... */
  544. msleep(DLM_DOMAIN_BACKOFF_MS);
  545. clear_node = 0;
  546. }
  547. spin_lock(&dlm->spinlock);
  548. /* If we're not clearing the node bit then we intend
  549. * to loop back around to try again. */
  550. if (clear_node)
  551. clear_bit(node, dlm->domain_map);
  552. }
  553. spin_unlock(&dlm->spinlock);
  554. }
  555. int dlm_joined(struct dlm_ctxt *dlm)
  556. {
  557. int ret = 0;
  558. spin_lock(&dlm_domain_lock);
  559. if (dlm->dlm_state == DLM_CTXT_JOINED)
  560. ret = 1;
  561. spin_unlock(&dlm_domain_lock);
  562. return ret;
  563. }
  564. int dlm_shutting_down(struct dlm_ctxt *dlm)
  565. {
  566. int ret = 0;
  567. spin_lock(&dlm_domain_lock);
  568. if (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN)
  569. ret = 1;
  570. spin_unlock(&dlm_domain_lock);
  571. return ret;
  572. }
  573. void dlm_unregister_domain(struct dlm_ctxt *dlm)
  574. {
  575. int leave = 0;
  576. struct dlm_lock_resource *res;
  577. spin_lock(&dlm_domain_lock);
  578. BUG_ON(dlm->dlm_state != DLM_CTXT_JOINED);
  579. BUG_ON(!dlm->num_joins);
  580. dlm->num_joins--;
  581. if (!dlm->num_joins) {
  582. /* We mark it "in shutdown" now so new register
  583. * requests wait until we've completely left the
  584. * domain. Don't use DLM_CTXT_LEAVING yet as we still
  585. * want new domain joins to communicate with us at
  586. * least until we've completed migration of our
  587. * resources. */
  588. dlm->dlm_state = DLM_CTXT_IN_SHUTDOWN;
  589. leave = 1;
  590. }
  591. spin_unlock(&dlm_domain_lock);
  592. if (leave) {
  593. mlog(0, "shutting down domain %s\n", dlm->name);
  594. dlm_begin_exit_domain(dlm);
  595. /* We changed dlm state, notify the thread */
  596. dlm_kick_thread(dlm, NULL);
  597. while (dlm_migrate_all_locks(dlm)) {
  598. /* Give dlm_thread time to purge the lockres' */
  599. msleep(500);
  600. mlog(0, "%s: more migration to do\n", dlm->name);
  601. }
  602. /* This list should be empty. If not, print remaining lockres */
  603. if (!list_empty(&dlm->tracking_list)) {
  604. mlog(ML_ERROR, "Following lockres' are still on the "
  605. "tracking list:\n");
  606. list_for_each_entry(res, &dlm->tracking_list, tracking)
  607. dlm_print_one_lock_resource(res);
  608. }
  609. dlm_mark_domain_leaving(dlm);
  610. dlm_leave_domain(dlm);
  611. printk(KERN_NOTICE "o2dlm: Leaving domain %s\n", dlm->name);
  612. dlm_force_free_mles(dlm);
  613. dlm_complete_dlm_shutdown(dlm);
  614. }
  615. dlm_put(dlm);
  616. }
  617. EXPORT_SYMBOL_GPL(dlm_unregister_domain);
  618. static int dlm_query_join_proto_check(char *proto_type, int node,
  619. struct dlm_protocol_version *ours,
  620. struct dlm_protocol_version *request)
  621. {
  622. int rc;
  623. struct dlm_protocol_version proto = *request;
  624. if (!dlm_protocol_compare(ours, &proto)) {
  625. mlog(0,
  626. "node %u wanted to join with %s locking protocol "
  627. "%u.%u, we respond with %u.%u\n",
  628. node, proto_type,
  629. request->pv_major,
  630. request->pv_minor,
  631. proto.pv_major, proto.pv_minor);
  632. request->pv_minor = proto.pv_minor;
  633. rc = 0;
  634. } else {
  635. mlog(ML_NOTICE,
  636. "Node %u wanted to join with %s locking "
  637. "protocol %u.%u, but we have %u.%u, disallowing\n",
  638. node, proto_type,
  639. request->pv_major,
  640. request->pv_minor,
  641. ours->pv_major,
  642. ours->pv_minor);
  643. rc = 1;
  644. }
  645. return rc;
  646. }
  647. /*
  648. * struct dlm_query_join_packet is made up of four one-byte fields. They
  649. * are effectively in big-endian order already. However, little-endian
  650. * machines swap them before putting the packet on the wire (because
  651. * query_join's response is a status, and that status is treated as a u32
  652. * on the wire). Thus, a big-endian and little-endian machines will treat
  653. * this structure differently.
  654. *
  655. * The solution is to have little-endian machines swap the structure when
  656. * converting from the structure to the u32 representation. This will
  657. * result in the structure having the correct format on the wire no matter
  658. * the host endian format.
  659. */
  660. static void dlm_query_join_packet_to_wire(struct dlm_query_join_packet *packet,
  661. u32 *wire)
  662. {
  663. union dlm_query_join_response response;
  664. response.packet = *packet;
  665. *wire = cpu_to_be32(response.intval);
  666. }
  667. static void dlm_query_join_wire_to_packet(u32 wire,
  668. struct dlm_query_join_packet *packet)
  669. {
  670. union dlm_query_join_response response;
  671. response.intval = cpu_to_be32(wire);
  672. *packet = response.packet;
  673. }
  674. static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data,
  675. void **ret_data)
  676. {
  677. struct dlm_query_join_request *query;
  678. struct dlm_query_join_packet packet = {
  679. .code = JOIN_DISALLOW,
  680. };
  681. struct dlm_ctxt *dlm = NULL;
  682. u32 response;
  683. u8 nodenum;
  684. query = (struct dlm_query_join_request *) msg->buf;
  685. mlog(0, "node %u wants to join domain %s\n", query->node_idx,
  686. query->domain);
  687. /*
  688. * If heartbeat doesn't consider the node live, tell it
  689. * to back off and try again. This gives heartbeat a chance
  690. * to catch up.
  691. */
  692. if (!o2hb_check_node_heartbeating(query->node_idx)) {
  693. mlog(0, "node %u is not in our live map yet\n",
  694. query->node_idx);
  695. packet.code = JOIN_DISALLOW;
  696. goto respond;
  697. }
  698. packet.code = JOIN_OK_NO_MAP;
  699. spin_lock(&dlm_domain_lock);
  700. dlm = __dlm_lookup_domain_full(query->domain, query->name_len);
  701. if (!dlm)
  702. goto unlock_respond;
  703. /*
  704. * There is a small window where the joining node may not see the
  705. * node(s) that just left but still part of the cluster. DISALLOW
  706. * join request if joining node has different node map.
  707. */
  708. nodenum=0;
  709. while (nodenum < O2NM_MAX_NODES) {
  710. if (test_bit(nodenum, dlm->domain_map)) {
  711. if (!byte_test_bit(nodenum, query->node_map)) {
  712. mlog(0, "disallow join as node %u does not "
  713. "have node %u in its nodemap\n",
  714. query->node_idx, nodenum);
  715. packet.code = JOIN_DISALLOW;
  716. goto unlock_respond;
  717. }
  718. }
  719. nodenum++;
  720. }
  721. /* Once the dlm ctxt is marked as leaving then we don't want
  722. * to be put in someone's domain map.
  723. * Also, explicitly disallow joining at certain troublesome
  724. * times (ie. during recovery). */
  725. if (dlm && dlm->dlm_state != DLM_CTXT_LEAVING) {
  726. int bit = query->node_idx;
  727. spin_lock(&dlm->spinlock);
  728. if (dlm->dlm_state == DLM_CTXT_NEW &&
  729. dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN) {
  730. /*If this is a brand new context and we
  731. * haven't started our join process yet, then
  732. * the other node won the race. */
  733. packet.code = JOIN_OK_NO_MAP;
  734. } else if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) {
  735. /* Disallow parallel joins. */
  736. packet.code = JOIN_DISALLOW;
  737. } else if (dlm->reco.state & DLM_RECO_STATE_ACTIVE) {
  738. mlog(0, "node %u trying to join, but recovery "
  739. "is ongoing.\n", bit);
  740. packet.code = JOIN_DISALLOW;
  741. } else if (test_bit(bit, dlm->recovery_map)) {
  742. mlog(0, "node %u trying to join, but it "
  743. "still needs recovery.\n", bit);
  744. packet.code = JOIN_DISALLOW;
  745. } else if (test_bit(bit, dlm->domain_map)) {
  746. mlog(0, "node %u trying to join, but it "
  747. "is still in the domain! needs recovery?\n",
  748. bit);
  749. packet.code = JOIN_DISALLOW;
  750. } else {
  751. /* Alright we're fully a part of this domain
  752. * so we keep some state as to who's joining
  753. * and indicate to him that needs to be fixed
  754. * up. */
  755. /* Make sure we speak compatible locking protocols. */
  756. if (dlm_query_join_proto_check("DLM", bit,
  757. &dlm->dlm_locking_proto,
  758. &query->dlm_proto)) {
  759. packet.code = JOIN_PROTOCOL_MISMATCH;
  760. } else if (dlm_query_join_proto_check("fs", bit,
  761. &dlm->fs_locking_proto,
  762. &query->fs_proto)) {
  763. packet.code = JOIN_PROTOCOL_MISMATCH;
  764. } else {
  765. packet.dlm_minor = query->dlm_proto.pv_minor;
  766. packet.fs_minor = query->fs_proto.pv_minor;
  767. packet.code = JOIN_OK;
  768. __dlm_set_joining_node(dlm, query->node_idx);
  769. }
  770. }
  771. spin_unlock(&dlm->spinlock);
  772. }
  773. unlock_respond:
  774. spin_unlock(&dlm_domain_lock);
  775. respond:
  776. mlog(0, "We respond with %u\n", packet.code);
  777. dlm_query_join_packet_to_wire(&packet, &response);
  778. return response;
  779. }
  780. static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data,
  781. void **ret_data)
  782. {
  783. struct dlm_assert_joined *assert;
  784. struct dlm_ctxt *dlm = NULL;
  785. assert = (struct dlm_assert_joined *) msg->buf;
  786. mlog(0, "node %u asserts join on domain %s\n", assert->node_idx,
  787. assert->domain);
  788. spin_lock(&dlm_domain_lock);
  789. dlm = __dlm_lookup_domain_full(assert->domain, assert->name_len);
  790. /* XXX should we consider no dlm ctxt an error? */
  791. if (dlm) {
  792. spin_lock(&dlm->spinlock);
  793. /* Alright, this node has officially joined our
  794. * domain. Set him in the map and clean up our
  795. * leftover join state. */
  796. BUG_ON(dlm->joining_node != assert->node_idx);
  797. set_bit(assert->node_idx, dlm->domain_map);
  798. clear_bit(assert->node_idx, dlm->exit_domain_map);
  799. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  800. printk(KERN_NOTICE "o2dlm: Node %u joins domain %s ",
  801. assert->node_idx, dlm->name);
  802. __dlm_print_nodes(dlm);
  803. /* notify anything attached to the heartbeat events */
  804. dlm_hb_event_notify_attached(dlm, assert->node_idx, 1);
  805. spin_unlock(&dlm->spinlock);
  806. }
  807. spin_unlock(&dlm_domain_lock);
  808. return 0;
  809. }
  810. static int dlm_match_regions(struct dlm_ctxt *dlm,
  811. struct dlm_query_region *qr,
  812. char *local, int locallen)
  813. {
  814. char *remote = qr->qr_regions;
  815. char *l, *r;
  816. int localnr, i, j, foundit;
  817. int status = 0;
  818. if (!o2hb_global_heartbeat_active()) {
  819. if (qr->qr_numregions) {
  820. mlog(ML_ERROR, "Domain %s: Joining node %d has global "
  821. "heartbeat enabled but local node %d does not\n",
  822. qr->qr_domain, qr->qr_node, dlm->node_num);
  823. status = -EINVAL;
  824. }
  825. goto bail;
  826. }
  827. if (o2hb_global_heartbeat_active() && !qr->qr_numregions) {
  828. mlog(ML_ERROR, "Domain %s: Local node %d has global "
  829. "heartbeat enabled but joining node %d does not\n",
  830. qr->qr_domain, dlm->node_num, qr->qr_node);
  831. status = -EINVAL;
  832. goto bail;
  833. }
  834. r = remote;
  835. for (i = 0; i < qr->qr_numregions; ++i) {
  836. mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, r);
  837. r += O2HB_MAX_REGION_NAME_LEN;
  838. }
  839. localnr = min(O2NM_MAX_REGIONS, locallen/O2HB_MAX_REGION_NAME_LEN);
  840. localnr = o2hb_get_all_regions(local, (u8)localnr);
  841. /* compare local regions with remote */
  842. l = local;
  843. for (i = 0; i < localnr; ++i) {
  844. foundit = 0;
  845. r = remote;
  846. for (j = 0; j <= qr->qr_numregions; ++j) {
  847. if (!memcmp(l, r, O2HB_MAX_REGION_NAME_LEN)) {
  848. foundit = 1;
  849. break;
  850. }
  851. r += O2HB_MAX_REGION_NAME_LEN;
  852. }
  853. if (!foundit) {
  854. status = -EINVAL;
  855. mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
  856. "in local node %d but not in joining node %d\n",
  857. qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, l,
  858. dlm->node_num, qr->qr_node);
  859. goto bail;
  860. }
  861. l += O2HB_MAX_REGION_NAME_LEN;
  862. }
  863. /* compare remote with local regions */
  864. r = remote;
  865. for (i = 0; i < qr->qr_numregions; ++i) {
  866. foundit = 0;
  867. l = local;
  868. for (j = 0; j < localnr; ++j) {
  869. if (!memcmp(r, l, O2HB_MAX_REGION_NAME_LEN)) {
  870. foundit = 1;
  871. break;
  872. }
  873. l += O2HB_MAX_REGION_NAME_LEN;
  874. }
  875. if (!foundit) {
  876. status = -EINVAL;
  877. mlog(ML_ERROR, "Domain %s: Region '%.*s' registered "
  878. "in joining node %d but not in local node %d\n",
  879. qr->qr_domain, O2HB_MAX_REGION_NAME_LEN, r,
  880. qr->qr_node, dlm->node_num);
  881. goto bail;
  882. }
  883. r += O2HB_MAX_REGION_NAME_LEN;
  884. }
  885. bail:
  886. return status;
  887. }
  888. static int dlm_send_regions(struct dlm_ctxt *dlm, unsigned long *node_map)
  889. {
  890. struct dlm_query_region *qr = NULL;
  891. int status, ret = 0, i;
  892. char *p;
  893. if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
  894. goto bail;
  895. qr = kzalloc(sizeof(struct dlm_query_region), GFP_KERNEL);
  896. if (!qr) {
  897. ret = -ENOMEM;
  898. mlog_errno(ret);
  899. goto bail;
  900. }
  901. qr->qr_node = dlm->node_num;
  902. qr->qr_namelen = strlen(dlm->name);
  903. memcpy(qr->qr_domain, dlm->name, qr->qr_namelen);
  904. /* if local hb, the numregions will be zero */
  905. if (o2hb_global_heartbeat_active())
  906. qr->qr_numregions = o2hb_get_all_regions(qr->qr_regions,
  907. O2NM_MAX_REGIONS);
  908. p = qr->qr_regions;
  909. for (i = 0; i < qr->qr_numregions; ++i, p += O2HB_MAX_REGION_NAME_LEN)
  910. mlog(0, "Region %.*s\n", O2HB_MAX_REGION_NAME_LEN, p);
  911. i = -1;
  912. while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
  913. i + 1)) < O2NM_MAX_NODES) {
  914. if (i == dlm->node_num)
  915. continue;
  916. mlog(0, "Sending regions to node %d\n", i);
  917. ret = o2net_send_message(DLM_QUERY_REGION, DLM_MOD_KEY, qr,
  918. sizeof(struct dlm_query_region),
  919. i, &status);
  920. if (ret >= 0)
  921. ret = status;
  922. if (ret) {
  923. mlog(ML_ERROR, "Region mismatch %d, node %d\n",
  924. ret, i);
  925. break;
  926. }
  927. }
  928. bail:
  929. kfree(qr);
  930. return ret;
  931. }
  932. static int dlm_query_region_handler(struct o2net_msg *msg, u32 len,
  933. void *data, void **ret_data)
  934. {
  935. struct dlm_query_region *qr;
  936. struct dlm_ctxt *dlm = NULL;
  937. char *local = NULL;
  938. int status = 0;
  939. int locked = 0;
  940. qr = (struct dlm_query_region *) msg->buf;
  941. mlog(0, "Node %u queries hb regions on domain %s\n", qr->qr_node,
  942. qr->qr_domain);
  943. /* buffer used in dlm_mast_regions() */
  944. local = kmalloc(sizeof(qr->qr_regions), GFP_KERNEL);
  945. if (!local) {
  946. status = -ENOMEM;
  947. goto bail;
  948. }
  949. status = -EINVAL;
  950. spin_lock(&dlm_domain_lock);
  951. dlm = __dlm_lookup_domain_full(qr->qr_domain, qr->qr_namelen);
  952. if (!dlm) {
  953. mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
  954. "before join domain\n", qr->qr_node, qr->qr_domain);
  955. goto bail;
  956. }
  957. spin_lock(&dlm->spinlock);
  958. locked = 1;
  959. if (dlm->joining_node != qr->qr_node) {
  960. mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
  961. "but joining node is %d\n", qr->qr_node, qr->qr_domain,
  962. dlm->joining_node);
  963. goto bail;
  964. }
  965. /* Support for global heartbeat was added in 1.1 */
  966. if (dlm->dlm_locking_proto.pv_major == 1 &&
  967. dlm->dlm_locking_proto.pv_minor == 0) {
  968. mlog(ML_ERROR, "Node %d queried hb regions on domain %s "
  969. "but active dlm protocol is %d.%d\n", qr->qr_node,
  970. qr->qr_domain, dlm->dlm_locking_proto.pv_major,
  971. dlm->dlm_locking_proto.pv_minor);
  972. goto bail;
  973. }
  974. status = dlm_match_regions(dlm, qr, local, sizeof(qr->qr_regions));
  975. bail:
  976. if (locked)
  977. spin_unlock(&dlm->spinlock);
  978. spin_unlock(&dlm_domain_lock);
  979. kfree(local);
  980. return status;
  981. }
  982. static int dlm_match_nodes(struct dlm_ctxt *dlm, struct dlm_query_nodeinfo *qn)
  983. {
  984. struct o2nm_node *local;
  985. struct dlm_node_info *remote;
  986. int i, j;
  987. int status = 0;
  988. for (j = 0; j < qn->qn_numnodes; ++j)
  989. mlog(0, "Node %3d, %pI4:%u\n", qn->qn_nodes[j].ni_nodenum,
  990. &(qn->qn_nodes[j].ni_ipv4_address),
  991. ntohs(qn->qn_nodes[j].ni_ipv4_port));
  992. for (i = 0; i < O2NM_MAX_NODES && !status; ++i) {
  993. local = o2nm_get_node_by_num(i);
  994. remote = NULL;
  995. for (j = 0; j < qn->qn_numnodes; ++j) {
  996. if (qn->qn_nodes[j].ni_nodenum == i) {
  997. remote = &(qn->qn_nodes[j]);
  998. break;
  999. }
  1000. }
  1001. if (!local && !remote)
  1002. continue;
  1003. if ((local && !remote) || (!local && remote))
  1004. status = -EINVAL;
  1005. if (!status &&
  1006. ((remote->ni_nodenum != local->nd_num) ||
  1007. (remote->ni_ipv4_port != local->nd_ipv4_port) ||
  1008. (remote->ni_ipv4_address != local->nd_ipv4_address)))
  1009. status = -EINVAL;
  1010. if (status) {
  1011. if (remote && !local)
  1012. mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
  1013. "registered in joining node %d but not in "
  1014. "local node %d\n", qn->qn_domain,
  1015. remote->ni_nodenum,
  1016. &(remote->ni_ipv4_address),
  1017. ntohs(remote->ni_ipv4_port),
  1018. qn->qn_nodenum, dlm->node_num);
  1019. if (local && !remote)
  1020. mlog(ML_ERROR, "Domain %s: Node %d (%pI4:%u) "
  1021. "registered in local node %d but not in "
  1022. "joining node %d\n", qn->qn_domain,
  1023. local->nd_num, &(local->nd_ipv4_address),
  1024. ntohs(local->nd_ipv4_port),
  1025. dlm->node_num, qn->qn_nodenum);
  1026. BUG_ON((!local && !remote));
  1027. }
  1028. if (local)
  1029. o2nm_node_put(local);
  1030. }
  1031. return status;
  1032. }
  1033. static int dlm_send_nodeinfo(struct dlm_ctxt *dlm, unsigned long *node_map)
  1034. {
  1035. struct dlm_query_nodeinfo *qn = NULL;
  1036. struct o2nm_node *node;
  1037. int ret = 0, status, count, i;
  1038. if (find_next_bit(node_map, O2NM_MAX_NODES, 0) >= O2NM_MAX_NODES)
  1039. goto bail;
  1040. qn = kzalloc(sizeof(struct dlm_query_nodeinfo), GFP_KERNEL);
  1041. if (!qn) {
  1042. ret = -ENOMEM;
  1043. mlog_errno(ret);
  1044. goto bail;
  1045. }
  1046. for (i = 0, count = 0; i < O2NM_MAX_NODES; ++i) {
  1047. node = o2nm_get_node_by_num(i);
  1048. if (!node)
  1049. continue;
  1050. qn->qn_nodes[count].ni_nodenum = node->nd_num;
  1051. qn->qn_nodes[count].ni_ipv4_port = node->nd_ipv4_port;
  1052. qn->qn_nodes[count].ni_ipv4_address = node->nd_ipv4_address;
  1053. mlog(0, "Node %3d, %pI4:%u\n", node->nd_num,
  1054. &(node->nd_ipv4_address), ntohs(node->nd_ipv4_port));
  1055. ++count;
  1056. o2nm_node_put(node);
  1057. }
  1058. qn->qn_nodenum = dlm->node_num;
  1059. qn->qn_numnodes = count;
  1060. qn->qn_namelen = strlen(dlm->name);
  1061. memcpy(qn->qn_domain, dlm->name, qn->qn_namelen);
  1062. i = -1;
  1063. while ((i = find_next_bit(node_map, O2NM_MAX_NODES,
  1064. i + 1)) < O2NM_MAX_NODES) {
  1065. if (i == dlm->node_num)
  1066. continue;
  1067. mlog(0, "Sending nodeinfo to node %d\n", i);
  1068. ret = o2net_send_message(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
  1069. qn, sizeof(struct dlm_query_nodeinfo),
  1070. i, &status);
  1071. if (ret >= 0)
  1072. ret = status;
  1073. if (ret) {
  1074. mlog(ML_ERROR, "node mismatch %d, node %d\n", ret, i);
  1075. break;
  1076. }
  1077. }
  1078. bail:
  1079. kfree(qn);
  1080. return ret;
  1081. }
  1082. static int dlm_query_nodeinfo_handler(struct o2net_msg *msg, u32 len,
  1083. void *data, void **ret_data)
  1084. {
  1085. struct dlm_query_nodeinfo *qn;
  1086. struct dlm_ctxt *dlm = NULL;
  1087. int locked = 0, status = -EINVAL;
  1088. qn = (struct dlm_query_nodeinfo *) msg->buf;
  1089. mlog(0, "Node %u queries nodes on domain %s\n", qn->qn_nodenum,
  1090. qn->qn_domain);
  1091. spin_lock(&dlm_domain_lock);
  1092. dlm = __dlm_lookup_domain_full(qn->qn_domain, qn->qn_namelen);
  1093. if (!dlm) {
  1094. mlog(ML_ERROR, "Node %d queried nodes on domain %s before "
  1095. "join domain\n", qn->qn_nodenum, qn->qn_domain);
  1096. goto bail;
  1097. }
  1098. spin_lock(&dlm->spinlock);
  1099. locked = 1;
  1100. if (dlm->joining_node != qn->qn_nodenum) {
  1101. mlog(ML_ERROR, "Node %d queried nodes on domain %s but "
  1102. "joining node is %d\n", qn->qn_nodenum, qn->qn_domain,
  1103. dlm->joining_node);
  1104. goto bail;
  1105. }
  1106. /* Support for node query was added in 1.1 */
  1107. if (dlm->dlm_locking_proto.pv_major == 1 &&
  1108. dlm->dlm_locking_proto.pv_minor == 0) {
  1109. mlog(ML_ERROR, "Node %d queried nodes on domain %s "
  1110. "but active dlm protocol is %d.%d\n", qn->qn_nodenum,
  1111. qn->qn_domain, dlm->dlm_locking_proto.pv_major,
  1112. dlm->dlm_locking_proto.pv_minor);
  1113. goto bail;
  1114. }
  1115. status = dlm_match_nodes(dlm, qn);
  1116. bail:
  1117. if (locked)
  1118. spin_unlock(&dlm->spinlock);
  1119. spin_unlock(&dlm_domain_lock);
  1120. return status;
  1121. }
  1122. static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data,
  1123. void **ret_data)
  1124. {
  1125. struct dlm_cancel_join *cancel;
  1126. struct dlm_ctxt *dlm = NULL;
  1127. cancel = (struct dlm_cancel_join *) msg->buf;
  1128. mlog(0, "node %u cancels join on domain %s\n", cancel->node_idx,
  1129. cancel->domain);
  1130. spin_lock(&dlm_domain_lock);
  1131. dlm = __dlm_lookup_domain_full(cancel->domain, cancel->name_len);
  1132. if (dlm) {
  1133. spin_lock(&dlm->spinlock);
  1134. /* Yikes, this guy wants to cancel his join. No
  1135. * problem, we simply cleanup our join state. */
  1136. BUG_ON(dlm->joining_node != cancel->node_idx);
  1137. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  1138. spin_unlock(&dlm->spinlock);
  1139. }
  1140. spin_unlock(&dlm_domain_lock);
  1141. return 0;
  1142. }
  1143. static int dlm_send_one_join_cancel(struct dlm_ctxt *dlm,
  1144. unsigned int node)
  1145. {
  1146. int status;
  1147. struct dlm_cancel_join cancel_msg;
  1148. memset(&cancel_msg, 0, sizeof(cancel_msg));
  1149. cancel_msg.node_idx = dlm->node_num;
  1150. cancel_msg.name_len = strlen(dlm->name);
  1151. memcpy(cancel_msg.domain, dlm->name, cancel_msg.name_len);
  1152. status = o2net_send_message(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1153. &cancel_msg, sizeof(cancel_msg), node,
  1154. NULL);
  1155. if (status < 0) {
  1156. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  1157. "node %u\n", status, DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1158. node);
  1159. goto bail;
  1160. }
  1161. bail:
  1162. return status;
  1163. }
  1164. /* map_size should be in bytes. */
  1165. static int dlm_send_join_cancels(struct dlm_ctxt *dlm,
  1166. unsigned long *node_map,
  1167. unsigned int map_size)
  1168. {
  1169. int status, tmpstat;
  1170. unsigned int node;
  1171. if (map_size != (BITS_TO_LONGS(O2NM_MAX_NODES) *
  1172. sizeof(unsigned long))) {
  1173. mlog(ML_ERROR,
  1174. "map_size %u != BITS_TO_LONGS(O2NM_MAX_NODES) %u\n",
  1175. map_size, (unsigned)BITS_TO_LONGS(O2NM_MAX_NODES));
  1176. return -EINVAL;
  1177. }
  1178. status = 0;
  1179. node = -1;
  1180. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  1181. node + 1)) < O2NM_MAX_NODES) {
  1182. if (node == dlm->node_num)
  1183. continue;
  1184. tmpstat = dlm_send_one_join_cancel(dlm, node);
  1185. if (tmpstat) {
  1186. mlog(ML_ERROR, "Error return %d cancelling join on "
  1187. "node %d\n", tmpstat, node);
  1188. if (!status)
  1189. status = tmpstat;
  1190. }
  1191. }
  1192. if (status)
  1193. mlog_errno(status);
  1194. return status;
  1195. }
  1196. static int dlm_request_join(struct dlm_ctxt *dlm,
  1197. int node,
  1198. enum dlm_query_join_response_code *response)
  1199. {
  1200. int status;
  1201. struct dlm_query_join_request join_msg;
  1202. struct dlm_query_join_packet packet;
  1203. u32 join_resp;
  1204. mlog(0, "querying node %d\n", node);
  1205. memset(&join_msg, 0, sizeof(join_msg));
  1206. join_msg.node_idx = dlm->node_num;
  1207. join_msg.name_len = strlen(dlm->name);
  1208. memcpy(join_msg.domain, dlm->name, join_msg.name_len);
  1209. join_msg.dlm_proto = dlm->dlm_locking_proto;
  1210. join_msg.fs_proto = dlm->fs_locking_proto;
  1211. /* copy live node map to join message */
  1212. byte_copymap(join_msg.node_map, dlm->live_nodes_map, O2NM_MAX_NODES);
  1213. status = o2net_send_message(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY, &join_msg,
  1214. sizeof(join_msg), node, &join_resp);
  1215. if (status < 0 && status != -ENOPROTOOPT) {
  1216. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  1217. "node %u\n", status, DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1218. node);
  1219. goto bail;
  1220. }
  1221. dlm_query_join_wire_to_packet(join_resp, &packet);
  1222. /* -ENOPROTOOPT from the net code means the other side isn't
  1223. listening for our message type -- that's fine, it means
  1224. his dlm isn't up, so we can consider him a 'yes' but not
  1225. joined into the domain. */
  1226. if (status == -ENOPROTOOPT) {
  1227. status = 0;
  1228. *response = JOIN_OK_NO_MAP;
  1229. } else if (packet.code == JOIN_DISALLOW ||
  1230. packet.code == JOIN_OK_NO_MAP) {
  1231. *response = packet.code;
  1232. } else if (packet.code == JOIN_PROTOCOL_MISMATCH) {
  1233. mlog(ML_NOTICE,
  1234. "This node requested DLM locking protocol %u.%u and "
  1235. "filesystem locking protocol %u.%u. At least one of "
  1236. "the protocol versions on node %d is not compatible, "
  1237. "disconnecting\n",
  1238. dlm->dlm_locking_proto.pv_major,
  1239. dlm->dlm_locking_proto.pv_minor,
  1240. dlm->fs_locking_proto.pv_major,
  1241. dlm->fs_locking_proto.pv_minor,
  1242. node);
  1243. status = -EPROTO;
  1244. *response = packet.code;
  1245. } else if (packet.code == JOIN_OK) {
  1246. *response = packet.code;
  1247. /* Use the same locking protocol as the remote node */
  1248. dlm->dlm_locking_proto.pv_minor = packet.dlm_minor;
  1249. dlm->fs_locking_proto.pv_minor = packet.fs_minor;
  1250. mlog(0,
  1251. "Node %d responds JOIN_OK with DLM locking protocol "
  1252. "%u.%u and fs locking protocol %u.%u\n",
  1253. node,
  1254. dlm->dlm_locking_proto.pv_major,
  1255. dlm->dlm_locking_proto.pv_minor,
  1256. dlm->fs_locking_proto.pv_major,
  1257. dlm->fs_locking_proto.pv_minor);
  1258. } else {
  1259. status = -EINVAL;
  1260. mlog(ML_ERROR, "invalid response %d from node %u\n",
  1261. packet.code, node);
  1262. }
  1263. mlog(0, "status %d, node %d response is %d\n", status, node,
  1264. *response);
  1265. bail:
  1266. return status;
  1267. }
  1268. static int dlm_send_one_join_assert(struct dlm_ctxt *dlm,
  1269. unsigned int node)
  1270. {
  1271. int status;
  1272. struct dlm_assert_joined assert_msg;
  1273. mlog(0, "Sending join assert to node %u\n", node);
  1274. memset(&assert_msg, 0, sizeof(assert_msg));
  1275. assert_msg.node_idx = dlm->node_num;
  1276. assert_msg.name_len = strlen(dlm->name);
  1277. memcpy(assert_msg.domain, dlm->name, assert_msg.name_len);
  1278. status = o2net_send_message(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1279. &assert_msg, sizeof(assert_msg), node,
  1280. NULL);
  1281. if (status < 0)
  1282. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  1283. "node %u\n", status, DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1284. node);
  1285. return status;
  1286. }
  1287. static void dlm_send_join_asserts(struct dlm_ctxt *dlm,
  1288. unsigned long *node_map)
  1289. {
  1290. int status, node, live;
  1291. status = 0;
  1292. node = -1;
  1293. while ((node = find_next_bit(node_map, O2NM_MAX_NODES,
  1294. node + 1)) < O2NM_MAX_NODES) {
  1295. if (node == dlm->node_num)
  1296. continue;
  1297. do {
  1298. /* It is very important that this message be
  1299. * received so we spin until either the node
  1300. * has died or it gets the message. */
  1301. status = dlm_send_one_join_assert(dlm, node);
  1302. spin_lock(&dlm->spinlock);
  1303. live = test_bit(node, dlm->live_nodes_map);
  1304. spin_unlock(&dlm->spinlock);
  1305. if (status) {
  1306. mlog(ML_ERROR, "Error return %d asserting "
  1307. "join on node %d\n", status, node);
  1308. /* give us some time between errors... */
  1309. if (live)
  1310. msleep(DLM_DOMAIN_BACKOFF_MS);
  1311. }
  1312. } while (status && live);
  1313. }
  1314. }
  1315. struct domain_join_ctxt {
  1316. unsigned long live_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1317. unsigned long yes_resp_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1318. };
  1319. static int dlm_should_restart_join(struct dlm_ctxt *dlm,
  1320. struct domain_join_ctxt *ctxt,
  1321. enum dlm_query_join_response_code response)
  1322. {
  1323. int ret;
  1324. if (response == JOIN_DISALLOW) {
  1325. mlog(0, "Latest response of disallow -- should restart\n");
  1326. return 1;
  1327. }
  1328. spin_lock(&dlm->spinlock);
  1329. /* For now, we restart the process if the node maps have
  1330. * changed at all */
  1331. ret = memcmp(ctxt->live_map, dlm->live_nodes_map,
  1332. sizeof(dlm->live_nodes_map));
  1333. spin_unlock(&dlm->spinlock);
  1334. if (ret)
  1335. mlog(0, "Node maps changed -- should restart\n");
  1336. return ret;
  1337. }
  1338. static int dlm_try_to_join_domain(struct dlm_ctxt *dlm)
  1339. {
  1340. int status = 0, tmpstat, node;
  1341. struct domain_join_ctxt *ctxt;
  1342. enum dlm_query_join_response_code response = JOIN_DISALLOW;
  1343. mlog(0, "%p", dlm);
  1344. ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
  1345. if (!ctxt) {
  1346. status = -ENOMEM;
  1347. mlog_errno(status);
  1348. goto bail;
  1349. }
  1350. /* group sem locking should work for us here -- we're already
  1351. * registered for heartbeat events so filling this should be
  1352. * atomic wrt getting those handlers called. */
  1353. o2hb_fill_node_map(dlm->live_nodes_map, sizeof(dlm->live_nodes_map));
  1354. spin_lock(&dlm->spinlock);
  1355. memcpy(ctxt->live_map, dlm->live_nodes_map, sizeof(ctxt->live_map));
  1356. __dlm_set_joining_node(dlm, dlm->node_num);
  1357. spin_unlock(&dlm->spinlock);
  1358. node = -1;
  1359. while ((node = find_next_bit(ctxt->live_map, O2NM_MAX_NODES,
  1360. node + 1)) < O2NM_MAX_NODES) {
  1361. if (node == dlm->node_num)
  1362. continue;
  1363. status = dlm_request_join(dlm, node, &response);
  1364. if (status < 0) {
  1365. mlog_errno(status);
  1366. goto bail;
  1367. }
  1368. /* Ok, either we got a response or the node doesn't have a
  1369. * dlm up. */
  1370. if (response == JOIN_OK)
  1371. set_bit(node, ctxt->yes_resp_map);
  1372. if (dlm_should_restart_join(dlm, ctxt, response)) {
  1373. status = -EAGAIN;
  1374. goto bail;
  1375. }
  1376. }
  1377. mlog(0, "Yay, done querying nodes!\n");
  1378. /* Yay, everyone agree's we can join the domain. My domain is
  1379. * comprised of all nodes who were put in the
  1380. * yes_resp_map. Copy that into our domain map and send a join
  1381. * assert message to clean up everyone elses state. */
  1382. spin_lock(&dlm->spinlock);
  1383. memcpy(dlm->domain_map, ctxt->yes_resp_map,
  1384. sizeof(ctxt->yes_resp_map));
  1385. set_bit(dlm->node_num, dlm->domain_map);
  1386. spin_unlock(&dlm->spinlock);
  1387. /* Support for global heartbeat and node info was added in 1.1 */
  1388. if (dlm->dlm_locking_proto.pv_major > 1 ||
  1389. dlm->dlm_locking_proto.pv_minor > 0) {
  1390. status = dlm_send_nodeinfo(dlm, ctxt->yes_resp_map);
  1391. if (status) {
  1392. mlog_errno(status);
  1393. goto bail;
  1394. }
  1395. status = dlm_send_regions(dlm, ctxt->yes_resp_map);
  1396. if (status) {
  1397. mlog_errno(status);
  1398. goto bail;
  1399. }
  1400. }
  1401. dlm_send_join_asserts(dlm, ctxt->yes_resp_map);
  1402. /* Joined state *must* be set before the joining node
  1403. * information, otherwise the query_join handler may read no
  1404. * current joiner but a state of NEW and tell joining nodes
  1405. * we're not in the domain. */
  1406. spin_lock(&dlm_domain_lock);
  1407. dlm->dlm_state = DLM_CTXT_JOINED;
  1408. dlm->num_joins++;
  1409. spin_unlock(&dlm_domain_lock);
  1410. bail:
  1411. spin_lock(&dlm->spinlock);
  1412. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  1413. if (!status) {
  1414. printk(KERN_NOTICE "o2dlm: Joining domain %s ", dlm->name);
  1415. __dlm_print_nodes(dlm);
  1416. }
  1417. spin_unlock(&dlm->spinlock);
  1418. if (ctxt) {
  1419. /* Do we need to send a cancel message to any nodes? */
  1420. if (status < 0) {
  1421. tmpstat = dlm_send_join_cancels(dlm,
  1422. ctxt->yes_resp_map,
  1423. sizeof(ctxt->yes_resp_map));
  1424. if (tmpstat < 0)
  1425. mlog_errno(tmpstat);
  1426. }
  1427. kfree(ctxt);
  1428. }
  1429. mlog(0, "returning %d\n", status);
  1430. return status;
  1431. }
  1432. static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm)
  1433. {
  1434. o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_up);
  1435. o2hb_unregister_callback(dlm->name, &dlm->dlm_hb_down);
  1436. o2net_unregister_handler_list(&dlm->dlm_domain_handlers);
  1437. }
  1438. static int dlm_register_domain_handlers(struct dlm_ctxt *dlm)
  1439. {
  1440. int status;
  1441. mlog(0, "registering handlers.\n");
  1442. o2hb_setup_callback(&dlm->dlm_hb_down, O2HB_NODE_DOWN_CB,
  1443. dlm_hb_node_down_cb, dlm, DLM_HB_NODE_DOWN_PRI);
  1444. status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_down);
  1445. if (status)
  1446. goto bail;
  1447. o2hb_setup_callback(&dlm->dlm_hb_up, O2HB_NODE_UP_CB,
  1448. dlm_hb_node_up_cb, dlm, DLM_HB_NODE_UP_PRI);
  1449. status = o2hb_register_callback(dlm->name, &dlm->dlm_hb_up);
  1450. if (status)
  1451. goto bail;
  1452. status = o2net_register_handler(DLM_MASTER_REQUEST_MSG, dlm->key,
  1453. sizeof(struct dlm_master_request),
  1454. dlm_master_request_handler,
  1455. dlm, NULL, &dlm->dlm_domain_handlers);
  1456. if (status)
  1457. goto bail;
  1458. status = o2net_register_handler(DLM_ASSERT_MASTER_MSG, dlm->key,
  1459. sizeof(struct dlm_assert_master),
  1460. dlm_assert_master_handler,
  1461. dlm, dlm_assert_master_post_handler,
  1462. &dlm->dlm_domain_handlers);
  1463. if (status)
  1464. goto bail;
  1465. status = o2net_register_handler(DLM_CREATE_LOCK_MSG, dlm->key,
  1466. sizeof(struct dlm_create_lock),
  1467. dlm_create_lock_handler,
  1468. dlm, NULL, &dlm->dlm_domain_handlers);
  1469. if (status)
  1470. goto bail;
  1471. status = o2net_register_handler(DLM_CONVERT_LOCK_MSG, dlm->key,
  1472. DLM_CONVERT_LOCK_MAX_LEN,
  1473. dlm_convert_lock_handler,
  1474. dlm, NULL, &dlm->dlm_domain_handlers);
  1475. if (status)
  1476. goto bail;
  1477. status = o2net_register_handler(DLM_UNLOCK_LOCK_MSG, dlm->key,
  1478. DLM_UNLOCK_LOCK_MAX_LEN,
  1479. dlm_unlock_lock_handler,
  1480. dlm, NULL, &dlm->dlm_domain_handlers);
  1481. if (status)
  1482. goto bail;
  1483. status = o2net_register_handler(DLM_PROXY_AST_MSG, dlm->key,
  1484. DLM_PROXY_AST_MAX_LEN,
  1485. dlm_proxy_ast_handler,
  1486. dlm, NULL, &dlm->dlm_domain_handlers);
  1487. if (status)
  1488. goto bail;
  1489. status = o2net_register_handler(DLM_EXIT_DOMAIN_MSG, dlm->key,
  1490. sizeof(struct dlm_exit_domain),
  1491. dlm_exit_domain_handler,
  1492. dlm, NULL, &dlm->dlm_domain_handlers);
  1493. if (status)
  1494. goto bail;
  1495. status = o2net_register_handler(DLM_DEREF_LOCKRES_MSG, dlm->key,
  1496. sizeof(struct dlm_deref_lockres),
  1497. dlm_deref_lockres_handler,
  1498. dlm, NULL, &dlm->dlm_domain_handlers);
  1499. if (status)
  1500. goto bail;
  1501. status = o2net_register_handler(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  1502. sizeof(struct dlm_migrate_request),
  1503. dlm_migrate_request_handler,
  1504. dlm, NULL, &dlm->dlm_domain_handlers);
  1505. if (status)
  1506. goto bail;
  1507. status = o2net_register_handler(DLM_MIG_LOCKRES_MSG, dlm->key,
  1508. DLM_MIG_LOCKRES_MAX_LEN,
  1509. dlm_mig_lockres_handler,
  1510. dlm, NULL, &dlm->dlm_domain_handlers);
  1511. if (status)
  1512. goto bail;
  1513. status = o2net_register_handler(DLM_MASTER_REQUERY_MSG, dlm->key,
  1514. sizeof(struct dlm_master_requery),
  1515. dlm_master_requery_handler,
  1516. dlm, NULL, &dlm->dlm_domain_handlers);
  1517. if (status)
  1518. goto bail;
  1519. status = o2net_register_handler(DLM_LOCK_REQUEST_MSG, dlm->key,
  1520. sizeof(struct dlm_lock_request),
  1521. dlm_request_all_locks_handler,
  1522. dlm, NULL, &dlm->dlm_domain_handlers);
  1523. if (status)
  1524. goto bail;
  1525. status = o2net_register_handler(DLM_RECO_DATA_DONE_MSG, dlm->key,
  1526. sizeof(struct dlm_reco_data_done),
  1527. dlm_reco_data_done_handler,
  1528. dlm, NULL, &dlm->dlm_domain_handlers);
  1529. if (status)
  1530. goto bail;
  1531. status = o2net_register_handler(DLM_BEGIN_RECO_MSG, dlm->key,
  1532. sizeof(struct dlm_begin_reco),
  1533. dlm_begin_reco_handler,
  1534. dlm, NULL, &dlm->dlm_domain_handlers);
  1535. if (status)
  1536. goto bail;
  1537. status = o2net_register_handler(DLM_FINALIZE_RECO_MSG, dlm->key,
  1538. sizeof(struct dlm_finalize_reco),
  1539. dlm_finalize_reco_handler,
  1540. dlm, NULL, &dlm->dlm_domain_handlers);
  1541. if (status)
  1542. goto bail;
  1543. status = o2net_register_handler(DLM_BEGIN_EXIT_DOMAIN_MSG, dlm->key,
  1544. sizeof(struct dlm_exit_domain),
  1545. dlm_begin_exit_domain_handler,
  1546. dlm, NULL, &dlm->dlm_domain_handlers);
  1547. if (status)
  1548. goto bail;
  1549. bail:
  1550. if (status)
  1551. dlm_unregister_domain_handlers(dlm);
  1552. return status;
  1553. }
  1554. static int dlm_join_domain(struct dlm_ctxt *dlm)
  1555. {
  1556. int status;
  1557. unsigned int backoff;
  1558. unsigned int total_backoff = 0;
  1559. BUG_ON(!dlm);
  1560. mlog(0, "Join domain %s\n", dlm->name);
  1561. status = dlm_register_domain_handlers(dlm);
  1562. if (status) {
  1563. mlog_errno(status);
  1564. goto bail;
  1565. }
  1566. status = dlm_debug_init(dlm);
  1567. if (status < 0) {
  1568. mlog_errno(status);
  1569. goto bail;
  1570. }
  1571. status = dlm_launch_thread(dlm);
  1572. if (status < 0) {
  1573. mlog_errno(status);
  1574. goto bail;
  1575. }
  1576. status = dlm_launch_recovery_thread(dlm);
  1577. if (status < 0) {
  1578. mlog_errno(status);
  1579. goto bail;
  1580. }
  1581. dlm->dlm_worker = create_singlethread_workqueue("dlm_wq");
  1582. if (!dlm->dlm_worker) {
  1583. status = -ENOMEM;
  1584. mlog_errno(status);
  1585. goto bail;
  1586. }
  1587. do {
  1588. status = dlm_try_to_join_domain(dlm);
  1589. /* If we're racing another node to the join, then we
  1590. * need to back off temporarily and let them
  1591. * complete. */
  1592. #define DLM_JOIN_TIMEOUT_MSECS 90000
  1593. if (status == -EAGAIN) {
  1594. if (signal_pending(current)) {
  1595. status = -ERESTARTSYS;
  1596. goto bail;
  1597. }
  1598. if (total_backoff >
  1599. msecs_to_jiffies(DLM_JOIN_TIMEOUT_MSECS)) {
  1600. status = -ERESTARTSYS;
  1601. mlog(ML_NOTICE, "Timed out joining dlm domain "
  1602. "%s after %u msecs\n", dlm->name,
  1603. jiffies_to_msecs(total_backoff));
  1604. goto bail;
  1605. }
  1606. /*
  1607. * <chip> After you!
  1608. * <dale> No, after you!
  1609. * <chip> I insist!
  1610. * <dale> But you first!
  1611. * ...
  1612. */
  1613. backoff = (unsigned int)(jiffies & 0x3);
  1614. backoff *= DLM_DOMAIN_BACKOFF_MS;
  1615. total_backoff += backoff;
  1616. mlog(0, "backoff %d\n", backoff);
  1617. msleep(backoff);
  1618. }
  1619. } while (status == -EAGAIN);
  1620. if (status < 0) {
  1621. mlog_errno(status);
  1622. goto bail;
  1623. }
  1624. status = 0;
  1625. bail:
  1626. wake_up(&dlm_domain_events);
  1627. if (status) {
  1628. dlm_unregister_domain_handlers(dlm);
  1629. dlm_debug_shutdown(dlm);
  1630. dlm_complete_thread(dlm);
  1631. dlm_complete_recovery_thread(dlm);
  1632. dlm_destroy_dlm_worker(dlm);
  1633. }
  1634. return status;
  1635. }
  1636. static struct dlm_ctxt *dlm_alloc_ctxt(const char *domain,
  1637. u32 key)
  1638. {
  1639. int i;
  1640. int ret;
  1641. struct dlm_ctxt *dlm = NULL;
  1642. dlm = kzalloc(sizeof(*dlm), GFP_KERNEL);
  1643. if (!dlm) {
  1644. mlog_errno(-ENOMEM);
  1645. goto leave;
  1646. }
  1647. dlm->name = kstrdup(domain, GFP_KERNEL);
  1648. if (dlm->name == NULL) {
  1649. mlog_errno(-ENOMEM);
  1650. kfree(dlm);
  1651. dlm = NULL;
  1652. goto leave;
  1653. }
  1654. dlm->lockres_hash = (struct hlist_head **)dlm_alloc_pagevec(DLM_HASH_PAGES);
  1655. if (!dlm->lockres_hash) {
  1656. mlog_errno(-ENOMEM);
  1657. kfree(dlm->name);
  1658. kfree(dlm);
  1659. dlm = NULL;
  1660. goto leave;
  1661. }
  1662. for (i = 0; i < DLM_HASH_BUCKETS; i++)
  1663. INIT_HLIST_HEAD(dlm_lockres_hash(dlm, i));
  1664. dlm->master_hash = (struct hlist_head **)
  1665. dlm_alloc_pagevec(DLM_HASH_PAGES);
  1666. if (!dlm->master_hash) {
  1667. mlog_errno(-ENOMEM);
  1668. dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
  1669. kfree(dlm->name);
  1670. kfree(dlm);
  1671. dlm = NULL;
  1672. goto leave;
  1673. }
  1674. for (i = 0; i < DLM_HASH_BUCKETS; i++)
  1675. INIT_HLIST_HEAD(dlm_master_hash(dlm, i));
  1676. dlm->key = key;
  1677. dlm->node_num = o2nm_this_node();
  1678. ret = dlm_create_debugfs_subroot(dlm);
  1679. if (ret < 0) {
  1680. dlm_free_pagevec((void **)dlm->master_hash, DLM_HASH_PAGES);
  1681. dlm_free_pagevec((void **)dlm->lockres_hash, DLM_HASH_PAGES);
  1682. kfree(dlm->name);
  1683. kfree(dlm);
  1684. dlm = NULL;
  1685. goto leave;
  1686. }
  1687. spin_lock_init(&dlm->spinlock);
  1688. spin_lock_init(&dlm->master_lock);
  1689. spin_lock_init(&dlm->ast_lock);
  1690. spin_lock_init(&dlm->track_lock);
  1691. INIT_LIST_HEAD(&dlm->list);
  1692. INIT_LIST_HEAD(&dlm->dirty_list);
  1693. INIT_LIST_HEAD(&dlm->reco.resources);
  1694. INIT_LIST_HEAD(&dlm->reco.received);
  1695. INIT_LIST_HEAD(&dlm->reco.node_data);
  1696. INIT_LIST_HEAD(&dlm->purge_list);
  1697. INIT_LIST_HEAD(&dlm->dlm_domain_handlers);
  1698. INIT_LIST_HEAD(&dlm->tracking_list);
  1699. dlm->reco.state = 0;
  1700. INIT_LIST_HEAD(&dlm->pending_asts);
  1701. INIT_LIST_HEAD(&dlm->pending_basts);
  1702. mlog(0, "dlm->recovery_map=%p, &(dlm->recovery_map[0])=%p\n",
  1703. dlm->recovery_map, &(dlm->recovery_map[0]));
  1704. memset(dlm->recovery_map, 0, sizeof(dlm->recovery_map));
  1705. memset(dlm->live_nodes_map, 0, sizeof(dlm->live_nodes_map));
  1706. memset(dlm->domain_map, 0, sizeof(dlm->domain_map));
  1707. dlm->dlm_thread_task = NULL;
  1708. dlm->dlm_reco_thread_task = NULL;
  1709. dlm->dlm_worker = NULL;
  1710. init_waitqueue_head(&dlm->dlm_thread_wq);
  1711. init_waitqueue_head(&dlm->dlm_reco_thread_wq);
  1712. init_waitqueue_head(&dlm->reco.event);
  1713. init_waitqueue_head(&dlm->ast_wq);
  1714. init_waitqueue_head(&dlm->migration_wq);
  1715. INIT_LIST_HEAD(&dlm->mle_hb_events);
  1716. dlm->joining_node = DLM_LOCK_RES_OWNER_UNKNOWN;
  1717. init_waitqueue_head(&dlm->dlm_join_events);
  1718. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  1719. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  1720. atomic_set(&dlm->res_tot_count, 0);
  1721. atomic_set(&dlm->res_cur_count, 0);
  1722. for (i = 0; i < DLM_MLE_NUM_TYPES; ++i) {
  1723. atomic_set(&dlm->mle_tot_count[i], 0);
  1724. atomic_set(&dlm->mle_cur_count[i], 0);
  1725. }
  1726. spin_lock_init(&dlm->work_lock);
  1727. INIT_LIST_HEAD(&dlm->work_list);
  1728. INIT_WORK(&dlm->dispatched_work, dlm_dispatch_work);
  1729. kref_init(&dlm->dlm_refs);
  1730. dlm->dlm_state = DLM_CTXT_NEW;
  1731. INIT_LIST_HEAD(&dlm->dlm_eviction_callbacks);
  1732. mlog(0, "context init: refcount %u\n",
  1733. atomic_read(&dlm->dlm_refs.refcount));
  1734. leave:
  1735. return dlm;
  1736. }
  1737. /*
  1738. * Compare a requested locking protocol version against the current one.
  1739. *
  1740. * If the major numbers are different, they are incompatible.
  1741. * If the current minor is greater than the request, they are incompatible.
  1742. * If the current minor is less than or equal to the request, they are
  1743. * compatible, and the requester should run at the current minor version.
  1744. */
  1745. static int dlm_protocol_compare(struct dlm_protocol_version *existing,
  1746. struct dlm_protocol_version *request)
  1747. {
  1748. if (existing->pv_major != request->pv_major)
  1749. return 1;
  1750. if (existing->pv_minor > request->pv_minor)
  1751. return 1;
  1752. if (existing->pv_minor < request->pv_minor)
  1753. request->pv_minor = existing->pv_minor;
  1754. return 0;
  1755. }
  1756. /*
  1757. * dlm_register_domain: one-time setup per "domain".
  1758. *
  1759. * The filesystem passes in the requested locking version via proto.
  1760. * If registration was successful, proto will contain the negotiated
  1761. * locking protocol.
  1762. */
  1763. struct dlm_ctxt * dlm_register_domain(const char *domain,
  1764. u32 key,
  1765. struct dlm_protocol_version *fs_proto)
  1766. {
  1767. int ret;
  1768. struct dlm_ctxt *dlm = NULL;
  1769. struct dlm_ctxt *new_ctxt = NULL;
  1770. if (strlen(domain) >= O2NM_MAX_NAME_LEN) {
  1771. ret = -ENAMETOOLONG;
  1772. mlog(ML_ERROR, "domain name length too long\n");
  1773. goto leave;
  1774. }
  1775. if (!o2hb_check_local_node_heartbeating()) {
  1776. mlog(ML_ERROR, "the local node has not been configured, or is "
  1777. "not heartbeating\n");
  1778. ret = -EPROTO;
  1779. goto leave;
  1780. }
  1781. mlog(0, "register called for domain \"%s\"\n", domain);
  1782. retry:
  1783. dlm = NULL;
  1784. if (signal_pending(current)) {
  1785. ret = -ERESTARTSYS;
  1786. mlog_errno(ret);
  1787. goto leave;
  1788. }
  1789. spin_lock(&dlm_domain_lock);
  1790. dlm = __dlm_lookup_domain(domain);
  1791. if (dlm) {
  1792. if (dlm->dlm_state != DLM_CTXT_JOINED) {
  1793. spin_unlock(&dlm_domain_lock);
  1794. mlog(0, "This ctxt is not joined yet!\n");
  1795. wait_event_interruptible(dlm_domain_events,
  1796. dlm_wait_on_domain_helper(
  1797. domain));
  1798. goto retry;
  1799. }
  1800. if (dlm_protocol_compare(&dlm->fs_locking_proto, fs_proto)) {
  1801. spin_unlock(&dlm_domain_lock);
  1802. mlog(ML_ERROR,
  1803. "Requested locking protocol version is not "
  1804. "compatible with already registered domain "
  1805. "\"%s\"\n", domain);
  1806. ret = -EPROTO;
  1807. goto leave;
  1808. }
  1809. __dlm_get(dlm);
  1810. dlm->num_joins++;
  1811. spin_unlock(&dlm_domain_lock);
  1812. ret = 0;
  1813. goto leave;
  1814. }
  1815. /* doesn't exist */
  1816. if (!new_ctxt) {
  1817. spin_unlock(&dlm_domain_lock);
  1818. new_ctxt = dlm_alloc_ctxt(domain, key);
  1819. if (new_ctxt)
  1820. goto retry;
  1821. ret = -ENOMEM;
  1822. mlog_errno(ret);
  1823. goto leave;
  1824. }
  1825. /* a little variable switch-a-roo here... */
  1826. dlm = new_ctxt;
  1827. new_ctxt = NULL;
  1828. /* add the new domain */
  1829. list_add_tail(&dlm->list, &dlm_domains);
  1830. spin_unlock(&dlm_domain_lock);
  1831. /*
  1832. * Pass the locking protocol version into the join. If the join
  1833. * succeeds, it will have the negotiated protocol set.
  1834. */
  1835. dlm->dlm_locking_proto = dlm_protocol;
  1836. dlm->fs_locking_proto = *fs_proto;
  1837. ret = dlm_join_domain(dlm);
  1838. if (ret) {
  1839. mlog_errno(ret);
  1840. dlm_put(dlm);
  1841. goto leave;
  1842. }
  1843. /* Tell the caller what locking protocol we negotiated */
  1844. *fs_proto = dlm->fs_locking_proto;
  1845. ret = 0;
  1846. leave:
  1847. if (new_ctxt)
  1848. dlm_free_ctxt_mem(new_ctxt);
  1849. if (ret < 0)
  1850. dlm = ERR_PTR(ret);
  1851. return dlm;
  1852. }
  1853. EXPORT_SYMBOL_GPL(dlm_register_domain);
  1854. static LIST_HEAD(dlm_join_handlers);
  1855. static void dlm_unregister_net_handlers(void)
  1856. {
  1857. o2net_unregister_handler_list(&dlm_join_handlers);
  1858. }
  1859. static int dlm_register_net_handlers(void)
  1860. {
  1861. int status = 0;
  1862. status = o2net_register_handler(DLM_QUERY_JOIN_MSG, DLM_MOD_KEY,
  1863. sizeof(struct dlm_query_join_request),
  1864. dlm_query_join_handler,
  1865. NULL, NULL, &dlm_join_handlers);
  1866. if (status)
  1867. goto bail;
  1868. status = o2net_register_handler(DLM_ASSERT_JOINED_MSG, DLM_MOD_KEY,
  1869. sizeof(struct dlm_assert_joined),
  1870. dlm_assert_joined_handler,
  1871. NULL, NULL, &dlm_join_handlers);
  1872. if (status)
  1873. goto bail;
  1874. status = o2net_register_handler(DLM_CANCEL_JOIN_MSG, DLM_MOD_KEY,
  1875. sizeof(struct dlm_cancel_join),
  1876. dlm_cancel_join_handler,
  1877. NULL, NULL, &dlm_join_handlers);
  1878. if (status)
  1879. goto bail;
  1880. status = o2net_register_handler(DLM_QUERY_REGION, DLM_MOD_KEY,
  1881. sizeof(struct dlm_query_region),
  1882. dlm_query_region_handler,
  1883. NULL, NULL, &dlm_join_handlers);
  1884. if (status)
  1885. goto bail;
  1886. status = o2net_register_handler(DLM_QUERY_NODEINFO, DLM_MOD_KEY,
  1887. sizeof(struct dlm_query_nodeinfo),
  1888. dlm_query_nodeinfo_handler,
  1889. NULL, NULL, &dlm_join_handlers);
  1890. bail:
  1891. if (status < 0)
  1892. dlm_unregister_net_handlers();
  1893. return status;
  1894. }
  1895. /* Domain eviction callback handling.
  1896. *
  1897. * The file system requires notification of node death *before* the
  1898. * dlm completes it's recovery work, otherwise it may be able to
  1899. * acquire locks on resources requiring recovery. Since the dlm can
  1900. * evict a node from it's domain *before* heartbeat fires, a similar
  1901. * mechanism is required. */
  1902. /* Eviction is not expected to happen often, so a per-domain lock is
  1903. * not necessary. Eviction callbacks are allowed to sleep for short
  1904. * periods of time. */
  1905. static DECLARE_RWSEM(dlm_callback_sem);
  1906. void dlm_fire_domain_eviction_callbacks(struct dlm_ctxt *dlm,
  1907. int node_num)
  1908. {
  1909. struct list_head *iter;
  1910. struct dlm_eviction_cb *cb;
  1911. down_read(&dlm_callback_sem);
  1912. list_for_each(iter, &dlm->dlm_eviction_callbacks) {
  1913. cb = list_entry(iter, struct dlm_eviction_cb, ec_item);
  1914. cb->ec_func(node_num, cb->ec_data);
  1915. }
  1916. up_read(&dlm_callback_sem);
  1917. }
  1918. void dlm_setup_eviction_cb(struct dlm_eviction_cb *cb,
  1919. dlm_eviction_func *f,
  1920. void *data)
  1921. {
  1922. INIT_LIST_HEAD(&cb->ec_item);
  1923. cb->ec_func = f;
  1924. cb->ec_data = data;
  1925. }
  1926. EXPORT_SYMBOL_GPL(dlm_setup_eviction_cb);
  1927. void dlm_register_eviction_cb(struct dlm_ctxt *dlm,
  1928. struct dlm_eviction_cb *cb)
  1929. {
  1930. down_write(&dlm_callback_sem);
  1931. list_add_tail(&cb->ec_item, &dlm->dlm_eviction_callbacks);
  1932. up_write(&dlm_callback_sem);
  1933. }
  1934. EXPORT_SYMBOL_GPL(dlm_register_eviction_cb);
  1935. void dlm_unregister_eviction_cb(struct dlm_eviction_cb *cb)
  1936. {
  1937. down_write(&dlm_callback_sem);
  1938. list_del_init(&cb->ec_item);
  1939. up_write(&dlm_callback_sem);
  1940. }
  1941. EXPORT_SYMBOL_GPL(dlm_unregister_eviction_cb);
  1942. static int __init dlm_init(void)
  1943. {
  1944. int status;
  1945. dlm_print_version();
  1946. status = dlm_init_mle_cache();
  1947. if (status) {
  1948. mlog(ML_ERROR, "Could not create o2dlm_mle slabcache\n");
  1949. goto error;
  1950. }
  1951. status = dlm_init_master_caches();
  1952. if (status) {
  1953. mlog(ML_ERROR, "Could not create o2dlm_lockres and "
  1954. "o2dlm_lockname slabcaches\n");
  1955. goto error;
  1956. }
  1957. status = dlm_init_lock_cache();
  1958. if (status) {
  1959. mlog(ML_ERROR, "Count not create o2dlm_lock slabcache\n");
  1960. goto error;
  1961. }
  1962. status = dlm_register_net_handlers();
  1963. if (status) {
  1964. mlog(ML_ERROR, "Unable to register network handlers\n");
  1965. goto error;
  1966. }
  1967. status = dlm_create_debugfs_root();
  1968. if (status)
  1969. goto error;
  1970. return 0;
  1971. error:
  1972. dlm_unregister_net_handlers();
  1973. dlm_destroy_lock_cache();
  1974. dlm_destroy_master_caches();
  1975. dlm_destroy_mle_cache();
  1976. return -1;
  1977. }
  1978. static void __exit dlm_exit (void)
  1979. {
  1980. dlm_destroy_debugfs_root();
  1981. dlm_unregister_net_handlers();
  1982. dlm_destroy_lock_cache();
  1983. dlm_destroy_master_caches();
  1984. dlm_destroy_mle_cache();
  1985. }
  1986. MODULE_AUTHOR("Oracle");
  1987. MODULE_LICENSE("GPL");
  1988. module_init(dlm_init);
  1989. module_exit(dlm_exit);