aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoel Becker <joel.becker@oracle.com>2008-02-20 17:44:34 -0500
committerMark Fasheh <mfasheh@suse.com>2008-04-18 11:56:06 -0400
commit3cfd4ab6b6b4bee2035b62e1c293801c3d257502 (patch)
treec1bc209a90a73be354f912dc385ddaa5b2b53752
parentde870ef02295c9f5601dbf2efdc1be6df44b187b (diff)
ocfs2: Add the local node id to the handshake.
This is the second part of the ocfs2_control handshake. After negotiating the ocfs2_control protocol, the daemon tells the filesystem what the local node id is via the SETN message. Signed-off-by: Joel Becker <joel.becker@oracle.com> Signed-off-by: Mark Fasheh <mfasheh@suse.com>
-rw-r--r--fs/ocfs2/stack_user.c222
1 files changed, 173 insertions, 49 deletions
diff --git a/fs/ocfs2/stack_user.c b/fs/ocfs2/stack_user.c
index a5e58e2b9d59..43e6105369c6 100644
--- a/fs/ocfs2/stack_user.c
+++ b/fs/ocfs2/stack_user.c
@@ -40,8 +40,18 @@
40 * unknown, -EINVAL is returned. Once the negotiation is complete, the 40 * unknown, -EINVAL is returned. Once the negotiation is complete, the
41 * client can start sending messages. 41 * client can start sending messages.
42 * 42 *
43 * The T01 protocol only has one message, "DOWN". It has the following 43 * The T01 protocol only has two messages. First is the "SETN" message.
44 * syntax: 44 * It has the following syntax:
45 *
46 * SETN<space><8-char-hex-nodenum><newline>
47 *
48 * This is 14 characters.
49 *
50 * The "SETN" message must be the first message following the protocol.
51 * It tells ocfs2_control the local node number.
52 *
53 * Once the local node number has been set, the "DOWN" message can be
54 * sent for node down notification. It has the following syntax:
45 * 55 *
46 * DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline> 56 * DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline>
47 * 57 *
@@ -58,11 +68,18 @@
58 */ 68 */
59#define OCFS2_CONTROL_PROTO "T01\n" 69#define OCFS2_CONTROL_PROTO "T01\n"
60#define OCFS2_CONTROL_PROTO_LEN 4 70#define OCFS2_CONTROL_PROTO_LEN 4
71
72/* Handshake states */
61#define OCFS2_CONTROL_HANDSHAKE_INVALID (0) 73#define OCFS2_CONTROL_HANDSHAKE_INVALID (0)
62#define OCFS2_CONTROL_HANDSHAKE_READ (1) 74#define OCFS2_CONTROL_HANDSHAKE_READ (1)
63#define OCFS2_CONTROL_HANDSHAKE_VALID (2) 75#define OCFS2_CONTROL_HANDSHAKE_PROTOCOL (2)
64#define OCFS2_CONTROL_MESSAGE_DOWN "DOWN" 76#define OCFS2_CONTROL_HANDSHAKE_VALID (3)
65#define OCFS2_CONTROL_MESSAGE_DOWN_LEN 4 77
78/* Messages */
79#define OCFS2_CONTROL_MESSAGE_OP_LEN 4
80#define OCFS2_CONTROL_MESSAGE_SETNODE_OP "SETN"
81#define OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN 14
82#define OCFS2_CONTROL_MESSAGE_DOWN_OP "DOWN"
66#define OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN 47 83#define OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN 47
67#define OCFS2_TEXT_UUID_LEN 32 84#define OCFS2_TEXT_UUID_LEN 32
68#define OCFS2_CONTROL_MESSAGE_NODENUM_LEN 8 85#define OCFS2_CONTROL_MESSAGE_NODENUM_LEN 8
@@ -79,9 +96,35 @@ struct ocfs2_live_connection {
79struct ocfs2_control_private { 96struct ocfs2_control_private {
80 struct list_head op_list; 97 struct list_head op_list;
81 int op_state; 98 int op_state;
99 int op_this_node;
100};
101
102/* SETN<space><8-char-hex-nodenum><newline> */
103struct ocfs2_control_message_setn {
104 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
105 char space;
106 char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
107 char newline;
108};
109
110/* DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline> */
111struct ocfs2_control_message_down {
112 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
113 char space1;
114 char uuid[OCFS2_TEXT_UUID_LEN];
115 char space2;
116 char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
117 char newline;
118};
119
120union ocfs2_control_message {
121 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
122 struct ocfs2_control_message_setn u_setn;
123 struct ocfs2_control_message_down u_down;
82}; 124};
83 125
84static atomic_t ocfs2_control_opened; 126static atomic_t ocfs2_control_opened;
127static int ocfs2_control_this_node = -1;
85 128
86static LIST_HEAD(ocfs2_live_connection_list); 129static LIST_HEAD(ocfs2_live_connection_list);
87static LIST_HEAD(ocfs2_control_private_list); 130static LIST_HEAD(ocfs2_control_private_list);
@@ -166,38 +209,37 @@ static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
166 kfree(c); 209 kfree(c);
167} 210}
168 211
169static ssize_t ocfs2_control_cfu(void *target, size_t target_len, 212static int ocfs2_control_cfu(void *target, size_t target_len,
170 const char __user *buf, size_t count) 213 const char __user *buf, size_t count)
171{ 214{
172 /* The T01 expects write(2) calls to have exactly one command */ 215 /* The T01 expects write(2) calls to have exactly one command */
173 if (count != target_len) 216 if ((count != target_len) ||
217 (count > sizeof(union ocfs2_control_message)))
174 return -EINVAL; 218 return -EINVAL;
175 219
176 if (copy_from_user(target, buf, target_len)) 220 if (copy_from_user(target, buf, target_len))
177 return -EFAULT; 221 return -EFAULT;
178 222
179 return count; 223 return 0;
180} 224}
181 225
182static ssize_t ocfs2_control_validate_handshake(struct file *file, 226static ssize_t ocfs2_control_validate_protocol(struct file *file,
183 const char __user *buf, 227 const char __user *buf,
184 size_t count) 228 size_t count)
185{ 229{
186 ssize_t ret; 230 ssize_t ret;
187 char kbuf[OCFS2_CONTROL_PROTO_LEN]; 231 char kbuf[OCFS2_CONTROL_PROTO_LEN];
188 232
189 ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN, 233 ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
190 buf, count); 234 buf, count);
191 if (ret != count) 235 if (ret)
192 return ret; 236 return ret;
193 237
194 if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN)) 238 if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
195 return -EINVAL; 239 return -EINVAL;
196 240
197 atomic_inc(&ocfs2_control_opened);
198 ocfs2_control_set_handshake_state(file, 241 ocfs2_control_set_handshake_state(file,
199 OCFS2_CONTROL_HANDSHAKE_VALID); 242 OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
200
201 243
202 return count; 244 return count;
203} 245}
@@ -219,45 +261,92 @@ static void ocfs2_control_send_down(const char *uuid,
219 mutex_unlock(&ocfs2_control_lock); 261 mutex_unlock(&ocfs2_control_lock);
220} 262}
221 263
222/* DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline> */ 264/*
223struct ocfs2_control_message_down { 265 * Called whenever configuration elements are sent to /dev/ocfs2_control.
224 char tag[OCFS2_CONTROL_MESSAGE_DOWN_LEN]; 266 * If all configuration elements are present, try to set the global
225 char space1; 267 * values. If not, return -EAGAIN. If there is a problem, return a
226 char uuid[OCFS2_TEXT_UUID_LEN]; 268 * different error.
227 char space2; 269 */
228 char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN]; 270static int ocfs2_control_install_private(struct file *file)
229 char newline; 271{
230}; 272 int rc = 0;
273 int set_p = 1;
274 struct ocfs2_control_private *p = file->private_data;
231 275
232static ssize_t ocfs2_control_message(struct file *file, 276 BUG_ON(p->op_state != OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
233 const char __user *buf, 277
234 size_t count) 278 if (p->op_this_node < 0)
279 set_p = 0;
280
281 mutex_lock(&ocfs2_control_lock);
282 if (ocfs2_control_this_node < 0) {
283 if (set_p)
284 ocfs2_control_this_node = p->op_this_node;
285 } else if (ocfs2_control_this_node != p->op_this_node)
286 rc = -EINVAL;
287 mutex_unlock(&ocfs2_control_lock);
288
289 if (!rc && set_p) {
290 /* We set the global values successfully */
291 atomic_inc(&ocfs2_control_opened);
292 ocfs2_control_set_handshake_state(file,
293 OCFS2_CONTROL_HANDSHAKE_VALID);
294 }
295
296 return rc;
297}
298
299static int ocfs2_control_do_setnode_msg(struct file *file,
300 struct ocfs2_control_message_setn *msg)
235{ 301{
236 ssize_t ret;
237 char *p = NULL;
238 long nodenum; 302 long nodenum;
239 struct ocfs2_control_message_down msg; 303 char *ptr = NULL;
304 struct ocfs2_control_private *p = file->private_data;
240 305
241 /* Try to catch padding issues */ 306 if (ocfs2_control_get_handshake_state(file) !=
242 WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) != 307 OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
243 (sizeof(msg.tag) + sizeof(msg.space1))); 308 return -EINVAL;
244 309
245 memset(&msg, 0, sizeof(struct ocfs2_control_message_down)); 310 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
246 ret = ocfs2_control_cfu(&msg, OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN, 311 OCFS2_CONTROL_MESSAGE_OP_LEN))
247 buf, count); 312 return -EINVAL;
248 if (ret != count) 313
249 return ret; 314 if ((msg->space != ' ') || (msg->newline != '\n'))
315 return -EINVAL;
316 msg->space = msg->newline = '\0';
317
318 nodenum = simple_strtol(msg->nodestr, &ptr, 16);
319 if (!ptr || *ptr)
320 return -EINVAL;
321
322 if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
323 (nodenum > INT_MAX) || (nodenum < 0))
324 return -ERANGE;
325 p->op_this_node = nodenum;
326
327 return ocfs2_control_install_private(file);
328}
329
330static int ocfs2_control_do_down_msg(struct file *file,
331 struct ocfs2_control_message_down *msg)
332{
333 long nodenum;
334 char *p = NULL;
335
336 if (ocfs2_control_get_handshake_state(file) !=
337 OCFS2_CONTROL_HANDSHAKE_VALID)
338 return -EINVAL;
250 339
251 if (strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN, 340 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
252 strlen(OCFS2_CONTROL_MESSAGE_DOWN))) 341 OCFS2_CONTROL_MESSAGE_OP_LEN))
253 return -EINVAL; 342 return -EINVAL;
254 343
255 if ((msg.space1 != ' ') || (msg.space2 != ' ') || 344 if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
256 (msg.newline != '\n')) 345 (msg->newline != '\n'))
257 return -EINVAL; 346 return -EINVAL;
258 msg.space1 = msg.space2 = msg.newline = '\0'; 347 msg->space1 = msg->space2 = msg->newline = '\0';
259 348
260 nodenum = simple_strtol(msg.nodestr, &p, 16); 349 nodenum = simple_strtol(msg->nodestr, &p, 16);
261 if (!p || *p) 350 if (!p || *p)
262 return -EINVAL; 351 return -EINVAL;
263 352
@@ -265,9 +354,40 @@ static ssize_t ocfs2_control_message(struct file *file,
265 (nodenum > INT_MAX) || (nodenum < 0)) 354 (nodenum > INT_MAX) || (nodenum < 0))
266 return -ERANGE; 355 return -ERANGE;
267 356
268 ocfs2_control_send_down(msg.uuid, nodenum); 357 ocfs2_control_send_down(msg->uuid, nodenum);
269 358
270 return count; 359 return 0;
360}
361
362static ssize_t ocfs2_control_message(struct file *file,
363 const char __user *buf,
364 size_t count)
365{
366 ssize_t ret;
367 union ocfs2_control_message msg;
368
369 /* Try to catch padding issues */
370 WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) !=
371 (sizeof(msg.u_down.tag) + sizeof(msg.u_down.space1)));
372
373 memset(&msg, 0, sizeof(union ocfs2_control_message));
374 ret = ocfs2_control_cfu(&msg, count, buf, count);
375 if (ret)
376 goto out;
377
378 if ((count == OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN) &&
379 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
380 OCFS2_CONTROL_MESSAGE_OP_LEN))
381 ret = ocfs2_control_do_setnode_msg(file, &msg.u_setn);
382 else if ((count == OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN) &&
383 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
384 OCFS2_CONTROL_MESSAGE_OP_LEN))
385 ret = ocfs2_control_do_down_msg(file, &msg.u_down);
386 else
387 ret = -EINVAL;
388
389out:
390 return ret ? ret : count;
271} 391}
272 392
273static ssize_t ocfs2_control_write(struct file *file, 393static ssize_t ocfs2_control_write(struct file *file,
@@ -283,10 +403,11 @@ static ssize_t ocfs2_control_write(struct file *file,
283 break; 403 break;
284 404
285 case OCFS2_CONTROL_HANDSHAKE_READ: 405 case OCFS2_CONTROL_HANDSHAKE_READ:
286 ret = ocfs2_control_validate_handshake(file, buf, 406 ret = ocfs2_control_validate_protocol(file, buf,
287 count); 407 count);
288 break; 408 break;
289 409
410 case OCFS2_CONTROL_HANDSHAKE_PROTOCOL:
290 case OCFS2_CONTROL_HANDSHAKE_VALID: 411 case OCFS2_CONTROL_HANDSHAKE_VALID:
291 ret = ocfs2_control_message(file, buf, count); 412 ret = ocfs2_control_message(file, buf, count);
292 break; 413 break;
@@ -350,6 +471,8 @@ static int ocfs2_control_release(struct inode *inode, struct file *file)
350 "an emergency restart!\n"); 471 "an emergency restart!\n");
351 emergency_restart(); 472 emergency_restart();
352 } 473 }
474 /* Last valid close clears the node number */
475 ocfs2_control_this_node = -1;
353 } 476 }
354 477
355out: 478out:
@@ -370,6 +493,7 @@ static int ocfs2_control_open(struct inode *inode, struct file *file)
370 p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL); 493 p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
371 if (!p) 494 if (!p)
372 return -ENOMEM; 495 return -ENOMEM;
496 p->op_this_node = -1;
373 497
374 mutex_lock(&ocfs2_control_lock); 498 mutex_lock(&ocfs2_control_lock);
375 file->private_data = p; 499 file->private_data = p;