netlink.c 2.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8
/*
 * Copyright (C) 2007 Red Hat, Inc.  All rights reserved.
 */

#include <net/genetlink.h>
#include <linux/dlm.h>
#include <linux/dlm_netlink.h>
9
#include <linux/gfp.h>
10 11 12 13

#include "dlm_internal.h"

static uint32_t dlm_nl_seqnum;
14
static uint32_t listener_nlportid;
15

16
static struct genl_family family;
17 18 19 20 21 22

static int prepare_data(u8 cmd, struct sk_buff **skbp, size_t size)
{
	struct sk_buff *skb;
	void *data;

D
David Teigland 已提交
23
	skb = genlmsg_new(size, GFP_NOFS);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	if (!skb)
		return -ENOMEM;

	/* add the message headers */
	data = genlmsg_put(skb, 0, dlm_nl_seqnum++, &family, 0, cmd);
	if (!data) {
		nlmsg_free(skb);
		return -EINVAL;
	}

	*skbp = skb;
	return 0;
}

static struct dlm_lock_data *mk_data(struct sk_buff *skb)
{
	struct nlattr *ret;

	ret = nla_reserve(skb, DLM_TYPE_LOCK, sizeof(struct dlm_lock_data));
	if (!ret)
		return NULL;
	return nla_data(ret);
}

static int send_data(struct sk_buff *skb)
{
	struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
	void *data = genlmsg_data(genlhdr);

53
	genlmsg_end(skb, data);
54

55
	return genlmsg_unicast(&init_net, skb, listener_nlportid);
56 57 58 59
}

static int user_cmd(struct sk_buff *skb, struct genl_info *info)
{
60 61
	listener_nlportid = info->snd_portid;
	printk("user_cmd nlpid %u\n", listener_nlportid);
62 63 64
	return 0;
}

65
static const struct genl_small_ops dlm_nl_ops[] = {
66 67
	{
		.cmd	= DLM_CMD_HELLO,
68
		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
69 70
		.doit	= user_cmd,
	},
71 72
};

73
static struct genl_family family __ro_after_init = {
74 75
	.name		= DLM_GENL_NAME,
	.version	= DLM_GENL_VERSION,
76 77
	.small_ops	= dlm_nl_ops,
	.n_small_ops	= ARRAY_SIZE(dlm_nl_ops),
78
	.resv_start_op	= DLM_CMD_HELLO + 1,
79 80 81
	.module		= THIS_MODULE,
};

82
int __init dlm_netlink_init(void)
83
{
84
	return genl_register_family(&family);
85 86
}

87
void dlm_netlink_exit(void)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
	genl_unregister_family(&family);
}

static void fill_data(struct dlm_lock_data *data, struct dlm_lkb *lkb)
{
	struct dlm_rsb *r = lkb->lkb_resource;

	memset(data, 0, sizeof(struct dlm_lock_data));

	data->version = DLM_LOCK_DATA_VERSION;
	data->nodeid = lkb->lkb_nodeid;
	data->ownpid = lkb->lkb_ownpid;
	data->id = lkb->lkb_id;
	data->remid = lkb->lkb_remid;
	data->status = lkb->lkb_status;
	data->grmode = lkb->lkb_grmode;
	data->rqmode = lkb->lkb_rqmode;
106 107
	if (lkb->lkb_ua)
		data->xid = lkb->lkb_ua->xid;
108 109 110 111 112 113 114 115 116
	if (r) {
		data->lockspace_id = r->res_ls->ls_global_id;
		data->resource_namelen = r->res_length;
		memcpy(data->resource_name, r->res_name, r->res_length);
	}
}

void dlm_timeout_warn(struct dlm_lkb *lkb)
{
117
	struct sk_buff *send_skb;
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	struct dlm_lock_data *data;
	size_t size;
	int rv;

	size = nla_total_size(sizeof(struct dlm_lock_data)) +
	       nla_total_size(0); /* why this? */

	rv = prepare_data(DLM_CMD_TIMEOUT, &send_skb, size);
	if (rv < 0)
		return;

	data = mk_data(send_skb);
	if (!data) {
		nlmsg_free(send_skb);
		return;
	}

	fill_data(data, lkb);

	send_data(send_skb);
}