1 // SPDX-License-Identifier: GPL-2.0-only
5 * Code which interfaces ocfs2 with fs/dlm and a userspace stack.
7 * Copyright (C) 2007 Oracle. All rights reserved.
10 #include <linux/module.h>
12 #include <linux/filelock.h>
13 #include <linux/miscdevice.h>
14 #include <linux/mutex.h>
15 #include <linux/slab.h>
16 #include <linux/reboot.h>
17 #include <linux/sched.h>
18 #include <linux/uaccess.h>
20 #include "stackglue.h"
22 #include <linux/dlm_plock.h>
25 * The control protocol starts with a handshake. Until the handshake
26 * is complete, the control device will fail all write(2)s.
28 * The handshake is simple. First, the client reads until EOF. Each line
29 * of output is a supported protocol tag. All protocol tags are a single
30 * character followed by a two hex digit version number. Currently the
31 * only things supported is T01, for "Text-base version 0x01". Next, the
32 * client writes the version they would like to use, including the newline.
33 * Thus, the protocol tag is 'T01\n'. If the version tag written is
34 * unknown, -EINVAL is returned. Once the negotiation is complete, the
35 * client can start sending messages.
37 * The T01 protocol has three messages. First is the "SETN" message.
38 * It has the following syntax:
40 * SETN<space><8-char-hex-nodenum><newline>
42 * This is 14 characters.
44 * The "SETN" message must be the first message following the protocol.
45 * It tells ocfs2_control the local node number.
47 * Next comes the "SETV" message. It has the following syntax:
49 * SETV<space><2-char-hex-major><space><2-char-hex-minor><newline>
51 * This is 11 characters.
53 * The "SETV" message sets the filesystem locking protocol version as
54 * negotiated by the client. The client negotiates based on the maximum
55 * version advertised in /sys/fs/ocfs2/max_locking_protocol. The major
56 * number from the "SETV" message must match
57 * ocfs2_user_plugin.sp_max_proto.pv_major, and the minor number
58 * must be less than or equal to ...sp_max_version.pv_minor.
60 * Once this information has been set, mounts will be allowed. From this
61 * point on, the "DOWN" message can be sent for node down notification.
62 * It has the following syntax:
64 * DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline>
68 * DOWN 632A924FDD844190BDA93C0DF6B94899 00000001\n
70 * This is 47 characters.
74 * Whether or not the client has done the handshake.
75 * For now, we have just one protocol version.
77 #define OCFS2_CONTROL_PROTO "T01\n"
78 #define OCFS2_CONTROL_PROTO_LEN 4
80 /* Handshake states */
81 #define OCFS2_CONTROL_HANDSHAKE_INVALID (0)
82 #define OCFS2_CONTROL_HANDSHAKE_READ (1)
83 #define OCFS2_CONTROL_HANDSHAKE_PROTOCOL (2)
84 #define OCFS2_CONTROL_HANDSHAKE_VALID (3)
87 #define OCFS2_CONTROL_MESSAGE_OP_LEN 4
88 #define OCFS2_CONTROL_MESSAGE_SETNODE_OP "SETN"
89 #define OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN 14
90 #define OCFS2_CONTROL_MESSAGE_SETVERSION_OP "SETV"
91 #define OCFS2_CONTROL_MESSAGE_SETVERSION_TOTAL_LEN 11
92 #define OCFS2_CONTROL_MESSAGE_DOWN_OP "DOWN"
93 #define OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN 47
94 #define OCFS2_TEXT_UUID_LEN 32
95 #define OCFS2_CONTROL_MESSAGE_VERNUM_LEN 2
96 #define OCFS2_CONTROL_MESSAGE_NODENUM_LEN 8
97 #define VERSION_LOCK "version_lock"
99 enum ocfs2_connection_type {
105 * ocfs2_live_connection is refcounted because the filesystem and
106 * miscdevice sides can detach in different order. Let's just be safe.
108 struct ocfs2_live_connection {
109 struct list_head oc_list;
110 struct ocfs2_cluster_connection *oc_conn;
111 enum ocfs2_connection_type oc_type;
112 atomic_t oc_this_node;
114 struct dlm_lksb oc_version_lksb;
115 char oc_lvb[DLM_LVB_LEN];
116 struct completion oc_sync_wait;
117 wait_queue_head_t oc_wait;
120 struct ocfs2_control_private {
121 struct list_head op_list;
124 struct ocfs2_protocol_version op_proto;
127 /* SETN<space><8-char-hex-nodenum><newline> */
128 struct ocfs2_control_message_setn {
129 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
131 char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
135 /* SETV<space><2-char-hex-major><space><2-char-hex-minor><newline> */
136 struct ocfs2_control_message_setv {
137 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
139 char major[OCFS2_CONTROL_MESSAGE_VERNUM_LEN];
141 char minor[OCFS2_CONTROL_MESSAGE_VERNUM_LEN];
145 /* DOWN<space><32-char-cap-hex-uuid><space><8-char-hex-nodenum><newline> */
146 struct ocfs2_control_message_down {
147 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
149 char uuid[OCFS2_TEXT_UUID_LEN];
151 char nodestr[OCFS2_CONTROL_MESSAGE_NODENUM_LEN];
155 union ocfs2_control_message {
156 char tag[OCFS2_CONTROL_MESSAGE_OP_LEN];
157 struct ocfs2_control_message_setn u_setn;
158 struct ocfs2_control_message_setv u_setv;
159 struct ocfs2_control_message_down u_down;
162 static struct ocfs2_stack_plugin ocfs2_user_plugin;
164 static atomic_t ocfs2_control_opened;
165 static int ocfs2_control_this_node = -1;
166 static struct ocfs2_protocol_version running_proto;
168 static LIST_HEAD(ocfs2_live_connection_list);
169 static LIST_HEAD(ocfs2_control_private_list);
170 static DEFINE_MUTEX(ocfs2_control_lock);
172 static inline void ocfs2_control_set_handshake_state(struct file *file,
175 struct ocfs2_control_private *p = file->private_data;
179 static inline int ocfs2_control_get_handshake_state(struct file *file)
181 struct ocfs2_control_private *p = file->private_data;
185 static struct ocfs2_live_connection *ocfs2_connection_find(const char *name)
187 size_t len = strlen(name);
188 struct ocfs2_live_connection *c;
190 BUG_ON(!mutex_is_locked(&ocfs2_control_lock));
192 list_for_each_entry(c, &ocfs2_live_connection_list, oc_list) {
193 if ((c->oc_conn->cc_namelen == len) &&
194 !strncmp(c->oc_conn->cc_name, name, len))
202 * ocfs2_live_connection structures are created underneath the ocfs2
203 * mount path. Since the VFS prevents multiple calls to
204 * fill_super(), we can't get dupes here.
206 static int ocfs2_live_connection_attach(struct ocfs2_cluster_connection *conn,
207 struct ocfs2_live_connection *c)
211 mutex_lock(&ocfs2_control_lock);
214 if ((c->oc_type == NO_CONTROLD) || atomic_read(&ocfs2_control_opened))
215 list_add(&c->oc_list, &ocfs2_live_connection_list);
218 "ocfs2: Userspace control daemon is not present\n");
222 mutex_unlock(&ocfs2_control_lock);
227 * This function disconnects the cluster connection from ocfs2_control.
228 * Afterwards, userspace can't affect the cluster connection.
230 static void ocfs2_live_connection_drop(struct ocfs2_live_connection *c)
232 mutex_lock(&ocfs2_control_lock);
233 list_del_init(&c->oc_list);
235 mutex_unlock(&ocfs2_control_lock);
240 static int ocfs2_control_cfu(void *target, size_t target_len,
241 const char __user *buf, size_t count)
243 /* The T01 expects write(2) calls to have exactly one command */
244 if ((count != target_len) ||
245 (count > sizeof(union ocfs2_control_message)))
248 if (copy_from_user(target, buf, target_len))
254 static ssize_t ocfs2_control_validate_protocol(struct file *file,
255 const char __user *buf,
259 char kbuf[OCFS2_CONTROL_PROTO_LEN];
261 ret = ocfs2_control_cfu(kbuf, OCFS2_CONTROL_PROTO_LEN,
266 if (strncmp(kbuf, OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN))
269 ocfs2_control_set_handshake_state(file,
270 OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
275 static void ocfs2_control_send_down(const char *uuid,
278 struct ocfs2_live_connection *c;
280 mutex_lock(&ocfs2_control_lock);
282 c = ocfs2_connection_find(uuid);
284 BUG_ON(c->oc_conn == NULL);
285 c->oc_conn->cc_recovery_handler(nodenum,
286 c->oc_conn->cc_recovery_data);
289 mutex_unlock(&ocfs2_control_lock);
293 * Called whenever configuration elements are sent to /dev/ocfs2_control.
294 * If all configuration elements are present, try to set the global
295 * values. If there is a problem, return an error. Skip any missing
296 * elements, and only bump ocfs2_control_opened when we have all elements
297 * and are successful.
299 static int ocfs2_control_install_private(struct file *file)
303 struct ocfs2_control_private *p = file->private_data;
305 BUG_ON(p->op_state != OCFS2_CONTROL_HANDSHAKE_PROTOCOL);
307 mutex_lock(&ocfs2_control_lock);
309 if (p->op_this_node < 0) {
311 } else if ((ocfs2_control_this_node >= 0) &&
312 (ocfs2_control_this_node != p->op_this_node)) {
317 if (!p->op_proto.pv_major) {
319 } else if (!list_empty(&ocfs2_live_connection_list) &&
320 ((running_proto.pv_major != p->op_proto.pv_major) ||
321 (running_proto.pv_minor != p->op_proto.pv_minor))) {
327 ocfs2_control_this_node = p->op_this_node;
328 running_proto.pv_major = p->op_proto.pv_major;
329 running_proto.pv_minor = p->op_proto.pv_minor;
333 mutex_unlock(&ocfs2_control_lock);
336 /* We set the global values successfully */
337 atomic_inc(&ocfs2_control_opened);
338 ocfs2_control_set_handshake_state(file,
339 OCFS2_CONTROL_HANDSHAKE_VALID);
345 static int ocfs2_control_get_this_node(void)
349 mutex_lock(&ocfs2_control_lock);
350 if (ocfs2_control_this_node < 0)
353 rc = ocfs2_control_this_node;
354 mutex_unlock(&ocfs2_control_lock);
359 static int ocfs2_control_do_setnode_msg(struct file *file,
360 struct ocfs2_control_message_setn *msg)
364 struct ocfs2_control_private *p = file->private_data;
366 if (ocfs2_control_get_handshake_state(file) !=
367 OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
370 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
371 OCFS2_CONTROL_MESSAGE_OP_LEN))
374 if ((msg->space != ' ') || (msg->newline != '\n'))
376 msg->space = msg->newline = '\0';
378 nodenum = simple_strtol(msg->nodestr, &ptr, 16);
382 if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
383 (nodenum > INT_MAX) || (nodenum < 0))
385 p->op_this_node = nodenum;
387 return ocfs2_control_install_private(file);
390 static int ocfs2_control_do_setversion_msg(struct file *file,
391 struct ocfs2_control_message_setv *msg)
395 struct ocfs2_control_private *p = file->private_data;
396 struct ocfs2_protocol_version *max =
397 &ocfs2_user_plugin.sp_max_proto;
399 if (ocfs2_control_get_handshake_state(file) !=
400 OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
403 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
404 OCFS2_CONTROL_MESSAGE_OP_LEN))
407 if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
408 (msg->newline != '\n'))
410 msg->space1 = msg->space2 = msg->newline = '\0';
412 major = simple_strtol(msg->major, &ptr, 16);
415 minor = simple_strtol(msg->minor, &ptr, 16);
420 * The major must be between 1 and 255, inclusive. The minor
421 * must be between 0 and 255, inclusive. The version passed in
422 * must be within the maximum version supported by the filesystem.
424 if ((major == LONG_MIN) || (major == LONG_MAX) ||
425 (major > (u8)-1) || (major < 1))
427 if ((minor == LONG_MIN) || (minor == LONG_MAX) ||
428 (minor > (u8)-1) || (minor < 0))
430 if ((major != max->pv_major) ||
431 (minor > max->pv_minor))
434 p->op_proto.pv_major = major;
435 p->op_proto.pv_minor = minor;
437 return ocfs2_control_install_private(file);
440 static int ocfs2_control_do_down_msg(struct file *file,
441 struct ocfs2_control_message_down *msg)
446 if (ocfs2_control_get_handshake_state(file) !=
447 OCFS2_CONTROL_HANDSHAKE_VALID)
450 if (strncmp(msg->tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
451 OCFS2_CONTROL_MESSAGE_OP_LEN))
454 if ((msg->space1 != ' ') || (msg->space2 != ' ') ||
455 (msg->newline != '\n'))
457 msg->space1 = msg->space2 = msg->newline = '\0';
459 nodenum = simple_strtol(msg->nodestr, &p, 16);
463 if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
464 (nodenum > INT_MAX) || (nodenum < 0))
467 ocfs2_control_send_down(msg->uuid, nodenum);
472 static ssize_t ocfs2_control_message(struct file *file,
473 const char __user *buf,
477 union ocfs2_control_message msg;
479 /* Try to catch padding issues */
480 WARN_ON(offsetof(struct ocfs2_control_message_down, uuid) !=
481 (sizeof(msg.u_down.tag) + sizeof(msg.u_down.space1)));
483 memset(&msg, 0, sizeof(union ocfs2_control_message));
484 ret = ocfs2_control_cfu(&msg, count, buf, count);
488 if ((count == OCFS2_CONTROL_MESSAGE_SETNODE_TOTAL_LEN) &&
489 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETNODE_OP,
490 OCFS2_CONTROL_MESSAGE_OP_LEN))
491 ret = ocfs2_control_do_setnode_msg(file, &msg.u_setn);
492 else if ((count == OCFS2_CONTROL_MESSAGE_SETVERSION_TOTAL_LEN) &&
493 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_SETVERSION_OP,
494 OCFS2_CONTROL_MESSAGE_OP_LEN))
495 ret = ocfs2_control_do_setversion_msg(file, &msg.u_setv);
496 else if ((count == OCFS2_CONTROL_MESSAGE_DOWN_TOTAL_LEN) &&
497 !strncmp(msg.tag, OCFS2_CONTROL_MESSAGE_DOWN_OP,
498 OCFS2_CONTROL_MESSAGE_OP_LEN))
499 ret = ocfs2_control_do_down_msg(file, &msg.u_down);
504 return ret ? ret : count;
507 static ssize_t ocfs2_control_write(struct file *file,
508 const char __user *buf,
514 switch (ocfs2_control_get_handshake_state(file)) {
515 case OCFS2_CONTROL_HANDSHAKE_INVALID:
519 case OCFS2_CONTROL_HANDSHAKE_READ:
520 ret = ocfs2_control_validate_protocol(file, buf,
524 case OCFS2_CONTROL_HANDSHAKE_PROTOCOL:
525 case OCFS2_CONTROL_HANDSHAKE_VALID:
526 ret = ocfs2_control_message(file, buf, count);
539 * This is a naive version. If we ever have a new protocol, we'll expand
540 * it. Probably using seq_file.
542 static ssize_t ocfs2_control_read(struct file *file,
549 ret = simple_read_from_buffer(buf, count, ppos,
550 OCFS2_CONTROL_PROTO, OCFS2_CONTROL_PROTO_LEN);
552 /* Have we read the whole protocol list? */
553 if (ret > 0 && *ppos >= OCFS2_CONTROL_PROTO_LEN)
554 ocfs2_control_set_handshake_state(file,
555 OCFS2_CONTROL_HANDSHAKE_READ);
560 static int ocfs2_control_release(struct inode *inode, struct file *file)
562 struct ocfs2_control_private *p = file->private_data;
564 mutex_lock(&ocfs2_control_lock);
566 if (ocfs2_control_get_handshake_state(file) !=
567 OCFS2_CONTROL_HANDSHAKE_VALID)
570 if (atomic_dec_and_test(&ocfs2_control_opened)) {
571 if (!list_empty(&ocfs2_live_connection_list)) {
572 /* XXX: Do bad things! */
574 "ocfs2: Unexpected release of ocfs2_control!\n"
575 " Loss of cluster connection requires "
576 "an emergency restart!\n");
580 * Last valid close clears the node number and resets
581 * the locking protocol version
583 ocfs2_control_this_node = -1;
584 running_proto.pv_major = 0;
585 running_proto.pv_minor = 0;
589 list_del_init(&p->op_list);
590 file->private_data = NULL;
592 mutex_unlock(&ocfs2_control_lock);
599 static int ocfs2_control_open(struct inode *inode, struct file *file)
601 struct ocfs2_control_private *p;
603 p = kzalloc(sizeof(struct ocfs2_control_private), GFP_KERNEL);
606 p->op_this_node = -1;
608 mutex_lock(&ocfs2_control_lock);
609 file->private_data = p;
610 list_add(&p->op_list, &ocfs2_control_private_list);
611 mutex_unlock(&ocfs2_control_lock);
616 static const struct file_operations ocfs2_control_fops = {
617 .open = ocfs2_control_open,
618 .release = ocfs2_control_release,
619 .read = ocfs2_control_read,
620 .write = ocfs2_control_write,
621 .owner = THIS_MODULE,
622 .llseek = default_llseek,
625 static struct miscdevice ocfs2_control_device = {
626 .minor = MISC_DYNAMIC_MINOR,
627 .name = "ocfs2_control",
628 .fops = &ocfs2_control_fops,
631 static int ocfs2_control_init(void)
635 atomic_set(&ocfs2_control_opened, 0);
637 rc = misc_register(&ocfs2_control_device);
640 "ocfs2: Unable to register ocfs2_control device "
647 static void ocfs2_control_exit(void)
649 misc_deregister(&ocfs2_control_device);
652 static void fsdlm_lock_ast_wrapper(void *astarg)
654 struct ocfs2_dlm_lksb *lksb = astarg;
655 int status = lksb->lksb_fsdlm.sb_status;
658 * For now we're punting on the issue of other non-standard errors
659 * where we can't tell if the unlock_ast or lock_ast should be called.
660 * The main "other error" that's possible is EINVAL which means the
661 * function was called with invalid args, which shouldn't be possible
662 * since the caller here is under our control. Other non-standard
663 * errors probably fall into the same category, or otherwise are fatal
664 * which means we can't carry on anyway.
667 if (status == -DLM_EUNLOCK || status == -DLM_ECANCEL)
668 lksb->lksb_conn->cc_proto->lp_unlock_ast(lksb, 0);
670 lksb->lksb_conn->cc_proto->lp_lock_ast(lksb);
673 static void fsdlm_blocking_ast_wrapper(void *astarg, int level)
675 struct ocfs2_dlm_lksb *lksb = astarg;
677 lksb->lksb_conn->cc_proto->lp_blocking_ast(lksb, level);
680 static int user_dlm_lock(struct ocfs2_cluster_connection *conn,
682 struct ocfs2_dlm_lksb *lksb,
685 unsigned int namelen)
687 if (!lksb->lksb_fsdlm.sb_lvbptr)
688 lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
689 sizeof(struct dlm_lksb);
691 return dlm_lock(conn->cc_lockspace, mode, &lksb->lksb_fsdlm,
692 flags|DLM_LKF_NODLCKWT, name, namelen, 0,
693 fsdlm_lock_ast_wrapper, lksb,
694 fsdlm_blocking_ast_wrapper);
697 static int user_dlm_unlock(struct ocfs2_cluster_connection *conn,
698 struct ocfs2_dlm_lksb *lksb,
701 return dlm_unlock(conn->cc_lockspace, lksb->lksb_fsdlm.sb_lkid,
702 flags, &lksb->lksb_fsdlm, lksb);
705 static int user_dlm_lock_status(struct ocfs2_dlm_lksb *lksb)
707 return lksb->lksb_fsdlm.sb_status;
710 static int user_dlm_lvb_valid(struct ocfs2_dlm_lksb *lksb)
712 int invalid = lksb->lksb_fsdlm.sb_flags & DLM_SBF_VALNOTVALID;
717 static void *user_dlm_lvb(struct ocfs2_dlm_lksb *lksb)
719 if (!lksb->lksb_fsdlm.sb_lvbptr)
720 lksb->lksb_fsdlm.sb_lvbptr = (char *)lksb +
721 sizeof(struct dlm_lksb);
722 return (void *)(lksb->lksb_fsdlm.sb_lvbptr);
725 static void user_dlm_dump_lksb(struct ocfs2_dlm_lksb *lksb)
729 static int user_plock(struct ocfs2_cluster_connection *conn,
733 struct file_lock *fl)
736 * This more or less just demuxes the plock request into any
737 * one of three dlm calls.
739 * Internally, fs/dlm will pass these to a misc device, which
740 * a userspace daemon will read and write to.
743 if (cmd == F_CANCELLK)
744 return dlm_posix_cancel(conn->cc_lockspace, ino, file, fl);
745 else if (IS_GETLK(cmd))
746 return dlm_posix_get(conn->cc_lockspace, ino, file, fl);
747 else if (fl->fl_type == F_UNLCK)
748 return dlm_posix_unlock(conn->cc_lockspace, ino, file, fl);
750 return dlm_posix_lock(conn->cc_lockspace, ino, file, cmd, fl);
754 * Compare a requested locking protocol version against the current one.
756 * If the major numbers are different, they are incompatible.
757 * If the current minor is greater than the request, they are incompatible.
758 * If the current minor is less than or equal to the request, they are
759 * compatible, and the requester should run at the current minor version.
761 static int fs_protocol_compare(struct ocfs2_protocol_version *existing,
762 struct ocfs2_protocol_version *request)
764 if (existing->pv_major != request->pv_major)
767 if (existing->pv_minor > request->pv_minor)
770 if (existing->pv_minor < request->pv_minor)
771 request->pv_minor = existing->pv_minor;
776 static void lvb_to_version(char *lvb, struct ocfs2_protocol_version *ver)
778 struct ocfs2_protocol_version *pv =
779 (struct ocfs2_protocol_version *)lvb;
781 * ocfs2_protocol_version has two u8 variables, so we don't
782 * need any endian conversion.
784 ver->pv_major = pv->pv_major;
785 ver->pv_minor = pv->pv_minor;
788 static void version_to_lvb(struct ocfs2_protocol_version *ver, char *lvb)
790 struct ocfs2_protocol_version *pv =
791 (struct ocfs2_protocol_version *)lvb;
793 * ocfs2_protocol_version has two u8 variables, so we don't
794 * need any endian conversion.
796 pv->pv_major = ver->pv_major;
797 pv->pv_minor = ver->pv_minor;
800 static void sync_wait_cb(void *arg)
802 struct ocfs2_cluster_connection *conn = arg;
803 struct ocfs2_live_connection *lc = conn->cc_private;
804 complete(&lc->oc_sync_wait);
807 static int sync_unlock(struct ocfs2_cluster_connection *conn,
808 struct dlm_lksb *lksb, char *name)
811 struct ocfs2_live_connection *lc = conn->cc_private;
813 error = dlm_unlock(conn->cc_lockspace, lksb->sb_lkid, 0, lksb, conn);
815 printk(KERN_ERR "%s lkid %x error %d\n",
816 name, lksb->sb_lkid, error);
820 wait_for_completion(&lc->oc_sync_wait);
822 if (lksb->sb_status != -DLM_EUNLOCK) {
823 printk(KERN_ERR "%s lkid %x status %d\n",
824 name, lksb->sb_lkid, lksb->sb_status);
830 static int sync_lock(struct ocfs2_cluster_connection *conn,
831 int mode, uint32_t flags,
832 struct dlm_lksb *lksb, char *name)
835 struct ocfs2_live_connection *lc = conn->cc_private;
837 error = dlm_lock(conn->cc_lockspace, mode, lksb, flags,
839 0, sync_wait_cb, conn, NULL);
841 printk(KERN_ERR "%s lkid %x flags %x mode %d error %d\n",
842 name, lksb->sb_lkid, flags, mode, error);
846 wait_for_completion(&lc->oc_sync_wait);
848 status = lksb->sb_status;
850 if (status && status != -EAGAIN) {
851 printk(KERN_ERR "%s lkid %x flags %x mode %d status %d\n",
852 name, lksb->sb_lkid, flags, mode, status);
859 static int version_lock(struct ocfs2_cluster_connection *conn, int mode,
862 struct ocfs2_live_connection *lc = conn->cc_private;
863 return sync_lock(conn, mode, flags,
864 &lc->oc_version_lksb, VERSION_LOCK);
867 static int version_unlock(struct ocfs2_cluster_connection *conn)
869 struct ocfs2_live_connection *lc = conn->cc_private;
870 return sync_unlock(conn, &lc->oc_version_lksb, VERSION_LOCK);
873 /* get_protocol_version()
875 * To exchange ocfs2 versioning, we use the LVB of the version dlm lock.
877 * 1. Attempt to take the lock in EX mode (non-blocking).
878 * 2. If successful (which means it is the first mount), write the
879 * version number and downconvert to PR lock.
880 * 3. If unsuccessful (returns -EAGAIN), read the version from the LVB after
881 * taking the PR lock.
884 static int get_protocol_version(struct ocfs2_cluster_connection *conn)
887 struct ocfs2_live_connection *lc = conn->cc_private;
888 struct ocfs2_protocol_version pv;
890 running_proto.pv_major =
891 ocfs2_user_plugin.sp_max_proto.pv_major;
892 running_proto.pv_minor =
893 ocfs2_user_plugin.sp_max_proto.pv_minor;
895 lc->oc_version_lksb.sb_lvbptr = lc->oc_lvb;
896 ret = version_lock(conn, DLM_LOCK_EX,
897 DLM_LKF_VALBLK|DLM_LKF_NOQUEUE);
899 conn->cc_version.pv_major = running_proto.pv_major;
900 conn->cc_version.pv_minor = running_proto.pv_minor;
901 version_to_lvb(&running_proto, lc->oc_lvb);
902 version_lock(conn, DLM_LOCK_PR, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
903 } else if (ret == -EAGAIN) {
904 ret = version_lock(conn, DLM_LOCK_PR, DLM_LKF_VALBLK);
907 lvb_to_version(lc->oc_lvb, &pv);
909 if ((pv.pv_major != running_proto.pv_major) ||
910 (pv.pv_minor > running_proto.pv_minor)) {
915 conn->cc_version.pv_major = pv.pv_major;
916 conn->cc_version.pv_minor = pv.pv_minor;
922 static void user_recover_prep(void *arg)
926 static void user_recover_slot(void *arg, struct dlm_slot *slot)
928 struct ocfs2_cluster_connection *conn = arg;
929 printk(KERN_INFO "ocfs2: Node %d/%d down. Initiating recovery.\n",
930 slot->nodeid, slot->slot);
931 conn->cc_recovery_handler(slot->nodeid, conn->cc_recovery_data);
935 static void user_recover_done(void *arg, struct dlm_slot *slots,
936 int num_slots, int our_slot,
939 struct ocfs2_cluster_connection *conn = arg;
940 struct ocfs2_live_connection *lc = conn->cc_private;
943 for (i = 0; i < num_slots; i++)
944 if (slots[i].slot == our_slot) {
945 atomic_set(&lc->oc_this_node, slots[i].nodeid);
949 lc->oc_our_slot = our_slot;
950 wake_up(&lc->oc_wait);
953 static const struct dlm_lockspace_ops ocfs2_ls_ops = {
954 .recover_prep = user_recover_prep,
955 .recover_slot = user_recover_slot,
956 .recover_done = user_recover_done,
959 static int user_cluster_disconnect(struct ocfs2_cluster_connection *conn)
961 version_unlock(conn);
962 dlm_release_lockspace(conn->cc_lockspace, 2);
963 conn->cc_lockspace = NULL;
964 ocfs2_live_connection_drop(conn->cc_private);
965 conn->cc_private = NULL;
969 static int user_cluster_connect(struct ocfs2_cluster_connection *conn)
971 dlm_lockspace_t *fsdlm;
972 struct ocfs2_live_connection *lc;
975 BUG_ON(conn == NULL);
977 lc = kzalloc(sizeof(struct ocfs2_live_connection), GFP_KERNEL);
981 init_waitqueue_head(&lc->oc_wait);
982 init_completion(&lc->oc_sync_wait);
983 atomic_set(&lc->oc_this_node, 0);
984 conn->cc_private = lc;
985 lc->oc_type = NO_CONTROLD;
987 rc = dlm_new_lockspace(conn->cc_name, conn->cc_cluster_name,
988 DLM_LSFL_NEWEXCL, DLM_LVB_LEN,
989 &ocfs2_ls_ops, conn, &ops_rv, &fsdlm);
991 if (rc == -EEXIST || rc == -EPROTO)
992 printk(KERN_ERR "ocfs2: Unable to create the "
993 "lockspace %s (%d), because a ocfs2-tools "
994 "program is running on this file system "
995 "with the same name lockspace\n",
1000 if (ops_rv == -EOPNOTSUPP) {
1001 lc->oc_type = WITH_CONTROLD;
1002 printk(KERN_NOTICE "ocfs2: You seem to be using an older "
1003 "version of dlm_controld and/or ocfs2-tools."
1004 " Please consider upgrading.\n");
1005 } else if (ops_rv) {
1009 conn->cc_lockspace = fsdlm;
1011 rc = ocfs2_live_connection_attach(conn, lc);
1015 if (lc->oc_type == NO_CONTROLD) {
1016 rc = get_protocol_version(conn);
1018 printk(KERN_ERR "ocfs2: Could not determine"
1019 " locking version\n");
1020 user_cluster_disconnect(conn);
1023 wait_event(lc->oc_wait, (atomic_read(&lc->oc_this_node) > 0));
1027 * running_proto must have been set before we allowed any mounts
1030 if (fs_protocol_compare(&running_proto, &conn->cc_version)) {
1032 "Unable to mount with fs locking protocol version "
1033 "%u.%u because negotiated protocol is %u.%u\n",
1034 conn->cc_version.pv_major, conn->cc_version.pv_minor,
1035 running_proto.pv_major, running_proto.pv_minor);
1037 ocfs2_live_connection_drop(lc);
1048 static int user_cluster_this_node(struct ocfs2_cluster_connection *conn,
1049 unsigned int *this_node)
1052 struct ocfs2_live_connection *lc = conn->cc_private;
1054 if (lc->oc_type == WITH_CONTROLD)
1055 rc = ocfs2_control_get_this_node();
1056 else if (lc->oc_type == NO_CONTROLD)
1057 rc = atomic_read(&lc->oc_this_node);
1068 static struct ocfs2_stack_operations ocfs2_user_plugin_ops = {
1069 .connect = user_cluster_connect,
1070 .disconnect = user_cluster_disconnect,
1071 .this_node = user_cluster_this_node,
1072 .dlm_lock = user_dlm_lock,
1073 .dlm_unlock = user_dlm_unlock,
1074 .lock_status = user_dlm_lock_status,
1075 .lvb_valid = user_dlm_lvb_valid,
1076 .lock_lvb = user_dlm_lvb,
1077 .plock = user_plock,
1078 .dump_lksb = user_dlm_dump_lksb,
1081 static struct ocfs2_stack_plugin ocfs2_user_plugin = {
1083 .sp_ops = &ocfs2_user_plugin_ops,
1084 .sp_owner = THIS_MODULE,
1088 static int __init ocfs2_user_plugin_init(void)
1092 rc = ocfs2_control_init();
1094 rc = ocfs2_stack_glue_register(&ocfs2_user_plugin);
1096 ocfs2_control_exit();
1102 static void __exit ocfs2_user_plugin_exit(void)
1104 ocfs2_stack_glue_unregister(&ocfs2_user_plugin);
1105 ocfs2_control_exit();
1108 MODULE_AUTHOR("Oracle");
1109 MODULE_DESCRIPTION("ocfs2 driver for userspace cluster stacks");
1110 MODULE_LICENSE("GPL");
1111 module_init(ocfs2_user_plugin_init);
1112 module_exit(ocfs2_user_plugin_exit);