diff options
| author | Bobby <[email protected]> | 2026-02-20 16:35:30 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-02-20 16:35:30 +0530 |
| commit | 3f68f9d0d1f833811eb60a577327e41404eba2b5 (patch) | |
| tree | 0d74327d688b75a29c8982e6bc6bac3e95344f09 | |
| parent | 51aaf6e5f1dfd8cefc2dd936fedf10cab79c3a45 (diff) | |
| download | akiba-3f68f9d0d1f833811eb60a577327e41404eba2b5.tar.xz akiba-3f68f9d0d1f833811eb60a577327e41404eba2b5.zip | |
feat: Add used_clusters tracking in AFS and update disk info retrieval
| -rw-r--r-- | mirai/fs/afs/afs.zig | 12 | ||||
| -rw-r--r-- | mirai/fs/afs/cluster.zig | 6 | ||||
| -rw-r--r-- | mirai/fs/afs/info.zig | 38 | ||||
| -rw-r--r-- | mirai/fs/afs/types.zig | 3 | ||||
| -rw-r--r-- | mirai/invocations/os/diskinfo.zig | 5 | ||||
| -rw-r--r-- | toolchain/grub-afs/afs.c | 3 | ||||
| -rw-r--r-- | toolchain/mkafsdisk/main.zig | 8 |
7 files changed, 31 insertions, 44 deletions
diff --git a/mirai/fs/afs/afs.zig b/mirai/fs/afs/afs.zig index 4f33ade..a7217a2 100644 --- a/mirai/fs/afs/afs.zig +++ b/mirai/fs/afs/afs.zig @@ -26,6 +26,7 @@ pub fn AFS(comptime BlockDeviceType: type) type { alloc_table_sector: u32, alloc_table_size: u32, data_area_sector: u32, + used_clusters: u32, parent_cache: [fs.PARENT_CACHE_SIZE]types.ParentCacheEntry, parent_cache_count: usize, @@ -57,6 +58,7 @@ pub fn AFS(comptime BlockDeviceType: type) type { .alloc_table_sector = boot.alloc_table_sector, .alloc_table_size = boot.alloc_table_size, .data_area_sector = boot.data_area_sector, + .used_clusters = boot.used_clusters, .parent_cache = undefined, .parent_cache_count = 0, }; @@ -68,6 +70,16 @@ pub fn AFS(comptime BlockDeviceType: type) type { return cluster_ops.to_lba(self, cluster); } + pub fn increment_used(self: *Self) void { + self.used_clusters += 1; + } + + pub fn decrement_used(self: *Self) void { + if (self.used_clusters > 0) { + self.used_clusters -= 1; + } + } + // Cache operations pub fn get_parent_cluster(self: *Self, cluster: u32) ?u32 { diff --git a/mirai/fs/afs/cluster.zig b/mirai/fs/afs/cluster.zig index 89fcaf2..50f3ca2 100644 --- a/mirai/fs/afs/cluster.zig +++ b/mirai/fs/afs/cluster.zig @@ -56,9 +56,15 @@ pub fn allocate(afs: anytype) !u32 { if (endian.read_u32_le(sector[offset..]) == fs.CLUSTER_FREE) { try write_alloc(afs, cluster, fs.CLUSTER_END); + afs.increment_used(); return cluster; } } return error.DiskFull; } + +pub fn free(afs: anytype, cluster: u32) !void { + try write_alloc(afs, cluster, fs.CLUSTER_FREE); + afs.decrement_used(); +} diff --git a/mirai/fs/afs/info.zig b/mirai/fs/afs/info.zig index 8c3df0a..8d9889e 100644 --- a/mirai/fs/afs/info.zig +++ b/mirai/fs/afs/info.zig @@ -1,49 +1,15 @@ //! AFS disk info operations -const endian = @import("../../utils/bytes/endian.zig"); const fs = @import("../../common/constants/fs.zig"); const types = @import("types.zig"); pub fn get_disk_info(afs: anytype) types.DiskInfo { const cluster_size: u64 = @as(u64, afs.sectors_per_cluster) * fs.SECTOR_SIZE; const total = @as(u64, afs.total_clusters) * cluster_size; - - // Count used clusters by scanning allocation table in bulk - var used_clusters: u64 = 0; - var sector_buf: [fs.SECTOR_SIZE]u8 align(fs.SECTOR_ALIGN) = undefined; - - const entries_per_sector = fs.SECTOR_SIZE / 4; // 4 bytes per entry - const table_sectors = (afs.total_clusters + entries_per_sector - 1) / entries_per_sector; - - var sector_idx: u32 = 0; - while (sector_idx < table_sectors) : (sector_idx += 1) { - const table_sector = afs.partition_offset + afs.alloc_table_sector + sector_idx; - - if (!afs.device.read_sector(table_sector, §or_buf)) { - break; - } - - // Count non-free entries in this sector - var entry_idx: usize = 0; - while (entry_idx < entries_per_sector) : (entry_idx += 1) { - const cluster = sector_idx * entries_per_sector + entry_idx; - if (cluster >= afs.total_clusters) break; - if (cluster < fs.CLUSTER_MIN) { - entry_idx += 1; - continue; - } - - const offset = entry_idx * 4; - const value = endian.read_u32_le(sector_buf[offset..]); - - if (value != fs.CLUSTER_FREE) { - used_clusters += 1; - } - } - } + const used = @as(u64, afs.used_clusters) * cluster_size; return types.DiskInfo{ .total_bytes = total, - .used_bytes = used_clusters * cluster_size, + .used_bytes = used, }; } diff --git a/mirai/fs/afs/types.zig b/mirai/fs/afs/types.zig index 2ee5352..77ad084 100644 --- a/mirai/fs/afs/types.zig +++ b/mirai/fs/afs/types.zig @@ -12,7 +12,8 @@ pub const BootSector = extern struct { alloc_table_sector: u32, alloc_table_size: u32, data_area_sector: u32, - reserved: [466]u8, + used_clusters: u32, + reserved: [462]u8, boot_signature: u16, }; diff --git a/mirai/invocations/os/diskinfo.zig b/mirai/invocations/os/diskinfo.zig index 7844e9b..62c73dc 100644 --- a/mirai/invocations/os/diskinfo.zig +++ b/mirai/invocations/os/diskinfo.zig @@ -2,7 +2,6 @@ const afs = @import("../../fs/afs/afs.zig"); const ahci = @import("../../drivers/ahci/ahci.zig"); -const cpu = @import("../../asm/cpu.zig"); const handler = @import("../handler.zig"); const result = @import("../../utils/types/result.zig"); @@ -17,11 +16,7 @@ pub fn invoke(ctx: *handler.InvocationContext) void { const used_ptr: *u64 = @ptrFromInt(ctx.rsi); if (fs_instance) |fs| { - // Disable interrupts during disk read to prevent race conditions - cpu.disable_interrupts(); const info = fs.get_disk_info(); - cpu.enable_interrupts(); - total_ptr.* = info.total_bytes; used_ptr.* = info.used_bytes; result.set_ok(ctx); diff --git a/toolchain/grub-afs/afs.c b/toolchain/grub-afs/afs.c index 536c189..5bf578b 100644 --- a/toolchain/grub-afs/afs.c +++ b/toolchain/grub-afs/afs.c @@ -22,7 +22,8 @@ struct afs_boot_sector grub_uint32_t alloc_table_sector; grub_uint32_t alloc_table_size; grub_uint32_t data_area_sector; - grub_uint8_t reserved[466]; + grub_uint32_t used_clusters; + grub_uint8_t reserved[462]; grub_uint16_t boot_signature; } GRUB_PACKED; diff --git a/toolchain/mkafsdisk/main.zig b/toolchain/mkafsdisk/main.zig index ab932f0..2312e17 100644 --- a/toolchain/mkafsdisk/main.zig +++ b/toolchain/mkafsdisk/main.zig @@ -14,7 +14,8 @@ const AFSBootSector = extern struct { alloc_table_sector: u32, alloc_table_size: u32, data_area_sector: u32, - reserved: [466]u8, + used_clusters: u32, + reserved: [462]u8, boot_signature: u16, }; @@ -629,6 +630,7 @@ fn createAFS(allocator: mem.Allocator, file: fs.File, source_dir: []const u8, pa boot.alloc_table_sector = 1; boot.alloc_table_size = alloc_table_size; boot.data_area_sector = data_area_start; + boot.used_clusters = 0; boot.boot_signature = 0xAA55; try file.seekTo(@as(u64, partition_start) * 512); @@ -642,6 +644,10 @@ fn createAFS(allocator: mem.Allocator, file: fs.File, source_dir: []const u8, pa try copyDirectoryAFS(allocator, file, source_dir, partition_start, data_area_start, cluster_alloc, root_cluster); try writeAllocationTable(file, partition_start, alloc_table_size, cluster_alloc); + // Update used_clusters in boot sector + boot.used_clusters = cluster_alloc.next_cluster - 2; // Subtract reserved clusters + try file.seekTo(@as(u64, partition_start) * 512); + try file.writeAll(mem.asBytes(&boot)); std.debug.print(" AFS partition created\n", .{}); } |
