netlink.c 3.0 KB

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