Linux 2.6.35-rc2
[linux/fpc-iii.git] / fs / dlm / netlink.c
blob2c6ad518100d509d2d0013ff466370a585559986
1 /*
2 * Copyright (C) 2007 Red Hat, Inc. All rights reserved.
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 */
9 #include <net/genetlink.h>
10 #include <linux/dlm.h>
11 #include <linux/dlm_netlink.h>
12 #include <linux/gfp.h>
14 #include "dlm_internal.h"
16 static uint32_t dlm_nl_seqnum;
17 static uint32_t listener_nlpid;
19 static struct genl_family family = {
20 .id = GENL_ID_GENERATE,
21 .name = DLM_GENL_NAME,
22 .version = DLM_GENL_VERSION,
25 static int prepare_data(u8 cmd, struct sk_buff **skbp, size_t size)
27 struct sk_buff *skb;
28 void *data;
30 skb = genlmsg_new(size, GFP_NOFS);
31 if (!skb)
32 return -ENOMEM;
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;
41 *skbp = skb;
42 return 0;
45 static struct dlm_lock_data *mk_data(struct sk_buff *skb)
47 struct nlattr *ret;
49 ret = nla_reserve(skb, DLM_TYPE_LOCK, sizeof(struct dlm_lock_data));
50 if (!ret)
51 return NULL;
52 return nla_data(ret);
55 static int send_data(struct sk_buff *skb)
57 struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
58 void *data = genlmsg_data(genlhdr);
59 int rv;
61 rv = genlmsg_end(skb, data);
62 if (rv < 0) {
63 nlmsg_free(skb);
64 return rv;
67 return genlmsg_unicast(&init_net, skb, listener_nlpid);
70 static int user_cmd(struct sk_buff *skb, struct genl_info *info)
72 listener_nlpid = info->snd_pid;
73 printk("user_cmd nlpid %u\n", listener_nlpid);
74 return 0;
77 static struct genl_ops dlm_nl_ops = {
78 .cmd = DLM_CMD_HELLO,
79 .doit = user_cmd,
82 int __init dlm_netlink_init(void)
84 int rv;
86 rv = genl_register_family(&family);
87 if (rv)
88 return rv;
90 rv = genl_register_ops(&family, &dlm_nl_ops);
91 if (rv < 0)
92 goto err;
93 return 0;
94 err:
95 genl_unregister_family(&family);
96 return rv;
99 void dlm_netlink_exit(void)
101 genl_unregister_ops(&family, &dlm_nl_ops);
102 genl_unregister_family(&family);
105 static void fill_data(struct dlm_lock_data *data, struct dlm_lkb *lkb)
107 struct dlm_rsb *r = lkb->lkb_resource;
109 memset(data, 0, sizeof(struct dlm_lock_data));
111 data->version = DLM_LOCK_DATA_VERSION;
112 data->nodeid = lkb->lkb_nodeid;
113 data->ownpid = lkb->lkb_ownpid;
114 data->id = lkb->lkb_id;
115 data->remid = lkb->lkb_remid;
116 data->status = lkb->lkb_status;
117 data->grmode = lkb->lkb_grmode;
118 data->rqmode = lkb->lkb_rqmode;
119 if (lkb->lkb_ua)
120 data->xid = lkb->lkb_ua->xid;
121 if (r) {
122 data->lockspace_id = r->res_ls->ls_global_id;
123 data->resource_namelen = r->res_length;
124 memcpy(data->resource_name, r->res_name, r->res_length);
128 void dlm_timeout_warn(struct dlm_lkb *lkb)
130 struct sk_buff *uninitialized_var(send_skb);
131 struct dlm_lock_data *data;
132 size_t size;
133 int rv;
135 size = nla_total_size(sizeof(struct dlm_lock_data)) +
136 nla_total_size(0); /* why this? */
138 rv = prepare_data(DLM_CMD_TIMEOUT, &send_skb, size);
139 if (rv < 0)
140 return;
142 data = mk_data(send_skb);
143 if (!data) {
144 nlmsg_free(send_skb);
145 return;
148 fill_data(data, lkb);
150 send_data(send_skb);