-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperblock.java
More file actions
79 lines (67 loc) · 2.01 KB
/
Superblock.java
File metadata and controls
79 lines (67 loc) · 2.01 KB
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
package file_structure;
// Superblock.java - File System Metadata
public class Superblock {
private int magicNumber; // File system identifier (0xEF53 for ext2)
private int blockSize; // Typically 4096 bytes
private long totalBlocks; // Total blocks in file system
private long freeBlocks; // Available blocks
private long totalInodes; // Total inodes
private long freeInodes; // Available inodes
private int firstDataBlock; // First block containing data
private long mountTime; // Last mount timestamp
private int mountCount; // Number of times mounted
private FileSystemState state; // CLEAN or DIRTY
public enum FileSystemState {
CLEAN, DIRTY
}
public Superblock(int blockSize, long totalBlocks, long totalInodes) {
this.magicNumber = 0xEF53;
this.blockSize = blockSize;
this.totalBlocks = totalBlocks;
this.freeBlocks = totalBlocks - 1; // Reserve first block
this.totalInodes = totalInodes;
this.freeInodes = totalInodes - 1; // Reserve root inode
this.firstDataBlock = 1;
this.state = FileSystemState.CLEAN;
}
public boolean allocateBlock() {
if (freeBlocks > 0) {
freeBlocks--;
return true;
}
return false;
}
public void freeBlock() {
if (freeBlocks < totalBlocks) {
freeBlocks++;
}
}
public boolean allocateInode() {
if (freeInodes > 0) {
freeInodes--;
return true;
}
return false;
}
public void freeInode() {
if (freeInodes < totalInodes) {
freeInodes++;
}
}
// Getters
public int getBlockSize() {
return blockSize;
}
public long getFreeBlocks() {
return freeBlocks;
}
public long getFreeInodes() {
return freeInodes;
}
public FileSystemState getState() {
return state;
}
public void setState(FileSystemState state) {
this.state = state;
}
}