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
522 }522 }
523 523
524 fscache_see_cookie(cookie, fscache_cookie_see_active);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 trace = fscache_access_lookup_cookie_end;533 trace = fscache_access_lookup_cookie_end;
527 534
528out:535out:
@@ -757,6 +764,9 @@ again_locked: · 228 unmodified lines
757 spin_lock(&cookie->lock);764 spin_lock(&cookie->lock);
758 }765 }
759 766
767 if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
768 fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
769
760 switch (state) {770 switch (state) {
761 case FSCACHE_COOKIE_STATE_RELINQUISHING:771 case FSCACHE_COOKIE_STATE_RELINQUISHING:
762 fscache_see_cookie(cookie, fscache_cookie_see_relinquish);772 fscache_see_cookie(cookie, fscache_cookie_see_relinquish);
@@ -1053,6 +1063,9 @@ void __fscache_invalidate(struct fscache_cookie *cookie, · 290 unmodified lines
1053 return;1063 return;
1054 1064
1055 case FSCACHE_COOKIE_STATE_LOOKING_UP:1065 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;
1056 case FSCACHE_COOKIE_STATE_CREATING:1069 case FSCACHE_COOKIE_STATE_CREATING:
1057 spin_unlock(&cookie->lock);1070 spin_unlock(&cookie->lock);
1058 _leave(" [look %x]", cookie->inval_counter);1071 _leave(" [look %x]", cookie->inval_counter);
include/linux/fscache.h+1−0
@@ -130,6 +130,7 @@ struct fscache_cookie { · 129 unmodified lines
130#define FSCACHE_COOKIE_DO_PREP_TO_WRITE 12 /* T if cookie needs write preparation */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 */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 */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 enum fscache_cookie_state state;135 enum fscache_cookie_state state;
135 u8 advice; /* FSCACHE_ADV_* */136 u8 advice; /* FSCACHE_ADV_* */