dlmdomain.c 60 KB

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