Skip to content

Commit 85e4ea1

Browse files
committed
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: d24af13 ("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]
1 parent 65aa5f6 commit 85e4ea1

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

fs/fscache/cookie.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,14 @@ static void fscache_perform_lookup(struct fscache_cookie *cookie)
522522
}
523523

524524
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);
526533
trace = fscache_access_lookup_cookie_end;
527534

528535
out:
@@ -757,6 +764,9 @@ static void fscache_cookie_state_machine(struct fscache_cookie *cookie)
757764
spin_lock(&cookie->lock);
758765
}
759766

767+
if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
768+
fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
769+
760770
switch (state) {
761771
case FSCACHE_COOKIE_STATE_RELINQUISHING:
762772
fscache_see_cookie(cookie, fscache_cookie_see_relinquish);
@@ -1053,6 +1063,9 @@ void __fscache_invalidate(struct fscache_cookie *cookie,
10531063
return;
10541064

10551065
case FSCACHE_COOKIE_STATE_LOOKING_UP:
1066+
__fscache_begin_cookie_access(cookie, fscache_access_invalidate_cookie);
1067+
set_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags);
1068+
fallthrough;
10561069
case FSCACHE_COOKIE_STATE_CREATING:
10571070
spin_unlock(&cookie->lock);
10581071
_leave(" [look %x]", cookie->inval_counter);

include/linux/fscache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ struct fscache_cookie {
130130
#define FSCACHE_COOKIE_DO_PREP_TO_WRITE 12 /* T if cookie needs write preparation */
131131
#define FSCACHE_COOKIE_HAVE_DATA 13 /* T if this cookie has data stored */
132132
#define FSCACHE_COOKIE_IS_HASHED 14 /* T if this cookie is hashed */
133+
#define FSCACHE_COOKIE_DO_INVALIDATE 15 /* T if cookie needs invalidation */
133134

134135
enum fscache_cookie_state state;
135136
u8 advice; /* FSCACHE_ADV_* */

0 commit comments

Comments
 (0)