blob: e7cfbaf8d0e2ed66b404c259dcd64c2d4ccd5f54 [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
19static struct genl_family family = {
20 .id = GENL_ID_GENERATE,
21 .name = DLM_GENL_NAME,
22 .version = DLM_GENL_VERSION,
23};
24
25static int prepare_data(u8 cmd, struct sk_buff **skbp, size_t size)
26{
27 struct sk_buff *skb;
28 void *data;
29
David Teigland573c24c2009-11-30 16:34:43 -060030 skb = genlmsg_new(size, GFP_NOFS);
David Teigland3ae1acf2007-05-18 08:59:31 -050031 if (!skb)
32 return -ENOMEM;
33
34 /* add the message headers */
35 data = genlmsg_put(skb, 0, dlm_nl_seqnum++, &family, 0, cmd);
36 if (!data) {
37 nlmsg_free(skb);
38 return -EINVAL;
39 }
40
41 *skbp = skb;
42 return 0;
43}
44
45static struct dlm_lock_data *mk_data(struct sk_buff *skb)
46{
47 struct nlattr *ret;
48
49 ret = nla_reserve(skb, DLM_TYPE_LOCK, sizeof(struct dlm_lock_data));
50 if (!ret)
51 return NULL;
52 return nla_data(ret);
53}
54
55static int send_data(struct sk_buff *skb)
56{
57 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
58 void *data = genlmsg_data(genlhdr);
59 int rv;
60
61 rv = genlmsg_end(skb, data);
62 if (rv < 0) {
63 nlmsg_free(skb);
64 return rv;
65 }
66
Eric W. Biederman15e47302012-09-07 20:12:54 +000067 return genlmsg_unicast(&init_net, skb, listener_nlportid);
David Teigland3ae1acf2007-05-18 08:59:31 -050068}
69
70static int user_cmd(struct sk_buff *skb, struct genl_info *info)
71{
Eric W. Biederman15e47302012-09-07 20:12:54 +000072 listener_nlportid = info->snd_portid;
73 printk("user_cmd nlpid %u\n", listener_nlportid);
David Teigland3ae1acf2007-05-18 08:59:31 -050074 return 0;
75}
76
Johannes Bergc53ed742013-11-19 15:19:31 +010077static struct genl_ops dlm_nl_ops[] = {
78 {
79 .cmd = DLM_CMD_HELLO,
80 .doit = user_cmd,
81 },
David Teigland3ae1acf2007-05-18 08:59:31 -050082};
83
Denis Cheng30727172008-02-02 01:53:46 +080084int __init dlm_netlink_init(void)
David Teigland3ae1acf2007-05-18 08:59:31 -050085{
Johannes Bergc53ed742013-11-19 15:19:31 +010086 return genl_register_family_with_ops(&family, dlm_nl_ops);
David Teigland3ae1acf2007-05-18 08:59:31 -050087}
88
Leonardo Potenza88ad2312008-05-11 19:15:34 +020089void dlm_netlink_exit(void)
David Teigland3ae1acf2007-05-18 08:59:31 -050090{
David Teigland3ae1acf2007-05-18 08:59:31 -050091 genl_unregister_family(&family);
92}
93
94static void fill_data(struct dlm_lock_data *data, struct dlm_lkb *lkb)
95{
96 struct dlm_rsb *r = lkb->lkb_resource;
David Teigland3ae1acf2007-05-18 08:59:31 -050097
98 memset(data, 0, sizeof(struct dlm_lock_data));
99
100 data->version = DLM_LOCK_DATA_VERSION;
101 data->nodeid = lkb->lkb_nodeid;
102 data->ownpid = lkb->lkb_ownpid;
103 data->id = lkb->lkb_id;
104 data->remid = lkb->lkb_remid;
105 data->status = lkb->lkb_status;
106 data->grmode = lkb->lkb_grmode;
107 data->rqmode = lkb->lkb_rqmode;
David Teiglandd292c0c2008-02-06 23:27:04 -0600108 if (lkb->lkb_ua)
109 data->xid = lkb->lkb_ua->xid;
David Teigland3ae1acf2007-05-18 08:59:31 -0500110 if (r) {
111 data->lockspace_id = r->res_ls->ls_global_id;
112 data->resource_namelen = r->res_length;
113 memcpy(data->resource_name, r->res_name, r->res_length);
114 }
115}
116
117void dlm_timeout_warn(struct dlm_lkb *lkb)
118{
Ingo Molnar180b65d2008-11-25 16:51:45 -0800119 struct sk_buff *uninitialized_var(send_skb);
David Teigland3ae1acf2007-05-18 08:59:31 -0500120 struct dlm_lock_data *data;
David Teigland3ae1acf2007-05-18 08:59:31 -0500121 size_t size;
122 int rv;
123
David Teigland3ae1acf2007-05-18 08:59:31 -0500124 size = nla_total_size(sizeof(struct dlm_lock_data)) +
125 nla_total_size(0); /* why this? */
126
127 rv = prepare_data(DLM_CMD_TIMEOUT, &send_skb, size);
128 if (rv < 0)
129 return;
130
131 data = mk_data(send_skb);
132 if (!data) {
133 nlmsg_free(send_skb);
134 return;
135 }
136
137 fill_data(data, lkb);
138
139 send_data(send_skb);
140}
141