dlm_internal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  6. **
  7. ** This copyrighted material is made available to anyone wishing to use,
  8. ** modify, copy, or redistribute it subject to the terms and conditions
  9. ** of the GNU General Public License v.2.
  10. **
  11. *******************************************************************************
  12. ******************************************************************************/
  13. #ifndef __DLM_INTERNAL_DOT_H__
  14. #define __DLM_INTERNAL_DOT_H__
  15. /*
  16. * This is the main header file to be included in each DLM source file.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/sched.h>
  21. #include <linux/types.h>
  22. #include <linux/ctype.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/list.h>
  26. #include <linux/errno.h>
  27. #include <linux/random.h>
  28. #include <linux/delay.h>
  29. #include <linux/socket.h>
  30. #include <linux/kthread.h>
  31. #include <linux/kobject.h>
  32. #include <linux/kref.h>
  33. #include <linux/kernel.h>
  34. #include <linux/jhash.h>
  35. #include <linux/mutex.h>
  36. #include <asm/semaphore.h>
  37. #include <asm/uaccess.h>
  38. #include <linux/dlm.h>
  39. #define DLM_LOCKSPACE_LEN 64
  40. /* Size of the temp buffer midcomms allocates on the stack.
  41. We try to make this large enough so most messages fit.
  42. FIXME: should sctp make this unnecessary? */
  43. #define DLM_INBUF_LEN 148
  44. struct dlm_ls;
  45. struct dlm_lkb;
  46. struct dlm_rsb;
  47. struct dlm_member;
  48. struct dlm_lkbtable;
  49. struct dlm_rsbtable;
  50. struct dlm_dirtable;
  51. struct dlm_direntry;
  52. struct dlm_recover;
  53. struct dlm_header;
  54. struct dlm_message;
  55. struct dlm_rcom;
  56. struct dlm_mhandle;
  57. #define log_print(fmt, args...) \
  58. printk(KERN_ERR "dlm: "fmt"\n" , ##args)
  59. #define log_error(ls, fmt, args...) \
  60. printk(KERN_ERR "dlm: %s: " fmt "\n", (ls)->ls_name , ##args)
  61. #ifdef DLM_LOG_DEBUG
  62. #define log_debug(ls, fmt, args...) log_error(ls, fmt, ##args)
  63. #else
  64. #define log_debug(ls, fmt, args...)
  65. #endif
  66. #define DLM_ASSERT(x, do) \
  67. { \
  68. if (!(x)) \
  69. { \
  70. printk(KERN_ERR "\nDLM: Assertion failed on line %d of file %s\n" \
  71. "DLM: assertion: \"%s\"\n" \
  72. "DLM: time = %lu\n", \
  73. __LINE__, __FILE__, #x, jiffies); \
  74. {do} \
  75. printk("\n"); \
  76. BUG(); \
  77. panic("DLM: Record message above and reboot.\n"); \
  78. } \
  79. }
  80. struct dlm_direntry {
  81. struct list_head list;
  82. uint32_t master_nodeid;
  83. uint16_t length;
  84. char name[1];
  85. };
  86. struct dlm_dirtable {
  87. struct list_head list;
  88. rwlock_t lock;
  89. };
  90. struct dlm_rsbtable {
  91. struct list_head list;
  92. struct list_head toss;
  93. rwlock_t lock;
  94. };
  95. struct dlm_lkbtable {
  96. struct list_head list;
  97. rwlock_t lock;
  98. uint16_t counter;
  99. };
  100. /*
  101. * Lockspace member (per node in a ls)
  102. */
  103. struct dlm_member {
  104. struct list_head list;
  105. int nodeid;
  106. int weight;
  107. };
  108. /*
  109. * Save and manage recovery state for a lockspace.
  110. */
  111. struct dlm_recover {
  112. struct list_head list;
  113. int *nodeids;
  114. int node_count;
  115. uint64_t seq;
  116. };
  117. /*
  118. * Pass input args to second stage locking function.
  119. */
  120. struct dlm_args {
  121. uint32_t flags;
  122. void *astaddr;
  123. long astparam;
  124. void *bastaddr;
  125. int mode;
  126. struct dlm_lksb *lksb;
  127. struct dlm_range *range;
  128. };
  129. /*
  130. * Lock block
  131. *
  132. * A lock can be one of three types:
  133. *
  134. * local copy lock is mastered locally
  135. * (lkb_nodeid is zero and DLM_LKF_MSTCPY is not set)
  136. * process copy lock is mastered on a remote node
  137. * (lkb_nodeid is non-zero and DLM_LKF_MSTCPY is not set)
  138. * master copy master node's copy of a lock owned by remote node
  139. * (lkb_nodeid is non-zero and DLM_LKF_MSTCPY is set)
  140. *
  141. * lkb_exflags: a copy of the most recent flags arg provided to dlm_lock or
  142. * dlm_unlock. The dlm does not modify these or use any private flags in
  143. * this field; it only contains DLM_LKF_ flags from dlm.h. These flags
  144. * are sent as-is to the remote master when the lock is remote.
  145. *
  146. * lkb_flags: internal dlm flags (DLM_IFL_ prefix) from dlm_internal.h.
  147. * Some internal flags are shared between the master and process nodes;
  148. * these shared flags are kept in the lower two bytes. One of these
  149. * flags set on the master copy will be propagated to the process copy
  150. * and v.v. Other internal flags are private to the master or process
  151. * node (e.g. DLM_IFL_MSTCPY). These are kept in the high two bytes.
  152. *
  153. * lkb_sbflags: status block flags. These flags are copied directly into
  154. * the caller's lksb.sb_flags prior to the dlm_lock/dlm_unlock completion
  155. * ast. All defined in dlm.h with DLM_SBF_ prefix.
  156. *
  157. * lkb_status: the lock status indicates which rsb queue the lock is
  158. * on, grant, convert, or wait. DLM_LKSTS_ WAITING/GRANTED/CONVERT
  159. *
  160. * lkb_wait_type: the dlm message type (DLM_MSG_ prefix) for which a
  161. * reply is needed. Only set when the lkb is on the lockspace waiters
  162. * list awaiting a reply from a remote node.
  163. *
  164. * lkb_nodeid: when the lkb is a local copy, nodeid is 0; when the lkb
  165. * is a master copy, nodeid specifies the remote lock holder, when the
  166. * lkb is a process copy, the nodeid specifies the lock master.
  167. */
  168. /* lkb_ast_type */
  169. #define AST_COMP 1
  170. #define AST_BAST 2
  171. /* lkb_range[] */
  172. #define GR_RANGE_START 0
  173. #define GR_RANGE_END 1
  174. #define RQ_RANGE_START 2
  175. #define RQ_RANGE_END 3
  176. /* lkb_status */
  177. #define DLM_LKSTS_WAITING 1
  178. #define DLM_LKSTS_GRANTED 2
  179. #define DLM_LKSTS_CONVERT 3
  180. /* lkb_flags */
  181. #define DLM_IFL_MSTCPY 0x00010000
  182. #define DLM_IFL_RESEND 0x00020000
  183. #define DLM_IFL_RANGE 0x00000001
  184. struct dlm_lkb {
  185. struct dlm_rsb *lkb_resource; /* the rsb */
  186. struct kref lkb_ref;
  187. int lkb_nodeid; /* copied from rsb */
  188. int lkb_ownpid; /* pid of lock owner */
  189. uint32_t lkb_id; /* our lock ID */
  190. uint32_t lkb_remid; /* lock ID on remote partner */
  191. uint32_t lkb_exflags; /* external flags from caller */
  192. uint32_t lkb_sbflags; /* lksb flags */
  193. uint32_t lkb_flags; /* internal flags */
  194. uint32_t lkb_lvbseq; /* lvb sequence number */
  195. int8_t lkb_status; /* granted, waiting, convert */
  196. int8_t lkb_rqmode; /* requested lock mode */
  197. int8_t lkb_grmode; /* granted lock mode */
  198. int8_t lkb_bastmode; /* requested mode */
  199. int8_t lkb_highbast; /* highest mode bast sent for */
  200. int8_t lkb_wait_type; /* type of reply waiting for */
  201. int8_t lkb_ast_type; /* type of ast queued for */
  202. struct list_head lkb_idtbl_list; /* lockspace lkbtbl */
  203. struct list_head lkb_statequeue; /* rsb g/c/w list */
  204. struct list_head lkb_rsb_lookup; /* waiting for rsb lookup */
  205. struct list_head lkb_wait_reply; /* waiting for remote reply */
  206. struct list_head lkb_astqueue; /* need ast to be sent */
  207. uint64_t *lkb_range; /* array of gr/rq ranges */
  208. char *lkb_lvbptr;
  209. struct dlm_lksb *lkb_lksb; /* caller's status block */
  210. void *lkb_astaddr; /* caller's ast function */
  211. void *lkb_bastaddr; /* caller's bast function */
  212. long lkb_astparam; /* caller's ast arg */
  213. };
  214. struct dlm_rsb {
  215. struct dlm_ls *res_ls; /* the lockspace */
  216. struct kref res_ref;
  217. struct mutex res_mutex;
  218. unsigned long res_flags;
  219. int res_length; /* length of rsb name */
  220. int res_nodeid;
  221. uint32_t res_lvbseq;
  222. uint32_t res_hash;
  223. uint32_t res_bucket; /* rsbtbl */
  224. unsigned long res_toss_time;
  225. uint32_t res_first_lkid;
  226. struct list_head res_lookup; /* lkbs waiting on first */
  227. struct list_head res_hashchain; /* rsbtbl */
  228. struct list_head res_grantqueue;
  229. struct list_head res_convertqueue;
  230. struct list_head res_waitqueue;
  231. struct list_head res_root_list; /* used for recovery */
  232. struct list_head res_recover_list; /* used for recovery */
  233. int res_recover_locks_count;
  234. char *res_lvbptr;
  235. char res_name[1];
  236. };
  237. /* find_rsb() flags */
  238. #define R_MASTER 1 /* only return rsb if it's a master */
  239. #define R_CREATE 2 /* create/add rsb if not found */
  240. /* rsb_flags */
  241. enum rsb_flags {
  242. RSB_MASTER_UNCERTAIN,
  243. RSB_VALNOTVALID,
  244. RSB_VALNOTVALID_PREV,
  245. RSB_NEW_MASTER,
  246. RSB_NEW_MASTER2,
  247. RSB_RECOVER_CONVERT,
  248. };
  249. static inline void rsb_set_flag(struct dlm_rsb *r, enum rsb_flags flag)
  250. {
  251. __set_bit(flag, &r->res_flags);
  252. }
  253. static inline void rsb_clear_flag(struct dlm_rsb *r, enum rsb_flags flag)
  254. {
  255. __clear_bit(flag, &r->res_flags);
  256. }
  257. static inline int rsb_flag(struct dlm_rsb *r, enum rsb_flags flag)
  258. {
  259. return test_bit(flag, &r->res_flags);
  260. }
  261. /* dlm_header is first element of all structs sent between nodes */
  262. #define DLM_HEADER_MAJOR 0x00020000
  263. #define DLM_HEADER_MINOR 0x00000001
  264. #define DLM_MSG 1
  265. #define DLM_RCOM 2
  266. struct dlm_header {
  267. uint32_t h_version;
  268. uint32_t h_lockspace;
  269. uint32_t h_nodeid; /* nodeid of sender */
  270. uint16_t h_length;
  271. uint8_t h_cmd; /* DLM_MSG, DLM_RCOM */
  272. uint8_t h_pad;
  273. };
  274. #define DLM_MSG_REQUEST 1
  275. #define DLM_MSG_CONVERT 2
  276. #define DLM_MSG_UNLOCK 3
  277. #define DLM_MSG_CANCEL 4
  278. #define DLM_MSG_REQUEST_REPLY 5
  279. #define DLM_MSG_CONVERT_REPLY 6
  280. #define DLM_MSG_UNLOCK_REPLY 7
  281. #define DLM_MSG_CANCEL_REPLY 8
  282. #define DLM_MSG_GRANT 9
  283. #define DLM_MSG_BAST 10
  284. #define DLM_MSG_LOOKUP 11
  285. #define DLM_MSG_REMOVE 12
  286. #define DLM_MSG_LOOKUP_REPLY 13
  287. struct dlm_message {
  288. struct dlm_header m_header;
  289. uint32_t m_type; /* DLM_MSG_ */
  290. uint32_t m_nodeid;
  291. uint32_t m_pid;
  292. uint32_t m_lkid; /* lkid on sender */
  293. uint32_t m_remid; /* lkid on receiver */
  294. uint32_t m_parent_lkid;
  295. uint32_t m_parent_remid;
  296. uint32_t m_exflags;
  297. uint32_t m_sbflags;
  298. uint32_t m_flags;
  299. uint32_t m_lvbseq;
  300. uint32_t m_hash;
  301. int m_status;
  302. int m_grmode;
  303. int m_rqmode;
  304. int m_bastmode;
  305. int m_asts;
  306. int m_result; /* 0 or -EXXX */
  307. uint64_t m_range[2];
  308. char m_extra[0]; /* name or lvb */
  309. };
  310. #define DLM_RS_NODES 0x00000001
  311. #define DLM_RS_NODES_ALL 0x00000002
  312. #define DLM_RS_DIR 0x00000004
  313. #define DLM_RS_DIR_ALL 0x00000008
  314. #define DLM_RS_LOCKS 0x00000010
  315. #define DLM_RS_LOCKS_ALL 0x00000020
  316. #define DLM_RS_DONE 0x00000040
  317. #define DLM_RS_DONE_ALL 0x00000080
  318. #define DLM_RCOM_STATUS 1
  319. #define DLM_RCOM_NAMES 2
  320. #define DLM_RCOM_LOOKUP 3
  321. #define DLM_RCOM_LOCK 4
  322. #define DLM_RCOM_STATUS_REPLY 5
  323. #define DLM_RCOM_NAMES_REPLY 6
  324. #define DLM_RCOM_LOOKUP_REPLY 7
  325. #define DLM_RCOM_LOCK_REPLY 8
  326. struct dlm_rcom {
  327. struct dlm_header rc_header;
  328. uint32_t rc_type; /* DLM_RCOM_ */
  329. int rc_result; /* multi-purpose */
  330. uint64_t rc_id; /* match reply with request */
  331. char rc_buf[0];
  332. };
  333. struct rcom_config {
  334. uint32_t rf_lvblen;
  335. uint32_t rf_lsflags;
  336. uint64_t rf_unused;
  337. };
  338. struct rcom_lock {
  339. uint32_t rl_ownpid;
  340. uint32_t rl_lkid;
  341. uint32_t rl_remid;
  342. uint32_t rl_parent_lkid;
  343. uint32_t rl_parent_remid;
  344. uint32_t rl_exflags;
  345. uint32_t rl_flags;
  346. uint32_t rl_lvbseq;
  347. int rl_result;
  348. int8_t rl_rqmode;
  349. int8_t rl_grmode;
  350. int8_t rl_status;
  351. int8_t rl_asts;
  352. uint16_t rl_wait_type;
  353. uint16_t rl_namelen;
  354. uint64_t rl_range[4];
  355. char rl_name[DLM_RESNAME_MAXLEN];
  356. char rl_lvb[0];
  357. };
  358. struct dlm_ls {
  359. struct list_head ls_list; /* list of lockspaces */
  360. uint32_t ls_global_id; /* global unique lockspace ID */
  361. uint32_t ls_exflags;
  362. int ls_lvblen;
  363. int ls_count; /* reference count */
  364. unsigned long ls_flags; /* LSFL_ */
  365. struct kobject ls_kobj;
  366. struct dlm_rsbtable *ls_rsbtbl;
  367. uint32_t ls_rsbtbl_size;
  368. struct dlm_lkbtable *ls_lkbtbl;
  369. uint32_t ls_lkbtbl_size;
  370. struct dlm_dirtable *ls_dirtbl;
  371. uint32_t ls_dirtbl_size;
  372. struct mutex ls_waiters_mutex;
  373. struct list_head ls_waiters; /* lkbs needing a reply */
  374. struct list_head ls_nodes; /* current nodes in ls */
  375. struct list_head ls_nodes_gone; /* dead node list, recovery */
  376. int ls_num_nodes; /* number of nodes in ls */
  377. int ls_low_nodeid;
  378. int ls_total_weight;
  379. int *ls_node_array;
  380. struct dlm_rsb ls_stub_rsb; /* for returning errors */
  381. struct dlm_lkb ls_stub_lkb; /* for returning errors */
  382. struct dlm_message ls_stub_ms; /* for faking a reply */
  383. struct dentry *ls_debug_dentry; /* debugfs */
  384. wait_queue_head_t ls_uevent_wait; /* user part of join/leave */
  385. int ls_uevent_result;
  386. /* recovery related */
  387. struct timer_list ls_timer;
  388. struct task_struct *ls_recoverd_task;
  389. struct mutex ls_recoverd_active;
  390. spinlock_t ls_recover_lock;
  391. uint32_t ls_recover_status; /* DLM_RS_ */
  392. uint64_t ls_recover_seq;
  393. struct dlm_recover *ls_recover_args;
  394. struct rw_semaphore ls_in_recovery; /* block local requests */
  395. struct list_head ls_requestqueue;/* queue remote requests */
  396. struct mutex ls_requestqueue_mutex;
  397. char *ls_recover_buf;
  398. struct list_head ls_recover_list;
  399. spinlock_t ls_recover_list_lock;
  400. int ls_recover_list_count;
  401. wait_queue_head_t ls_wait_general;
  402. struct list_head ls_root_list; /* root resources */
  403. struct rw_semaphore ls_root_sem; /* protect root_list */
  404. int ls_namelen;
  405. char ls_name[1];
  406. };
  407. #define LSFL_WORK 0
  408. #define LSFL_RUNNING 1
  409. #define LSFL_RECOVERY_STOP 2
  410. #define LSFL_RCOM_READY 3
  411. #define LSFL_UEVENT_WAIT 4
  412. static inline int dlm_locking_stopped(struct dlm_ls *ls)
  413. {
  414. return !test_bit(LSFL_RUNNING, &ls->ls_flags);
  415. }
  416. static inline int dlm_recovery_stopped(struct dlm_ls *ls)
  417. {
  418. return test_bit(LSFL_RECOVERY_STOP, &ls->ls_flags);
  419. }
  420. static inline int dlm_no_directory(struct dlm_ls *ls)
  421. {
  422. return (ls->ls_exflags & DLM_LSFL_NODIR) ? 1 : 0;
  423. }
  424. #endif /* __DLM_INTERNAL_DOT_H__ */