fscache: Fix invalidation/lookup race

If an NFS file is opened for writing and closed, fscache_invalidate() will
be asked to invalidate the file - however, if the cookie is in the
LOOKING_UP state (or the CREATING state), then request to invalidate
doesn't get recorded for fscache_cookie_state_machine() to do something
with.

Fix this by making __fscache_invalidate() set a flag if it sees the cookie
is in the LOOKING_UP state to indicate that we need to go to invalidation.
Note that this requires a count on the n_accesses counter for the state
machine, which that will release when it's done.

fscache_cookie_state_machine() then shifts to the INVALIDATING state if it
sees the flag.

Without this, an nfs file can get corrupted if it gets modified locally and
then read locally as the cache contents may not get updated.

Fixes: d24af13e2e23 ("fscache: Implement cookie invalidation")
Reported-by: Max Kellermann <mk@cm4all.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Tested-by: Max Kellermann <mk@cm4all.com>
Link: https://lore.kernel.org/r/YlWWbpW5Foynjllo@rabbit.intern.cm-ag [1]

+15 -1
+14 -1
fs/fscache/cookie.c
··· 522 } 523 524 fscache_see_cookie(cookie, fscache_cookie_see_active); 525 - fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_ACTIVE); 526 trace = fscache_access_lookup_cookie_end; 527 528 out: ··· 763 cookie->volume->cache->ops->withdraw_cookie(cookie); 764 spin_lock(&cookie->lock); 765 } 766 767 switch (state) { 768 case FSCACHE_COOKIE_STATE_RELINQUISHING: ··· 1063 return; 1064 1065 case FSCACHE_COOKIE_STATE_LOOKING_UP: 1066 case FSCACHE_COOKIE_STATE_CREATING: 1067 spin_unlock(&cookie->lock); 1068 _leave(" [look %x]", cookie->inval_counter);
··· 522 } 523 524 fscache_see_cookie(cookie, fscache_cookie_see_active); 525 + spin_lock(&cookie->lock); 526 + if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags)) 527 + __fscache_set_cookie_state(cookie, 528 + FSCACHE_COOKIE_STATE_INVALIDATING); 529 + else 530 + __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_ACTIVE); 531 + spin_unlock(&cookie->lock); 532 + wake_up_cookie_state(cookie); 533 trace = fscache_access_lookup_cookie_end; 534 535 out: ··· 756 cookie->volume->cache->ops->withdraw_cookie(cookie); 757 spin_lock(&cookie->lock); 758 } 759 + 760 + if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags)) 761 + fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end); 762 763 switch (state) { 764 case FSCACHE_COOKIE_STATE_RELINQUISHING: ··· 1053 return; 1054 1055 case FSCACHE_COOKIE_STATE_LOOKING_UP: 1056 + __fscache_begin_cookie_access(cookie, fscache_access_invalidate_cookie); 1057 + set_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags); 1058 + fallthrough; 1059 case FSCACHE_COOKIE_STATE_CREATING: 1060 spin_unlock(&cookie->lock); 1061 _leave(" [look %x]", cookie->inval_counter);
+1
include/linux/fscache.h
··· 130 #define FSCACHE_COOKIE_DO_PREP_TO_WRITE 12 /* T if cookie needs write preparation */ 131 #define FSCACHE_COOKIE_HAVE_DATA 13 /* T if this cookie has data stored */ 132 #define FSCACHE_COOKIE_IS_HASHED 14 /* T if this cookie is hashed */ 133 134 enum fscache_cookie_state state; 135 u8 advice; /* FSCACHE_ADV_* */
··· 130 #define FSCACHE_COOKIE_DO_PREP_TO_WRITE 12 /* T if cookie needs write preparation */ 131 #define FSCACHE_COOKIE_HAVE_DATA 13 /* T if this cookie has data stored */ 132 #define FSCACHE_COOKIE_IS_HASHED 14 /* T if this cookie is hashed */ 133 + #define FSCACHE_COOKIE_DO_INVALIDATE 15 /* T if cookie needs invalidation */ 134 135 enum fscache_cookie_state state; 136 u8 advice; /* FSCACHE_ADV_* */