blob: 55ea369f43a9f0d153ca769074b89e71d52082cf [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>
12
13#include "dlm_internal.h"
14
15static uint32_t dlm_nl_seqnum;
16static uint32_t listener_nlpid;
17
18static struct genl_family family = {
19 .id = GENL_ID_GENERATE,
20 .name = DLM_GENL_NAME,
21 .version = DLM_GENL_VERSION,
22};
23
24static int prepare_data(u8 cmd, struct sk_buff **skbp, size_t size)
25{
26 struct sk_buff *skb;
27 void *data;
28
29 skb = genlmsg_new(size, GFP_KERNEL);
30 if (!skb)
31 return -ENOMEM;
32
33 /* add the message headers */
34 data = genlmsg_put(skb, 0, dlm_nl_seqnum++, &family, 0, cmd);
35 if (!data) {
36 nlmsg_free(skb);
37 return -EINVAL;
38 }
39
40 *skbp = skb;
41 return 0;
42}
43
44static struct dlm_lock_data *mk_data(struct sk_buff *skb)
45{
46 struct nlattr *ret;
47
48 ret = nla_reserve(skb, DLM_TYPE_LOCK, sizeof(struct dlm_lock_data));
49 if (!ret)
50 return NULL;
51 return nla_data(ret);
52}
53
54static int send_data(struct sk_buff *skb)
55{
56 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
57 void *data = genlmsg_data(genlhdr);
58 int rv;
59
60 rv = genlmsg_end(skb, data);
61 if (rv < 0) {
62 nlmsg_free(skb);
63 return rv;
64 }
65
Johannes Berg134e6372009-07-10 09:51:34 +000066 return genlmsg_unicast(&init_net, skb, listener_nlpid);
David Teigland3ae1acf2007-05-18 08:59:31 -050067}
68
69static int user_cmd(struct sk_buff *skb, struct genl_info *info)
70{
71 listener_nlpid = info->snd_pid;
72 printk("user_cmd nlpid %u\n", listener_nlpid);
73 return 0;
74}
75
76static struct genl_ops dlm_nl_ops = {
77 .cmd = DLM_CMD_HELLO,
78 .doit = user_cmd,
79};
80
Denis Cheng30727172008-02-02 01:53:46 +080081int __init dlm_netlink_init(void)
David Teigland3ae1acf2007-05-18 08:59:31 -050082{
83 int rv;
84
85 rv = genl_register_family(&family);
86 if (rv)
87 return rv;
88
89 rv = genl_register_ops(&family, &dlm_nl_ops);
90 if (rv < 0)
91 goto err;
92 return 0;
93 err:
94 genl_unregister_family(&family);
95 return rv;
96}
97
Leonardo Potenza88ad2312008-05-11 19:15:34 +020098void dlm_netlink_exit(void)
David Teigland3ae1acf2007-05-18 08:59:31 -050099{
100 genl_unregister_ops(&family, &dlm_nl_ops);
101 genl_unregister_family(&family);
102}
103
104static void fill_data(struct dlm_lock_data *data, struct dlm_lkb *lkb)
105{
106 struct dlm_rsb *r = lkb->lkb_resource;
David Teigland3ae1acf2007-05-18 08:59:31 -0500107
108 memset(data, 0, sizeof(struct dlm_lock_data));
109
110 data->version = DLM_LOCK_DATA_VERSION;
111 data->nodeid = lkb->lkb_nodeid;
112 data->ownpid = lkb->lkb_ownpid;
113 data->id = lkb->lkb_id;
114 data->remid = lkb->lkb_remid;
115 data->status = lkb->lkb_status;
116 data->grmode = lkb->lkb_grmode;
117 data->rqmode = lkb->lkb_rqmode;
David Teiglandd292c0c2008-02-06 23:27:04 -0600118 if (lkb->lkb_ua)
119 data->xid = lkb->lkb_ua->xid;
David Teigland3ae1acf2007-05-18 08:59:31 -0500120 if (r) {
121 data->lockspace_id = r->res_ls->ls_global_id;
122 data->resource_namelen = r->res_length;
123 memcpy(data->resource_name, r->res_name, r->res_length);
124 }
125}
126
127void dlm_timeout_warn(struct dlm_lkb *lkb)
128{
Ingo Molnar180b65d2008-11-25 16:51:45 -0800129 struct sk_buff *uninitialized_var(send_skb);
David Teigland3ae1acf2007-05-18 08:59:31 -0500130 struct dlm_lock_data *data;
David Teigland3ae1acf2007-05-18 08:59:31 -0500131 size_t size;
132 int rv;
133
David Teigland3ae1acf2007-05-18 08:59:31 -0500134 size = nla_total_size(sizeof(struct dlm_lock_data)) +
135 nla_total_size(0); /* why this? */
136
137 rv = prepare_data(DLM_CMD_TIMEOUT, &send_skb, size);
138 if (rv < 0)
139 return;
140
141 data = mk_data(send_skb);
142 if (!data) {
143 nlmsg_free(send_skb);
144 return;
145 }
146
147 fill_data(data, lkb);
148
149 send_data(send_skb);
150}
151