code wiki / _hdl_build / nx_blockfs.nx
nx_blockfs.nx source
↩ module page · 112 lines · 7878 B
1// nx_blockfs.nx -- SOVEREIGN on-disk FILESYSTEM (OS gap: FILESYSTEM, the block layer; complements the nx_fs coreutils
2// file-ops tool). A block-structured FS over an arena (a RAM/disk image): block 0 = superblock (magic/nblocks/
3// next_free), blocks 1..16 = inode table (one inode/block: used/size/start/count/name/datasum), blocks 17.. =
4// bump-allocated contiguous data (ADDITIVE-ONLY, rule 13: history-preserving). Own format (no ext4/FAT). Persistence:
5// save/load the image to disk.img. Integrity: per-file checksum (tamper-evident).
6// T1 format. T2 create 2 files + ls=2. T3 read-back byte-exact. T4 persist->clear->reload byte-exact. T5 teeth: corrupt->verify detects.
7// expect_exit: 0 Sovereign: nx_syscalls. NEVER-BRICK: operates on RAM/an image file, writes 0 firmware.
8import "nx_syscalls.nx"
9import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
10import "nx_g_puts_lib.nx"
11
12// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
13// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
14// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
15// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
16func g_pn(v: i64) -> i64 { nxi_out(v); return 0 }
17func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c }
18
19const BS: i64 = 256
20const NB: i64 = 256
21const NI: i64 = 16
22const DATA0: i64 = 17
23const MAGIC: i64 = 0x4E534653
24
25func fs_rd(a: *u8, o: i64) -> i64 { var v: i64=0; var i: i64=0; while i<8 { v=v|((a[o+i] as i64)<<(i*8)); i=i+1 } return v }
26func fs_wr(a: *u8, o: i64, v: i64) -> i64 { var i: i64=0; while i<8 { a[o+i]=((v>>(i*8))&255) as u8; i=i+1 } return 0 }
27func ino_off(i: i64) -> i64 { return (1+i)*BS }
28
29func fs_format(a: *u8) -> i64 {
30 var z: i64=0; while z<NB*BS { a[z]=0 as u8; z=z+1 }
31 fs_wr(a,0,MAGIC); fs_wr(a,8,NB); fs_wr(a,16,DATA0)
32 var i: i64=0; while i<NI { fs_wr(a, ino_off(i), 0); i=i+1 }
33 return 0
34}
35func fs_setname(a: *u8, boff: i64, name: *u8) -> i64 { var i: i64=0; while i<31 { if name[i]==(0 as u8) { a[boff+32+i]=0 as u8; i=31 } else { a[boff+32+i]=name[i]; i=i+1 } } a[boff+32+31]=0 as u8; return 0 }
36func fs_namematch(a: *u8, boff: i64, name: *u8) -> i64 { var i: i64=0; while i<32 { let cn: i64 = name[i] as i64; let cb: i64 = a[boff+32+i] as i64; if cn != cb { return 0 } if cn==0 { return 1 } i=i+1 } return 1 }
37func fs_sum(a: *u8, off: i64, n: i64) -> i64 { var s: i64=0; var i: i64=0; while i<n { s=s+(a[off+i] as i64); i=i+1 } return s }
38func fs_find(a: *u8, name: *u8) -> i64 { var i: i64=0; while i<NI { let bo: i64=ino_off(i); if fs_rd(a,bo)==1 { if fs_namematch(a,bo,name)==1 { return i } } i=i+1 } return 0-1 }
39func fs_create(a: *u8, name: *u8, data: *u8, len: i64) -> i64 {
40 var idx: i64=0-1; var i: i64=0; while i<NI { if fs_rd(a,ino_off(i))==0 { idx=i; i=NI } else { i=i+1 } }
41 if idx<0 { return 0-1 }
42 var nb: i64=(len+BS-1)/BS; if nb==0 { nb=1 }
43 let start: i64=fs_rd(a,16)
44 if start+nb > NB { return 0-1 }
45 var k: i64=0; while k<len { a[start*BS+k]=data[k]; k=k+1 }
46 let bo: i64=ino_off(idx)
47 fs_wr(a,bo,1); fs_wr(a,bo+8,len); fs_wr(a,bo+16,start); fs_wr(a,bo+24,nb); fs_setname(a,bo,name); fs_wr(a,bo+64,fs_sum(a,start*BS,len))
48 fs_wr(a,16,start+nb)
49 return idx
50}
51func fs_read(a: *u8, name: *u8, out: *u8) -> i64 {
52 let idx: i64=fs_find(a,name); if idx<0 { return 0-1 }
53 let bo: i64=ino_off(idx); let sz: i64=fs_rd(a,bo+8); let start: i64=fs_rd(a,bo+16)
54 var k: i64=0; while k<sz { out[k]=a[start*BS+k]; k=k+1 }
55 return sz
56}
57func fs_verify(a: *u8, name: *u8) -> i64 {
58 let idx: i64=fs_find(a,name); if idx<0 { return 0-1 }
59 let bo: i64=ino_off(idx); let sz: i64=fs_rd(a,bo+8); let start: i64=fs_rd(a,bo+16); let stored: i64=fs_rd(a,bo+64)
60 if fs_sum(a,start*BS,sz)==stored { return 1 } return 0
61}
62func fs_count(a: *u8) -> i64 { var c: i64=0; var i: i64=0; while i<NI { if fs_rd(a,ino_off(i))==1 { c=c+1 } i=i+1 } return c }
63func fs_save(a: *u8, path: *u8) -> i64 { let fd: i64=sys_openat_wr(path,0x1a4); if fd<0 { return 0-1 } sys_write(fd,a,NB*BS); sys_close(fd); return 0 }
64func fs_load(a: *u8, path: *u8) -> i64 { let fd: i64=sys_openat_rd(path); if fd<0 { return 0-1 } var t: i64=0; var go: i64=1; while go==1 { let k: i64=sys_read(fd,a+t as i64,NB*BS-t); if k<=0 { go=0 } else { t=t+k } } sys_close(fd); return t }
65func streq_n(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 }
66
67func main() -> i64 {
68 g_puts("nx_blockfs (SOVEREIGN on-disk filesystem: superblock/inodes/data + persist + integrity; own format, no ext4/FAT)\n" as *u8)
69 var pass: i64=0; var total: i64=0
70 let a: *u8 = sys_mmap(NB*BS)
71
72 fs_format(a)
73 var t1: i64=0; if fs_rd(a,0)==MAGIC { if fs_rd(a,16)==DATA0 { if fs_count(a)==0 { t1=1 } } }
74 pass=pass+ck("T1: format -> magic 'NSFS', next_free=17, 0 files" as *u8, t1); total=total+1
75
76 let i1: i64=fs_create(a,"hello.txt" as *u8,"Hello Nishi" as *u8,11)
77 let i2: i64=fs_create(a,"data.bin" as *u8,"0123456789ABCDEF" as *u8,16)
78 var t2: i64=0; if i1>=0 { if i2>=0 { if fs_count(a)==2 { t2=1 } } }
79 g_puts(" T2 created hello.txt(inode "); g_pn(i1); g_puts("), data.bin(inode "); g_pn(i2); g_puts("); files="); g_pn(fs_count(a)); g_puts("\n" as *u8)
80 pass=pass+ck("T2: create 2 files, ls count == 2" as *u8, t2); total=total+1
81
82 let o1: *u8 = sys_mmap(256); let s1: i64=fs_read(a,"hello.txt" as *u8,o1)
83 let o2: *u8 = sys_mmap(256); let s2: i64=fs_read(a,"data.bin" as *u8,o2)
84 var t3: i64=0; if s1==11 { if streq_n(o1,"Hello Nishi" as *u8,11)==1 { if s2==16 { if streq_n(o2,"0123456789ABCDEF" as *u8,16)==1 { t3=1 } } } }
85 g_puts(" T3 read hello.txt("); g_pn(s1); g_puts("B)='"); sys_write(1,o1,s1); g_puts("' data.bin("); g_pn(s2); g_puts("B)\n" as *u8)
86 pass=pass+ck("T3: read-back byte-exact (content + size)" as *u8, t3); total=total+1
87
88 fs_save(a,"knowledge/blockfs_disk.img" as *u8)
89 var z: i64=0; while z<NB*BS { a[z]=0 as u8; z=z+1 }
90 fs_load(a,"knowledge/blockfs_disk.img" as *u8)
91 let o3: *u8 = sys_mmap(256); let s3: i64=fs_read(a,"hello.txt" as *u8,o3)
92 var t4: i64=0; if fs_rd(a,0)==MAGIC { if s3==11 { if streq_n(o3,"Hello Nishi" as *u8,11)==1 { t4=1 } } }
93 g_puts(" T4 persisted to disk.img, cleared, reloaded -> hello.txt='"); sys_write(1,o3,s3); g_puts("'\n" as *u8)
94 pass=pass+ck("T4: persistence -- save -> clear -> reload -> files byte-exact (on-disk round-trip)" as *u8, t4); total=total+1
95
96 let vok: i64=fs_verify(a,"data.bin" as *u8)
97 let bo: i64=ino_off(fs_find(a,"hello.txt" as *u8)); let st: i64=fs_rd(a,bo+16)
98 a[st*BS+3] = (a[st*BS+3] ^ (0xFF as u8))
99 let vbad: i64=fs_verify(a,"hello.txt" as *u8)
100 var t5: i64=0; if vok==1 { if vbad==0 { t5=1 } }
101 g_puts(" T5 verify data.bin(uncorrupted)="); g_pn(vok); g_puts(" verify hello.txt(corrupted)="); g_pn(vbad); g_puts("\n" as *u8)
102 pass=pass+ck("T5 (teeth): per-file checksum -- uncorrupted=ok, corrupted byte detected" as *u8, t5); total=total+1
103
104 var okall: i64=0; if pass==total { okall=1 }
105 g_puts("---- nx_blockfs: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8)
106 if okall==1 {
107 let logf: i64=sys_openat_append("knowledge/status/blockfs.log" as *u8, 420)
108 if logf>=0 { let zz: i64=sys_write(logf,"NXBLOCKFS GREEN: sovereign on-disk FS format/create/read/ls + persist round-trip + per-file integrity (own format)\n" as *u8,109); sys_close(logf) }
109 g_puts("verdict=GREEN (sovereign on-disk filesystem: own block format, create/read/ls, on-disk persistence, tamper-evident; the FILESYSTEM gap)\n" as *u8); sys_exit(0); return 0
110 }
111 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
112}