From b11e3e4a2804779a1444c4f4ab2f564e960f8474 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 7 Nov 2017 16:39:58 +0100 Subject: [SERVER] Properly clamp to 4k borders in updateCachemap() Refs #3231 --- src/server/image.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/server') diff --git a/src/server/image.c b/src/server/image.c index f0db0a8..d5dd208 100644 --- a/src/server/image.c +++ b/src/server/image.c @@ -110,8 +110,15 @@ void image_updateCachemap(dnbd3_image_t *image, uint64_t start, uint64_t end, co assert( image != NULL ); // This should always be block borders due to how the protocol works, but better be safe // than accidentally mark blocks as cached when they really aren't entirely cached. - end &= ~(uint64_t)(DNBD3_BLOCK_SIZE - 1); - start = (uint64_t)(start + DNBD3_BLOCK_SIZE - 1) & ~(uint64_t)(DNBD3_BLOCK_SIZE - 1); + if ( set ) { + // If we set as cached, move "inwards" in case we're not at 4k border + end &= ~(uint64_t)(DNBD3_BLOCK_SIZE - 1); + start = (uint64_t)(start + DNBD3_BLOCK_SIZE - 1) & ~(uint64_t)(DNBD3_BLOCK_SIZE - 1); + } else { + // If marking as NOT cached, move "outwards" in case we're not at 4k border + start &= ~(uint64_t)(DNBD3_BLOCK_SIZE - 1); + end = (uint64_t)(end + DNBD3_BLOCK_SIZE - 1) & ~(uint64_t)(DNBD3_BLOCK_SIZE - 1); + } bool dirty = false; uint64_t pos = start; spin_lock( &image->lock ); -- cgit v1.2.3-55-g7522