History

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.

David Howells committed 4 years ago

Commit 85e4ea11 parent 65aa5f62 files · +15 −1Browse files

Unified · Split

2 files
  1. fs/fscache/cookie.c +14 −1
  2. include/linux/fscache.h +1 −0
fs/fscache/cookie.c+14−1
@@ -522,7 +522,14 @@ static void fscache_perform_lookup(struct fscache_cookie *cookie) · 521 unmodified lines
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 @@ again_locked: · 228 unmodified lines
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, · 290 unmodified lines
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+1−0
@@ -130,6 +130,7 @@ struct fscache_cookie { · 129 unmodified lines
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_* */