dlmcommon.h 29 KB

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