Skip to content

Commit

Permalink
shmem: recalculate file inode when fstat
Browse files Browse the repository at this point in the history
Shmem uses shmem_recalc_inode to update i_blocks when it allocates page,
undoes range or swaps.  But mm can drop clean page without notifying
shmem.  This makes fstat sometimes return out-of-date block size.

The problem can be partially solved when we add
inode_operations->getattr which calls shmem_recalc_inode to update
i_blocks for fstat.

shmem_recalc_inode also updates counter used by statfs and
vm_committed_as.  For them the situation is not changed.  They still
suffer from the discrepancy after dropping clean page and before the
function is called by aforementioned triggers.

Signed-off-by: Yu Zhao <[email protected]>
Signed-off-by: Hugh Dickins <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
yuzhaogoogle authored and torvalds committed Sep 8, 2015
1 parent 567d117 commit 44a3022
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions mm/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,21 @@ void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
}
EXPORT_SYMBOL_GPL(shmem_truncate_range);

static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry,
struct kstat *stat)
{
struct inode *inode = dentry->d_inode;
struct shmem_inode_info *info = SHMEM_I(inode);

spin_lock(&info->lock);
shmem_recalc_inode(inode);
spin_unlock(&info->lock);

generic_fillattr(inode, stat);

return 0;
}

static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
{
struct inode *inode = d_inode(dentry);
Expand Down Expand Up @@ -3122,6 +3137,7 @@ static const struct file_operations shmem_file_operations = {
};

static const struct inode_operations shmem_inode_operations = {
.getattr = shmem_getattr,
.setattr = shmem_setattr,
#ifdef CONFIG_TMPFS_XATTR
.setxattr = shmem_setxattr,
Expand Down

0 comments on commit 44a3022

Please sign in to comment.