Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions host/src/vfs/memory-fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -960,12 +960,7 @@ export class MemoryFileSystem implements FileSystemBackend {
length: number,
): number {
if (offset !== null) {
// pread semantics: read at offset without changing file position
const savedPos = this.fs.lseek(handle, 0, 1); // SEEK_CUR
this.fs.lseek(handle, offset, 0); // SEEK_SET
const n = this.fs.read(handle, buffer.subarray(0, length));
this.fs.lseek(handle, savedPos, 0); // restore position
return n;
return this.fs.readAt(handle, buffer.subarray(0, length), offset);
}
return this.fs.read(handle, buffer.subarray(0, length));
}
Expand All @@ -977,12 +972,7 @@ export class MemoryFileSystem implements FileSystemBackend {
length: number,
): number {
if (offset !== null) {
// pwrite semantics: write at offset without changing file position
const savedPos = this.fs.lseek(handle, 0, 1); // SEEK_CUR
this.fs.lseek(handle, offset, 0); // SEEK_SET
const n = this.fs.write(handle, buffer.subarray(0, length));
this.fs.lseek(handle, savedPos, 0); // restore position
return n;
return this.fs.writeAt(handle, buffer.subarray(0, length), offset);
}
return this.fs.write(handle, buffer.subarray(0, length));
}
Expand Down
115 changes: 103 additions & 12 deletions host/src/vfs/sharedfs-vendor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,24 @@ export class SharedFS {
}
}

private fdReferencesInode(ino: number): boolean {
for (let fd = 0; fd < MAX_FDS; fd++) {
const base = FD_TABLE_OFFSET + fd * FD_ENTRY_SIZE;
if (Atomics.load(this.i32, base >> 2) === 0) continue;
if (this.r32(base + FD_INO) === ino) return true;
}
return false;
}

private prepareFreeUnlinkedInode(ino: number): boolean {
const off = this.inodeOffset(ino);
if (this.r32(off + INO_LINK_COUNT) !== 0) return false;
if (this.fdReferencesInode(ino)) return false;

this.inodeTruncate(ino, 0);
return true;
}

// ── Build stat result from inode ─────────────────────────────────

private buildStat(ino: number): StatResult {
Expand Down Expand Up @@ -1292,6 +1310,15 @@ export class SharedFS {
const entry = this.fdGet(fd);
if (!entry) throw new SFSError(EBADF);
this.fdFree(fd);

let shouldFree = false;
this.inodeWriteLock(entry.ino);
try {
shouldFree = this.prepareFreeUnlinkedInode(entry.ino);
} finally {
this.inodeWriteUnlock(entry.ino);
}
if (shouldFree) this.inodeFree(entry.ino);
}

read(fd: number, buffer: Uint8Array): number {
Expand All @@ -1315,6 +1342,19 @@ export class SharedFS {
}
}

readAt(fd: number, buffer: Uint8Array, offset: number): number {
const entry = this.fdGet(fd);
if (!entry) throw new SFSError(EBADF);
if (offset < 0) throw new SFSError(EINVAL);

this.inodeReadLock(entry.ino);
try {
return this.inodeReadData(entry.ino, offset, buffer, buffer.length);
} finally {
this.inodeReadUnlock(entry.ino);
}
}

write(fd: number, data: Uint8Array): number {
const entry = this.fdGet(fd);
if (!entry) throw new SFSError(EBADF);
Expand Down Expand Up @@ -1345,6 +1385,27 @@ export class SharedFS {
}
}

writeAt(fd: number, data: Uint8Array, offset: number): number {
const entry = this.fdGet(fd);
if (!entry) throw new SFSError(EBADF);
if (offset < 0) throw new SFSError(EINVAL);

const accMode = entry.flags & O_ACCMODE;
if (accMode === O_RDONLY) throw new SFSError(EBADF);

this.inodeWriteLock(entry.ino);
try {
let writeOffset = offset;
if (entry.flags & O_APPEND) {
const inoOff = this.inodeOffset(entry.ino);
writeOffset = this.r64(inoOff + INO_SIZE);
}
return this.inodeWriteData(entry.ino, writeOffset, data, data.length);
} finally {
this.inodeWriteUnlock(entry.ino);
}
}

lseek(fd: number, offset: number, whence: number): number {
const entry = this.fdGet(fd);
if (!entry) throw new SFSError(EBADF);
Expand Down Expand Up @@ -1433,17 +1494,20 @@ export class SharedFS {
const rc = this.dirRemoveEntry(parentIno, nameBytes);
if (rc < 0) throw new SFSError(rc);

let shouldFree = false;
this.inodeWriteLock(childIno);
const linkCount = this.r32(childOff + INO_LINK_COUNT);
if (linkCount <= 1) {
this.inodeTruncate(childIno, 0);
this.w32(childOff + INO_LINK_COUNT, 0);
this.inodeWriteUnlock(childIno);
this.inodeFree(childIno);
} else {
this.w32(childOff + INO_LINK_COUNT, linkCount - 1);
try {
const linkCount = this.r32(childOff + INO_LINK_COUNT);
if (linkCount <= 1) {
this.w32(childOff + INO_LINK_COUNT, 0);
shouldFree = this.prepareFreeUnlinkedInode(childIno);
} else {
this.w32(childOff + INO_LINK_COUNT, linkCount - 1);
}
} finally {
this.inodeWriteUnlock(childIno);
}
if (shouldFree) this.inodeFree(childIno);
} finally {
this.inodeWriteUnlock(parentIno);
}
Expand All @@ -1466,19 +1530,46 @@ export class SharedFS {
try {
const srcIno = this.dirLookup(oldParent, oldNameBytes);
if (srcIno < 0) throw new SFSError(srcIno);
if (oldParent === newParent && oldName === newName) return;

// Remove any existing entry at destination
const existingIno = this.dirLookup(newParent, newNameBytes);
if (existingIno >= 0) {
if (existingIno === srcIno) {
const rc = this.dirRemoveEntry(oldParent, oldNameBytes);
if (rc < 0) throw new SFSError(rc);

const srcOff = this.inodeOffset(srcIno);
this.inodeWriteLock(srcIno);
try {
const linkCount = this.r32(srcOff + INO_LINK_COUNT);
if (linkCount > 1) {
this.w32(srcOff + INO_LINK_COUNT, linkCount - 1);
}
} finally {
this.inodeWriteUnlock(srcIno);
}
return;
}

const existOff = this.inodeOffset(existingIno);
const existMode = this.r32(existOff + INO_MODE);
if ((existMode & S_IFMT) === S_IFDIR) throw new SFSError(EISDIR);
this.dirRemoveEntry(newParent, newNameBytes);
let shouldFree = false;
this.inodeWriteLock(existingIno);
this.inodeTruncate(existingIno, 0);
this.w32(existOff + INO_LINK_COUNT, 0);
this.inodeWriteUnlock(existingIno);
this.inodeFree(existingIno);
try {
const linkCount = this.r32(existOff + INO_LINK_COUNT);
if (linkCount <= 1) {
this.w32(existOff + INO_LINK_COUNT, 0);
shouldFree = this.prepareFreeUnlinkedInode(existingIno);
} else {
this.w32(existOff + INO_LINK_COUNT, linkCount - 1);
}
} finally {
this.inodeWriteUnlock(existingIno);
}
if (shouldFree) this.inodeFree(existingIno);
}

// Add entry in new directory
Expand Down
110 changes: 110 additions & 0 deletions host/test/vfs/sharedfs-positioned-io.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { describe, expect, it } from "vitest";
import { MemoryFileSystem } from "../../src/vfs/memory-fs";
import {
O_CREAT,
O_RDWR,
O_TRUNC,
SEEK_SET,
SharedFS,
} from "../../src/vfs/sharedfs-vendor";

const encoder = new TextEncoder();
const decoder = new TextDecoder();

function text(bytes: Uint8Array): string {
return decoder.decode(bytes);
}

describe("SharedFS positioned I/O", () => {
it("readAt and writeAt do not mutate the shared fd offset", () => {
const sab = new SharedArrayBuffer(1024 * 1024);
const fs = SharedFS.mkfs(sab);
const fd = fs.open("/sorter.tmp", O_RDWR | O_CREAT | O_TRUNC, 0o600);

expect(fs.write(fd, encoder.encode("0123456789abcdef"))).toBe(16);
expect(fs.lseek(fd, 10, SEEK_SET)).toBe(10);

const positionedRead = new Uint8Array(4);
expect(fs.readAt(fd, positionedRead, 2)).toBe(4);
expect(text(positionedRead)).toBe("2345");

expect(fs.writeAt(fd, encoder.encode("XY"), 4)).toBe(2);

const sequentialRead = new Uint8Array(3);
expect(fs.read(fd, sequentialRead)).toBe(3);
expect(text(sequentialRead)).toBe("abc");

expect(fs.lseek(fd, 0, SEEK_SET)).toBe(0);
const full = new Uint8Array(16);
expect(fs.read(fd, full)).toBe(16);
expect(text(full)).toBe("0123XY6789abcdef");
});

it("keeps unlinked open files alive until the last fd closes", () => {
const sab = new SharedArrayBuffer(1024 * 1024);
const fs = SharedFS.mkfs(sab);
const fd = fs.open("/etilqs_a", O_RDWR | O_CREAT | O_TRUNC, 0o600);
const page = new Uint8Array(4096);
page.fill("A".charCodeAt(0));

expect(fs.writeAt(fd, page, 0)).toBe(4096);
fs.unlink("/etilqs_a");

const fd2 = fs.open("/etilqs_b", O_RDWR | O_CREAT | O_TRUNC, 0o600);
expect(fs.writeAt(fd2, encoder.encode("replacement"), 0)).toBe(11);

const positionedRead = new Uint8Array(16);
expect(fs.readAt(fd, positionedRead, 1024)).toBe(16);
expect(text(positionedRead)).toBe("AAAAAAAAAAAAAAAA");

fs.close(fd);
expect(fs.readAt(fd2, positionedRead, 0)).toBe(11);
expect(text(positionedRead.subarray(0, 11))).toBe("replacement");
});

it("keeps renamed-over open files alive until the last fd closes", () => {
const sab = new SharedArrayBuffer(1024 * 1024);
const fs = SharedFS.mkfs(sab);
const replaced = fs.open("/target", O_RDWR | O_CREAT | O_TRUNC, 0o600);
const replacement = fs.open("/replacement", O_RDWR | O_CREAT | O_TRUNC, 0o600);
const page = new Uint8Array(4096);
page.fill("A".charCodeAt(0));

expect(fs.writeAt(replaced, page, 0)).toBe(4096);
expect(fs.writeAt(replacement, encoder.encode("new contents"), 0)).toBe(12);
fs.rename("/replacement", "/target");

const positionedRead = new Uint8Array(16);
expect(fs.readAt(replaced, positionedRead, 1024)).toBe(16);
expect(text(positionedRead)).toBe("AAAAAAAAAAAAAAAA");

const reopened = fs.open("/target", O_RDWR, 0);
const replacementRead = new Uint8Array(12);
expect(fs.readAt(reopened, replacementRead, 0)).toBe(12);
expect(text(replacementRead)).toBe("new contents");
});

it("MemoryFileSystem pread and pwrite use positioned SharedFS I/O", () => {
const sab = new SharedArrayBuffer(1024 * 1024);
const fs = MemoryFileSystem.create(sab);
const fd = fs.open("/sorter.tmp", O_RDWR | O_CREAT | O_TRUNC, 0o600);

expect(fs.write(fd, encoder.encode("0123456789abcdef"), null, 16)).toBe(16);
expect(fs.seek(fd, 10, SEEK_SET)).toBe(10);

const positionedRead = new Uint8Array(4);
expect(fs.read(fd, positionedRead, 2, 4)).toBe(4);
expect(text(positionedRead)).toBe("2345");

expect(fs.write(fd, encoder.encode("XY"), 4, 2)).toBe(2);

const sequentialRead = new Uint8Array(3);
expect(fs.read(fd, sequentialRead, null, 3)).toBe(3);
expect(text(sequentialRead)).toBe("abc");

expect(fs.seek(fd, 0, SEEK_SET)).toBe(0);
const full = new Uint8Array(16);
expect(fs.read(fd, full, null, 16)).toBe(16);
expect(text(full)).toBe("0123XY6789abcdef");
});
});
Loading