ibmpex: report temperatures in mC, not C
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / res_counter.c
blobefbfc0fc232f33c49b5b918704b4e13ae5d0f2cc
1 /*
2 * resource cgroups
4 * Copyright 2007 OpenVZ SWsoft Inc
6 * Author: Pavel Emelianov <xemul@openvz.org>
8 */
10 #include <linux/types.h>
11 #include <linux/parser.h>
12 #include <linux/fs.h>
13 #include <linux/res_counter.h>
14 #include <linux/uaccess.h>
16 void res_counter_init(struct res_counter *counter)
18 spin_lock_init(&counter->lock);
19 counter->limit = (unsigned long long)LLONG_MAX;
22 int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
24 if (counter->usage + val > counter->limit) {
25 counter->failcnt++;
26 return -ENOMEM;
29 counter->usage += val;
30 return 0;
33 int res_counter_charge(struct res_counter *counter, unsigned long val)
35 int ret;
36 unsigned long flags;
38 spin_lock_irqsave(&counter->lock, flags);
39 ret = res_counter_charge_locked(counter, val);
40 spin_unlock_irqrestore(&counter->lock, flags);
41 return ret;
44 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
46 if (WARN_ON(counter->usage < val))
47 val = counter->usage;
49 counter->usage -= val;
52 void res_counter_uncharge(struct res_counter *counter, unsigned long val)
54 unsigned long flags;
56 spin_lock_irqsave(&counter->lock, flags);
57 res_counter_uncharge_locked(counter, val);
58 spin_unlock_irqrestore(&counter->lock, flags);
62 static inline unsigned long long *
63 res_counter_member(struct res_counter *counter, int member)
65 switch (member) {
66 case RES_USAGE:
67 return &counter->usage;
68 case RES_LIMIT:
69 return &counter->limit;
70 case RES_FAILCNT:
71 return &counter->failcnt;
74 BUG();
75 return NULL;
78 ssize_t res_counter_read(struct res_counter *counter, int member,
79 const char __user *userbuf, size_t nbytes, loff_t *pos,
80 int (*read_strategy)(unsigned long long val, char *st_buf))
82 unsigned long long *val;
83 char buf[64], *s;
85 s = buf;
86 val = res_counter_member(counter, member);
87 if (read_strategy)
88 s += read_strategy(*val, s);
89 else
90 s += sprintf(s, "%llu\n", *val);
91 return simple_read_from_buffer((void __user *)userbuf, nbytes,
92 pos, buf, s - buf);
95 ssize_t res_counter_write(struct res_counter *counter, int member,
96 const char __user *userbuf, size_t nbytes, loff_t *pos,
97 int (*write_strategy)(char *st_buf, unsigned long long *val))
99 int ret;
100 char *buf, *end;
101 unsigned long flags;
102 unsigned long long tmp, *val;
104 buf = kmalloc(nbytes + 1, GFP_KERNEL);
105 ret = -ENOMEM;
106 if (buf == NULL)
107 goto out;
109 buf[nbytes] = '\0';
110 ret = -EFAULT;
111 if (copy_from_user(buf, userbuf, nbytes))
112 goto out_free;
114 ret = -EINVAL;
116 strstrip(buf);
117 if (write_strategy) {
118 if (write_strategy(buf, &tmp)) {
119 goto out_free;
121 } else {
122 tmp = simple_strtoull(buf, &end, 10);
123 if (*end != '\0')
124 goto out_free;
126 spin_lock_irqsave(&counter->lock, flags);
127 val = res_counter_member(counter, member);
128 *val = tmp;
129 spin_unlock_irqrestore(&counter->lock, flags);
130 ret = nbytes;
131 out_free:
132 kfree(buf);
133 out:
134 return ret;