1/******************************************************************************
2*******************************************************************************
3**
4**  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
5**  Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
6**
7**  This copyrighted material is made available to anyone wishing to use,
8**  modify, copy, or redistribute it subject to the terms and conditions
9**  of the GNU General Public License v.2.
10**
11*******************************************************************************
12******************************************************************************/
13
14#ifndef __DLM_INTERNAL_DOT_H__
15#define __DLM_INTERNAL_DOT_H__
16
17/*
18 * This is the main header file to be included in each DLM source file.
19 */
20
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/sched.h>
24#include <linux/types.h>
25#include <linux/ctype.h>
26#include <linux/spinlock.h>
27#include <linux/vmalloc.h>
28#include <linux/list.h>
29#include <linux/errno.h>
30#include <linux/random.h>
31#include <linux/delay.h>
32#include <linux/socket.h>
33#include <linux/kthread.h>
34#include <linux/kobject.h>
35#include <linux/kref.h>
36#include <linux/kernel.h>
37#include <linux/jhash.h>
38#include <linux/miscdevice.h>
39#include <linux/mutex.h>
40#include <asm/semaphore.h>
41#include <asm/uaccess.h>
42
43#include <linux/dlm.h>
44#include "config.h"
45
46#define DLM_LOCKSPACE_LEN	64
47
48
49#define DLM_INBUF_LEN		148
50
51struct dlm_ls;
52struct dlm_lkb;
53struct dlm_rsb;
54struct dlm_member;
55struct dlm_lkbtable;
56struct dlm_rsbtable;
57struct dlm_dirtable;
58struct dlm_direntry;
59struct dlm_recover;
60struct dlm_header;
61struct dlm_message;
62struct dlm_rcom;
63struct dlm_mhandle;
64
65#define log_print(fmt, args...) \
66	printk(KERN_ERR "dlm: "fmt"\n" , ##args)
67#define log_error(ls, fmt, args...) \
68	printk(KERN_ERR "dlm: %s: " fmt "\n", (ls)->ls_name , ##args)
69
70#define log_debug(ls, fmt, args...) \
71do { \
72	if (dlm_config.ci_log_debug) \
73		printk(KERN_DEBUG "dlm: %s: " fmt "\n", \
74		       (ls)->ls_name , ##args); \
75} while (0)
76
77#define DLM_ASSERT(x, do) \
78{ \
79  if (!(x)) \
80  { \
81    printk(KERN_ERR "\nDLM:  Assertion failed on line %d of file %s\n" \
82               "DLM:  assertion:  \"%s\"\n" \
83               "DLM:  time = %lu\n", \
84               __LINE__, __FILE__, #x, jiffies); \
85    {do} \
86    printk("\n"); \
87    BUG(); \
88    panic("DLM:  Record message above and reboot.\n"); \
89  } \
90}
91
92#define DLM_FAKE_USER_AST ERR_PTR(-EINVAL)
93
94
95struct dlm_direntry {
96	struct list_head	list;
97	uint32_t		master_nodeid;
98	uint16_t		length;
99	char			name[1];
100};
101
102struct dlm_dirtable {
103	struct list_head	list;
104	rwlock_t		lock;
105};
106
107struct dlm_rsbtable {
108	struct list_head	list;
109	struct list_head	toss;
110	rwlock_t		lock;
111};
112
113struct dlm_lkbtable {
114	struct list_head	list;
115	rwlock_t		lock;
116	uint16_t		counter;
117};
118
119/*
120 * Lockspace member (per node in a ls)
121 */
122
123struct dlm_member {
124	struct list_head	list;
125	int			nodeid;
126	int			weight;
127};
128
129/*
130 * Save and manage recovery state for a lockspace.
131 */
132
133struct dlm_recover {
134	struct list_head	list;
135	int			*nodeids;
136	int			node_count;
137	uint64_t		seq;
138};
139
140/*
141 * Pass input args to second stage locking function.
142 */
143
144struct dlm_args {
145	uint32_t		flags;
146	void			*astaddr;
147	long			astparam;
148	void			*bastaddr;
149	int			mode;
150	struct dlm_lksb		*lksb;
151};
152
153
154/*
155 * Lock block
156 *
157 * A lock can be one of three types:
158 *
159 * local copy      lock is mastered locally
160 *                 (lkb_nodeid is zero and DLM_LKF_MSTCPY is not set)
161 * process copy    lock is mastered on a remote node
162 *                 (lkb_nodeid is non-zero and DLM_LKF_MSTCPY is not set)
163 * master copy     master node's copy of a lock owned by remote node
164 *                 (lkb_nodeid is non-zero and DLM_LKF_MSTCPY is set)
165 *
166 * lkb_exflags: a copy of the most recent flags arg provided to dlm_lock or
167 * dlm_unlock.  The dlm does not modify these or use any private flags in
168 * this field; it only contains DLM_LKF_ flags from dlm.h.  These flags
169 * are sent as-is to the remote master when the lock is remote.
170 *
171 * lkb_flags: internal dlm flags (DLM_IFL_ prefix) from dlm_internal.h.
172 * Some internal flags are shared between the master and process nodes;
173 * these shared flags are kept in the lower two bytes.  One of these
174 * flags set on the master copy will be propagated to the process copy
175 * and v.v.  Other internal flags are private to the master or process
176 * node (e.g. DLM_IFL_MSTCPY).  These are kept in the high two bytes.
177 *
178 * lkb_sbflags: status block flags.  These flags are copied directly into
179 * the caller's lksb.sb_flags prior to the dlm_lock/dlm_unlock completion
180 * ast.  All defined in dlm.h with DLM_SBF_ prefix.
181 *
182 * lkb_status: the lock status indicates which rsb queue the lock is
183 * on, grant, convert, or wait.  DLM_LKSTS_ WAITING/GRANTED/CONVERT
184 *
185 * lkb_wait_type: the dlm message type (DLM_MSG_ prefix) for which a
186 * reply is needed.  Only set when the lkb is on the lockspace waiters
187 * list awaiting a reply from a remote node.
188 *
189 * lkb_nodeid: when the lkb is a local copy, nodeid is 0; when the lkb
190 * is a master copy, nodeid specifies the remote lock holder, when the
191 * lkb is a process copy, the nodeid specifies the lock master.
192 */
193
194/* lkb_ast_type */
195
196#define AST_COMP		1
197#define AST_BAST		2
198
199/* lkb_status */
200
201#define DLM_LKSTS_WAITING	1
202#define DLM_LKSTS_GRANTED	2
203#define DLM_LKSTS_CONVERT	3
204
205/* lkb_flags */
206
207#define DLM_IFL_MSTCPY		0x00010000
208#define DLM_IFL_RESEND		0x00020000
209#define DLM_IFL_DEAD		0x00040000
210#define DLM_IFL_OVERLAP_UNLOCK  0x00080000
211#define DLM_IFL_OVERLAP_CANCEL  0x00100000
212#define DLM_IFL_ENDOFLIFE	0x00200000
213#define DLM_IFL_USER		0x00000001
214#define DLM_IFL_ORPHAN		0x00000002
215
216struct dlm_lkb {
217	struct dlm_rsb		*lkb_resource;	/* the rsb */
218	struct kref		lkb_ref;
219	int			lkb_nodeid;	/* copied from rsb */
220	int			lkb_ownpid;	/* pid of lock owner */
221	uint32_t		lkb_id;		/* our lock ID */
222	uint32_t		lkb_remid;	/* lock ID on remote partner */
223	uint32_t		lkb_exflags;	/* external flags from caller */
224	uint32_t		lkb_sbflags;	/* lksb flags */
225	uint32_t		lkb_flags;	/* internal flags */
226	uint32_t		lkb_lvbseq;	/* lvb sequence number */
227
228	int8_t			lkb_status;     /* granted, waiting, convert */
229	int8_t			lkb_rqmode;	/* requested lock mode */
230	int8_t			lkb_grmode;	/* granted lock mode */
231	int8_t			lkb_bastmode;	/* requested mode */
232	int8_t			lkb_highbast;	/* highest mode bast sent for */
233	int8_t			lkb_wait_type;	/* type of reply waiting for */
234	int8_t			lkb_wait_count;
235	int8_t			lkb_ast_type;	/* type of ast queued for */
236
237	struct list_head	lkb_idtbl_list;	/* lockspace lkbtbl */
238	struct list_head	lkb_statequeue;	/* rsb g/c/w list */
239	struct list_head	lkb_rsb_lookup;	/* waiting for rsb lookup */
240	struct list_head	lkb_wait_reply;	/* waiting for remote reply */
241	struct list_head	lkb_astqueue;	/* need ast to be sent */
242	struct list_head	lkb_ownqueue;	/* list of locks for a process */
243
244	char			*lkb_lvbptr;
245	struct dlm_lksb		*lkb_lksb;      /* caller's status block */
246	void			*lkb_astaddr;	/* caller's ast function */
247	void			*lkb_bastaddr;	/* caller's bast function */
248	long			lkb_astparam;	/* caller's ast arg */
249};
250
251
252struct dlm_rsb {
253	struct dlm_ls		*res_ls;	/* the lockspace */
254	struct kref		res_ref;
255	struct mutex		res_mutex;
256	unsigned long		res_flags;
257	int			res_length;	/* length of rsb name */
258	int			res_nodeid;
259	uint32_t                res_lvbseq;
260	uint32_t		res_hash;
261	uint32_t		res_bucket;	/* rsbtbl */
262	unsigned long		res_toss_time;
263	uint32_t		res_first_lkid;
264	struct list_head	res_lookup;	/* lkbs waiting on first */
265	struct list_head	res_hashchain;	/* rsbtbl */
266	struct list_head	res_grantqueue;
267	struct list_head	res_convertqueue;
268	struct list_head	res_waitqueue;
269
270	struct list_head	res_root_list;	    /* used for recovery */
271	struct list_head	res_recover_list;   /* used for recovery */
272	int			res_recover_locks_count;
273
274	char			*res_lvbptr;
275	char			res_name[1];
276};
277
278/* find_rsb() flags */
279
280#define R_MASTER		1	/* only return rsb if it's a master */
281#define R_CREATE		2	/* create/add rsb if not found */
282
283/* rsb_flags */
284
285enum rsb_flags {
286	RSB_MASTER_UNCERTAIN,
287	RSB_VALNOTVALID,
288	RSB_VALNOTVALID_PREV,
289	RSB_NEW_MASTER,
290	RSB_NEW_MASTER2,
291	RSB_RECOVER_CONVERT,
292	RSB_LOCKS_PURGED,
293};
294
295static inline void rsb_set_flag(struct dlm_rsb *r, enum rsb_flags flag)
296{
297	__set_bit(flag, &r->res_flags);
298}
299
300static inline void rsb_clear_flag(struct dlm_rsb *r, enum rsb_flags flag)
301{
302	__clear_bit(flag, &r->res_flags);
303}
304
305static inline int rsb_flag(struct dlm_rsb *r, enum rsb_flags flag)
306{
307	return test_bit(flag, &r->res_flags);
308}
309
310
311/* dlm_header is first element of all structs sent between nodes */
312
313#define DLM_HEADER_MAJOR	0x00030000
314#define DLM_HEADER_MINOR	0x00000000
315
316#define DLM_MSG			1
317#define DLM_RCOM		2
318
319struct dlm_header {
320	uint32_t		h_version;
321	uint32_t		h_lockspace;
322	uint32_t		h_nodeid;	/* nodeid of sender */
323	uint16_t		h_length;
324	uint8_t			h_cmd;		/* DLM_MSG, DLM_RCOM */
325	uint8_t			h_pad;
326};
327
328
329#define DLM_MSG_REQUEST		1
330#define DLM_MSG_CONVERT		2
331#define DLM_MSG_UNLOCK		3
332#define DLM_MSG_CANCEL		4
333#define DLM_MSG_REQUEST_REPLY	5
334#define DLM_MSG_CONVERT_REPLY	6
335#define DLM_MSG_UNLOCK_REPLY	7
336#define DLM_MSG_CANCEL_REPLY	8
337#define DLM_MSG_GRANT		9
338#define DLM_MSG_BAST		10
339#define DLM_MSG_LOOKUP		11
340#define DLM_MSG_REMOVE		12
341#define DLM_MSG_LOOKUP_REPLY	13
342#define DLM_MSG_PURGE		14
343
344struct dlm_message {
345	struct dlm_header	m_header;
346	uint32_t		m_type;		/* DLM_MSG_ */
347	uint32_t		m_nodeid;
348	uint32_t		m_pid;
349	uint32_t		m_lkid;		/* lkid on sender */
350	uint32_t		m_remid;	/* lkid on receiver */
351	uint32_t		m_parent_lkid;
352	uint32_t		m_parent_remid;
353	uint32_t		m_exflags;
354	uint32_t		m_sbflags;
355	uint32_t		m_flags;
356	uint32_t		m_lvbseq;
357	uint32_t		m_hash;
358	int			m_status;
359	int			m_grmode;
360	int			m_rqmode;
361	int			m_bastmode;
362	int			m_asts;
363	int			m_result;	/* 0 or -EXXX */
364	char			m_extra[0];	/* name or lvb */
365};
366
367
368#define DLM_RS_NODES		0x00000001
369#define DLM_RS_NODES_ALL	0x00000002
370#define DLM_RS_DIR		0x00000004
371#define DLM_RS_DIR_ALL		0x00000008
372#define DLM_RS_LOCKS		0x00000010
373#define DLM_RS_LOCKS_ALL	0x00000020
374#define DLM_RS_DONE		0x00000040
375#define DLM_RS_DONE_ALL		0x00000080
376
377#define DLM_RCOM_STATUS		1
378#define DLM_RCOM_NAMES		2
379#define DLM_RCOM_LOOKUP		3
380#define DLM_RCOM_LOCK		4
381#define DLM_RCOM_STATUS_REPLY	5
382#define DLM_RCOM_NAMES_REPLY	6
383#define DLM_RCOM_LOOKUP_REPLY	7
384#define DLM_RCOM_LOCK_REPLY	8
385
386struct dlm_rcom {
387	struct dlm_header	rc_header;
388	uint32_t		rc_type;	/* DLM_RCOM_ */
389	int			rc_result;	/* multi-purpose */
390	uint64_t		rc_id;		/* match reply with request */
391	uint64_t		rc_seq;		/* sender's ls_recover_seq */
392	uint64_t		rc_seq_reply;	/* remote ls_recover_seq */
393	char			rc_buf[0];
394};
395
396struct rcom_config {
397	uint32_t		rf_lvblen;
398	uint32_t		rf_lsflags;
399	uint64_t		rf_unused;
400};
401
402struct rcom_lock {
403	uint32_t		rl_ownpid;
404	uint32_t		rl_lkid;
405	uint32_t		rl_remid;
406	uint32_t		rl_parent_lkid;
407	uint32_t		rl_parent_remid;
408	uint32_t		rl_exflags;
409	uint32_t		rl_flags;
410	uint32_t		rl_lvbseq;
411	int			rl_result;
412	int8_t			rl_rqmode;
413	int8_t			rl_grmode;
414	int8_t			rl_status;
415	int8_t			rl_asts;
416	uint16_t		rl_wait_type;
417	uint16_t		rl_namelen;
418	char			rl_name[DLM_RESNAME_MAXLEN];
419	char			rl_lvb[0];
420};
421
422struct dlm_ls {
423	struct list_head	ls_list;	/* list of lockspaces */
424	dlm_lockspace_t		*ls_local_handle;
425	uint32_t		ls_global_id;	/* global unique lockspace ID */
426	uint32_t		ls_exflags;
427	int			ls_lvblen;
428	int			ls_count;	/* reference count */
429	unsigned long		ls_flags;	/* LSFL_ */
430	struct kobject		ls_kobj;
431
432	struct dlm_rsbtable	*ls_rsbtbl;
433	uint32_t		ls_rsbtbl_size;
434
435	struct dlm_lkbtable	*ls_lkbtbl;
436	uint32_t		ls_lkbtbl_size;
437
438	struct dlm_dirtable	*ls_dirtbl;
439	uint32_t		ls_dirtbl_size;
440
441	struct mutex		ls_waiters_mutex;
442	struct list_head	ls_waiters;	/* lkbs needing a reply */
443
444	struct mutex		ls_orphans_mutex;
445	struct list_head	ls_orphans;
446
447	struct list_head	ls_nodes;	/* current nodes in ls */
448	struct list_head	ls_nodes_gone;	/* dead node list, recovery */
449	int			ls_num_nodes;	/* number of nodes in ls */
450	int			ls_low_nodeid;
451	int			ls_total_weight;
452	int			*ls_node_array;
453
454	struct dlm_rsb		ls_stub_rsb;	/* for returning errors */
455	struct dlm_lkb		ls_stub_lkb;	/* for returning errors */
456	struct dlm_message	ls_stub_ms;	/* for faking a reply */
457
458	struct dentry		*ls_debug_rsb_dentry; /* debugfs */
459	struct dentry		*ls_debug_waiters_dentry; /* debugfs */
460
461	wait_queue_head_t	ls_uevent_wait;	/* user part of join/leave */
462	int			ls_uevent_result;
463
464	struct miscdevice       ls_device;
465
466	/* recovery related */
467
468	struct timer_list	ls_timer;
469	struct task_struct	*ls_recoverd_task;
470	struct mutex		ls_recoverd_active;
471	spinlock_t		ls_recover_lock;
472	uint32_t		ls_recover_status; /* DLM_RS_ */
473	uint64_t		ls_recover_seq;
474	struct dlm_recover	*ls_recover_args;
475	struct rw_semaphore	ls_in_recovery;	/* block local requests */
476	struct list_head	ls_requestqueue;/* queue remote requests */
477	struct mutex		ls_requestqueue_mutex;
478	char			*ls_recover_buf;
479	int			ls_recover_nodeid; /* for debugging */
480	uint64_t		ls_rcom_seq;
481	spinlock_t		ls_rcom_spin;
482	struct list_head	ls_recover_list;
483	spinlock_t		ls_recover_list_lock;
484	int			ls_recover_list_count;
485	wait_queue_head_t	ls_wait_general;
486	struct mutex		ls_clear_proc_locks;
487
488	struct list_head	ls_root_list;	/* root resources */
489	struct rw_semaphore	ls_root_sem;	/* protect root_list */
490
491	int			ls_namelen;
492	char			ls_name[1];
493};
494
495#define LSFL_WORK		0
496#define LSFL_RUNNING		1
497#define LSFL_RECOVERY_STOP	2
498#define LSFL_RCOM_READY		3
499#define LSFL_RCOM_WAIT		4
500#define LSFL_UEVENT_WAIT	5
501
502/* much of this is just saving user space pointers associated with the
503   lock that we pass back to the user lib with an ast */
504
505struct dlm_user_args {
506	struct dlm_user_proc	*proc; /* each process that opens the lockspace
507					  device has private data
508					  (dlm_user_proc) on the struct file,
509					  the process's locks point back to it*/
510	struct dlm_lksb		lksb;
511	int			old_mode;
512	int			update_user_lvb;
513	struct dlm_lksb __user	*user_lksb;
514	void __user		*castparam;
515	void __user		*castaddr;
516	void __user		*bastparam;
517	void __user		*bastaddr;
518};
519
520#define DLM_PROC_FLAGS_CLOSING 1
521#define DLM_PROC_FLAGS_COMPAT  2
522
523/* locks list is kept so we can remove all a process's locks when it
524   exits (or orphan those that are persistent) */
525
526struct dlm_user_proc {
527	dlm_lockspace_t		*lockspace;
528	unsigned long		flags; /* DLM_PROC_FLAGS */
529	struct list_head	asts;
530	spinlock_t		asts_spin;
531	struct list_head	locks;
532	spinlock_t		locks_spin;
533	struct list_head	unlocking;
534	wait_queue_head_t	wait;
535};
536
537static inline int dlm_locking_stopped(struct dlm_ls *ls)
538{
539	return !test_bit(LSFL_RUNNING, &ls->ls_flags);
540}
541
542static inline int dlm_recovery_stopped(struct dlm_ls *ls)
543{
544	return test_bit(LSFL_RECOVERY_STOP, &ls->ls_flags);
545}
546
547static inline int dlm_no_directory(struct dlm_ls *ls)
548{
549	return (ls->ls_exflags & DLM_LSFL_NODIR) ? 1 : 0;
550}
551
552#endif				/* __DLM_INTERNAL_DOT_H__ */
553