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. If the version tag
39 * written is unknown, -EINVAL is returned. Once the negotiation is
40 * complete, the client can start sending messages.
44 * Whether or not the client has done the handshake.
45 * For now, we have just one protocol version.
47 #define OCFS2_CONTROL_PROTO "T01\n"
48 #define OCFS2_CONTROL_PROTO_LEN 4
49 #define OCFS2_CONTROL_HANDSHAKE_INVALID (0)
50 #define OCFS2_CONTROL_HANDSHAKE_READ (1)
51 #define OCFS2_CONTROL_HANDSHAKE_VALID (2)
54 * ocfs2_live_connection is refcounted because the filesystem and
55 * miscdevice sides can detach in different order. Let's just be safe.
57 struct ocfs2_live_connection {
58 struct list_head oc_list;
59 struct ocfs2_cluster_connection *oc_conn;
62 struct ocfs2_control_private {
63 struct list_head op_list;
67 static atomic_t ocfs2_control_opened;
69 static LIST_HEAD(ocfs2_live_connection_list);
70 static LIST_HEAD(ocfs2_control_private_list);
71 static DEFINE_MUTEX(ocfs2_control_lock);
73 static inline void ocfs2_control_set_handshake_state(struct file *file,
76 struct ocfs2_control_private *p = file->private_data;
80 static inline int ocfs2_control_get_handshake_state(struct file *file)
82 struct ocfs2_control_private *p = file->private_data;
86 static struct ocfs2_live_connection *ocfs2_connection_find(const char *name)
88 size_t len = strlen(name);
89 struct ocfs2_live_connection *c;
91 BUG_ON(!mutex_is_locked(&ocfs2_control_lock));
93 list_for_each_entry(c, &ocfs2_live_connection_list, oc_list) {
94 if ((c->oc_conn->cc_namelen == len) &&
95 !strncmp(c->oc_conn->cc_name, name, len))
103 * ocfs2_live_connection structures are created underneath the ocfs2
104 * mount path. Since the VFS prevents multiple calls to
105 * fill_super(), we can't get dupes here.
107 static int ocfs2_live_connection_new(struct ocfs2_cluster_connection *conn,
108 struct ocfs2_live_connection **c_ret)
111 struct ocfs2_live_connection *c;
113 c = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
117 mutex_lock(&ocfs2_control_lock);
120 if (atomic_read(&ocfs2_control_opened))
121 list_add(&c->oc_list, &ocfs2_live_connection_list);
124 "ocfs2: Userspace control daemon is not present\n");
128 mutex_unlock(&ocfs2_control_lock);
139 * This function disconnects the cluster connection from ocfs2_control.
140 * Afterwards, userspace can't affect the cluster connection.
142 static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
144 mutex_lock(&ocfs2_control_lock);
145 list_del_init(&c->oc_list);
147 mutex_unlock(&ocfs2_control_lock);
152 static ssize_t ocfs2_control_cfu(char *target, size_t target_len,
153 const char __user *buf, size_t count)
155 /* The T01 expects write(2) calls to have exactly one command */
156 if (count != target_len)
159 if (copy_from_user(target, buf, target_len))
165 static ssize_t ocfs2_control_validate_handshake(struct file *file,
166 const char __user *buf,
170 char kbuf[OCFS2_CONTROL_PROTO_LEN];
172 ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
177 if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
180 atomic_inc(&ocfs2_control_opened);
181 ocfs2_control_set_handshake_state(file,
182 OCFS2_CONTROL_HANDSHAKE_VALID);
189 static ssize_t ocfs2_control_write(struct file *file,
190 const char __user *buf,
196 switch (ocfs2_control_get_handshake_state(file)) {
197 case OCFS2_CONTROL_HANDSHAKE_INVALID:
201 case OCFS2_CONTROL_HANDSHAKE_READ:
202 ret = ocfs2_control_validate_handshake(file, buf,
206 case OCFS2_CONTROL_HANDSHAKE_VALID:
207 ret = count; /* XXX */
220 * This is a naive version. If we ever have a new protocol, we'll expand
221 * it. Probably using seq_file.
223 static ssize_t ocfs2_control_read(struct file *file,
228 char *proto_string = OCFS2_CONTROL_PROTO;
231 if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
234 to_write = OCFS2_CONTROL_PROTO_LEN - *ppos;
235 if (to_write > count)
237 if (copy_to_user(buf, proto_string + *ppos, to_write))
242 /* Have we read the whole protocol list? */
243 if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
244 ocfs2_control_set_handshake_state(file,
245 OCFS2_CONTROL_HANDSHAKE_READ);
250 static int ocfs2_control_release(struct inode *inode, struct file *file)
252 struct ocfs2_control_private *p = file->private_data;
254 mutex_lock(&ocfs2_control_lock);
256 if (ocfs2_control_get_handshake_state(file) !=
257 OCFS2_CONTROL_HANDSHAKE_VALID)
260 if (atomic_dec_and_test(&ocfs2_control_opened)) {
261 if (!list_empty(&ocfs2_live_connection_list)) {
262 /* XXX: Do bad things! */
264 "ocfs2: Unexpected release of ocfs2_control!\n"
265 " Loss of cluster connection requires "
266 "an emergency restart!\n");
272 list_del_init(&p->op_list);
273 file->private_data = NULL;
275 mutex_unlock(&ocfs2_control_lock);
282 static int ocfs2_control_open(struct inode *inode, struct file *file)
284 struct ocfs2_control_private *p;
286 p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
290 mutex_lock(&ocfs2_control_lock);
291 file->private_data = p;
292 list_add(&p->op_list, &ocfs2_control_private_list);
293 mutex_unlock(&ocfs2_control_lock);
298 static const struct file_operations ocfs2_control_fops = {
299 .open = ocfs2_control_open,
300 .release = ocfs2_control_release,
301 .read = ocfs2_control_read,
302 .write = ocfs2_control_write,
303 .owner = THIS_MODULE,
306 struct miscdevice ocfs2_control_device = {
307 .minor = MISC_DYNAMIC_MINOR,
308 .name = "ocfs2_control",
309 .fops = &ocfs2_control_fops,
312 static int ocfs2_control_init(void)
316 atomic_set(&ocfs2_control_opened, 0);
318 rc = misc_register(&ocfs2_control_device);
321 "ocfs2: Unable to register ocfs2_control device "
328 static void ocfs2_control_exit(void)
332 rc = misc_deregister(&ocfs2_control_device);
335 "ocfs2: Unable to deregister ocfs2_control device "
340 static int __init user_stack_init(void)
342 return ocfs2_control_init();
345 static void __exit user_stack_exit(void)
347 ocfs2_control_exit();
350 MODULE_AUTHOR("Oracle");
351 MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
352 MODULE_LICENSE("GPL");
353 module_init(user_stack_init);
354 module_exit(user_stack_exit);