]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/ocfs2/stack_user.c
ocfs2: Introduce the DOWN message to ocfs2_control
[karo-tx-linux.git] / fs / ocfs2 / stack_user.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * stack_user.c
5  *
6  * Code which interfaces ocfs2 with fs/dlm and a userspace stack.
7  *
8  * Copyright (C) 2007 Oracle.  All rights reserved.
9  *
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.
13  *
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.
18  */
19
20 #include <linux/module.h>
21 #include <linux/fs.h>
22 #include <linux/miscdevice.h>
23 #include <linux/mutex.h>
24 #include <linux/reboot.h>
25 #include <asm/uaccess.h>
26
27 #include "stackglue.h"
28
29
30 /*
31  * The control protocol starts with a handshake.  Until the handshake
32  * is complete, the control device will fail all write(2)s.
33  *
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.
42  *
43  * The T01 protocol only has one message, "DOWN".  It has the following
44  * syntax:
45  *
46  *  DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline>
47  *
48  * eg:
49  *
50  *  DOWN 632A924FDD844190BDA93C0DF6B94899 00000001\n
51  *
52  * This is 47 characters.
53  */
54
55 /*
56  * Whether or not the client has done the handshake.
57  * For now, we have just one protocol version.
58  */
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
69
70 /*
71  * ocfs2_live_connection is refcounted because the filesystem and
72  * miscdevice sides can detach in different order.  Let's just be safe.
73  */
74 struct ocfs2_live_connection {
75         struct list_head                oc_list;
76         struct ocfs2_cluster_connection *oc_conn;
77 };
78
79 struct ocfs2_control_private {
80         struct list_head op_list;
81         int op_state;
82 };
83
84 static atomic_t ocfs2_control_opened;
85
86 static LIST_HEAD(ocfs2_live_connection_list);
87 static LIST_HEAD(ocfs2_control_private_list);
88 static DEFINE_MUTEX(ocfs2_control_lock);
89
90 static inline void ocfs2_control_set_handshake_state(struct file *file,
91                                                      int state)
92 {
93         struct ocfs2_control_private *p = file->private_data;
94         p->op_state = state;
95 }
96
97 static inline int ocfs2_control_get_handshake_state(struct file *file)
98 {
99         struct ocfs2_control_private *p = file->private_data;
100         return p->op_state;
101 }
102
103 static struct ocfs2_live_connection *ocfs2_connection_find(const char *name)
104 {
105         size_t len = strlen(name);
106         struct ocfs2_live_connection *c;
107
108         BUG_ON(!mutex_is_locked(&ocfs2_control_lock));
109
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))
113                         return c;
114         }
115
116         return c;
117 }
118
119 /*
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.
123  */
124 static int ocfs2_live_connection_new(struct ocfs2_cluster_connection *conn,
125                                      struct ocfs2_live_connection **c_ret)
126 {
127         int rc = 0;
128         struct ocfs2_live_connection *c;
129
130         c = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
131         if (!c)
132                 return -ENOMEM;
133
134         mutex_lock(&ocfs2_control_lock);
135         c->oc_conn = conn;
136
137         if (atomic_read(&ocfs2_control_opened))
138                 list_add(&c->oc_list, &ocfs2_live_connection_list);
139         else {
140                 printk(KERN_ERR
141                        "ocfs2: Userspace control daemon is not present\n");
142                 rc = -ESRCH;
143         }
144
145         mutex_unlock(&ocfs2_control_lock);
146
147         if (!rc)
148                 *c_ret = c;
149         else
150                 kfree(c);
151
152         return rc;
153 }
154
155 /*
156  * This function disconnects the cluster connection from ocfs2_control.
157  * Afterwards, userspace can't affect the cluster connection.
158  */
159 static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
160 {
161         mutex_lock(&ocfs2_control_lock);
162         list_del_init(&c->oc_list);
163         c->oc_conn = NULL;
164         mutex_unlock(&ocfs2_control_lock);
165
166         kfree(c);
167 }
168
169 static ssize_t ocfs2_control_cfu(void *target, size_t target_len,
170                                  const char __user *buf, size_t count)
171 {
172         /* The T01 expects write(2) calls to have exactly one command */
173         if (count != target_len)
174                 return -EINVAL;
175
176         if (copy_from_user(target, buf, target_len))
177                 return -EFAULT;
178
179         return count;
180 }
181
182 static ssize_t ocfs2_control_validate_handshake(struct file *file,
183                                                 const char __user *buf,
184                                                 size_t count)
185 {
186         ssize_t ret;
187         char kbuf[OCFS2_CONTROL_PROTO_LEN];
188
189         ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
190                                 buf, count);
191         if (ret != count)
192                 return ret;
193
194         if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
195                 return -EINVAL;
196
197         atomic_inc(&ocfs2_control_opened);
198         ocfs2_control_set_handshake_state(file,
199                                           OCFS2_CONTROL_HANDSHAKE_VALID);
200
201
202         return count;
203 }
204
205 static void ocfs2_control_send_down(const char *uuid,
206                                     int nodenum)
207 {
208         struct ocfs2_live_connection *c;
209
210         mutex_lock(&ocfs2_control_lock);
211
212         c = ocfs2_connection_find(uuid);
213         if (c) {
214                 BUG_ON(c->oc_conn == NULL);
215                 c->oc_conn->cc_recovery_handler(nodenum,
216                                                 c->oc_conn->cc_recovery_data);
217         }
218
219         mutex_unlock(&ocfs2_control_lock);
220 }
221
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];
225         char    space1;
226         char    uuid[OCFS2_TEXT_UUID_LEN];
227         char    space2;
228         char    nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
229         char    newline;
230 };
231
232 static ssize_t ocfs2_control_message(struct file *file,
233                                      const char __user *buf,
234                                      size_t count)
235 {
236         ssize_t ret;
237         char *p = NULL;
238         long nodenum;
239         struct ocfs2_control_message_down msg;
240
241         /* Try to catch padding issues */
242         WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) !=
243                 (sizeof(msg.tag) + sizeof(msg.space1)));
244
245         memset(&msg, 0, sizeof(struct ocfs2_control_message_down));
246         ret = ocfs2_control_cfu(&msg, OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN,
247                                 buf, count);
248         if (ret != count)
249                 return ret;
250
251         if (strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN,
252                     strlen(OCFS2_CONTROL_MESSAGE_DOWN)))
253                 return -EINVAL;
254
255         if ((msg.space1 != ' ') || (msg.space2 != ' ') ||
256             (msg.newline != '\n'))
257                 return -EINVAL;
258         msg.space1 = msg.space2 = msg.newline = '\0';
259
260         nodenum = simple_strtol(msg.nodestr, &p, 16);
261         if (!p || *p)
262                 return -EINVAL;
263
264         if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
265             (nodenum > INT_MAX) || (nodenum < 0))
266                 return -ERANGE;
267
268         ocfs2_control_send_down(msg.uuid, nodenum);
269
270         return count;
271 }
272
273 static ssize_t ocfs2_control_write(struct file *file,
274                                    const char __user *buf,
275                                    size_t count,
276                                    loff_t *ppos)
277 {
278         ssize_t ret;
279
280         switch (ocfs2_control_get_handshake_state(file)) {
281                 case OCFS2_CONTROL_HANDSHAKE_INVALID:
282                         ret = -EINVAL;
283                         break;
284
285                 case OCFS2_CONTROL_HANDSHAKE_READ:
286                         ret = ocfs2_control_validate_handshake(file, buf,
287                                                                count);
288                         break;
289
290                 case OCFS2_CONTROL_HANDSHAKE_VALID:
291                         ret = ocfs2_control_message(file, buf, count);
292                         break;
293
294                 default:
295                         BUG();
296                         ret = -EIO;
297                         break;
298         }
299
300         return ret;
301 }
302
303 /*
304  * This is a naive version.  If we ever have a new protocol, we'll expand
305  * it.  Probably using seq_file.
306  */
307 static ssize_t ocfs2_control_read(struct file *file,
308                                   char __user *buf,
309                                   size_t count,
310                                   loff_t *ppos)
311 {
312         char *proto_string = OCFS2_CONTROL_PROTO;
313         size_t to_write = 0;
314
315         if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
316                 return 0;
317
318         to_write = OCFS2_CONTROL_PROTO_LEN - *ppos;
319         if (to_write > count)
320                 to_write = count;
321         if (copy_to_user(buf, proto_string + *ppos, to_write))
322                 return -EFAULT;
323
324         *ppos += to_write;
325
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);
330
331         return to_write;
332 }
333
334 static int ocfs2_control_release(struct inode *inode, struct file *file)
335 {
336         struct ocfs2_control_private *p = file->private_data;
337
338         mutex_lock(&ocfs2_control_lock);
339
340         if (ocfs2_control_get_handshake_state(file) !=
341             OCFS2_CONTROL_HANDSHAKE_VALID)
342                 goto out;
343
344         if (atomic_dec_and_test(&ocfs2_control_opened)) {
345                 if (!list_empty(&ocfs2_live_connection_list)) {
346                         /* XXX: Do bad things! */
347                         printk(KERN_ERR
348                                "ocfs2: Unexpected release of ocfs2_control!\n"
349                                "       Loss of cluster connection requires "
350                                "an emergency restart!\n");
351                         emergency_restart();
352                 }
353         }
354
355 out:
356         list_del_init(&p->op_list);
357         file->private_data = NULL;
358
359         mutex_unlock(&ocfs2_control_lock);
360
361         kfree(p);
362
363         return 0;
364 }
365
366 static int ocfs2_control_open(struct inode *inode, struct file *file)
367 {
368         struct ocfs2_control_private *p;
369
370         p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
371         if (!p)
372                 return -ENOMEM;
373
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);
378
379         return 0;
380 }
381
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,
388 };
389
390 struct miscdevice ocfs2_control_device = {
391         .minor          = MISC_DYNAMIC_MINOR,
392         .name           = "ocfs2_control",
393         .fops           = &ocfs2_control_fops,
394 };
395
396 static int ocfs2_control_init(void)
397 {
398         int rc;
399
400         atomic_set(&ocfs2_control_opened, 0);
401
402         rc = misc_register(&ocfs2_control_device);
403         if (rc)
404                 printk(KERN_ERR
405                        "ocfs2: Unable to register ocfs2_control device "
406                        "(errno %d)\n",
407                        -rc);
408
409         return rc;
410 }
411
412 static void ocfs2_control_exit(void)
413 {
414         int rc;
415
416         rc = misc_deregister(&ocfs2_control_device);
417         if (rc)
418                 printk(KERN_ERR
419                        "ocfs2: Unable to deregister ocfs2_control device "
420                        "(errno %d)\n",
421                        -rc);
422 }
423
424 static int __init user_stack_init(void)
425 {
426         return ocfs2_control_init();
427 }
428
429 static void __exit user_stack_exit(void)
430 {
431         ocfs2_control_exit();
432 }
433
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);