Skip to content

Commit

Permalink
[PATCH] percpu counter data type changes to suppport more than 2**31 …
Browse files Browse the repository at this point in the history
…ext3 free blocks counter

The percpu counter data type are changed in this set of patches to support
more users like ext3 who need more than 32 bit to store the free blocks
total in the filesystem.

- Generic perpcu counters data type changes.  The size of the global counter
  and local counter were explictly specified using s64 and s32.  The global
  counter is changed from long to s64, while the local counter is changed from
  long to s32, so we could avoid doing 64 bit update in most cases.

- Users of the percpu counters are updated to make use of the new
  percpu_counter_init() routine now taking an additional parameter to allow
  users to pass the initial value of the global counter.

Signed-off-by: Mingming Cao <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Mingming Cao authored and Linus Torvalds committed Jun 23, 2006
1 parent 3cbc564 commit 0216bfc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 53 deletions.
25 changes: 13 additions & 12 deletions fs/ext2/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,6 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
printk ("EXT2-fs: not enough memory\n");
goto failed_mount;
}
percpu_counter_init(&sbi->s_freeblocks_counter);
percpu_counter_init(&sbi->s_freeinodes_counter);
percpu_counter_init(&sbi->s_dirs_counter);
bgl_lock_init(&sbi->s_blockgroup_lock);
sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
GFP_KERNEL);
Expand All @@ -863,6 +860,13 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
sbi->s_gdb_count = db_count;
get_random_bytes(&sbi->s_next_generation, sizeof(u32));
spin_lock_init(&sbi->s_next_gen_lock);

percpu_counter_init(&sbi->s_freeblocks_counter,
ext2_count_free_blocks(sb));
percpu_counter_init(&sbi->s_freeinodes_counter,
ext2_count_free_inodes(sb));
percpu_counter_init(&sbi->s_dirs_counter,
ext2_count_dirs(sb));
/*
* set up enough so that it can read an inode
*/
Expand All @@ -874,32 +878,29 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
if (!sb->s_root) {
iput(root);
printk(KERN_ERR "EXT2-fs: get root inode failed\n");
goto failed_mount2;
goto failed_mount3;
}
if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
dput(sb->s_root);
sb->s_root = NULL;
printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
goto failed_mount2;
goto failed_mount3;
}
if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
ext2_warning(sb, __FUNCTION__,
"mounting ext3 filesystem as ext2");
ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
percpu_counter_mod(&sbi->s_freeblocks_counter,
ext2_count_free_blocks(sb));
percpu_counter_mod(&sbi->s_freeinodes_counter,
ext2_count_free_inodes(sb));
percpu_counter_mod(&sbi->s_dirs_counter,
ext2_count_dirs(sb));
return 0;

cantfind_ext2:
if (!silent)
printk("VFS: Can't find an ext2 filesystem on dev %s.\n",
sb->s_id);
goto failed_mount;

failed_mount3:
percpu_counter_destroy(&sbi->s_freeblocks_counter);
percpu_counter_destroy(&sbi->s_freeinodes_counter);
percpu_counter_destroy(&sbi->s_dirs_counter);
failed_mount2:
for (i = 0; i < db_count; i++)
brelse(sbi->s_group_desc[i]);
Expand Down
36 changes: 19 additions & 17 deletions fs/ext3/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -1580,9 +1580,6 @@ static int ext3_fill_super (struct super_block *sb, void *data, int silent)
goto failed_mount;
}

percpu_counter_init(&sbi->s_freeblocks_counter);
percpu_counter_init(&sbi->s_freeinodes_counter);
percpu_counter_init(&sbi->s_dirs_counter);
bgl_lock_init(&sbi->s_blockgroup_lock);

for (i = 0; i < db_count; i++) {
Expand All @@ -1602,6 +1599,14 @@ static int ext3_fill_super (struct super_block *sb, void *data, int silent)
sbi->s_gdb_count = db_count;
get_random_bytes(&sbi->s_next_generation, sizeof(u32));
spin_lock_init(&sbi->s_next_gen_lock);

percpu_counter_init(&sbi->s_freeblocks_counter,
ext3_count_free_blocks(sb));
percpu_counter_init(&sbi->s_freeinodes_counter,
ext3_count_free_inodes(sb));
percpu_counter_init(&sbi->s_dirs_counter,
ext3_count_dirs(sb));

/* per fileystem reservation list head & lock */
spin_lock_init(&sbi->s_rsv_window_lock);
sbi->s_rsv_window_root = RB_ROOT;
Expand Down Expand Up @@ -1640,16 +1645,16 @@ static int ext3_fill_super (struct super_block *sb, void *data, int silent)
if (!test_opt(sb, NOLOAD) &&
EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
if (ext3_load_journal(sb, es, journal_devnum))
goto failed_mount2;
goto failed_mount3;
} else if (journal_inum) {
if (ext3_create_journal(sb, es, journal_inum))
goto failed_mount2;
goto failed_mount3;
} else {
if (!silent)
printk (KERN_ERR
"ext3: No journal on filesystem on %s\n",
sb->s_id);
goto failed_mount2;
goto failed_mount3;
}

/* We have now updated the journal if required, so we can
Expand All @@ -1672,7 +1677,7 @@ static int ext3_fill_super (struct super_block *sb, void *data, int silent)
(sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
printk(KERN_ERR "EXT3-fs: Journal does not support "
"requested data journaling mode\n");
goto failed_mount3;
goto failed_mount4;
}
default:
break;
Expand All @@ -1695,13 +1700,13 @@ static int ext3_fill_super (struct super_block *sb, void *data, int silent)
if (!sb->s_root) {
printk(KERN_ERR "EXT3-fs: get root inode failed\n");
iput(root);
goto failed_mount3;
goto failed_mount4;
}
if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
dput(sb->s_root);
sb->s_root = NULL;
printk(KERN_ERR "EXT3-fs: corrupt root inode, run e2fsck\n");
goto failed_mount3;
goto failed_mount4;
}

ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
Expand All @@ -1724,13 +1729,6 @@ static int ext3_fill_super (struct super_block *sb, void *data, int silent)
test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
"writeback");

percpu_counter_mod(&sbi->s_freeblocks_counter,
ext3_count_free_blocks(sb));
percpu_counter_mod(&sbi->s_freeinodes_counter,
ext3_count_free_inodes(sb));
percpu_counter_mod(&sbi->s_dirs_counter,
ext3_count_dirs(sb));

lock_kernel();
return 0;

Expand All @@ -1740,8 +1738,12 @@ static int ext3_fill_super (struct super_block *sb, void *data, int silent)
sb->s_id);
goto failed_mount;

failed_mount3:
failed_mount4:
journal_destroy(sbi->s_journal);
failed_mount3:
percpu_counter_destroy(&sbi->s_freeblocks_counter);
percpu_counter_destroy(&sbi->s_freeinodes_counter);
percpu_counter_destroy(&sbi->s_dirs_counter);
failed_mount2:
for (i = 0; i < db_count; i++)
brelse(sbi->s_group_desc[i]);
Expand Down
2 changes: 1 addition & 1 deletion fs/file_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,5 @@ void __init files_init(unsigned long mempages)
if (files_stat.max_files < NR_FILE)
files_stat.max_files = NR_FILE;
files_defer_init();
percpu_counter_init(&nr_files);
percpu_counter_init(&nr_files, 0);
}
38 changes: 20 additions & 18 deletions include/linux/percpu_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
#include <linux/smp.h>
#include <linux/threads.h>
#include <linux/percpu.h>
#include <linux/types.h>

#ifdef CONFIG_SMP

struct percpu_counter {
spinlock_t lock;
long count;
long *counters;
s64 count;
s32 *counters;
};

#if NR_CPUS >= 16
Expand All @@ -25,74 +26,75 @@ struct percpu_counter {
#define FBC_BATCH (NR_CPUS*4)
#endif

static inline void percpu_counter_init(struct percpu_counter *fbc)
static inline void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
{
spin_lock_init(&fbc->lock);
fbc->count = 0;
fbc->counters = alloc_percpu(long);
fbc->count = amount;
fbc->counters = alloc_percpu(s32);
}

static inline void percpu_counter_destroy(struct percpu_counter *fbc)
{
free_percpu(fbc->counters);
}

void percpu_counter_mod(struct percpu_counter *fbc, long amount);
long percpu_counter_sum(struct percpu_counter *fbc);
void percpu_counter_mod(struct percpu_counter *fbc, s32 amount);
s64 percpu_counter_sum(struct percpu_counter *fbc);

static inline long percpu_counter_read(struct percpu_counter *fbc)
static inline s64 percpu_counter_read(struct percpu_counter *fbc)
{
return fbc->count;
}

/*
* It is possible for the percpu_counter_read() to return a small negative
* number for some counter which should never be negative.
*
*/
static inline long percpu_counter_read_positive(struct percpu_counter *fbc)
static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
{
long ret = fbc->count;
s64 ret = fbc->count;

barrier(); /* Prevent reloads of fbc->count */
if (ret > 0)
if (ret >= 0)
return ret;
return 1;
}

#else

struct percpu_counter {
long count;
s64 count;
};

static inline void percpu_counter_init(struct percpu_counter *fbc)
static inline void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
{
fbc->count = 0;
fbc->count = amount;
}

static inline void percpu_counter_destroy(struct percpu_counter *fbc)
{
}

static inline void
percpu_counter_mod(struct percpu_counter *fbc, long amount)
percpu_counter_mod(struct percpu_counter *fbc, s32 amount)
{
preempt_disable();
fbc->count += amount;
preempt_enable();
}

static inline long percpu_counter_read(struct percpu_counter *fbc)
static inline s64 percpu_counter_read(struct percpu_counter *fbc)
{
return fbc->count;
}

static inline long percpu_counter_read_positive(struct percpu_counter *fbc)
static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
{
return fbc->count;
}

static inline long percpu_counter_sum(struct percpu_counter *fbc)
static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
{
return percpu_counter_read_positive(fbc);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/percpu_counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <linux/percpu_counter.h>
#include <linux/module.h>

void percpu_counter_mod(struct percpu_counter *fbc, long amount)
void percpu_counter_mod(struct percpu_counter *fbc, s32 amount)
{
long count;
long *pcount;
s32 *pcount;
int cpu = get_cpu();

pcount = per_cpu_ptr(fbc->counters, cpu);
Expand All @@ -29,15 +29,15 @@ EXPORT_SYMBOL(percpu_counter_mod);
* Add up all the per-cpu counts, return the result. This is a more accurate
* but much slower version of percpu_counter_read_positive()
*/
long percpu_counter_sum(struct percpu_counter *fbc)
s64 percpu_counter_sum(struct percpu_counter *fbc)
{
long ret;
s64 ret;
int cpu;

spin_lock(&fbc->lock);
ret = fbc->count;
for_each_possible_cpu(cpu) {
long *pcount = per_cpu_ptr(fbc->counters, cpu);
s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
ret += *pcount;
}
spin_unlock(&fbc->lock);
Expand Down

0 comments on commit 0216bfc

Please sign in to comment.