netlink.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <linux/cred.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/kernel.h>
  5. #include <linux/quotaops.h>
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include <net/netlink.h>
  9. #include <net/genetlink.h>
  10. /* Netlink family structure for quota */
  11. static struct genl_family quota_genl_family = {
  12. /*
  13. * Needed due to multicast group ID abuse - old code assumed
  14. * the family ID was also a valid multicast group ID (which
  15. * isn't true) and userspace might thus rely on it. Assign a
  16. * static ID for this group to make dealing with that easier.
  17. */
  18. .id = GENL_ID_VFS_DQUOT,
  19. .hdrsize = 0,
  20. .name = "VFS_DQUOT",
  21. .version = 1,
  22. .maxattr = QUOTA_NL_A_MAX,
  23. };
  24. static struct genl_multicast_group quota_mcgrp = {
  25. .name = "events",
  26. };
  27. /**
  28. * quota_send_warning - Send warning to userspace about exceeded quota
  29. * @type: The quota type: USRQQUOTA, GRPQUOTA,...
  30. * @id: The user or group id of the quota that was exceeded
  31. * @dev: The device on which the fs is mounted (sb->s_dev)
  32. * @warntype: The type of the warning: QUOTA_NL_...
  33. *
  34. * This can be used by filesystems (including those which don't use
  35. * dquot) to send a message to userspace relating to quota limits.
  36. *
  37. */
  38. void quota_send_warning(struct kqid qid, dev_t dev,
  39. const char warntype)
  40. {
  41. static atomic_t seq;
  42. struct sk_buff *skb;
  43. void *msg_head;
  44. int ret;
  45. int msg_size = 4 * nla_total_size(sizeof(u32)) +
  46. 2 * nla_total_size(sizeof(u64));
  47. /* We have to allocate using GFP_NOFS as we are called from a
  48. * filesystem performing write and thus further recursion into
  49. * the fs to free some data could cause deadlocks. */
  50. skb = genlmsg_new(msg_size, GFP_NOFS);
  51. if (!skb) {
  52. printk(KERN_ERR
  53. "VFS: Not enough memory to send quota warning.\n");
  54. return;
  55. }
  56. msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
  57. &quota_genl_family, 0, QUOTA_NL_C_WARNING);
  58. if (!msg_head) {
  59. printk(KERN_ERR
  60. "VFS: Cannot store netlink header in quota warning.\n");
  61. goto err_out;
  62. }
  63. ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, qid.type);
  64. if (ret)
  65. goto attr_err_out;
  66. ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID,
  67. from_kqid_munged(&init_user_ns, qid));
  68. if (ret)
  69. goto attr_err_out;
  70. ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
  71. if (ret)
  72. goto attr_err_out;
  73. ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
  74. if (ret)
  75. goto attr_err_out;
  76. ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
  77. if (ret)
  78. goto attr_err_out;
  79. ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID,
  80. from_kuid_munged(&init_user_ns, current_uid()));
  81. if (ret)
  82. goto attr_err_out;
  83. genlmsg_end(skb, msg_head);
  84. genlmsg_multicast(&quota_genl_family, skb, 0, quota_mcgrp.id, GFP_NOFS);
  85. return;
  86. attr_err_out:
  87. printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
  88. err_out:
  89. kfree_skb(skb);
  90. }
  91. EXPORT_SYMBOL(quota_send_warning);
  92. static int __init quota_init(void)
  93. {
  94. if (genl_register_family(&quota_genl_family) != 0)
  95. printk(KERN_ERR
  96. "VFS: Failed to create quota netlink interface.\n");
  97. if (genl_register_mc_group(&quota_genl_family, &quota_mcgrp))
  98. printk(KERN_ERR
  99. "VFS: Failed to register quota mcast group.\n");
  100. return 0;
  101. };
  102. module_init(quota_init);