xref: /openbmc/linux/fs/dlm/dlm_internal.h (revision 4875647a08e35f77274838d97ca8fa44158d50e2)
1e7fd4179SDavid Teigland /******************************************************************************
2e7fd4179SDavid Teigland *******************************************************************************
3e7fd4179SDavid Teigland **
4e7fd4179SDavid Teigland **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
560f98d18SDavid Teigland **  Copyright (C) 2004-2011 Red Hat, Inc.  All rights reserved.
6e7fd4179SDavid Teigland **
7e7fd4179SDavid Teigland **  This copyrighted material is made available to anyone wishing to use,
8e7fd4179SDavid Teigland **  modify, copy, or redistribute it subject to the terms and conditions
9e7fd4179SDavid Teigland **  of the GNU General Public License v.2.
10e7fd4179SDavid Teigland **
11e7fd4179SDavid Teigland *******************************************************************************
12e7fd4179SDavid Teigland ******************************************************************************/
13e7fd4179SDavid Teigland 
14e7fd4179SDavid Teigland #ifndef __DLM_INTERNAL_DOT_H__
15e7fd4179SDavid Teigland #define __DLM_INTERNAL_DOT_H__
16e7fd4179SDavid Teigland 
17e7fd4179SDavid Teigland /*
18e7fd4179SDavid Teigland  * This is the main header file to be included in each DLM source file.
19e7fd4179SDavid Teigland  */
20e7fd4179SDavid Teigland 
21e7fd4179SDavid Teigland #include <linux/module.h>
22e7fd4179SDavid Teigland #include <linux/slab.h>
23e7fd4179SDavid Teigland #include <linux/sched.h>
24e7fd4179SDavid Teigland #include <linux/types.h>
25e7fd4179SDavid Teigland #include <linux/ctype.h>
26e7fd4179SDavid Teigland #include <linux/spinlock.h>
27e7fd4179SDavid Teigland #include <linux/vmalloc.h>
28e7fd4179SDavid Teigland #include <linux/list.h>
29e7fd4179SDavid Teigland #include <linux/errno.h>
30e7fd4179SDavid Teigland #include <linux/random.h>
31e7fd4179SDavid Teigland #include <linux/delay.h>
32e7fd4179SDavid Teigland #include <linux/socket.h>
33e7fd4179SDavid Teigland #include <linux/kthread.h>
34e7fd4179SDavid Teigland #include <linux/kobject.h>
35e7fd4179SDavid Teigland #include <linux/kref.h>
36e7fd4179SDavid Teigland #include <linux/kernel.h>
37e7fd4179SDavid Teigland #include <linux/jhash.h>
38597d0caeSDavid Teigland #include <linux/miscdevice.h>
3990135925SDavid Teigland #include <linux/mutex.h>
403d6aa675SDavid Teigland #include <linux/idr.h>
41d6e24788SDavid Teigland #include <linux/ratelimit.h>
42e7fd4179SDavid Teigland #include <asm/uaccess.h>
43e7fd4179SDavid Teigland 
44e7fd4179SDavid Teigland #include <linux/dlm.h>
4599fc6487SDavid Teigland #include "config.h"
46e7fd4179SDavid Teigland 
47e7fd4179SDavid Teigland /* Size of the temp buffer midcomms allocates on the stack.
48e7fd4179SDavid Teigland    We try to make this large enough so most messages fit.
49e7fd4179SDavid Teigland    FIXME: should sctp make this unnecessary? */
50e7fd4179SDavid Teigland 
51e7fd4179SDavid Teigland #define DLM_INBUF_LEN		148
52e7fd4179SDavid Teigland 
53e7fd4179SDavid Teigland struct dlm_ls;
54e7fd4179SDavid Teigland struct dlm_lkb;
55e7fd4179SDavid Teigland struct dlm_rsb;
56e7fd4179SDavid Teigland struct dlm_member;
57e7fd4179SDavid Teigland struct dlm_rsbtable;
58e7fd4179SDavid Teigland struct dlm_dirtable;
59e7fd4179SDavid Teigland struct dlm_direntry;
60e7fd4179SDavid Teigland struct dlm_recover;
61e7fd4179SDavid Teigland struct dlm_header;
62e7fd4179SDavid Teigland struct dlm_message;
63e7fd4179SDavid Teigland struct dlm_rcom;
64e7fd4179SDavid Teigland struct dlm_mhandle;
65e7fd4179SDavid Teigland 
66e7fd4179SDavid Teigland #define log_print(fmt, args...) \
67e7fd4179SDavid Teigland 	printk(KERN_ERR "dlm: "fmt"\n" , ##args)
68e7fd4179SDavid Teigland #define log_error(ls, fmt, args...) \
69e7fd4179SDavid Teigland 	printk(KERN_ERR "dlm: %s: " fmt "\n", (ls)->ls_name , ##args)
70e7fd4179SDavid Teigland 
7199fc6487SDavid Teigland #define log_debug(ls, fmt, args...) \
7299fc6487SDavid Teigland do { \
7399fc6487SDavid Teigland 	if (dlm_config.ci_log_debug) \
7499fc6487SDavid Teigland 		printk(KERN_DEBUG "dlm: %s: " fmt "\n", \
7599fc6487SDavid Teigland 		       (ls)->ls_name , ##args); \
7699fc6487SDavid Teigland } while (0)
77e7fd4179SDavid Teigland 
78d6e24788SDavid Teigland #define log_limit(ls, fmt, args...) \
79d6e24788SDavid Teigland do { \
80d6e24788SDavid Teigland 	if (dlm_config.ci_log_debug) \
81d6e24788SDavid Teigland 		printk_ratelimited(KERN_DEBUG "dlm: %s: " fmt "\n", \
82d6e24788SDavid Teigland 			(ls)->ls_name , ##args); \
83d6e24788SDavid Teigland } while (0)
84d6e24788SDavid Teigland 
85e7fd4179SDavid Teigland #define DLM_ASSERT(x, do) \
86e7fd4179SDavid Teigland { \
87e7fd4179SDavid Teigland   if (!(x)) \
88e7fd4179SDavid Teigland   { \
89e7fd4179SDavid Teigland     printk(KERN_ERR "\nDLM:  Assertion failed on line %d of file %s\n" \
90e7fd4179SDavid Teigland                "DLM:  assertion:  \"%s\"\n" \
91e7fd4179SDavid Teigland                "DLM:  time = %lu\n", \
92e7fd4179SDavid Teigland                __LINE__, __FILE__, #x, jiffies); \
93e7fd4179SDavid Teigland     {do} \
94e7fd4179SDavid Teigland     printk("\n"); \
95e7fd4179SDavid Teigland     BUG(); \
96e7fd4179SDavid Teigland     panic("DLM:  Record message above and reboot.\n"); \
97e7fd4179SDavid Teigland   } \
98e7fd4179SDavid Teigland }
99e7fd4179SDavid Teigland 
100e7fd4179SDavid Teigland 
101e7fd4179SDavid Teigland struct dlm_direntry {
102e7fd4179SDavid Teigland 	struct list_head	list;
103e7fd4179SDavid Teigland 	uint32_t		master_nodeid;
104e7fd4179SDavid Teigland 	uint16_t		length;
105e7fd4179SDavid Teigland 	char			name[1];
106e7fd4179SDavid Teigland };
107e7fd4179SDavid Teigland 
108e7fd4179SDavid Teigland struct dlm_dirtable {
109e7fd4179SDavid Teigland 	struct list_head	list;
110305a47b1SSteven Whitehouse 	spinlock_t		lock;
111e7fd4179SDavid Teigland };
112e7fd4179SDavid Teigland 
113e7fd4179SDavid Teigland struct dlm_rsbtable {
1149beb3bf5SBob Peterson 	struct rb_root		keep;
1159beb3bf5SBob Peterson 	struct rb_root		toss;
116c7be761aSDavid Teigland 	spinlock_t		lock;
117e7fd4179SDavid Teigland };
118e7fd4179SDavid Teigland 
119e7fd4179SDavid Teigland 
120e7fd4179SDavid Teigland /*
121e7fd4179SDavid Teigland  * Lockspace member (per node in a ls)
122e7fd4179SDavid Teigland  */
123e7fd4179SDavid Teigland 
124e7fd4179SDavid Teigland struct dlm_member {
125e7fd4179SDavid Teigland 	struct list_head	list;
126e7fd4179SDavid Teigland 	int			nodeid;
127e7fd4179SDavid Teigland 	int			weight;
128757a4271SDavid Teigland 	int			slot;
129757a4271SDavid Teigland 	int			slot_prev;
13060f98d18SDavid Teigland 	int			comm_seq;
131757a4271SDavid Teigland 	uint32_t		generation;
132757a4271SDavid Teigland };
133757a4271SDavid Teigland 
134757a4271SDavid Teigland /*
135e7fd4179SDavid Teigland  * Save and manage recovery state for a lockspace.
136e7fd4179SDavid Teigland  */
137e7fd4179SDavid Teigland 
138e7fd4179SDavid Teigland struct dlm_recover {
139e7fd4179SDavid Teigland 	struct list_head	list;
14060f98d18SDavid Teigland 	struct dlm_config_node	*nodes;
14160f98d18SDavid Teigland 	int			nodes_count;
142e7fd4179SDavid Teigland 	uint64_t		seq;
143e7fd4179SDavid Teigland };
144e7fd4179SDavid Teigland 
145e7fd4179SDavid Teigland /*
146e7fd4179SDavid Teigland  * Pass input args to second stage locking function.
147e7fd4179SDavid Teigland  */
148e7fd4179SDavid Teigland 
149e7fd4179SDavid Teigland struct dlm_args {
150e7fd4179SDavid Teigland 	uint32_t		flags;
151e5dae548SDavid Teigland 	void			(*astfn) (void *astparam);
152e5dae548SDavid Teigland 	void			*astparam;
153e5dae548SDavid Teigland 	void			(*bastfn) (void *astparam, int mode);
154e7fd4179SDavid Teigland 	int			mode;
155e7fd4179SDavid Teigland 	struct dlm_lksb		*lksb;
156d7db923eSDavid Teigland 	unsigned long		timeout;
157e7fd4179SDavid Teigland };
158e7fd4179SDavid Teigland 
159e7fd4179SDavid Teigland 
160e7fd4179SDavid Teigland /*
161e7fd4179SDavid Teigland  * Lock block
162e7fd4179SDavid Teigland  *
163e7fd4179SDavid Teigland  * A lock can be one of three types:
164e7fd4179SDavid Teigland  *
165e7fd4179SDavid Teigland  * local copy      lock is mastered locally
166e7fd4179SDavid Teigland  *                 (lkb_nodeid is zero and DLM_LKF_MSTCPY is not set)
167e7fd4179SDavid Teigland  * process copy    lock is mastered on a remote node
168e7fd4179SDavid Teigland  *                 (lkb_nodeid is non-zero and DLM_LKF_MSTCPY is not set)
169e7fd4179SDavid Teigland  * master copy     master node's copy of a lock owned by remote node
170e7fd4179SDavid Teigland  *                 (lkb_nodeid is non-zero and DLM_LKF_MSTCPY is set)
171e7fd4179SDavid Teigland  *
172e7fd4179SDavid Teigland  * lkb_exflags: a copy of the most recent flags arg provided to dlm_lock or
173e7fd4179SDavid Teigland  * dlm_unlock.  The dlm does not modify these or use any private flags in
174e7fd4179SDavid Teigland  * this field; it only contains DLM_LKF_ flags from dlm.h.  These flags
175e7fd4179SDavid Teigland  * are sent as-is to the remote master when the lock is remote.
176e7fd4179SDavid Teigland  *
177e7fd4179SDavid Teigland  * lkb_flags: internal dlm flags (DLM_IFL_ prefix) from dlm_internal.h.
178e7fd4179SDavid Teigland  * Some internal flags are shared between the master and process nodes;
179e7fd4179SDavid Teigland  * these shared flags are kept in the lower two bytes.  One of these
180e7fd4179SDavid Teigland  * flags set on the master copy will be propagated to the process copy
181e7fd4179SDavid Teigland  * and v.v.  Other internal flags are private to the master or process
182e7fd4179SDavid Teigland  * node (e.g. DLM_IFL_MSTCPY).  These are kept in the high two bytes.
183e7fd4179SDavid Teigland  *
184e7fd4179SDavid Teigland  * lkb_sbflags: status block flags.  These flags are copied directly into
185e7fd4179SDavid Teigland  * the caller's lksb.sb_flags prior to the dlm_lock/dlm_unlock completion
186e7fd4179SDavid Teigland  * ast.  All defined in dlm.h with DLM_SBF_ prefix.
187e7fd4179SDavid Teigland  *
188e7fd4179SDavid Teigland  * lkb_status: the lock status indicates which rsb queue the lock is
189e7fd4179SDavid Teigland  * on, grant, convert, or wait.  DLM_LKSTS_ WAITING/GRANTED/CONVERT
190e7fd4179SDavid Teigland  *
191e7fd4179SDavid Teigland  * lkb_wait_type: the dlm message type (DLM_MSG_ prefix) for which a
192e7fd4179SDavid Teigland  * reply is needed.  Only set when the lkb is on the lockspace waiters
193e7fd4179SDavid Teigland  * list awaiting a reply from a remote node.
194e7fd4179SDavid Teigland  *
195e7fd4179SDavid Teigland  * lkb_nodeid: when the lkb is a local copy, nodeid is 0; when the lkb
196e7fd4179SDavid Teigland  * is a master copy, nodeid specifies the remote lock holder, when the
197e7fd4179SDavid Teigland  * lkb is a process copy, the nodeid specifies the lock master.
198e7fd4179SDavid Teigland  */
199e7fd4179SDavid Teigland 
200e7fd4179SDavid Teigland /* lkb_status */
201e7fd4179SDavid Teigland 
202e7fd4179SDavid Teigland #define DLM_LKSTS_WAITING	1
203e7fd4179SDavid Teigland #define DLM_LKSTS_GRANTED	2
204e7fd4179SDavid Teigland #define DLM_LKSTS_CONVERT	3
205e7fd4179SDavid Teigland 
206e7fd4179SDavid Teigland /* lkb_flags */
207e7fd4179SDavid Teigland 
208e7fd4179SDavid Teigland #define DLM_IFL_MSTCPY		0x00010000
209e7fd4179SDavid Teigland #define DLM_IFL_RESEND		0x00020000
210597d0caeSDavid Teigland #define DLM_IFL_DEAD		0x00040000
211ef0c2bb0SDavid Teigland #define DLM_IFL_OVERLAP_UNLOCK  0x00080000
212ef0c2bb0SDavid Teigland #define DLM_IFL_OVERLAP_CANCEL  0x00100000
213ef0c2bb0SDavid Teigland #define DLM_IFL_ENDOFLIFE	0x00200000
2143ae1acf9SDavid Teigland #define DLM_IFL_WATCH_TIMEWARN	0x00400000
21584d8cd69SDavid Teigland #define DLM_IFL_TIMEOUT_CANCEL	0x00800000
2168b4021faSDavid Teigland #define DLM_IFL_DEADLOCK_CANCEL	0x01000000
2172a7ce0edSDavid Teigland #define DLM_IFL_STUB_MS		0x02000000 /* magic number for m_flags */
218597d0caeSDavid Teigland #define DLM_IFL_USER		0x00000001
219597d0caeSDavid Teigland #define DLM_IFL_ORPHAN		0x00000002
220e7fd4179SDavid Teigland 
2218304d6f2SDavid Teigland #define DLM_CALLBACKS_SIZE	6
2228304d6f2SDavid Teigland 
2238304d6f2SDavid Teigland #define DLM_CB_CAST		0x00000001
2248304d6f2SDavid Teigland #define DLM_CB_BAST		0x00000002
2258304d6f2SDavid Teigland #define DLM_CB_SKIP		0x00000004
2268304d6f2SDavid Teigland 
2278304d6f2SDavid Teigland struct dlm_callback {
2288304d6f2SDavid Teigland 	uint64_t		seq;
2298304d6f2SDavid Teigland 	uint32_t		flags;		/* DLM_CBF_ */
2308304d6f2SDavid Teigland 	int			sb_status;	/* copy to lksb status */
2318304d6f2SDavid Teigland 	uint8_t			sb_flags;	/* copy to lksb flags */
2328304d6f2SDavid Teigland 	int8_t			mode; /* rq mode of bast, gr mode of cast */
2338304d6f2SDavid Teigland };
2348304d6f2SDavid Teigland 
235e7fd4179SDavid Teigland struct dlm_lkb {
236e7fd4179SDavid Teigland 	struct dlm_rsb		*lkb_resource;	/* the rsb */
237e7fd4179SDavid Teigland 	struct kref		lkb_ref;
238e7fd4179SDavid Teigland 	int			lkb_nodeid;	/* copied from rsb */
239e7fd4179SDavid Teigland 	int			lkb_ownpid;	/* pid of lock owner */
240e7fd4179SDavid Teigland 	uint32_t		lkb_id;		/* our lock ID */
241e7fd4179SDavid Teigland 	uint32_t		lkb_remid;	/* lock ID on remote partner */
242e7fd4179SDavid Teigland 	uint32_t		lkb_exflags;	/* external flags from caller */
243e7fd4179SDavid Teigland 	uint32_t		lkb_sbflags;	/* lksb flags */
244e7fd4179SDavid Teigland 	uint32_t		lkb_flags;	/* internal flags */
245e7fd4179SDavid Teigland 	uint32_t		lkb_lvbseq;	/* lvb sequence number */
246e7fd4179SDavid Teigland 
247e7fd4179SDavid Teigland 	int8_t			lkb_status;     /* granted, waiting, convert */
248e7fd4179SDavid Teigland 	int8_t			lkb_rqmode;	/* requested lock mode */
249e7fd4179SDavid Teigland 	int8_t			lkb_grmode;	/* granted lock mode */
250e7fd4179SDavid Teigland 	int8_t			lkb_highbast;	/* highest mode bast sent for */
2517fe2b319SDavid Teigland 
252e7fd4179SDavid Teigland 	int8_t			lkb_wait_type;	/* type of reply waiting for */
253ef0c2bb0SDavid Teigland 	int8_t			lkb_wait_count;
254c6ff669bSDavid Teigland 	int			lkb_wait_nodeid; /* for debugging */
255e7fd4179SDavid Teigland 
256e7fd4179SDavid Teigland 	struct list_head	lkb_statequeue;	/* rsb g/c/w list */
257e7fd4179SDavid Teigland 	struct list_head	lkb_rsb_lookup;	/* waiting for rsb lookup */
258e7fd4179SDavid Teigland 	struct list_head	lkb_wait_reply;	/* waiting for remote reply */
259597d0caeSDavid Teigland 	struct list_head	lkb_ownqueue;	/* list of locks for a process */
2603ae1acf9SDavid Teigland 	struct list_head	lkb_time_list;
261eeda418dSDavid Teigland 	ktime_t			lkb_timestamp;
262c6ff669bSDavid Teigland 	ktime_t			lkb_wait_time;
2633ae1acf9SDavid Teigland 	unsigned long		lkb_timeout_cs;
264e7fd4179SDavid Teigland 
26523e8e1aaSDavid Teigland 	struct mutex		lkb_cb_mutex;
26623e8e1aaSDavid Teigland 	struct work_struct	lkb_cb_work;
26723e8e1aaSDavid Teigland 	struct list_head	lkb_cb_list; /* for ls_cb_delay or proc->asts */
2688304d6f2SDavid Teigland 	struct dlm_callback	lkb_callbacks[DLM_CALLBACKS_SIZE];
2698304d6f2SDavid Teigland 	struct dlm_callback	lkb_last_cast;
2708304d6f2SDavid Teigland 	struct dlm_callback	lkb_last_bast;
2718304d6f2SDavid Teigland 	ktime_t			lkb_last_cast_time;	/* for debugging */
2728304d6f2SDavid Teigland 	ktime_t			lkb_last_bast_time;	/* for debugging */
2738304d6f2SDavid Teigland 
274*4875647aSDavid Teigland 	uint64_t		lkb_recover_seq; /* from ls_recover_seq */
275*4875647aSDavid Teigland 
276e7fd4179SDavid Teigland 	char			*lkb_lvbptr;
277e7fd4179SDavid Teigland 	struct dlm_lksb		*lkb_lksb;      /* caller's status block */
278e5dae548SDavid Teigland 	void			(*lkb_astfn) (void *astparam);
279e5dae548SDavid Teigland 	void			(*lkb_bastfn) (void *astparam, int mode);
280d292c0ccSDavid Teigland 	union {
281e5dae548SDavid Teigland 		void			*lkb_astparam;	/* caller's ast arg */
282d292c0ccSDavid Teigland 		struct dlm_user_args	*lkb_ua;
283d292c0ccSDavid Teigland 	};
284e7fd4179SDavid Teigland };
285e7fd4179SDavid Teigland 
286e7fd4179SDavid Teigland 
287e7fd4179SDavid Teigland struct dlm_rsb {
288e7fd4179SDavid Teigland 	struct dlm_ls		*res_ls;	/* the lockspace */
289e7fd4179SDavid Teigland 	struct kref		res_ref;
29090135925SDavid Teigland 	struct mutex		res_mutex;
291e7fd4179SDavid Teigland 	unsigned long		res_flags;
292e7fd4179SDavid Teigland 	int			res_length;	/* length of rsb name */
293e7fd4179SDavid Teigland 	int			res_nodeid;
294e7fd4179SDavid Teigland 	uint32_t                res_lvbseq;
295e7fd4179SDavid Teigland 	uint32_t		res_hash;
296e7fd4179SDavid Teigland 	uint32_t		res_bucket;	/* rsbtbl */
297e7fd4179SDavid Teigland 	unsigned long		res_toss_time;
298e7fd4179SDavid Teigland 	uint32_t		res_first_lkid;
299e7fd4179SDavid Teigland 	struct list_head	res_lookup;	/* lkbs waiting on first */
3009beb3bf5SBob Peterson 	union {
3019beb3bf5SBob Peterson 		struct list_head	res_hashchain;
3029beb3bf5SBob Peterson 		struct rb_node		res_hashnode;	/* rsbtbl */
3039beb3bf5SBob Peterson 	};
304e7fd4179SDavid Teigland 	struct list_head	res_grantqueue;
305e7fd4179SDavid Teigland 	struct list_head	res_convertqueue;
306e7fd4179SDavid Teigland 	struct list_head	res_waitqueue;
307e7fd4179SDavid Teigland 
308e7fd4179SDavid Teigland 	struct list_head	res_root_list;	    /* used for recovery */
309e7fd4179SDavid Teigland 	struct list_head	res_recover_list;   /* used for recovery */
310e7fd4179SDavid Teigland 	int			res_recover_locks_count;
311e7fd4179SDavid Teigland 
312e7fd4179SDavid Teigland 	char			*res_lvbptr;
3133881ac04SDavid Teigland 	char			res_name[DLM_RESNAME_MAXLEN+1];
314e7fd4179SDavid Teigland };
315e7fd4179SDavid Teigland 
316e7fd4179SDavid Teigland /* find_rsb() flags */
317e7fd4179SDavid Teigland 
318e7fd4179SDavid Teigland #define R_MASTER		1	/* only return rsb if it's a master */
319e7fd4179SDavid Teigland #define R_CREATE		2	/* create/add rsb if not found */
320e7fd4179SDavid Teigland 
321e7fd4179SDavid Teigland /* rsb_flags */
322e7fd4179SDavid Teigland 
323e7fd4179SDavid Teigland enum rsb_flags {
324e7fd4179SDavid Teigland 	RSB_MASTER_UNCERTAIN,
325e7fd4179SDavid Teigland 	RSB_VALNOTVALID,
326e7fd4179SDavid Teigland 	RSB_VALNOTVALID_PREV,
327e7fd4179SDavid Teigland 	RSB_NEW_MASTER,
328e7fd4179SDavid Teigland 	RSB_NEW_MASTER2,
329e7fd4179SDavid Teigland 	RSB_RECOVER_CONVERT,
330*4875647aSDavid Teigland 	RSB_RECOVER_GRANT,
331e7fd4179SDavid Teigland };
332e7fd4179SDavid Teigland 
333e7fd4179SDavid Teigland static inline void rsb_set_flag(struct dlm_rsb *r, enum rsb_flags flag)
334e7fd4179SDavid Teigland {
335e7fd4179SDavid Teigland 	__set_bit(flag, &r->res_flags);
336e7fd4179SDavid Teigland }
337e7fd4179SDavid Teigland 
338e7fd4179SDavid Teigland static inline void rsb_clear_flag(struct dlm_rsb *r, enum rsb_flags flag)
339e7fd4179SDavid Teigland {
340e7fd4179SDavid Teigland 	__clear_bit(flag, &r->res_flags);
341e7fd4179SDavid Teigland }
342e7fd4179SDavid Teigland 
343e7fd4179SDavid Teigland static inline int rsb_flag(struct dlm_rsb *r, enum rsb_flags flag)
344e7fd4179SDavid Teigland {
345e7fd4179SDavid Teigland 	return test_bit(flag, &r->res_flags);
346e7fd4179SDavid Teigland }
347e7fd4179SDavid Teigland 
348e7fd4179SDavid Teigland 
349e7fd4179SDavid Teigland /* dlm_header is first element of all structs sent between nodes */
350e7fd4179SDavid Teigland 
35138aa8b0cSDavid Teigland #define DLM_HEADER_MAJOR	0x00030000
352757a4271SDavid Teigland #define DLM_HEADER_MINOR	0x00000001
353757a4271SDavid Teigland 
354757a4271SDavid Teigland #define DLM_HEADER_SLOTS	0x00000001
355e7fd4179SDavid Teigland 
356e7fd4179SDavid Teigland #define DLM_MSG			1
357e7fd4179SDavid Teigland #define DLM_RCOM		2
358e7fd4179SDavid Teigland 
359e7fd4179SDavid Teigland struct dlm_header {
360e7fd4179SDavid Teigland 	uint32_t		h_version;
361e7fd4179SDavid Teigland 	uint32_t		h_lockspace;
362e7fd4179SDavid Teigland 	uint32_t		h_nodeid;	/* nodeid of sender */
363e7fd4179SDavid Teigland 	uint16_t		h_length;
364e7fd4179SDavid Teigland 	uint8_t			h_cmd;		/* DLM_MSG, DLM_RCOM */
365e7fd4179SDavid Teigland 	uint8_t			h_pad;
366e7fd4179SDavid Teigland };
367e7fd4179SDavid Teigland 
368e7fd4179SDavid Teigland 
369e7fd4179SDavid Teigland #define DLM_MSG_REQUEST		1
370e7fd4179SDavid Teigland #define DLM_MSG_CONVERT		2
371e7fd4179SDavid Teigland #define DLM_MSG_UNLOCK		3
372e7fd4179SDavid Teigland #define DLM_MSG_CANCEL		4
373e7fd4179SDavid Teigland #define DLM_MSG_REQUEST_REPLY	5
374e7fd4179SDavid Teigland #define DLM_MSG_CONVERT_REPLY	6
375e7fd4179SDavid Teigland #define DLM_MSG_UNLOCK_REPLY	7
376e7fd4179SDavid Teigland #define DLM_MSG_CANCEL_REPLY	8
377e7fd4179SDavid Teigland #define DLM_MSG_GRANT		9
378e7fd4179SDavid Teigland #define DLM_MSG_BAST		10
379e7fd4179SDavid Teigland #define DLM_MSG_LOOKUP		11
380e7fd4179SDavid Teigland #define DLM_MSG_REMOVE		12
381e7fd4179SDavid Teigland #define DLM_MSG_LOOKUP_REPLY	13
3828499137dSDavid Teigland #define DLM_MSG_PURGE		14
383e7fd4179SDavid Teigland 
384e7fd4179SDavid Teigland struct dlm_message {
385e7fd4179SDavid Teigland 	struct dlm_header	m_header;
386e7fd4179SDavid Teigland 	uint32_t		m_type;		/* DLM_MSG_ */
387e7fd4179SDavid Teigland 	uint32_t		m_nodeid;
388e7fd4179SDavid Teigland 	uint32_t		m_pid;
389e7fd4179SDavid Teigland 	uint32_t		m_lkid;		/* lkid on sender */
390e7fd4179SDavid Teigland 	uint32_t		m_remid;	/* lkid on receiver */
391e7fd4179SDavid Teigland 	uint32_t		m_parent_lkid;
392e7fd4179SDavid Teigland 	uint32_t		m_parent_remid;
393e7fd4179SDavid Teigland 	uint32_t		m_exflags;
394e7fd4179SDavid Teigland 	uint32_t		m_sbflags;
395e7fd4179SDavid Teigland 	uint32_t		m_flags;
396e7fd4179SDavid Teigland 	uint32_t		m_lvbseq;
397e7fd4179SDavid Teigland 	uint32_t		m_hash;
398e7fd4179SDavid Teigland 	int			m_status;
399e7fd4179SDavid Teigland 	int			m_grmode;
400e7fd4179SDavid Teigland 	int			m_rqmode;
401e7fd4179SDavid Teigland 	int			m_bastmode;
402e7fd4179SDavid Teigland 	int			m_asts;
403e7fd4179SDavid Teigland 	int			m_result;	/* 0 or -EXXX */
404e7fd4179SDavid Teigland 	char			m_extra[0];	/* name or lvb */
405e7fd4179SDavid Teigland };
406e7fd4179SDavid Teigland 
407e7fd4179SDavid Teigland 
408e7fd4179SDavid Teigland #define DLM_RS_NODES		0x00000001
409e7fd4179SDavid Teigland #define DLM_RS_NODES_ALL	0x00000002
410e7fd4179SDavid Teigland #define DLM_RS_DIR		0x00000004
411e7fd4179SDavid Teigland #define DLM_RS_DIR_ALL		0x00000008
412e7fd4179SDavid Teigland #define DLM_RS_LOCKS		0x00000010
413e7fd4179SDavid Teigland #define DLM_RS_LOCKS_ALL	0x00000020
414e7fd4179SDavid Teigland #define DLM_RS_DONE		0x00000040
415e7fd4179SDavid Teigland #define DLM_RS_DONE_ALL		0x00000080
416e7fd4179SDavid Teigland 
417e7fd4179SDavid Teigland #define DLM_RCOM_STATUS		1
418e7fd4179SDavid Teigland #define DLM_RCOM_NAMES		2
419e7fd4179SDavid Teigland #define DLM_RCOM_LOOKUP		3
420e7fd4179SDavid Teigland #define DLM_RCOM_LOCK		4
421e7fd4179SDavid Teigland #define DLM_RCOM_STATUS_REPLY	5
422e7fd4179SDavid Teigland #define DLM_RCOM_NAMES_REPLY	6
423e7fd4179SDavid Teigland #define DLM_RCOM_LOOKUP_REPLY	7
424e7fd4179SDavid Teigland #define DLM_RCOM_LOCK_REPLY	8
425e7fd4179SDavid Teigland 
426e7fd4179SDavid Teigland struct dlm_rcom {
427e7fd4179SDavid Teigland 	struct dlm_header	rc_header;
428e7fd4179SDavid Teigland 	uint32_t		rc_type;	/* DLM_RCOM_ */
429e7fd4179SDavid Teigland 	int			rc_result;	/* multi-purpose */
430e7fd4179SDavid Teigland 	uint64_t		rc_id;		/* match reply with request */
43138aa8b0cSDavid Teigland 	uint64_t		rc_seq;		/* sender's ls_recover_seq */
43238aa8b0cSDavid Teigland 	uint64_t		rc_seq_reply;	/* remote ls_recover_seq */
433e7fd4179SDavid Teigland 	char			rc_buf[0];
434e7fd4179SDavid Teigland };
435e7fd4179SDavid Teigland 
436eef7d739SAl Viro union dlm_packet {
437eef7d739SAl Viro 	struct dlm_header	header;		/* common to other two */
438eef7d739SAl Viro 	struct dlm_message	message;
439eef7d739SAl Viro 	struct dlm_rcom		rcom;
440eef7d739SAl Viro };
441eef7d739SAl Viro 
442757a4271SDavid Teigland #define DLM_RSF_NEED_SLOTS	0x00000001
443757a4271SDavid Teigland 
444757a4271SDavid Teigland /* RCOM_STATUS data */
445757a4271SDavid Teigland struct rcom_status {
446757a4271SDavid Teigland 	__le32			rs_flags;
447757a4271SDavid Teigland 	__le32			rs_unused1;
448757a4271SDavid Teigland 	__le64			rs_unused2;
449757a4271SDavid Teigland };
450757a4271SDavid Teigland 
451757a4271SDavid Teigland /* RCOM_STATUS_REPLY data */
452e7fd4179SDavid Teigland struct rcom_config {
45393ff2971SAl Viro 	__le32			rf_lvblen;
45493ff2971SAl Viro 	__le32			rf_lsflags;
455757a4271SDavid Teigland 
456757a4271SDavid Teigland 	/* DLM_HEADER_SLOTS adds: */
457757a4271SDavid Teigland 	__le32			rf_flags;
458757a4271SDavid Teigland 	__le16			rf_our_slot;
459757a4271SDavid Teigland 	__le16			rf_num_slots;
460757a4271SDavid Teigland 	__le32			rf_generation;
461757a4271SDavid Teigland 	__le32			rf_unused1;
462757a4271SDavid Teigland 	__le64			rf_unused2;
463757a4271SDavid Teigland };
464757a4271SDavid Teigland 
465757a4271SDavid Teigland struct rcom_slot {
466757a4271SDavid Teigland 	__le32			ro_nodeid;
467757a4271SDavid Teigland 	__le16			ro_slot;
468757a4271SDavid Teigland 	__le16			ro_unused1;
469757a4271SDavid Teigland 	__le64			ro_unused2;
470e7fd4179SDavid Teigland };
471e7fd4179SDavid Teigland 
472e7fd4179SDavid Teigland struct rcom_lock {
473163a1859SAl Viro 	__le32			rl_ownpid;
474163a1859SAl Viro 	__le32			rl_lkid;
475163a1859SAl Viro 	__le32			rl_remid;
476163a1859SAl Viro 	__le32			rl_parent_lkid;
477163a1859SAl Viro 	__le32			rl_parent_remid;
478163a1859SAl Viro 	__le32			rl_exflags;
479163a1859SAl Viro 	__le32			rl_flags;
480163a1859SAl Viro 	__le32			rl_lvbseq;
481163a1859SAl Viro 	__le32			rl_result;
482e7fd4179SDavid Teigland 	int8_t			rl_rqmode;
483e7fd4179SDavid Teigland 	int8_t			rl_grmode;
484e7fd4179SDavid Teigland 	int8_t			rl_status;
485e7fd4179SDavid Teigland 	int8_t			rl_asts;
486163a1859SAl Viro 	__le16			rl_wait_type;
487163a1859SAl Viro 	__le16			rl_namelen;
488e7fd4179SDavid Teigland 	char			rl_name[DLM_RESNAME_MAXLEN];
489e7fd4179SDavid Teigland 	char			rl_lvb[0];
490e7fd4179SDavid Teigland };
491e7fd4179SDavid Teigland 
492e7fd4179SDavid Teigland struct dlm_ls {
493e7fd4179SDavid Teigland 	struct list_head	ls_list;	/* list of lockspaces */
494597d0caeSDavid Teigland 	dlm_lockspace_t		*ls_local_handle;
495e7fd4179SDavid Teigland 	uint32_t		ls_global_id;	/* global unique lockspace ID */
496757a4271SDavid Teigland 	uint32_t		ls_generation;
497e7fd4179SDavid Teigland 	uint32_t		ls_exflags;
498e7fd4179SDavid Teigland 	int			ls_lvblen;
4990f8e0d9aSDavid Teigland 	int			ls_count;	/* refcount of processes in
5000f8e0d9aSDavid Teigland 						   the dlm using this ls */
5010f8e0d9aSDavid Teigland 	int			ls_create_count; /* create/release refcount */
502e7fd4179SDavid Teigland 	unsigned long		ls_flags;	/* LSFL_ */
503c1dcf65fSDavid Teigland 	unsigned long		ls_scan_time;
504e7fd4179SDavid Teigland 	struct kobject		ls_kobj;
505e7fd4179SDavid Teigland 
5063d6aa675SDavid Teigland 	struct idr		ls_lkbidr;
5073d6aa675SDavid Teigland 	spinlock_t		ls_lkbidr_spin;
5083d6aa675SDavid Teigland 
509e7fd4179SDavid Teigland 	struct dlm_rsbtable	*ls_rsbtbl;
510e7fd4179SDavid Teigland 	uint32_t		ls_rsbtbl_size;
511e7fd4179SDavid Teigland 
512e7fd4179SDavid Teigland 	struct dlm_dirtable	*ls_dirtbl;
513e7fd4179SDavid Teigland 	uint32_t		ls_dirtbl_size;
514e7fd4179SDavid Teigland 
51590135925SDavid Teigland 	struct mutex		ls_waiters_mutex;
516e7fd4179SDavid Teigland 	struct list_head	ls_waiters;	/* lkbs needing a reply */
517e7fd4179SDavid Teigland 
518ef0c2bb0SDavid Teigland 	struct mutex		ls_orphans_mutex;
519ef0c2bb0SDavid Teigland 	struct list_head	ls_orphans;
520ef0c2bb0SDavid Teigland 
5213ae1acf9SDavid Teigland 	struct mutex		ls_timeout_mutex;
5223ae1acf9SDavid Teigland 	struct list_head	ls_timeout;
5233ae1acf9SDavid Teigland 
5243881ac04SDavid Teigland 	spinlock_t		ls_new_rsb_spin;
5253881ac04SDavid Teigland 	int			ls_new_rsb_count;
5263881ac04SDavid Teigland 	struct list_head	ls_new_rsb;	/* new rsb structs */
5273881ac04SDavid Teigland 
528e7fd4179SDavid Teigland 	struct list_head	ls_nodes;	/* current nodes in ls */
529e7fd4179SDavid Teigland 	struct list_head	ls_nodes_gone;	/* dead node list, recovery */
530e7fd4179SDavid Teigland 	int			ls_num_nodes;	/* number of nodes in ls */
531e7fd4179SDavid Teigland 	int			ls_low_nodeid;
532e7fd4179SDavid Teigland 	int			ls_total_weight;
533e7fd4179SDavid Teigland 	int			*ls_node_array;
534e7fd4179SDavid Teigland 
535757a4271SDavid Teigland 	int			ls_slot;
536757a4271SDavid Teigland 	int			ls_num_slots;
537757a4271SDavid Teigland 	int			ls_slots_size;
538757a4271SDavid Teigland 	struct dlm_slot		*ls_slots;
539757a4271SDavid Teigland 
540e7fd4179SDavid Teigland 	struct dlm_rsb		ls_stub_rsb;	/* for returning errors */
541e7fd4179SDavid Teigland 	struct dlm_lkb		ls_stub_lkb;	/* for returning errors */
542e7fd4179SDavid Teigland 	struct dlm_message	ls_stub_ms;	/* for faking a reply */
543e7fd4179SDavid Teigland 
5445de6319bSDavid Teigland 	struct dentry		*ls_debug_rsb_dentry; /* debugfs */
5455de6319bSDavid Teigland 	struct dentry		*ls_debug_waiters_dentry; /* debugfs */
546ac90a255SDavid Teigland 	struct dentry		*ls_debug_locks_dentry; /* debugfs */
547d022509dSDavid Teigland 	struct dentry		*ls_debug_all_dentry; /* debugfs */
548e7fd4179SDavid Teigland 
549e7fd4179SDavid Teigland 	wait_queue_head_t	ls_uevent_wait;	/* user part of join/leave */
550e7fd4179SDavid Teigland 	int			ls_uevent_result;
5518b0e7b2cSDavid Teigland 	struct completion	ls_members_done;
5528b0e7b2cSDavid Teigland 	int			ls_members_result;
553e7fd4179SDavid Teigland 
554597d0caeSDavid Teigland 	struct miscdevice       ls_device;
555597d0caeSDavid Teigland 
55623e8e1aaSDavid Teigland 	struct workqueue_struct	*ls_callback_wq;
55723e8e1aaSDavid Teigland 
558e7fd4179SDavid Teigland 	/* recovery related */
559e7fd4179SDavid Teigland 
56023e8e1aaSDavid Teigland 	struct mutex		ls_cb_mutex;
56123e8e1aaSDavid Teigland 	struct list_head	ls_cb_delay; /* save for queue_work later */
562e7fd4179SDavid Teigland 	struct timer_list	ls_timer;
563e7fd4179SDavid Teigland 	struct task_struct	*ls_recoverd_task;
56490135925SDavid Teigland 	struct mutex		ls_recoverd_active;
565e7fd4179SDavid Teigland 	spinlock_t		ls_recover_lock;
5663ae1acf9SDavid Teigland 	unsigned long		ls_recover_begin; /* jiffies timestamp */
567e7fd4179SDavid Teigland 	uint32_t		ls_recover_status; /* DLM_RS_ */
568e7fd4179SDavid Teigland 	uint64_t		ls_recover_seq;
569e7fd4179SDavid Teigland 	struct dlm_recover	*ls_recover_args;
570e7fd4179SDavid Teigland 	struct rw_semaphore	ls_in_recovery;	/* block local requests */
571c36258b5SDavid Teigland 	struct rw_semaphore	ls_recv_active;	/* block dlm_recv */
572e7fd4179SDavid Teigland 	struct list_head	ls_requestqueue;/* queue remote requests */
57390135925SDavid Teigland 	struct mutex		ls_requestqueue_mutex;
5744007685cSAl Viro 	struct dlm_rcom		*ls_recover_buf;
575faa0f267SDavid Teigland 	int			ls_recover_nodeid; /* for debugging */
576*4875647aSDavid Teigland 	unsigned int		ls_recover_locks_in; /* for log info */
5774a99c3d9SDavid Teigland 	uint64_t		ls_rcom_seq;
57898f176fbSDavid Teigland 	spinlock_t		ls_rcom_spin;
579e7fd4179SDavid Teigland 	struct list_head	ls_recover_list;
580e7fd4179SDavid Teigland 	spinlock_t		ls_recover_list_lock;
581e7fd4179SDavid Teigland 	int			ls_recover_list_count;
582e7fd4179SDavid Teigland 	wait_queue_head_t	ls_wait_general;
583597d0caeSDavid Teigland 	struct mutex		ls_clear_proc_locks;
584e7fd4179SDavid Teigland 
585e7fd4179SDavid Teigland 	struct list_head	ls_root_list;	/* root resources */
586e7fd4179SDavid Teigland 	struct rw_semaphore	ls_root_sem;	/* protect root_list */
587e7fd4179SDavid Teigland 
58860f98d18SDavid Teigland 	const struct dlm_lockspace_ops *ls_ops;
58960f98d18SDavid Teigland 	void			*ls_ops_arg;
59060f98d18SDavid Teigland 
591e7fd4179SDavid Teigland 	int			ls_namelen;
592e7fd4179SDavid Teigland 	char			ls_name[1];
593e7fd4179SDavid Teigland };
594e7fd4179SDavid Teigland 
595e7fd4179SDavid Teigland #define LSFL_WORK		0
596e7fd4179SDavid Teigland #define LSFL_RUNNING		1
597e7fd4179SDavid Teigland #define LSFL_RECOVERY_STOP	2
598e7fd4179SDavid Teigland #define LSFL_RCOM_READY		3
59998f176fbSDavid Teigland #define LSFL_RCOM_WAIT		4
60098f176fbSDavid Teigland #define LSFL_UEVENT_WAIT	5
6013ae1acf9SDavid Teigland #define LSFL_TIMEWARN		6
60223e8e1aaSDavid Teigland #define LSFL_CB_DELAY		7
603*4875647aSDavid Teigland #define LSFL_NODIR		8
604e7fd4179SDavid Teigland 
605597d0caeSDavid Teigland /* much of this is just saving user space pointers associated with the
606597d0caeSDavid Teigland    lock that we pass back to the user lib with an ast */
607597d0caeSDavid Teigland 
608597d0caeSDavid Teigland struct dlm_user_args {
609597d0caeSDavid Teigland 	struct dlm_user_proc	*proc; /* each process that opens the lockspace
610597d0caeSDavid Teigland 					  device has private data
611597d0caeSDavid Teigland 					  (dlm_user_proc) on the struct file,
612597d0caeSDavid Teigland 					  the process's locks point back to it*/
613597d0caeSDavid Teigland 	struct dlm_lksb		lksb;
614597d0caeSDavid Teigland 	struct dlm_lksb __user	*user_lksb;
615597d0caeSDavid Teigland 	void __user		*castparam;
616597d0caeSDavid Teigland 	void __user		*castaddr;
617597d0caeSDavid Teigland 	void __user		*bastparam;
618597d0caeSDavid Teigland 	void __user		*bastaddr;
619d7db923eSDavid Teigland 	uint64_t		xid;
620597d0caeSDavid Teigland };
621597d0caeSDavid Teigland 
622597d0caeSDavid Teigland #define DLM_PROC_FLAGS_CLOSING 1
623597d0caeSDavid Teigland #define DLM_PROC_FLAGS_COMPAT  2
624597d0caeSDavid Teigland 
625597d0caeSDavid Teigland /* locks list is kept so we can remove all a process's locks when it
626597d0caeSDavid Teigland    exits (or orphan those that are persistent) */
627597d0caeSDavid Teigland 
628597d0caeSDavid Teigland struct dlm_user_proc {
629597d0caeSDavid Teigland 	dlm_lockspace_t		*lockspace;
630597d0caeSDavid Teigland 	unsigned long		flags; /* DLM_PROC_FLAGS */
631597d0caeSDavid Teigland 	struct list_head	asts;
632597d0caeSDavid Teigland 	spinlock_t		asts_spin;
633597d0caeSDavid Teigland 	struct list_head	locks;
634597d0caeSDavid Teigland 	spinlock_t		locks_spin;
635a1bc86e6SDavid Teigland 	struct list_head	unlocking;
636597d0caeSDavid Teigland 	wait_queue_head_t	wait;
637597d0caeSDavid Teigland };
638597d0caeSDavid Teigland 
639e7fd4179SDavid Teigland static inline int dlm_locking_stopped(struct dlm_ls *ls)
640e7fd4179SDavid Teigland {
641e7fd4179SDavid Teigland 	return !test_bit(LSFL_RUNNING, &ls->ls_flags);
642e7fd4179SDavid Teigland }
643e7fd4179SDavid Teigland 
644e7fd4179SDavid Teigland static inline int dlm_recovery_stopped(struct dlm_ls *ls)
645e7fd4179SDavid Teigland {
646e7fd4179SDavid Teigland 	return test_bit(LSFL_RECOVERY_STOP, &ls->ls_flags);
647e7fd4179SDavid Teigland }
648e7fd4179SDavid Teigland 
649e7fd4179SDavid Teigland static inline int dlm_no_directory(struct dlm_ls *ls)
650e7fd4179SDavid Teigland {
651*4875647aSDavid Teigland 	return test_bit(LSFL_NODIR, &ls->ls_flags);
652e7fd4179SDavid Teigland }
653e7fd4179SDavid Teigland 
654e028398dSAdrian Bunk int dlm_netlink_init(void);
655e028398dSAdrian Bunk void dlm_netlink_exit(void);
656e028398dSAdrian Bunk void dlm_timeout_warn(struct dlm_lkb *lkb);
6572402211aSDavid Teigland int dlm_plock_init(void);
6582402211aSDavid Teigland void dlm_plock_exit(void);
659e028398dSAdrian Bunk 
660e028398dSAdrian Bunk #ifdef CONFIG_DLM_DEBUG
661e028398dSAdrian Bunk int dlm_register_debugfs(void);
662e028398dSAdrian Bunk void dlm_unregister_debugfs(void);
663e028398dSAdrian Bunk int dlm_create_debug_file(struct dlm_ls *ls);
664e028398dSAdrian Bunk void dlm_delete_debug_file(struct dlm_ls *ls);
665e028398dSAdrian Bunk #else
666e028398dSAdrian Bunk static inline int dlm_register_debugfs(void) { return 0; }
667e028398dSAdrian Bunk static inline void dlm_unregister_debugfs(void) { }
668e028398dSAdrian Bunk static inline int dlm_create_debug_file(struct dlm_ls *ls) { return 0; }
669e028398dSAdrian Bunk static inline void dlm_delete_debug_file(struct dlm_ls *ls) { }
670e028398dSAdrian Bunk #endif
671e028398dSAdrian Bunk 
672e7fd4179SDavid Teigland #endif				/* __DLM_INTERNAL_DOT_H__ */
673e7fd4179SDavid Teigland 
674