netlink.c 2.8 KB

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