Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 2 | #include <linux/cred.h> |
| 3 | #include <linux/init.h> |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 4 | #include <linux/kernel.h> |
| 5 | #include <linux/quotaops.h> |
| 6 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 8 | #include <net/netlink.h> |
| 9 | #include <net/genetlink.h> |
| 10 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 11 | static const struct genl_multicast_group quota_mcgrps[] = { |
| 12 | { .name = "events", }, |
| 13 | }; |
| 14 | |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 15 | /* Netlink family structure for quota */ |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 16 | static struct genl_family quota_genl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 17 | .module = THIS_MODULE, |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 18 | .hdrsize = 0, |
| 19 | .name = "VFS_DQUOT", |
| 20 | .version = 1, |
| 21 | .maxattr = QUOTA_NL_A_MAX, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 22 | .mcgrps = quota_mcgrps, |
| 23 | .n_mcgrps = ARRAY_SIZE(quota_mcgrps), |
Johannes Berg | 2ecf753 | 2013-11-19 15:19:33 +0100 | [diff] [blame] | 24 | }; |
| 25 | |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 26 | /** |
| 27 | * quota_send_warning - Send warning to userspace about exceeded quota |
Fabian Frederick | 2c15ac5 | 2014-07-04 21:44:25 +0200 | [diff] [blame] | 28 | * @qid: The kernel internal quota identifier. |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 29 | * @dev: The device on which the fs is mounted (sb->s_dev) |
| 30 | * @warntype: The type of the warning: QUOTA_NL_... |
| 31 | * |
| 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. |
| 34 | * |
| 35 | */ |
| 36 | |
Eric W. Biederman | 431f197 | 2012-09-16 02:32:43 -0700 | [diff] [blame] | 37 | void quota_send_warning(struct kqid qid, dev_t dev, |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 38 | const char warntype) |
| 39 | { |
| 40 | static atomic_t seq; |
| 41 | struct sk_buff *skb; |
| 42 | void *msg_head; |
| 43 | int ret; |
| 44 | int msg_size = 4 * nla_total_size(sizeof(u32)) + |
Nicolas Dichtel | 3c6f371 | 2016-04-26 10:06:13 +0200 | [diff] [blame] | 45 | 2 * nla_total_size_64bit(sizeof(u64)); |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 46 | |
| 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); |
| 51 | if (!skb) { |
| 52 | printk(KERN_ERR |
| 53 | "VFS: Not enough memory to send quota warning.\n"); |
| 54 | return; |
| 55 | } |
| 56 | msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq), |
| 57 | "a_genl_family, 0, QUOTA_NL_C_WARNING); |
| 58 | if (!msg_head) { |
| 59 | printk(KERN_ERR |
| 60 | "VFS: Cannot store netlink header in quota warning.\n"); |
| 61 | goto err_out; |
| 62 | } |
Eric W. Biederman | 431f197 | 2012-09-16 02:32:43 -0700 | [diff] [blame] | 63 | ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, qid.type); |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 64 | if (ret) |
| 65 | goto attr_err_out; |
Nicolas Dichtel | 3c6f371 | 2016-04-26 10:06:13 +0200 | [diff] [blame] | 66 | ret = nla_put_u64_64bit(skb, QUOTA_NL_A_EXCESS_ID, |
| 67 | from_kqid_munged(&init_user_ns, qid), |
| 68 | QUOTA_NL_A_PAD); |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 69 | if (ret) |
| 70 | goto attr_err_out; |
| 71 | ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype); |
| 72 | if (ret) |
| 73 | goto attr_err_out; |
| 74 | ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev)); |
| 75 | if (ret) |
| 76 | goto attr_err_out; |
| 77 | ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev)); |
| 78 | if (ret) |
| 79 | goto attr_err_out; |
Nicolas Dichtel | 3c6f371 | 2016-04-26 10:06:13 +0200 | [diff] [blame] | 80 | ret = nla_put_u64_64bit(skb, QUOTA_NL_A_CAUSED_ID, |
| 81 | from_kuid_munged(&init_user_ns, current_uid()), |
| 82 | QUOTA_NL_A_PAD); |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 83 | if (ret) |
| 84 | goto attr_err_out; |
| 85 | genlmsg_end(skb, msg_head); |
| 86 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 87 | genlmsg_multicast("a_genl_family, skb, 0, 0, GFP_NOFS); |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 88 | return; |
| 89 | attr_err_out: |
| 90 | printk(KERN_ERR "VFS: Not enough space to compose quota message!\n"); |
| 91 | err_out: |
| 92 | kfree_skb(skb); |
| 93 | } |
| 94 | EXPORT_SYMBOL(quota_send_warning); |
| 95 | |
| 96 | static int __init quota_init(void) |
| 97 | { |
| 98 | if (genl_register_family("a_genl_family) != 0) |
| 99 | printk(KERN_ERR |
| 100 | "VFS: Failed to create quota netlink interface.\n"); |
Christoph Hellwig | 799a9d4 | 2010-02-16 03:44:54 -0500 | [diff] [blame] | 101 | return 0; |
| 102 | }; |
Paul Gortmaker | 7da5446 | 2015-12-12 16:30:04 -0500 | [diff] [blame] | 103 | fs_initcall(quota_init); |