dlmcommon.h 28 KB

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