xref: /OK3568_Linux_fs/kernel/fs/ocfs2/stackglue.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /* -*- mode: c; c-basic-offset: 8; -*-
3*4882a593Smuzhiyun  * vim: noexpandtab sw=8 ts=8 sts=0:
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * stackglue.h
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Glue to the underlying cluster stack.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (C) 2007 Oracle.  All rights reserved.
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #ifndef STACKGLUE_H
14*4882a593Smuzhiyun #define STACKGLUE_H
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <linux/types.h>
17*4882a593Smuzhiyun #include <linux/list.h>
18*4882a593Smuzhiyun #include <linux/dlmconstants.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include "dlm/dlmapi.h"
21*4882a593Smuzhiyun #include <linux/dlm.h>
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun /* Needed for plock-related prototypes */
24*4882a593Smuzhiyun struct file;
25*4882a593Smuzhiyun struct file_lock;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun  * dlmconstants.h does not have a LOCAL flag.  We hope to remove it
29*4882a593Smuzhiyun  * some day, but right now we need it.  Let's fake it.  This value is larger
30*4882a593Smuzhiyun  * than any flag in dlmconstants.h.
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun #define DLM_LKF_LOCAL		0x00100000
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun  * This shadows DLM_LOCKSPACE_LEN in fs/dlm/dlm_internal.h.  That probably
36*4882a593Smuzhiyun  * wants to be in a public header.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun #define GROUP_NAME_MAX		64
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /* This shadows  OCFS2_CLUSTER_NAME_LEN */
41*4882a593Smuzhiyun #define CLUSTER_NAME_MAX	16
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun  * ocfs2_protocol_version changes when ocfs2 does something different in
46*4882a593Smuzhiyun  * its inter-node behavior.  See dlmglue.c for more information.
47*4882a593Smuzhiyun  */
48*4882a593Smuzhiyun struct ocfs2_protocol_version {
49*4882a593Smuzhiyun 	u8 pv_major;
50*4882a593Smuzhiyun 	u8 pv_minor;
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun  * The dlm_lockstatus struct includes lvb space, but the dlm_lksb struct only
55*4882a593Smuzhiyun  * has a pointer to separately allocated lvb space.  This struct exists only to
56*4882a593Smuzhiyun  * include in the lksb union to make space for a combined dlm_lksb and lvb.
57*4882a593Smuzhiyun  */
58*4882a593Smuzhiyun struct fsdlm_lksb_plus_lvb {
59*4882a593Smuzhiyun 	struct dlm_lksb lksb;
60*4882a593Smuzhiyun 	char lvb[DLM_LVB_LEN];
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun /*
64*4882a593Smuzhiyun  * A union of all lock status structures.  We define it here so that the
65*4882a593Smuzhiyun  * size of the union is known.  Lock status structures are embedded in
66*4882a593Smuzhiyun  * ocfs2 inodes.
67*4882a593Smuzhiyun  */
68*4882a593Smuzhiyun struct ocfs2_cluster_connection;
69*4882a593Smuzhiyun struct ocfs2_dlm_lksb {
70*4882a593Smuzhiyun 	 union {
71*4882a593Smuzhiyun 		 struct dlm_lockstatus lksb_o2dlm;
72*4882a593Smuzhiyun 		 struct dlm_lksb lksb_fsdlm;
73*4882a593Smuzhiyun 		 struct fsdlm_lksb_plus_lvb padding;
74*4882a593Smuzhiyun 	 };
75*4882a593Smuzhiyun 	 struct ocfs2_cluster_connection *lksb_conn;
76*4882a593Smuzhiyun };
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun  * The ocfs2_locking_protocol defines the handlers called on ocfs2's behalf.
80*4882a593Smuzhiyun  */
81*4882a593Smuzhiyun struct ocfs2_locking_protocol {
82*4882a593Smuzhiyun 	struct ocfs2_protocol_version lp_max_version;
83*4882a593Smuzhiyun 	void (*lp_lock_ast)(struct ocfs2_dlm_lksb *lksb);
84*4882a593Smuzhiyun 	void (*lp_blocking_ast)(struct ocfs2_dlm_lksb *lksb, int level);
85*4882a593Smuzhiyun 	void (*lp_unlock_ast)(struct ocfs2_dlm_lksb *lksb, int error);
86*4882a593Smuzhiyun };
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /*
90*4882a593Smuzhiyun  * A cluster connection.  Mostly opaque to ocfs2, the connection holds
91*4882a593Smuzhiyun  * state for the underlying stack.  ocfs2 does use cc_version to determine
92*4882a593Smuzhiyun  * locking compatibility.
93*4882a593Smuzhiyun  */
94*4882a593Smuzhiyun struct ocfs2_cluster_connection {
95*4882a593Smuzhiyun 	char cc_name[GROUP_NAME_MAX + 1];
96*4882a593Smuzhiyun 	int cc_namelen;
97*4882a593Smuzhiyun 	char cc_cluster_name[CLUSTER_NAME_MAX + 1];
98*4882a593Smuzhiyun 	int cc_cluster_name_len;
99*4882a593Smuzhiyun 	struct ocfs2_protocol_version cc_version;
100*4882a593Smuzhiyun 	struct ocfs2_locking_protocol *cc_proto;
101*4882a593Smuzhiyun 	void (*cc_recovery_handler)(int node_num, void *recovery_data);
102*4882a593Smuzhiyun 	void *cc_recovery_data;
103*4882a593Smuzhiyun 	void *cc_lockspace;
104*4882a593Smuzhiyun 	void *cc_private;
105*4882a593Smuzhiyun };
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun  * Each cluster stack implements the stack operations structure.  Not used
109*4882a593Smuzhiyun  * in the ocfs2 code, the stackglue code translates generic cluster calls
110*4882a593Smuzhiyun  * into stack operations.
111*4882a593Smuzhiyun  */
112*4882a593Smuzhiyun struct ocfs2_stack_operations {
113*4882a593Smuzhiyun 	/*
114*4882a593Smuzhiyun 	 * The fs code calls ocfs2_cluster_connect() to attach a new
115*4882a593Smuzhiyun 	 * filesystem to the cluster stack.  The ->connect() op is passed
116*4882a593Smuzhiyun 	 * an ocfs2_cluster_connection with the name and recovery field
117*4882a593Smuzhiyun 	 * filled in.
118*4882a593Smuzhiyun 	 *
119*4882a593Smuzhiyun 	 * The stack must set up any notification mechanisms and create
120*4882a593Smuzhiyun 	 * the filesystem lockspace in the DLM.  The lockspace should be
121*4882a593Smuzhiyun 	 * stored on cc_lockspace.  Any other information can be stored on
122*4882a593Smuzhiyun 	 * cc_private.
123*4882a593Smuzhiyun 	 *
124*4882a593Smuzhiyun 	 * ->connect() must not return until it is guaranteed that
125*4882a593Smuzhiyun 	 *
126*4882a593Smuzhiyun 	 *  - Node down notifications for the filesystem will be received
127*4882a593Smuzhiyun 	 *    and passed to conn->cc_recovery_handler().
128*4882a593Smuzhiyun 	 *  - Locking requests for the filesystem will be processed.
129*4882a593Smuzhiyun 	 */
130*4882a593Smuzhiyun 	int (*connect)(struct ocfs2_cluster_connection *conn);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/*
133*4882a593Smuzhiyun 	 * The fs code calls ocfs2_cluster_disconnect() when a filesystem
134*4882a593Smuzhiyun 	 * no longer needs cluster services.  All DLM locks have been
135*4882a593Smuzhiyun 	 * dropped, and recovery notification is being ignored by the
136*4882a593Smuzhiyun 	 * fs code.  The stack must disengage from the DLM and discontinue
137*4882a593Smuzhiyun 	 * recovery notification.
138*4882a593Smuzhiyun 	 *
139*4882a593Smuzhiyun 	 * Once ->disconnect() has returned, the connection structure will
140*4882a593Smuzhiyun 	 * be freed.  Thus, a stack must not return from ->disconnect()
141*4882a593Smuzhiyun 	 * until it will no longer reference the conn pointer.
142*4882a593Smuzhiyun 	 *
143*4882a593Smuzhiyun 	 * Once this call returns, the stack glue will be dropping this
144*4882a593Smuzhiyun 	 * connection's reference on the module.
145*4882a593Smuzhiyun 	 */
146*4882a593Smuzhiyun 	int (*disconnect)(struct ocfs2_cluster_connection *conn);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	/*
149*4882a593Smuzhiyun 	 * ->this_node() returns the cluster's unique identifier for the
150*4882a593Smuzhiyun 	 * local node.
151*4882a593Smuzhiyun 	 */
152*4882a593Smuzhiyun 	int (*this_node)(struct ocfs2_cluster_connection *conn,
153*4882a593Smuzhiyun 			 unsigned int *node);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	/*
156*4882a593Smuzhiyun 	 * Call the underlying dlm lock function.  The ->dlm_lock()
157*4882a593Smuzhiyun 	 * callback should convert the flags and mode as appropriate.
158*4882a593Smuzhiyun 	 *
159*4882a593Smuzhiyun 	 * ast and bast functions are not part of the call because the
160*4882a593Smuzhiyun 	 * stack will likely want to wrap ast and bast calls before passing
161*4882a593Smuzhiyun 	 * them to stack->sp_proto.  There is no astarg.  The lksb will
162*4882a593Smuzhiyun 	 * be passed back to the ast and bast functions.  The caller can
163*4882a593Smuzhiyun 	 * use this to find their object.
164*4882a593Smuzhiyun 	 */
165*4882a593Smuzhiyun 	int (*dlm_lock)(struct ocfs2_cluster_connection *conn,
166*4882a593Smuzhiyun 			int mode,
167*4882a593Smuzhiyun 			struct ocfs2_dlm_lksb *lksb,
168*4882a593Smuzhiyun 			u32 flags,
169*4882a593Smuzhiyun 			void *name,
170*4882a593Smuzhiyun 			unsigned int namelen);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	/*
173*4882a593Smuzhiyun 	 * Call the underlying dlm unlock function.  The ->dlm_unlock()
174*4882a593Smuzhiyun 	 * function should convert the flags as appropriate.
175*4882a593Smuzhiyun 	 *
176*4882a593Smuzhiyun 	 * The unlock ast is not passed, as the stack will want to wrap
177*4882a593Smuzhiyun 	 * it before calling stack->sp_proto->lp_unlock_ast().  There is
178*4882a593Smuzhiyun 	 * no astarg.  The lksb will be passed back to the unlock ast
179*4882a593Smuzhiyun 	 * function.  The caller can use this to find their object.
180*4882a593Smuzhiyun 	 */
181*4882a593Smuzhiyun 	int (*dlm_unlock)(struct ocfs2_cluster_connection *conn,
182*4882a593Smuzhiyun 			  struct ocfs2_dlm_lksb *lksb,
183*4882a593Smuzhiyun 			  u32 flags);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	/*
186*4882a593Smuzhiyun 	 * Return the status of the current lock status block.  The fs
187*4882a593Smuzhiyun 	 * code should never dereference the union.  The ->lock_status()
188*4882a593Smuzhiyun 	 * callback pulls out the stack-specific lksb, converts the status
189*4882a593Smuzhiyun 	 * to a proper errno, and returns it.
190*4882a593Smuzhiyun 	 */
191*4882a593Smuzhiyun 	int (*lock_status)(struct ocfs2_dlm_lksb *lksb);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	/*
194*4882a593Smuzhiyun 	 * Return non-zero if the LVB is valid.
195*4882a593Smuzhiyun 	 */
196*4882a593Smuzhiyun 	int (*lvb_valid)(struct ocfs2_dlm_lksb *lksb);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	/*
199*4882a593Smuzhiyun 	 * Pull the lvb pointer off of the stack-specific lksb.
200*4882a593Smuzhiyun 	 */
201*4882a593Smuzhiyun 	void *(*lock_lvb)(struct ocfs2_dlm_lksb *lksb);
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	/*
204*4882a593Smuzhiyun 	 * Cluster-aware posix locks
205*4882a593Smuzhiyun 	 *
206*4882a593Smuzhiyun 	 * This is NULL for stacks which do not support posix locks.
207*4882a593Smuzhiyun 	 */
208*4882a593Smuzhiyun 	int (*plock)(struct ocfs2_cluster_connection *conn,
209*4882a593Smuzhiyun 		     u64 ino,
210*4882a593Smuzhiyun 		     struct file *file,
211*4882a593Smuzhiyun 		     int cmd,
212*4882a593Smuzhiyun 		     struct file_lock *fl);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	/*
215*4882a593Smuzhiyun 	 * This is an optoinal debugging hook.  If provided, the
216*4882a593Smuzhiyun 	 * stack can dump debugging information about this lock.
217*4882a593Smuzhiyun 	 */
218*4882a593Smuzhiyun 	void (*dump_lksb)(struct ocfs2_dlm_lksb *lksb);
219*4882a593Smuzhiyun };
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun  * Each stack plugin must describe itself by registering a
223*4882a593Smuzhiyun  * ocfs2_stack_plugin structure.  This is only seen by stackglue and the
224*4882a593Smuzhiyun  * stack driver.
225*4882a593Smuzhiyun  */
226*4882a593Smuzhiyun struct ocfs2_stack_plugin {
227*4882a593Smuzhiyun 	char *sp_name;
228*4882a593Smuzhiyun 	struct ocfs2_stack_operations *sp_ops;
229*4882a593Smuzhiyun 	struct module *sp_owner;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	/* These are managed by the stackglue code. */
232*4882a593Smuzhiyun 	struct list_head sp_list;
233*4882a593Smuzhiyun 	unsigned int sp_count;
234*4882a593Smuzhiyun 	struct ocfs2_protocol_version sp_max_proto;
235*4882a593Smuzhiyun };
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun /* Used by the filesystem */
239*4882a593Smuzhiyun int ocfs2_cluster_connect(const char *stack_name,
240*4882a593Smuzhiyun 			  const char *cluster_name,
241*4882a593Smuzhiyun 			  int cluster_name_len,
242*4882a593Smuzhiyun 			  const char *group,
243*4882a593Smuzhiyun 			  int grouplen,
244*4882a593Smuzhiyun 			  struct ocfs2_locking_protocol *lproto,
245*4882a593Smuzhiyun 			  void (*recovery_handler)(int node_num,
246*4882a593Smuzhiyun 						   void *recovery_data),
247*4882a593Smuzhiyun 			  void *recovery_data,
248*4882a593Smuzhiyun 			  struct ocfs2_cluster_connection **conn);
249*4882a593Smuzhiyun /*
250*4882a593Smuzhiyun  * Used by callers that don't store their stack name.  They must ensure
251*4882a593Smuzhiyun  * all nodes have the same stack.
252*4882a593Smuzhiyun  */
253*4882a593Smuzhiyun int ocfs2_cluster_connect_agnostic(const char *group,
254*4882a593Smuzhiyun 				   int grouplen,
255*4882a593Smuzhiyun 				   struct ocfs2_locking_protocol *lproto,
256*4882a593Smuzhiyun 				   void (*recovery_handler)(int node_num,
257*4882a593Smuzhiyun 							    void *recovery_data),
258*4882a593Smuzhiyun 				   void *recovery_data,
259*4882a593Smuzhiyun 				   struct ocfs2_cluster_connection **conn);
260*4882a593Smuzhiyun int ocfs2_cluster_disconnect(struct ocfs2_cluster_connection *conn,
261*4882a593Smuzhiyun 			     int hangup_pending);
262*4882a593Smuzhiyun void ocfs2_cluster_hangup(const char *group, int grouplen);
263*4882a593Smuzhiyun int ocfs2_cluster_this_node(struct ocfs2_cluster_connection *conn,
264*4882a593Smuzhiyun 			    unsigned int *node);
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun struct ocfs2_lock_res;
267*4882a593Smuzhiyun int ocfs2_dlm_lock(struct ocfs2_cluster_connection *conn,
268*4882a593Smuzhiyun 		   int mode,
269*4882a593Smuzhiyun 		   struct ocfs2_dlm_lksb *lksb,
270*4882a593Smuzhiyun 		   u32 flags,
271*4882a593Smuzhiyun 		   void *name,
272*4882a593Smuzhiyun 		   unsigned int namelen);
273*4882a593Smuzhiyun int ocfs2_dlm_unlock(struct ocfs2_cluster_connection *conn,
274*4882a593Smuzhiyun 		     struct ocfs2_dlm_lksb *lksb,
275*4882a593Smuzhiyun 		     u32 flags);
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun int ocfs2_dlm_lock_status(struct ocfs2_dlm_lksb *lksb);
278*4882a593Smuzhiyun int ocfs2_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb);
279*4882a593Smuzhiyun void *ocfs2_dlm_lvb(struct ocfs2_dlm_lksb *lksb);
280*4882a593Smuzhiyun void ocfs2_dlm_dump_lksb(struct ocfs2_dlm_lksb *lksb);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun int ocfs2_stack_supports_plocks(void);
283*4882a593Smuzhiyun int ocfs2_plock(struct ocfs2_cluster_connection *conn, u64 ino,
284*4882a593Smuzhiyun 		struct file *file, int cmd, struct file_lock *fl);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun void ocfs2_stack_glue_set_max_proto_version(struct ocfs2_protocol_version *max_proto);
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun /* Used by stack plugins */
290*4882a593Smuzhiyun int ocfs2_stack_glue_register(struct ocfs2_stack_plugin *plugin);
291*4882a593Smuzhiyun void ocfs2_stack_glue_unregister(struct ocfs2_stack_plugin *plugin);
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun extern struct kset *ocfs2_kset;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun #endif  /* STACKGLUE_H */
296