dlm_internal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2007 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. #include "config.h"
  41. #define DLM_LOCKSPACE_LEN 64
  42. /* Size of the temp buffer midcomms allocates on the stack.
  43. We try to make this large enough so most messages fit.
  44. FIXME: should sctp make this unnecessary? */
  45. #define DLM_INBUF_LEN 148
  46. struct dlm_ls;
  47. struct dlm_lkb;
  48. struct dlm_rsb;
  49. struct dlm_member;
  50. struct dlm_lkbtable;
  51. struct dlm_rsbtable;
  52. struct dlm_dirtable;
  53. struct dlm_direntry;
  54. struct dlm_recover;
  55. struct dlm_header;
  56. struct dlm_message;
  57. struct dlm_rcom;
  58. struct dlm_mhandle;
  59. #define log_print(fmt, args...) \
  60. printk(KERN_ERR "dlm: "fmt"\n" , ##args)
  61. #define log_error(ls, fmt, args...) \
  62. printk(KERN_ERR "dlm: %s: " fmt "\n", (ls)->ls_name , ##args)
  63. #define log_debug(ls, fmt, args...) \
  64. do { \
  65. if (dlm_config.ci_log_debug) \
  66. printk(KERN_DEBUG "dlm: %s: " fmt "\n", \
  67. (ls)->ls_name , ##args); \
  68. } while (0)
  69. #define DLM_ASSERT(x, do) \
  70. { \
  71. if (!(x)) \
  72. { \
  73. printk(KERN_ERR "\nDLM: Assertion failed on line %d of file %s\n" \
  74. "DLM: assertion: \"%s\"\n" \
  75. "DLM: time = %lu\n", \
  76. __LINE__, __FILE__, #x, jiffies); \
  77. {do} \
  78. printk("\n"); \
  79. BUG(); \
  80. panic("DLM: Record message above and reboot.\n"); \
  81. } \
  82. }
  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; /* nodeids of all members */
  117. int node_count;
  118. int *new; /* nodeids of new members */
  119. int new_count;
  120. uint64_t seq;
  121. };
  122. /*
  123. * Pass input args to second stage locking function.
  124. */
  125. struct dlm_args {
  126. uint32_t flags;
  127. void (*astfn) (void *astparam);
  128. void *astparam;
  129. void (*bastfn) (void *astparam, int mode);
  130. int mode;
  131. struct dlm_lksb *lksb;
  132. unsigned long timeout;
  133. };
  134. /*
  135. * Lock block
  136. *
  137. * A lock can be one of three types:
  138. *
  139. * local copy lock is mastered locally
  140. * (lkb_nodeid is zero and DLM_LKF_MSTCPY is not set)
  141. * process copy lock is mastered on a remote node
  142. * (lkb_nodeid is non-zero and DLM_LKF_MSTCPY is not set)
  143. * master copy master node's copy of a lock owned by remote node
  144. * (lkb_nodeid is non-zero and DLM_LKF_MSTCPY is set)
  145. *
  146. * lkb_exflags: a copy of the most recent flags arg provided to dlm_lock or
  147. * dlm_unlock. The dlm does not modify these or use any private flags in
  148. * this field; it only contains DLM_LKF_ flags from dlm.h. These flags
  149. * are sent as-is to the remote master when the lock is remote.
  150. *
  151. * lkb_flags: internal dlm flags (DLM_IFL_ prefix) from dlm_internal.h.
  152. * Some internal flags are shared between the master and process nodes;
  153. * these shared flags are kept in the lower two bytes. One of these
  154. * flags set on the master copy will be propagated to the process copy
  155. * and v.v. Other internal flags are private to the master or process
  156. * node (e.g. DLM_IFL_MSTCPY). These are kept in the high two bytes.
  157. *
  158. * lkb_sbflags: status block flags. These flags are copied directly into
  159. * the caller's lksb.sb_flags prior to the dlm_lock/dlm_unlock completion
  160. * ast. All defined in dlm.h with DLM_SBF_ prefix.
  161. *
  162. * lkb_status: the lock status indicates which rsb queue the lock is
  163. * on, grant, convert, or wait. DLM_LKSTS_ WAITING/GRANTED/CONVERT
  164. *
  165. * lkb_wait_type: the dlm message type (DLM_MSG_ prefix) for which a
  166. * reply is needed. Only set when the lkb is on the lockspace waiters
  167. * list awaiting a reply from a remote node.
  168. *
  169. * lkb_nodeid: when the lkb is a local copy, nodeid is 0; when the lkb
  170. * is a master copy, nodeid specifies the remote lock holder, when the
  171. * lkb is a process copy, the nodeid specifies the lock master.
  172. */
  173. /* lkb_ast_type */
  174. #define AST_COMP 1
  175. #define AST_BAST 2
  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_DEAD 0x00040000
  184. #define DLM_IFL_OVERLAP_UNLOCK 0x00080000
  185. #define DLM_IFL_OVERLAP_CANCEL 0x00100000
  186. #define DLM_IFL_ENDOFLIFE 0x00200000
  187. #define DLM_IFL_WATCH_TIMEWARN 0x00400000
  188. #define DLM_IFL_TIMEOUT_CANCEL 0x00800000
  189. #define DLM_IFL_DEADLOCK_CANCEL 0x01000000
  190. #define DLM_IFL_USER 0x00000001
  191. #define DLM_IFL_ORPHAN 0x00000002
  192. struct dlm_lkb {
  193. struct dlm_rsb *lkb_resource; /* the rsb */
  194. struct kref lkb_ref;
  195. int lkb_nodeid; /* copied from rsb */
  196. int lkb_ownpid; /* pid of lock owner */
  197. uint32_t lkb_id; /* our lock ID */
  198. uint32_t lkb_remid; /* lock ID on remote partner */
  199. uint32_t lkb_exflags; /* external flags from caller */
  200. uint32_t lkb_sbflags; /* lksb flags */
  201. uint32_t lkb_flags; /* internal flags */
  202. uint32_t lkb_lvbseq; /* lvb sequence number */
  203. int8_t lkb_status; /* granted, waiting, convert */
  204. int8_t lkb_rqmode; /* requested lock mode */
  205. int8_t lkb_grmode; /* granted lock mode */
  206. int8_t lkb_bastmode; /* requested mode */
  207. int8_t lkb_highbast; /* highest mode bast sent for */
  208. int8_t lkb_wait_type; /* type of reply waiting for */
  209. int8_t lkb_wait_count;
  210. int8_t lkb_ast_type; /* type of ast queued for */
  211. struct list_head lkb_idtbl_list; /* lockspace lkbtbl */
  212. struct list_head lkb_statequeue; /* rsb g/c/w list */
  213. struct list_head lkb_rsb_lookup; /* waiting for rsb lookup */
  214. struct list_head lkb_wait_reply; /* waiting for remote reply */
  215. struct list_head lkb_astqueue; /* need ast to be sent */
  216. struct list_head lkb_ownqueue; /* list of locks for a process */
  217. struct list_head lkb_time_list;
  218. unsigned long lkb_timestamp;
  219. unsigned long lkb_timeout_cs;
  220. char *lkb_lvbptr;
  221. struct dlm_lksb *lkb_lksb; /* caller's status block */
  222. void (*lkb_astfn) (void *astparam);
  223. void (*lkb_bastfn) (void *astparam, int mode);
  224. union {
  225. void *lkb_astparam; /* caller's ast arg */
  226. struct dlm_user_args *lkb_ua;
  227. };
  228. };
  229. struct dlm_rsb {
  230. struct dlm_ls *res_ls; /* the lockspace */
  231. struct kref res_ref;
  232. struct mutex res_mutex;
  233. unsigned long res_flags;
  234. int res_length; /* length of rsb name */
  235. int res_nodeid;
  236. uint32_t res_lvbseq;
  237. uint32_t res_hash;
  238. uint32_t res_bucket; /* rsbtbl */
  239. unsigned long res_toss_time;
  240. uint32_t res_first_lkid;
  241. struct list_head res_lookup; /* lkbs waiting on first */
  242. struct list_head res_hashchain; /* rsbtbl */
  243. struct list_head res_grantqueue;
  244. struct list_head res_convertqueue;
  245. struct list_head res_waitqueue;
  246. struct list_head res_root_list; /* used for recovery */
  247. struct list_head res_recover_list; /* used for recovery */
  248. int res_recover_locks_count;
  249. char *res_lvbptr;
  250. char res_name[1];
  251. };
  252. /* find_rsb() flags */
  253. #define R_MASTER 1 /* only return rsb if it's a master */
  254. #define R_CREATE 2 /* create/add rsb if not found */
  255. /* rsb_flags */
  256. enum rsb_flags {
  257. RSB_MASTER_UNCERTAIN,
  258. RSB_VALNOTVALID,
  259. RSB_VALNOTVALID_PREV,
  260. RSB_NEW_MASTER,
  261. RSB_NEW_MASTER2,
  262. RSB_RECOVER_CONVERT,
  263. RSB_LOCKS_PURGED,
  264. };
  265. static inline void rsb_set_flag(struct dlm_rsb *r, enum rsb_flags flag)
  266. {
  267. __set_bit(flag, &r->res_flags);
  268. }
  269. static inline void rsb_clear_flag(struct dlm_rsb *r, enum rsb_flags flag)
  270. {
  271. __clear_bit(flag, &r->res_flags);
  272. }
  273. static inline int rsb_flag(struct dlm_rsb *r, enum rsb_flags flag)
  274. {
  275. return test_bit(flag, &r->res_flags);
  276. }
  277. /* dlm_header is first element of all structs sent between nodes */
  278. #define DLM_HEADER_MAJOR 0x00030000
  279. #define DLM_HEADER_MINOR 0x00000000
  280. #define DLM_MSG 1
  281. #define DLM_RCOM 2
  282. struct dlm_header {
  283. uint32_t h_version;
  284. uint32_t h_lockspace;
  285. uint32_t h_nodeid; /* nodeid of sender */
  286. uint16_t h_length;
  287. uint8_t h_cmd; /* DLM_MSG, DLM_RCOM */
  288. uint8_t h_pad;
  289. };
  290. #define DLM_MSG_REQUEST 1
  291. #define DLM_MSG_CONVERT 2
  292. #define DLM_MSG_UNLOCK 3
  293. #define DLM_MSG_CANCEL 4
  294. #define DLM_MSG_REQUEST_REPLY 5
  295. #define DLM_MSG_CONVERT_REPLY 6
  296. #define DLM_MSG_UNLOCK_REPLY 7
  297. #define DLM_MSG_CANCEL_REPLY 8
  298. #define DLM_MSG_GRANT 9
  299. #define DLM_MSG_BAST 10
  300. #define DLM_MSG_LOOKUP 11
  301. #define DLM_MSG_REMOVE 12
  302. #define DLM_MSG_LOOKUP_REPLY 13
  303. #define DLM_MSG_PURGE 14
  304. struct dlm_message {
  305. struct dlm_header m_header;
  306. uint32_t m_type; /* DLM_MSG_ */
  307. uint32_t m_nodeid;
  308. uint32_t m_pid;
  309. uint32_t m_lkid; /* lkid on sender */
  310. uint32_t m_remid; /* lkid on receiver */
  311. uint32_t m_parent_lkid;
  312. uint32_t m_parent_remid;
  313. uint32_t m_exflags;
  314. uint32_t m_sbflags;
  315. uint32_t m_flags;
  316. uint32_t m_lvbseq;
  317. uint32_t m_hash;
  318. int m_status;
  319. int m_grmode;
  320. int m_rqmode;
  321. int m_bastmode;
  322. int m_asts;
  323. int m_result; /* 0 or -EXXX */
  324. char m_extra[0]; /* name or lvb */
  325. };
  326. #define DLM_RS_NODES 0x00000001
  327. #define DLM_RS_NODES_ALL 0x00000002
  328. #define DLM_RS_DIR 0x00000004
  329. #define DLM_RS_DIR_ALL 0x00000008
  330. #define DLM_RS_LOCKS 0x00000010
  331. #define DLM_RS_LOCKS_ALL 0x00000020
  332. #define DLM_RS_DONE 0x00000040
  333. #define DLM_RS_DONE_ALL 0x00000080
  334. #define DLM_RCOM_STATUS 1
  335. #define DLM_RCOM_NAMES 2
  336. #define DLM_RCOM_LOOKUP 3
  337. #define DLM_RCOM_LOCK 4
  338. #define DLM_RCOM_STATUS_REPLY 5
  339. #define DLM_RCOM_NAMES_REPLY 6
  340. #define DLM_RCOM_LOOKUP_REPLY 7
  341. #define DLM_RCOM_LOCK_REPLY 8
  342. struct dlm_rcom {
  343. struct dlm_header rc_header;
  344. uint32_t rc_type; /* DLM_RCOM_ */
  345. int rc_result; /* multi-purpose */
  346. uint64_t rc_id; /* match reply with request */
  347. uint64_t rc_seq; /* sender's ls_recover_seq */
  348. uint64_t rc_seq_reply; /* remote ls_recover_seq */
  349. char rc_buf[0];
  350. };
  351. union dlm_packet {
  352. struct dlm_header header; /* common to other two */
  353. struct dlm_message message;
  354. struct dlm_rcom rcom;
  355. };
  356. struct rcom_config {
  357. __le32 rf_lvblen;
  358. __le32 rf_lsflags;
  359. __le64 rf_unused;
  360. };
  361. struct rcom_lock {
  362. __le32 rl_ownpid;
  363. __le32 rl_lkid;
  364. __le32 rl_remid;
  365. __le32 rl_parent_lkid;
  366. __le32 rl_parent_remid;
  367. __le32 rl_exflags;
  368. __le32 rl_flags;
  369. __le32 rl_lvbseq;
  370. __le32 rl_result;
  371. int8_t rl_rqmode;
  372. int8_t rl_grmode;
  373. int8_t rl_status;
  374. int8_t rl_asts;
  375. __le16 rl_wait_type;
  376. __le16 rl_namelen;
  377. char rl_name[DLM_RESNAME_MAXLEN];
  378. char rl_lvb[0];
  379. };
  380. struct dlm_ls {
  381. struct list_head ls_list; /* list of lockspaces */
  382. dlm_lockspace_t *ls_local_handle;
  383. uint32_t ls_global_id; /* global unique lockspace ID */
  384. uint32_t ls_exflags;
  385. int ls_lvblen;
  386. int ls_count; /* reference count */
  387. unsigned long ls_flags; /* LSFL_ */
  388. struct kobject ls_kobj;
  389. struct dlm_rsbtable *ls_rsbtbl;
  390. uint32_t ls_rsbtbl_size;
  391. struct dlm_lkbtable *ls_lkbtbl;
  392. uint32_t ls_lkbtbl_size;
  393. struct dlm_dirtable *ls_dirtbl;
  394. uint32_t ls_dirtbl_size;
  395. struct mutex ls_waiters_mutex;
  396. struct list_head ls_waiters; /* lkbs needing a reply */
  397. struct mutex ls_orphans_mutex;
  398. struct list_head ls_orphans;
  399. struct mutex ls_timeout_mutex;
  400. struct list_head ls_timeout;
  401. struct list_head ls_nodes; /* current nodes in ls */
  402. struct list_head ls_nodes_gone; /* dead node list, recovery */
  403. int ls_num_nodes; /* number of nodes in ls */
  404. int ls_low_nodeid;
  405. int ls_total_weight;
  406. int *ls_node_array;
  407. gfp_t ls_allocation;
  408. struct dlm_rsb ls_stub_rsb; /* for returning errors */
  409. struct dlm_lkb ls_stub_lkb; /* for returning errors */
  410. struct dlm_message ls_stub_ms; /* for faking a reply */
  411. struct dentry *ls_debug_rsb_dentry; /* debugfs */
  412. struct dentry *ls_debug_waiters_dentry; /* debugfs */
  413. struct dentry *ls_debug_locks_dentry; /* debugfs */
  414. wait_queue_head_t ls_uevent_wait; /* user part of join/leave */
  415. int ls_uevent_result;
  416. struct completion ls_members_done;
  417. int ls_members_result;
  418. struct miscdevice ls_device;
  419. /* recovery related */
  420. struct timer_list ls_timer;
  421. struct task_struct *ls_recoverd_task;
  422. struct mutex ls_recoverd_active;
  423. spinlock_t ls_recover_lock;
  424. unsigned long ls_recover_begin; /* jiffies timestamp */
  425. uint32_t ls_recover_status; /* DLM_RS_ */
  426. uint64_t ls_recover_seq;
  427. struct dlm_recover *ls_recover_args;
  428. struct rw_semaphore ls_in_recovery; /* block local requests */
  429. struct rw_semaphore ls_recv_active; /* block dlm_recv */
  430. struct list_head ls_requestqueue;/* queue remote requests */
  431. struct mutex ls_requestqueue_mutex;
  432. struct dlm_rcom *ls_recover_buf;
  433. int ls_recover_nodeid; /* for debugging */
  434. uint64_t ls_rcom_seq;
  435. spinlock_t ls_rcom_spin;
  436. struct list_head ls_recover_list;
  437. spinlock_t ls_recover_list_lock;
  438. int ls_recover_list_count;
  439. wait_queue_head_t ls_wait_general;
  440. struct mutex ls_clear_proc_locks;
  441. struct list_head ls_root_list; /* root resources */
  442. struct rw_semaphore ls_root_sem; /* protect root_list */
  443. int ls_namelen;
  444. char ls_name[1];
  445. };
  446. #define LSFL_WORK 0
  447. #define LSFL_RUNNING 1
  448. #define LSFL_RECOVERY_STOP 2
  449. #define LSFL_RCOM_READY 3
  450. #define LSFL_RCOM_WAIT 4
  451. #define LSFL_UEVENT_WAIT 5
  452. #define LSFL_TIMEWARN 6
  453. /* much of this is just saving user space pointers associated with the
  454. lock that we pass back to the user lib with an ast */
  455. struct dlm_user_args {
  456. struct dlm_user_proc *proc; /* each process that opens the lockspace
  457. device has private data
  458. (dlm_user_proc) on the struct file,
  459. the process's locks point back to it*/
  460. struct dlm_lksb lksb;
  461. int old_mode;
  462. int update_user_lvb;
  463. struct dlm_lksb __user *user_lksb;
  464. void __user *castparam;
  465. void __user *castaddr;
  466. void __user *bastparam;
  467. void __user *bastaddr;
  468. uint64_t xid;
  469. };
  470. #define DLM_PROC_FLAGS_CLOSING 1
  471. #define DLM_PROC_FLAGS_COMPAT 2
  472. /* locks list is kept so we can remove all a process's locks when it
  473. exits (or orphan those that are persistent) */
  474. struct dlm_user_proc {
  475. dlm_lockspace_t *lockspace;
  476. unsigned long flags; /* DLM_PROC_FLAGS */
  477. struct list_head asts;
  478. spinlock_t asts_spin;
  479. struct list_head locks;
  480. spinlock_t locks_spin;
  481. struct list_head unlocking;
  482. wait_queue_head_t wait;
  483. };
  484. static inline int dlm_locking_stopped(struct dlm_ls *ls)
  485. {
  486. return !test_bit(LSFL_RUNNING, &ls->ls_flags);
  487. }
  488. static inline int dlm_recovery_stopped(struct dlm_ls *ls)
  489. {
  490. return test_bit(LSFL_RECOVERY_STOP, &ls->ls_flags);
  491. }
  492. static inline int dlm_no_directory(struct dlm_ls *ls)
  493. {
  494. return (ls->ls_exflags & DLM_LSFL_NODIR) ? 1 : 0;
  495. }
  496. int dlm_netlink_init(void);
  497. void dlm_netlink_exit(void);
  498. void dlm_timeout_warn(struct dlm_lkb *lkb);
  499. #ifdef CONFIG_DLM_DEBUG
  500. int dlm_register_debugfs(void);
  501. void dlm_unregister_debugfs(void);
  502. int dlm_create_debug_file(struct dlm_ls *ls);
  503. void dlm_delete_debug_file(struct dlm_ls *ls);
  504. #else
  505. static inline int dlm_register_debugfs(void) { return 0; }
  506. static inline void dlm_unregister_debugfs(void) { }
  507. static inline int dlm_create_debug_file(struct dlm_ls *ls) { return 0; }
  508. static inline void dlm_delete_debug_file(struct dlm_ls *ls) { }
  509. #endif
  510. #endif /* __DLM_INTERNAL_DOT_H__ */