Skip to content

Commit bc1ecd5

Browse files
committed
Refactor index and I/O constants usage
- Use defined constants for index parsing and I/O operations - Enhance clarity by replacing magic numbers with named constants
1 parent ba9208f commit bc1ecd5

5 files changed

Lines changed: 31 additions & 20 deletions

File tree

bfc_format.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ bfc_parse_index_entry(const uint8_t *buf, size_t buflen, size_t *offset,
144144
size_t remaining = buflen - *offset;
145145
uint32_t path_len;
146146

147-
/* Need at least 4 bytes for path_len */
148-
if (remaining < 4)
147+
/* Need at least BFC_PATH_LEN_SIZE bytes for path_len */
148+
if (remaining < BFC_PATH_LEN_SIZE)
149149
return (EINVAL);
150150

151151
/* Read path length (32-bit) */
152152
READ_LE32(p, path_len);
153-
remaining -= 4;
153+
remaining -= BFC_PATH_LEN_SIZE;
154154

155155
/* Sanity check path length */
156156
if (path_len == 0 || path_len > BFC_MAX_PATH_LEN || path_len > remaining)
@@ -191,6 +191,8 @@ int
191191
bfc_read_index(struct bfcfs_mount *bmp, struct thread *td)
192192
{
193193
uint8_t *index_buf = NULL;
194+
uint8_t *p;
195+
uint32_t version, count;
194196
size_t offset = 0;
195197
int error, i;
196198

@@ -222,13 +224,13 @@ bfc_read_index(struct bfcfs_mount *bmp, struct thread *td)
222224
}
223225

224226
/* Parse index header (version and count) */
225-
uint32_t version, count;
226-
uint8_t *p = index_buf;
227+
p = index_buf;
227228
READ_LE32(p, version);
228229
READ_LE32(p, count);
229230

230-
if (version != 1) {
231-
BFCFS_ERR("unsupported index version: %u", version);
231+
if (version != BFC_INDEX_VERSION) {
232+
BFCFS_ERR("unsupported index version: %u (expected %u)",
233+
version, BFC_INDEX_VERSION);
232234
free(index_buf, M_BFCFS);
233235
return (EINVAL);
234236
}
@@ -247,11 +249,11 @@ bfc_read_index(struct bfcfs_mount *bmp, struct thread *td)
247249
bmp->index = malloc(bmp->num_entries * sizeof(struct bfcfs_index_entry),
248250
M_BFCFS, M_WAITOK | M_ZERO);
249251

250-
/* Parse all entries (starting after 8-byte header) */
251-
offset = 8;
252+
/* Parse all entries (starting after index header) */
253+
offset = BFC_INDEX_HEADER_SIZE;
252254
for (i = 0; i < (int)bmp->num_entries; i++) {
253255
error = bfc_parse_index_entry(index_buf, bmp->footer.index_size,
254-
&offset, &bmp->index[i], i + 1); /* inode numbers start at 1 */
256+
&offset, &bmp->index[i], i + BFC_INODE_START);
255257
if (error) {
256258
BFCFS_ERR("failed to parse index entry %d: %d", i, error);
257259
goto fail;

bfc_io.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ bfc_crc32c(const void *buf, size_t len)
7878
{
7979
/*
8080
* calculate_crc32c() from FreeBSD already handles initialization and XOR
81-
* Just pass 0 as initial CRC for a new calculation
81+
* Just pass BFC_CRC32C_INIT as initial CRC for a new calculation
8282
*/
83-
return calculate_crc32c(0, buf, len);
83+
return calculate_crc32c(BFC_CRC32C_INIT, buf, len);
8484
}
8585

8686
/*

bfcfs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@
4343
#define BFC_MAX_PATH_LEN 4096 /* Maximum path length */
4444

4545
/* BFC index format constants */
46+
#define BFC_INDEX_VERSION 1 /* Supported index version */
4647
#define BFC_INDEX_HEADER_SIZE 8 /* Version (4) + count (4) */
4748
#define BFC_INDEX_ENTRY_METADATA_SIZE 48 /* Size of fixed metadata fields */
49+
#define BFC_PATH_LEN_SIZE 4 /* Size of path_len field (uint32_t) */
50+
#define BFC_INODE_START 1 /* First inode number (root is synthetic) */
4851

4952
/* Time conversion constants */
5053
#define NSEC_PER_SEC 1000000000ULL /* Nanoseconds per second */
@@ -75,6 +78,10 @@
7578
#define BFC_NAME_MAX 255 /* Maximum filename length */
7679
#define BFC_PATH_MAX 1024 /* Maximum path length for pathconf */
7780
#define BFC_FILESIZEBITS 64 /* File size bits (64-bit offsets) */
81+
#define BFC_LINK_MAX 1 /* Max hard links (read-only FS) */
82+
83+
/* I/O constants */
84+
#define BFC_CRC32C_INIT 0 /* Initial CRC32C value */
7885

7986
/* BFC object types */
8087
#define BFC_TYPE_FILE 1

bfcfs_vfs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ bfcfs_mount(struct mount *mp)
137137
LIST_INIT(&bmp->index_list);
138138

139139
/* Parse optional mount options */
140-
bmp->verify_crc = 1; /* Default: verify CRC */
140+
bmp->verify_crc = 1; /* Default: verify CRC enabled */
141+
bmp->no_readahead = 0; /* Default: readahead enabled */
141142
if (vfs_getopt(mp->mnt_optnew, "noverify", NULL, NULL) == 0)
142143
bmp->verify_crc = 0;
143144
if (vfs_getopt(mp->mnt_optnew, "noreadahead", NULL, NULL) == 0)

bfcfs_vnops.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <sys/stat.h>
1515
#include <sys/buf.h>
1616
#include <sys/bio.h>
17+
#include <sys/unistd.h>
1718
#include <vm/vm.h>
1819
#include <vm/vnode_pager.h>
1920

@@ -475,19 +476,19 @@ bfcfs_pathconf(struct vop_pathconf_args *ap)
475476
int error = 0;
476477

477478
switch (ap->a_name) {
478-
case 1: /* _PC_LINK_MAX */
479-
*ap->a_retval = 1;
479+
case _PC_LINK_MAX:
480+
*ap->a_retval = BFC_LINK_MAX;
480481
break;
481-
case 4: /* _PC_NAME_MAX */
482+
case _PC_NAME_MAX:
482483
*ap->a_retval = BFC_NAME_MAX;
483484
break;
484-
case 5: /* _PC_PATH_MAX */
485+
case _PC_PATH_MAX:
485486
*ap->a_retval = BFC_PATH_MAX;
486487
break;
487-
case 7: /* _PC_NO_TRUNC */
488-
*ap->a_retval = 1;
488+
case _PC_NO_TRUNC:
489+
*ap->a_retval = 1; /* Filenames are never truncated */
489490
break;
490-
case 18: /* _PC_FILESIZEBITS */
491+
case _PC_FILESIZEBITS:
491492
*ap->a_retval = BFC_FILESIZEBITS;
492493
break;
493494
default:

0 commit comments

Comments
 (0)