glock.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #ifndef __GLOCK_DOT_H__
  10. #define __GLOCK_DOT_H__
  11. #include <linux/sched.h>
  12. #include <linux/parser.h>
  13. #include "incore.h"
  14. #include "util.h"
  15. /* Options for hostdata parser */
  16. enum {
  17. Opt_jid,
  18. Opt_id,
  19. Opt_first,
  20. Opt_nodir,
  21. Opt_err,
  22. };
  23. /*
  24. * lm_lockname types
  25. */
  26. #define LM_TYPE_RESERVED 0x00
  27. #define LM_TYPE_NONDISK 0x01
  28. #define LM_TYPE_INODE 0x02
  29. #define LM_TYPE_RGRP 0x03
  30. #define LM_TYPE_META 0x04
  31. #define LM_TYPE_IOPEN 0x05
  32. #define LM_TYPE_FLOCK 0x06
  33. #define LM_TYPE_PLOCK 0x07
  34. #define LM_TYPE_QUOTA 0x08
  35. #define LM_TYPE_JOURNAL 0x09
  36. /*
  37. * lm_lock() states
  38. *
  39. * SHARED is compatible with SHARED, not with DEFERRED or EX.
  40. * DEFERRED is compatible with DEFERRED, not with SHARED or EX.
  41. */
  42. #define LM_ST_UNLOCKED 0
  43. #define LM_ST_EXCLUSIVE 1
  44. #define LM_ST_DEFERRED 2
  45. #define LM_ST_SHARED 3
  46. /*
  47. * lm_lock() flags
  48. *
  49. * LM_FLAG_TRY
  50. * Don't wait to acquire the lock if it can't be granted immediately.
  51. *
  52. * LM_FLAG_TRY_1CB
  53. * Send one blocking callback if TRY is set and the lock is not granted.
  54. *
  55. * LM_FLAG_NOEXP
  56. * GFS sets this flag on lock requests it makes while doing journal recovery.
  57. * These special requests should not be blocked due to the recovery like
  58. * ordinary locks would be.
  59. *
  60. * LM_FLAG_ANY
  61. * A SHARED request may also be granted in DEFERRED, or a DEFERRED request may
  62. * also be granted in SHARED. The preferred state is whichever is compatible
  63. * with other granted locks, or the specified state if no other locks exist.
  64. *
  65. * LM_FLAG_PRIORITY
  66. * Override fairness considerations. Suppose a lock is held in a shared state
  67. * and there is a pending request for the deferred state. A shared lock
  68. * request with the priority flag would be allowed to bypass the deferred
  69. * request and directly join the other shared lock. A shared lock request
  70. * without the priority flag might be forced to wait until the deferred
  71. * requested had acquired and released the lock.
  72. */
  73. #define LM_FLAG_TRY 0x0001
  74. #define LM_FLAG_TRY_1CB 0x0002
  75. #define LM_FLAG_NOEXP 0x0004
  76. #define LM_FLAG_ANY 0x0008
  77. #define LM_FLAG_PRIORITY 0x0010
  78. #define GL_ASYNC 0x0040
  79. #define GL_EXACT 0x0080
  80. #define GL_SKIP 0x0100
  81. #define GL_NOCACHE 0x0400
  82. /*
  83. * lm_async_cb return flags
  84. *
  85. * LM_OUT_ST_MASK
  86. * Masks the lower two bits of lock state in the returned value.
  87. *
  88. * LM_OUT_CANCELED
  89. * The lock request was canceled.
  90. *
  91. */
  92. #define LM_OUT_ST_MASK 0x00000003
  93. #define LM_OUT_CANCELED 0x00000008
  94. #define LM_OUT_ERROR 0x00000004
  95. /*
  96. * lm_recovery_done() messages
  97. */
  98. #define LM_RD_GAVEUP 308
  99. #define LM_RD_SUCCESS 309
  100. #define GLR_TRYFAILED 13
  101. #define GL_GLOCK_MAX_HOLD (long)(HZ / 5)
  102. #define GL_GLOCK_DFT_HOLD (long)(HZ / 5)
  103. #define GL_GLOCK_MIN_HOLD (long)(10)
  104. #define GL_GLOCK_HOLD_INCR (long)(HZ / 20)
  105. #define GL_GLOCK_HOLD_DECR (long)(HZ / 40)
  106. struct lm_lockops {
  107. const char *lm_proto_name;
  108. int (*lm_mount) (struct gfs2_sbd *sdp, const char *table);
  109. void (*lm_first_done) (struct gfs2_sbd *sdp);
  110. void (*lm_recovery_result) (struct gfs2_sbd *sdp, unsigned int jid,
  111. unsigned int result);
  112. void (*lm_unmount) (struct gfs2_sbd *sdp);
  113. void (*lm_withdraw) (struct gfs2_sbd *sdp);
  114. void (*lm_put_lock) (struct gfs2_glock *gl);
  115. int (*lm_lock) (struct gfs2_glock *gl, unsigned int req_state,
  116. unsigned int flags);
  117. void (*lm_cancel) (struct gfs2_glock *gl);
  118. const match_table_t *lm_tokens;
  119. };
  120. extern struct workqueue_struct *gfs2_delete_workqueue;
  121. static inline struct gfs2_holder *gfs2_glock_is_locked_by_me(struct gfs2_glock *gl)
  122. {
  123. struct gfs2_holder *gh;
  124. struct pid *pid;
  125. /* Look in glock's list of holders for one with current task as owner */
  126. spin_lock(&gl->gl_lockref.lock);
  127. pid = task_pid(current);
  128. list_for_each_entry(gh, &gl->gl_holders, gh_list) {
  129. if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
  130. break;
  131. if (gh->gh_owner_pid == pid)
  132. goto out;
  133. }
  134. gh = NULL;
  135. out:
  136. spin_unlock(&gl->gl_lockref.lock);
  137. return gh;
  138. }
  139. static inline int gfs2_glock_is_held_excl(struct gfs2_glock *gl)
  140. {
  141. return gl->gl_state == LM_ST_EXCLUSIVE;
  142. }
  143. static inline int gfs2_glock_is_held_dfrd(struct gfs2_glock *gl)
  144. {
  145. return gl->gl_state == LM_ST_DEFERRED;
  146. }
  147. static inline int gfs2_glock_is_held_shrd(struct gfs2_glock *gl)
  148. {
  149. return gl->gl_state == LM_ST_SHARED;
  150. }
  151. static inline struct address_space *gfs2_glock2aspace(struct gfs2_glock *gl)
  152. {
  153. if (gl->gl_ops->go_flags & GLOF_ASPACE)
  154. return (struct address_space *)(gl + 1);
  155. return NULL;
  156. }
  157. extern int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
  158. const struct gfs2_glock_operations *glops,
  159. int create, struct gfs2_glock **glp);
  160. extern void gfs2_glock_hold(struct gfs2_glock *gl);
  161. extern void gfs2_glock_put(struct gfs2_glock *gl);
  162. extern void gfs2_glock_queue_put(struct gfs2_glock *gl);
  163. extern void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state,
  164. u16 flags, struct gfs2_holder *gh);
  165. extern void gfs2_holder_reinit(unsigned int state, u16 flags,
  166. struct gfs2_holder *gh);
  167. extern void gfs2_holder_uninit(struct gfs2_holder *gh);
  168. extern int gfs2_glock_nq(struct gfs2_holder *gh);
  169. extern int gfs2_glock_poll(struct gfs2_holder *gh);
  170. extern int gfs2_glock_wait(struct gfs2_holder *gh);
  171. extern void gfs2_glock_dq(struct gfs2_holder *gh);
  172. extern void gfs2_glock_dq_wait(struct gfs2_holder *gh);
  173. extern void gfs2_glock_dq_uninit(struct gfs2_holder *gh);
  174. extern int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
  175. const struct gfs2_glock_operations *glops,
  176. unsigned int state, u16 flags,
  177. struct gfs2_holder *gh);
  178. extern int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs);
  179. extern void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs);
  180. extern void gfs2_dump_glock(struct seq_file *seq, const struct gfs2_glock *gl);
  181. #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { gfs2_dump_glock(NULL, gl); BUG(); } } while(0)
  182. extern __printf(2, 3)
  183. void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...);
  184. /**
  185. * gfs2_glock_nq_init - initialize a holder and enqueue it on a glock
  186. * @gl: the glock
  187. * @state: the state we're requesting
  188. * @flags: the modifier flags
  189. * @gh: the holder structure
  190. *
  191. * Returns: 0, GLR_*, or errno
  192. */
  193. static inline int gfs2_glock_nq_init(struct gfs2_glock *gl,
  194. unsigned int state, u16 flags,
  195. struct gfs2_holder *gh)
  196. {
  197. int error;
  198. gfs2_holder_init(gl, state, flags, gh);
  199. error = gfs2_glock_nq(gh);
  200. if (error)
  201. gfs2_holder_uninit(gh);
  202. return error;
  203. }
  204. extern void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state);
  205. extern void gfs2_glock_complete(struct gfs2_glock *gl, int ret);
  206. extern void gfs2_gl_hash_clear(struct gfs2_sbd *sdp);
  207. extern void gfs2_glock_finish_truncate(struct gfs2_inode *ip);
  208. extern void gfs2_glock_thaw(struct gfs2_sbd *sdp);
  209. extern void gfs2_glock_add_to_lru(struct gfs2_glock *gl);
  210. extern void gfs2_glock_free(struct gfs2_glock *gl);
  211. extern int __init gfs2_glock_init(void);
  212. extern void gfs2_glock_exit(void);
  213. extern int gfs2_create_debugfs_file(struct gfs2_sbd *sdp);
  214. extern void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp);
  215. extern int gfs2_register_debugfs(void);
  216. extern void gfs2_unregister_debugfs(void);
  217. extern const struct lm_lockops gfs2_dlm_ops;
  218. static inline void gfs2_holder_mark_uninitialized(struct gfs2_holder *gh)
  219. {
  220. gh->gh_gl = NULL;
  221. }
  222. static inline bool gfs2_holder_initialized(struct gfs2_holder *gh)
  223. {
  224. return gh->gh_gl;
  225. }
  226. /**
  227. * glock_set_object - set the gl_object field of a glock
  228. * @gl: the glock
  229. * @object: the object
  230. */
  231. static inline void glock_set_object(struct gfs2_glock *gl, void *object)
  232. {
  233. spin_lock(&gl->gl_lockref.lock);
  234. if (gfs2_assert_warn(gl->gl_name.ln_sbd, gl->gl_object == NULL))
  235. gfs2_dump_glock(NULL, gl);
  236. gl->gl_object = object;
  237. spin_unlock(&gl->gl_lockref.lock);
  238. }
  239. /**
  240. * glock_clear_object - clear the gl_object field of a glock
  241. * @gl: the glock
  242. * @object: the object
  243. *
  244. * I'd love to similarly add this:
  245. * else if (gfs2_assert_warn(gl->gl_sbd, gl->gl_object == object))
  246. * gfs2_dump_glock(NULL, gl);
  247. * Unfortunately, that's not possible because as soon as gfs2_delete_inode
  248. * frees the block in the rgrp, another process can reassign it for an I_NEW
  249. * inode in gfs2_create_inode because that calls new_inode, not gfs2_iget.
  250. * That means gfs2_delete_inode may subsequently try to call this function
  251. * for a glock that's already pointing to a brand new inode. If we clear the
  252. * new inode's gl_object, we'll introduce metadata corruption. Function
  253. * gfs2_delete_inode calls clear_inode which calls gfs2_clear_inode which also
  254. * tries to clear gl_object, so it's more than just gfs2_delete_inode.
  255. *
  256. */
  257. static inline void glock_clear_object(struct gfs2_glock *gl, void *object)
  258. {
  259. spin_lock(&gl->gl_lockref.lock);
  260. if (gl->gl_object == object)
  261. gl->gl_object = NULL;
  262. spin_unlock(&gl->gl_lockref.lock);
  263. }
  264. #endif /* __GLOCK_DOT_H__ */