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>
11 static const struct genl_multicast_group quota_mcgrps[] = {
12 { .name = "events", },
15 /* Netlink family structure for quota */
16 static struct genl_family quota_genl_family __ro_after_init = {
17 .module = THIS_MODULE,
21 .maxattr = QUOTA_NL_A_MAX,
22 .mcgrps = quota_mcgrps,
23 .n_mcgrps = ARRAY_SIZE(quota_mcgrps),
27 * quota_send_warning - Send warning to userspace about exceeded quota
28 * @qid: The kernel internal quota identifier.
29 * @dev: The device on which the fs is mounted (sb->s_dev)
30 * @warntype: The type of the warning: QUOTA_NL_...
32 * This can be used by filesystems (including those which don't use
33 * dquot) to send a message to userspace relating to quota limits.
37 void quota_send_warning(struct kqid qid, dev_t dev,
44 int msg_size = 4 * nla_total_size(sizeof(u32)) +
45 2 * nla_total_size_64bit(sizeof(u64));
47 /* We have to allocate using GFP_NOFS as we are called from a
48 * filesystem performing write and thus further recursion into
49 * the fs to free some data could cause deadlocks. */
50 skb = genlmsg_new(msg_size, GFP_NOFS);
53 "VFS: Not enough memory to send quota warning.\n");
56 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
57 "a_genl_family, 0, QUOTA_NL_C_WARNING);
60 "VFS: Cannot store netlink header in quota warning.\n");
63 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, qid.type);
66 ret = nla_put_u64_64bit(skb, QUOTA_NL_A_EXCESS_ID,
67 from_kqid_munged(&init_user_ns, qid),
71 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
74 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
77 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
80 ret = nla_put_u64_64bit(skb, QUOTA_NL_A_CAUSED_ID,
81 from_kuid_munged(&init_user_ns, current_uid()),
85 genlmsg_end(skb, msg_head);
87 genlmsg_multicast("a_genl_family, skb, 0, 0, GFP_NOFS);
90 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
94 EXPORT_SYMBOL(quota_send_warning);
96 static int __init quota_init(void)
98 if (genl_register_family("a_genl_family) != 0)
100 "VFS: Failed to create quota netlink interface.\n");
103 fs_initcall(quota_init);