]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/ocfs2/stack_user.c
43e6105369c6dc8de9d91d2f73c84217eeae8321
[linux-2.6-omap-h63xx.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 two messages.  First is the "SETN" message.
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:
55  *
56  *  DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline>
57  *
58  * eg:
59  *
60  *  DOWN 632A924FDD844190BDA93C0DF6B94899 00000001\n
61  *
62  * This is 47 characters.
63  */
64
65 /*
66  * Whether or not the client has done the handshake.
67  * For now, we have just one protocol version.
68  */
69 #define OCFS2_CONTROL_PROTO                     "T01\n"
70 #define OCFS2_CONTROL_PROTO_LEN                 4
71
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)
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"
83 #define OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN    47
84 #define OCFS2_TEXT_UUID_LEN                     32
85 #define OCFS2_CONTROL_MESSAGE_NODENUM_LEN       8
86
87 /*
88  * ocfs2_live_connection is refcounted because the filesystem and
89  * miscdevice sides can detach in different order.  Let's just be safe.
90  */
91 struct ocfs2_live_connection {
92         struct list_head                oc_list;
93         struct ocfs2_cluster_connection *oc_conn;
94 };
95
96 struct ocfs2_control_private {
97         struct list_head op_list;
98         int op_state;
99         int op_this_node;
100 };
101
102 /* SETN<space><8-char-hex-nodenum><newline> */
103 struct 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> */
111 struct 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
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;
124 };
125
126 static atomic_t ocfs2_control_opened;
127 static int ocfs2_control_this_node = -1;
128
129 static LIST_HEAD(ocfs2_live_connection_list);
130 static LIST_HEAD(ocfs2_control_private_list);
131 static DEFINE_MUTEX(ocfs2_control_lock);
132
133 static inline void ocfs2_control_set_handshake_state(struct file *file,
134                                                      int state)
135 {
136         struct ocfs2_control_private *p = file->private_data;
137         p->op_state = state;
138 }
139
140 static inline int ocfs2_control_get_handshake_state(struct file *file)
141 {
142         struct ocfs2_control_private *p = file->private_data;
143         return p->op_state;
144 }
145
146 static struct ocfs2_live_connection *ocfs2_connection_find(const char *name)
147 {
148         size_t len = strlen(name);
149         struct ocfs2_live_connection *c;
150
151         BUG_ON(!mutex_is_locked(&ocfs2_control_lock));
152
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))
156                         return c;
157         }
158
159         return c;
160 }
161
162 /*
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.
166  */
167 static int ocfs2_live_connection_new(struct ocfs2_cluster_connection *conn,
168                                      struct ocfs2_live_connection **c_ret)
169 {
170         int rc = 0;
171         struct ocfs2_live_connection *c;
172
173         c = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
174         if (!c)
175                 return -ENOMEM;
176
177         mutex_lock(&ocfs2_control_lock);
178         c->oc_conn = conn;
179
180         if (atomic_read(&ocfs2_control_opened))
181                 list_add(&c->oc_list, &ocfs2_live_connection_list);
182         else {
183                 printk(KERN_ERR
184                        "ocfs2: Userspace control daemon is not present\n");
185                 rc = -ESRCH;
186         }
187
188         mutex_unlock(&ocfs2_control_lock);
189
190         if (!rc)
191                 *c_ret = c;
192         else
193                 kfree(c);
194
195         return rc;
196 }
197
198 /*
199  * This function disconnects the cluster connection from ocfs2_control.
200  * Afterwards, userspace can't affect the cluster connection.
201  */
202 static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
203 {
204         mutex_lock(&ocfs2_control_lock);
205         list_del_init(&c->oc_list);
206         c->oc_conn = NULL;
207         mutex_unlock(&ocfs2_control_lock);
208
209         kfree(c);
210 }
211
212 static int ocfs2_control_cfu(void *target, size_t target_len,
213                              const char __user *buf, size_t count)
214 {
215         /* The T01 expects write(2) calls to have exactly one command */
216         if ((count != target_len) ||
217             (count > sizeof(union ocfs2_control_message)))
218                 return -EINVAL;
219
220         if (copy_from_user(target, buf, target_len))
221                 return -EFAULT;
222
223         return 0;
224 }
225
226 static ssize_t ocfs2_control_validate_protocol(struct file *file,
227                                                const char __user *buf,
228                                                size_t count)
229 {
230         ssize_t ret;
231         char kbuf[OCFS2_CONTROL_PROTO_LEN];
232
233         ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
234                                 buf, count);
235         if (ret)
236                 return ret;
237
238         if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
239                 return -EINVAL;
240
241         ocfs2_control_set_handshake_state(file,
242                                           OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
243
244         return count;
245 }
246
247 static void ocfs2_control_send_down(const char *uuid,
248                                     int nodenum)
249 {
250         struct ocfs2_live_connection *c;
251
252         mutex_lock(&ocfs2_control_lock);
253
254         c = ocfs2_connection_find(uuid);
255         if (c) {
256                 BUG_ON(c->oc_conn == NULL);
257                 c->oc_conn->cc_recovery_handler(nodenum,
258                                                 c->oc_conn->cc_recovery_data);
259         }
260
261         mutex_unlock(&ocfs2_control_lock);
262 }
263
264 /*
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
268  * different error.
269  */
270 static int ocfs2_control_install_private(struct file *file)
271 {
272         int rc = 0;
273         int set_p = 1;
274         struct ocfs2_control_private *p = file->private_data;
275
276         BUG_ON(p->op_state != OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
277
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
299 static int ocfs2_control_do_setnode_msg(struct file *file,
300                                         struct ocfs2_control_message_setn *msg)
301 {
302         long nodenum;
303         char *ptr = NULL;
304         struct ocfs2_control_private *p = file->private_data;
305
306         if (ocfs2_control_get_handshake_state(file) !=
307             OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
308                 return -EINVAL;
309
310         if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
311                     OCFS2_CONTROL_MESSAGE_OP_LEN))
312                 return -EINVAL;
313
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
330 static 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;
339
340         if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
341                     OCFS2_CONTROL_MESSAGE_OP_LEN))
342                 return -EINVAL;
343
344         if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
345             (msg->newline != '\n'))
346                 return -EINVAL;
347         msg->space1 = msg->space2 = msg->newline = '\0';
348
349         nodenum = simple_strtol(msg->nodestr, &p, 16);
350         if (!p || *p)
351                 return -EINVAL;
352
353         if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
354             (nodenum > INT_MAX) || (nodenum < 0))
355                 return -ERANGE;
356
357         ocfs2_control_send_down(msg->uuid, nodenum);
358
359         return 0;
360 }
361
362 static 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
389 out:
390         return ret ? ret : count;
391 }
392
393 static ssize_t ocfs2_control_write(struct file *file,
394                                    const char __user *buf,
395                                    size_t count,
396                                    loff_t *ppos)
397 {
398         ssize_t ret;
399
400         switch (ocfs2_control_get_handshake_state(file)) {
401                 case OCFS2_CONTROL_HANDSHAKE_INVALID:
402                         ret = -EINVAL;
403                         break;
404
405                 case OCFS2_CONTROL_HANDSHAKE_READ:
406                         ret = ocfs2_control_validate_protocol(file, buf,
407                                                               count);
408                         break;
409
410                 case OCFS2_CONTROL_HANDSHAKE_PROTOCOL:
411                 case OCFS2_CONTROL_HANDSHAKE_VALID:
412                         ret = ocfs2_control_message(file, buf, count);
413                         break;
414
415                 default:
416                         BUG();
417                         ret = -EIO;
418                         break;
419         }
420
421         return ret;
422 }
423
424 /*
425  * This is a naive version.  If we ever have a new protocol, we'll expand
426  * it.  Probably using seq_file.
427  */
428 static ssize_t ocfs2_control_read(struct file *file,
429                                   char __user *buf,
430                                   size_t count,
431                                   loff_t *ppos)
432 {
433         char *proto_string = OCFS2_CONTROL_PROTO;
434         size_t to_write = 0;
435
436         if (*ppos >= OCFS2_CONTROL_PROTO_LEN)
437                 return 0;
438
439         to_write = OCFS2_CONTROL_PROTO_LEN - *ppos;
440         if (to_write > count)
441                 to_write = count;
442         if (copy_to_user(buf, proto_string + *ppos, to_write))
443                 return -EFAULT;
444
445         *ppos += to_write;
446
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);
451
452         return to_write;
453 }
454
455 static int ocfs2_control_release(struct inode *inode, struct file *file)
456 {
457         struct ocfs2_control_private *p = file->private_data;
458
459         mutex_lock(&ocfs2_control_lock);
460
461         if (ocfs2_control_get_handshake_state(file) !=
462             OCFS2_CONTROL_HANDSHAKE_VALID)
463                 goto out;
464
465         if (atomic_dec_and_test(&ocfs2_control_opened)) {
466                 if (!list_empty(&ocfs2_live_connection_list)) {
467                         /* XXX: Do bad things! */
468                         printk(KERN_ERR
469                                "ocfs2: Unexpected release of ocfs2_control!\n"
470                                "       Loss of cluster connection requires "
471                                "an emergency restart!\n");
472                         emergency_restart();
473                 }
474                 /* Last valid close clears the node number */
475                 ocfs2_control_this_node = -1;
476         }
477
478 out:
479         list_del_init(&p->op_list);
480         file->private_data = NULL;
481
482         mutex_unlock(&ocfs2_control_lock);
483
484         kfree(p);
485
486         return 0;
487 }
488
489 static int ocfs2_control_open(struct inode *inode, struct file *file)
490 {
491         struct ocfs2_control_private *p;
492
493         p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
494         if (!p)
495                 return -ENOMEM;
496         p->op_this_node = -1;
497
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);
502
503         return 0;
504 }
505
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,
512 };
513
514 struct miscdevice ocfs2_control_device = {
515         .minor          = MISC_DYNAMIC_MINOR,
516         .name           = "ocfs2_control",
517         .fops           = &ocfs2_control_fops,
518 };
519
520 static int ocfs2_control_init(void)
521 {
522         int rc;
523
524         atomic_set(&ocfs2_control_opened, 0);
525
526         rc = misc_register(&ocfs2_control_device);
527         if (rc)
528                 printk(KERN_ERR
529                        "ocfs2: Unable to register ocfs2_control device "
530                        "(errno %d)\n",
531                        -rc);
532
533         return rc;
534 }
535
536 static void ocfs2_control_exit(void)
537 {
538         int rc;
539
540         rc = misc_deregister(&ocfs2_control_device);
541         if (rc)
542                 printk(KERN_ERR
543                        "ocfs2: Unable to deregister ocfs2_control device "
544                        "(errno %d)\n",
545                        -rc);
546 }
547
548 static int __init user_stack_init(void)
549 {
550         return ocfs2_control_init();
551 }
552
553 static void __exit user_stack_exit(void)
554 {
555         ocfs2_control_exit();
556 }
557
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);