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