netlink.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include <net/genetlink.h>
  9. #include <linux/dlm.h>
  10. #include <linux/dlm_netlink.h>
  11. #include <linux/gfp.h>
  12. #include "dlm_internal.h"
  13. static uint32_t dlm_nl_seqnum;
  14. static uint32_t listener_nlportid;
  15. static struct genl_family family;
  16. static int prepare_data(u8 cmd, struct sk_buff **skbp, size_t size)
  17. {
  18. struct sk_buff *skb;
  19. void *data;
  20. skb = genlmsg_new(size, GFP_NOFS);
  21. if (!skb)
  22. return -ENOMEM;
  23. /* add the message headers */
  24. data = genlmsg_put(skb, 0, dlm_nl_seqnum++, &family, 0, cmd);
  25. if (!data) {
  26. nlmsg_free(skb);
  27. return -EINVAL;
  28. }
  29. *skbp = skb;
  30. return 0;
  31. }
  32. static struct dlm_lock_data *mk_data(struct sk_buff *skb)
  33. {
  34. struct nlattr *ret;
  35. ret = nla_reserve(skb, DLM_TYPE_LOCK, sizeof(struct dlm_lock_data));
  36. if (!ret)
  37. return NULL;
  38. return nla_data(ret);
  39. }
  40. static int send_data(struct sk_buff *skb)
  41. {
  42. struct genlmsghdr *genlhdr = nlmsg_data((struct nlmsghdr *)skb->data);
  43. void *data = genlmsg_data(genlhdr);
  44. genlmsg_end(skb, data);
  45. return genlmsg_unicast(&init_net, skb, listener_nlportid);
  46. }
  47. static int user_cmd(struct sk_buff *skb, struct genl_info *info)
  48. {
  49. listener_nlportid = info->snd_portid;
  50. printk("user_cmd nlpid %u\n", listener_nlportid);
  51. return 0;
  52. }
  53. static const struct genl_ops dlm_nl_ops[] = {
  54. {
  55. .cmd = DLM_CMD_HELLO,
  56. .doit = user_cmd,
  57. },
  58. };
  59. static struct genl_family family __ro_after_init = {
  60. .name = DLM_GENL_NAME,
  61. .version = DLM_GENL_VERSION,
  62. .ops = dlm_nl_ops,
  63. .n_ops = ARRAY_SIZE(dlm_nl_ops),
  64. .module = THIS_MODULE,
  65. };
  66. int __init dlm_netlink_init(void)
  67. {
  68. return genl_register_family(&family);
  69. }
  70. void dlm_netlink_exit(void)
  71. {
  72. genl_unregister_family(&family);
  73. }
  74. static void fill_data(struct dlm_lock_data *data, struct dlm_lkb *lkb)
  75. {
  76. struct dlm_rsb *r = lkb->lkb_resource;
  77. memset(data, 0, sizeof(struct dlm_lock_data));
  78. data->version = DLM_LOCK_DATA_VERSION;
  79. data->nodeid = lkb->lkb_nodeid;
  80. data->ownpid = lkb->lkb_ownpid;
  81. data->id = lkb->lkb_id;
  82. data->remid = lkb->lkb_remid;
  83. data->status = lkb->lkb_status;
  84. data->grmode = lkb->lkb_grmode;
  85. data->rqmode = lkb->lkb_rqmode;
  86. if (lkb->lkb_ua)
  87. data->xid = lkb->lkb_ua->xid;
  88. if (r) {
  89. data->lockspace_id = r->res_ls->ls_global_id;
  90. data->resource_namelen = r->res_length;
  91. memcpy(data->resource_name, r->res_name, r->res_length);
  92. }
  93. }
  94. void dlm_timeout_warn(struct dlm_lkb *lkb)
  95. {
  96. struct sk_buff *uninitialized_var(send_skb);
  97. struct dlm_lock_data *data;
  98. size_t size;
  99. int rv;
  100. size = nla_total_size(sizeof(struct dlm_lock_data)) +
  101. nla_total_size(0); /* why this? */
  102. rv = prepare_data(DLM_CMD_TIMEOUT, &send_skb, size);
  103. if (rv < 0)
  104. return;
  105. data = mk_data(send_skb);
  106. if (!data) {
  107. nlmsg_free(send_skb);
  108. return;
  109. }
  110. fill_data(data, lkb);
  111. send_data(send_skb);
  112. }