1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Code which interfaces ocfs2 with fs/dlm and a userspace stack.
8 * Copyright (C) 2007 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
20 #include <linux/module.h>
22 #include <linux/miscdevice.h>
23 #include <linux/mutex.h>
24 #include <linux/reboot.h>
25 #include <asm/uaccess.h>
27 #include "stackglue.h"
31 * The control protocol starts with a handshake. Until the handshake
32 * is complete, the control device will fail all write(2)s.
34 * The handshake is simple. First, the client reads until EOF. Each line
35 * of output is a supported protocol tag. All protocol tags are a single
36 * character followed by a two hex digit version number. Currently the
37 * only things supported is T01, for "Text-base version 0x01". Next, the
38 * client writes the version they would like to use, including the newline.
39 * Thus, the protocol tag is 'T01\n'. If the version tag written is
40 * unknown, -EINVAL is returned. Once the negotiation is complete, the
41 * client can start sending messages.
43 * The T01 protocol only has two messages. First is the "SETN" message.
44 * It has the following syntax:
46 * SETN<space><8-char-hex-nodenum><newline>
48 * This is 14 characters.
50 * The "SETN" message must be the first message following the protocol.
51 * It tells ocfs2_control the local node number.
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:
56 * DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline>
60 * DOWN 632A924FDD844190BDA93C0DF6B94899 00000001\n
62 * This is 47 characters.
66 * Whether or not the client has done the handshake.
67 * For now, we have just one protocol version.
69 #define OCFS2_CONTROL_PROTO "T01\n"
70 #define OCFS2_CONTROL_PROTO_LEN 4
72 /* Handshake states */
73 #define OCFS2_CONTROL_HANDSHAKE_INVALID (0)
74 #define OCFS2_CONTROL_HANDSHAKE_READ (1)
75 #define OCFS2_CONTROL_HANDSHAKE_PROTOCOL (2)
76 #define OCFS2_CONTROL_HANDSHAKE_VALID (3)
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"
83 #define OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN 47
84 #define OCFS2_TEXT_UUID_LEN 32
85 #define OCFS2_CONTROL_MESSAGE_NODENUM_LEN 8
88 * ocfs2_live_connection is refcounted because the filesystem and
89 * miscdevice sides can detach in different order. Let's just be safe.
91 struct ocfs2_live_connection {
92 struct list_head oc_list;
93 struct ocfs2_cluster_connection *oc_conn;
96 struct ocfs2_control_private {
97 struct list_head op_list;
102 /* SETN<space><8-char-hex-nodenum><newline> */
103 struct ocfs2_control_message_setn {
104 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
106 char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
110 /* DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline> */
111 struct ocfs2_control_message_down {
112 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
114 char uuid[OCFS2_TEXT_UUID_LEN];
116 char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
120 union 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;
126 static atomic_t ocfs2_control_opened;
127 static int ocfs2_control_this_node = -1;
129 static LIST_HEAD(ocfs2_live_connection_list);
130 static LIST_HEAD(ocfs2_control_private_list);
131 static DEFINE_MUTEX(ocfs2_control_lock);
133 static inline void ocfs2_control_set_handshake_state(struct file *file,
136 struct ocfs2_control_private *p = file->private_data;
140 static inline int ocfs2_control_get_handshake_state(struct file *file)
142 struct ocfs2_control_private *p = file->private_data;
146 static struct ocfs2_live_connection *ocfs2_connection_find(const char *name)
148 size_t len = strlen(name);
149 struct ocfs2_live_connection *c;
151 BUG_ON(!mutex_is_locked(&ocfs2_control_lock));
153 list_for_each_entry(c, &ocfs2_live_connection_list, oc_list) {
154 if ((c->oc_conn->cc_namelen == len) &&
155 !strncmp(c->oc_conn->cc_name, name, len))
163 * ocfs2_live_connection structures are created underneath the ocfs2
164 * mount path. Since the VFS prevents multiple calls to
165 * fill_super(), we can't get dupes here.
167 static int ocfs2_live_connection_new(struct ocfs2_cluster_connection *conn,
168 struct ocfs2_live_connection **c_ret)
171 struct ocfs2_live_connection *c;
173 c = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
177 mutex_lock(&ocfs2_control_lock);
180 if (atomic_read(&ocfs2_control_opened))
181 list_add(&c->oc_list, &ocfs2_live_connection_list);
184 "ocfs2: Userspace control daemon is not present\n");
188 mutex_unlock(&ocfs2_control_lock);
199 * This function disconnects the cluster connection from ocfs2_control.
200 * Afterwards, userspace can't affect the cluster connection.
202 static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
204 mutex_lock(&ocfs2_control_lock);
205 list_del_init(&c->oc_list);
207 mutex_unlock(&ocfs2_control_lock);
212 static int ocfs2_control_cfu(void *target, size_t target_len,
213 const char __user *buf, size_t count)
215 /* The T01 expects write(2) calls to have exactly one command */
216 if ((count != target_len) ||
217 (count > sizeof(union ocfs2_control_message)))
220 if (copy_from_user(target, buf, target_len))
226 static ssize_t ocfs2_control_validate_protocol(struct file *file,
227 const char __user *buf,
231 char kbuf[OCFS2_CONTROL_PROTO_LEN];
233 ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
238 if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
241 ocfs2_control_set_handshake_state(file,
242 OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
247 static void ocfs2_control_send_down(const char *uuid,
250 struct ocfs2_live_connection *c;
252 mutex_lock(&ocfs2_control_lock);
254 c = ocfs2_connection_find(uuid);
256 BUG_ON(c->oc_conn == NULL);
257 c->oc_conn->cc_recovery_handler(nodenum,
258 c->oc_conn->cc_recovery_data);
261 mutex_unlock(&ocfs2_control_lock);
265 * Called whenever configuration elements are sent to /dev/ocfs2_control.
266 * If all configuration elements are present, try to set the global
267 * values. If not, return -EAGAIN. If there is a problem, return a
270 static int ocfs2_control_install_private(struct file *file)
274 struct ocfs2_control_private *p = file->private_data;
276 BUG_ON(p->op_state != OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
278 if (p->op_this_node < 0)
281 mutex_lock(&ocfs2_control_lock);
282 if (ocfs2_control_this_node < 0) {
284 ocfs2_control_this_node = p->op_this_node;
285 } else if (ocfs2_control_this_node != p->op_this_node)
287 mutex_unlock(&ocfs2_control_lock);
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);
299 static int ocfs2_control_do_setnode_msg(struct file *file,
300 struct ocfs2_control_message_setn *msg)
304 struct ocfs2_control_private *p = file->private_data;
306 if (ocfs2_control_get_handshake_state(file) !=
307 OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
310 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
311 OCFS2_CONTROL_MESSAGE_OP_LEN))
314 if ((msg->space != ' ') || (msg->newline != '\n'))
316 msg->space = msg->newline = '\0';
318 nodenum = simple_strtol(msg->nodestr, &ptr, 16);
322 if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
323 (nodenum > INT_MAX) || (nodenum < 0))
325 p->op_this_node = nodenum;
327 return ocfs2_control_install_private(file);
330 static int ocfs2_control_do_down_msg(struct file *file,
331 struct ocfs2_control_message_down *msg)
336 if (ocfs2_control_get_handshake_state(file) !=
337 OCFS2_CONTROL_HANDSHAKE_VALID)
340 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
341 OCFS2_CONTROL_MESSAGE_OP_LEN))
344 if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
345 (msg->newline != '\n'))
347 msg->space1 = msg->space2 = msg->newline = '\0';
349 nodenum = simple_strtol(msg->nodestr, &p, 16);
353 if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
354 (nodenum > INT_MAX) || (nodenum < 0))
357 ocfs2_control_send_down(msg->uuid, nodenum);
362 static ssize_t ocfs2_control_message(struct file *file,
363 const char __user *buf,
367 union ocfs2_control_message msg;
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)));
373 memset(&msg, 0, sizeof(union ocfs2_control_message));
374 ret = ocfs2_control_cfu(&msg, count, buf, count);
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);
390 return ret ? ret : count;
393 static ssize_t ocfs2_control_write(struct file *file,
394 const char __user *buf,
400 switch (ocfs2_control_get_handshake_state(file)) {
401 case OCFS2_CONTROL_HANDSHAKE_INVALID:
405 case OCFS2_CONTROL_HANDSHAKE_READ:
406 ret = ocfs2_control_validate_protocol(file, buf,
410 case OCFS2_CONTROL_HANDSHAKE_PROTOCOL:
411 case OCFS2_CONTROL_HANDSHAKE_VALID:
412 ret = ocfs2_control_message(file, buf, count);
425 * This is a naive version. If we ever have a new protocol, we'll expand
426 * it. Probably using seq_file.
428 static ssize_t ocfs2_control_read(struct file *file,
433 char *proto_string = OCFS2_CONTROL_PROTO;
436 if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
439 to_write = OCFS2_CONTROL_PROTO_LEN - *ppos;
440 if (to_write > count)
442 if (copy_to_user(buf, proto_string + *ppos, to_write))
447 /* Have we read the whole protocol list? */
448 if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
449 ocfs2_control_set_handshake_state(file,
450 OCFS2_CONTROL_HANDSHAKE_READ);
455 static int ocfs2_control_release(struct inode *inode, struct file *file)
457 struct ocfs2_control_private *p = file->private_data;
459 mutex_lock(&ocfs2_control_lock);
461 if (ocfs2_control_get_handshake_state(file) !=
462 OCFS2_CONTROL_HANDSHAKE_VALID)
465 if (atomic_dec_and_test(&ocfs2_control_opened)) {
466 if (!list_empty(&ocfs2_live_connection_list)) {
467 /* XXX: Do bad things! */
469 "ocfs2: Unexpected release of ocfs2_control!\n"
470 " Loss of cluster connection requires "
471 "an emergency restart!\n");
474 /* Last valid close clears the node number */
475 ocfs2_control_this_node = -1;
479 list_del_init(&p->op_list);
480 file->private_data = NULL;
482 mutex_unlock(&ocfs2_control_lock);
489 static int ocfs2_control_open(struct inode *inode, struct file *file)
491 struct ocfs2_control_private *p;
493 p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
496 p->op_this_node = -1;
498 mutex_lock(&ocfs2_control_lock);
499 file->private_data = p;
500 list_add(&p->op_list, &ocfs2_control_private_list);
501 mutex_unlock(&ocfs2_control_lock);
506 static const struct file_operations ocfs2_control_fops = {
507 .open = ocfs2_control_open,
508 .release = ocfs2_control_release,
509 .read = ocfs2_control_read,
510 .write = ocfs2_control_write,
511 .owner = THIS_MODULE,
514 struct miscdevice ocfs2_control_device = {
515 .minor = MISC_DYNAMIC_MINOR,
516 .name = "ocfs2_control",
517 .fops = &ocfs2_control_fops,
520 static int ocfs2_control_init(void)
524 atomic_set(&ocfs2_control_opened, 0);
526 rc = misc_register(&ocfs2_control_device);
529 "ocfs2: Unable to register ocfs2_control device "
536 static void ocfs2_control_exit(void)
540 rc = misc_deregister(&ocfs2_control_device);
543 "ocfs2: Unable to deregister ocfs2_control device "
548 static int __init user_stack_init(void)
550 return ocfs2_control_init();
553 static void __exit user_stack_exit(void)
555 ocfs2_control_exit();
558 MODULE_AUTHOR("Oracle");
559 MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
560 MODULE_LICENSE("GPL");
561 module_init(user_stack_init);
562 module_exit(user_stack_exit);