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