dlmdomain.c 61 KB

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