dlmdomain.c 60 KB

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