]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - kernel/res_counter.c
CGroup API files: rename read/write_uint methods to read_write_u64
[mirror_ubuntu-artful-kernel.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;
31 return 0;
32}
33
34int res_counter_charge(struct res_counter *counter, unsigned long val)
35{
36 int ret;
37 unsigned long flags;
38
39 spin_lock_irqsave(&counter->lock, flags);
40 ret = res_counter_charge_locked(counter, val);
41 spin_unlock_irqrestore(&counter->lock, flags);
42 return ret;
43}
44
45void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
46{
47 if (WARN_ON(counter->usage < val))
48 val = counter->usage;
49
50 counter->usage -= val;
51}
52
53void res_counter_uncharge(struct res_counter *counter, unsigned long val)
54{
55 unsigned long flags;
56
57 spin_lock_irqsave(&counter->lock, flags);
58 res_counter_uncharge_locked(counter, val);
59 spin_unlock_irqrestore(&counter->lock, flags);
60}
61
62
0eea1030
BS
63static inline unsigned long long *
64res_counter_member(struct res_counter *counter, int member)
e552b661
PE
65{
66 switch (member) {
67 case RES_USAGE:
68 return &counter->usage;
69 case RES_LIMIT:
70 return &counter->limit;
71 case RES_FAILCNT:
72 return &counter->failcnt;
73 };
74
75 BUG();
76 return NULL;
77}
78
79ssize_t res_counter_read(struct res_counter *counter, int member,
0eea1030
BS
80 const char __user *userbuf, size_t nbytes, loff_t *pos,
81 int (*read_strategy)(unsigned long long val, char *st_buf))
e552b661 82{
0eea1030 83 unsigned long long *val;
e552b661
PE
84 char buf[64], *s;
85
86 s = buf;
87 val = res_counter_member(counter, member);
0eea1030
BS
88 if (read_strategy)
89 s += read_strategy(*val, s);
90 else
91 s += sprintf(s, "%llu\n", *val);
e552b661
PE
92 return simple_read_from_buffer((void __user *)userbuf, nbytes,
93 pos, buf, s - buf);
94}
95
96ssize_t res_counter_write(struct res_counter *counter, int member,
0eea1030
BS
97 const char __user *userbuf, size_t nbytes, loff_t *pos,
98 int (*write_strategy)(char *st_buf, unsigned long long *val))
e552b661
PE
99{
100 int ret;
101 char *buf, *end;
0eea1030
BS
102 unsigned long flags;
103 unsigned long long tmp, *val;
e552b661
PE
104
105 buf = kmalloc(nbytes + 1, GFP_KERNEL);
106 ret = -ENOMEM;
107 if (buf == NULL)
108 goto out;
109
110 buf[nbytes] = '\0';
111 ret = -EFAULT;
112 if (copy_from_user(buf, userbuf, nbytes))
113 goto out_free;
114
115 ret = -EINVAL;
e552b661 116
fb78922c 117 strstrip(buf);
0eea1030
BS
118 if (write_strategy) {
119 if (write_strategy(buf, &tmp)) {
120 goto out_free;
121 }
122 } else {
123 tmp = simple_strtoull(buf, &end, 10);
124 if (*end != '\0')
125 goto out_free;
126 }
127 spin_lock_irqsave(&counter->lock, flags);
e552b661
PE
128 val = res_counter_member(counter, member);
129 *val = tmp;
0eea1030 130 spin_unlock_irqrestore(&counter->lock, flags);
e552b661
PE
131 ret = nbytes;
132out_free:
133 kfree(buf);
134out:
135 return ret;
136}