glock.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_PRIORITY
  63. * Override fairness considerations. Suppose a lock is held in a shared state
  64. * and there is a pending request for the deferred state. A shared lock
  65. * request with the priority flag would be allowed to bypass the deferred
  66. * request and directly join the other shared lock. A shared lock request
  67. * without the priority flag might be forced to wait until the deferred
  68. * requested had acquired and released the lock.
  69. *
  70. * LM_FLAG_NODE_SCOPE
  71. * This holder agrees to share the lock within this node. In other words,
  72. * the glock is held in EX mode according to DLM, but local holders on the
  73. * same node can share it.
  74. */
  75. #define LM_FLAG_TRY 0x0001
  76. #define LM_FLAG_TRY_1CB 0x0002
  77. #define LM_FLAG_NOEXP 0x0004
  78. #define LM_FLAG_ANY 0x0008
  79. #define LM_FLAG_PRIORITY 0x0010
  80. #define LM_FLAG_NODE_SCOPE 0x0020
  81. #define GL_ASYNC 0x0040
  82. #define GL_EXACT 0x0080
  83. #define GL_SKIP 0x0100
  84. #define GL_NOPID 0x0200
  85. #define GL_NOCACHE 0x0400
  86. /*
  87. * lm_async_cb return flags
  88. *
  89. * LM_OUT_ST_MASK
  90. * Masks the lower two bits of lock state in the returned value.
  91. *
  92. * LM_OUT_CANCELED
  93. * The lock request was canceled.
  94. *
  95. */
  96. #define LM_OUT_ST_MASK 0x00000003
  97. #define LM_OUT_CANCELED 0x00000008
  98. #define LM_OUT_ERROR 0x00000004
  99. /*
  100. * lm_recovery_done() messages
  101. */
  102. #define LM_RD_GAVEUP 308
  103. #define LM_RD_SUCCESS 309
  104. #define GLR_TRYFAILED 13
  105. #define GL_GLOCK_MAX_HOLD (long)(HZ / 5)
  106. #define GL_GLOCK_DFT_HOLD (long)(HZ / 5)
  107. #define GL_GLOCK_MIN_HOLD (long)(10)
  108. #define GL_GLOCK_HOLD_INCR (long)(HZ / 20)
  109. #define GL_GLOCK_HOLD_DECR (long)(HZ / 40)
  110. struct lm_lockops {
  111. const char *lm_proto_name;
  112. int (*lm_mount) (struct gfs2_sbd *sdp, const char *table);
  113. void (*lm_first_done) (struct gfs2_sbd *sdp);
  114. void (*lm_recovery_result) (struct gfs2_sbd *sdp, unsigned int jid,
  115. unsigned int result);
  116. void (*lm_unmount) (struct gfs2_sbd *sdp);
  117. void (*lm_withdraw) (struct gfs2_sbd *sdp);
  118. void (*lm_put_lock) (struct gfs2_glock *gl);
  119. int (*lm_lock) (struct gfs2_glock *gl, unsigned int req_state,
  120. unsigned int flags);
  121. void (*lm_cancel) (struct gfs2_glock *gl);
  122. const match_table_t *lm_tokens;
  123. };
  124. struct gfs2_glock_aspace {
  125. struct gfs2_glock glock;
  126. struct address_space mapping;
  127. };
  128. extern struct workqueue_struct *gfs2_delete_workqueue;
  129. static inline struct gfs2_holder *gfs2_glock_is_locked_by_me(struct gfs2_glock *gl)
  130. {
  131. struct gfs2_holder *gh;
  132. struct pid *pid;
  133. /* Look in glock's list of holders for one with current task as owner */
  134. spin_lock(&gl->gl_lockref.lock);
  135. pid = task_pid(current);
  136. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  137. if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
  138. break;
  139. if (test_bit(HIF_MAY_DEMOTE, &gh->gh_iflags))
  140. continue;
  141. if (gh->gh_owner_pid == pid)
  142. goto out;
  143. }
  144. gh = NULL;
  145. out:
  146. spin_unlock(&gl->gl_lockref.lock);
  147. return gh;
  148. }
  149. static inline int gfs2_glock_is_held_excl(struct gfs2_glock *gl)
  150. {
  151. return gl->gl_state == LM_ST_EXCLUSIVE;
  152. }
  153. static inline int gfs2_glock_is_held_dfrd(struct gfs2_glock *gl)
  154. {
  155. return gl->gl_state == LM_ST_DEFERRED;
  156. }
  157. static inline int gfs2_glock_is_held_shrd(struct gfs2_glock *gl)
  158. {
  159. return gl->gl_state == LM_ST_SHARED;
  160. }
  161. static inline struct address_space *gfs2_glock2aspace(struct gfs2_glock *gl)
  162. {
  163. if (gl->gl_ops->go_flags & GLOF_ASPACE) {
  164. struct gfs2_glock_aspace *gla =
  165. container_of(gl, struct gfs2_glock_aspace, glock);
  166. return &gla->mapping;
  167. }
  168. return NULL;
  169. }
  170. extern int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
  171. const struct gfs2_glock_operations *glops,
  172. int create, struct gfs2_glock **glp);
  173. extern void gfs2_glock_hold(struct gfs2_glock *gl);
  174. extern void gfs2_glock_put(struct gfs2_glock *gl);
  175. extern void gfs2_glock_queue_put(struct gfs2_glock *gl);
  176. extern void __gfs2_holder_init(struct gfs2_glock *gl, unsigned int state,
  177. u16 flags, struct gfs2_holder *gh,
  178. unsigned long ip);
  179. static inline void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state,
  180. u16 flags, struct gfs2_holder *gh) {
  181. __gfs2_holder_init(gl, state, flags, gh, _RET_IP_);
  182. }
  183. extern void gfs2_holder_reinit(unsigned int state, u16 flags,
  184. struct gfs2_holder *gh);
  185. extern void gfs2_holder_uninit(struct gfs2_holder *gh);
  186. extern int gfs2_glock_nq(struct gfs2_holder *gh);
  187. extern int gfs2_glock_poll(struct gfs2_holder *gh);
  188. extern int gfs2_instantiate(struct gfs2_holder *gh);
  189. extern int gfs2_glock_holder_ready(struct gfs2_holder *gh);
  190. extern int gfs2_glock_wait(struct gfs2_holder *gh);
  191. extern int gfs2_glock_async_wait(unsigned int num_gh, struct gfs2_holder *ghs);
  192. extern void gfs2_glock_dq(struct gfs2_holder *gh);
  193. extern void gfs2_glock_dq_wait(struct gfs2_holder *gh);
  194. extern void gfs2_glock_dq_uninit(struct gfs2_holder *gh);
  195. extern int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  196. const struct gfs2_glock_operations *glops,
  197. unsigned int state, u16 flags,
  198. struct gfs2_holder *gh);
  199. extern int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs);
  200. extern void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs);
  201. extern void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl,
  202. bool fsid);
  203. #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { \
  204. gfs2_dump_glock(NULL, gl, true); \
  205. BUG(); } } while(0)
  206. #define gfs2_glock_assert_warn(gl, x) do { if (unlikely(!(x))) { \
  207. gfs2_dump_glock(NULL, gl, true); \
  208. gfs2_assert_warn((gl)->gl_name.ln_sbd, (x)); } } \
  209. while (0)
  210. #define gfs2_glock_assert_withdraw(gl, x) do { if (unlikely(!(x))) { \
  211. gfs2_dump_glock(NULL, gl, true); \
  212. gfs2_assert_withdraw((gl)->gl_name.ln_sbd, (x)); } } \
  213. while (0)
  214. extern __printf(2, 3)
  215. void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...);
  216. /**
  217. * gfs2_glock_nq_init - initialize a holder and enqueue it on a glock
  218. * @gl: the glock
  219. * @state: the state we're requesting
  220. * @flags: the modifier flags
  221. * @gh: the holder structure
  222. *
  223. * Returns: 0, GLR_*, or errno
  224. */
  225. static inline int gfs2_glock_nq_init(struct gfs2_glock *gl,
  226. unsigned int state, u16 flags,
  227. struct gfs2_holder *gh)
  228. {
  229. int error;
  230. __gfs2_holder_init(gl, state, flags, gh, _RET_IP_);
  231. error = gfs2_glock_nq(gh);
  232. if (error)
  233. gfs2_holder_uninit(gh);
  234. return error;
  235. }
  236. extern void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state);
  237. extern void gfs2_glock_complete(struct gfs2_glock *gl, int ret);
  238. extern bool gfs2_queue_delete_work(struct gfs2_glock *gl, unsigned long delay);
  239. extern void gfs2_cancel_delete_work(struct gfs2_glock *gl);
  240. extern bool gfs2_delete_work_queued(const struct gfs2_glock *gl);
  241. extern void gfs2_flush_delete_work(struct gfs2_sbd *sdp);
  242. extern void gfs2_gl_hash_clear(struct gfs2_sbd *sdp);
  243. extern void gfs2_gl_dq_holders(struct gfs2_sbd *sdp);
  244. extern void gfs2_glock_thaw(struct gfs2_sbd *sdp);
  245. extern void gfs2_glock_add_to_lru(struct gfs2_glock *gl);
  246. extern void gfs2_glock_free(struct gfs2_glock *gl);
  247. extern int __init gfs2_glock_init(void);
  248. extern void gfs2_glock_exit(void);
  249. extern void gfs2_create_debugfs_file(struct gfs2_sbd *sdp);
  250. extern void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp);
  251. extern void gfs2_register_debugfs(void);
  252. extern void gfs2_unregister_debugfs(void);
  253. extern const struct lm_lockops gfs2_dlm_ops;
  254. static inline void gfs2_holder_mark_uninitialized(struct gfs2_holder *gh)
  255. {
  256. gh->gh_gl = NULL;
  257. }
  258. static inline bool gfs2_holder_initialized(struct gfs2_holder *gh)
  259. {
  260. return gh->gh_gl;
  261. }
  262. static inline bool gfs2_holder_queued(struct gfs2_holder *gh)
  263. {
  264. return !list_empty(&gh->gh_list);
  265. }
  266. /**
  267. * glock_set_object - set the gl_object field of a glock
  268. * @gl: the glock
  269. * @object: the object
  270. */
  271. static inline void glock_set_object(struct gfs2_glock *gl, void *object)
  272. {
  273. spin_lock(&gl->gl_lockref.lock);
  274. if (gfs2_assert_warn(gl->gl_name.ln_sbd, gl->gl_object == NULL))
  275. gfs2_dump_glock(NULL, gl, true);
  276. gl->gl_object = object;
  277. spin_unlock(&gl->gl_lockref.lock);
  278. }
  279. /**
  280. * glock_clear_object - clear the gl_object field of a glock
  281. * @gl: the glock
  282. * @object: the object
  283. *
  284. * I'd love to similarly add this:
  285. * else if (gfs2_assert_warn(gl->gl_sbd, gl->gl_object == object))
  286. * gfs2_dump_glock(NULL, gl, true);
  287. * Unfortunately, that's not possible because as soon as gfs2_delete_inode
  288. * frees the block in the rgrp, another process can reassign it for an I_NEW
  289. * inode in gfs2_create_inode because that calls new_inode, not gfs2_iget.
  290. * That means gfs2_delete_inode may subsequently try to call this function
  291. * for a glock that's already pointing to a brand new inode. If we clear the
  292. * new inode's gl_object, we'll introduce metadata corruption. Function
  293. * gfs2_delete_inode calls clear_inode which calls gfs2_clear_inode which also
  294. * tries to clear gl_object, so it's more than just gfs2_delete_inode.
  295. *
  296. */
  297. static inline void glock_clear_object(struct gfs2_glock *gl, void *object)
  298. {
  299. spin_lock(&gl->gl_lockref.lock);
  300. if (gl->gl_object == object)
  301. gl->gl_object = NULL;
  302. spin_unlock(&gl->gl_lockref.lock);
  303. }
  304. static inline void gfs2_holder_allow_demote(struct gfs2_holder *gh)
  305. {
  306. struct gfs2_glock *gl = gh->gh_gl;
  307. spin_lock(&gl->gl_lockref.lock);
  308. set_bit(HIF_MAY_DEMOTE, &gh->gh_iflags);
  309. spin_unlock(&gl->gl_lockref.lock);
  310. }
  311. static inline void gfs2_holder_disallow_demote(struct gfs2_holder *gh)
  312. {
  313. struct gfs2_glock *gl = gh->gh_gl;
  314. spin_lock(&gl->gl_lockref.lock);
  315. clear_bit(HIF_MAY_DEMOTE, &gh->gh_iflags);
  316. spin_unlock(&gl->gl_lockref.lock);
  317. }
  318. extern void gfs2_inode_remember_delete(struct gfs2_glock *gl, u64 generation);
  319. extern bool gfs2_inode_already_deleted(struct gfs2_glock *gl, u64 generation);
  320. #endif /* __GLOCK_DOT_H__ */