From aff8d5ca7a3dca0bd6f6e86c9a85bf04941262ce Mon Sep 17 00:00:00 2001 From: Jeff Layton Date: Wed, 30 Apr 2014 09:31:45 -0400 Subject: cifs: convert booleans in cifsInodeInfo to a flags field In later patches, we'll need to have a bitlock, so go ahead and convert these bools to use atomic bitops instead. Also, clean up the initialization of the flags field. There's no need to unset each bit individually just after it was zeroed on allocation. Signed-off-by: Jeff Layton Signed-off-by: Steve French --- fs/cifs/inode.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'fs/cifs/inode.c') diff --git a/fs/cifs/inode.c b/fs/cifs/inode.c index a22d667f1069..fa9ef8d902b5 100644 --- a/fs/cifs/inode.c +++ b/fs/cifs/inode.c @@ -117,7 +117,7 @@ cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr) cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n", __func__, cifs_i->uniqueid); - cifs_i->invalid_mapping = true; + set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags); } /* @@ -177,7 +177,10 @@ cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr) else cifs_i->time = jiffies; - cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING; + if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING) + set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags); + else + clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags); cifs_i->server_eof = fattr->cf_eof; /* @@ -1121,7 +1124,7 @@ cifs_rename_pending_delete(const char *full_path, struct dentry *dentry, } /* try to set DELETE_ON_CLOSE */ - if (!cifsInode->delete_pending) { + if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) { rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid, current->tgid); /* @@ -1138,7 +1141,7 @@ cifs_rename_pending_delete(const char *full_path, struct dentry *dentry, rc = -EBUSY; goto undo_rename; } - cifsInode->delete_pending = true; + set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags); } out_close: @@ -1761,14 +1764,14 @@ cifs_invalidate_mapping(struct inode *inode) int rc = 0; struct cifsInodeInfo *cifs_i = CIFS_I(inode); - cifs_i->invalid_mapping = false; + clear_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags); if (inode->i_mapping && inode->i_mapping->nrpages != 0) { rc = invalidate_inode_pages2(inode->i_mapping); if (rc) { cifs_dbg(VFS, "%s: could not invalidate inode %p\n", __func__, inode); - cifs_i->invalid_mapping = true; + set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags); } } @@ -1842,7 +1845,7 @@ int cifs_revalidate_file(struct file *filp) if (rc) return rc; - if (CIFS_I(inode)->invalid_mapping) + if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags)) rc = cifs_invalidate_mapping(inode); return rc; } @@ -1857,7 +1860,7 @@ int cifs_revalidate_dentry(struct dentry *dentry) if (rc) return rc; - if (CIFS_I(inode)->invalid_mapping) + if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags)) rc = cifs_invalidate_mapping(inode); return rc; } -- cgit v1.2.2 From e284e53fdea1dfd66e73c239fa190685985ae465 Mon Sep 17 00:00:00 2001 From: Jeff Layton Date: Wed, 30 Apr 2014 09:31:46 -0400 Subject: cifs: new helper function: cifs_revalidate_mapping Consolidate a bit of code. In a later patch we'll expand this to fix some races. Signed-off-by: Jeff Layton Signed-off-by: Steve French --- fs/cifs/inode.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'fs/cifs/inode.c') diff --git a/fs/cifs/inode.c b/fs/cifs/inode.c index fa9ef8d902b5..ff420b275777 100644 --- a/fs/cifs/inode.c +++ b/fs/cifs/inode.c @@ -1779,6 +1779,14 @@ cifs_invalidate_mapping(struct inode *inode) return rc; } +int +cifs_revalidate_mapping(struct inode *inode) +{ + if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags)) + return cifs_invalidate_mapping(inode); + return 0; +} + int cifs_revalidate_file_attr(struct file *filp) { int rc = 0; @@ -1845,9 +1853,7 @@ int cifs_revalidate_file(struct file *filp) if (rc) return rc; - if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags)) - rc = cifs_invalidate_mapping(inode); - return rc; + return cifs_revalidate_mapping(inode); } /* revalidate a dentry's inode attributes */ @@ -1860,9 +1866,7 @@ int cifs_revalidate_dentry(struct dentry *dentry) if (rc) return rc; - if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags)) - rc = cifs_invalidate_mapping(inode); - return rc; + return cifs_revalidate_mapping(inode); } int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry, -- cgit v1.2.2 From 4f73c7d342d57d065bdbc0995cb56d8d1701b0c0 Mon Sep 17 00:00:00 2001 From: Jeff Layton Date: Wed, 30 Apr 2014 09:31:47 -0400 Subject: cifs: fix potential races in cifs_revalidate_mapping The handling of the CIFS_INO_INVALID_MAPPING flag is racy. It's possible for two tasks to attempt to revalidate the mapping at the same time. The first sees that CIFS_INO_INVALID_MAPPING is set. It clears the flag and then calls invalidate_inode_pages2 to start shooting down the pagecache. While that's going on, another task checks the flag and sees that it's clear. It then ends up trusting the pagecache to satisfy a read when it shouldn't. Fix this by adding a bitlock to ensure that the clearing of the flag is atomic with respect to the actual cache invalidation. Also, move the other existing users of cifs_invalidate_mapping to use a new cifs_zap_mapping() function that just sets the INVALID_MAPPING bit and then uses the standard codepath to handle the invalidation. Signed-off-by: Jeff Layton Signed-off-by: Steve French --- fs/cifs/inode.c | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) (limited to 'fs/cifs/inode.c') diff --git a/fs/cifs/inode.c b/fs/cifs/inode.c index ff420b275777..9ff8df8b4d84 100644 --- a/fs/cifs/inode.c +++ b/fs/cifs/inode.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "cifsfs.h" #include "cifspdu.h" @@ -1762,29 +1763,60 @@ int cifs_invalidate_mapping(struct inode *inode) { int rc = 0; - struct cifsInodeInfo *cifs_i = CIFS_I(inode); - - clear_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags); if (inode->i_mapping && inode->i_mapping->nrpages != 0) { rc = invalidate_inode_pages2(inode->i_mapping); - if (rc) { + if (rc) cifs_dbg(VFS, "%s: could not invalidate inode %p\n", __func__, inode); - set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags); - } } cifs_fscache_reset_inode_cookie(inode); return rc; } +/** + * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks + * @word: long word containing the bit lock + */ +static int +cifs_wait_bit_killable(void *word) +{ + if (fatal_signal_pending(current)) + return -ERESTARTSYS; + freezable_schedule_unsafe(); + return 0; +} + int cifs_revalidate_mapping(struct inode *inode) { - if (test_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags)) - return cifs_invalidate_mapping(inode); - return 0; + int rc; + unsigned long *flags = &CIFS_I(inode)->flags; + + rc = wait_on_bit_lock(flags, CIFS_INO_LOCK, cifs_wait_bit_killable, + TASK_KILLABLE); + if (rc) + return rc; + + if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) { + rc = cifs_invalidate_mapping(inode); + if (rc) + set_bit(CIFS_INO_INVALID_MAPPING, flags); + } + + clear_bit_unlock(CIFS_INO_LOCK, flags); + smp_mb__after_clear_bit(); + wake_up_bit(flags, CIFS_INO_LOCK); + + return rc; +} + +int +cifs_zap_mapping(struct inode *inode) +{ + set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags); + return cifs_revalidate_mapping(inode); } int cifs_revalidate_file_attr(struct file *filp) -- cgit v1.2.2