Blame view

fs/quota/netlink.c 3.02 KB
799a9d440   Christoph Hellwig   quota: split out ...
1
2
  #include <linux/cred.h>
  #include <linux/init.h>
799a9d440   Christoph Hellwig   quota: split out ...
3
4
5
  #include <linux/kernel.h>
  #include <linux/quotaops.h>
  #include <linux/sched.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
6
  #include <linux/slab.h>
799a9d440   Christoph Hellwig   quota: split out ...
7
8
  #include <net/netlink.h>
  #include <net/genetlink.h>
2a94fe48f   Johannes Berg   genetlink: make m...
9
10
11
  static const struct genl_multicast_group quota_mcgrps[] = {
  	{ .name = "events", },
  };
799a9d440   Christoph Hellwig   quota: split out ...
12
13
  /* Netlink family structure for quota */
  static struct genl_family quota_genl_family = {
2ecf7536b   Johannes Berg   quota/genetlink: ...
14
15
16
17
18
19
20
  	/*
  	 * Needed due to multicast group ID abuse - old code assumed
  	 * the family ID was also a valid multicast group ID (which
  	 * isn't true) and userspace might thus rely on it. Assign a
  	 * static ID for this group to make dealing with that easier.
  	 */
  	.id = GENL_ID_VFS_DQUOT,
799a9d440   Christoph Hellwig   quota: split out ...
21
22
23
24
  	.hdrsize = 0,
  	.name = "VFS_DQUOT",
  	.version = 1,
  	.maxattr = QUOTA_NL_A_MAX,
2a94fe48f   Johannes Berg   genetlink: make m...
25
26
  	.mcgrps = quota_mcgrps,
  	.n_mcgrps = ARRAY_SIZE(quota_mcgrps),
2ecf7536b   Johannes Berg   quota/genetlink: ...
27
  };
799a9d440   Christoph Hellwig   quota: split out ...
28
29
  /**
   * quota_send_warning - Send warning to userspace about exceeded quota
2c15ac5bd   Fabian Frederick   fs/quota: kernel-...
30
   * @qid: The kernel internal quota identifier.
799a9d440   Christoph Hellwig   quota: split out ...
31
32
33
34
35
36
37
   * @dev: The device on which the fs is mounted (sb->s_dev)
   * @warntype: The type of the warning: QUOTA_NL_...
   *
   * This can be used by filesystems (including those which don't use
   * dquot) to send a message to userspace relating to quota limits.
   *
   */
431f19744   Eric W. Biederman   userns: Convert q...
38
  void quota_send_warning(struct kqid qid, dev_t dev,
799a9d440   Christoph Hellwig   quota: split out ...
39
40
41
42
43
44
45
  			const char warntype)
  {
  	static atomic_t seq;
  	struct sk_buff *skb;
  	void *msg_head;
  	int ret;
  	int msg_size = 4 * nla_total_size(sizeof(u32)) +
3c6f3714d   Nicolas Dichtel   fs/quota: use nla...
46
  		       2 * nla_total_size_64bit(sizeof(u64));
799a9d440   Christoph Hellwig   quota: split out ...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  
  	/* We have to allocate using GFP_NOFS as we are called from a
  	 * filesystem performing write and thus further recursion into
  	 * the fs to free some data could cause deadlocks. */
  	skb = genlmsg_new(msg_size, GFP_NOFS);
  	if (!skb) {
  		printk(KERN_ERR
  		  "VFS: Not enough memory to send quota warning.
  ");
  		return;
  	}
  	msg_head = genlmsg_put(skb, 0, atomic_add_return(1, &seq),
  			&quota_genl_family, 0, QUOTA_NL_C_WARNING);
  	if (!msg_head) {
  		printk(KERN_ERR
  		  "VFS: Cannot store netlink header in quota warning.
  ");
  		goto err_out;
  	}
431f19744   Eric W. Biederman   userns: Convert q...
66
  	ret = nla_put_u32(skb, QUOTA_NL_A_QTYPE, qid.type);
799a9d440   Christoph Hellwig   quota: split out ...
67
68
  	if (ret)
  		goto attr_err_out;
3c6f3714d   Nicolas Dichtel   fs/quota: use nla...
69
70
71
  	ret = nla_put_u64_64bit(skb, QUOTA_NL_A_EXCESS_ID,
  				from_kqid_munged(&init_user_ns, qid),
  				QUOTA_NL_A_PAD);
799a9d440   Christoph Hellwig   quota: split out ...
72
73
74
75
76
77
78
79
80
81
82
  	if (ret)
  		goto attr_err_out;
  	ret = nla_put_u32(skb, QUOTA_NL_A_WARNING, warntype);
  	if (ret)
  		goto attr_err_out;
  	ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MAJOR, MAJOR(dev));
  	if (ret)
  		goto attr_err_out;
  	ret = nla_put_u32(skb, QUOTA_NL_A_DEV_MINOR, MINOR(dev));
  	if (ret)
  		goto attr_err_out;
3c6f3714d   Nicolas Dichtel   fs/quota: use nla...
83
84
85
  	ret = nla_put_u64_64bit(skb, QUOTA_NL_A_CAUSED_ID,
  				from_kuid_munged(&init_user_ns, current_uid()),
  				QUOTA_NL_A_PAD);
799a9d440   Christoph Hellwig   quota: split out ...
86
87
88
  	if (ret)
  		goto attr_err_out;
  	genlmsg_end(skb, msg_head);
2a94fe48f   Johannes Berg   genetlink: make m...
89
  	genlmsg_multicast(&quota_genl_family, skb, 0, 0, GFP_NOFS);
799a9d440   Christoph Hellwig   quota: split out ...
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  	return;
  attr_err_out:
  	printk(KERN_ERR "VFS: Not enough space to compose quota message!
  ");
  err_out:
  	kfree_skb(skb);
  }
  EXPORT_SYMBOL(quota_send_warning);
  
  static int __init quota_init(void)
  {
  	if (genl_register_family(&quota_genl_family) != 0)
  		printk(KERN_ERR
  		       "VFS: Failed to create quota netlink interface.
  ");
  	return 0;
  };
7da544636   Paul Gortmaker   fs: make quota/ne...
107
  fs_initcall(quota_init);