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 522 } 523 523 524 524 fscache_see_cookie(cookie, fscache_cookie_see_active); 525 - fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_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); 526 533 trace = fscache_access_lookup_cookie_end; 527 534 528 535 out: ··· 763 756 cookie->volume->cache->ops->withdraw_cookie(cookie); 764 757 spin_lock(&cookie->lock); 765 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); 766 762 767 763 switch (state) { 768 764 case FSCACHE_COOKIE_STATE_RELINQUISHING: ··· 1063 1053 return; 1064 1054 1065 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; 1066 1059 case FSCACHE_COOKIE_STATE_CREATING: 1067 1060 spin_unlock(&cookie->lock); 1068 1061 _leave(" [look %x]", cookie->inval_counter);
+1
include/linux/fscache.h
··· 130 130 #define FSCACHE_COOKIE_DO_PREP_TO_WRITE 12 /* T if cookie needs write preparation */ 131 131 #define FSCACHE_COOKIE_HAVE_DATA 13 /* T if this cookie has data stored */ 132 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 */ 133 134 134 135 enum fscache_cookie_state state; 135 136 u8 advice; /* FSCACHE_ADV_* */