blob: 43a96c330570d68c6b12b6c4701afad30144312b [file] [log] [blame]
David Teigland3ae1acf2007-05-18 08:59:31 -05001/*
2 * Copyright (C) 2007 Red Hat, Inc. All rights reserved.
3 *
4 * This copyrighted material is made available to anyone wishing to use,
5 * modify, copy, or redistribute it subject to the terms and conditions
6 * of the GNU General Public License v.2.
7 */
8
9#include <net/genetlink.h>
10#include <linux/dlm.h>
11#include <linux/dlm_netlink.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
David Teigland3ae1acf2007-05-18 08:59:31 -050013
14#include "dlm_internal.h"
15
16static uint32_t dlm_nl_seqnum;
Eric W. Biederman15e47302012-09-07 20:12:54 +000017static uint32_t listener_nlportid;
David Teigland3ae1acf2007-05-18 08:59:31 -050018
Johannes Berg489111e2016-10-24 14:40:03 +020019static struct genl_family family;
David Teigland3ae1acf2007-05-18 08:59:31 -050020
21static int prepare_data(u8 cmd, struct sk_buff **skbp, size_t size)
22{
23 struct sk_buff *skb;
24 void *data;
25
David Teigland573c24c2009-11-30 16:34:43 -060026 skb = genlmsg_new(size, GFP_NOFS);
David Teigland3ae1acf2007-05-18 08:59:31 -050027 if (!skb)
28 return -ENOMEM;
29
30 /* add the message headers */
31 data = genlmsg_put(skb, 0, dlm_nl_seqnum++, &family, 0, cmd);
32 if (!data) {
33 nlmsg_free(skb);
34 return -EINVAL;
35 }
36
37 *skbp = skb;
38 return 0;
39}
40
41static struct dlm_lock_data *mk_data(struct sk_buff *skb)
42{
43 struct nlattr *ret;
44
45 ret = nla_reserve(skb, DLM_TYPE_LOCK, sizeof(struct dlm_lock_data));
46 if (!ret)
47 return NULL;
48 return nla_data(ret);
49}
50
51static int send_data(struct sk_buff *skb)
52{
53 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
54 void *data = genlmsg_data(genlhdr);
David Teigland3ae1acf2007-05-18 08:59:31 -050055
Johannes Berg053c0952015-01-16 22:09:00 +010056 genlmsg_end(skb, data);
David Teigland3ae1acf2007-05-18 08:59:31 -050057
Eric W. Biederman15e47302012-09-07 20:12:54 +000058 return genlmsg_unicast(&init_net, skb, listener_nlportid);
David Teigland3ae1acf2007-05-18 08:59:31 -050059}
60
61static int user_cmd(struct sk_buff *skb, struct genl_info *info)
62{
Eric W. Biederman15e47302012-09-07 20:12:54 +000063 listener_nlportid = info->snd_portid;
64 printk("user_cmd nlpid %u\n", listener_nlportid);
David Teigland3ae1acf2007-05-18 08:59:31 -050065 return 0;
66}
67
Stephen Hemmingerdbef1c02016-08-31 10:26:28 -070068static const struct genl_ops dlm_nl_ops[] = {
Johannes Bergc53ed742013-11-19 15:19:31 +010069 {
70 .cmd = DLM_CMD_HELLO,
71 .doit = user_cmd,
72 },
David Teigland3ae1acf2007-05-18 08:59:31 -050073};
74
Johannes Berg56989f62016-10-24 14:40:05 +020075static struct genl_family family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +020076 .name = DLM_GENL_NAME,
77 .version = DLM_GENL_VERSION,
78 .ops = dlm_nl_ops,
79 .n_ops = ARRAY_SIZE(dlm_nl_ops),
80 .module = THIS_MODULE,
81};
82
Denis Cheng30727172008-02-02 01:53:46 +080083int __init dlm_netlink_init(void)
David Teigland3ae1acf2007-05-18 08:59:31 -050084{
Johannes Berg489111e2016-10-24 14:40:03 +020085 return genl_register_family(&family);
David Teigland3ae1acf2007-05-18 08:59:31 -050086}
87
Leonardo Potenza88ad2312008-05-11 19:15:34 +020088void dlm_netlink_exit(void)
David Teigland3ae1acf2007-05-18 08:59:31 -050089{
David Teigland3ae1acf2007-05-18 08:59:31 -050090 genl_unregister_family(&family);
91}
92
93static void fill_data(struct dlm_lock_data *data, struct dlm_lkb *lkb)
94{
95 struct dlm_rsb *r = lkb->lkb_resource;
David Teigland3ae1acf2007-05-18 08:59:31 -050096
97 memset(data, 0, sizeof(struct dlm_lock_data));
98
99 data->version = DLM_LOCK_DATA_VERSION;
100 data->nodeid = lkb->lkb_nodeid;
101 data->ownpid = lkb->lkb_ownpid;
102 data->id = lkb->lkb_id;
103 data->remid = lkb->lkb_remid;
104 data->status = lkb->lkb_status;
105 data->grmode = lkb->lkb_grmode;
106 data->rqmode = lkb->lkb_rqmode;
David Teiglandd292c0c2008-02-06 23:27:04 -0600107 if (lkb->lkb_ua)
108 data->xid = lkb->lkb_ua->xid;
David Teigland3ae1acf2007-05-18 08:59:31 -0500109 if (r) {
110 data->lockspace_id = r->res_ls->ls_global_id;
111 data->resource_namelen = r->res_length;
112 memcpy(data->resource_name, r->res_name, r->res_length);
113 }
114}
115
116void dlm_timeout_warn(struct dlm_lkb *lkb)
117{
Ingo Molnar180b65d2008-11-25 16:51:45 -0800118 struct sk_buff *uninitialized_var(send_skb);
David Teigland3ae1acf2007-05-18 08:59:31 -0500119 struct dlm_lock_data *data;
David Teigland3ae1acf2007-05-18 08:59:31 -0500120 size_t size;
121 int rv;
122
David Teigland3ae1acf2007-05-18 08:59:31 -0500123 size = nla_total_size(sizeof(struct dlm_lock_data)) +
124 nla_total_size(0); /* why this? */
125
126 rv = prepare_data(DLM_CMD_TIMEOUT, &send_skb, size);
127 if (rv < 0)
128 return;
129
130 data = mk_data(send_skb);
131 if (!data) {
132 nlmsg_free(send_skb);
133 return;
134 }
135
136 fill_data(data, lkb);
137
138 send_data(send_skb);
139}
140