dlm_internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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/miscdevice.h>
  36. #include <linux/mutex.h>
  37. #include <asm/semaphore.h>
  38. #include <asm/uaccess.h>
  39. #include <linux/dlm.h>
  40. #define DLM_LOCKSPACE_LEN 64
  41. /* Size of the temp buffer midcomms allocates on the stack.
  42. We try to make this large enough so most messages fit.
  43. FIXME: should sctp make this unnecessary? */
  44. #define DLM_INBUF_LEN 148
  45. struct dlm_ls;
  46. struct dlm_lkb;
  47. struct dlm_rsb;
  48. struct dlm_member;
  49. struct dlm_lkbtable;
  50. struct dlm_rsbtable;
  51. struct dlm_dirtable;
  52. struct dlm_direntry;
  53. struct dlm_recover;
  54. struct dlm_header;
  55. struct dlm_message;
  56. struct dlm_rcom;
  57. struct dlm_mhandle;
  58. #define log_print(fmt, args...) \
  59. printk(KERN_ERR "dlm: "fmt"\n" , ##args)
  60. #define log_error(ls, fmt, args...) \
  61. printk(KERN_ERR "dlm: %s: " fmt "\n", (ls)->ls_name , ##args)
  62. #define DLM_LOG_DEBUG
  63. #ifdef DLM_LOG_DEBUG
  64. #define log_debug(ls, fmt, args...) log_error(ls, fmt, ##args)
  65. #else
  66. #define log_debug(ls, fmt, args...)
  67. #endif
  68. #define DLM_ASSERT(x, do) \
  69. { \
  70. if (!(x)) \
  71. { \
  72. printk(KERN_ERR "\nDLM: Assertion failed on line %d of file %s\n" \
  73. "DLM: assertion: \"%s\"\n" \
  74. "DLM: time = %lu\n", \
  75. __LINE__, __FILE__, #x, jiffies); \
  76. {do} \
  77. printk("\n"); \
  78. BUG(); \
  79. panic("DLM: Record message above and reboot.\n"); \
  80. } \
  81. }
  82. #define DLM_FAKE_USER_AST ERR_PTR(-EINVAL)
  83. struct dlm_direntry {
  84. struct list_head list;
  85. uint32_t master_nodeid;
  86. uint16_t length;
  87. char name[1];
  88. };
  89. struct dlm_dirtable {
  90. struct list_head list;
  91. rwlock_t lock;
  92. };
  93. struct dlm_rsbtable {
  94. struct list_head list;
  95. struct list_head toss;
  96. rwlock_t lock;
  97. };
  98. struct dlm_lkbtable {
  99. struct list_head list;
  100. rwlock_t lock;
  101. uint16_t counter;
  102. };
  103. /*
  104. * Lockspace member (per node in a ls)
  105. */
  106. struct dlm_member {
  107. struct list_head list;
  108. int nodeid;
  109. int weight;
  110. };
  111. /*
  112. * Save and manage recovery state for a lockspace.
  113. */
  114. struct dlm_recover {
  115. struct list_head list;
  116. int *nodeids;
  117. int node_count;
  118. uint64_t seq;
  119. };
  120. /*
  121. * Pass input args to second stage locking function.
  122. */
  123. struct dlm_args {
  124. uint32_t flags;
  125. void *astaddr;
  126. long astparam;
  127. void *bastaddr;
  128. int mode;
  129. struct dlm_lksb *lksb;
  130. };
  131. /*
  132. * Lock block
  133. *
  134. * A lock can be one of three types:
  135. *
  136. * local copy lock is mastered locally
  137. * (lkb_nodeid is zero and DLM_LKF_MSTCPY is not set)
  138. * process copy lock is mastered on a remote node
  139. * (lkb_nodeid is non-zero and DLM_LKF_MSTCPY is not set)
  140. * master copy master node's copy of a lock owned by remote node
  141. * (lkb_nodeid is non-zero and DLM_LKF_MSTCPY is set)
  142. *
  143. * lkb_exflags: a copy of the most recent flags arg provided to dlm_lock or
  144. * dlm_unlock. The dlm does not modify these or use any private flags in
  145. * this field; it only contains DLM_LKF_ flags from dlm.h. These flags
  146. * are sent as-is to the remote master when the lock is remote.
  147. *
  148. * lkb_flags: internal dlm flags (DLM_IFL_ prefix) from dlm_internal.h.
  149. * Some internal flags are shared between the master and process nodes;
  150. * these shared flags are kept in the lower two bytes. One of these
  151. * flags set on the master copy will be propagated to the process copy
  152. * and v.v. Other internal flags are private to the master or process
  153. * node (e.g. DLM_IFL_MSTCPY). These are kept in the high two bytes.
  154. *
  155. * lkb_sbflags: status block flags. These flags are copied directly into
  156. * the caller's lksb.sb_flags prior to the dlm_lock/dlm_unlock completion
  157. * ast. All defined in dlm.h with DLM_SBF_ prefix.
  158. *
  159. * lkb_status: the lock status indicates which rsb queue the lock is
  160. * on, grant, convert, or wait. DLM_LKSTS_ WAITING/GRANTED/CONVERT
  161. *
  162. * lkb_wait_type: the dlm message type (DLM_MSG_ prefix) for which a
  163. * reply is needed. Only set when the lkb is on the lockspace waiters
  164. * list awaiting a reply from a remote node.
  165. *
  166. * lkb_nodeid: when the lkb is a local copy, nodeid is 0; when the lkb
  167. * is a master copy, nodeid specifies the remote lock holder, when the
  168. * lkb is a process copy, the nodeid specifies the lock master.
  169. */
  170. /* lkb_ast_type */
  171. #define AST_COMP 1
  172. #define AST_BAST 2
  173. /* lkb_status */
  174. #define DLM_LKSTS_WAITING 1
  175. #define DLM_LKSTS_GRANTED 2
  176. #define DLM_LKSTS_CONVERT 3
  177. /* lkb_flags */
  178. #define DLM_IFL_MSTCPY 0x00010000
  179. #define DLM_IFL_RESEND 0x00020000
  180. #define DLM_IFL_DEAD 0x00040000
  181. #define DLM_IFL_USER 0x00000001
  182. #define DLM_IFL_ORPHAN 0x00000002
  183. struct dlm_lkb {
  184. struct dlm_rsb *lkb_resource; /* the rsb */
  185. struct kref lkb_ref;
  186. int lkb_nodeid; /* copied from rsb */
  187. int lkb_ownpid; /* pid of lock owner */
  188. uint32_t lkb_id; /* our lock ID */
  189. uint32_t lkb_remid; /* lock ID on remote partner */
  190. uint32_t lkb_exflags; /* external flags from caller */
  191. uint32_t lkb_sbflags; /* lksb flags */
  192. uint32_t lkb_flags; /* internal flags */
  193. uint32_t lkb_lvbseq; /* lvb sequence number */
  194. int8_t lkb_status; /* granted, waiting, convert */
  195. int8_t lkb_rqmode; /* requested lock mode */
  196. int8_t lkb_grmode; /* granted lock mode */
  197. int8_t lkb_bastmode; /* requested mode */
  198. int8_t lkb_highbast; /* highest mode bast sent for */
  199. int8_t lkb_wait_type; /* type of reply waiting for */
  200. int8_t lkb_ast_type; /* type of ast queued for */
  201. struct list_head lkb_idtbl_list; /* lockspace lkbtbl */
  202. struct list_head lkb_statequeue; /* rsb g/c/w list */
  203. struct list_head lkb_rsb_lookup; /* waiting for rsb lookup */
  204. struct list_head lkb_wait_reply; /* waiting for remote reply */
  205. struct list_head lkb_astqueue; /* need ast to be sent */
  206. struct list_head lkb_ownqueue; /* list of locks for a process */
  207. char *lkb_lvbptr;
  208. struct dlm_lksb *lkb_lksb; /* caller's status block */
  209. void *lkb_astaddr; /* caller's ast function */
  210. void *lkb_bastaddr; /* caller's bast function */
  211. long lkb_astparam; /* caller's ast arg */
  212. };
  213. struct dlm_rsb {
  214. struct dlm_ls *res_ls; /* the lockspace */
  215. struct kref res_ref;
  216. struct mutex res_mutex;
  217. unsigned long res_flags;
  218. int res_length; /* length of rsb name */
  219. int res_nodeid;
  220. uint32_t res_lvbseq;
  221. uint32_t res_hash;
  222. uint32_t res_bucket; /* rsbtbl */
  223. unsigned long res_toss_time;
  224. uint32_t res_first_lkid;
  225. struct list_head res_lookup; /* lkbs waiting on first */
  226. struct list_head res_hashchain; /* rsbtbl */
  227. struct list_head res_grantqueue;
  228. struct list_head res_convertqueue;
  229. struct list_head res_waitqueue;
  230. struct list_head res_root_list; /* used for recovery */
  231. struct list_head res_recover_list; /* used for recovery */
  232. int res_recover_locks_count;
  233. char *res_lvbptr;
  234. char res_name[1];
  235. };
  236. /* find_rsb() flags */
  237. #define R_MASTER 1 /* only return rsb if it's a master */
  238. #define R_CREATE 2 /* create/add rsb if not found */
  239. /* rsb_flags */
  240. enum rsb_flags {
  241. RSB_MASTER_UNCERTAIN,
  242. RSB_VALNOTVALID,
  243. RSB_VALNOTVALID_PREV,
  244. RSB_NEW_MASTER,
  245. RSB_NEW_MASTER2,
  246. RSB_RECOVER_CONVERT,
  247. RSB_LOCKS_PURGED,
  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. char m_extra[0]; /* name or lvb */
  308. };
  309. #define DLM_RS_NODES 0x00000001
  310. #define DLM_RS_NODES_ALL 0x00000002
  311. #define DLM_RS_DIR 0x00000004
  312. #define DLM_RS_DIR_ALL 0x00000008
  313. #define DLM_RS_LOCKS 0x00000010
  314. #define DLM_RS_LOCKS_ALL 0x00000020
  315. #define DLM_RS_DONE 0x00000040
  316. #define DLM_RS_DONE_ALL 0x00000080
  317. #define DLM_RCOM_STATUS 1
  318. #define DLM_RCOM_NAMES 2
  319. #define DLM_RCOM_LOOKUP 3
  320. #define DLM_RCOM_LOCK 4
  321. #define DLM_RCOM_STATUS_REPLY 5
  322. #define DLM_RCOM_NAMES_REPLY 6
  323. #define DLM_RCOM_LOOKUP_REPLY 7
  324. #define DLM_RCOM_LOCK_REPLY 8
  325. struct dlm_rcom {
  326. struct dlm_header rc_header;
  327. uint32_t rc_type; /* DLM_RCOM_ */
  328. int rc_result; /* multi-purpose */
  329. uint64_t rc_id; /* match reply with request */
  330. char rc_buf[0];
  331. };
  332. struct rcom_config {
  333. uint32_t rf_lvblen;
  334. uint32_t rf_lsflags;
  335. uint64_t rf_unused;
  336. };
  337. struct rcom_lock {
  338. uint32_t rl_ownpid;
  339. uint32_t rl_lkid;
  340. uint32_t rl_remid;
  341. uint32_t rl_parent_lkid;
  342. uint32_t rl_parent_remid;
  343. uint32_t rl_exflags;
  344. uint32_t rl_flags;
  345. uint32_t rl_lvbseq;
  346. int rl_result;
  347. int8_t rl_rqmode;
  348. int8_t rl_grmode;
  349. int8_t rl_status;
  350. int8_t rl_asts;
  351. uint16_t rl_wait_type;
  352. uint16_t rl_namelen;
  353. char rl_name[DLM_RESNAME_MAXLEN];
  354. char rl_lvb[0];
  355. };
  356. struct dlm_ls {
  357. struct list_head ls_list; /* list of lockspaces */
  358. dlm_lockspace_t *ls_local_handle;
  359. uint32_t ls_global_id; /* global unique lockspace ID */
  360. uint32_t ls_exflags;
  361. int ls_lvblen;
  362. int ls_count; /* reference count */
  363. unsigned long ls_flags; /* LSFL_ */
  364. struct kobject ls_kobj;
  365. struct dlm_rsbtable *ls_rsbtbl;
  366. uint32_t ls_rsbtbl_size;
  367. struct dlm_lkbtable *ls_lkbtbl;
  368. uint32_t ls_lkbtbl_size;
  369. struct dlm_dirtable *ls_dirtbl;
  370. uint32_t ls_dirtbl_size;
  371. struct mutex ls_waiters_mutex;
  372. struct list_head ls_waiters; /* lkbs needing a reply */
  373. struct list_head ls_nodes; /* current nodes in ls */
  374. struct list_head ls_nodes_gone; /* dead node list, recovery */
  375. int ls_num_nodes; /* number of nodes in ls */
  376. int ls_low_nodeid;
  377. int ls_total_weight;
  378. int *ls_node_array;
  379. struct dlm_rsb ls_stub_rsb; /* for returning errors */
  380. struct dlm_lkb ls_stub_lkb; /* for returning errors */
  381. struct dlm_message ls_stub_ms; /* for faking a reply */
  382. struct dentry *ls_debug_rsb_dentry; /* debugfs */
  383. struct dentry *ls_debug_waiters_dentry; /* debugfs */
  384. wait_queue_head_t ls_uevent_wait; /* user part of join/leave */
  385. int ls_uevent_result;
  386. struct miscdevice ls_device;
  387. /* recovery related */
  388. struct timer_list ls_timer;
  389. struct task_struct *ls_recoverd_task;
  390. struct mutex ls_recoverd_active;
  391. spinlock_t ls_recover_lock;
  392. uint32_t ls_recover_status; /* DLM_RS_ */
  393. uint64_t ls_recover_seq;
  394. struct dlm_recover *ls_recover_args;
  395. struct rw_semaphore ls_in_recovery; /* block local requests */
  396. struct list_head ls_requestqueue;/* queue remote requests */
  397. struct mutex ls_requestqueue_mutex;
  398. char *ls_recover_buf;
  399. int ls_recover_nodeid; /* for debugging */
  400. uint64_t ls_rcom_seq;
  401. struct list_head ls_recover_list;
  402. spinlock_t ls_recover_list_lock;
  403. int ls_recover_list_count;
  404. wait_queue_head_t ls_wait_general;
  405. struct mutex ls_clear_proc_locks;
  406. struct list_head ls_root_list; /* root resources */
  407. struct rw_semaphore ls_root_sem; /* protect root_list */
  408. int ls_namelen;
  409. char ls_name[1];
  410. };
  411. #define LSFL_WORK 0
  412. #define LSFL_RUNNING 1
  413. #define LSFL_RECOVERY_STOP 2
  414. #define LSFL_RCOM_READY 3
  415. #define LSFL_UEVENT_WAIT 4
  416. /* much of this is just saving user space pointers associated with the
  417. lock that we pass back to the user lib with an ast */
  418. struct dlm_user_args {
  419. struct dlm_user_proc *proc; /* each process that opens the lockspace
  420. device has private data
  421. (dlm_user_proc) on the struct file,
  422. the process's locks point back to it*/
  423. struct dlm_lksb lksb;
  424. int old_mode;
  425. int update_user_lvb;
  426. struct dlm_lksb __user *user_lksb;
  427. void __user *castparam;
  428. void __user *castaddr;
  429. void __user *bastparam;
  430. void __user *bastaddr;
  431. };
  432. #define DLM_PROC_FLAGS_CLOSING 1
  433. #define DLM_PROC_FLAGS_COMPAT 2
  434. /* locks list is kept so we can remove all a process's locks when it
  435. exits (or orphan those that are persistent) */
  436. struct dlm_user_proc {
  437. dlm_lockspace_t *lockspace;
  438. unsigned long flags; /* DLM_PROC_FLAGS */
  439. struct list_head asts;
  440. spinlock_t asts_spin;
  441. struct list_head locks;
  442. spinlock_t locks_spin;
  443. wait_queue_head_t wait;
  444. };
  445. static inline int dlm_locking_stopped(struct dlm_ls *ls)
  446. {
  447. return !test_bit(LSFL_RUNNING, &ls->ls_flags);
  448. }
  449. static inline int dlm_recovery_stopped(struct dlm_ls *ls)
  450. {
  451. return test_bit(LSFL_RECOVERY_STOP, &ls->ls_flags);
  452. }
  453. static inline int dlm_no_directory(struct dlm_ls *ls)
  454. {
  455. return (ls->ls_exflags & DLM_LSFL_NODIR) ? 1 : 0;
  456. }
  457. #endif /* __DLM_INTERNAL_DOT_H__ */