dlmcommon.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmcommon.h
  5. *
  6. * Copyright (C) 2004 Oracle. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 021110-1307, USA.
  22. *
  23. */
  24. #ifndef DLMCOMMON_H
  25. #define DLMCOMMON_H
  26. #include <linux/kref.h>
  27. #define DLM_HB_NODE_DOWN_PRI (0xf000000)
  28. #define DLM_HB_NODE_UP_PRI (0x8000000)
  29. #define DLM_LOCKID_NAME_MAX 32
  30. #define DLM_DOMAIN_NAME_MAX_LEN 255
  31. #define DLM_LOCK_RES_OWNER_UNKNOWN O2NM_MAX_NODES
  32. #define DLM_THREAD_SHUFFLE_INTERVAL 5 // flush everything every 5 passes
  33. #define DLM_THREAD_MS 200 // flush at least every 200 ms
  34. #define DLM_HASH_SIZE_DEFAULT (1 << 14)
  35. #if DLM_HASH_SIZE_DEFAULT < PAGE_SIZE
  36. # define DLM_HASH_PAGES 1
  37. #else
  38. # define DLM_HASH_PAGES (DLM_HASH_SIZE_DEFAULT / PAGE_SIZE)
  39. #endif
  40. #define DLM_BUCKETS_PER_PAGE (PAGE_SIZE / sizeof(struct hlist_head))
  41. #define DLM_HASH_BUCKETS (DLM_HASH_PAGES * DLM_BUCKETS_PER_PAGE)
  42. /* Intended to make it easier for us to switch out hash functions */
  43. #define dlm_lockid_hash(_n, _l) full_name_hash(_n, _l)
  44. enum dlm_mle_type {
  45. DLM_MLE_BLOCK,
  46. DLM_MLE_MASTER,
  47. DLM_MLE_MIGRATION,
  48. DLM_MLE_NUM_TYPES
  49. };
  50. struct dlm_lock_name {
  51. unsigned int hash;
  52. unsigned int len;
  53. unsigned char name[DLM_LOCKID_NAME_MAX];
  54. };
  55. struct dlm_master_list_entry {
  56. struct hlist_node master_hash_node;
  57. struct list_head hb_events;
  58. struct dlm_ctxt *dlm;
  59. spinlock_t spinlock;
  60. wait_queue_head_t wq;
  61. atomic_t woken;
  62. struct kref mle_refs;
  63. int inuse;
  64. unsigned long maybe_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  65. unsigned long vote_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  66. unsigned long response_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  67. unsigned long node_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  68. u8 master;
  69. u8 new_master;
  70. enum dlm_mle_type type;
  71. struct o2hb_callback_func mle_hb_up;
  72. struct o2hb_callback_func mle_hb_down;
  73. union {
  74. struct dlm_lock_resource *mleres;
  75. struct dlm_lock_name mlename;
  76. } u;
  77. };
  78. enum dlm_ast_type {
  79. DLM_AST = 0,
  80. DLM_BAST,
  81. DLM_ASTUNLOCK
  82. };
  83. #define LKM_VALID_FLAGS (LKM_VALBLK | LKM_CONVERT | LKM_UNLOCK | \
  84. LKM_CANCEL | LKM_INVVALBLK | LKM_FORCE | \
  85. LKM_RECOVERY | LKM_LOCAL | LKM_NOQUEUE)
  86. #define DLM_RECOVERY_LOCK_NAME "$RECOVERY"
  87. #define DLM_RECOVERY_LOCK_NAME_LEN 9
  88. static inline int dlm_is_recovery_lock(const char *lock_name, int name_len)
  89. {
  90. if (name_len == DLM_RECOVERY_LOCK_NAME_LEN &&
  91. memcmp(lock_name, DLM_RECOVERY_LOCK_NAME, name_len)==0)
  92. return 1;
  93. return 0;
  94. }
  95. #define DLM_RECO_STATE_ACTIVE 0x0001
  96. #define DLM_RECO_STATE_FINALIZE 0x0002
  97. struct dlm_recovery_ctxt
  98. {
  99. struct list_head resources;
  100. struct list_head received;
  101. struct list_head node_data;
  102. u8 new_master;
  103. u8 dead_node;
  104. u16 state;
  105. unsigned long node_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  106. wait_queue_head_t event;
  107. };
  108. enum dlm_ctxt_state {
  109. DLM_CTXT_NEW = 0,
  110. DLM_CTXT_JOINED,
  111. DLM_CTXT_IN_SHUTDOWN,
  112. DLM_CTXT_LEAVING,
  113. };
  114. struct dlm_ctxt
  115. {
  116. struct list_head list;
  117. struct hlist_head **lockres_hash;
  118. struct list_head dirty_list;
  119. struct list_head purge_list;
  120. struct list_head pending_asts;
  121. struct list_head pending_basts;
  122. struct list_head tracking_list;
  123. unsigned int purge_count;
  124. spinlock_t spinlock;
  125. spinlock_t ast_lock;
  126. spinlock_t track_lock;
  127. char *name;
  128. u8 node_num;
  129. u32 key;
  130. u8 joining_node;
  131. wait_queue_head_t dlm_join_events;
  132. unsigned long live_nodes_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  133. unsigned long domain_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  134. unsigned long recovery_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  135. struct dlm_recovery_ctxt reco;
  136. spinlock_t master_lock;
  137. struct hlist_head **master_hash;
  138. struct list_head mle_hb_events;
  139. /* these give a really vague idea of the system load */
  140. atomic_t mle_tot_count[DLM_MLE_NUM_TYPES];
  141. atomic_t mle_cur_count[DLM_MLE_NUM_TYPES];
  142. atomic_t local_resources;
  143. atomic_t remote_resources;
  144. atomic_t unknown_resources;
  145. struct dlm_debug_ctxt *dlm_debug_ctxt;
  146. struct dentry *dlm_debugfs_subroot;
  147. /* NOTE: Next three are protected by dlm_domain_lock */
  148. struct kref dlm_refs;
  149. enum dlm_ctxt_state dlm_state;
  150. unsigned int num_joins;
  151. struct o2hb_callback_func dlm_hb_up;
  152. struct o2hb_callback_func dlm_hb_down;
  153. struct task_struct *dlm_thread_task;
  154. struct task_struct *dlm_reco_thread_task;
  155. struct workqueue_struct *dlm_worker;
  156. wait_queue_head_t dlm_thread_wq;
  157. wait_queue_head_t dlm_reco_thread_wq;
  158. wait_queue_head_t ast_wq;
  159. wait_queue_head_t migration_wq;
  160. struct work_struct dispatched_work;
  161. struct list_head work_list;
  162. spinlock_t work_lock;
  163. struct list_head dlm_domain_handlers;
  164. struct list_head dlm_eviction_callbacks;
  165. /* The filesystem specifies this at domain registration. We
  166. * cache it here to know what to tell other nodes. */
  167. struct dlm_protocol_version fs_locking_proto;
  168. /* This is the inter-dlm communication version */
  169. struct dlm_protocol_version dlm_locking_proto;
  170. };
  171. static inline struct hlist_head *dlm_lockres_hash(struct dlm_ctxt *dlm, unsigned i)
  172. {
  173. return dlm->lockres_hash[(i / DLM_BUCKETS_PER_PAGE) % DLM_HASH_PAGES] + (i % DLM_BUCKETS_PER_PAGE);
  174. }
  175. static inline struct hlist_head *dlm_master_hash(struct dlm_ctxt *dlm,
  176. unsigned i)
  177. {
  178. return dlm->master_hash[(i / DLM_BUCKETS_PER_PAGE) % DLM_HASH_PAGES] +
  179. (i % DLM_BUCKETS_PER_PAGE);
  180. }
  181. /* these keventd work queue items are for less-frequently
  182. * called functions that cannot be directly called from the
  183. * net message handlers for some reason, usually because
  184. * they need to send net messages of their own. */
  185. void dlm_dispatch_work(struct work_struct *work);
  186. struct dlm_lock_resource;
  187. struct dlm_work_item;
  188. typedef void (dlm_workfunc_t)(struct dlm_work_item *, void *);
  189. struct dlm_request_all_locks_priv
  190. {
  191. u8 reco_master;
  192. u8 dead_node;
  193. };
  194. struct dlm_mig_lockres_priv
  195. {
  196. struct dlm_lock_resource *lockres;
  197. u8 real_master;
  198. u8 extra_ref;
  199. };
  200. struct dlm_assert_master_priv
  201. {
  202. struct dlm_lock_resource *lockres;
  203. u8 request_from;
  204. u32 flags;
  205. unsigned ignore_higher:1;
  206. };
  207. struct dlm_deref_lockres_priv
  208. {
  209. struct dlm_lock_resource *deref_res;
  210. u8 deref_node;
  211. };
  212. struct dlm_work_item
  213. {
  214. struct list_head list;
  215. dlm_workfunc_t *func;
  216. struct dlm_ctxt *dlm;
  217. void *data;
  218. union {
  219. struct dlm_request_all_locks_priv ral;
  220. struct dlm_mig_lockres_priv ml;
  221. struct dlm_assert_master_priv am;
  222. struct dlm_deref_lockres_priv dl;
  223. } u;
  224. };
  225. static inline void dlm_init_work_item(struct dlm_ctxt *dlm,
  226. struct dlm_work_item *i,
  227. dlm_workfunc_t *f, void *data)
  228. {
  229. memset(i, 0, sizeof(*i));
  230. i->func = f;
  231. INIT_LIST_HEAD(&i->list);
  232. i->data = data;
  233. i->dlm = dlm; /* must have already done a dlm_grab on this! */
  234. }
  235. static inline void __dlm_set_joining_node(struct dlm_ctxt *dlm,
  236. u8 node)
  237. {
  238. assert_spin_locked(&dlm->spinlock);
  239. dlm->joining_node = node;
  240. wake_up(&dlm->dlm_join_events);
  241. }
  242. #define DLM_LOCK_RES_UNINITED 0x00000001
  243. #define DLM_LOCK_RES_RECOVERING 0x00000002
  244. #define DLM_LOCK_RES_READY 0x00000004
  245. #define DLM_LOCK_RES_DIRTY 0x00000008
  246. #define DLM_LOCK_RES_IN_PROGRESS 0x00000010
  247. #define DLM_LOCK_RES_MIGRATING 0x00000020
  248. #define DLM_LOCK_RES_DROPPING_REF 0x00000040
  249. #define DLM_LOCK_RES_BLOCK_DIRTY 0x00001000
  250. #define DLM_LOCK_RES_SETREF_INPROG 0x00002000
  251. /* max milliseconds to wait to sync up a network failure with a node death */
  252. #define DLM_NODE_DEATH_WAIT_MAX (5 * 1000)
  253. #define DLM_PURGE_INTERVAL_MS (8 * 1000)
  254. struct dlm_lock_resource
  255. {
  256. /* WARNING: Please see the comment in dlm_init_lockres before
  257. * adding fields here. */
  258. struct hlist_node hash_node;
  259. struct qstr lockname;
  260. struct kref refs;
  261. /*
  262. * Please keep granted, converting, and blocked in this order,
  263. * as some funcs want to iterate over all lists.
  264. *
  265. * All four lists are protected by the hash's reference.
  266. */
  267. struct list_head granted;
  268. struct list_head converting;
  269. struct list_head blocked;
  270. struct list_head purge;
  271. /*
  272. * These two lists require you to hold an additional reference
  273. * while they are on the list.
  274. */
  275. struct list_head dirty;
  276. struct list_head recovering; // dlm_recovery_ctxt.resources list
  277. /* Added during init and removed during release */
  278. struct list_head tracking; /* dlm->tracking_list */
  279. /* unused lock resources have their last_used stamped and are
  280. * put on a list for the dlm thread to run. */
  281. unsigned long last_used;
  282. struct dlm_ctxt *dlm;
  283. unsigned migration_pending:1;
  284. atomic_t asts_reserved;
  285. spinlock_t spinlock;
  286. wait_queue_head_t wq;
  287. u8 owner; //node which owns the lock resource, or unknown
  288. u16 state;
  289. char lvb[DLM_LVB_LEN];
  290. unsigned int inflight_locks;
  291. unsigned long refmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  292. };
  293. struct dlm_migratable_lock
  294. {
  295. __be64 cookie;
  296. /* these 3 are just padding for the in-memory structure, but
  297. * list and flags are actually used when sent over the wire */
  298. __be16 pad1;
  299. u8 list; // 0=granted, 1=converting, 2=blocked
  300. u8 flags;
  301. s8 type;
  302. s8 convert_type;
  303. s8 highest_blocked;
  304. u8 node;
  305. }; // 16 bytes
  306. struct dlm_lock
  307. {
  308. struct dlm_migratable_lock ml;
  309. struct list_head list;
  310. struct list_head ast_list;
  311. struct list_head bast_list;
  312. struct dlm_lock_resource *lockres;
  313. spinlock_t spinlock;
  314. struct kref lock_refs;
  315. // ast and bast must be callable while holding a spinlock!
  316. dlm_astlockfunc_t *ast;
  317. dlm_bastlockfunc_t *bast;
  318. void *astdata;
  319. struct dlm_lockstatus *lksb;
  320. unsigned ast_pending:1,
  321. bast_pending:1,
  322. convert_pending:1,
  323. lock_pending:1,
  324. cancel_pending:1,
  325. unlock_pending:1,
  326. lksb_kernel_allocated:1;
  327. };
  328. #define DLM_LKSB_UNUSED1 0x01
  329. #define DLM_LKSB_PUT_LVB 0x02
  330. #define DLM_LKSB_GET_LVB 0x04
  331. #define DLM_LKSB_UNUSED2 0x08
  332. #define DLM_LKSB_UNUSED3 0x10
  333. #define DLM_LKSB_UNUSED4 0x20
  334. #define DLM_LKSB_UNUSED5 0x40
  335. #define DLM_LKSB_UNUSED6 0x80
  336. enum dlm_lockres_list {
  337. DLM_GRANTED_LIST = 0,
  338. DLM_CONVERTING_LIST,
  339. DLM_BLOCKED_LIST
  340. };
  341. static inline int dlm_lvb_is_empty(char *lvb)
  342. {
  343. int i;
  344. for (i=0; i<DLM_LVB_LEN; i++)
  345. if (lvb[i])
  346. return 0;
  347. return 1;
  348. }
  349. static inline struct list_head *
  350. dlm_list_idx_to_ptr(struct dlm_lock_resource *res, enum dlm_lockres_list idx)
  351. {
  352. struct list_head *ret = NULL;
  353. if (idx == DLM_GRANTED_LIST)
  354. ret = &res->granted;
  355. else if (idx == DLM_CONVERTING_LIST)
  356. ret = &res->converting;
  357. else if (idx == DLM_BLOCKED_LIST)
  358. ret = &res->blocked;
  359. else
  360. BUG();
  361. return ret;
  362. }
  363. struct dlm_node_iter
  364. {
  365. unsigned long node_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  366. int curnode;
  367. };
  368. enum {
  369. DLM_MASTER_REQUEST_MSG = 500,
  370. DLM_UNUSED_MSG1, /* 501 */
  371. DLM_ASSERT_MASTER_MSG, /* 502 */
  372. DLM_CREATE_LOCK_MSG, /* 503 */
  373. DLM_CONVERT_LOCK_MSG, /* 504 */
  374. DLM_PROXY_AST_MSG, /* 505 */
  375. DLM_UNLOCK_LOCK_MSG, /* 506 */
  376. DLM_DEREF_LOCKRES_MSG, /* 507 */
  377. DLM_MIGRATE_REQUEST_MSG, /* 508 */
  378. DLM_MIG_LOCKRES_MSG, /* 509 */
  379. DLM_QUERY_JOIN_MSG, /* 510 */
  380. DLM_ASSERT_JOINED_MSG, /* 511 */
  381. DLM_CANCEL_JOIN_MSG, /* 512 */
  382. DLM_EXIT_DOMAIN_MSG, /* 513 */
  383. DLM_MASTER_REQUERY_MSG, /* 514 */
  384. DLM_LOCK_REQUEST_MSG, /* 515 */
  385. DLM_RECO_DATA_DONE_MSG, /* 516 */
  386. DLM_BEGIN_RECO_MSG, /* 517 */
  387. DLM_FINALIZE_RECO_MSG /* 518 */
  388. };
  389. struct dlm_reco_node_data
  390. {
  391. int state;
  392. u8 node_num;
  393. struct list_head list;
  394. };
  395. enum {
  396. DLM_RECO_NODE_DATA_DEAD = -1,
  397. DLM_RECO_NODE_DATA_INIT = 0,
  398. DLM_RECO_NODE_DATA_REQUESTING,
  399. DLM_RECO_NODE_DATA_REQUESTED,
  400. DLM_RECO_NODE_DATA_RECEIVING,
  401. DLM_RECO_NODE_DATA_DONE,
  402. DLM_RECO_NODE_DATA_FINALIZE_SENT,
  403. };
  404. enum {
  405. DLM_MASTER_RESP_NO = 0,
  406. DLM_MASTER_RESP_YES,
  407. DLM_MASTER_RESP_MAYBE,
  408. DLM_MASTER_RESP_ERROR
  409. };
  410. struct dlm_master_request
  411. {
  412. u8 node_idx;
  413. u8 namelen;
  414. __be16 pad1;
  415. __be32 flags;
  416. u8 name[O2NM_MAX_NAME_LEN];
  417. };
  418. #define DLM_ASSERT_RESPONSE_REASSERT 0x00000001
  419. #define DLM_ASSERT_RESPONSE_MASTERY_REF 0x00000002
  420. #define DLM_ASSERT_MASTER_MLE_CLEANUP 0x00000001
  421. #define DLM_ASSERT_MASTER_REQUERY 0x00000002
  422. #define DLM_ASSERT_MASTER_FINISH_MIGRATION 0x00000004
  423. struct dlm_assert_master
  424. {
  425. u8 node_idx;
  426. u8 namelen;
  427. __be16 pad1;
  428. __be32 flags;
  429. u8 name[O2NM_MAX_NAME_LEN];
  430. };
  431. #define DLM_MIGRATE_RESPONSE_MASTERY_REF 0x00000001
  432. struct dlm_migrate_request
  433. {
  434. u8 master;
  435. u8 new_master;
  436. u8 namelen;
  437. u8 pad1;
  438. __be32 pad2;
  439. u8 name[O2NM_MAX_NAME_LEN];
  440. };
  441. struct dlm_master_requery
  442. {
  443. u8 pad1;
  444. u8 pad2;
  445. u8 node_idx;
  446. u8 namelen;
  447. __be32 pad3;
  448. u8 name[O2NM_MAX_NAME_LEN];
  449. };
  450. #define DLM_MRES_RECOVERY 0x01
  451. #define DLM_MRES_MIGRATION 0x02
  452. #define DLM_MRES_ALL_DONE 0x04
  453. /*
  454. * We would like to get one whole lockres into a single network
  455. * message whenever possible. Generally speaking, there will be
  456. * at most one dlm_lock on a lockres for each node in the cluster,
  457. * plus (infrequently) any additional locks coming in from userdlm.
  458. *
  459. * struct _dlm_lockres_page
  460. * {
  461. * dlm_migratable_lockres mres;
  462. * dlm_migratable_lock ml[DLM_MAX_MIGRATABLE_LOCKS];
  463. * u8 pad[DLM_MIG_LOCKRES_RESERVED];
  464. * };
  465. *
  466. * from ../cluster/tcp.h
  467. * NET_MAX_PAYLOAD_BYTES (4096 - sizeof(net_msg))
  468. * (roughly 4080 bytes)
  469. * and sizeof(dlm_migratable_lockres) = 112 bytes
  470. * and sizeof(dlm_migratable_lock) = 16 bytes
  471. *
  472. * Choosing DLM_MAX_MIGRATABLE_LOCKS=240 and
  473. * DLM_MIG_LOCKRES_RESERVED=128 means we have this:
  474. *
  475. * (DLM_MAX_MIGRATABLE_LOCKS * sizeof(dlm_migratable_lock)) +
  476. * sizeof(dlm_migratable_lockres) + DLM_MIG_LOCKRES_RESERVED =
  477. * NET_MAX_PAYLOAD_BYTES
  478. * (240 * 16) + 112 + 128 = 4080
  479. *
  480. * So a lockres would need more than 240 locks before it would
  481. * use more than one network packet to recover. Not too bad.
  482. */
  483. #define DLM_MAX_MIGRATABLE_LOCKS 240
  484. struct dlm_migratable_lockres
  485. {
  486. u8 master;
  487. u8 lockname_len;
  488. u8 num_locks; // locks sent in this structure
  489. u8 flags;
  490. __be32 total_locks; // locks to be sent for this migration cookie
  491. __be64 mig_cookie; // cookie for this lockres migration
  492. // or zero if not needed
  493. // 16 bytes
  494. u8 lockname[DLM_LOCKID_NAME_MAX];
  495. // 48 bytes
  496. u8 lvb[DLM_LVB_LEN];
  497. // 112 bytes
  498. struct dlm_migratable_lock ml[0]; // 16 bytes each, begins at byte 112
  499. };
  500. #define DLM_MIG_LOCKRES_MAX_LEN \
  501. (sizeof(struct dlm_migratable_lockres) + \
  502. (sizeof(struct dlm_migratable_lock) * \
  503. DLM_MAX_MIGRATABLE_LOCKS) )
  504. /* from above, 128 bytes
  505. * for some undetermined future use */
  506. #define DLM_MIG_LOCKRES_RESERVED (NET_MAX_PAYLOAD_BYTES - \
  507. DLM_MIG_LOCKRES_MAX_LEN)
  508. struct dlm_create_lock
  509. {
  510. __be64 cookie;
  511. __be32 flags;
  512. u8 pad1;
  513. u8 node_idx;
  514. s8 requested_type;
  515. u8 namelen;
  516. u8 name[O2NM_MAX_NAME_LEN];
  517. };
  518. struct dlm_convert_lock
  519. {
  520. __be64 cookie;
  521. __be32 flags;
  522. u8 pad1;
  523. u8 node_idx;
  524. s8 requested_type;
  525. u8 namelen;
  526. u8 name[O2NM_MAX_NAME_LEN];
  527. s8 lvb[0];
  528. };
  529. #define DLM_CONVERT_LOCK_MAX_LEN (sizeof(struct dlm_convert_lock)+DLM_LVB_LEN)
  530. struct dlm_unlock_lock
  531. {
  532. __be64 cookie;
  533. __be32 flags;
  534. __be16 pad1;
  535. u8 node_idx;
  536. u8 namelen;
  537. u8 name[O2NM_MAX_NAME_LEN];
  538. s8 lvb[0];
  539. };
  540. #define DLM_UNLOCK_LOCK_MAX_LEN (sizeof(struct dlm_unlock_lock)+DLM_LVB_LEN)
  541. struct dlm_proxy_ast
  542. {
  543. __be64 cookie;
  544. __be32 flags;
  545. u8 node_idx;
  546. u8 type;
  547. u8 blocked_type;
  548. u8 namelen;
  549. u8 name[O2NM_MAX_NAME_LEN];
  550. s8 lvb[0];
  551. };
  552. #define DLM_PROXY_AST_MAX_LEN (sizeof(struct dlm_proxy_ast)+DLM_LVB_LEN)
  553. #define DLM_MOD_KEY (0x666c6172)
  554. enum dlm_query_join_response_code {
  555. JOIN_DISALLOW = 0,
  556. JOIN_OK,
  557. JOIN_OK_NO_MAP,
  558. JOIN_PROTOCOL_MISMATCH,
  559. };
  560. struct dlm_query_join_packet {
  561. u8 code; /* Response code. dlm_minor and fs_minor
  562. are only valid if this is JOIN_OK */
  563. u8 dlm_minor; /* The minor version of the protocol the
  564. dlm is speaking. */
  565. u8 fs_minor; /* The minor version of the protocol the
  566. filesystem is speaking. */
  567. u8 reserved;
  568. };
  569. union dlm_query_join_response {
  570. u32 intval;
  571. struct dlm_query_join_packet packet;
  572. };
  573. struct dlm_lock_request
  574. {
  575. u8 node_idx;
  576. u8 dead_node;
  577. __be16 pad1;
  578. __be32 pad2;
  579. };
  580. struct dlm_reco_data_done
  581. {
  582. u8 node_idx;
  583. u8 dead_node;
  584. __be16 pad1;
  585. __be32 pad2;
  586. /* unused for now */
  587. /* eventually we can use this to attempt
  588. * lvb recovery based on each node's info */
  589. u8 reco_lvb[DLM_LVB_LEN];
  590. };
  591. struct dlm_begin_reco
  592. {
  593. u8 node_idx;
  594. u8 dead_node;
  595. __be16 pad1;
  596. __be32 pad2;
  597. };
  598. #define BITS_PER_BYTE 8
  599. #define BITS_TO_BYTES(bits) (((bits)+BITS_PER_BYTE-1)/BITS_PER_BYTE)
  600. struct dlm_query_join_request
  601. {
  602. u8 node_idx;
  603. u8 pad1[2];
  604. u8 name_len;
  605. struct dlm_protocol_version dlm_proto;
  606. struct dlm_protocol_version fs_proto;
  607. u8 domain[O2NM_MAX_NAME_LEN];
  608. u8 node_map[BITS_TO_BYTES(O2NM_MAX_NODES)];
  609. };
  610. struct dlm_assert_joined
  611. {
  612. u8 node_idx;
  613. u8 pad1[2];
  614. u8 name_len;
  615. u8 domain[O2NM_MAX_NAME_LEN];
  616. };
  617. struct dlm_cancel_join
  618. {
  619. u8 node_idx;
  620. u8 pad1[2];
  621. u8 name_len;
  622. u8 domain[O2NM_MAX_NAME_LEN];
  623. };
  624. struct dlm_exit_domain
  625. {
  626. u8 node_idx;
  627. u8 pad1[3];
  628. };
  629. struct dlm_finalize_reco
  630. {
  631. u8 node_idx;
  632. u8 dead_node;
  633. u8 flags;
  634. u8 pad1;
  635. __be32 pad2;
  636. };
  637. struct dlm_deref_lockres
  638. {
  639. u32 pad1;
  640. u16 pad2;
  641. u8 node_idx;
  642. u8 namelen;
  643. u8 name[O2NM_MAX_NAME_LEN];
  644. };
  645. static inline enum dlm_status
  646. __dlm_lockres_state_to_status(struct dlm_lock_resource *res)
  647. {
  648. enum dlm_status status = DLM_NORMAL;
  649. assert_spin_locked(&res->spinlock);
  650. if (res->state & DLM_LOCK_RES_RECOVERING)
  651. status = DLM_RECOVERING;
  652. else if (res->state & DLM_LOCK_RES_MIGRATING)
  653. status = DLM_MIGRATING;
  654. else if (res->state & DLM_LOCK_RES_IN_PROGRESS)
  655. status = DLM_FORWARD;
  656. return status;
  657. }
  658. static inline u8 dlm_get_lock_cookie_node(u64 cookie)
  659. {
  660. u8 ret;
  661. cookie >>= 56;
  662. ret = (u8)(cookie & 0xffULL);
  663. return ret;
  664. }
  665. static inline unsigned long long dlm_get_lock_cookie_seq(u64 cookie)
  666. {
  667. unsigned long long ret;
  668. ret = ((unsigned long long)cookie) & 0x00ffffffffffffffULL;
  669. return ret;
  670. }
  671. struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie,
  672. struct dlm_lockstatus *lksb);
  673. void dlm_lock_get(struct dlm_lock *lock);
  674. void dlm_lock_put(struct dlm_lock *lock);
  675. void dlm_lock_attach_lockres(struct dlm_lock *lock,
  676. struct dlm_lock_resource *res);
  677. int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data,
  678. void **ret_data);
  679. int dlm_convert_lock_handler(struct o2net_msg *msg, u32 len, void *data,
  680. void **ret_data);
  681. int dlm_proxy_ast_handler(struct o2net_msg *msg, u32 len, void *data,
  682. void **ret_data);
  683. void dlm_revert_pending_convert(struct dlm_lock_resource *res,
  684. struct dlm_lock *lock);
  685. void dlm_revert_pending_lock(struct dlm_lock_resource *res,
  686. struct dlm_lock *lock);
  687. int dlm_unlock_lock_handler(struct o2net_msg *msg, u32 len, void *data,
  688. void **ret_data);
  689. void dlm_commit_pending_cancel(struct dlm_lock_resource *res,
  690. struct dlm_lock *lock);
  691. void dlm_commit_pending_unlock(struct dlm_lock_resource *res,
  692. struct dlm_lock *lock);
  693. int dlm_launch_thread(struct dlm_ctxt *dlm);
  694. void dlm_complete_thread(struct dlm_ctxt *dlm);
  695. int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
  696. void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
  697. void dlm_wait_for_recovery(struct dlm_ctxt *dlm);
  698. void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
  699. int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node);
  700. int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout);
  701. int dlm_wait_for_node_recovery(struct dlm_ctxt *dlm, u8 node, int timeout);
  702. void dlm_put(struct dlm_ctxt *dlm);
  703. struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm);
  704. int dlm_domain_fully_joined(struct dlm_ctxt *dlm);
  705. void __dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
  706. struct dlm_lock_resource *res);
  707. void dlm_lockres_calc_usage(struct dlm_ctxt *dlm,
  708. struct dlm_lock_resource *res);
  709. static inline void dlm_lockres_get(struct dlm_lock_resource *res)
  710. {
  711. /* This is called on every lookup, so it might be worth
  712. * inlining. */
  713. kref_get(&res->refs);
  714. }
  715. void dlm_lockres_put(struct dlm_lock_resource *res);
  716. void __dlm_unhash_lockres(struct dlm_lock_resource *res);
  717. void __dlm_insert_lockres(struct dlm_ctxt *dlm,
  718. struct dlm_lock_resource *res);
  719. struct dlm_lock_resource * __dlm_lookup_lockres_full(struct dlm_ctxt *dlm,
  720. const char *name,
  721. unsigned int len,
  722. unsigned int hash);
  723. struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm,
  724. const char *name,
  725. unsigned int len,
  726. unsigned int hash);
  727. struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm,
  728. const char *name,
  729. unsigned int len);
  730. int dlm_is_host_down(int errno);
  731. void dlm_change_lockres_owner(struct dlm_ctxt *dlm,
  732. struct dlm_lock_resource *res,
  733. u8 owner);
  734. struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
  735. const char *lockid,
  736. int namelen,
  737. int flags);
  738. struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
  739. const char *name,
  740. unsigned int namelen);
  741. #define dlm_lockres_set_refmap_bit(bit,res) \
  742. __dlm_lockres_set_refmap_bit(bit,res,__FILE__,__LINE__)
  743. #define dlm_lockres_clear_refmap_bit(bit,res) \
  744. __dlm_lockres_clear_refmap_bit(bit,res,__FILE__,__LINE__)
  745. static inline void __dlm_lockres_set_refmap_bit(int bit,
  746. struct dlm_lock_resource *res,
  747. const char *file,
  748. int line)
  749. {
  750. //printk("%s:%d:%.*s: setting bit %d\n", file, line,
  751. // res->lockname.len, res->lockname.name, bit);
  752. set_bit(bit, res->refmap);
  753. }
  754. static inline void __dlm_lockres_clear_refmap_bit(int bit,
  755. struct dlm_lock_resource *res,
  756. const char *file,
  757. int line)
  758. {
  759. //printk("%s:%d:%.*s: clearing bit %d\n", file, line,
  760. // res->lockname.len, res->lockname.name, bit);
  761. clear_bit(bit, res->refmap);
  762. }
  763. void __dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm,
  764. struct dlm_lock_resource *res,
  765. const char *file,
  766. int line);
  767. void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
  768. struct dlm_lock_resource *res,
  769. int new_lockres,
  770. const char *file,
  771. int line);
  772. #define dlm_lockres_drop_inflight_ref(d,r) \
  773. __dlm_lockres_drop_inflight_ref(d,r,__FILE__,__LINE__)
  774. #define dlm_lockres_grab_inflight_ref(d,r) \
  775. __dlm_lockres_grab_inflight_ref(d,r,0,__FILE__,__LINE__)
  776. #define dlm_lockres_grab_inflight_ref_new(d,r) \
  777. __dlm_lockres_grab_inflight_ref(d,r,1,__FILE__,__LINE__)
  778. void dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock);
  779. void dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock);
  780. void dlm_do_local_ast(struct dlm_ctxt *dlm,
  781. struct dlm_lock_resource *res,
  782. struct dlm_lock *lock);
  783. int dlm_do_remote_ast(struct dlm_ctxt *dlm,
  784. struct dlm_lock_resource *res,
  785. struct dlm_lock *lock);
  786. void dlm_do_local_bast(struct dlm_ctxt *dlm,
  787. struct dlm_lock_resource *res,
  788. struct dlm_lock *lock,
  789. int blocked_type);
  790. int dlm_send_proxy_ast_msg(struct dlm_ctxt *dlm,
  791. struct dlm_lock_resource *res,
  792. struct dlm_lock *lock,
  793. int msg_type,
  794. int blocked_type, int flags);
  795. static inline int dlm_send_proxy_bast(struct dlm_ctxt *dlm,
  796. struct dlm_lock_resource *res,
  797. struct dlm_lock *lock,
  798. int blocked_type)
  799. {
  800. return dlm_send_proxy_ast_msg(dlm, res, lock, DLM_BAST,
  801. blocked_type, 0);
  802. }
  803. static inline int dlm_send_proxy_ast(struct dlm_ctxt *dlm,
  804. struct dlm_lock_resource *res,
  805. struct dlm_lock *lock,
  806. int flags)
  807. {
  808. return dlm_send_proxy_ast_msg(dlm, res, lock, DLM_AST,
  809. 0, flags);
  810. }
  811. void dlm_print_one_lock_resource(struct dlm_lock_resource *res);
  812. void __dlm_print_one_lock_resource(struct dlm_lock_resource *res);
  813. u8 dlm_nm_this_node(struct dlm_ctxt *dlm);
  814. void dlm_kick_thread(struct dlm_ctxt *dlm, struct dlm_lock_resource *res);
  815. void __dlm_dirty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res);
  816. int dlm_nm_init(struct dlm_ctxt *dlm);
  817. int dlm_heartbeat_init(struct dlm_ctxt *dlm);
  818. void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data);
  819. void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data);
  820. int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res);
  821. int dlm_finish_migration(struct dlm_ctxt *dlm,
  822. struct dlm_lock_resource *res,
  823. u8 old_master);
  824. void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
  825. struct dlm_lock_resource *res);
  826. void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res);
  827. int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data,
  828. void **ret_data);
  829. int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data,
  830. void **ret_data);
  831. void dlm_assert_master_post_handler(int status, void *data, void *ret_data);
  832. int dlm_deref_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
  833. void **ret_data);
  834. int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data,
  835. void **ret_data);
  836. int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
  837. void **ret_data);
  838. int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data,
  839. void **ret_data);
  840. int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data,
  841. void **ret_data);
  842. int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data,
  843. void **ret_data);
  844. int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data,
  845. void **ret_data);
  846. int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data,
  847. void **ret_data);
  848. int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  849. u8 nodenum, u8 *real_master);
  850. int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
  851. struct dlm_lock_resource *res,
  852. int ignore_higher,
  853. u8 request_from,
  854. u32 flags);
  855. int dlm_send_one_lockres(struct dlm_ctxt *dlm,
  856. struct dlm_lock_resource *res,
  857. struct dlm_migratable_lockres *mres,
  858. u8 send_to,
  859. u8 flags);
  860. void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
  861. struct dlm_lock_resource *res);
  862. /* will exit holding res->spinlock, but may drop in function */
  863. void __dlm_wait_on_lockres_flags(struct dlm_lock_resource *res, int flags);
  864. void __dlm_wait_on_lockres_flags_set(struct dlm_lock_resource *res, int flags);
  865. /* will exit holding res->spinlock, but may drop in function */
  866. static inline void __dlm_wait_on_lockres(struct dlm_lock_resource *res)
  867. {
  868. __dlm_wait_on_lockres_flags(res, (DLM_LOCK_RES_IN_PROGRESS|
  869. DLM_LOCK_RES_RECOVERING|
  870. DLM_LOCK_RES_MIGRATING));
  871. }
  872. void __dlm_unlink_mle(struct dlm_ctxt *dlm, struct dlm_master_list_entry *mle);
  873. void __dlm_insert_mle(struct dlm_ctxt *dlm, struct dlm_master_list_entry *mle);
  874. /* create/destroy slab caches */
  875. int dlm_init_master_caches(void);
  876. void dlm_destroy_master_caches(void);
  877. int dlm_init_lock_cache(void);
  878. void dlm_destroy_lock_cache(void);
  879. int dlm_init_mle_cache(void);
  880. void dlm_destroy_mle_cache(void);
  881. void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up);
  882. int dlm_drop_lockres_ref(struct dlm_ctxt *dlm,
  883. struct dlm_lock_resource *res);
  884. void dlm_clean_master_list(struct dlm_ctxt *dlm,
  885. u8 dead_node);
  886. int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock);
  887. int __dlm_lockres_has_locks(struct dlm_lock_resource *res);
  888. int __dlm_lockres_unused(struct dlm_lock_resource *res);
  889. static inline const char * dlm_lock_mode_name(int mode)
  890. {
  891. switch (mode) {
  892. case LKM_EXMODE:
  893. return "EX";
  894. case LKM_PRMODE:
  895. return "PR";
  896. case LKM_NLMODE:
  897. return "NL";
  898. }
  899. return "UNKNOWN";
  900. }
  901. static inline int dlm_lock_compatible(int existing, int request)
  902. {
  903. /* NO_LOCK compatible with all */
  904. if (request == LKM_NLMODE ||
  905. existing == LKM_NLMODE)
  906. return 1;
  907. /* EX incompatible with all non-NO_LOCK */
  908. if (request == LKM_EXMODE)
  909. return 0;
  910. /* request must be PR, which is compatible with PR */
  911. if (existing == LKM_PRMODE)
  912. return 1;
  913. return 0;
  914. }
  915. static inline int dlm_lock_on_list(struct list_head *head,
  916. struct dlm_lock *lock)
  917. {
  918. struct list_head *iter;
  919. struct dlm_lock *tmplock;
  920. list_for_each(iter, head) {
  921. tmplock = list_entry(iter, struct dlm_lock, list);
  922. if (tmplock == lock)
  923. return 1;
  924. }
  925. return 0;
  926. }
  927. static inline enum dlm_status dlm_err_to_dlm_status(int err)
  928. {
  929. enum dlm_status ret;
  930. if (err == -ENOMEM)
  931. ret = DLM_SYSERR;
  932. else if (err == -ETIMEDOUT || o2net_link_down(err, NULL))
  933. ret = DLM_NOLOCKMGR;
  934. else if (err == -EINVAL)
  935. ret = DLM_BADPARAM;
  936. else if (err == -ENAMETOOLONG)
  937. ret = DLM_IVBUFLEN;
  938. else
  939. ret = DLM_BADARGS;
  940. return ret;
  941. }
  942. static inline void dlm_node_iter_init(unsigned long *map,
  943. struct dlm_node_iter *iter)
  944. {
  945. memcpy(iter->node_map, map, sizeof(iter->node_map));
  946. iter->curnode = -1;
  947. }
  948. static inline int dlm_node_iter_next(struct dlm_node_iter *iter)
  949. {
  950. int bit;
  951. bit = find_next_bit(iter->node_map, O2NM_MAX_NODES, iter->curnode+1);
  952. if (bit >= O2NM_MAX_NODES) {
  953. iter->curnode = O2NM_MAX_NODES;
  954. return -ENOENT;
  955. }
  956. iter->curnode = bit;
  957. return bit;
  958. }
  959. #endif /* DLMCOMMON_H */