netlink.c 2.6 KB

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