glock.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  5. */
  6. #ifndef __GLOCK_DOT_H__
  7. #define __GLOCK_DOT_H__
  8. #include <linux/sched.h>
  9. #include <linux/parser.h>
  10. #include "incore.h"
  11. #include "util.h"
  12. /* Options for hostdata parser */
  13. enum {
  14. Opt_jid,
  15. Opt_id,
  16. Opt_first,
  17. Opt_nodir,
  18. Opt_err,
  19. };
  20. /*
  21. * lm_lockname types
  22. */
  23. #define LM_TYPE_RESERVED 0x00
  24. #define LM_TYPE_NONDISK 0x01
  25. #define LM_TYPE_INODE 0x02
  26. #define LM_TYPE_RGRP 0x03
  27. #define LM_TYPE_META 0x04
  28. #define LM_TYPE_IOPEN 0x05
  29. #define LM_TYPE_FLOCK 0x06
  30. #define LM_TYPE_PLOCK 0x07
  31. #define LM_TYPE_QUOTA 0x08
  32. #define LM_TYPE_JOURNAL 0x09
  33. /*
  34. * lm_lock() states
  35. *
  36. * SHARED is compatible with SHARED, not with DEFERRED or EX.
  37. * DEFERRED is compatible with DEFERRED, not with SHARED or EX.
  38. */
  39. #define LM_ST_UNLOCKED 0
  40. #define LM_ST_EXCLUSIVE 1
  41. #define LM_ST_DEFERRED 2
  42. #define LM_ST_SHARED 3
  43. /*
  44. * lm_lock() flags
  45. *
  46. * LM_FLAG_TRY
  47. * Don't wait to acquire the lock if it can't be granted immediately.
  48. *
  49. * LM_FLAG_TRY_1CB
  50. * Send one blocking callback if TRY is set and the lock is not granted.
  51. *
  52. * LM_FLAG_NOEXP
  53. * GFS sets this flag on lock requests it makes while doing journal recovery.
  54. * These special requests should not be blocked due to the recovery like
  55. * ordinary locks would be.
  56. *
  57. * LM_FLAG_ANY
  58. * A SHARED request may also be granted in DEFERRED, or a DEFERRED request may
  59. * also be granted in SHARED. The preferred state is whichever is compatible
  60. * with other granted locks, or the specified state if no other locks exist.
  61. *
  62. * LM_FLAG_NODE_SCOPE
  63. * This holder agrees to share the lock within this node. In other words,
  64. * the glock is held in EX mode according to DLM, but local holders on the
  65. * same node can share it.
  66. */
  67. #define LM_FLAG_TRY 0x0001
  68. #define LM_FLAG_TRY_1CB 0x0002
  69. #define LM_FLAG_NOEXP 0x0004
  70. #define LM_FLAG_ANY 0x0008
  71. #define LM_FLAG_NODE_SCOPE 0x0020
  72. #define GL_ASYNC 0x0040
  73. #define GL_EXACT 0x0080
  74. #define GL_SKIP 0x0100
  75. #define GL_NOPID 0x0200
  76. #define GL_NOCACHE 0x0400
  77. #define GL_NOBLOCK 0x0800
  78. /*
  79. * lm_async_cb return flags
  80. *
  81. * LM_OUT_ST_MASK
  82. * Masks the lower two bits of lock state in the returned value.
  83. *
  84. * LM_OUT_CANCELED
  85. * The lock request was canceled.
  86. *
  87. */
  88. #define LM_OUT_ST_MASK 0x00000003
  89. #define LM_OUT_CANCELED 0x00000008
  90. #define LM_OUT_ERROR 0x00000004
  91. /*
  92. * lm_recovery_done() messages
  93. */
  94. #define LM_RD_GAVEUP 308
  95. #define LM_RD_SUCCESS 309
  96. #define GLR_TRYFAILED 13
  97. #define GL_GLOCK_MAX_HOLD (long)(HZ / 5)
  98. #define GL_GLOCK_DFT_HOLD (long)(HZ / 5)
  99. #define GL_GLOCK_MIN_HOLD (long)(10)
  100. #define GL_GLOCK_HOLD_INCR (long)(HZ / 20)
  101. #define GL_GLOCK_HOLD_DECR (long)(HZ / 40)
  102. struct lm_lockops {
  103. const char *lm_proto_name;
  104. int (*lm_mount) (struct gfs2_sbd *sdp, const char *table);
  105. void (*lm_first_done) (struct gfs2_sbd *sdp);
  106. void (*lm_recovery_result) (struct gfs2_sbd *sdp, unsigned int jid,
  107. unsigned int result);
  108. void (*lm_unmount) (struct gfs2_sbd *sdp);
  109. void (*lm_withdraw) (struct gfs2_sbd *sdp);
  110. void (*lm_put_lock) (struct gfs2_glock *gl);
  111. int (*lm_lock) (struct gfs2_glock *gl, unsigned int req_state,
  112. unsigned int flags);
  113. void (*lm_cancel) (struct gfs2_glock *gl);
  114. const match_table_t *lm_tokens;
  115. };
  116. struct gfs2_glock_aspace {
  117. struct gfs2_glock glock;
  118. struct address_space mapping;
  119. };
  120. static inline struct gfs2_holder *gfs2_glock_is_locked_by_me(struct gfs2_glock *gl)
  121. {
  122. struct gfs2_holder *gh;
  123. struct pid *pid;
  124. /* Look in glock's list of holders for one with current task as owner */
  125. spin_lock(&gl->gl_lockref.lock);
  126. pid = task_pid(current);
  127. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  128. if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
  129. break;
  130. if (gh->gh_owner_pid == pid)
  131. goto out;
  132. }
  133. gh = NULL;
  134. out:
  135. spin_unlock(&gl->gl_lockref.lock);
  136. return gh;
  137. }
  138. static inline struct address_space *gfs2_glock2aspace(struct gfs2_glock *gl)
  139. {
  140. if (gl->gl_ops->go_flags & GLOF_ASPACE) {
  141. struct gfs2_glock_aspace *gla =
  142. container_of(gl, struct gfs2_glock_aspace, glock);
  143. return &gla->mapping;
  144. }
  145. return NULL;
  146. }
  147. int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
  148. const struct gfs2_glock_operations *glops,
  149. int create, struct gfs2_glock **glp);
  150. struct gfs2_glock *gfs2_glock_hold(struct gfs2_glock *gl);
  151. void gfs2_glock_put(struct gfs2_glock *gl);
  152. void gfs2_glock_put_async(struct gfs2_glock *gl);
  153. void __gfs2_holder_init(struct gfs2_glock *gl, unsigned int state,
  154. u16 flags, struct gfs2_holder *gh,
  155. unsigned long ip);
  156. static inline void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state,
  157. u16 flags, struct gfs2_holder *gh) {
  158. __gfs2_holder_init(gl, state, flags, gh, _RET_IP_);
  159. }
  160. void gfs2_holder_reinit(unsigned int state, u16 flags,
  161. struct gfs2_holder *gh);
  162. void gfs2_holder_uninit(struct gfs2_holder *gh);
  163. int gfs2_glock_nq(struct gfs2_holder *gh);
  164. int gfs2_glock_poll(struct gfs2_holder *gh);
  165. int gfs2_instantiate(struct gfs2_holder *gh);
  166. int gfs2_glock_holder_ready(struct gfs2_holder *gh);
  167. int gfs2_glock_wait(struct gfs2_holder *gh);
  168. int gfs2_glock_async_wait(unsigned int num_gh, struct gfs2_holder *ghs);
  169. void gfs2_glock_dq(struct gfs2_holder *gh);
  170. void gfs2_glock_dq_wait(struct gfs2_holder *gh);
  171. void gfs2_glock_dq_uninit(struct gfs2_holder *gh);
  172. int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  173. const struct gfs2_glock_operations *glops,
  174. unsigned int state, u16 flags,
  175. struct gfs2_holder *gh);
  176. int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs);
  177. void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs);
  178. void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl,
  179. bool fsid);
  180. #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { \
  181. gfs2_dump_glock(NULL, gl, true); \
  182. BUG(); } } while(0)
  183. #define gfs2_glock_assert_warn(gl, x) do { if (unlikely(!(x))) { \
  184. gfs2_dump_glock(NULL, gl, true); \
  185. gfs2_assert_warn((gl)->gl_name.ln_sbd, (x)); } } \
  186. while (0)
  187. #define gfs2_glock_assert_withdraw(gl, x) do { if (unlikely(!(x))) { \
  188. gfs2_dump_glock(NULL, gl, true); \
  189. gfs2_assert_withdraw((gl)->gl_name.ln_sbd, (x)); } } \
  190. while (0)
  191. __printf(2, 3)
  192. void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...);
  193. /**
  194. * gfs2_glock_nq_init - initialize a holder and enqueue it on a glock
  195. * @gl: the glock
  196. * @state: the state we're requesting
  197. * @flags: the modifier flags
  198. * @gh: the holder structure
  199. *
  200. * Returns: 0, GLR_*, or errno
  201. */
  202. static inline int gfs2_glock_nq_init(struct gfs2_glock *gl,
  203. unsigned int state, u16 flags,
  204. struct gfs2_holder *gh)
  205. {
  206. int error;
  207. __gfs2_holder_init(gl, state, flags, gh, _RET_IP_);
  208. error = gfs2_glock_nq(gh);
  209. if (error)
  210. gfs2_holder_uninit(gh);
  211. return error;
  212. }
  213. void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state);
  214. void gfs2_glock_complete(struct gfs2_glock *gl, int ret);
  215. bool gfs2_queue_try_to_evict(struct gfs2_glock *gl);
  216. bool gfs2_queue_verify_delete(struct gfs2_glock *gl, bool later);
  217. void gfs2_cancel_delete_work(struct gfs2_glock *gl);
  218. void gfs2_flush_delete_work(struct gfs2_sbd *sdp);
  219. void gfs2_gl_hash_clear(struct gfs2_sbd *sdp);
  220. void gfs2_gl_dq_holders(struct gfs2_sbd *sdp);
  221. void gfs2_glock_thaw(struct gfs2_sbd *sdp);
  222. void gfs2_glock_free(struct gfs2_glock *gl);
  223. void gfs2_glock_free_later(struct gfs2_glock *gl);
  224. int __init gfs2_glock_init(void);
  225. void gfs2_glock_exit(void);
  226. void gfs2_create_debugfs_file(struct gfs2_sbd *sdp);
  227. void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp);
  228. void gfs2_register_debugfs(void);
  229. void gfs2_unregister_debugfs(void);
  230. void glock_set_object(struct gfs2_glock *gl, void *object);
  231. void glock_clear_object(struct gfs2_glock *gl, void *object);
  232. extern const struct lm_lockops gfs2_dlm_ops;
  233. static inline void gfs2_holder_mark_uninitialized(struct gfs2_holder *gh)
  234. {
  235. gh->gh_gl = NULL;
  236. }
  237. static inline bool gfs2_holder_initialized(struct gfs2_holder *gh)
  238. {
  239. return gh->gh_gl;
  240. }
  241. static inline bool gfs2_holder_queued(struct gfs2_holder *gh)
  242. {
  243. return !list_empty(&gh->gh_list);
  244. }
  245. void gfs2_inode_remember_delete(struct gfs2_glock *gl, u64 generation);
  246. bool gfs2_inode_already_deleted(struct gfs2_glock *gl, u64 generation);
  247. #endif /* __GLOCK_DOT_H__ */