[RFC PATCH] percpu_counters: Add new function percpu_counter_sum_and_sub

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



percpu_counter_sum_and_sub(struct percpu_counter *fbc, s64 amount)

This will be later used by ext4 code. It adds up the local
per cpu counter values to global count and subtract amount
from the gloabl count if gloabl count is > amount. This is
used during block resrevation to check within a lock we
have sufficient free blocks if so also claim the blocks.

Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@xxxxxxxxxxxxxxxxxx>
---
 include/linux/percpu_counter.h |   17 +++++++++++++++++
 lib/percpu_counter.c           |   27 +++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 0 deletions(-)

diff --git a/include/linux/percpu_counter.h b/include/linux/percpu_counter.h
index af485b1..978db67 100644
--- a/include/linux/percpu_counter.h
+++ b/include/linux/percpu_counter.h
@@ -36,6 +36,7 @@ void percpu_counter_destroy(struct percpu_counter *fbc);
 void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
 void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
 s64 __percpu_counter_sum(struct percpu_counter *fbc);
+s64 __percpu_counter_sum_and_sub(struct percpu_counter *fbc, s64 amount);
 
 static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
 {
@@ -53,6 +54,12 @@ static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
 	return __percpu_counter_sum(fbc);
 }
 
+static inline int percpu_counter_sum_and_sub(struct percpu_counter *fbc,
+						s64 amount)
+{
+	return __percpu_counter_sum_and_sub(fbc, amount);
+}
+
 #if BITS_PER_LONG == 64
 static inline s64 fbc_count(struct percpu_counter *fbc)
 {
@@ -146,6 +153,16 @@ static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
 	return percpu_counter_read(fbc);
 }
 
+static inline int percpu_counter_sum_and_sub(struct percpu_counter *fbc,
+						s64 amount)
+{
+	if (fbc->count >= amount) {
+		percpu_counter_sub(fbc, amount);
+		return 0;
+	}
+	return -E2BIG;
+}
+
 #endif	/* CONFIG_SMP */
 
 static inline void percpu_counter_inc(struct percpu_counter *fbc)
diff --git a/lib/percpu_counter.c b/lib/percpu_counter.c
index a866389..0336062 100644
--- a/lib/percpu_counter.c
+++ b/lib/percpu_counter.c
@@ -71,6 +71,33 @@ s64 __percpu_counter_sum(struct percpu_counter *fbc)
 }
 EXPORT_SYMBOL(__percpu_counter_sum);
 
+ /*
+  * Add up all the per-cpu counts. If the result is greater than
+  * amount subtract amount from result and return 0. Otherwise return
+  * -E2BIG
+  */
+s64 __percpu_counter_sum_and_sub(struct percpu_counter *fbc, s64 amount)
+{
+	s64 ret;
+	int cpu;
+
+	spin_lock(&fbc->lock);
+	ret = fbc->count;
+	for_each_online_cpu(cpu) {
+		s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
+		ret += *pcount;
+		*pcount = 0;
+	}
+	if (ret >= amount) {
+		fbc->count = ret - amount;
+		spin_unlock(&fbc->lock);
+		return 0;
+	}
+	spin_unlock(&fbc->lock);
+	return -E2BIG;
+}
+EXPORT_SYMBOL(__percpu_counter_sum_and_sub);
+
 static struct lock_class_key percpu_counter_irqsafe;
 
 int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
-- 
1.6.0.2.g2ebc0

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Reiser Filesystem Development]     [Ceph FS]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Linux FS]     [Yosemite National Park]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Device Mapper]     [Linux Media]

  Powered by Linux