blob: 0afb86d9b2791a622a8eab74360f2ed58fe27e81 [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 */
Goldwyn Rodrigues24aa3382014-01-21 15:48:23 -0800203static int ocfs2_live_connection_attach(struct ocfs2_cluster_connection *conn,
204 struct ocfs2_live_connection *c)
Joel Becker6427a722008-02-18 19:23:28 -0800205{
206 int rc = 0;
Joel Becker6427a722008-02-18 19:23:28 -0800207
208 mutex_lock(&ocfs2_control_lock);
209 c->oc_conn = conn;
210
211 if (atomic_read(&ocfs2_control_opened))
212 list_add(&c->oc_list, &ocfs2_live_connection_list);
213 else {
214 printk(KERN_ERR
215 "ocfs2: Userspace control daemon is not present\n");
216 rc = -ESRCH;
217 }
218
219 mutex_unlock(&ocfs2_control_lock);
Joel Becker6427a722008-02-18 19:23:28 -0800220 return rc;
221}
222
223/*
224 * This function disconnects the cluster connection from ocfs2_control.
225 * Afterwards, userspace can't affect the cluster connection.
226 */
227static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
228{
229 mutex_lock(&ocfs2_control_lock);
230 list_del_init(&c->oc_list);
231 c->oc_conn = NULL;
232 mutex_unlock(&ocfs2_control_lock);
233
234 kfree(c);
235}
236
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800237static int ocfs2_control_cfu(void *target, size_t target_len,
238 const char __user *buf, size_t count)
Joel Becker462c7e62008-02-18 19:40:12 -0800239{
240 /* The T01 expects write(2) calls to have exactly one command */
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800241 if ((count != target_len) ||
242 (count > sizeof(union ocfs2_control_message)))
Joel Becker462c7e62008-02-18 19:40:12 -0800243 return -EINVAL;
244
245 if (copy_from_user(target, buf, target_len))
246 return -EFAULT;
247
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800248 return 0;
Joel Becker462c7e62008-02-18 19:40:12 -0800249}
250
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800251static ssize_t ocfs2_control_validate_protocol(struct file *file,
252 const char __user *buf,
253 size_t count)
Joel Becker462c7e62008-02-18 19:40:12 -0800254{
255 ssize_t ret;
256 char kbuf[OCFS2_CONTROL_PROTO_LEN];
257
258 ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
259 buf, count);
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800260 if (ret)
Joel Becker462c7e62008-02-18 19:40:12 -0800261 return ret;
262
263 if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
264 return -EINVAL;
265
Joel Becker462c7e62008-02-18 19:40:12 -0800266 ocfs2_control_set_handshake_state(file,
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800267 OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
Joel Becker462c7e62008-02-18 19:40:12 -0800268
269 return count;
270}
271
Joel Beckerde870ef2008-02-18 17:07:09 -0800272static void ocfs2_control_send_down(const char *uuid,
273 int nodenum)
274{
275 struct ocfs2_live_connection *c;
276
277 mutex_lock(&ocfs2_control_lock);
278
279 c = ocfs2_connection_find(uuid);
280 if (c) {
281 BUG_ON(c->oc_conn == NULL);
282 c->oc_conn->cc_recovery_handler(nodenum,
283 c->oc_conn->cc_recovery_data);
284 }
285
286 mutex_unlock(&ocfs2_control_lock);
287}
288
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800289/*
290 * Called whenever configuration elements are sent to /dev/ocfs2_control.
291 * If all configuration elements are present, try to set the global
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800292 * values. If there is a problem, return an error. Skip any missing
293 * elements, and only bump ocfs2_control_opened when we have all elements
294 * and are successful.
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800295 */
296static int ocfs2_control_install_private(struct file *file)
Joel Beckerde870ef2008-02-18 17:07:09 -0800297{
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800298 int rc = 0;
299 int set_p = 1;
300 struct ocfs2_control_private *p = file->private_data;
301
302 BUG_ON(p->op_state != OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
303
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800304 mutex_lock(&ocfs2_control_lock);
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800305
306 if (p->op_this_node < 0) {
307 set_p = 0;
308 } else if ((ocfs2_control_this_node >= 0) &&
309 (ocfs2_control_this_node != p->op_this_node)) {
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800310 rc = -EINVAL;
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800311 goto out_unlock;
312 }
313
314 if (!p->op_proto.pv_major) {
315 set_p = 0;
316 } else if (!list_empty(&ocfs2_live_connection_list) &&
317 ((running_proto.pv_major != p->op_proto.pv_major) ||
318 (running_proto.pv_minor != p->op_proto.pv_minor))) {
319 rc = -EINVAL;
320 goto out_unlock;
321 }
322
323 if (set_p) {
324 ocfs2_control_this_node = p->op_this_node;
325 running_proto.pv_major = p->op_proto.pv_major;
326 running_proto.pv_minor = p->op_proto.pv_minor;
327 }
328
329out_unlock:
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800330 mutex_unlock(&ocfs2_control_lock);
331
332 if (!rc && set_p) {
333 /* We set the global values successfully */
334 atomic_inc(&ocfs2_control_opened);
335 ocfs2_control_set_handshake_state(file,
336 OCFS2_CONTROL_HANDSHAKE_VALID);
337 }
338
339 return rc;
340}
341
David Teiglandcf4d8d72008-02-20 14:29:27 -0800342static int ocfs2_control_get_this_node(void)
343{
344 int rc;
345
346 mutex_lock(&ocfs2_control_lock);
347 if (ocfs2_control_this_node < 0)
348 rc = -EINVAL;
349 else
350 rc = ocfs2_control_this_node;
351 mutex_unlock(&ocfs2_control_lock);
352
353 return rc;
354}
355
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800356static int ocfs2_control_do_setnode_msg(struct file *file,
357 struct ocfs2_control_message_setn *msg)
358{
Joel Beckerde870ef2008-02-18 17:07:09 -0800359 long nodenum;
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800360 char *ptr = NULL;
361 struct ocfs2_control_private *p = file->private_data;
Joel Beckerde870ef2008-02-18 17:07:09 -0800362
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800363 if (ocfs2_control_get_handshake_state(file) !=
364 OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
Joel Beckerde870ef2008-02-18 17:07:09 -0800365 return -EINVAL;
366
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800367 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
368 OCFS2_CONTROL_MESSAGE_OP_LEN))
Joel Beckerde870ef2008-02-18 17:07:09 -0800369 return -EINVAL;
Joel Beckerde870ef2008-02-18 17:07:09 -0800370
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800371 if ((msg->space != ' ') || (msg->newline != '\n'))
372 return -EINVAL;
373 msg->space = msg->newline = '\0';
374
375 nodenum = simple_strtol(msg->nodestr, &ptr, 16);
376 if (!ptr || *ptr)
377 return -EINVAL;
378
379 if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
380 (nodenum > INT_MAX) || (nodenum < 0))
381 return -ERANGE;
382 p->op_this_node = nodenum;
383
384 return ocfs2_control_install_private(file);
385}
386
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800387static int ocfs2_control_do_setversion_msg(struct file *file,
388 struct ocfs2_control_message_setv *msg)
389 {
390 long major, minor;
391 char *ptr = NULL;
392 struct ocfs2_control_private *p = file->private_data;
393 struct ocfs2_protocol_version *max =
Joel Beckere603cfb2010-01-29 16:06:29 -0800394 &ocfs2_user_plugin.sp_max_proto;
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800395
396 if (ocfs2_control_get_handshake_state(file) !=
397 OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
398 return -EINVAL;
399
400 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
401 OCFS2_CONTROL_MESSAGE_OP_LEN))
402 return -EINVAL;
403
404 if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
405 (msg->newline != '\n'))
406 return -EINVAL;
407 msg->space1 = msg->space2 = msg->newline = '\0';
408
409 major = simple_strtol(msg->major, &ptr, 16);
410 if (!ptr || *ptr)
411 return -EINVAL;
412 minor = simple_strtol(msg->minor, &ptr, 16);
413 if (!ptr || *ptr)
414 return -EINVAL;
415
416 /*
417 * The major must be between 1 and 255, inclusive. The minor
418 * must be between 0 and 255, inclusive. The version passed in
419 * must be within the maximum version supported by the filesystem.
420 */
421 if ((major == LONG_MIN) || (major == LONG_MAX) ||
422 (major > (u8)-1) || (major < 1))
423 return -ERANGE;
424 if ((minor == LONG_MIN) || (minor == LONG_MAX) ||
425 (minor > (u8)-1) || (minor < 0))
426 return -ERANGE;
427 if ((major != max->pv_major) ||
428 (minor > max->pv_minor))
429 return -EINVAL;
430
431 p->op_proto.pv_major = major;
432 p->op_proto.pv_minor = minor;
433
434 return ocfs2_control_install_private(file);
435}
436
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800437static int ocfs2_control_do_down_msg(struct file *file,
438 struct ocfs2_control_message_down *msg)
439{
440 long nodenum;
441 char *p = NULL;
442
443 if (ocfs2_control_get_handshake_state(file) !=
444 OCFS2_CONTROL_HANDSHAKE_VALID)
445 return -EINVAL;
446
447 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
448 OCFS2_CONTROL_MESSAGE_OP_LEN))
449 return -EINVAL;
450
451 if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
452 (msg->newline != '\n'))
453 return -EINVAL;
454 msg->space1 = msg->space2 = msg->newline = '\0';
455
456 nodenum = simple_strtol(msg->nodestr, &p, 16);
Joel Beckerde870ef2008-02-18 17:07:09 -0800457 if (!p || *p)
458 return -EINVAL;
459
460 if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
461 (nodenum > INT_MAX) || (nodenum < 0))
462 return -ERANGE;
463
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800464 ocfs2_control_send_down(msg->uuid, nodenum);
Joel Beckerde870ef2008-02-18 17:07:09 -0800465
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800466 return 0;
467}
468
469static ssize_t ocfs2_control_message(struct file *file,
470 const char __user *buf,
471 size_t count)
472{
473 ssize_t ret;
474 union ocfs2_control_message msg;
475
476 /* Try to catch padding issues */
477 WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) !=
478 (sizeof(msg.u_down.tag) + sizeof(msg.u_down.space1)));
479
480 memset(&msg, 0, sizeof(union ocfs2_control_message));
481 ret = ocfs2_control_cfu(&msg, count, buf, count);
482 if (ret)
483 goto out;
484
485 if ((count == OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN) &&
486 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
487 OCFS2_CONTROL_MESSAGE_OP_LEN))
488 ret = ocfs2_control_do_setnode_msg(file, &msg.u_setn);
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800489 else if ((count == OCFS2_CONTROL_MESSAGE_SETVERSION_TOTAL_LEN) &&
490 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
491 OCFS2_CONTROL_MESSAGE_OP_LEN))
492 ret = ocfs2_control_do_setversion_msg(file, &msg.u_setv);
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800493 else if ((count == OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN) &&
494 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
495 OCFS2_CONTROL_MESSAGE_OP_LEN))
496 ret = ocfs2_control_do_down_msg(file, &msg.u_down);
497 else
498 ret = -EINVAL;
499
500out:
501 return ret ? ret : count;
Joel Beckerde870ef2008-02-18 17:07:09 -0800502}
Joel Becker6427a722008-02-18 19:23:28 -0800503
504static ssize_t ocfs2_control_write(struct file *file,
505 const char __user *buf,
506 size_t count,
507 loff_t *ppos)
Joel Becker8adf0532007-11-28 14:38:40 -0800508{
Joel Becker462c7e62008-02-18 19:40:12 -0800509 ssize_t ret;
510
511 switch (ocfs2_control_get_handshake_state(file)) {
512 case OCFS2_CONTROL_HANDSHAKE_INVALID:
513 ret = -EINVAL;
514 break;
515
516 case OCFS2_CONTROL_HANDSHAKE_READ:
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800517 ret = ocfs2_control_validate_protocol(file, buf,
518 count);
Joel Becker462c7e62008-02-18 19:40:12 -0800519 break;
520
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800521 case OCFS2_CONTROL_HANDSHAKE_PROTOCOL:
Joel Becker462c7e62008-02-18 19:40:12 -0800522 case OCFS2_CONTROL_HANDSHAKE_VALID:
Joel Beckerde870ef2008-02-18 17:07:09 -0800523 ret = ocfs2_control_message(file, buf, count);
Joel Becker462c7e62008-02-18 19:40:12 -0800524 break;
525
526 default:
527 BUG();
528 ret = -EIO;
529 break;
530 }
531
532 return ret;
Joel Becker8adf0532007-11-28 14:38:40 -0800533}
534
Joel Becker462c7e62008-02-18 19:40:12 -0800535/*
536 * This is a naive version. If we ever have a new protocol, we'll expand
537 * it. Probably using seq_file.
538 */
Joel Becker6427a722008-02-18 19:23:28 -0800539static ssize_t ocfs2_control_read(struct file *file,
540 char __user *buf,
541 size_t count,
542 loff_t *ppos)
543{
Akinobu Mita7600c722008-06-09 16:34:23 -0700544 ssize_t ret;
Joel Becker462c7e62008-02-18 19:40:12 -0800545
Akinobu Mita7600c722008-06-09 16:34:23 -0700546 ret = simple_read_from_buffer(buf, count, ppos,
547 OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN);
Joel Becker462c7e62008-02-18 19:40:12 -0800548
549 /* Have we read the whole protocol list? */
Akinobu Mita7600c722008-06-09 16:34:23 -0700550 if (ret > 0 && *ppos >= OCFS2_CONTROL_PROTO_LEN)
Joel Becker462c7e62008-02-18 19:40:12 -0800551 ocfs2_control_set_handshake_state(file,
552 OCFS2_CONTROL_HANDSHAKE_READ);
553
Akinobu Mita7600c722008-06-09 16:34:23 -0700554 return ret;
Joel Becker6427a722008-02-18 19:23:28 -0800555}
556
557static int ocfs2_control_release(struct inode *inode, struct file *file)
558{
Joel Becker462c7e62008-02-18 19:40:12 -0800559 struct ocfs2_control_private *p = file->private_data;
560
561 mutex_lock(&ocfs2_control_lock);
562
563 if (ocfs2_control_get_handshake_state(file) !=
564 OCFS2_CONTROL_HANDSHAKE_VALID)
565 goto out;
566
Joel Becker6427a722008-02-18 19:23:28 -0800567 if (atomic_dec_and_test(&ocfs2_control_opened)) {
Joel Becker6427a722008-02-18 19:23:28 -0800568 if (!list_empty(&ocfs2_live_connection_list)) {
569 /* XXX: Do bad things! */
570 printk(KERN_ERR
571 "ocfs2: Unexpected release of ocfs2_control!\n"
572 " Loss of cluster connection requires "
573 "an emergency restart!\n");
574 emergency_restart();
575 }
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800576 /*
577 * Last valid close clears the node number and resets
578 * the locking protocol version
579 */
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800580 ocfs2_control_this_node = -1;
Joel Beckerd4b95ee2008-02-20 15:39:44 -0800581 running_proto.pv_major = 0;
582 running_proto.pv_major = 0;
Joel Becker6427a722008-02-18 19:23:28 -0800583 }
584
Joel Becker462c7e62008-02-18 19:40:12 -0800585out:
586 list_del_init(&p->op_list);
587 file->private_data = NULL;
588
589 mutex_unlock(&ocfs2_control_lock);
590
591 kfree(p);
592
Joel Becker6427a722008-02-18 19:23:28 -0800593 return 0;
594}
595
596static int ocfs2_control_open(struct inode *inode, struct file *file)
597{
Joel Becker462c7e62008-02-18 19:40:12 -0800598 struct ocfs2_control_private *p;
599
600 p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
601 if (!p)
602 return -ENOMEM;
Joel Becker3cfd4ab2008-02-20 14:44:34 -0800603 p->op_this_node = -1;
Joel Becker462c7e62008-02-18 19:40:12 -0800604
605 mutex_lock(&ocfs2_control_lock);
606 file->private_data = p;
607 list_add(&p->op_list, &ocfs2_control_private_list);
608 mutex_unlock(&ocfs2_control_lock);
Joel Becker6427a722008-02-18 19:23:28 -0800609
610 return 0;
611}
612
613static const struct file_operations ocfs2_control_fops = {
614 .open = ocfs2_control_open,
615 .release = ocfs2_control_release,
616 .read = ocfs2_control_read,
617 .write = ocfs2_control_write,
618 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200619 .llseek = default_llseek,
Joel Becker6427a722008-02-18 19:23:28 -0800620};
621
Adrian Bunk4d8755b2008-04-21 11:49:26 +0300622static struct miscdevice ocfs2_control_device = {
Joel Becker6427a722008-02-18 19:23:28 -0800623 .minor = MISC_DYNAMIC_MINOR,
624 .name = "ocfs2_control",
625 .fops = &ocfs2_control_fops,
626};
627
628static int ocfs2_control_init(void)
629{
630 int rc;
631
632 atomic_set(&ocfs2_control_opened, 0);
633
634 rc = misc_register(&ocfs2_control_device);
635 if (rc)
636 printk(KERN_ERR
637 "ocfs2: Unable to register ocfs2_control device "
638 "(errno %d)\n",
639 -rc);
640
641 return rc;
642}
643
644static void ocfs2_control_exit(void)
645{
646 int rc;
647
648 rc = misc_deregister(&ocfs2_control_device);
649 if (rc)
650 printk(KERN_ERR
651 "ocfs2: Unable to deregister ocfs2_control device "
652 "(errno %d)\n",
653 -rc);
654}
655
David Teiglandcf4d8d72008-02-20 14:29:27 -0800656static void fsdlm_lock_ast_wrapper(void *astarg)
657{
Joel Beckerc0e41332010-01-29 14:46:44 -0800658 struct ocfs2_dlm_lksb *lksb = astarg;
Joel Beckera796d282010-01-28 19:22:39 -0800659 int status = lksb->lksb_fsdlm.sb_status;
David Teiglandcf4d8d72008-02-20 14:29:27 -0800660
David Teiglandcf4d8d72008-02-20 14:29:27 -0800661 /*
662 * For now we're punting on the issue of other non-standard errors
663 * where we can't tell if the unlock_ast or lock_ast should be called.
664 * The main "other error" that's possible is EINVAL which means the
665 * function was called with invalid args, which shouldn't be possible
666 * since the caller here is under our control. Other non-standard
667 * errors probably fall into the same category, or otherwise are fatal
668 * which means we can't carry on anyway.
669 */
670
671 if (status == -DLM_EUNLOCK || status == -DLM_ECANCEL)
Joel Becker110946c2010-01-29 15:46:23 -0800672 lksb->lksb_conn->cc_proto->lp_unlock_ast(lksb, 0);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800673 else
Joel Becker110946c2010-01-29 15:46:23 -0800674 lksb->lksb_conn->cc_proto->lp_lock_ast(lksb);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800675}
676
677static void fsdlm_blocking_ast_wrapper(void *astarg, int level)
678{
Joel Beckerc0e41332010-01-29 14:46:44 -0800679 struct ocfs2_dlm_lksb *lksb = astarg;
Joel Beckera796d282010-01-28 19:22:39 -0800680
Joel Becker110946c2010-01-29 15:46:23 -0800681 lksb->lksb_conn->cc_proto->lp_blocking_ast(lksb, level);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800682}
683
684static int user_dlm_lock(struct ocfs2_cluster_connection *conn,
685 int mode,
Joel Beckerc0e41332010-01-29 14:46:44 -0800686 struct ocfs2_dlm_lksb *lksb,
David Teiglandcf4d8d72008-02-20 14:29:27 -0800687 u32 flags,
688 void *name,
Joel Beckera796d282010-01-28 19:22:39 -0800689 unsigned int namelen)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800690{
691 int ret;
692
693 if (!lksb->lksb_fsdlm.sb_lvbptr)
694 lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
695 sizeof(struct dlm_lksb);
696
697 ret = dlm_lock(conn->cc_lockspace, mode, &lksb->lksb_fsdlm,
698 flags|DLM_LKF_NODLCKWT, name, namelen, 0,
Joel Beckera796d282010-01-28 19:22:39 -0800699 fsdlm_lock_ast_wrapper, lksb,
David Teiglandcf4d8d72008-02-20 14:29:27 -0800700 fsdlm_blocking_ast_wrapper);
701 return ret;
702}
703
704static int user_dlm_unlock(struct ocfs2_cluster_connection *conn,
Joel Beckerc0e41332010-01-29 14:46:44 -0800705 struct ocfs2_dlm_lksb *lksb,
Joel Beckera796d282010-01-28 19:22:39 -0800706 u32 flags)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800707{
708 int ret;
709
710 ret = dlm_unlock(conn->cc_lockspace, lksb->lksb_fsdlm.sb_lkid,
Joel Beckera796d282010-01-28 19:22:39 -0800711 flags, &lksb->lksb_fsdlm, lksb);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800712 return ret;
713}
714
Joel Beckerc0e41332010-01-29 14:46:44 -0800715static int user_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800716{
717 return lksb->lksb_fsdlm.sb_status;
718}
719
Joel Beckerc0e41332010-01-29 14:46:44 -0800720static int user_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
Joel Becker1c520df2009-06-19 15:14:13 -0700721{
722 int invalid = lksb->lksb_fsdlm.sb_flags & DLM_SBF_VALNOTVALID;
723
724 return !invalid;
725}
726
Joel Beckerc0e41332010-01-29 14:46:44 -0800727static void *user_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800728{
David Teigland66f502a2008-11-10 16:24:57 -0600729 if (!lksb->lksb_fsdlm.sb_lvbptr)
730 lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
731 sizeof(struct dlm_lksb);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800732 return (void *)(lksb->lksb_fsdlm.sb_lvbptr);
733}
734
Joel Beckerc0e41332010-01-29 14:46:44 -0800735static void user_dlm_dump_lksb(struct ocfs2_dlm_lksb *lksb)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800736{
737}
738
Mark Fasheh53da4932008-07-21 14:29:16 -0700739static int user_plock(struct ocfs2_cluster_connection *conn,
740 u64 ino,
741 struct file *file,
742 int cmd,
743 struct file_lock *fl)
744{
745 /*
746 * This more or less just demuxes the plock request into any
747 * one of three dlm calls.
748 *
749 * Internally, fs/dlm will pass these to a misc device, which
750 * a userspace daemon will read and write to.
751 *
752 * For now, cancel requests (which happen internally only),
753 * are turned into unlocks. Most of this function taken from
754 * gfs2_lock.
755 */
756
757 if (cmd == F_CANCELLK) {
758 cmd = F_SETLK;
759 fl->fl_type = F_UNLCK;
760 }
761
762 if (IS_GETLK(cmd))
763 return dlm_posix_get(conn->cc_lockspace, ino, file, fl);
764 else if (fl->fl_type == F_UNLCK)
765 return dlm_posix_unlock(conn->cc_lockspace, ino, file, fl);
766 else
767 return dlm_posix_lock(conn->cc_lockspace, ino, file, cmd, fl);
768}
769
David Teiglandcf4d8d72008-02-20 14:29:27 -0800770/*
771 * Compare a requested locking protocol version against the current one.
772 *
773 * If the major numbers are different, they are incompatible.
774 * If the current minor is greater than the request, they are incompatible.
775 * If the current minor is less than or equal to the request, they are
776 * compatible, and the requester should run at the current minor version.
777 */
778static int fs_protocol_compare(struct ocfs2_protocol_version *existing,
779 struct ocfs2_protocol_version *request)
780{
781 if (existing->pv_major != request->pv_major)
782 return 1;
783
784 if (existing->pv_minor > request->pv_minor)
785 return 1;
786
787 if (existing->pv_minor < request->pv_minor)
788 request->pv_minor = existing->pv_minor;
789
790 return 0;
791}
792
Goldwyn Rodrigues66e188f2014-01-21 15:48:22 -0800793static void user_recover_prep(void *arg)
794{
795}
796
797static void user_recover_slot(void *arg, struct dlm_slot *slot)
798{
799 struct ocfs2_cluster_connection *conn = arg;
800 printk(KERN_INFO "ocfs2: Node %d/%d down. Initiating recovery.\n",
801 slot->nodeid, slot->slot);
802 conn->cc_recovery_handler(slot->nodeid, conn->cc_recovery_data);
803
804}
805
806static void user_recover_done(void *arg, struct dlm_slot *slots,
807 int num_slots, int our_slot,
808 uint32_t generation)
809{
810 struct ocfs2_cluster_connection *conn = arg;
811 struct ocfs2_live_connection *lc = conn->cc_private;
812 int i;
813
814 for (i = 0; i < num_slots; i++)
815 if (slots[i].slot == our_slot) {
816 atomic_set(&lc->oc_this_node, slots[i].nodeid);
817 break;
818 }
819
820 lc->oc_our_slot = our_slot;
821}
822
823const struct dlm_lockspace_ops ocfs2_ls_ops = {
824 .recover_prep = user_recover_prep,
825 .recover_slot = user_recover_slot,
826 .recover_done = user_recover_done,
827};
828
David Teiglandcf4d8d72008-02-20 14:29:27 -0800829static int user_cluster_connect(struct ocfs2_cluster_connection *conn)
830{
831 dlm_lockspace_t *fsdlm;
Goldwyn Rodrigues24aa3382014-01-21 15:48:23 -0800832 struct ocfs2_live_connection *lc;
833 int rc;
David Teiglandcf4d8d72008-02-20 14:29:27 -0800834
835 BUG_ON(conn == NULL);
836
Goldwyn Rodrigues24aa3382014-01-21 15:48:23 -0800837 lc = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
838 if (!lc) {
839 rc = -ENOMEM;
840 goto out;
841 }
842
843 rc = ocfs2_live_connection_attach(conn, lc);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800844 if (rc)
845 goto out;
846
847 /*
848 * running_proto must have been set before we allowed any mounts
849 * to proceed.
850 */
851 if (fs_protocol_compare(&running_proto, &conn->cc_version)) {
852 printk(KERN_ERR
853 "Unable to mount with fs locking protocol version "
854 "%u.%u because the userspace control daemon has "
855 "negotiated %u.%u\n",
856 conn->cc_version.pv_major, conn->cc_version.pv_minor,
857 running_proto.pv_major, running_proto.pv_minor);
858 rc = -EPROTO;
Goldwyn Rodrigues24aa3382014-01-21 15:48:23 -0800859 ocfs2_live_connection_drop(lc);
860 lc = NULL;
David Teiglandcf4d8d72008-02-20 14:29:27 -0800861 goto out;
862 }
863
David Teigland60f98d12011-11-02 14:30:58 -0500864 rc = dlm_new_lockspace(conn->cc_name, NULL, DLM_LSFL_FS, DLM_LVB_LEN,
865 NULL, NULL, NULL, &fsdlm);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800866 if (rc) {
Goldwyn Rodrigues24aa3382014-01-21 15:48:23 -0800867 ocfs2_live_connection_drop(lc);
868 lc = NULL;
David Teiglandcf4d8d72008-02-20 14:29:27 -0800869 goto out;
870 }
871
Goldwyn Rodrigues24aa3382014-01-21 15:48:23 -0800872 conn->cc_private = lc;
David Teiglandcf4d8d72008-02-20 14:29:27 -0800873 conn->cc_lockspace = fsdlm;
874out:
Goldwyn Rodrigues24aa3382014-01-21 15:48:23 -0800875 if (rc && lc)
876 kfree(lc);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800877 return rc;
878}
879
Joel Becker2c394502008-05-30 15:58:26 -0700880static int user_cluster_disconnect(struct ocfs2_cluster_connection *conn)
David Teiglandcf4d8d72008-02-20 14:29:27 -0800881{
882 dlm_release_lockspace(conn->cc_lockspace, 2);
883 conn->cc_lockspace = NULL;
884 ocfs2_live_connection_drop(conn->cc_private);
885 conn->cc_private = NULL;
886 return 0;
887}
888
889static int user_cluster_this_node(unsigned int *this_node)
890{
891 int rc;
892
893 rc = ocfs2_control_get_this_node();
894 if (rc < 0)
895 return rc;
896
897 *this_node = rc;
898 return 0;
899}
900
Joel Beckera12630b2008-05-09 18:49:29 -0700901static struct ocfs2_stack_operations ocfs2_user_plugin_ops = {
David Teiglandcf4d8d72008-02-20 14:29:27 -0800902 .connect = user_cluster_connect,
903 .disconnect = user_cluster_disconnect,
904 .this_node = user_cluster_this_node,
905 .dlm_lock = user_dlm_lock,
906 .dlm_unlock = user_dlm_unlock,
907 .lock_status = user_dlm_lock_status,
Joel Becker1c520df2009-06-19 15:14:13 -0700908 .lvb_valid = user_dlm_lvb_valid,
David Teiglandcf4d8d72008-02-20 14:29:27 -0800909 .lock_lvb = user_dlm_lvb,
Mark Fasheh53da4932008-07-21 14:29:16 -0700910 .plock = user_plock,
David Teiglandcf4d8d72008-02-20 14:29:27 -0800911 .dump_lksb = user_dlm_dump_lksb,
912};
913
Joel Beckera12630b2008-05-09 18:49:29 -0700914static struct ocfs2_stack_plugin ocfs2_user_plugin = {
David Teiglandcf4d8d72008-02-20 14:29:27 -0800915 .sp_name = "user",
Joel Beckera12630b2008-05-09 18:49:29 -0700916 .sp_ops = &ocfs2_user_plugin_ops,
David Teiglandcf4d8d72008-02-20 14:29:27 -0800917 .sp_owner = THIS_MODULE,
918};
919
920
Joel Beckera12630b2008-05-09 18:49:29 -0700921static int __init ocfs2_user_plugin_init(void)
Joel Becker6427a722008-02-18 19:23:28 -0800922{
David Teiglandcf4d8d72008-02-20 14:29:27 -0800923 int rc;
924
925 rc = ocfs2_control_init();
926 if (!rc) {
Joel Beckera12630b2008-05-09 18:49:29 -0700927 rc = ocfs2_stack_glue_register(&ocfs2_user_plugin);
David Teiglandcf4d8d72008-02-20 14:29:27 -0800928 if (rc)
929 ocfs2_control_exit();
930 }
931
932 return rc;
Joel Becker6427a722008-02-18 19:23:28 -0800933}
934
Joel Beckera12630b2008-05-09 18:49:29 -0700935static void __exit ocfs2_user_plugin_exit(void)
Joel Becker8adf0532007-11-28 14:38:40 -0800936{
Joel Beckera12630b2008-05-09 18:49:29 -0700937 ocfs2_stack_glue_unregister(&ocfs2_user_plugin);
Joel Becker6427a722008-02-18 19:23:28 -0800938 ocfs2_control_exit();
Joel Becker8adf0532007-11-28 14:38:40 -0800939}
940
941MODULE_AUTHOR("Oracle");
942MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
943MODULE_LICENSE("GPL");
Joel Beckera12630b2008-05-09 18:49:29 -0700944module_init(ocfs2_user_plugin_init);
945module_exit(ocfs2_user_plugin_exit);