netlink.c 2.8 KB

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