code wiki / _hdl_build / nx_vfs_blockfs.nx

nx_vfs_blockfs.nx source

↩ module page · 101 lines · 6976 B

1// nx_vfs_blockfs.nx -- GATE: the hierarchical VFS MOUNTED ON the block filesystem (NSFS v2, nx_vfsblock_lib). 2// Closes the FILESYSTEM census gap half "block-backed VFS mount": the WHOLE namespace (dirs + files + hierarchy) 3// lives in the block image and survives save -> clear -> reload -> MOUNT. Grounded on the banked field research 4// (osb_vfs/osb_mount/osb_inode: mount = attach a validated on-disk FS into the namespace; inode = the on-disk 5// record). Research-grounded exceed kept honest: this is OUR OWN format (NSFS v2), not ext/FAT. 6// T1 format + mount (superblock magic+version validated, root dir valid, empty). 7// T2 build a REAL tree with nesting -- /docs /src /docs/sub + 3 files -- resolve + read + ls all correct. 8// T3 THE MOUNT PROOF: save image -> zero RAM -> load -> mount -> paths resolve + content byte-exact + ls intact. 9// T4 teeth: corrupt a data byte in the image -> per-file checksum DETECTS; restore -> clean again. 10// T5 teeth: corrupt the superblock magic -> mount REFUSED (-1); restore -> mounts again. 11// expect_exit: 0 Sovereign: nx_cc->nxasm via nx_syscalls. NEVER-BRICK: RAM + an image file, 0 firmware writes. 12import "nx_vfsblock_lib.nx" 13import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 14 15func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 16// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 17// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 18// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 19// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 20func g_pn(v: i64) -> i64 { nxi_out(v); return 0 } 21func 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 } 22func 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 } 23func wlog(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 24 25func main() -> i64 { 26 g_puts("nx_vfs_blockfs (VFS MOUNTED ON blockfs: the hierarchical namespace persisted in the block image, NSFS v2)\n" as *u8) 27 var pass: i64=0; var total: i64=0 28 let a: *u8 = sys_mmap(VB_NB*VB_BS) 29 let out: *u8 = sys_mmap(VB_BS*32) 30 31 // T1: format + mount 32 vb_format(a) 33 let m0: i64=vb_mount(a) 34 var t1: i64=0 35 if m0==0 { if vb_resolve(a,"/" as *u8)==0 { if vb_lscount(a,0)==0 { t1=1 } } } 36 g_puts(" T1 format -> mount rc="); g_pn(m0); g_puts(" root inode=0 children="); g_pn(vb_lscount(a,0)); g_puts("\n" as *u8) 37 pass=pass+ck("T1: format + MOUNT (magic+version+root validated), empty root" as *u8, t1); total=total+1 38 39 // T2: build a nested tree with the direct ops 40 let docs: i64=vb_mkdir(a,0,"docs" as *u8) 41 let src: i64=vb_mkdir(a,0,"src" as *u8) 42 let rd: i64=vb_create(a,docs,"readme" as *u8,"hello vfs-on-blockfs" as *u8,20) 43 let mn: i64=vb_create(a,src,"main" as *u8,"int main" as *u8,8) 44 let sub: i64=vb_mkdir(a,docs,"sub" as *u8) 45 let dp: i64=vb_create(a,sub,"deep" as *u8,"nested-depth-3" as *u8,14) 46 let r1: i64=vb_resolve(a,"/docs/readme" as *u8) 47 let r2: i64=vb_resolve(a,"/docs/sub/deep" as *u8) 48 let n1: i64=vb_read(a,r1,out) 49 var t2: i64=0 50 if r1==rd { if r2==dp { if n1==20 { if streq_n(out,"hello vfs-on-blockfs" as *u8,20)==1 { 51 if vb_lscount(a,0)==2 { if vb_lscount(a,docs)==2 { if vb_lscount(a,sub)==1 { t2=1 } } } } } } } 52 g_puts(" T2 tree: /docs("); g_pn(docs); g_puts(") /src("); g_pn(src); g_puts(") /docs/readme("); g_pn(rd) 53 g_puts(") /docs/sub/deep("); g_pn(dp); g_puts("); resolve deep="); g_pn(r2); g_puts(" ls root="); g_pn(vb_lscount(a,0)); g_puts("\n" as *u8) 54 pass=pass+ck("T2: nested tree on the block image -- mkdir/create/resolve/read/ls all correct" as *u8, t2); total=total+1 55 56 // T3: THE MOUNT PROOF -- persist, wipe RAM, reload, mount, everything intact 57 vb_save(a,"knowledge/vfsblock_disk.img" as *u8) 58 var z: i64=0; while z<VB_NB*VB_BS { a[z]=0 as u8; z=z+1 } 59 let ld: i64=vb_load(a,"knowledge/vfsblock_disk.img" as *u8) 60 let m1: i64=vb_mount(a) 61 let q1: i64=vb_resolve(a,"/docs/readme" as *u8) 62 let q2: i64=vb_resolve(a,"/docs/sub/deep" as *u8) 63 let qn1: i64=vb_read(a,q1,out) 64 let ok1: i64=streq_n(out,"hello vfs-on-blockfs" as *u8,20) 65 let qn2: i64=vb_read(a,q2,out) 66 let ok2: i64=streq_n(out,"nested-depth-3" as *u8,14) 67 var t3: i64=0 68 if ld==VB_NB*VB_BS { if m1==0 { if qn1==20 { if ok1==1 { if qn2==14 { if ok2==1 { if vb_lscount(a,0)==2 { t3=1 } } } } } } } 69 g_puts(" T3 saved "); g_pn(ld); g_puts("B -> wiped RAM -> loaded -> mount rc="); g_pn(m1); g_puts(" -> '/docs/sub/deep'="); g_pn(qn2); g_puts("B byte-exact\n" as *u8) 70 pass=pass+ck("T3: MOUNT the persisted image -- namespace + nested paths + content survive the round-trip byte-exact" as *u8, t3); total=total+1 71 72 // T4 teeth: corrupt one data byte in the image -> checksum detects 73 let vi: i64=vb_resolve(a,"/src/main" as *u8) 74 let vok: i64=vb_verify(a,vi) 75 let bo: i64=vb_ino(vi); let st: i64=vb_rd(a,bo+32) 76 a[st*VB_BS+2] = (a[st*VB_BS+2] ^ (0xFF as u8)) 77 let vbad: i64=vb_verify(a,vi) 78 a[st*VB_BS+2] = (a[st*VB_BS+2] ^ (0xFF as u8)) 79 let vok2: i64=vb_verify(a,vi) 80 var t4: i64=0; if vok==1 { if vbad==0 { if vok2==1 { t4=1 } } } 81 g_puts(" T4 verify /src/main clean="); g_pn(vok); g_puts(" corrupted="); g_pn(vbad); g_puts(" restored="); g_pn(vok2); g_puts("\n" as *u8) 82 pass=pass+ck("T4 (teeth): a corrupted data byte is DETECTED by the per-file checksum; restore verifies clean" as *u8, t4); total=total+1 83 84 // T5 teeth: corrupt the superblock magic -> mount REFUSED 85 a[0] = (a[0] ^ (0xFF as u8)) 86 let mref: i64=vb_mount(a) 87 a[0] = (a[0] ^ (0xFF as u8)) 88 let mok: i64=vb_mount(a) 89 var t5: i64=0; if mref==(0-1) { if mok==0 { t5=1 } } 90 g_puts(" T5 mount corrupted-magic rc="); g_pn(mref); g_puts(" (-1=REFUSED) restored rc="); g_pn(mok); g_puts("\n" as *u8) 91 pass=pass+ck("T5 (teeth): a bad superblock is REFUSED at mount; the restored image mounts" as *u8, t5); total=total+1 92 93 var okall: i64=0; if pass==total { okall=1 } 94 g_puts("---- nx_vfs_blockfs: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8) 95 if okall==1 { 96 let logf: i64=sys_openat_append("knowledge/status/vfs_blockfs.log" as *u8, 420) 97 if logf>=0 { wlog(logf,"NXVFSBLOCKFS GREEN: hierarchical VFS mounted ON the block image (NSFS v2) -- nested tree persists + mounts byte-exact; checksum + superblock teeth\n" as *u8); sys_close(logf) } 98 g_puts("verdict=GREEN (VFS-on-blockfs MOUNT: the namespace lives in the block image and mounts back -- the FILESYSTEM mount gap half closed)\n" as *u8); sys_exit(0); return 0 99 } 100 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 101}