Commit | Line | Data |
---|---|---|
799a9d44 CH |
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> | |
5a0e3ad6 | 8 | #include <linux/slab.h> |
799a9d44 CH |
9 | #include <net/netlink.h> |
10 | #include <net/genetlink.h> | |
11 | ||
12 | /* Netlink family structure for quota */ | |
13 | static struct genl_family quota_genl_family = { | |
2ecf7536 JB |
14 | /* |
15 | * Needed due to multicast group ID abuse - old code assumed | |
16 | * the family ID was also a valid multicast group ID (which | |
17 | * isn't true) and userspace might thus rely on it. Assign a | |
18 | * static ID for this group to make dealing with that easier. | |
19 | */ | |
20 | .id = GENL_ID_VFS_DQUOT, | |
799a9d44 CH |
21 | .hdrsize = 0, |
22 | .name = "VFS_DQUOT", | |
23 | .version = 1, | |
24 | .maxattr = QUOTA_NL_A_MAX, | |
25 | }; | |
26 | ||
2ecf7536 JB |
27 | static struct genl_multicast_group quota_mcgrp = { |
28 | .name = "events", | |
29 | }; | |
30 | ||
799a9d44 CH |
31 | /** |
32 | * quota_send_warning - Send warning to userspace about exceeded quota | |
33 | * @type: The quota type: USRQQUOTA, GRPQUOTA,... | |
34 | * @id: The user or group id of the quota that was exceeded | |
35 | * @dev: The device on which the fs is mounted (sb->s_dev) | |
36 | * @warntype: The type of the warning: QUOTA_NL_... | |
37 | * | |
38 | * This can be used by filesystems (including those which don't use | |
39 | * dquot) to send a message to userspace relating to quota limits. | |
40 | * | |
41 | */ | |
42 | ||
431f1974 | 43 | void quota_send_warning(struct kqid qid, dev_t dev, |
799a9d44 CH |
44 | const char warntype) |
45 | { | |
46 | static atomic_t seq; | |
47 | struct sk_buff *skb; | |
48 | void *msg_head; | |
49 | int ret; | |
50 | int msg_size = 4 * nla_total_size(sizeof(u32)) + | |
51 | 2 * nla_total_size(sizeof(u64)); | |
52 | ||
53 | /* We have to allocate using GFP_NOFS as we are called from a | |
54 | * filesystem performing write and thus further recursion into | |
55 | * the fs to free some data could cause deadlocks. */ | |
56 | skb = genlmsg_new(msg_size, GFP_NOFS); | |
57 | if (!skb) { | |
58 | printk(KERN_ERR | |
59 | "VFS: Not enough memory to send quota warning.\n"); | |
60 | return; | |
61 | } | |
62 | msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq), | |
63 | "a_genl_family, 0, QUOTA_NL_C_WARNING); | |
64 | if (!msg_head) { | |
65 | printk(KERN_ERR | |
66 | "VFS: Cannot store netlink header in quota warning.\n"); | |
67 | goto err_out; | |
68 | } | |
431f1974 | 69 | ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, qid.type); |
799a9d44 CH |
70 | if (ret) |
71 | goto attr_err_out; | |
431f1974 EB |
72 | ret = nla_put_u64(skb, QUOTA_NL_A_EXCESS_ID, |
73 | from_kqid_munged(&init_user_ns, qid)); | |
799a9d44 CH |
74 | 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; | |
431f1974 EB |
85 | ret = nla_put_u64(skb, QUOTA_NL_A_CAUSED_ID, |
86 | from_kuid_munged(&init_user_ns, current_uid())); | |
799a9d44 CH |
87 | if (ret) |
88 | goto attr_err_out; | |
89 | genlmsg_end(skb, msg_head); | |
90 | ||
2ecf7536 | 91 | genlmsg_multicast(skb, 0, quota_mcgrp.id, GFP_NOFS); |
799a9d44 CH |
92 | return; |
93 | attr_err_out: | |
94 | printk(KERN_ERR "VFS: Not enough space to compose quota message!\n"); | |
95 | err_out: | |
96 | kfree_skb(skb); | |
97 | } | |
98 | EXPORT_SYMBOL(quota_send_warning); | |
99 | ||
100 | static int __init quota_init(void) | |
101 | { | |
102 | if (genl_register_family("a_genl_family) != 0) | |
103 | printk(KERN_ERR | |
104 | "VFS: Failed to create quota netlink interface.\n"); | |
2ecf7536 JB |
105 | if (genl_register_mc_group("a_genl_family, "a_mcgrp)) |
106 | printk(KERN_ERR | |
107 | "VFS: Failed to register quota mcast group.\n"); | |
799a9d44 CH |
108 | return 0; |
109 | }; | |
110 | ||
111 | module_init(quota_init); |