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