1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
| #include <stdint.h> #include <stdbool.h> #include <string.h>
#define BLOCK_SIZE 512 #define MAX_FILENAME_LEN 255 #define MAX_FILES 1024 #define MAX_BLOCKS 8192 #define INODE_SIZE 128 #define DIRECT_BLOCKS 12 #define INDIRECT_BLOCKS 1 #define DOUBLE_INDIRECT 1
typedef enum { FILE_TYPE_REGULAR = 1, FILE_TYPE_DIRECTORY = 2, FILE_TYPE_SYMLINK = 3, FILE_TYPE_DEVICE = 4 } file_type_t;
typedef struct { uint32_t inode_num; file_type_t type; uint32_t size; uint32_t blocks; uint32_t uid; uint32_t gid; uint32_t mode; uint32_t atime; uint32_t mtime; uint32_t ctime; uint32_t direct_blocks[DIRECT_BLOCKS]; uint32_t indirect_block; uint32_t double_indirect_block; uint32_t link_count; uint8_t reserved[32]; } inode_t;
typedef struct { uint32_t inode_num; uint16_t rec_len; uint8_t name_len; uint8_t file_type; char name[MAX_FILENAME_LEN]; } __attribute__((packed)) dir_entry_t;
typedef struct { uint32_t magic; uint32_t block_count; uint32_t inode_count; uint32_t free_blocks; uint32_t free_inodes; uint32_t first_data_block; uint32_t block_size; uint32_t inode_size; uint32_t blocks_per_group; uint32_t inodes_per_group; uint32_t mount_time; uint32_t write_time; uint16_t mount_count; uint16_t max_mount_count; uint16_t state; uint16_t errors; uint8_t uuid[16]; char volume_name[16]; uint8_t reserved[1024 - 100]; } __attribute__((packed)) superblock_t;
typedef struct { superblock_t* superblock; uint8_t* block_bitmap; uint8_t* inode_bitmap; inode_t* inode_table; uint8_t* data_blocks; bool mounted; } filesystem_t;
static filesystem_t fs = {0};
void set_bit(uint8_t* bitmap, uint32_t bit) { bitmap[bit / 8] |= (1 << (bit % 8)); }
void clear_bit(uint8_t* bitmap, uint32_t bit) { bitmap[bit / 8] &= ~(1 << (bit % 8)); }
bool test_bit(uint8_t* bitmap, uint32_t bit) { return (bitmap[bit / 8] & (1 << (bit % 8))) != 0; }
uint32_t find_free_block(void) { for (uint32_t i = 0; i < fs.superblock->block_count; i++) { if (!test_bit(fs.block_bitmap, i)) { return i; } } return 0; }
uint32_t find_free_inode(void) { for (uint32_t i = 1; i < fs.superblock->inode_count; i++) { if (!test_bit(fs.inode_bitmap, i)) { return i; } } return 0; }
uint32_t allocate_block(void) { uint32_t block = find_free_block(); if (block == 0) { printf("错误: 没有空闲块\n"); return 0; } set_bit(fs.block_bitmap, block); fs.superblock->free_blocks--; printf("分配块: %d\n", block); return block; }
void free_block(uint32_t block) { if (block == 0 || block >= fs.superblock->block_count) { printf("错误: 无效的块号 %d\n", block); return; } clear_bit(fs.block_bitmap, block); fs.superblock->free_blocks++; printf("释放块: %d\n", block); }
uint32_t allocate_inode(void) { uint32_t inode_num = find_free_inode(); if (inode_num == 0) { printf("错误: 没有空闲inode\n"); return 0; } set_bit(fs.inode_bitmap, inode_num); fs.superblock->free_inodes--; inode_t* inode = &fs.inode_table[inode_num]; memset(inode, 0, sizeof(inode_t)); inode->inode_num = inode_num; inode->ctime = inode->mtime = inode->atime = get_system_time(); inode->link_count = 1; printf("分配inode: %d\n", inode_num); return inode_num; }
void free_inode(uint32_t inode_num) { if (inode_num == 0 || inode_num >= fs.superblock->inode_count) { printf("错误: 无效的inode号 %d\n", inode_num); return; } inode_t* inode = &fs.inode_table[inode_num]; for (int i = 0; i < DIRECT_BLOCKS; i++) { if (inode->direct_blocks[i] != 0) { free_block(inode->direct_blocks[i]); } } if (inode->indirect_block != 0) { free_block(inode->indirect_block); } clear_bit(fs.inode_bitmap, inode_num); fs.superblock->free_inodes++; memset(inode, 0, sizeof(inode_t)); printf("释放inode: %d\n", inode_num); }
void read_block(uint32_t block_num, void* buffer) { if (block_num >= fs.superblock->block_count) { printf("错误: 无效的块号 %d\n", block_num); return; } memcpy(buffer, &fs.data_blocks[block_num * BLOCK_SIZE], BLOCK_SIZE); }
void write_block(uint32_t block_num, const void* buffer) { if (block_num >= fs.superblock->block_count) { printf("错误: 无效的块号 %d\n", block_num); return; } memcpy(&fs.data_blocks[block_num * BLOCK_SIZE], buffer, BLOCK_SIZE); }
uint32_t create_file(const char* name, file_type_t type, uint32_t mode) { uint32_t inode_num = allocate_inode(); if (inode_num == 0) { return 0; } inode_t* inode = &fs.inode_table[inode_num]; inode->type = type; inode->mode = mode; inode->uid = 0; inode->gid = 0; if (type == FILE_TYPE_DIRECTORY) { uint32_t block = allocate_block(); if (block == 0) { free_inode(inode_num); return 0; } inode->direct_blocks[0] = block; inode->size = BLOCK_SIZE; inode->blocks = 1; dir_entry_t dir_entries[2]; dir_entries[0].inode_num = inode_num; dir_entries[0].rec_len = sizeof(dir_entry_t); dir_entries[0].name_len = 1; dir_entries[0].file_type = FILE_TYPE_DIRECTORY; strcpy(dir_entries[0].name, "."); dir_entries[1].inode_num = inode_num; dir_entries[1].rec_len = sizeof(dir_entry_t); dir_entries[1].name_len = 2; dir_entries[1].file_type = FILE_TYPE_DIRECTORY; strcpy(dir_entries[1].name, ".."); write_block(block, dir_entries); } printf("创建文件: %s, inode=%d, 类型=%d\n", name, inode_num, type); return inode_num; }
void delete_file(uint32_t inode_num) { if (inode_num == 0 || inode_num >= fs.superblock->inode_count) { printf("错误: 无效的inode号 %d\n", inode_num); return; } inode_t* inode = &fs.inode_table[inode_num]; if (inode->link_count > 1) { inode->link_count--; printf("减少硬链接计数: inode=%d, 链接数=%d\n", inode_num, inode->link_count); return; } printf("删除文件: inode=%d\n", inode_num); free_inode(inode_num); }
int read_file(uint32_t inode_num, void* buffer, uint32_t offset, uint32_t size) { if (inode_num == 0 || inode_num >= fs.superblock->inode_count) { return -1; } inode_t* inode = &fs.inode_table[inode_num]; if (offset >= inode->size) { return 0; } if (offset + size > inode->size) { size = inode->size - offset; } uint32_t bytes_read = 0; uint8_t* buf = (uint8_t*)buffer; while (bytes_read < size) { uint32_t block_index = (offset + bytes_read) / BLOCK_SIZE; uint32_t block_offset = (offset + bytes_read) % BLOCK_SIZE; uint32_t bytes_to_read = BLOCK_SIZE - block_offset; if (bytes_to_read > size - bytes_read) { bytes_to_read = size - bytes_read; } if (block_index >= DIRECT_BLOCKS) { printf("错误: 不支持间接块读取\n"); break; } uint32_t block_num = inode->direct_blocks[block_index]; if (block_num == 0) { memset(buf + bytes_read, 0, bytes_to_read); } else { uint8_t block_buffer[BLOCK_SIZE]; read_block(block_num, block_buffer); memcpy(buf + bytes_read, block_buffer + block_offset, bytes_to_read); } bytes_read += bytes_to_read; } inode->atime = get_system_time(); printf("读取文件: inode=%d, 偏移=%d, 大小=%d, 实际读取=%d\n", inode_num, offset, size, bytes_read); return bytes_read; }
int write_file(uint32_t inode_num, const void* buffer, uint32_t offset, uint32_t size) { if (inode_num == 0 || inode_num >= fs.superblock->inode_count) { return -1; } inode_t* inode = &fs.inode_table[inode_num]; const uint8_t* buf = (const uint8_t*)buffer; uint32_t bytes_written = 0; while (bytes_written < size) { uint32_t block_index = (offset + bytes_written) / BLOCK_SIZE; uint32_t block_offset = (offset + bytes_written) % BLOCK_SIZE; uint32_t bytes_to_write = BLOCK_SIZE - block_offset; if (bytes_to_write > size - bytes_written) { bytes_to_write = size - bytes_written; } if (block_index >= DIRECT_BLOCKS) { printf("错误: 不支持间接块写入\n"); break; } uint32_t block_num = inode->direct_blocks[block_index]; if (block_num == 0) { block_num = allocate_block(); if (block_num == 0) { break; } inode->direct_blocks[block_index] = block_num; inode->blocks++; } uint8_t block_buffer[BLOCK_SIZE]; if (block_offset != 0 || bytes_to_write != BLOCK_SIZE) { read_block(block_num, block_buffer); } memcpy(block_buffer + block_offset, buf + bytes_written, bytes_to_write); write_block(block_num, block_buffer); bytes_written += bytes_to_write; } if (offset + bytes_written > inode->size) { inode->size = offset + bytes_written; } inode->mtime = get_system_time(); printf("写入文件: inode=%d, 偏移=%d, 大小=%d, 实际写入=%d\n", inode_num, offset, size, bytes_written); return bytes_written; }
void filesystem_init(void) { printf("初始化文件系统...\n"); fs.superblock = (superblock_t*)kmalloc(sizeof(superblock_t)); fs.block_bitmap = (uint8_t*)kmalloc(MAX_BLOCKS / 8); fs.inode_bitmap = (uint8_t*)kmalloc(MAX_FILES / 8); fs.inode_table = (inode_t*)kmalloc(MAX_FILES * sizeof(inode_t)); fs.data_blocks = (uint8_t*)kmalloc(MAX_BLOCKS * BLOCK_SIZE); if (!fs.superblock || !fs.block_bitmap || !fs.inode_bitmap || !fs.inode_table || !fs.data_blocks) { printf("错误: 无法分配文件系统内存\n"); return; } memset(fs.superblock, 0, sizeof(superblock_t)); fs.superblock->magic = 0xEF53; fs.superblock->block_count = MAX_BLOCKS; fs.superblock->inode_count = MAX_FILES; fs.superblock->free_blocks = MAX_BLOCKS; fs.superblock->free_inodes = MAX_FILES - 1; fs.superblock->first_data_block = 1; fs.superblock->block_size = BLOCK_SIZE; fs.superblock->inode_size = INODE_SIZE; fs.superblock->mount_time = get_system_time(); fs.superblock->state = 1; strcpy(fs.superblock->volume_name, "SimpleFS"); memset(fs.block_bitmap, 0, MAX_BLOCKS / 8); memset(fs.inode_bitmap, 0, MAX_FILES / 8); set_bit(fs.block_bitmap, 0); set_bit(fs.inode_bitmap, 0); fs.superblock->free_blocks--; memset(fs.inode_table, 0, MAX_FILES * sizeof(inode_t)); memset(fs.data_blocks, 0, MAX_BLOCKS * BLOCK_SIZE); uint32_t root_inode = create_file("/", FILE_TYPE_DIRECTORY, 0755); if (root_inode != 1) { printf("错误: 无法创建根目录\n"); return; } fs.mounted = true; printf("文件系统初始化完成:\n"); printf(" 总块数: %d\n", fs.superblock->block_count); printf(" 总inode数: %d\n", fs.superblock->inode_count); printf(" 块大小: %d字节\n", fs.superblock->block_size); printf(" 根目录inode: %d\n", root_inode); }
void filesystem_demo(void) { printf("=== 文件系统演示 ===\n"); uint32_t file1 = create_file("test.txt", FILE_TYPE_REGULAR, 0644); uint32_t file2 = create_file("data.bin", FILE_TYPE_REGULAR, 0644); uint32_t dir1 = create_file("subdir", FILE_TYPE_DIRECTORY, 0755); const char* text = "Hello, World! This is a test file."; write_file(file1, text, 0, strlen(text)); uint8_t binary_data[256]; for (int i = 0; i < 256; i++) { binary_data[i] = i; } write_file(file2, binary_data, 0, 256); char read_buffer[100]; int bytes_read = read_file(file1, read_buffer, 0, sizeof(read_buffer) - 1); read_buffer[bytes_read] = '\0'; printf("读取文本文件: %s\n", read_buffer); uint8_t read_binary[256]; bytes_read = read_file(file2, read_binary, 0, 256); printf("读取二进制文件: %d字节\n", bytes_read); printf("\n文件系统状态:\n"); printf(" 空闲块数: %d\n", fs.superblock->free_blocks); printf(" 空闲inode数: %d\n", fs.superblock->free_inodes); delete_file(file2); printf("删除文件后:\n"); printf(" 空闲块数: %d\n", fs.superblock->free_blocks); printf(" 空闲inode数: %d\n", fs.superblock->free_inodes); }
|