blob: e99b1a72d9a7348abd9d59a37d661ec335bfe73b [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 */
Johannes Berg56989f62016-10-24 14:40:05 +020015static struct genl_family quota_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +020016 .module = THIS_MODULE,
Christoph Hellwig799a9d42010-02-16 03:44:54 -050017 .hdrsize = 0,
18 .name = "VFS_DQUOT",
19 .version = 1,
20 .maxattr = QUOTA_NL_A_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +010021 .mcgrps = quota_mcgrps,
22 .n_mcgrps = ARRAY_SIZE(quota_mcgrps),
Johannes Berg2ecf7532013-11-19 15:19:33 +010023};
24
Christoph Hellwig799a9d42010-02-16 03:44:54 -050025/**
26 * quota_send_warning - Send warning to userspace about exceeded quota
Fabian Frederick2c15ac52014-07-04 21:44:25 +020027 * @qid: The kernel internal quota identifier.
Christoph Hellwig799a9d42010-02-16 03:44:54 -050028 * @dev: The device on which the fs is mounted (sb->s_dev)
29 * @warntype: The type of the warning: QUOTA_NL_...
30 *
31 * This can be used by filesystems (including those which don't use
32 * dquot) to send a message to userspace relating to quota limits.
33 *
34 */
35
Eric W. Biederman431f1972012-09-16 02:32:43 -070036void quota_send_warning(struct kqid qid, dev_t dev,
Christoph Hellwig799a9d42010-02-16 03:44:54 -050037 const char warntype)
38{
39 static atomic_t seq;
40 struct sk_buff *skb;
41 void *msg_head;
42 int ret;
43 int msg_size = 4 * nla_total_size(sizeof(u32)) +
Nicolas Dichtel3c6f3712016-04-26 10:06:13 +020044 2 * nla_total_size_64bit(sizeof(u64));
Christoph Hellwig799a9d42010-02-16 03:44:54 -050045
46 /* We have to allocate using GFP_NOFS as we are called from a
47 * filesystem performing write and thus further recursion into
48 * the fs to free some data could cause deadlocks. */
49 skb = genlmsg_new(msg_size, GFP_NOFS);
50 if (!skb) {
51 printk(KERN_ERR
52 "VFS: Not enough memory to send quota warning.\n");
53 return;
54 }
55 msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
56 &quota_genl_family, 0, QUOTA_NL_C_WARNING);
57 if (!msg_head) {
58 printk(KERN_ERR
59 "VFS: Cannot store netlink header in quota warning.\n");
60 goto err_out;
61 }
Eric W. Biederman431f1972012-09-16 02:32:43 -070062 ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, qid.type);
Christoph Hellwig799a9d42010-02-16 03:44:54 -050063 if (ret)
64 goto attr_err_out;
Nicolas Dichtel3c6f3712016-04-26 10:06:13 +020065 ret = nla_put_u64_64bit(skb, QUOTA_NL_A_EXCESS_ID,
66 from_kqid_munged(&init_user_ns, qid),
67 QUOTA_NL_A_PAD);
Christoph Hellwig799a9d42010-02-16 03:44:54 -050068 if (ret)
69 goto attr_err_out;
70 ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
71 if (ret)
72 goto attr_err_out;
73 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
74 if (ret)
75 goto attr_err_out;
76 ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
77 if (ret)
78 goto attr_err_out;
Nicolas Dichtel3c6f3712016-04-26 10:06:13 +020079 ret = nla_put_u64_64bit(skb, QUOTA_NL_A_CAUSED_ID,
80 from_kuid_munged(&init_user_ns, current_uid()),
81 QUOTA_NL_A_PAD);
Christoph Hellwig799a9d42010-02-16 03:44:54 -050082 if (ret)
83 goto attr_err_out;
84 genlmsg_end(skb, msg_head);
85
Johannes Berg2a94fe42013-11-19 15:19:39 +010086 genlmsg_multicast(&quota_genl_family, skb, 0, 0, GFP_NOFS);
Christoph Hellwig799a9d42010-02-16 03:44:54 -050087 return;
88attr_err_out:
89 printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
90err_out:
91 kfree_skb(skb);
92}
93EXPORT_SYMBOL(quota_send_warning);
94
95static int __init quota_init(void)
96{
97 if (genl_register_family(&quota_genl_family) != 0)
98 printk(KERN_ERR
99 "VFS: Failed to create quota netlink interface.\n");
Christoph Hellwig799a9d42010-02-16 03:44:54 -0500100 return 0;
101};
Paul Gortmaker7da54462015-12-12 16:30:04 -0500102fs_initcall(quota_init);