dlmdomain.c 59 KB

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