cgroup files: convert devcgroup_access_write() into a cgroup write_string() handler
[linux-2.6-block.git] / kernel / res_counter.c
CommitLineData
e552b661
PE
1/*
2 * resource cgroups
3 *
4 * Copyright 2007 OpenVZ SWsoft Inc
5 *
6 * Author: Pavel Emelianov <xemul@openvz.org>
7 *
8 */
9
10#include <linux/types.h>
11#include <linux/parser.h>
12#include <linux/fs.h>
1aeb272c 13#include <linux/slab.h>
e552b661
PE
14#include <linux/res_counter.h>
15#include <linux/uaccess.h>
16
17void res_counter_init(struct res_counter *counter)
18{
19 spin_lock_init(&counter->lock);
0eea1030 20 counter->limit = (unsigned long long)LLONG_MAX;
e552b661
PE
21}
22
23int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
24{
25 if (counter->usage + val > counter->limit) {
26 counter->failcnt++;
27 return -ENOMEM;
28 }
29
30 counter->usage += val;
c84872e1
PE
31 if (counter->usage > counter->max_usage)
32 counter->max_usage = counter->usage;
e552b661
PE
33 return 0;
34}
35
36int res_counter_charge(struct res_counter *counter, unsigned long val)
37{
38 int ret;
39 unsigned long flags;
40
41 spin_lock_irqsave(&counter->lock, flags);
42 ret = res_counter_charge_locked(counter, val);
43 spin_unlock_irqrestore(&counter->lock, flags);
44 return ret;
45}
46
47void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
48{
49 if (WARN_ON(counter->usage < val))
50 val = counter->usage;
51
52 counter->usage -= val;
53}
54
55void res_counter_uncharge(struct res_counter *counter, unsigned long val)
56{
57 unsigned long flags;
58
59 spin_lock_irqsave(&counter->lock, flags);
60 res_counter_uncharge_locked(counter, val);
61 spin_unlock_irqrestore(&counter->lock, flags);
62}
63
64
0eea1030
BS
65static inline unsigned long long *
66res_counter_member(struct res_counter *counter, int member)
e552b661
PE
67{
68 switch (member) {
69 case RES_USAGE:
70 return &counter->usage;
c84872e1
PE
71 case RES_MAX_USAGE:
72 return &counter->max_usage;
e552b661
PE
73 case RES_LIMIT:
74 return &counter->limit;
75 case RES_FAILCNT:
76 return &counter->failcnt;
77 };
78
79 BUG();
80 return NULL;
81}
82
83ssize_t res_counter_read(struct res_counter *counter, int member,
0eea1030
BS
84 const char __user *userbuf, size_t nbytes, loff_t *pos,
85 int (*read_strategy)(unsigned long long val, char *st_buf))
e552b661 86{
0eea1030 87 unsigned long long *val;
e552b661
PE
88 char buf[64], *s;
89
90 s = buf;
91 val = res_counter_member(counter, member);
0eea1030
BS
92 if (read_strategy)
93 s += read_strategy(*val, s);
94 else
95 s += sprintf(s, "%llu\n", *val);
e552b661
PE
96 return simple_read_from_buffer((void __user *)userbuf, nbytes,
97 pos, buf, s - buf);
98}
99
2c7eabf3
PM
100u64 res_counter_read_u64(struct res_counter *counter, int member)
101{
102 return *res_counter_member(counter, member);
103}
104
e552b661 105ssize_t res_counter_write(struct res_counter *counter, int member,
0eea1030
BS
106 const char __user *userbuf, size_t nbytes, loff_t *pos,
107 int (*write_strategy)(char *st_buf, unsigned long long *val))
e552b661
PE
108{
109 int ret;
110 char *buf, *end;
0eea1030
BS
111 unsigned long flags;
112 unsigned long long tmp, *val;
e552b661
PE
113
114 buf = kmalloc(nbytes + 1, GFP_KERNEL);
115 ret = -ENOMEM;
116 if (buf == NULL)
117 goto out;
118
119 buf[nbytes] = '\0';
120 ret = -EFAULT;
121 if (copy_from_user(buf, userbuf, nbytes))
122 goto out_free;
123
124 ret = -EINVAL;
e552b661 125
fb78922c 126 strstrip(buf);
0eea1030
BS
127 if (write_strategy) {
128 if (write_strategy(buf, &tmp)) {
129 goto out_free;
130 }
131 } else {
132 tmp = simple_strtoull(buf, &end, 10);
133 if (*end != '\0')
134 goto out_free;
135 }
136 spin_lock_irqsave(&counter->lock, flags);
e552b661
PE
137 val = res_counter_member(counter, member);
138 *val = tmp;
0eea1030 139 spin_unlock_irqrestore(&counter->lock, flags);
e552b661
PE
140 ret = nbytes;
141out_free:
142 kfree(buf);
143out:
144 return ret;
145}