xref: /linux/fs/quota/netlink.c (revision 56989f6d8568c21257dcec0f5e644d5570ba3281)
1799a9d44SChristoph Hellwig #include <linux/cred.h>
2799a9d44SChristoph Hellwig #include <linux/init.h>
3799a9d44SChristoph Hellwig #include <linux/kernel.h>
4799a9d44SChristoph Hellwig #include <linux/quotaops.h>
5799a9d44SChristoph Hellwig #include <linux/sched.h>
65a0e3ad6STejun Heo #include <linux/slab.h>
7799a9d44SChristoph Hellwig #include <net/netlink.h>
8799a9d44SChristoph Hellwig #include <net/genetlink.h>
9799a9d44SChristoph Hellwig 
102a94fe48SJohannes Berg static const struct genl_multicast_group quota_mcgrps[] = {
112a94fe48SJohannes Berg 	{ .name = "events", },
122a94fe48SJohannes Berg };
132a94fe48SJohannes Berg 
14799a9d44SChristoph Hellwig /* Netlink family structure for quota */
15*56989f6dSJohannes Berg static struct genl_family quota_genl_family __ro_after_init = {
16489111e5SJohannes Berg 	.module = THIS_MODULE,
17799a9d44SChristoph Hellwig 	.hdrsize = 0,
18799a9d44SChristoph Hellwig 	.name = "VFS_DQUOT",
19799a9d44SChristoph Hellwig 	.version = 1,
20799a9d44SChristoph Hellwig 	.maxattr = QUOTA_NL_A_MAX,
212a94fe48SJohannes Berg 	.mcgrps = quota_mcgrps,
222a94fe48SJohannes Berg 	.n_mcgrps = ARRAY_SIZE(quota_mcgrps),
232ecf7536SJohannes Berg };
242ecf7536SJohannes Berg 
25799a9d44SChristoph Hellwig /**
26799a9d44SChristoph Hellwig  * quota_send_warning - Send warning to userspace about exceeded quota
272c15ac5bSFabian Frederick  * @qid: The kernel internal quota identifier.
28799a9d44SChristoph Hellwig  * @dev: The device on which the fs is mounted (sb->s_dev)
29799a9d44SChristoph Hellwig  * @warntype: The type of the warning: QUOTA_NL_...
30799a9d44SChristoph Hellwig  *
31799a9d44SChristoph Hellwig  * This can be used by filesystems (including those which don't use
32799a9d44SChristoph Hellwig  * dquot) to send a message to userspace relating to quota limits.
33799a9d44SChristoph Hellwig  *
34799a9d44SChristoph Hellwig  */
35799a9d44SChristoph Hellwig 
36431f1974SEric W. Biederman void quota_send_warning(struct kqid qid, dev_t dev,
37799a9d44SChristoph Hellwig 			const char warntype)
38799a9d44SChristoph Hellwig {
39799a9d44SChristoph Hellwig 	static atomic_t seq;
40799a9d44SChristoph Hellwig 	struct sk_buff *skb;
41799a9d44SChristoph Hellwig 	void *msg_head;
42799a9d44SChristoph Hellwig 	int ret;
43799a9d44SChristoph Hellwig 	int msg_size = 4 * nla_total_size(sizeof(u32)) +
443c6f3714SNicolas Dichtel 		       2 * nla_total_size_64bit(sizeof(u64));
45799a9d44SChristoph Hellwig 
46799a9d44SChristoph Hellwig 	/* We have to allocate using GFP_NOFS as we are called from a
47799a9d44SChristoph Hellwig 	 * filesystem performing write and thus further recursion into
48799a9d44SChristoph Hellwig 	 * the fs to free some data could cause deadlocks. */
49799a9d44SChristoph Hellwig 	skb = genlmsg_new(msg_size, GFP_NOFS);
50799a9d44SChristoph Hellwig 	if (!skb) {
51799a9d44SChristoph Hellwig 		printk(KERN_ERR
52799a9d44SChristoph Hellwig 		  "VFS: Not enough memory to send quota warning.\n");
53799a9d44SChristoph Hellwig 		return;
54799a9d44SChristoph Hellwig 	}
55799a9d44SChristoph Hellwig 	msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
56799a9d44SChristoph Hellwig 			&quota_genl_family, 0, QUOTA_NL_C_WARNING);
57799a9d44SChristoph Hellwig 	if (!msg_head) {
58799a9d44SChristoph Hellwig 		printk(KERN_ERR
59799a9d44SChristoph Hellwig 		  "VFS: Cannot store netlink header in quota warning.\n");
60799a9d44SChristoph Hellwig 		goto err_out;
61799a9d44SChristoph Hellwig 	}
62431f1974SEric W. Biederman 	ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, qid.type);
63799a9d44SChristoph Hellwig 	if (ret)
64799a9d44SChristoph Hellwig 		goto attr_err_out;
653c6f3714SNicolas Dichtel 	ret = nla_put_u64_64bit(skb, QUOTA_NL_A_EXCESS_ID,
663c6f3714SNicolas Dichtel 				from_kqid_munged(&init_user_ns, qid),
673c6f3714SNicolas Dichtel 				QUOTA_NL_A_PAD);
68799a9d44SChristoph Hellwig 	if (ret)
69799a9d44SChristoph Hellwig 		goto attr_err_out;
70799a9d44SChristoph Hellwig 	ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
71799a9d44SChristoph Hellwig 	if (ret)
72799a9d44SChristoph Hellwig 		goto attr_err_out;
73799a9d44SChristoph Hellwig 	ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
74799a9d44SChristoph Hellwig 	if (ret)
75799a9d44SChristoph Hellwig 		goto attr_err_out;
76799a9d44SChristoph Hellwig 	ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
77799a9d44SChristoph Hellwig 	if (ret)
78799a9d44SChristoph Hellwig 		goto attr_err_out;
793c6f3714SNicolas Dichtel 	ret = nla_put_u64_64bit(skb, QUOTA_NL_A_CAUSED_ID,
803c6f3714SNicolas Dichtel 				from_kuid_munged(&init_user_ns, current_uid()),
813c6f3714SNicolas Dichtel 				QUOTA_NL_A_PAD);
82799a9d44SChristoph Hellwig 	if (ret)
83799a9d44SChristoph Hellwig 		goto attr_err_out;
84799a9d44SChristoph Hellwig 	genlmsg_end(skb, msg_head);
85799a9d44SChristoph Hellwig 
862a94fe48SJohannes Berg 	genlmsg_multicast(&quota_genl_family, skb, 0, 0, GFP_NOFS);
87799a9d44SChristoph Hellwig 	return;
88799a9d44SChristoph Hellwig attr_err_out:
89799a9d44SChristoph Hellwig 	printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
90799a9d44SChristoph Hellwig err_out:
91799a9d44SChristoph Hellwig 	kfree_skb(skb);
92799a9d44SChristoph Hellwig }
93799a9d44SChristoph Hellwig EXPORT_SYMBOL(quota_send_warning);
94799a9d44SChristoph Hellwig 
95799a9d44SChristoph Hellwig static int __init quota_init(void)
96799a9d44SChristoph Hellwig {
97799a9d44SChristoph Hellwig 	if (genl_register_family(&quota_genl_family) != 0)
98799a9d44SChristoph Hellwig 		printk(KERN_ERR
99799a9d44SChristoph Hellwig 		       "VFS: Failed to create quota netlink interface.\n");
100799a9d44SChristoph Hellwig 	return 0;
101799a9d44SChristoph Hellwig };
1027da54463SPaul Gortmaker fs_initcall(quota_init);
103