^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/quotaops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <net/netlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <net/genetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static const struct genl_multicast_group quota_mcgrps[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) { .name = "events", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* Netlink family structure for quota */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static struct genl_family quota_genl_family __ro_after_init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) .hdrsize = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) .name = "VFS_DQUOT",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) .version = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) .maxattr = QUOTA_NL_A_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) .mcgrps = quota_mcgrps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .n_mcgrps = ARRAY_SIZE(quota_mcgrps),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * quota_send_warning - Send warning to userspace about exceeded quota
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * @qid: The kernel internal quota identifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @dev: The device on which the fs is mounted (sb->s_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * @warntype: The type of the warning: QUOTA_NL_...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * This can be used by filesystems (including those which don't use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * dquot) to send a message to userspace relating to quota limits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) void quota_send_warning(struct kqid qid, dev_t dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) const char warntype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static atomic_t seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void *msg_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int msg_size = 4 * nla_total_size(sizeof(u32)) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) 2 * nla_total_size_64bit(sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* We have to allocate using GFP_NOFS as we are called from a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * filesystem performing write and thus further recursion into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * the fs to free some data could cause deadlocks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) skb = genlmsg_new(msg_size, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (!skb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) "VFS: Not enough memory to send quota warning.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) "a_genl_family, 0, QUOTA_NL_C_WARNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!msg_head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) "VFS: Cannot store netlink header in quota warning.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) goto err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, qid.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) goto attr_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ret = nla_put_u64_64bit(skb, QUOTA_NL_A_EXCESS_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) from_kqid_munged(&init_user_ns, qid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) QUOTA_NL_A_PAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) goto attr_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto attr_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) goto attr_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto attr_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ret = nla_put_u64_64bit(skb, QUOTA_NL_A_CAUSED_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) from_kuid_munged(&init_user_ns, current_uid()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) QUOTA_NL_A_PAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto attr_err_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) genlmsg_end(skb, msg_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) genlmsg_multicast("a_genl_family, skb, 0, 0, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) attr_err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) printk(KERN_ERR "VFS: Not enough space to compose quota message!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) err_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) EXPORT_SYMBOL(quota_send_warning);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static int __init quota_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (genl_register_family("a_genl_family) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) "VFS: Failed to create quota netlink interface.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) fs_initcall(quota_init);