Skip to content

Commit 5e068d2

Browse files
committed
Btrfs (Linux): refactors Btrfs allocation to use profile string
Fixes #1941
1 parent b77646f commit 5e068d2

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

src/detection/btrfs/btrfs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ typedef struct FFBtrfsDiskUsage
88
uint64_t total;
99
uint64_t used;
1010
const char* type;
11-
bool dup;
12-
uint8_t copies; // 1=single/raid0/raid5/raid6, 2=dup/raid1/raid10, 3=raid1c3, 4=raid1c4
11+
const char* profile; // single / dup / raidx
12+
uint8_t copies;
1313
} FFBtrfsDiskUsage;
1414

1515
typedef struct FFBtrfsResult

src/detection/btrfs/btrfs_linux.c

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,43 @@ static const char* detectAllocation(FFBtrfsResult* item, int dfd, FFstrbuf* buff
7373
item->globalReservationUsed = ffStrbufToUInt(buffer, 0);
7474
item->globalReservationUsed = item->globalReservationTotal - item->globalReservationUsed;
7575

76-
#define FF_BTRFS_DETECT_TYPE(index, _type) \
77-
if (ffReadFileBufferRelative(subfd, #_type "/total_bytes", buffer)) \
78-
item->allocation[index].total = ffStrbufToUInt(buffer, 0); \
79-
\
80-
if (ffReadFileBufferRelative(subfd, #_type "/bytes_used", buffer)) \
81-
item->allocation[index].used = ffStrbufToUInt(buffer, 0); \
82-
\
83-
item->allocation[index].dup = faccessat(subfd, #_type "/dup/", F_OK, 0) == 0; \
76+
#define FF_BTRFS_DETECT_PROFILE(_index, _type, _profile, _copies) \
77+
else if (faccessat(subfd, _type "/" _profile "/", F_OK, 0) == 0) { \
78+
item->allocation[_index].profile = _profile; \
79+
item->allocation[_index].copies = _copies; \
80+
}
81+
82+
#define FF_BTRFS_DETECT_TYPE(_index, _type) \
8483
do { \
85-
uint8_t _copies = 1; \
86-
if (faccessat(subfd, #_type "/raid1c4/", F_OK, 0) == 0) _copies = 4; \
87-
else if (faccessat(subfd, #_type "/raid1c3/", F_OK, 0) == 0) _copies = 3; \
88-
else if (faccessat(subfd, #_type "/raid1/", F_OK, 0) == 0) _copies = 2; \
89-
else if (faccessat(subfd, #_type "/raid10/", F_OK, 0) == 0) _copies = 2; \
90-
else if (item->allocation[index].dup) _copies = 2; /* DUP on single device */ \
91-
item->allocation[index].copies = _copies; \
92-
} while(0); \
93-
\
94-
item->allocation[index].type = #_type;
95-
96-
FF_BTRFS_DETECT_TYPE(0, data);
97-
FF_BTRFS_DETECT_TYPE(1, metadata);
98-
FF_BTRFS_DETECT_TYPE(2, system);
84+
item->allocation[_index].type = _type; \
85+
if (ffReadFileBufferRelative(subfd, _type "/total_bytes", buffer)) \
86+
item->allocation[_index].total = ffStrbufToUInt(buffer, 0); \
87+
\
88+
if (ffReadFileBufferRelative(subfd, _type "/bytes_used", buffer)) \
89+
item->allocation[_index].used = ffStrbufToUInt(buffer, 0); \
90+
\
91+
if (false) {} \
92+
FF_BTRFS_DETECT_PROFILE(_index, _type, "single", 1) \
93+
FF_BTRFS_DETECT_PROFILE(_index, _type, "dup", 2) \
94+
FF_BTRFS_DETECT_PROFILE(_index, _type, "raid0", 1) \
95+
FF_BTRFS_DETECT_PROFILE(_index, _type, "raid1", 2) \
96+
FF_BTRFS_DETECT_PROFILE(_index, _type, "raid10", 2) \
97+
FF_BTRFS_DETECT_PROFILE(_index, _type, "raid1c3", 3) \
98+
FF_BTRFS_DETECT_PROFILE(_index, _type, "raid1c4", 4) \
99+
FF_BTRFS_DETECT_PROFILE(_index, _type, "raid5", 1) /* (n-1)/n */ \
100+
FF_BTRFS_DETECT_PROFILE(_index, _type, "raid6", 1) /* (n-2)/n */ \
101+
else { \
102+
item->allocation[_index].profile = "unknown"; \
103+
item->allocation[_index].copies = 1; \
104+
} \
105+
} while (0)
106+
107+
FF_BTRFS_DETECT_TYPE(0, "data");
108+
FF_BTRFS_DETECT_TYPE(1, "metadata");
109+
FF_BTRFS_DETECT_TYPE(2, "system");
99110

100111
#undef FF_BTRFS_DETECT_TYPE
101112

102-
if (item->allocation[0].copies > 1) // index 0 = data
103-
item->totalSize /= item->allocation[0].copies;
104-
105113
return NULL;
106114
}
107115

src/modules/btrfs/btrfs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ static void printBtrfs(FFBtrfsOptions* options, FFBtrfsResult* result, uint8_t i
2727
}
2828

2929
uint64_t used = 0, allocated = 0, total = result->totalSize;
30-
for (int i = 0; i < 3; ++i)
30+
for (uint32_t i = 0; i < ARRAY_SIZE(result->allocation); ++i)
3131
{
32-
uint64_t times = result->allocation[i].dup ? 2 : 1;
32+
uint64_t times = result->allocation[i].copies;
3333
used += result->allocation[i].used * times;
3434
allocated += result->allocation[i].total * times;
3535
}
@@ -180,11 +180,11 @@ bool ffGenerateBtrfsJsonResult(FF_MAYBE_UNUSED FFBtrfsOptions* options, yyjson_m
180180
yyjson_mut_obj_add_uint(doc, obj, "sectorSize", btrfs->sectorSize);
181181
yyjson_mut_obj_add_uint(doc, obj, "totalSize", btrfs->totalSize);
182182
yyjson_mut_val* allocation = yyjson_mut_obj_add_arr(doc, obj, "allocation");
183-
for (int i = 0; i < 3; ++i)
183+
for (uint32_t i = 0; i < ARRAY_SIZE(btrfs->allocation); ++i)
184184
{
185185
yyjson_mut_val* item = yyjson_mut_arr_add_obj(doc, allocation);
186186
yyjson_mut_obj_add_str(doc, item, "type", btrfs->allocation[i].type);
187-
yyjson_mut_obj_add_bool(doc, item, "dup", btrfs->allocation[i].dup);
187+
yyjson_mut_obj_add_str(doc, item, "profile", btrfs->allocation[i].profile);
188188
yyjson_mut_obj_add_uint(doc, item, "copies", btrfs->allocation[i].copies);
189189
yyjson_mut_obj_add_uint(doc, item, "used", btrfs->allocation[i].used);
190190
yyjson_mut_obj_add_uint(doc, item, "total", btrfs->allocation[i].total);

0 commit comments

Comments
 (0)