blob: 8b252673d4540b519e4a46096869ff039246e614 [file] [log] [blame]
Christoph Hellwig799a9d42010-02-16 03:44:54 -05001#include <linux/cred.h>
2#include <linux/init.h>
Christoph Hellwig799a9d42010-02-16 03:44:54 -05003#include <linux/kernel.h>
4#include <linux/quotaops.h>
5#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09006#include <linux/slab.h>
Christoph Hellwig799a9d42010-02-16 03:44:54 -05007#include <net/netlink.h>
8#include <net/genetlink.h>
9
Johannes Berg2a94fe42013-11-19 15:19:39 +010010static const struct genl_multicast_group quota_mcgrps[] = {
11 { .name = "events", },
12};
13
Christoph Hellwig799a9d42010-02-16 03:44:54 -050014/* Netlink family structure for quota */
15static struct genl_family quota_genl_family = {
Johannes Berg2ecf7532013-11-19 15:19:33 +010016 /*
17 * Needed due to multicast group ID abuse - old code assumed
18 * the family ID was also a valid multicast group ID (which
19 * isn't true) and userspace might thus rely on it. Assign a
20 * static ID for this group to make dealing with that easier.
21 */
22 .id = GENL_ID_VFS_DQUOT,
Christoph Hellwig799a9d42010-02-16 03:44:54 -050023 .hdrsize = 0,
24 .name = "VFS_DQUOT",
25 .version = 1,
26 .maxattr = QUOTA_NL_A_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +010027 .mcgrps = quota_mcgrps,
28 .n_mcgrps = ARRAY_SIZE(quota_mcgrps),
Johannes Berg2ecf7532013-11-19 15:19:33 +010029};
30
Christoph Hellwig799a9d42010-02-16 03:44:54 -050031/**
32 * quota_send_warning - Send warning to userspace about exceeded quota
Fabian Frederick2c15ac52014-07-04 21:44:25 +020033 * @qid: The kernel internal quota identifier.
Christoph Hellwig799a9d42010-02-16 03:44:54 -050034 * @dev: The device on which the fs is mounted (sb->s_dev)
35 * @warntype: The type of the warning: QUOTA_NL_...
36 *
37 * This can be used by filesystems (including those which don't use
38 * dquot) to send a message to userspace relating to quota limits.
39 *
40 */
41
Eric W. Biederman431f1972012-09-16 02:32:43 -070042void quota_send_warning(struct kqid qid, dev_t dev,
Christoph Hellwig799a9d42010-02-16 03:44:54 -050043 const char warntype)
44{
45 static atomic_t seq;
46 struct sk_buff *skb;
47 void *msg_head;
48 int ret;
49 int msg_size = 4 * nla_total_size(sizeof(u32)) +
Nicolas Dichtel3c6f3712016-04-26 10:06:13 +020050 2 * nla_total_size_64bit(sizeof(u64));
Christoph Hellwig799a9d42010-02-16 03:44:54 -050051
52 /* We have to allocate using GFP_NOFS as we are called from a
53 * filesystem performing write and thus further recursion into
54 * the fs to free some data could cause deadlocks. */
55 skb = genlmsg_new(msg_size, GFP_NOFS);
56 if (!skb) {
57 printk(KERN_ERR
58 "VFS: Not enough memory to send quota warning.\n");
59 return;
60 }
61 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
62 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
63 if (!msg_head) {
64 printk(KERN_ERR
65 "VFS: Cannot store netlink header in quota warning.\n");
66 goto err_out;
67 }
Eric W. Biederman431f1972012-09-16 02:32:43 -070068 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, qid.type);
Christoph Hellwig799a9d42010-02-16 03:44:54 -050069 if (ret)
70 goto attr_err_out;
Nicolas Dichtel3c6f3712016-04-26 10:06:13 +020071 ret = nla_put_u64_64bit(skb, QUOTA_NL_A_EXCESS_ID,
72 from_kqid_munged(&init_user_ns, qid),
73 QUOTA_NL_A_PAD);
Christoph Hellwig799a9d42010-02-16 03:44:54 -050074 if (ret)
75 goto attr_err_out;
76 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
77 if (ret)
78 goto attr_err_out;
79 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
80 if (ret)
81 goto attr_err_out;
82 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
83 if (ret)
84 goto attr_err_out;
Nicolas Dichtel3c6f3712016-04-26 10:06:13 +020085 ret = nla_put_u64_64bit(skb, QUOTA_NL_A_CAUSED_ID,
86 from_kuid_munged(&init_user_ns, current_uid()),
87 QUOTA_NL_A_PAD);
Christoph Hellwig799a9d42010-02-16 03:44:54 -050088 if (ret)
89 goto attr_err_out;
90 genlmsg_end(skb, msg_head);
91
Johannes Berg2a94fe42013-11-19 15:19:39 +010092 genlmsg_multicast(&quota_genl_family, skb, 0, 0, GFP_NOFS);
Christoph Hellwig799a9d42010-02-16 03:44:54 -050093 return;
94attr_err_out:
95 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
96err_out:
97 kfree_skb(skb);
98}
99EXPORT_SYMBOL(quota_send_warning);
100
101static int __init quota_init(void)
102{
103 if (genl_register_family(&quota_genl_family) != 0)
104 printk(KERN_ERR
105 "VFS: Failed to create quota netlink interface.\n");
Christoph Hellwig799a9d42010-02-16 03:44:54 -0500106 return 0;
107};
Paul Gortmaker7da54462015-12-12 16:30:04 -0500108fs_initcall(quota_init);