From: Chuck Lever Print a warning if the page invalidation functions don't behave as expected. A BUG is probably overkill here since the client's internal data structures will remain consistent. We're trying to catch cases where invaliding an inode's page cache races with vmscan or direct I/O, resulting in stale data remaining in the page cache. Signed-off-by: Chuck Lever Signed-off-by: Andrew Morton --- fs/nfs/dir.c | 34 +++++++++++++++++++++++++++++----- fs/nfs/direct.c | 2 +- fs/nfs/inode.c | 25 +++++++++++++++++++++++-- fs/nfs/iostat.h | 1 + include/linux/nfs_fs.h | 1 + 5 files changed, 55 insertions(+), 8 deletions(-) diff -puN fs/nfs/dir.c~nfs-add-return-code-checks-for-page-invalidation fs/nfs/dir.c --- a/fs/nfs/dir.c~nfs-add-return-code-checks-for-page-invalidation +++ a/fs/nfs/dir.c @@ -154,6 +154,32 @@ typedef struct { int error; } nfs_readdir_descriptor_t; +/* + * Trim off all pages past page zero. This ensures consistent page + * alignment of cached data. + * + * NB: This assumes we have exclusive access to this mapping either + * through inode->i_mutex or some other mechanism. + */ +static void nfs_truncate_directory_cache(struct inode *inode) +{ + int result; + + dfprintk(DIRCACHE, "NFS: %s: truncating directory (%s/%Ld)\n", + __FUNCTION__, inode->i_sb->s_id, + (long long)NFS_FILEID(inode)); + + result = invalidate_inode_pages2_range(inode->i_mapping, + PAGE_CACHE_SIZE, -1); + if (unlikely(result < 0)) { + nfs_inc_stats(inode, NFSIOS_INVALIDATEFAILED); + printk(KERN_ERR + "NFS: error %d invalidating cache for dir (%s/%Ld)\n", + result, inode->i_sb->s_id, + (long long)NFS_FILEID(inode)); + } +} + /* Now we cache directories properly, by stuffing the dirent * data directly in the page cache. * @@ -197,12 +223,10 @@ int nfs_readdir_filler(nfs_readdir_descr spin_lock(&inode->i_lock); NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME; spin_unlock(&inode->i_lock); - /* Ensure consistent page alignment of the data. - * Note: assumes we have exclusive access to this mapping either - * through inode->i_mutex or some other mechanism. - */ + if (page->index == 0) - invalidate_inode_pages2_range(inode->i_mapping, PAGE_CACHE_SIZE, -1); + nfs_truncate_directory_cache(inode); + unlock_page(page); return 0; error: diff -puN fs/nfs/direct.c~nfs-add-return-code-checks-for-page-invalidation fs/nfs/direct.c --- a/fs/nfs/direct.c~nfs-add-return-code-checks-for-page-invalidation +++ a/fs/nfs/direct.c @@ -823,7 +823,7 @@ ssize_t nfs_file_direct_write(struct kio * occur before the writes complete. Kind of racey. */ if (mapping->nrpages) - invalidate_inode_pages2(mapping); + nfs_invalidate_mapping(mapping->host, mapping); if (retval > 0) iocb->ki_pos = pos + retval; diff -puN fs/nfs/inode.c~nfs-add-return-code-checks-for-page-invalidation fs/nfs/inode.c --- a/fs/nfs/inode.c~nfs-add-return-code-checks-for-page-invalidation +++ a/fs/nfs/inode.c @@ -664,6 +664,27 @@ int nfs_revalidate_inode(struct nfs_serv } /** + * nfs_invalidate_mapping - Invalidate the inode's page cache + * @inode - pointer to host inode + * @mapping - pointer to mapping + */ +void nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping) +{ + int result; + + nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE); + + result = invalidate_inode_pages2(mapping); + if (unlikely(result) < 0) { + nfs_inc_stats(inode, NFSIOS_INVALIDATEFAILED); + printk(KERN_ERR + "NFS: error %d invalidating pages for inode (%s/%Ld)\n", + result, inode->i_sb->s_id, + (long long)NFS_FILEID(inode)); + } +} + +/** * nfs_revalidate_mapping - Revalidate the pagecache * @inode - pointer to host inode * @mapping - pointer to mapping @@ -680,10 +701,10 @@ int nfs_revalidate_mapping(struct inode ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode); if (nfsi->cache_validity & NFS_INO_INVALID_DATA) { - nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE); if (S_ISREG(inode->i_mode)) nfs_sync_mapping(mapping); - invalidate_inode_pages2(mapping); + + nfs_invalidate_mapping(inode, mapping); spin_lock(&inode->i_lock); nfsi->cache_validity &= ~NFS_INO_INVALID_DATA; diff -puN fs/nfs/iostat.h~nfs-add-return-code-checks-for-page-invalidation fs/nfs/iostat.h --- a/fs/nfs/iostat.h~nfs-add-return-code-checks-for-page-invalidation +++ a/fs/nfs/iostat.h @@ -104,6 +104,7 @@ enum nfs_stat_eventcounters { NFSIOS_SHORTREAD, NFSIOS_SHORTWRITE, NFSIOS_DELAY, + NFSIOS_INVALIDATEFAILED, __NFSIOS_COUNTSMAX, }; diff -puN include/linux/nfs_fs.h~nfs-add-return-code-checks-for-page-invalidation include/linux/nfs_fs.h --- a/include/linux/nfs_fs.h~nfs-add-return-code-checks-for-page-invalidation +++ a/include/linux/nfs_fs.h @@ -303,6 +303,7 @@ extern int nfs_attribute_timeout(struct extern int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode); extern int __nfs_revalidate_inode(struct nfs_server *, struct inode *); extern int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping); +extern void nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping); extern int nfs_setattr(struct dentry *, struct iattr *); extern void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr); extern void nfs_begin_attr_update(struct inode *); _