From b77646f23e5012335d5f7dd8618d559fb7d75315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 5 Sep 2025 00:12:37 +0800 Subject: [PATCH] Btrfs (Linux): adds tracking of Btrfs data copy counts Fixes(?) #1941 --- src/detection/btrfs/btrfs.h | 1 + src/detection/btrfs/btrfs_linux.c | 12 ++++++++++++ src/modules/btrfs/btrfs.c | 1 + 3 files changed, 14 insertions(+) diff --git a/src/detection/btrfs/btrfs.h b/src/detection/btrfs/btrfs.h index 8940ebc8a..19338ce64 100644 --- a/src/detection/btrfs/btrfs.h +++ b/src/detection/btrfs/btrfs.h @@ -9,6 +9,7 @@ typedef struct FFBtrfsDiskUsage uint64_t used; const char* type; bool dup; + uint8_t copies; // 1=single/raid0/raid5/raid6, 2=dup/raid1/raid10, 3=raid1c3, 4=raid1c4 } FFBtrfsDiskUsage; typedef struct FFBtrfsResult diff --git a/src/detection/btrfs/btrfs_linux.c b/src/detection/btrfs/btrfs_linux.c index e3f626717..3c4455910 100644 --- a/src/detection/btrfs/btrfs_linux.c +++ b/src/detection/btrfs/btrfs_linux.c @@ -81,6 +81,15 @@ static const char* detectAllocation(FFBtrfsResult* item, int dfd, FFstrbuf* buff item->allocation[index].used = ffStrbufToUInt(buffer, 0); \ \ item->allocation[index].dup = faccessat(subfd, #_type "/dup/", F_OK, 0) == 0; \ + do { \ + uint8_t _copies = 1; \ + if (faccessat(subfd, #_type "/raid1c4/", F_OK, 0) == 0) _copies = 4; \ + else if (faccessat(subfd, #_type "/raid1c3/", F_OK, 0) == 0) _copies = 3; \ + else if (faccessat(subfd, #_type "/raid1/", F_OK, 0) == 0) _copies = 2; \ + else if (faccessat(subfd, #_type "/raid10/", F_OK, 0) == 0) _copies = 2; \ + else if (item->allocation[index].dup) _copies = 2; /* DUP on single device */ \ + item->allocation[index].copies = _copies; \ + } while(0); \ \ item->allocation[index].type = #_type; @@ -90,6 +99,9 @@ static const char* detectAllocation(FFBtrfsResult* item, int dfd, FFstrbuf* buff #undef FF_BTRFS_DETECT_TYPE + if (item->allocation[0].copies > 1) // index 0 = data + item->totalSize /= item->allocation[0].copies; + return NULL; } diff --git a/src/modules/btrfs/btrfs.c b/src/modules/btrfs/btrfs.c index e021d8035..947af51b1 100644 --- a/src/modules/btrfs/btrfs.c +++ b/src/modules/btrfs/btrfs.c @@ -185,6 +185,7 @@ bool ffGenerateBtrfsJsonResult(FF_MAYBE_UNUSED FFBtrfsOptions* options, yyjson_m yyjson_mut_val* item = yyjson_mut_arr_add_obj(doc, allocation); yyjson_mut_obj_add_str(doc, item, "type", btrfs->allocation[i].type); yyjson_mut_obj_add_bool(doc, item, "dup", btrfs->allocation[i].dup); + yyjson_mut_obj_add_uint(doc, item, "copies", btrfs->allocation[i].copies); yyjson_mut_obj_add_uint(doc, item, "used", btrfs->allocation[i].used); yyjson_mut_obj_add_uint(doc, item, "total", btrfs->allocation[i].total); }