usb: return error code instead of 0 in the enqueue function.
[linux-2.6/mini2440.git] / kernel / res_counter.c
blobf275c8eca772c4dfe6bcdda6b1253a65fefe3e83
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/slab.h>
14 #include <linux/res_counter.h>
15 #include <linux/uaccess.h>
16 #include <linux/mm.h>
18 void res_counter_init(struct res_counter *counter)
20 spin_lock_init(&counter->lock);
21 counter->limit = (unsigned long long)LLONG_MAX;
24 int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
26 if (counter->usage + val > counter->limit) {
27 counter->failcnt++;
28 return -ENOMEM;
31 counter->usage += val;
32 if (counter->usage > counter->max_usage)
33 counter->max_usage = counter->usage;
34 return 0;
37 int res_counter_charge(struct res_counter *counter, unsigned long val)
39 int ret;
40 unsigned long flags;
42 spin_lock_irqsave(&counter->lock, flags);
43 ret = res_counter_charge_locked(counter, val);
44 spin_unlock_irqrestore(&counter->lock, flags);
45 return ret;
48 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
50 if (WARN_ON(counter->usage < val))
51 val = counter->usage;
53 counter->usage -= val;
56 void res_counter_uncharge(struct res_counter *counter, unsigned long val)
58 unsigned long flags;
60 spin_lock_irqsave(&counter->lock, flags);
61 res_counter_uncharge_locked(counter, val);
62 spin_unlock_irqrestore(&counter->lock, flags);
66 static inline unsigned long long *
67 res_counter_member(struct res_counter *counter, int member)
69 switch (member) {
70 case RES_USAGE:
71 return &counter->usage;
72 case RES_MAX_USAGE:
73 return &counter->max_usage;
74 case RES_LIMIT:
75 return &counter->limit;
76 case RES_FAILCNT:
77 return &counter->failcnt;
80 BUG();
81 return NULL;
84 ssize_t res_counter_read(struct res_counter *counter, int member,
85 const char __user *userbuf, size_t nbytes, loff_t *pos,
86 int (*read_strategy)(unsigned long long val, char *st_buf))
88 unsigned long long *val;
89 char buf[64], *s;
91 s = buf;
92 val = res_counter_member(counter, member);
93 if (read_strategy)
94 s += read_strategy(*val, s);
95 else
96 s += sprintf(s, "%llu\n", *val);
97 return simple_read_from_buffer((void __user *)userbuf, nbytes,
98 pos, buf, s - buf);
101 u64 res_counter_read_u64(struct res_counter *counter, int member)
103 return *res_counter_member(counter, member);
106 int res_counter_memparse_write_strategy(const char *buf,
107 unsigned long long *res)
109 char *end;
110 /* FIXME - make memparse() take const char* args */
111 *res = memparse((char *)buf, &end);
112 if (*end != '\0')
113 return -EINVAL;
115 *res = PAGE_ALIGN(*res);
116 return 0;
119 int res_counter_write(struct res_counter *counter, int member,
120 const char *buf, write_strategy_fn write_strategy)
122 char *end;
123 unsigned long flags;
124 unsigned long long tmp, *val;
126 if (write_strategy) {
127 if (write_strategy(buf, &tmp))
128 return -EINVAL;
129 } else {
130 tmp = simple_strtoull(buf, &end, 10);
131 if (*end != '\0')
132 return -EINVAL;
134 spin_lock_irqsave(&counter->lock, flags);
135 val = res_counter_member(counter, member);
136 *val = tmp;
137 spin_unlock_irqrestore(&counter->lock, flags);
138 return 0;