nfsd: take file and mnt write in nfs4_upgrade_open
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / include / linux / res_counter.h
CommitLineData
e552b661
PE
1#ifndef __RES_COUNTER_H__
2#define __RES_COUNTER_H__
3
4/*
5 * Resource Counters
6 * Contain common data types and routines for resource accounting
7 *
8 * Copyright 2007 OpenVZ SWsoft Inc
9 *
10 * Author: Pavel Emelianov <xemul@openvz.org>
11 *
faebe9fd
PE
12 * See Documentation/controllers/resource_counter.txt for more
13 * info about what this counter is.
e552b661
PE
14 */
15
16#include <linux/cgroup.h>
17
18/*
19 * The core object. the cgroup that wishes to account for some
20 * resource may include this counter into its structures and use
21 * the helpers described beyond
22 */
23
24struct res_counter {
25 /*
26 * the current resource consumption level
27 */
0eea1030 28 unsigned long long usage;
c84872e1
PE
29 /*
30 * the maximal value of the usage from the counter creation
31 */
32 unsigned long long max_usage;
e552b661
PE
33 /*
34 * the limit that usage cannot exceed
35 */
0eea1030 36 unsigned long long limit;
e552b661
PE
37 /*
38 * the number of unsuccessful attempts to consume the resource
39 */
0eea1030 40 unsigned long long failcnt;
e552b661
PE
41 /*
42 * the lock to protect all of the above.
43 * the routines below consider this to be IRQ-safe
44 */
45 spinlock_t lock;
46};
47
2c7eabf3 48/**
e552b661 49 * Helpers to interact with userspace
2c7eabf3 50 * res_counter_read_u64() - returns the value of the specified member.
e552b661
PE
51 * res_counter_read/_write - put/get the specified fields from the
52 * res_counter struct to/from the user
53 *
54 * @counter: the counter in question
55 * @member: the field to work with (see RES_xxx below)
56 * @buf: the buffer to opeate on,...
57 * @nbytes: its size...
58 * @pos: and the offset.
59 */
60
2c7eabf3
PM
61u64 res_counter_read_u64(struct res_counter *counter, int member);
62
e552b661 63ssize_t res_counter_read(struct res_counter *counter, int member,
0eea1030
BS
64 const char __user *buf, size_t nbytes, loff_t *pos,
65 int (*read_strategy)(unsigned long long val, char *s));
e552b661 66ssize_t res_counter_write(struct res_counter *counter, int member,
0eea1030
BS
67 const char __user *buf, size_t nbytes, loff_t *pos,
68 int (*write_strategy)(char *buf, unsigned long long *val));
e552b661
PE
69
70/*
71 * the field descriptors. one for each member of res_counter
72 */
73
74enum {
75 RES_USAGE,
c84872e1 76 RES_MAX_USAGE,
e552b661
PE
77 RES_LIMIT,
78 RES_FAILCNT,
79};
80
81/*
82 * helpers for accounting
83 */
84
85void res_counter_init(struct res_counter *counter);
86
87/*
88 * charge - try to consume more resource.
89 *
90 * @counter: the counter
91 * @val: the amount of the resource. each controller defines its own
92 * units, e.g. numbers, bytes, Kbytes, etc
93 *
94 * returns 0 on success and <0 if the counter->usage will exceed the
95 * counter->limit _locked call expects the counter->lock to be taken
96 */
97
98int res_counter_charge_locked(struct res_counter *counter, unsigned long val);
99int res_counter_charge(struct res_counter *counter, unsigned long val);
100
101/*
102 * uncharge - tell that some portion of the resource is released
103 *
104 * @counter: the counter
105 * @val: the amount of the resource
106 *
107 * these calls check for usage underflow and show a warning on the console
108 * _locked call expects the counter->lock to be taken
109 */
110
111void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
112void res_counter_uncharge(struct res_counter *counter, unsigned long val);
113
66e1707b
BS
114static inline bool res_counter_limit_check_locked(struct res_counter *cnt)
115{
116 if (cnt->usage < cnt->limit)
117 return true;
118
119 return false;
120}
121
122/*
123 * Helper function to detect if the cgroup is within it's limit or
124 * not. It's currently called from cgroup_rss_prepare()
125 */
126static inline bool res_counter_check_under_limit(struct res_counter *cnt)
127{
128 bool ret;
129 unsigned long flags;
130
131 spin_lock_irqsave(&cnt->lock, flags);
132 ret = res_counter_limit_check_locked(cnt);
133 spin_unlock_irqrestore(&cnt->lock, flags);
134 return ret;
135}
136
c84872e1
PE
137static inline void res_counter_reset_max(struct res_counter *cnt)
138{
139 unsigned long flags;
140
141 spin_lock_irqsave(&cnt->lock, flags);
142 cnt->max_usage = cnt->usage;
143 spin_unlock_irqrestore(&cnt->lock, flags);
144}
145
29f2a4da
PE
146static inline void res_counter_reset_failcnt(struct res_counter *cnt)
147{
148 unsigned long flags;
149
150 spin_lock_irqsave(&cnt->lock, flags);
151 cnt->failcnt = 0;
152 spin_unlock_irqrestore(&cnt->lock, flags);
153}
e552b661 154#endif