From 4e59ea9568ca27e14f2b6e461958c57f027964a1 Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Mon, 5 Mar 2018 11:16:00 -0500 Subject: [PATCH] librbd: discard from cache after committed to disk This prevents a possible race condition with readahead racing with a discard and re-populating the cache with non-zeroed data. Signed-off-by: Jason Dillaman --- .../cache/ObjectCacherObjectDispatch.cc | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/librbd/cache/ObjectCacherObjectDispatch.cc b/src/librbd/cache/ObjectCacherObjectDispatch.cc index 32a1d8952773f..8531a190ba139 100644 --- a/src/librbd/cache/ObjectCacherObjectDispatch.cc +++ b/src/librbd/cache/ObjectCacherObjectDispatch.cc @@ -215,12 +215,19 @@ bool ObjectCacherObjectDispatch::discard( ldout(cct, 20) << "object_no=" << object_no << " " << object_off << "~" << object_len << dendl; - ObjectExtents object_extents; - object_extents.emplace_back(oid, object_no, object_off, object_len, 0); - - m_cache_lock.Lock(); - m_object_cacher->discard_set(m_object_set, object_extents); - m_cache_lock.Unlock(); + // discard the cache state after changes are committed to disk + auto ctx = *on_finish; + *on_finish = new FunctionContext( + [this, oid, object_no, object_off, object_len, ctx](int r) { + ObjectExtents object_extents; + object_extents.emplace_back(oid, object_no, object_off, object_len, 0); + + m_cache_lock.Lock(); + m_object_cacher->discard_set(m_object_set, object_extents); + m_cache_lock.Unlock(); + + ctx->complete(r); + }); // pass-through the discard request since ObjectCacher won't // writeback discards. -- 2.39.5