From 09f79e914e807562f046f2d9a647e74310f67042 Mon Sep 17 00:00:00 2001 From: Cebtenzzre Date: Mon, 13 Sep 2021 17:49:15 -0400 Subject: [PATCH] utils: rm_offset_get_from_fd: Don't combine separate extents There can be a hole between two physically adjacent extents. To avoid breaking any code that calls this function, don't merge their representation, since it completely hides the hole and makes the physical length of the on-disk extent look larger than it really is. Fixes #530 --- lib/utilities.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/utilities.c b/lib/utilities.c index b4dae7ef..3423846e 100644 --- a/lib/utilities.c +++ b/lib/utilities.c @@ -1200,11 +1200,11 @@ RmOff rm_offset_get_from_fd(int fd, RmOff file_offset, RmOff *file_offset_next, RmOff result = 0; bool done = FALSE; bool first = TRUE; + struct fiemap_extent fm_last; rm_util_set_nullable_bool(is_last, FALSE); rm_util_set_nullable_bool(is_inline, FALSE); - /* used for detecting contiguous extents */ - unsigned long expected = 0; + memset(&fm_last, 0, sizeof(fm_last)); fsync(fd); @@ -1235,7 +1235,9 @@ RmOff rm_offset_get_from_fd(int fd, RmOff file_offset, RmOff *file_offset_next, first = FALSE; } else { /* check if subsequent extents are contiguous */ - if(fm_ext.fe_physical != expected) { + unsigned long expected_dense = fm_last.fe_physical + fm_last.fe_length; + unsigned long expected = fm_last.fe_physical + fm_ext.fe_logical - fm_last.fe_logical; + if(fm_ext.fe_physical != expected || fm_ext.fe_physical != expected_dense) { /* current extent is not contiguous with previous, so we can stop */ g_free(fm); break; @@ -1259,7 +1261,7 @@ RmOff rm_offset_get_from_fd(int fd, RmOff file_offset, RmOff *file_offset_next, /* move offsets in preparation for reading next extent */ file_offset = fm_ext.fe_logical + fm_ext.fe_length; - expected = fm_ext.fe_physical + fm_ext.fe_length; + fm_last = fm_ext; } g_free(fm);