David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/gfp.h> |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 13 | |
| 14 | #include "dlm_internal.h" |
| 15 | |
| 16 | static uint32_t dlm_nl_seqnum; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 17 | static uint32_t listener_nlportid; |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 18 | |
| 19 | static struct genl_family family = { |
| 20 | .id = GENL_ID_GENERATE, |
| 21 | .name = DLM_GENL_NAME, |
| 22 | .version = DLM_GENL_VERSION, |
| 23 | }; |
| 24 | |
| 25 | static int prepare_data(u8 cmd, struct sk_buff **skbp, size_t size) |
| 26 | { |
| 27 | struct sk_buff *skb; |
| 28 | void *data; |
| 29 | |
David Teigland | 573c24c | 2009-11-30 16:34:43 -0600 | [diff] [blame] | 30 | skb = genlmsg_new(size, GFP_NOFS); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 31 | 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 | |
| 45 | static 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 | |
| 55 | static int send_data(struct sk_buff *skb) |
| 56 | { |
| 57 | struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data); |
| 58 | void *data = genlmsg_data(genlhdr); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 59 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame^] | 60 | genlmsg_end(skb, data); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 61 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 62 | return genlmsg_unicast(&init_net, skb, listener_nlportid); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static int user_cmd(struct sk_buff *skb, struct genl_info *info) |
| 66 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 67 | listener_nlportid = info->snd_portid; |
| 68 | printk("user_cmd nlpid %u\n", listener_nlportid); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
Johannes Berg | c53ed74 | 2013-11-19 15:19:31 +0100 | [diff] [blame] | 72 | static struct genl_ops dlm_nl_ops[] = { |
| 73 | { |
| 74 | .cmd = DLM_CMD_HELLO, |
| 75 | .doit = user_cmd, |
| 76 | }, |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 77 | }; |
| 78 | |
Denis Cheng | 3072717 | 2008-02-02 01:53:46 +0800 | [diff] [blame] | 79 | int __init dlm_netlink_init(void) |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 80 | { |
Johannes Berg | c53ed74 | 2013-11-19 15:19:31 +0100 | [diff] [blame] | 81 | return genl_register_family_with_ops(&family, dlm_nl_ops); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Leonardo Potenza | 88ad231 | 2008-05-11 19:15:34 +0200 | [diff] [blame] | 84 | void dlm_netlink_exit(void) |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 85 | { |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 86 | genl_unregister_family(&family); |
| 87 | } |
| 88 | |
| 89 | static void fill_data(struct dlm_lock_data *data, struct dlm_lkb *lkb) |
| 90 | { |
| 91 | struct dlm_rsb *r = lkb->lkb_resource; |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 92 | |
| 93 | memset(data, 0, sizeof(struct dlm_lock_data)); |
| 94 | |
| 95 | data->version = DLM_LOCK_DATA_VERSION; |
| 96 | data->nodeid = lkb->lkb_nodeid; |
| 97 | data->ownpid = lkb->lkb_ownpid; |
| 98 | data->id = lkb->lkb_id; |
| 99 | data->remid = lkb->lkb_remid; |
| 100 | data->status = lkb->lkb_status; |
| 101 | data->grmode = lkb->lkb_grmode; |
| 102 | data->rqmode = lkb->lkb_rqmode; |
David Teigland | d292c0c | 2008-02-06 23:27:04 -0600 | [diff] [blame] | 103 | if (lkb->lkb_ua) |
| 104 | data->xid = lkb->lkb_ua->xid; |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 105 | if (r) { |
| 106 | data->lockspace_id = r->res_ls->ls_global_id; |
| 107 | data->resource_namelen = r->res_length; |
| 108 | memcpy(data->resource_name, r->res_name, r->res_length); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void dlm_timeout_warn(struct dlm_lkb *lkb) |
| 113 | { |
Ingo Molnar | 180b65d | 2008-11-25 16:51:45 -0800 | [diff] [blame] | 114 | struct sk_buff *uninitialized_var(send_skb); |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 115 | struct dlm_lock_data *data; |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 116 | size_t size; |
| 117 | int rv; |
| 118 | |
David Teigland | 3ae1acf | 2007-05-18 08:59:31 -0500 | [diff] [blame] | 119 | size = nla_total_size(sizeof(struct dlm_lock_data)) + |
| 120 | nla_total_size(0); /* why this? */ |
| 121 | |
| 122 | rv = prepare_data(DLM_CMD_TIMEOUT, &send_skb, size); |
| 123 | if (rv < 0) |
| 124 | return; |
| 125 | |
| 126 | data = mk_data(send_skb); |
| 127 | if (!data) { |
| 128 | nlmsg_free(send_skb); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | fill_data(data, lkb); |
| 133 | |
| 134 | send_data(send_skb); |
| 135 | } |
| 136 | |