blob: 4111855a4defad8f5c66ecc3bf1b3e695e2f3fb7 [file] [log] [blame]
Joel Becker8adf0532007-11-28 14:38:40 -08001/* -*- 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>
Joel Becker6427a722008-02-18 19:23:28 -080021#include <linux/fs.h>
22#include <linux/miscdevice.h>
23#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Joel Becker6427a722008-02-18 19:23:28 -080025#include <linux/reboot.h>
Joel Becker462c7e62008-02-18 19:40:12 -080026#include <asm/uaccess.h>
Joel Becker8adf0532007-11-28 14:38:40 -080027
28#include "stackglue.h"
29
Mark Fasheh53da4932008-07-21 14:29:16 -070030#include <linux/dlm_plock.h>
Joel Becker8adf0532007-11-28 14:38:40 -080031
Joel Becker6427a722008-02-18 19:23:28 -080032/*
33 * The control protocol starts with a handshake. Until the handshake
34 * is complete, the control device will fail all write(2)s.
35 *
36 * The handshake is simple. First, the client reads until EOF. Each line
37 * of output is a supported protocol tag. All protocol tags are a single
38 * character followed by a two hex digit version number. Currently the
39 * only things supported is T01, for "Text-base version 0x01". Next, the
Joel Beckerde870ef2008-02-18 17:07:09 -080040 * client writes the version they would like to use, including the newline.
41 * Thus, the protocol tag is 'T01\n'. If the version tag written is
42 * unknown, -EINVAL is returned. Once the negotiation is complete, the
43 * client can start sending messages.
44 *
Joel Beckerd4b95ee2008-02-20 15:39:44 -080045 * The T01 protocol has three messages. First is the "SETN" message.
Joel Becker3cfd4ab2008-02-20 14:44:34 -080046 * It has the following syntax:
47 *
48 * SETN<space><8-char-hex-nodenum><newline>
49 *
50 * This is 14 characters.
51 *
52 * The "SETN" message must be the first message following the protocol.
53 * It tells ocfs2_control the local node number.
54 *
Joel Beckerd4b95ee2008-02-20 15:39:44 -080055 * Next comes the "SETV" message. It has the following syntax:
56 *
57 * SETV<space><2-char-hex-major><space><2-char-hex-minor><newline>
58 *
59 * This is 11 characters.
60 *
61 * The "SETV" message sets the filesystem locking protocol version as
62 * negotiated by the client. The client negotiates based on the maximum
63 * version advertised in /sys/fs/ocfs2/max_locking_protocol. The major
64 * number from the "SETV" message must match
Joel Beckere603cfb2010-01-29 16:06:29 -080065 * ocfs2_user_plugin.sp_max_proto.pv_major, and the minor number
66 * must be less than or equal to ...sp_max_version.pv_minor.
Joel Beckerd4b95ee2008-02-20 15:39:44 -080067 *
68 * Once this information has been set, mounts will be allowed. From this
69 * point on, the "DOWN" message can be sent for node down notification.
70 * It has the following syntax:
Joel Beckerde870ef2008-02-18 17:07:09 -080071 *
72 * DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline>
73 *
74 * eg:
75 *
76 * DOWN 632A924FDD844190BDA93C0DF6B94899 00000001\n
77 *
78 * This is 47 characters.
Joel Becker6427a722008-02-18 19:23:28 -080079 */
80
81/*
Joel Becker462c7e62008-02-18 19:40:12 -080082 * Whether or not the client has done the handshake.
83 * For now, we have just one protocol version.
84 */
85#define OCFS2_CONTROL_PROTO "T01\n"
86#define OCFS2_CONTROL_PROTO_LEN 4
Joel Becker3cfd4ab2008-02-20 14:44:34 -080087
88/* Handshake states */
Joel Becker462c7e62008-02-18 19:40:12 -080089#define OCFS2_CONTROL_HANDSHAKE_INVALID (0)
90#define OCFS2_CONTROL_HANDSHAKE_READ (1)
Joel Becker3cfd4ab2008-02-20 14:44:34 -080091#define OCFS2_CONTROL_HANDSHAKE_PROTOCOL (2)
92#define OCFS2_CONTROL_HANDSHAKE_VALID (3)
93
94/* Messages */
95#define OCFS2_CONTROL_MESSAGE_OP_LEN 4
96#define OCFS2_CONTROL_MESSAGE_SETNODE_OP "SETN"
97#define OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN 14
Joel Beckerd4b95ee2008-02-20 15:39:44 -080098#define OCFS2_CONTROL_MESSAGE_SETVERSION_OP "SETV"
99#define OCFS2_CONTROL_MESSAGE_SETVERSION_TOTAL_LEN 11
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800100#define OCFS2_CONTROL_MESSAGE_DOWN_OP "DOWN"
Joel Beckerde870ef2008-02-18 17:07:09 -0800101#define OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN 47
102#define OCFS2_TEXT_UUID_LEN 32
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800103#define OCFS2_CONTROL_MESSAGE_VERNUM_LEN 2
Joel Beckerde870ef2008-02-18 17:07:09 -0800104#define OCFS2_CONTROL_MESSAGE_NODENUM_LEN 8
Joel Becker462c7e62008-02-18 19:40:12 -0800105
106/*
Joel Becker6427a722008-02-18 19:23:28 -0800107 * ocfs2_live_connection is refcounted because the filesystem and
108 * miscdevice sides can detach in different order. Let's just be safe.
109 */
110struct ocfs2_live_connection {
111 struct list_head oc_list;
112 struct ocfs2_cluster_connection *oc_conn;
Goldwyn Rodrigues66e188f2014-01-21 15:48:22 -0800113 atomic_t oc_this_node;
114 int oc_our_slot;
Joel Becker6427a722008-02-18 19:23:28 -0800115};
116
Joel Becker462c7e62008-02-18 19:40:12 -0800117struct ocfs2_control_private {
118 struct list_head op_list;
119 int op_state;
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800120 int op_this_node;
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800121 struct ocfs2_protocol_version op_proto;
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800122};
123
124/* SETN<space><8-char-hex-nodenum><newline> */
125struct ocfs2_control_message_setn {
126 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
127 char space;
128 char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
129 char newline;
130};
131
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800132/* SETV<space><2-char-hex-major><space><2-char-hex-minor><newline> */
133struct ocfs2_control_message_setv {
134 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
135 char space1;
136 char major[OCFS2_CONTROL_MESSAGE_VERNUM_LEN];
137 char space2;
138 char minor[OCFS2_CONTROL_MESSAGE_VERNUM_LEN];
139 char newline;
140};
141
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800142/* DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline> */
143struct ocfs2_control_message_down {
144 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
145 char space1;
146 char uuid[OCFS2_TEXT_UUID_LEN];
147 char space2;
148 char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
149 char newline;
150};
151
152union ocfs2_control_message {
153 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
154 struct ocfs2_control_message_setn u_setn;
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800155 struct ocfs2_control_message_setv u_setv;
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800156 struct ocfs2_control_message_down u_down;
Joel Becker462c7e62008-02-18 19:40:12 -0800157};
158
Joel Beckera12630b2008-05-09 18:49:29 -0700159static struct ocfs2_stack_plugin ocfs2_user_plugin;
David Teiglandcf4d8d72008-02-20 14:29:27 -0800160
Joel Becker6427a722008-02-18 19:23:28 -0800161static atomic_t ocfs2_control_opened;
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800162static int ocfs2_control_this_node = -1;
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800163static struct ocfs2_protocol_version running_proto;
Joel Becker6427a722008-02-18 19:23:28 -0800164
165static LIST_HEAD(ocfs2_live_connection_list);
Joel Becker462c7e62008-02-18 19:40:12 -0800166static LIST_HEAD(ocfs2_control_private_list);
Joel Becker6427a722008-02-18 19:23:28 -0800167static DEFINE_MUTEX(ocfs2_control_lock);
168
Joel Becker462c7e62008-02-18 19:40:12 -0800169static inline void ocfs2_control_set_handshake_state(struct file *file,
170 int state)
171{
172 struct ocfs2_control_private *p = file->private_data;
173 p->op_state = state;
174}
175
176static inline int ocfs2_control_get_handshake_state(struct file *file)
177{
178 struct ocfs2_control_private *p = file->private_data;
179 return p->op_state;
180}
181
Joel Becker6427a722008-02-18 19:23:28 -0800182static struct ocfs2_live_connection *ocfs2_connection_find(const char *name)
183{
184 size_t len = strlen(name);
185 struct ocfs2_live_connection *c;
186
187 BUG_ON(!mutex_is_locked(&ocfs2_control_lock));
188
189 list_for_each_entry(c, &ocfs2_live_connection_list, oc_list) {
190 if ((c->oc_conn->cc_namelen == len) &&
191 !strncmp(c->oc_conn->cc_name, name, len))
192 return c;
193 }
194
dann frazier226291a2010-11-18 15:03:09 -0700195 return NULL;
Joel Becker6427a722008-02-18 19:23:28 -0800196}
197
198/*
199 * ocfs2_live_connection structures are created underneath the ocfs2
200 * mount path. Since the VFS prevents multiple calls to
201 * fill_super(), we can't get dupes here.
202 */
203static int ocfs2_live_connection_new(struct ocfs2_cluster_connection *conn,
204 struct ocfs2_live_connection **c_ret)
205{
206 int rc = 0;
207 struct ocfs2_live_connection *c;
208
209 c = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
210 if (!c)
211 return -ENOMEM;
212
213 mutex_lock(&ocfs2_control_lock);
214 c->oc_conn = conn;
215
216 if (atomic_read(&ocfs2_control_opened))
217 list_add(&c->oc_list, &ocfs2_live_connection_list);
218 else {
219 printk(KERN_ERR
220 "ocfs2: Userspace control daemon is not present\n");
221 rc = -ESRCH;
222 }
223
224 mutex_unlock(&ocfs2_control_lock);
225
226 if (!rc)
227 *c_ret = c;
228 else
229 kfree(c);
230
231 return rc;
232}
233
234/*
235 * This function disconnects the cluster connection from ocfs2_control.
236 * Afterwards, userspace can't affect the cluster connection.
237 */
238static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
239{
240 mutex_lock(&ocfs2_control_lock);
241 list_del_init(&c->oc_list);
242 c->oc_conn = NULL;
243 mutex_unlock(&ocfs2_control_lock);
244
245 kfree(c);
246}
247
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800248static int ocfs2_control_cfu(void *target, size_t target_len,
249 const char __user *buf, size_t count)
Joel Becker462c7e62008-02-18 19:40:12 -0800250{
251 /* The T01 expects write(2) calls to have exactly one command */
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800252 if ((count != target_len) ||
253 (count > sizeof(union ocfs2_control_message)))
Joel Becker462c7e62008-02-18 19:40:12 -0800254 return -EINVAL;
255
256 if (copy_from_user(target, buf, target_len))
257 return -EFAULT;
258
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800259 return 0;
Joel Becker462c7e62008-02-18 19:40:12 -0800260}
261
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800262static ssize_t ocfs2_control_validate_protocol(struct file *file,
263 const char __user *buf,
264 size_t count)
Joel Becker462c7e62008-02-18 19:40:12 -0800265{
266 ssize_t ret;
267 char kbuf[OCFS2_CONTROL_PROTO_LEN];
268
269 ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
270 buf, count);
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800271 if (ret)
Joel Becker462c7e62008-02-18 19:40:12 -0800272 return ret;
273
274 if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
275 return -EINVAL;
276
Joel Becker462c7e62008-02-18 19:40:12 -0800277 ocfs2_control_set_handshake_state(file,
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800278 OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
Joel Becker462c7e62008-02-18 19:40:12 -0800279
280 return count;
281}
282
Joel Beckerde870ef2008-02-18 17:07:09 -0800283static void ocfs2_control_send_down(const char *uuid,
284 int nodenum)
285{
286 struct ocfs2_live_connection *c;
287
288 mutex_lock(&ocfs2_control_lock);
289
290 c = ocfs2_connection_find(uuid);
291 if (c) {
292 BUG_ON(c->oc_conn == NULL);
293 c->oc_conn->cc_recovery_handler(nodenum,
294 c->oc_conn->cc_recovery_data);
295 }
296
297 mutex_unlock(&ocfs2_control_lock);
298}
299
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800300/*
301 * Called whenever configuration elements are sent to /dev/ocfs2_control.
302 * If all configuration elements are present, try to set the global
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800303 * values. If there is a problem, return an error. Skip any missing
304 * elements, and only bump ocfs2_control_opened when we have all elements
305 * and are successful.
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800306 */
307static int ocfs2_control_install_private(struct file *file)
Joel Beckerde870ef2008-02-18 17:07:09 -0800308{
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800309 int rc = 0;
310 int set_p = 1;
311 struct ocfs2_control_private *p = file->private_data;
312
313 BUG_ON(p->op_state != OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
314
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800315 mutex_lock(&ocfs2_control_lock);
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800316
317 if (p->op_this_node < 0) {
318 set_p = 0;
319 } else if ((ocfs2_control_this_node >= 0) &&
320 (ocfs2_control_this_node != p->op_this_node)) {
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800321 rc = -EINVAL;
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800322 goto out_unlock;
323 }
324
325 if (!p->op_proto.pv_major) {
326 set_p = 0;
327 } else if (!list_empty(&ocfs2_live_connection_list) &&
328 ((running_proto.pv_major != p->op_proto.pv_major) ||
329 (running_proto.pv_minor != p->op_proto.pv_minor))) {
330 rc = -EINVAL;
331 goto out_unlock;
332 }
333
334 if (set_p) {
335 ocfs2_control_this_node = p->op_this_node;
336 running_proto.pv_major = p->op_proto.pv_major;
337 running_proto.pv_minor = p->op_proto.pv_minor;
338 }
339
340out_unlock:
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800341 mutex_unlock(&ocfs2_control_lock);
342
343 if (!rc && set_p) {
344 /* We set the global values successfully */
345 atomic_inc(&ocfs2_control_opened);
346 ocfs2_control_set_handshake_state(file,
347 OCFS2_CONTROL_HANDSHAKE_VALID);
348 }
349
350 return rc;
351}
352
David Teiglandcf4d8d72008-02-20 14:29:27 -0800353static int ocfs2_control_get_this_node(void)
354{
355 int rc;
356
357 mutex_lock(&ocfs2_control_lock);
358 if (ocfs2_control_this_node < 0)
359 rc = -EINVAL;
360 else
361 rc = ocfs2_control_this_node;
362 mutex_unlock(&ocfs2_control_lock);
363
364 return rc;
365}
366
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800367static int ocfs2_control_do_setnode_msg(struct file *file,
368 struct ocfs2_control_message_setn *msg)
369{
Joel Beckerde870ef2008-02-18 17:07:09 -0800370 long nodenum;
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800371 char *ptr = NULL;
372 struct ocfs2_control_private *p = file->private_data;
Joel Beckerde870ef2008-02-18 17:07:09 -0800373
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800374 if (ocfs2_control_get_handshake_state(file) !=
375 OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
Joel Beckerde870ef2008-02-18 17:07:09 -0800376 return -EINVAL;
377
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800378 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
379 OCFS2_CONTROL_MESSAGE_OP_LEN))
Joel Beckerde870ef2008-02-18 17:07:09 -0800380 return -EINVAL;
Joel Beckerde870ef2008-02-18 17:07:09 -0800381
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800382 if ((msg->space != ' ') || (msg->newline != '\n'))
383 return -EINVAL;
384 msg->space = msg->newline = '\0';
385
386 nodenum = simple_strtol(msg->nodestr, &ptr, 16);
387 if (!ptr || *ptr)
388 return -EINVAL;
389
390 if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
391 (nodenum > INT_MAX) || (nodenum < 0))
392 return -ERANGE;
393 p->op_this_node = nodenum;
394
395 return ocfs2_control_install_private(file);
396}
397
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800398static int ocfs2_control_do_setversion_msg(struct file *file,
399 struct ocfs2_control_message_setv *msg)
400 {
401 long major, minor;
402 char *ptr = NULL;
403 struct ocfs2_control_private *p = file->private_data;
404 struct ocfs2_protocol_version *max =
Joel Beckere603cfb2010-01-29 16:06:29 -0800405 &ocfs2_user_plugin.sp_max_proto;
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800406
407 if (ocfs2_control_get_handshake_state(file) !=
408 OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
409 return -EINVAL;
410
411 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
412 OCFS2_CONTROL_MESSAGE_OP_LEN))
413 return -EINVAL;
414
415 if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
416 (msg->newline != '\n'))
417 return -EINVAL;
418 msg->space1 = msg->space2 = msg->newline = '\0';
419
420 major = simple_strtol(msg->major, &ptr, 16);
421 if (!ptr || *ptr)
422 return -EINVAL;
423 minor = simple_strtol(msg->minor, &ptr, 16);
424 if (!ptr || *ptr)
425 return -EINVAL;
426
427 /*
428 * The major must be between 1 and 255, inclusive. The minor
429 * must be between 0 and 255, inclusive. The version passed in
430 * must be within the maximum version supported by the filesystem.
431 */
432 if ((major == LONG_MIN) || (major == LONG_MAX) ||
433 (major > (u8)-1) || (major < 1))
434 return -ERANGE;
435 if ((minor == LONG_MIN) || (minor == LONG_MAX) ||
436 (minor > (u8)-1) || (minor < 0))
437 return -ERANGE;
438 if ((major != max->pv_major) ||
439 (minor > max->pv_minor))
440 return -EINVAL;
441
442 p->op_proto.pv_major = major;
443 p->op_proto.pv_minor = minor;
444
445 return ocfs2_control_install_private(file);
446}
447
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800448static int ocfs2_control_do_down_msg(struct file *file,
449 struct ocfs2_control_message_down *msg)
450{
451 long nodenum;
452 char *p = NULL;
453
454 if (ocfs2_control_get_handshake_state(file) !=
455 OCFS2_CONTROL_HANDSHAKE_VALID)
456 return -EINVAL;
457
458 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
459 OCFS2_CONTROL_MESSAGE_OP_LEN))
460 return -EINVAL;
461
462 if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
463 (msg->newline != '\n'))
464 return -EINVAL;
465 msg->space1 = msg->space2 = msg->newline = '\0';
466
467 nodenum = simple_strtol(msg->nodestr, &p, 16);
Joel Beckerde870ef2008-02-18 17:07:09 -0800468 if (!p || *p)
469 return -EINVAL;
470
471 if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
472 (nodenum > INT_MAX) || (nodenum < 0))
473 return -ERANGE;
474
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800475 ocfs2_control_send_down(msg->uuid, nodenum);
Joel Beckerde870ef2008-02-18 17:07:09 -0800476
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800477 return 0;
478}
479
480static ssize_t ocfs2_control_message(struct file *file,
481 const char __user *buf,
482 size_t count)
483{
484 ssize_t ret;
485 union ocfs2_control_message msg;
486
487 /* Try to catch padding issues */
488 WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) !=
489 (sizeof(msg.u_down.tag) + sizeof(msg.u_down.space1)));
490
491 memset(&msg, 0, sizeof(union ocfs2_control_message));
492 ret = ocfs2_control_cfu(&msg, count, buf, count);
493 if (ret)
494 goto out;
495
496 if ((count == OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN) &&
497 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
498 OCFS2_CONTROL_MESSAGE_OP_LEN))
499 ret = ocfs2_control_do_setnode_msg(file, &msg.u_setn);
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800500 else if ((count == OCFS2_CONTROL_MESSAGE_SETVERSION_TOTAL_LEN) &&
501 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
502 OCFS2_CONTROL_MESSAGE_OP_LEN))
503 ret = ocfs2_control_do_setversion_msg(file, &msg.u_setv);
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800504 else if ((count == OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN) &&
505 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
506 OCFS2_CONTROL_MESSAGE_OP_LEN))
507 ret = ocfs2_control_do_down_msg(file, &msg.u_down);
508 else
509 ret = -EINVAL;
510
511out:
512 return ret ? ret : count;
Joel Beckerde870ef2008-02-18 17:07:09 -0800513}
Joel Becker6427a722008-02-18 19:23:28 -0800514
515static ssize_t ocfs2_control_write(struct file *file,
516 const char __user *buf,
517 size_t count,
518 loff_t *ppos)
Joel Becker8adf0532007-11-28 14:38:40 -0800519{
Joel Becker462c7e62008-02-18 19:40:12 -0800520 ssize_t ret;
521
522 switch (ocfs2_control_get_handshake_state(file)) {
523 case OCFS2_CONTROL_HANDSHAKE_INVALID:
524 ret = -EINVAL;
525 break;
526
527 case OCFS2_CONTROL_HANDSHAKE_READ:
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800528 ret = ocfs2_control_validate_protocol(file, buf,
529 count);
Joel Becker462c7e62008-02-18 19:40:12 -0800530 break;
531
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800532 case OCFS2_CONTROL_HANDSHAKE_PROTOCOL:
Joel Becker462c7e62008-02-18 19:40:12 -0800533 case OCFS2_CONTROL_HANDSHAKE_VALID:
Joel Beckerde870ef2008-02-18 17:07:09 -0800534 ret = ocfs2_control_message(file, buf, count);
Joel Becker462c7e62008-02-18 19:40:12 -0800535 break;
536
537 default:
538 BUG();
539 ret = -EIO;
540 break;
541 }
542
543 return ret;
Joel Becker8adf0532007-11-28 14:38:40 -0800544}
545
Joel Becker462c7e62008-02-18 19:40:12 -0800546/*
547 * This is a naive version. If we ever have a new protocol, we'll expand
548 * it. Probably using seq_file.
549 */
Joel Becker6427a722008-02-18 19:23:28 -0800550static ssize_t ocfs2_control_read(struct file *file,
551 char __user *buf,
552 size_t count,
553 loff_t *ppos)
554{
Akinobu Mita7600c722008-06-09 16:34:23 -0700555 ssize_t ret;
Joel Becker462c7e62008-02-18 19:40:12 -0800556
Akinobu Mita7600c722008-06-09 16:34:23 -0700557 ret = simple_read_from_buffer(buf, count, ppos,
558 OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN);
Joel Becker462c7e62008-02-18 19:40:12 -0800559
560 /* Have we read the whole protocol list? */
Akinobu Mita7600c722008-06-09 16:34:23 -0700561 if (ret > 0 && *ppos >= OCFS2_CONTROL_PROTO_LEN)
Joel Becker462c7e62008-02-18 19:40:12 -0800562 ocfs2_control_set_handshake_state(file,
563 OCFS2_CONTROL_HANDSHAKE_READ);
564
Akinobu Mita7600c722008-06-09 16:34:23 -0700565 return ret;
Joel Becker6427a722008-02-18 19:23:28 -0800566}
567
568static int ocfs2_control_release(struct inode *inode, struct file *file)
569{
Joel Becker462c7e62008-02-18 19:40:12 -0800570 struct ocfs2_control_private *p = file->private_data;
571
572 mutex_lock(&ocfs2_control_lock);
573
574 if (ocfs2_control_get_handshake_state(file) !=
575 OCFS2_CONTROL_HANDSHAKE_VALID)
576 goto out;
577
Joel Becker6427a722008-02-18 19:23:28 -0800578 if (atomic_dec_and_test(&ocfs2_control_opened)) {
Joel Becker6427a722008-02-18 19:23:28 -0800579 if (!list_empty(&ocfs2_live_connection_list)) {
580 /* XXX: Do bad things! */
581 printk(KERN_ERR
582 "ocfs2: Unexpected release of ocfs2_control!\n"
583 " Loss of cluster connection requires "
584 "an emergency restart!\n");
585 emergency_restart();
586 }
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800587 /*
588 * Last valid close clears the node number and resets
589 * the locking protocol version
590 */
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800591 ocfs2_control_this_node = -1;
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800592 running_proto.pv_major = 0;
593 running_proto.pv_major = 0;
Joel Becker6427a722008-02-18 19:23:28 -0800594 }
595
Joel Becker462c7e62008-02-18 19:40:12 -0800596out:
597 list_del_init(&p->op_list);
598 file->private_data = NULL;
599
600 mutex_unlock(&ocfs2_control_lock);
601
602 kfree(p);
603
Joel Becker6427a722008-02-18 19:23:28 -0800604 return 0;
605}
606
607static int ocfs2_control_open(struct inode *inode, struct file *file)
608{
Joel Becker462c7e62008-02-18 19:40:12 -0800609 struct ocfs2_control_private *p;
610
611 p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
612 if (!p)
613 return -ENOMEM;
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800614 p->op_this_node = -1;
Joel Becker462c7e62008-02-18 19:40:12 -0800615
616 mutex_lock(&ocfs2_control_lock);
617 file->private_data = p;
618 list_add(&p->op_list, &ocfs2_control_private_list);
619 mutex_unlock(&ocfs2_control_lock);
Joel Becker6427a722008-02-18 19:23:28 -0800620
621 return 0;
622}
623
624static const struct file_operations ocfs2_control_fops = {
625 .open = ocfs2_control_open,
626 .release = ocfs2_control_release,
627 .read = ocfs2_control_read,
628 .write = ocfs2_control_write,
629 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200630 .llseek = default_llseek,
Joel Becker6427a722008-02-18 19:23:28 -0800631};
632
Adrian Bunk4d8755b2008-04-21 11:49:26 +0300633static struct miscdevice ocfs2_control_device = {
Joel Becker6427a722008-02-18 19:23:28 -0800634 .minor = MISC_DYNAMIC_MINOR,
635 .name = "ocfs2_control",
636 .fops = &ocfs2_control_fops,
637};
638
639static int ocfs2_control_init(void)
640{
641 int rc;
642
643 atomic_set(&ocfs2_control_opened, 0);
644
645 rc = misc_register(&ocfs2_control_device);
646 if (rc)
647 printk(KERN_ERR
648 "ocfs2: Unable to register ocfs2_control device "
649 "(errno %d)\n",
650 -rc);
651
652 return rc;
653}
654
655static void ocfs2_control_exit(void)
656{
657 int rc;
658
659 rc = misc_deregister(&ocfs2_control_device);
660 if (rc)
661 printk(KERN_ERR
662 "ocfs2: Unable to deregister ocfs2_control device "
663 "(errno %d)\n",
664 -rc);
665}
666
David Teiglandcf4d8d72008-02-20 14:29:27 -0800667static void fsdlm_lock_ast_wrapper(void *astarg)
668{
Joel Beckerc0e41332010-01-29 14:46:44 -0800669 struct ocfs2_dlm_lksb *lksb = astarg;
Joel Beckera796d282010-01-28 19:22:39 -0800670 int status = lksb->lksb_fsdlm.sb_status;
David Teiglandcf4d8d72008-02-20 14:29:27 -0800671
David Teiglandcf4d8d72008-02-20 14:29:27 -0800672 /*
673 * For now we're punting on the issue of other non-standard errors
674 * where we can't tell if the unlock_ast or lock_ast should be called.
675 * The main "other error" that's possible is EINVAL which means the
676 * function was called with invalid args, which shouldn't be possible
677 * since the caller here is under our control. Other non-standard
678 * errors probably fall into the same category, or otherwise are fatal
679 * which means we can't carry on anyway.
680 */
681
682 if (status == -DLM_EUNLOCK || status == -DLM_ECANCEL)
Joel Becker110946c2010-01-29 15:46:23 -0800683 lksb->lksb_conn->cc_proto->lp_unlock_ast(lksb, 0);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800684 else
Joel Becker110946c2010-01-29 15:46:23 -0800685 lksb->lksb_conn->cc_proto->lp_lock_ast(lksb);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800686}
687
688static void fsdlm_blocking_ast_wrapper(void *astarg, int level)
689{
Joel Beckerc0e41332010-01-29 14:46:44 -0800690 struct ocfs2_dlm_lksb *lksb = astarg;
Joel Beckera796d282010-01-28 19:22:39 -0800691
Joel Becker110946c2010-01-29 15:46:23 -0800692 lksb->lksb_conn->cc_proto->lp_blocking_ast(lksb, level);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800693}
694
695static int user_dlm_lock(struct ocfs2_cluster_connection *conn,
696 int mode,
Joel Beckerc0e41332010-01-29 14:46:44 -0800697 struct ocfs2_dlm_lksb *lksb,
David Teiglandcf4d8d72008-02-20 14:29:27 -0800698 u32 flags,
699 void *name,
Joel Beckera796d282010-01-28 19:22:39 -0800700 unsigned int namelen)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800701{
702 int ret;
703
704 if (!lksb->lksb_fsdlm.sb_lvbptr)
705 lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
706 sizeof(struct dlm_lksb);
707
708 ret = dlm_lock(conn->cc_lockspace, mode, &lksb->lksb_fsdlm,
709 flags|DLM_LKF_NODLCKWT, name, namelen, 0,
Joel Beckera796d282010-01-28 19:22:39 -0800710 fsdlm_lock_ast_wrapper, lksb,
David Teiglandcf4d8d72008-02-20 14:29:27 -0800711 fsdlm_blocking_ast_wrapper);
712 return ret;
713}
714
715static int user_dlm_unlock(struct ocfs2_cluster_connection *conn,
Joel Beckerc0e41332010-01-29 14:46:44 -0800716 struct ocfs2_dlm_lksb *lksb,
Joel Beckera796d282010-01-28 19:22:39 -0800717 u32 flags)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800718{
719 int ret;
720
721 ret = dlm_unlock(conn->cc_lockspace, lksb->lksb_fsdlm.sb_lkid,
Joel Beckera796d282010-01-28 19:22:39 -0800722 flags, &lksb->lksb_fsdlm, lksb);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800723 return ret;
724}
725
Joel Beckerc0e41332010-01-29 14:46:44 -0800726static int user_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800727{
728 return lksb->lksb_fsdlm.sb_status;
729}
730
Joel Beckerc0e41332010-01-29 14:46:44 -0800731static int user_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
Joel Becker1c520df2009-06-19 15:14:13 -0700732{
733 int invalid = lksb->lksb_fsdlm.sb_flags & DLM_SBF_VALNOTVALID;
734
735 return !invalid;
736}
737
Joel Beckerc0e41332010-01-29 14:46:44 -0800738static void *user_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800739{
David Teigland66f502a2008-11-10 16:24:57 -0600740 if (!lksb->lksb_fsdlm.sb_lvbptr)
741 lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
742 sizeof(struct dlm_lksb);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800743 return (void *)(lksb->lksb_fsdlm.sb_lvbptr);
744}
745
Joel Beckerc0e41332010-01-29 14:46:44 -0800746static void user_dlm_dump_lksb(struct ocfs2_dlm_lksb *lksb)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800747{
748}
749
Mark Fasheh53da4932008-07-21 14:29:16 -0700750static int user_plock(struct ocfs2_cluster_connection *conn,
751 u64 ino,
752 struct file *file,
753 int cmd,
754 struct file_lock *fl)
755{
756 /*
757 * This more or less just demuxes the plock request into any
758 * one of three dlm calls.
759 *
760 * Internally, fs/dlm will pass these to a misc device, which
761 * a userspace daemon will read and write to.
762 *
763 * For now, cancel requests (which happen internally only),
764 * are turned into unlocks. Most of this function taken from
765 * gfs2_lock.
766 */
767
768 if (cmd == F_CANCELLK) {
769 cmd = F_SETLK;
770 fl->fl_type = F_UNLCK;
771 }
772
773 if (IS_GETLK(cmd))
774 return dlm_posix_get(conn->cc_lockspace, ino, file, fl);
775 else if (fl->fl_type == F_UNLCK)
776 return dlm_posix_unlock(conn->cc_lockspace, ino, file, fl);
777 else
778 return dlm_posix_lock(conn->cc_lockspace, ino, file, cmd, fl);
779}
780
David Teiglandcf4d8d72008-02-20 14:29:27 -0800781/*
782 * Compare a requested locking protocol version against the current one.
783 *
784 * If the major numbers are different, they are incompatible.
785 * If the current minor is greater than the request, they are incompatible.
786 * If the current minor is less than or equal to the request, they are
787 * compatible, and the requester should run at the current minor version.
788 */
789static int fs_protocol_compare(struct ocfs2_protocol_version *existing,
790 struct ocfs2_protocol_version *request)
791{
792 if (existing->pv_major != request->pv_major)
793 return 1;
794
795 if (existing->pv_minor > request->pv_minor)
796 return 1;
797
798 if (existing->pv_minor < request->pv_minor)
799 request->pv_minor = existing->pv_minor;
800
801 return 0;
802}
803
Goldwyn Rodrigues66e188f2014-01-21 15:48:22 -0800804static void user_recover_prep(void *arg)
805{
806}
807
808static void user_recover_slot(void *arg, struct dlm_slot *slot)
809{
810 struct ocfs2_cluster_connection *conn = arg;
811 printk(KERN_INFO "ocfs2: Node %d/%d down. Initiating recovery.\n",
812 slot->nodeid, slot->slot);
813 conn->cc_recovery_handler(slot->nodeid, conn->cc_recovery_data);
814
815}
816
817static void user_recover_done(void *arg, struct dlm_slot *slots,
818 int num_slots, int our_slot,
819 uint32_t generation)
820{
821 struct ocfs2_cluster_connection *conn = arg;
822 struct ocfs2_live_connection *lc = conn->cc_private;
823 int i;
824
825 for (i = 0; i < num_slots; i++)
826 if (slots[i].slot == our_slot) {
827 atomic_set(&lc->oc_this_node, slots[i].nodeid);
828 break;
829 }
830
831 lc->oc_our_slot = our_slot;
832}
833
834const struct dlm_lockspace_ops ocfs2_ls_ops = {
835 .recover_prep = user_recover_prep,
836 .recover_slot = user_recover_slot,
837 .recover_done = user_recover_done,
838};
839
David Teiglandcf4d8d72008-02-20 14:29:27 -0800840static int user_cluster_connect(struct ocfs2_cluster_connection *conn)
841{
842 dlm_lockspace_t *fsdlm;
Coly Li3a05d792009-12-04 02:02:35 +0800843 struct ocfs2_live_connection *uninitialized_var(control);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800844 int rc = 0;
845
846 BUG_ON(conn == NULL);
847
848 rc = ocfs2_live_connection_new(conn, &control);
849 if (rc)
850 goto out;
851
852 /*
853 * running_proto must have been set before we allowed any mounts
854 * to proceed.
855 */
856 if (fs_protocol_compare(&running_proto, &conn->cc_version)) {
857 printk(KERN_ERR
858 "Unable to mount with fs locking protocol version "
859 "%u.%u because the userspace control daemon has "
860 "negotiated %u.%u\n",
861 conn->cc_version.pv_major, conn->cc_version.pv_minor,
862 running_proto.pv_major, running_proto.pv_minor);
863 rc = -EPROTO;
864 ocfs2_live_connection_drop(control);
865 goto out;
866 }
867
David Teigland60f98d12011-11-02 14:30:58 -0500868 rc = dlm_new_lockspace(conn->cc_name, NULL, DLM_LSFL_FS, DLM_LVB_LEN,
869 NULL, NULL, NULL, &fsdlm);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800870 if (rc) {
871 ocfs2_live_connection_drop(control);
872 goto out;
873 }
874
875 conn->cc_private = control;
876 conn->cc_lockspace = fsdlm;
877out:
878 return rc;
879}
880
Joel Becker2c394502008-05-30 15:58:26 -0700881static int user_cluster_disconnect(struct ocfs2_cluster_connection *conn)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800882{
883 dlm_release_lockspace(conn->cc_lockspace, 2);
884 conn->cc_lockspace = NULL;
885 ocfs2_live_connection_drop(conn->cc_private);
886 conn->cc_private = NULL;
887 return 0;
888}
889
890static int user_cluster_this_node(unsigned int *this_node)
891{
892 int rc;
893
894 rc = ocfs2_control_get_this_node();
895 if (rc < 0)
896 return rc;
897
898 *this_node = rc;
899 return 0;
900}
901
Joel Beckera12630b2008-05-09 18:49:29 -0700902static struct ocfs2_stack_operations ocfs2_user_plugin_ops = {
David Teiglandcf4d8d72008-02-20 14:29:27 -0800903 .connect = user_cluster_connect,
904 .disconnect = user_cluster_disconnect,
905 .this_node = user_cluster_this_node,
906 .dlm_lock = user_dlm_lock,
907 .dlm_unlock = user_dlm_unlock,
908 .lock_status = user_dlm_lock_status,
Joel Becker1c520df2009-06-19 15:14:13 -0700909 .lvb_valid = user_dlm_lvb_valid,
David Teiglandcf4d8d72008-02-20 14:29:27 -0800910 .lock_lvb = user_dlm_lvb,
Mark Fasheh53da4932008-07-21 14:29:16 -0700911 .plock = user_plock,
David Teiglandcf4d8d72008-02-20 14:29:27 -0800912 .dump_lksb = user_dlm_dump_lksb,
913};
914
Joel Beckera12630b2008-05-09 18:49:29 -0700915static struct ocfs2_stack_plugin ocfs2_user_plugin = {
David Teiglandcf4d8d72008-02-20 14:29:27 -0800916 .sp_name = "user",
Joel Beckera12630b2008-05-09 18:49:29 -0700917 .sp_ops = &ocfs2_user_plugin_ops,
David Teiglandcf4d8d72008-02-20 14:29:27 -0800918 .sp_owner = THIS_MODULE,
919};
920
921
Joel Beckera12630b2008-05-09 18:49:29 -0700922static int __init ocfs2_user_plugin_init(void)
Joel Becker6427a722008-02-18 19:23:28 -0800923{
David Teiglandcf4d8d72008-02-20 14:29:27 -0800924 int rc;
925
926 rc = ocfs2_control_init();
927 if (!rc) {
Joel Beckera12630b2008-05-09 18:49:29 -0700928 rc = ocfs2_stack_glue_register(&ocfs2_user_plugin);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800929 if (rc)
930 ocfs2_control_exit();
931 }
932
933 return rc;
Joel Becker6427a722008-02-18 19:23:28 -0800934}
935
Joel Beckera12630b2008-05-09 18:49:29 -0700936static void __exit ocfs2_user_plugin_exit(void)
Joel Becker8adf0532007-11-28 14:38:40 -0800937{
Joel Beckera12630b2008-05-09 18:49:29 -0700938 ocfs2_stack_glue_unregister(&ocfs2_user_plugin);
Joel Becker6427a722008-02-18 19:23:28 -0800939 ocfs2_control_exit();
Joel Becker8adf0532007-11-28 14:38:40 -0800940}
941
942MODULE_AUTHOR("Oracle");
943MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
944MODULE_LICENSE("GPL");
Joel Beckera12630b2008-05-09 18:49:29 -0700945module_init(ocfs2_user_plugin_init);
946module_exit(ocfs2_user_plugin_exit);