]> bbs.cooldavid.org Git - net-next-2.6.git/blame - include/linux/percpu_counter.h
[PATCH] percpu counter data type changes to suppport more than 2**31 ext3 free blocks...
[net-next-2.6.git] / include / linux / percpu_counter.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_PERCPU_COUNTER_H
2#define _LINUX_PERCPU_COUNTER_H
3/*
4 * A simple "approximate counter" for use in ext2 and ext3 superblocks.
5 *
6 * WARNING: these things are HUGE. 4 kbytes per counter on 32-way P4.
7 */
8
1da177e4
LT
9#include <linux/spinlock.h>
10#include <linux/smp.h>
11#include <linux/threads.h>
12#include <linux/percpu.h>
0216bfcf 13#include <linux/types.h>
1da177e4
LT
14
15#ifdef CONFIG_SMP
16
17struct percpu_counter {
18 spinlock_t lock;
0216bfcf
MC
19 s64 count;
20 s32 *counters;
1da177e4
LT
21};
22
23#if NR_CPUS >= 16
24#define FBC_BATCH (NR_CPUS*2)
25#else
26#define FBC_BATCH (NR_CPUS*4)
27#endif
28
0216bfcf 29static inline void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
1da177e4
LT
30{
31 spin_lock_init(&fbc->lock);
0216bfcf
MC
32 fbc->count = amount;
33 fbc->counters = alloc_percpu(s32);
1da177e4
LT
34}
35
36static inline void percpu_counter_destroy(struct percpu_counter *fbc)
37{
38 free_percpu(fbc->counters);
39}
40
0216bfcf
MC
41void percpu_counter_mod(struct percpu_counter *fbc, s32 amount);
42s64 percpu_counter_sum(struct percpu_counter *fbc);
1da177e4 43
0216bfcf 44static inline s64 percpu_counter_read(struct percpu_counter *fbc)
1da177e4
LT
45{
46 return fbc->count;
47}
48
49/*
50 * It is possible for the percpu_counter_read() to return a small negative
51 * number for some counter which should never be negative.
0216bfcf 52 *
1da177e4 53 */
0216bfcf 54static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
1da177e4 55{
0216bfcf 56 s64 ret = fbc->count;
1da177e4
LT
57
58 barrier(); /* Prevent reloads of fbc->count */
0216bfcf 59 if (ret >= 0)
1da177e4
LT
60 return ret;
61 return 1;
62}
63
64#else
65
66struct percpu_counter {
0216bfcf 67 s64 count;
1da177e4
LT
68};
69
0216bfcf 70static inline void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
1da177e4 71{
0216bfcf 72 fbc->count = amount;
1da177e4
LT
73}
74
75static inline void percpu_counter_destroy(struct percpu_counter *fbc)
76{
77}
78
79static inline void
0216bfcf 80percpu_counter_mod(struct percpu_counter *fbc, s32 amount)
1da177e4
LT
81{
82 preempt_disable();
83 fbc->count += amount;
84 preempt_enable();
85}
86
0216bfcf 87static inline s64 percpu_counter_read(struct percpu_counter *fbc)
1da177e4
LT
88{
89 return fbc->count;
90}
91
0216bfcf 92static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
1da177e4
LT
93{
94 return fbc->count;
95}
96
0216bfcf 97static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
e2bab3d9
AM
98{
99 return percpu_counter_read_positive(fbc);
100}
101
1da177e4
LT
102#endif /* CONFIG_SMP */
103
104static inline void percpu_counter_inc(struct percpu_counter *fbc)
105{
106 percpu_counter_mod(fbc, 1);
107}
108
109static inline void percpu_counter_dec(struct percpu_counter *fbc)
110{
111 percpu_counter_mod(fbc, -1);
112}
113
114#endif /* _LINUX_PERCPU_COUNTER_H */