netlink.c 2.8 KB

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