Skip to content

Commit

Permalink
superblock: add filesystem shrinker operations
Browse files Browse the repository at this point in the history
Now we have a per-superblock shrinker implementation, we can add a
filesystem specific callout to it to allow filesystem internal
caches to be shrunk by the superblock shrinker.

Rather than perpetuate the multipurpose shrinker callback API (i.e.
nr_to_scan == 0 meaning "tell me how many objects freeable in the
cache), two operations will be added. The first will return the
number of objects that are freeable, the second is the actual
shrinker call.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
  • Loading branch information
Dave Chinner authored and Al Viro committed Jul 21, 2011
1 parent 4f8c19f commit 0e1fdaf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
16 changes: 16 additions & 0 deletions Documentation/filesystems/vfs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ struct super_operations {

ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
int (*nr_cached_objects)(struct super_block *);
void (*free_cached_objects)(struct super_block *, int);
};

All methods are called without any locks being held, unless otherwise
Expand Down Expand Up @@ -301,6 +303,20 @@ or bottom half).

quota_write: called by the VFS to write to filesystem quota file.

nr_cached_objects: called by the sb cache shrinking function for the
filesystem to return the number of freeable cached objects it contains.
Optional.

free_cache_objects: called by the sb cache shrinking function for the
filesystem to scan the number of objects indicated to try to free them.
Optional, but any filesystem implementing this method needs to also
implement ->nr_cached_objects for it to be called correctly.

We can't do anything with any errors that the filesystem might
encountered, hence the void return type. This will never be called if
the VM is trying to reclaim under GFP_NOFS conditions, hence this
method does not need to handle that situation itself.

Whoever sets up the inode is responsible for filling in the "i_op" field. This
is a pointer to a "struct inode_operations" which describes the methods that
can be performed on individual inodes.
Expand Down
45 changes: 33 additions & 12 deletions fs/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ DEFINE_SPINLOCK(sb_lock);
static int prune_super(struct shrinker *shrink, struct shrink_control *sc)
{
struct super_block *sb;
int count;
int fs_objects = 0;
int total_objects;

sb = container_of(shrink, struct super_block, s_shrink);

Expand All @@ -62,22 +63,42 @@ static int prune_super(struct shrinker *shrink, struct shrink_control *sc)
if (!grab_super_passive(sb))
return -1;

if (sc->nr_to_scan) {
/* proportion the scan between the two caches */
int total;
if (sb->s_op && sb->s_op->nr_cached_objects)
fs_objects = sb->s_op->nr_cached_objects(sb);

total_objects = sb->s_nr_dentry_unused +
sb->s_nr_inodes_unused + fs_objects + 1;

total = sb->s_nr_dentry_unused + sb->s_nr_inodes_unused + 1;
count = (sc->nr_to_scan * sb->s_nr_dentry_unused) / total;
if (sc->nr_to_scan) {
int dentries;
int inodes;

/* proportion the scan between the caches */
dentries = (sc->nr_to_scan * sb->s_nr_dentry_unused) /
total_objects;
inodes = (sc->nr_to_scan * sb->s_nr_inodes_unused) /
total_objects;
if (fs_objects)
fs_objects = (sc->nr_to_scan * fs_objects) /
total_objects;
/*
* prune the dcache first as the icache is pinned by it, then
* prune the icache, followed by the filesystem specific caches
*/
prune_dcache_sb(sb, dentries);
prune_icache_sb(sb, inodes);

/* prune dcache first as icache is pinned by it */
prune_dcache_sb(sb, count);
prune_icache_sb(sb, sc->nr_to_scan - count);
if (fs_objects && sb->s_op->free_cached_objects) {
sb->s_op->free_cached_objects(sb, fs_objects);
fs_objects = sb->s_op->nr_cached_objects(sb);
}
total_objects = sb->s_nr_dentry_unused +
sb->s_nr_inodes_unused + fs_objects;
}

count = ((sb->s_nr_dentry_unused + sb->s_nr_inodes_unused) / 100)
* sysctl_vfs_cache_pressure;
total_objects = (total_objects / 100) * sysctl_vfs_cache_pressure;
drop_super(sb);
return count;
return total_objects;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions include/linux/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,8 @@ struct super_operations {
ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
#endif
int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);
int (*nr_cached_objects)(struct super_block *);
void (*free_cached_objects)(struct super_block *, int);
};

/*
Expand Down

0 comments on commit 0e1fdaf

Please sign in to comment.