xref: /openbmc/linux/fs/gfs2/glock.h (revision 0636b34b)
17336d0e6SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2b3b94faaSDavid Teigland /*
3b3b94faaSDavid Teigland  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
43a8a9a10SSteven Whitehouse  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
5b3b94faaSDavid Teigland  */
6b3b94faaSDavid Teigland 
7b3b94faaSDavid Teigland #ifndef __GLOCK_DOT_H__
8b3b94faaSDavid Teigland #define __GLOCK_DOT_H__
9b3b94faaSDavid Teigland 
10e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
11f057f6cdSSteven Whitehouse #include <linux/parser.h>
12f2f7ba52SSteven Whitehouse #include "incore.h"
13df3d87bdSBob Peterson #include "util.h"
14f2f7ba52SSteven Whitehouse 
15f057f6cdSSteven Whitehouse /* Options for hostdata parser */
16f057f6cdSSteven Whitehouse 
17f057f6cdSSteven Whitehouse enum {
18f057f6cdSSteven Whitehouse 	Opt_jid,
19f057f6cdSSteven Whitehouse 	Opt_id,
20f057f6cdSSteven Whitehouse 	Opt_first,
21f057f6cdSSteven Whitehouse 	Opt_nodir,
22f057f6cdSSteven Whitehouse 	Opt_err,
23f057f6cdSSteven Whitehouse };
24f057f6cdSSteven Whitehouse 
25f057f6cdSSteven Whitehouse /*
26f057f6cdSSteven Whitehouse  * lm_lockname types
27f057f6cdSSteven Whitehouse  */
28f057f6cdSSteven Whitehouse 
29f057f6cdSSteven Whitehouse #define LM_TYPE_RESERVED	0x00
30f057f6cdSSteven Whitehouse #define LM_TYPE_NONDISK		0x01
31f057f6cdSSteven Whitehouse #define LM_TYPE_INODE		0x02
32f057f6cdSSteven Whitehouse #define LM_TYPE_RGRP		0x03
33f057f6cdSSteven Whitehouse #define LM_TYPE_META		0x04
34f057f6cdSSteven Whitehouse #define LM_TYPE_IOPEN		0x05
35f057f6cdSSteven Whitehouse #define LM_TYPE_FLOCK		0x06
36f057f6cdSSteven Whitehouse #define LM_TYPE_PLOCK		0x07
37f057f6cdSSteven Whitehouse #define LM_TYPE_QUOTA		0x08
38f057f6cdSSteven Whitehouse #define LM_TYPE_JOURNAL		0x09
39f057f6cdSSteven Whitehouse 
40f057f6cdSSteven Whitehouse /*
41f057f6cdSSteven Whitehouse  * lm_lock() states
42f057f6cdSSteven Whitehouse  *
43f057f6cdSSteven Whitehouse  * SHARED is compatible with SHARED, not with DEFERRED or EX.
44f057f6cdSSteven Whitehouse  * DEFERRED is compatible with DEFERRED, not with SHARED or EX.
45f057f6cdSSteven Whitehouse  */
46f057f6cdSSteven Whitehouse 
47f057f6cdSSteven Whitehouse #define LM_ST_UNLOCKED		0
48f057f6cdSSteven Whitehouse #define LM_ST_EXCLUSIVE		1
49f057f6cdSSteven Whitehouse #define LM_ST_DEFERRED		2
50f057f6cdSSteven Whitehouse #define LM_ST_SHARED		3
51f057f6cdSSteven Whitehouse 
52f057f6cdSSteven Whitehouse /*
53f057f6cdSSteven Whitehouse  * lm_lock() flags
54f057f6cdSSteven Whitehouse  *
55f057f6cdSSteven Whitehouse  * LM_FLAG_TRY
56f057f6cdSSteven Whitehouse  * Don't wait to acquire the lock if it can't be granted immediately.
57f057f6cdSSteven Whitehouse  *
58f057f6cdSSteven Whitehouse  * LM_FLAG_TRY_1CB
59f057f6cdSSteven Whitehouse  * Send one blocking callback if TRY is set and the lock is not granted.
60f057f6cdSSteven Whitehouse  *
61f057f6cdSSteven Whitehouse  * LM_FLAG_NOEXP
62f057f6cdSSteven Whitehouse  * GFS sets this flag on lock requests it makes while doing journal recovery.
63f057f6cdSSteven Whitehouse  * These special requests should not be blocked due to the recovery like
64f057f6cdSSteven Whitehouse  * ordinary locks would be.
65f057f6cdSSteven Whitehouse  *
66f057f6cdSSteven Whitehouse  * LM_FLAG_ANY
67f057f6cdSSteven Whitehouse  * A SHARED request may also be granted in DEFERRED, or a DEFERRED request may
68f057f6cdSSteven Whitehouse  * also be granted in SHARED.  The preferred state is whichever is compatible
69f057f6cdSSteven Whitehouse  * with other granted locks, or the specified state if no other locks exist.
70f057f6cdSSteven Whitehouse  *
7106e908cdSBob Peterson  * LM_FLAG_NODE_SCOPE
7206e908cdSBob Peterson  * This holder agrees to share the lock within this node. In other words,
7306e908cdSBob Peterson  * the glock is held in EX mode according to DLM, but local holders on the
7406e908cdSBob Peterson  * same node can share it.
75f057f6cdSSteven Whitehouse  */
76f057f6cdSSteven Whitehouse 
77b58bf407SBob Peterson #define LM_FLAG_TRY		0x0001
78b58bf407SBob Peterson #define LM_FLAG_TRY_1CB		0x0002
79b58bf407SBob Peterson #define LM_FLAG_NOEXP		0x0004
80b58bf407SBob Peterson #define LM_FLAG_ANY		0x0008
8106e908cdSBob Peterson #define LM_FLAG_NODE_SCOPE	0x0020
82b58bf407SBob Peterson #define GL_ASYNC		0x0040
83b58bf407SBob Peterson #define GL_EXACT		0x0080
84b58bf407SBob Peterson #define GL_SKIP			0x0100
85cbe6d257SAndreas Gruenbacher #define GL_NOPID		0x0200
86b58bf407SBob Peterson #define GL_NOCACHE		0x0400
87f057f6cdSSteven Whitehouse 
88f057f6cdSSteven Whitehouse /*
89921169caSSteven Whitehouse  * lm_async_cb return flags
90f057f6cdSSteven Whitehouse  *
91f057f6cdSSteven Whitehouse  * LM_OUT_ST_MASK
92f057f6cdSSteven Whitehouse  * Masks the lower two bits of lock state in the returned value.
93f057f6cdSSteven Whitehouse  *
94f057f6cdSSteven Whitehouse  * LM_OUT_CANCELED
95f057f6cdSSteven Whitehouse  * The lock request was canceled.
96f057f6cdSSteven Whitehouse  *
97f057f6cdSSteven Whitehouse  */
98f057f6cdSSteven Whitehouse 
99f057f6cdSSteven Whitehouse #define LM_OUT_ST_MASK		0x00000003
100f057f6cdSSteven Whitehouse #define LM_OUT_CANCELED		0x00000008
101921169caSSteven Whitehouse #define LM_OUT_ERROR		0x00000004
102f057f6cdSSteven Whitehouse 
103f057f6cdSSteven Whitehouse /*
104f057f6cdSSteven Whitehouse  * lm_recovery_done() messages
105f057f6cdSSteven Whitehouse  */
106f057f6cdSSteven Whitehouse 
107f057f6cdSSteven Whitehouse #define LM_RD_GAVEUP		308
108f057f6cdSSteven Whitehouse #define LM_RD_SUCCESS		309
109f057f6cdSSteven Whitehouse 
110f057f6cdSSteven Whitehouse #define GLR_TRYFAILED		13
111f057f6cdSSteven Whitehouse 
1127cf8dcd3SBob Peterson #define GL_GLOCK_MAX_HOLD        (long)(HZ / 5)
1137cf8dcd3SBob Peterson #define GL_GLOCK_DFT_HOLD        (long)(HZ / 5)
1147cf8dcd3SBob Peterson #define GL_GLOCK_MIN_HOLD        (long)(10)
1157cf8dcd3SBob Peterson #define GL_GLOCK_HOLD_INCR       (long)(HZ / 20)
1167cf8dcd3SBob Peterson #define GL_GLOCK_HOLD_DECR       (long)(HZ / 40)
1177cf8dcd3SBob Peterson 
118f057f6cdSSteven Whitehouse struct lm_lockops {
119f057f6cdSSteven Whitehouse 	const char *lm_proto_name;
120e0c2a9aaSDavid Teigland 	int (*lm_mount) (struct gfs2_sbd *sdp, const char *table);
121e0c2a9aaSDavid Teigland 	void (*lm_first_done) (struct gfs2_sbd *sdp);
122e0c2a9aaSDavid Teigland 	void (*lm_recovery_result) (struct gfs2_sbd *sdp, unsigned int jid,
123e0c2a9aaSDavid Teigland 				    unsigned int result);
124f057f6cdSSteven Whitehouse 	void (*lm_unmount) (struct gfs2_sbd *sdp);
125f057f6cdSSteven Whitehouse 	void (*lm_withdraw) (struct gfs2_sbd *sdp);
126bc015cb8SSteven Whitehouse 	void (*lm_put_lock) (struct gfs2_glock *gl);
127921169caSSteven Whitehouse 	int (*lm_lock) (struct gfs2_glock *gl, unsigned int req_state,
128921169caSSteven Whitehouse 			unsigned int flags);
129f057f6cdSSteven Whitehouse 	void (*lm_cancel) (struct gfs2_glock *gl);
130f057f6cdSSteven Whitehouse 	const match_table_t *lm_tokens;
131f057f6cdSSteven Whitehouse };
132f057f6cdSSteven Whitehouse 
13311d8b79eSKees Cook struct gfs2_glock_aspace {
13411d8b79eSKees Cook 	struct gfs2_glock glock;
13511d8b79eSKees Cook 	struct address_space mapping;
13611d8b79eSKees Cook };
13711d8b79eSKees Cook 
gfs2_glock_is_locked_by_me(struct gfs2_glock * gl)1387afd88d9SSteven Whitehouse static inline struct gfs2_holder *gfs2_glock_is_locked_by_me(struct gfs2_glock *gl)
139b3b94faaSDavid Teigland {
140b3b94faaSDavid Teigland 	struct gfs2_holder *gh;
141b1e058daSPavel Emelyanov 	struct pid *pid;
142b3b94faaSDavid Teigland 
143b3b94faaSDavid Teigland 	/* Look in glock's list of holders for one with current task as owner */
144f3dd1649SAndreas Gruenbacher 	spin_lock(&gl->gl_lockref.lock);
145b1e058daSPavel Emelyanov 	pid = task_pid(current);
146b3b94faaSDavid Teigland 	list_for_each_entry(gh, &gl->gl_holders, gh_list) {
1476802e340SSteven Whitehouse 		if (!test_bit(HIF_HOLDER, &gh->gh_iflags))
1486802e340SSteven Whitehouse 			break;
1497afd88d9SSteven Whitehouse 		if (gh->gh_owner_pid == pid)
1507afd88d9SSteven Whitehouse 			goto out;
151b3b94faaSDavid Teigland 	}
1527afd88d9SSteven Whitehouse 	gh = NULL;
1537afd88d9SSteven Whitehouse out:
154f3dd1649SAndreas Gruenbacher 	spin_unlock(&gl->gl_lockref.lock);
155b3b94faaSDavid Teigland 
1567afd88d9SSteven Whitehouse 	return gh;
157b3b94faaSDavid Teigland }
158b3b94faaSDavid Teigland 
gfs2_glock_is_held_excl(struct gfs2_glock * gl)159b3b94faaSDavid Teigland static inline int gfs2_glock_is_held_excl(struct gfs2_glock *gl)
160b3b94faaSDavid Teigland {
16150299965SSteven Whitehouse 	return gl->gl_state == LM_ST_EXCLUSIVE;
162b3b94faaSDavid Teigland }
163b3b94faaSDavid Teigland 
gfs2_glock_is_held_dfrd(struct gfs2_glock * gl)164b3b94faaSDavid Teigland static inline int gfs2_glock_is_held_dfrd(struct gfs2_glock *gl)
165b3b94faaSDavid Teigland {
16650299965SSteven Whitehouse 	return gl->gl_state == LM_ST_DEFERRED;
167b3b94faaSDavid Teigland }
168b3b94faaSDavid Teigland 
gfs2_glock_is_held_shrd(struct gfs2_glock * gl)169b3b94faaSDavid Teigland static inline int gfs2_glock_is_held_shrd(struct gfs2_glock *gl)
170b3b94faaSDavid Teigland {
17150299965SSteven Whitehouse 	return gl->gl_state == LM_ST_SHARED;
172b3b94faaSDavid Teigland }
173b3b94faaSDavid Teigland 
gfs2_glock2aspace(struct gfs2_glock * gl)174009d8518SSteven Whitehouse static inline struct address_space *gfs2_glock2aspace(struct gfs2_glock *gl)
175009d8518SSteven Whitehouse {
17611d8b79eSKees Cook 	if (gl->gl_ops->go_flags & GLOF_ASPACE) {
17711d8b79eSKees Cook 		struct gfs2_glock_aspace *gla =
17811d8b79eSKees Cook 			container_of(gl, struct gfs2_glock_aspace, glock);
17911d8b79eSKees Cook 		return &gla->mapping;
18011d8b79eSKees Cook 	}
181009d8518SSteven Whitehouse 	return NULL;
182009d8518SSteven Whitehouse }
183009d8518SSteven Whitehouse 
184d312fbf6SAndreas Gruenbacher int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number,
1858eae1ca0SSteven Whitehouse 		   const struct gfs2_glock_operations *glops,
186b3b94faaSDavid Teigland 		   int create, struct gfs2_glock **glp);
187d312fbf6SAndreas Gruenbacher struct gfs2_glock *gfs2_glock_hold(struct gfs2_glock *gl);
188d312fbf6SAndreas Gruenbacher void gfs2_glock_put(struct gfs2_glock *gl);
189d312fbf6SAndreas Gruenbacher void gfs2_glock_queue_put(struct gfs2_glock *gl);
190b016d9a8SAndreas Gruenbacher 
191d312fbf6SAndreas Gruenbacher void __gfs2_holder_init(struct gfs2_glock *gl, unsigned int state,
192b016d9a8SAndreas Gruenbacher 		        u16 flags, struct gfs2_holder *gh,
193b016d9a8SAndreas Gruenbacher 		        unsigned long ip);
gfs2_holder_init(struct gfs2_glock * gl,unsigned int state,u16 flags,struct gfs2_holder * gh)194b016d9a8SAndreas Gruenbacher static inline void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state,
195b016d9a8SAndreas Gruenbacher 				    u16 flags, struct gfs2_holder *gh) {
196b016d9a8SAndreas Gruenbacher 	__gfs2_holder_init(gl, state, flags, gh, _RET_IP_);
197b016d9a8SAndreas Gruenbacher }
198b016d9a8SAndreas Gruenbacher 
199d312fbf6SAndreas Gruenbacher void gfs2_holder_reinit(unsigned int state, u16 flags,
200b3b94faaSDavid Teigland 		        struct gfs2_holder *gh);
201d312fbf6SAndreas Gruenbacher void gfs2_holder_uninit(struct gfs2_holder *gh);
202d312fbf6SAndreas Gruenbacher int gfs2_glock_nq(struct gfs2_holder *gh);
203d312fbf6SAndreas Gruenbacher int gfs2_glock_poll(struct gfs2_holder *gh);
204d312fbf6SAndreas Gruenbacher int gfs2_instantiate(struct gfs2_holder *gh);
205d312fbf6SAndreas Gruenbacher int gfs2_glock_holder_ready(struct gfs2_holder *gh);
206d312fbf6SAndreas Gruenbacher int gfs2_glock_wait(struct gfs2_holder *gh);
207d312fbf6SAndreas Gruenbacher int gfs2_glock_async_wait(unsigned int num_gh, struct gfs2_holder *ghs);
208d312fbf6SAndreas Gruenbacher void gfs2_glock_dq(struct gfs2_holder *gh);
209d312fbf6SAndreas Gruenbacher void gfs2_glock_dq_wait(struct gfs2_holder *gh);
210d312fbf6SAndreas Gruenbacher void gfs2_glock_dq_uninit(struct gfs2_holder *gh);
211d312fbf6SAndreas Gruenbacher int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number,
2128eae1ca0SSteven Whitehouse 		      const struct gfs2_glock_operations *glops,
213b58bf407SBob Peterson 		      unsigned int state, u16 flags,
214190562bdSSteven Whitehouse 		      struct gfs2_holder *gh);
215d312fbf6SAndreas Gruenbacher int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs);
216d312fbf6SAndreas Gruenbacher void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs);
217d312fbf6SAndreas Gruenbacher void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl,
2183792ce97SBob Peterson 			    bool fsid);
2193792ce97SBob Peterson #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) {		\
2203792ce97SBob Peterson 			gfs2_dump_glock(NULL, gl, true);	\
2213792ce97SBob Peterson 			BUG(); } } while(0)
222ea4e61c7SBob Peterson #define gfs2_glock_assert_warn(gl, x) do { if (unlikely(!(x))) {	\
223ea4e61c7SBob Peterson 			gfs2_dump_glock(NULL, gl, true);		\
224ea4e61c7SBob Peterson 			gfs2_assert_warn((gl)->gl_name.ln_sbd, (x)); } } \
225ea4e61c7SBob Peterson 	while (0)
226ea4e61c7SBob Peterson #define gfs2_glock_assert_withdraw(gl, x) do { if (unlikely(!(x))) {	\
227ea4e61c7SBob Peterson 			gfs2_dump_glock(NULL, gl, true);		\
228ea4e61c7SBob Peterson 			gfs2_assert_withdraw((gl)->gl_name.ln_sbd, (x)); } } \
229ea4e61c7SBob Peterson 	while (0)
230ea4e61c7SBob Peterson 
231d312fbf6SAndreas Gruenbacher __printf(2, 3)
2326802e340SSteven Whitehouse void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...);
233b3b94faaSDavid Teigland 
234d0dc80dbSSteven Whitehouse /**
23533027af6SAndrea Gelmini  * gfs2_glock_nq_init - initialize a holder and enqueue it on a glock
236d0dc80dbSSteven Whitehouse  * @gl: the glock
237d0dc80dbSSteven Whitehouse  * @state: the state we're requesting
238d0dc80dbSSteven Whitehouse  * @flags: the modifier flags
239d0dc80dbSSteven Whitehouse  * @gh: the holder structure
240d0dc80dbSSteven Whitehouse  *
241d0dc80dbSSteven Whitehouse  * Returns: 0, GLR_*, or errno
242d0dc80dbSSteven Whitehouse  */
243d0dc80dbSSteven Whitehouse 
gfs2_glock_nq_init(struct gfs2_glock * gl,unsigned int state,u16 flags,struct gfs2_holder * gh)244d0dc80dbSSteven Whitehouse static inline int gfs2_glock_nq_init(struct gfs2_glock *gl,
245b58bf407SBob Peterson 				     unsigned int state, u16 flags,
246d0dc80dbSSteven Whitehouse 				     struct gfs2_holder *gh)
247d0dc80dbSSteven Whitehouse {
248d0dc80dbSSteven Whitehouse 	int error;
249d0dc80dbSSteven Whitehouse 
250b016d9a8SAndreas Gruenbacher 	__gfs2_holder_init(gl, state, flags, gh, _RET_IP_);
251d0dc80dbSSteven Whitehouse 
252d0dc80dbSSteven Whitehouse 	error = gfs2_glock_nq(gh);
253d0dc80dbSSteven Whitehouse 	if (error)
254d0dc80dbSSteven Whitehouse 		gfs2_holder_uninit(gh);
255d0dc80dbSSteven Whitehouse 
256d0dc80dbSSteven Whitehouse 	return error;
257d0dc80dbSSteven Whitehouse }
258d0dc80dbSSteven Whitehouse 
259d312fbf6SAndreas Gruenbacher void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state);
260d312fbf6SAndreas Gruenbacher void gfs2_glock_complete(struct gfs2_glock *gl, int ret);
261d312fbf6SAndreas Gruenbacher bool gfs2_queue_try_to_evict(struct gfs2_glock *gl);
262d312fbf6SAndreas Gruenbacher void gfs2_cancel_delete_work(struct gfs2_glock *gl);
263d312fbf6SAndreas Gruenbacher void gfs2_flush_delete_work(struct gfs2_sbd *sdp);
264d312fbf6SAndreas Gruenbacher void gfs2_gl_hash_clear(struct gfs2_sbd *sdp);
265d312fbf6SAndreas Gruenbacher void gfs2_gl_dq_holders(struct gfs2_sbd *sdp);
266d312fbf6SAndreas Gruenbacher void gfs2_glock_thaw(struct gfs2_sbd *sdp);
267d312fbf6SAndreas Gruenbacher void gfs2_glock_add_to_lru(struct gfs2_glock *gl);
268d312fbf6SAndreas Gruenbacher void gfs2_glock_free(struct gfs2_glock *gl);
2690636b34bSAndreas Gruenbacher void gfs2_glock_free_later(struct gfs2_glock *gl);
270b3b94faaSDavid Teigland 
271d312fbf6SAndreas Gruenbacher int __init gfs2_glock_init(void);
272d312fbf6SAndreas Gruenbacher void gfs2_glock_exit(void);
273b3b94faaSDavid Teigland 
274d312fbf6SAndreas Gruenbacher void gfs2_create_debugfs_file(struct gfs2_sbd *sdp);
275d312fbf6SAndreas Gruenbacher void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp);
276d312fbf6SAndreas Gruenbacher void gfs2_register_debugfs(void);
277d312fbf6SAndreas Gruenbacher void gfs2_unregister_debugfs(void);
27885d1da67SSteven Whitehouse 
279d312fbf6SAndreas Gruenbacher void glock_set_object(struct gfs2_glock *gl, void *object);
280d312fbf6SAndreas Gruenbacher void glock_clear_object(struct gfs2_glock *gl, void *object);
2813781ec9eSAndreas Gruenbacher 
282f057f6cdSSteven Whitehouse extern const struct lm_lockops gfs2_dlm_ops;
283f057f6cdSSteven Whitehouse 
gfs2_holder_mark_uninitialized(struct gfs2_holder * gh)2846df9f9a2SAndreas Gruenbacher static inline void gfs2_holder_mark_uninitialized(struct gfs2_holder *gh)
2856df9f9a2SAndreas Gruenbacher {
2866df9f9a2SAndreas Gruenbacher 	gh->gh_gl = NULL;
2876df9f9a2SAndreas Gruenbacher }
2886df9f9a2SAndreas Gruenbacher 
gfs2_holder_initialized(struct gfs2_holder * gh)2896df9f9a2SAndreas Gruenbacher static inline bool gfs2_holder_initialized(struct gfs2_holder *gh)
2906df9f9a2SAndreas Gruenbacher {
2916df9f9a2SAndreas Gruenbacher 	return gh->gh_gl;
2926df9f9a2SAndreas Gruenbacher }
2936df9f9a2SAndreas Gruenbacher 
gfs2_holder_queued(struct gfs2_holder * gh)294ad26967bSBob Peterson static inline bool gfs2_holder_queued(struct gfs2_holder *gh)
295ad26967bSBob Peterson {
296ad26967bSBob Peterson 	return !list_empty(&gh->gh_list);
297ad26967bSBob Peterson }
298ad26967bSBob Peterson 
299d312fbf6SAndreas Gruenbacher void gfs2_inode_remember_delete(struct gfs2_glock *gl, u64 generation);
300d312fbf6SAndreas Gruenbacher bool gfs2_inode_already_deleted(struct gfs2_glock *gl, u64 generation);
301f286d627SAndreas Gruenbacher 
302b3b94faaSDavid Teigland #endif /* __GLOCK_DOT_H__ */
303