xref: /openbmc/linux/fs/gfs2/glock.h (revision 27a2660f)
1b3b94faaSDavid Teigland /*
2b3b94faaSDavid Teigland  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
33a8a9a10SSteven Whitehouse  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
4b3b94faaSDavid Teigland  *
5b3b94faaSDavid Teigland  * This copyrighted material is made available to anyone wishing to use,
6b3b94faaSDavid Teigland  * modify, copy, or redistribute it subject to the terms and conditions
7e9fc2aa0SSteven Whitehouse  * of the GNU General Public License version 2.
8b3b94faaSDavid Teigland  */
9b3b94faaSDavid Teigland 
10b3b94faaSDavid Teigland #ifndef __GLOCK_DOT_H__
11b3b94faaSDavid Teigland #define __GLOCK_DOT_H__
12b3b94faaSDavid Teigland 
13e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
14f057f6cdSSteven Whitehouse #include <linux/parser.h>
15f2f7ba52SSteven Whitehouse #include "incore.h"
16df3d87bdSBob Peterson #include "util.h"
17f2f7ba52SSteven Whitehouse 
18f057f6cdSSteven Whitehouse /* Options for hostdata parser */
19f057f6cdSSteven Whitehouse 
20f057f6cdSSteven Whitehouse enum {
21f057f6cdSSteven Whitehouse 	Opt_jid,
22f057f6cdSSteven Whitehouse 	Opt_id,
23f057f6cdSSteven Whitehouse 	Opt_first,
24f057f6cdSSteven Whitehouse 	Opt_nodir,
25f057f6cdSSteven Whitehouse 	Opt_err,
26f057f6cdSSteven Whitehouse };
27f057f6cdSSteven Whitehouse 
28f057f6cdSSteven Whitehouse /*
29f057f6cdSSteven Whitehouse  * lm_lockname types
30f057f6cdSSteven Whitehouse  */
31f057f6cdSSteven Whitehouse 
32f057f6cdSSteven Whitehouse #define LM_TYPE_RESERVED	0x00
33f057f6cdSSteven Whitehouse #define LM_TYPE_NONDISK		0x01
34f057f6cdSSteven Whitehouse #define LM_TYPE_INODE		0x02
35f057f6cdSSteven Whitehouse #define LM_TYPE_RGRP		0x03
36f057f6cdSSteven Whitehouse #define LM_TYPE_META		0x04
37f057f6cdSSteven Whitehouse #define LM_TYPE_IOPEN		0x05
38f057f6cdSSteven Whitehouse #define LM_TYPE_FLOCK		0x06
39f057f6cdSSteven Whitehouse #define LM_TYPE_PLOCK		0x07
40f057f6cdSSteven Whitehouse #define LM_TYPE_QUOTA		0x08
41f057f6cdSSteven Whitehouse #define LM_TYPE_JOURNAL		0x09
42f057f6cdSSteven Whitehouse 
43f057f6cdSSteven Whitehouse /*
44f057f6cdSSteven Whitehouse  * lm_lock() states
45f057f6cdSSteven Whitehouse  *
46f057f6cdSSteven Whitehouse  * SHARED is compatible with SHARED, not with DEFERRED or EX.
47f057f6cdSSteven Whitehouse  * DEFERRED is compatible with DEFERRED, not with SHARED or EX.
48f057f6cdSSteven Whitehouse  */
49f057f6cdSSteven Whitehouse 
50f057f6cdSSteven Whitehouse #define LM_ST_UNLOCKED		0
51f057f6cdSSteven Whitehouse #define LM_ST_EXCLUSIVE		1
52f057f6cdSSteven Whitehouse #define LM_ST_DEFERRED		2
53f057f6cdSSteven Whitehouse #define LM_ST_SHARED		3
54f057f6cdSSteven Whitehouse 
55f057f6cdSSteven Whitehouse /*
56f057f6cdSSteven Whitehouse  * lm_lock() flags
57f057f6cdSSteven Whitehouse  *
58f057f6cdSSteven Whitehouse  * LM_FLAG_TRY
59f057f6cdSSteven Whitehouse  * Don't wait to acquire the lock if it can't be granted immediately.
60f057f6cdSSteven Whitehouse  *
61f057f6cdSSteven Whitehouse  * LM_FLAG_TRY_1CB
62f057f6cdSSteven Whitehouse  * Send one blocking callback if TRY is set and the lock is not granted.
63f057f6cdSSteven Whitehouse  *
64f057f6cdSSteven Whitehouse  * LM_FLAG_NOEXP
65f057f6cdSSteven Whitehouse  * GFS sets this flag on lock requests it makes while doing journal recovery.
66f057f6cdSSteven Whitehouse  * These special requests should not be blocked due to the recovery like
67f057f6cdSSteven Whitehouse  * ordinary locks would be.
68f057f6cdSSteven Whitehouse  *
69f057f6cdSSteven Whitehouse  * LM_FLAG_ANY
70f057f6cdSSteven Whitehouse  * A SHARED request may also be granted in DEFERRED, or a DEFERRED request may
71f057f6cdSSteven Whitehouse  * also be granted in SHARED.  The preferred state is whichever is compatible
72f057f6cdSSteven Whitehouse  * with other granted locks, or the specified state if no other locks exist.
73f057f6cdSSteven Whitehouse  *
74f057f6cdSSteven Whitehouse  * LM_FLAG_PRIORITY
75f057f6cdSSteven Whitehouse  * Override fairness considerations.  Suppose a lock is held in a shared state
76f057f6cdSSteven Whitehouse  * and there is a pending request for the deferred state.  A shared lock
77f057f6cdSSteven Whitehouse  * request with the priority flag would be allowed to bypass the deferred
78f057f6cdSSteven Whitehouse  * request and directly join the other shared lock.  A shared lock request
79f057f6cdSSteven Whitehouse  * without the priority flag might be forced to wait until the deferred
80f057f6cdSSteven Whitehouse  * requested had acquired and released the lock.
81f057f6cdSSteven Whitehouse  */
82f057f6cdSSteven Whitehouse 
83b58bf407SBob Peterson #define LM_FLAG_TRY		0x0001
84b58bf407SBob Peterson #define LM_FLAG_TRY_1CB		0x0002
85b58bf407SBob Peterson #define LM_FLAG_NOEXP		0x0004
86b58bf407SBob Peterson #define LM_FLAG_ANY		0x0008
87b58bf407SBob Peterson #define LM_FLAG_PRIORITY	0x0010
88b58bf407SBob Peterson #define GL_ASYNC		0x0040
89b58bf407SBob Peterson #define GL_EXACT		0x0080
90b58bf407SBob Peterson #define GL_SKIP			0x0100
91b58bf407SBob Peterson #define GL_NOCACHE		0x0400
92f057f6cdSSteven Whitehouse 
93f057f6cdSSteven Whitehouse /*
94921169caSSteven Whitehouse  * lm_async_cb return flags
95f057f6cdSSteven Whitehouse  *
96f057f6cdSSteven Whitehouse  * LM_OUT_ST_MASK
97f057f6cdSSteven Whitehouse  * Masks the lower two bits of lock state in the returned value.
98f057f6cdSSteven Whitehouse  *
99f057f6cdSSteven Whitehouse  * LM_OUT_CANCELED
100f057f6cdSSteven Whitehouse  * The lock request was canceled.
101f057f6cdSSteven Whitehouse  *
102f057f6cdSSteven Whitehouse  */
103f057f6cdSSteven Whitehouse 
104f057f6cdSSteven Whitehouse #define LM_OUT_ST_MASK		0x00000003
105f057f6cdSSteven Whitehouse #define LM_OUT_CANCELED		0x00000008
106921169caSSteven Whitehouse #define LM_OUT_ERROR		0x00000004
107f057f6cdSSteven Whitehouse 
108f057f6cdSSteven Whitehouse /*
109f057f6cdSSteven Whitehouse  * lm_recovery_done() messages
110f057f6cdSSteven Whitehouse  */
111f057f6cdSSteven Whitehouse 
112f057f6cdSSteven Whitehouse #define LM_RD_GAVEUP		308
113f057f6cdSSteven Whitehouse #define LM_RD_SUCCESS		309
114f057f6cdSSteven Whitehouse 
115f057f6cdSSteven Whitehouse #define GLR_TRYFAILED		13
116f057f6cdSSteven Whitehouse 
1177cf8dcd3SBob Peterson #define GL_GLOCK_MAX_HOLD        (long)(HZ / 5)
1187cf8dcd3SBob Peterson #define GL_GLOCK_DFT_HOLD        (long)(HZ / 5)
1197cf8dcd3SBob Peterson #define GL_GLOCK_MIN_HOLD        (long)(10)
1207cf8dcd3SBob Peterson #define GL_GLOCK_HOLD_INCR       (long)(HZ / 20)
1217cf8dcd3SBob Peterson #define GL_GLOCK_HOLD_DECR       (long)(HZ / 40)
1227cf8dcd3SBob Peterson 
123f057f6cdSSteven Whitehouse struct lm_lockops {
124f057f6cdSSteven Whitehouse 	const char *lm_proto_name;
125e0c2a9aaSDavid Teigland 	int (*lm_mount) (struct gfs2_sbd *sdp, const char *table);
126e0c2a9aaSDavid Teigland 	void (*lm_first_done) (struct gfs2_sbd *sdp);
127e0c2a9aaSDavid Teigland 	void (*lm_recovery_result) (struct gfs2_sbd *sdp, unsigned int jid,
128e0c2a9aaSDavid Teigland 				    unsigned int result);
129f057f6cdSSteven Whitehouse 	void (*lm_unmount) (struct gfs2_sbd *sdp);
130f057f6cdSSteven Whitehouse 	void (*lm_withdraw) (struct gfs2_sbd *sdp);
131bc015cb8SSteven Whitehouse 	void (*lm_put_lock) (struct gfs2_glock *gl);
132921169caSSteven Whitehouse 	int (*lm_lock) (struct gfs2_glock *gl, unsigned int req_state,
133921169caSSteven Whitehouse 			unsigned int flags);
134f057f6cdSSteven Whitehouse 	void (*lm_cancel) (struct gfs2_glock *gl);
135f057f6cdSSteven Whitehouse 	const match_table_t *lm_tokens;
136f057f6cdSSteven Whitehouse };
137f057f6cdSSteven Whitehouse 
138b94a170eSBenjamin Marzinski extern struct workqueue_struct *gfs2_delete_workqueue;
1397afd88d9SSteven Whitehouse static inline struct gfs2_holder *gfs2_glock_is_locked_by_me(struct gfs2_glock *gl)
140b3b94faaSDavid Teigland {
141b3b94faaSDavid Teigland 	struct gfs2_holder *gh;
142b1e058daSPavel Emelyanov 	struct pid *pid;
143b3b94faaSDavid Teigland 
144b3b94faaSDavid Teigland 	/* Look in glock's list of holders for one with current task as owner */
145f3dd1649SAndreas Gruenbacher 	spin_lock(&gl->gl_lockref.lock);
146b1e058daSPavel Emelyanov 	pid = task_pid(current);
147b3b94faaSDavid Teigland 	list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1486802e340SSteven Whitehouse 		if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
1496802e340SSteven Whitehouse 			break;
1507afd88d9SSteven Whitehouse 		if (gh->gh_owner_pid == pid)
1517afd88d9SSteven Whitehouse 			goto out;
152b3b94faaSDavid Teigland 	}
1537afd88d9SSteven Whitehouse 	gh = NULL;
1547afd88d9SSteven Whitehouse out:
155f3dd1649SAndreas Gruenbacher 	spin_unlock(&gl->gl_lockref.lock);
156b3b94faaSDavid Teigland 
1577afd88d9SSteven Whitehouse 	return gh;
158b3b94faaSDavid Teigland }
159b3b94faaSDavid Teigland 
160b3b94faaSDavid Teigland static inline int gfs2_glock_is_held_excl(struct gfs2_glock *gl)
161b3b94faaSDavid Teigland {
16250299965SSteven Whitehouse 	return gl->gl_state == LM_ST_EXCLUSIVE;
163b3b94faaSDavid Teigland }
164b3b94faaSDavid Teigland 
165b3b94faaSDavid Teigland static inline int gfs2_glock_is_held_dfrd(struct gfs2_glock *gl)
166b3b94faaSDavid Teigland {
16750299965SSteven Whitehouse 	return gl->gl_state == LM_ST_DEFERRED;
168b3b94faaSDavid Teigland }
169b3b94faaSDavid Teigland 
170b3b94faaSDavid Teigland static inline int gfs2_glock_is_held_shrd(struct gfs2_glock *gl)
171b3b94faaSDavid Teigland {
17250299965SSteven Whitehouse 	return gl->gl_state == LM_ST_SHARED;
173b3b94faaSDavid Teigland }
174b3b94faaSDavid Teigland 
175009d8518SSteven Whitehouse static inline struct address_space *gfs2_glock2aspace(struct gfs2_glock *gl)
176009d8518SSteven Whitehouse {
177009d8518SSteven Whitehouse 	if (gl->gl_ops->go_flags & GLOF_ASPACE)
178009d8518SSteven Whitehouse 		return (struct address_space *)(gl + 1);
179009d8518SSteven Whitehouse 	return NULL;
180009d8518SSteven Whitehouse }
181009d8518SSteven Whitehouse 
1828eae1ca0SSteven Whitehouse extern int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
1838eae1ca0SSteven Whitehouse 			  const struct gfs2_glock_operations *glops,
184b3b94faaSDavid Teigland 			  int create, struct gfs2_glock **glp);
18571c1b213SAndreas Gruenbacher extern void gfs2_glock_hold(struct gfs2_glock *gl);
1868eae1ca0SSteven Whitehouse extern void gfs2_glock_put(struct gfs2_glock *gl);
18771c1b213SAndreas Gruenbacher extern void gfs2_glock_queue_put(struct gfs2_glock *gl);
1888eae1ca0SSteven Whitehouse extern void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state,
189b58bf407SBob Peterson 			     u16 flags, struct gfs2_holder *gh);
190b58bf407SBob Peterson extern void gfs2_holder_reinit(unsigned int state, u16 flags,
191b3b94faaSDavid Teigland 			       struct gfs2_holder *gh);
1928eae1ca0SSteven Whitehouse extern void gfs2_holder_uninit(struct gfs2_holder *gh);
1938eae1ca0SSteven Whitehouse extern int gfs2_glock_nq(struct gfs2_holder *gh);
1948eae1ca0SSteven Whitehouse extern int gfs2_glock_poll(struct gfs2_holder *gh);
1958eae1ca0SSteven Whitehouse extern int gfs2_glock_wait(struct gfs2_holder *gh);
1968eae1ca0SSteven Whitehouse extern void gfs2_glock_dq(struct gfs2_holder *gh);
1978eae1ca0SSteven Whitehouse extern void gfs2_glock_dq_wait(struct gfs2_holder *gh);
1988eae1ca0SSteven Whitehouse extern void gfs2_glock_dq_uninit(struct gfs2_holder *gh);
1998eae1ca0SSteven Whitehouse extern int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
2008eae1ca0SSteven Whitehouse 			     const struct gfs2_glock_operations *glops,
201b58bf407SBob Peterson 			     unsigned int state, u16 flags,
202190562bdSSteven Whitehouse 			     struct gfs2_holder *gh);
2038eae1ca0SSteven Whitehouse extern int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs);
2048eae1ca0SSteven Whitehouse extern void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs);
20527a2660fSBob Peterson extern void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl);
2068eae1ca0SSteven Whitehouse #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { gfs2_dump_glock(NULL, gl); BUG(); } } while(0)
2078eae1ca0SSteven Whitehouse extern __printf(2, 3)
2086802e340SSteven Whitehouse void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...);
209b3b94faaSDavid Teigland 
210d0dc80dbSSteven Whitehouse /**
21133027af6SAndrea Gelmini  * gfs2_glock_nq_init - initialize a holder and enqueue it on a glock
212d0dc80dbSSteven Whitehouse  * @gl: the glock
213d0dc80dbSSteven Whitehouse  * @state: the state we're requesting
214d0dc80dbSSteven Whitehouse  * @flags: the modifier flags
215d0dc80dbSSteven Whitehouse  * @gh: the holder structure
216d0dc80dbSSteven Whitehouse  *
217d0dc80dbSSteven Whitehouse  * Returns: 0, GLR_*, or errno
218d0dc80dbSSteven Whitehouse  */
219d0dc80dbSSteven Whitehouse 
220d0dc80dbSSteven Whitehouse static inline int gfs2_glock_nq_init(struct gfs2_glock *gl,
221b58bf407SBob Peterson 				     unsigned int state, u16 flags,
222d0dc80dbSSteven Whitehouse 				     struct gfs2_holder *gh)
223d0dc80dbSSteven Whitehouse {
224d0dc80dbSSteven Whitehouse 	int error;
225d0dc80dbSSteven Whitehouse 
226d0dc80dbSSteven Whitehouse 	gfs2_holder_init(gl, state, flags, gh);
227d0dc80dbSSteven Whitehouse 
228d0dc80dbSSteven Whitehouse 	error = gfs2_glock_nq(gh);
229d0dc80dbSSteven Whitehouse 	if (error)
230d0dc80dbSSteven Whitehouse 		gfs2_holder_uninit(gh);
231d0dc80dbSSteven Whitehouse 
232d0dc80dbSSteven Whitehouse 	return error;
233d0dc80dbSSteven Whitehouse }
234d0dc80dbSSteven Whitehouse 
235bc015cb8SSteven Whitehouse extern void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state);
236bc015cb8SSteven Whitehouse extern void gfs2_glock_complete(struct gfs2_glock *gl, int ret);
237bc015cb8SSteven Whitehouse extern void gfs2_gl_hash_clear(struct gfs2_sbd *sdp);
238bc015cb8SSteven Whitehouse extern void gfs2_glock_finish_truncate(struct gfs2_inode *ip);
239bc015cb8SSteven Whitehouse extern void gfs2_glock_thaw(struct gfs2_sbd *sdp);
24029687a2aSSteven Whitehouse extern void gfs2_glock_add_to_lru(struct gfs2_glock *gl);
241fc0e38daSSteven Whitehouse extern void gfs2_glock_free(struct gfs2_glock *gl);
242b3b94faaSDavid Teigland 
243bc015cb8SSteven Whitehouse extern int __init gfs2_glock_init(void);
244bc015cb8SSteven Whitehouse extern void gfs2_glock_exit(void);
245b3b94faaSDavid Teigland 
246bc015cb8SSteven Whitehouse extern int gfs2_create_debugfs_file(struct gfs2_sbd *sdp);
247bc015cb8SSteven Whitehouse extern void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp);
248bc015cb8SSteven Whitehouse extern int gfs2_register_debugfs(void);
249bc015cb8SSteven Whitehouse extern void gfs2_unregister_debugfs(void);
25085d1da67SSteven Whitehouse 
251f057f6cdSSteven Whitehouse extern const struct lm_lockops gfs2_dlm_ops;
252f057f6cdSSteven Whitehouse 
2536df9f9a2SAndreas Gruenbacher static inline void gfs2_holder_mark_uninitialized(struct gfs2_holder *gh)
2546df9f9a2SAndreas Gruenbacher {
2556df9f9a2SAndreas Gruenbacher 	gh->gh_gl = NULL;
2566df9f9a2SAndreas Gruenbacher }
2576df9f9a2SAndreas Gruenbacher 
2586df9f9a2SAndreas Gruenbacher static inline bool gfs2_holder_initialized(struct gfs2_holder *gh)
2596df9f9a2SAndreas Gruenbacher {
2606df9f9a2SAndreas Gruenbacher 	return gh->gh_gl;
2616df9f9a2SAndreas Gruenbacher }
2626df9f9a2SAndreas Gruenbacher 
263df3d87bdSBob Peterson /**
264df3d87bdSBob Peterson  * glock_set_object - set the gl_object field of a glock
265df3d87bdSBob Peterson  * @gl: the glock
266df3d87bdSBob Peterson  * @object: the object
267df3d87bdSBob Peterson  */
2684fd1a579SAndreas Gruenbacher static inline void glock_set_object(struct gfs2_glock *gl, void *object)
2694fd1a579SAndreas Gruenbacher {
2704fd1a579SAndreas Gruenbacher 	spin_lock(&gl->gl_lockref.lock);
271df3d87bdSBob Peterson 	if (gfs2_assert_warn(gl->gl_name.ln_sbd, gl->gl_object == NULL))
272df3d87bdSBob Peterson 		gfs2_dump_glock(NULL, gl);
2734fd1a579SAndreas Gruenbacher 	gl->gl_object = object;
2744fd1a579SAndreas Gruenbacher 	spin_unlock(&gl->gl_lockref.lock);
2754fd1a579SAndreas Gruenbacher }
2764fd1a579SAndreas Gruenbacher 
277df3d87bdSBob Peterson /**
278df3d87bdSBob Peterson  * glock_clear_object - clear the gl_object field of a glock
279df3d87bdSBob Peterson  * @gl: the glock
280df3d87bdSBob Peterson  * @object: the object
281df3d87bdSBob Peterson  *
282df3d87bdSBob Peterson  * I'd love to similarly add this:
283df3d87bdSBob Peterson  *	else if (gfs2_assert_warn(gl->gl_sbd, gl->gl_object == object))
284df3d87bdSBob Peterson  *		gfs2_dump_glock(NULL, gl);
285df3d87bdSBob Peterson  * Unfortunately, that's not possible because as soon as gfs2_delete_inode
286df3d87bdSBob Peterson  * frees the block in the rgrp, another process can reassign it for an I_NEW
287df3d87bdSBob Peterson  * inode in gfs2_create_inode because that calls new_inode, not gfs2_iget.
288df3d87bdSBob Peterson  * That means gfs2_delete_inode may subsequently try to call this function
289df3d87bdSBob Peterson  * for a glock that's already pointing to a brand new inode. If we clear the
290df3d87bdSBob Peterson  * new inode's gl_object, we'll introduce metadata corruption. Function
291df3d87bdSBob Peterson  * gfs2_delete_inode calls clear_inode which calls gfs2_clear_inode which also
292df3d87bdSBob Peterson  * tries to clear gl_object, so it's more than just gfs2_delete_inode.
293df3d87bdSBob Peterson  *
294df3d87bdSBob Peterson  */
295df3d87bdSBob Peterson static inline void glock_clear_object(struct gfs2_glock *gl, void *object)
296df3d87bdSBob Peterson {
297df3d87bdSBob Peterson 	spin_lock(&gl->gl_lockref.lock);
298df3d87bdSBob Peterson 	if (gl->gl_object == object)
299df3d87bdSBob Peterson 		gl->gl_object = NULL;
300df3d87bdSBob Peterson 	spin_unlock(&gl->gl_lockref.lock);
301df3d87bdSBob Peterson }
302df3d87bdSBob Peterson 
303b3b94faaSDavid Teigland #endif /* __GLOCK_DOT_H__ */
304