dlmdomain.c 61 KB

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