dlmdomain.c 60 KB

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