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 one message, "DOWN". It has the following
46 * DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline>
50 * DOWN 632A924FDD844190BDA93C0DF6B94899 00000001\n
52 * This is 47 characters.
56 * Whether or not the client has done the handshake.
57 * For now, we have just one protocol version.
59 #define OCFS2_CONTROL_PROTO "T01\n"
60 #define OCFS2_CONTROL_PROTO_LEN 4
61 #define OCFS2_CONTROL_HANDSHAKE_INVALID (0)
62 #define OCFS2_CONTROL_HANDSHAKE_READ (1)
63 #define OCFS2_CONTROL_HANDSHAKE_VALID (2)
64 #define OCFS2_CONTROL_MESSAGE_DOWN "DOWN"
65 #define OCFS2_CONTROL_MESSAGE_DOWN_LEN 4
66 #define OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN 47
67 #define OCFS2_TEXT_UUID_LEN 32
68 #define OCFS2_CONTROL_MESSAGE_NODENUM_LEN 8
71 * ocfs2_live_connection is refcounted because the filesystem and
72 * miscdevice sides can detach in different order. Let's just be safe.
74 struct ocfs2_live_connection {
75 struct list_head oc_list;
76 struct ocfs2_cluster_connection *oc_conn;
79 struct ocfs2_control_private {
80 struct list_head op_list;
84 static atomic_t ocfs2_control_opened;
86 static LIST_HEAD(ocfs2_live_connection_list);
87 static LIST_HEAD(ocfs2_control_private_list);
88 static DEFINE_MUTEX(ocfs2_control_lock);
90 static inline void ocfs2_control_set_handshake_state(struct file *file,
93 struct ocfs2_control_private *p = file->private_data;
97 static inline int ocfs2_control_get_handshake_state(struct file *file)
99 struct ocfs2_control_private *p = file->private_data;
103 static struct ocfs2_live_connection *ocfs2_connection_find(const char *name)
105 size_t len = strlen(name);
106 struct ocfs2_live_connection *c;
108 BUG_ON(!mutex_is_locked(&ocfs2_control_lock));
110 list_for_each_entry(c, &ocfs2_live_connection_list, oc_list) {
111 if ((c->oc_conn->cc_namelen == len) &&
112 !strncmp(c->oc_conn->cc_name, name, len))
120 * ocfs2_live_connection structures are created underneath the ocfs2
121 * mount path. Since the VFS prevents multiple calls to
122 * fill_super(), we can't get dupes here.
124 static int ocfs2_live_connection_new(struct ocfs2_cluster_connection *conn,
125 struct ocfs2_live_connection **c_ret)
128 struct ocfs2_live_connection *c;
130 c = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
134 mutex_lock(&ocfs2_control_lock);
137 if (atomic_read(&ocfs2_control_opened))
138 list_add(&c->oc_list, &ocfs2_live_connection_list);
141 "ocfs2: Userspace control daemon is not present\n");
145 mutex_unlock(&ocfs2_control_lock);
156 * This function disconnects the cluster connection from ocfs2_control.
157 * Afterwards, userspace can't affect the cluster connection.
159 static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
161 mutex_lock(&ocfs2_control_lock);
162 list_del_init(&c->oc_list);
164 mutex_unlock(&ocfs2_control_lock);
169 static ssize_t ocfs2_control_cfu(void *target, size_t target_len,
170 const char __user *buf, size_t count)
172 /* The T01 expects write(2) calls to have exactly one command */
173 if (count != target_len)
176 if (copy_from_user(target, buf, target_len))
182 static ssize_t ocfs2_control_validate_handshake(struct file *file,
183 const char __user *buf,
187 char kbuf[OCFS2_CONTROL_PROTO_LEN];
189 ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
194 if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
197 atomic_inc(&ocfs2_control_opened);
198 ocfs2_control_set_handshake_state(file,
199 OCFS2_CONTROL_HANDSHAKE_VALID);
205 static void ocfs2_control_send_down(const char *uuid,
208 struct ocfs2_live_connection *c;
210 mutex_lock(&ocfs2_control_lock);
212 c = ocfs2_connection_find(uuid);
214 BUG_ON(c->oc_conn == NULL);
215 c->oc_conn->cc_recovery_handler(nodenum,
216 c->oc_conn->cc_recovery_data);
219 mutex_unlock(&ocfs2_control_lock);
222 /* DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline> */
223 struct ocfs2_control_message_down {
224 char tag[OCFS2_CONTROL_MESSAGE_DOWN_LEN];
226 char uuid[OCFS2_TEXT_UUID_LEN];
228 char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
232 static ssize_t ocfs2_control_message(struct file *file,
233 const char __user *buf,
239 struct ocfs2_control_message_down msg;
241 /* Try to catch padding issues */
242 WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) !=
243 (sizeof(msg.tag) + sizeof(msg.space1)));
245 memset(&msg, 0, sizeof(struct ocfs2_control_message_down));
246 ret = ocfs2_control_cfu(&msg, OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN,
251 if (strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN,
252 strlen(OCFS2_CONTROL_MESSAGE_DOWN)))
255 if ((msg.space1 != ' ') || (msg.space2 != ' ') ||
256 (msg.newline != '\n'))
258 msg.space1 = msg.space2 = msg.newline = '\0';
260 nodenum = simple_strtol(msg.nodestr, &p, 16);
264 if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
265 (nodenum > INT_MAX) || (nodenum < 0))
268 ocfs2_control_send_down(msg.uuid, nodenum);
273 static ssize_t ocfs2_control_write(struct file *file,
274 const char __user *buf,
280 switch (ocfs2_control_get_handshake_state(file)) {
281 case OCFS2_CONTROL_HANDSHAKE_INVALID:
285 case OCFS2_CONTROL_HANDSHAKE_READ:
286 ret = ocfs2_control_validate_handshake(file, buf,
290 case OCFS2_CONTROL_HANDSHAKE_VALID:
291 ret = ocfs2_control_message(file, buf, count);
304 * This is a naive version. If we ever have a new protocol, we'll expand
305 * it. Probably using seq_file.
307 static ssize_t ocfs2_control_read(struct file *file,
312 char *proto_string = OCFS2_CONTROL_PROTO;
315 if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
318 to_write = OCFS2_CONTROL_PROTO_LEN - *ppos;
319 if (to_write > count)
321 if (copy_to_user(buf, proto_string + *ppos, to_write))
326 /* Have we read the whole protocol list? */
327 if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
328 ocfs2_control_set_handshake_state(file,
329 OCFS2_CONTROL_HANDSHAKE_READ);
334 static int ocfs2_control_release(struct inode *inode, struct file *file)
336 struct ocfs2_control_private *p = file->private_data;
338 mutex_lock(&ocfs2_control_lock);
340 if (ocfs2_control_get_handshake_state(file) !=
341 OCFS2_CONTROL_HANDSHAKE_VALID)
344 if (atomic_dec_and_test(&ocfs2_control_opened)) {
345 if (!list_empty(&ocfs2_live_connection_list)) {
346 /* XXX: Do bad things! */
348 "ocfs2: Unexpected release of ocfs2_control!\n"
349 " Loss of cluster connection requires "
350 "an emergency restart!\n");
356 list_del_init(&p->op_list);
357 file->private_data = NULL;
359 mutex_unlock(&ocfs2_control_lock);
366 static int ocfs2_control_open(struct inode *inode, struct file *file)
368 struct ocfs2_control_private *p;
370 p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
374 mutex_lock(&ocfs2_control_lock);
375 file->private_data = p;
376 list_add(&p->op_list, &ocfs2_control_private_list);
377 mutex_unlock(&ocfs2_control_lock);
382 static const struct file_operations ocfs2_control_fops = {
383 .open = ocfs2_control_open,
384 .release = ocfs2_control_release,
385 .read = ocfs2_control_read,
386 .write = ocfs2_control_write,
387 .owner = THIS_MODULE,
390 struct miscdevice ocfs2_control_device = {
391 .minor = MISC_DYNAMIC_MINOR,
392 .name = "ocfs2_control",
393 .fops = &ocfs2_control_fops,
396 static int ocfs2_control_init(void)
400 atomic_set(&ocfs2_control_opened, 0);
402 rc = misc_register(&ocfs2_control_device);
405 "ocfs2: Unable to register ocfs2_control device "
412 static void ocfs2_control_exit(void)
416 rc = misc_deregister(&ocfs2_control_device);
419 "ocfs2: Unable to deregister ocfs2_control device "
424 static int __init user_stack_init(void)
426 return ocfs2_control_init();
429 static void __exit user_stack_exit(void)
431 ocfs2_control_exit();
434 MODULE_AUTHOR("Oracle");
435 MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
436 MODULE_LICENSE("GPL");
437 module_init(user_stack_init);
438 module_exit(user_stack_exit);