@waffle-os/runtime 0.0.155 → 0.0.157
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser/filesystem-backend.ts +11 -1
- package/browser/preview-protocol.ts +2 -0
- package/browser/preview.ts +128 -18
- package/browser/runtime-terminals.ts +65 -24
- package/browser/shared-fs.ts +141 -14
- package/browser/worker.ts +73 -6
- package/core/descriptors.ts +19 -2
- package/core/devices.ts +237 -2
- package/core/fd-reopen.ts +105 -20
- package/core/filesystem.ts +79 -0
- package/core/index.ts +4 -0
- package/core/open-file.ts +212 -18
- package/core/wasi.ts +449 -47
- package/dist/browser/filesystem-backend.js +4 -2
- package/dist/browser/preview.js +120 -17
- package/dist/browser/runtime-terminals.js +53 -10
- package/dist/browser/shared-fs.js +124 -11
- package/dist/browser/worker.js +59 -6
- package/dist/core/descriptors.js +9 -1
- package/dist/core/devices.js +211 -1
- package/dist/core/fd-reopen.js +79 -18
- package/dist/core/filesystem.js +14 -0
- package/dist/core/open-file.js +166 -13
- package/dist/core/wasi.js +402 -29
- package/dist/fs/backend.js +51 -3
- package/dist/fs/client.js +204 -7
- package/dist/fs/cold-read-cache.js +23 -0
- package/dist/fs/index.js +21 -1
- package/dist/fs/indexeddb.js +30 -2
- package/dist/fs/locks.js +1112 -0
- package/dist/fs/memory.js +126 -2
- package/dist/fs/multitab-backend.js +317 -196
- package/dist/fs/multitab-protocol.js +2 -0
- package/dist/fs/persistent.js +32 -7
- package/dist/fs/protocol.js +9 -3
- package/dist/fs/service.js +387 -22
- package/dist/fs/shared.js +54 -1
- package/dist/fs/storage-geometry.js +2 -0
- package/dist/interrupt/rewriter-internal.js +4 -1
- package/dist/proc/abi.js +80 -6
- package/dist/proc/errors.js +1 -0
- package/dist/proc/filesystem-codec.js +40 -9
- package/dist/proc/index.js +10 -1
- package/dist/proc/pty.js +193 -0
- package/dist/proc/worker-client.js +11 -2
- package/dist/proc/worker-pipes.js +51 -0
- package/dist/runtime.js +11 -2
- package/dist/search/engine.js +53 -0
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/backend.ts +54 -2
- package/fs/client.ts +230 -6
- package/fs/cold-read-cache.ts +38 -0
- package/fs/index.ts +22 -1
- package/fs/indexeddb.ts +36 -2
- package/fs/locks.ts +1406 -0
- package/fs/memory.ts +132 -1
- package/fs/multitab-backend.ts +563 -304
- package/fs/multitab-protocol.ts +2 -0
- package/fs/persistent.ts +46 -4
- package/fs/protocol.ts +17 -5
- package/fs/service.ts +469 -23
- package/fs/shared.ts +60 -2
- package/fs/storage-geometry.ts +3 -0
- package/index.ts +2 -0
- package/interrupt/rewriter-internal.ts +4 -1
- package/package.json +2 -2
- package/proc/abi.ts +79 -6
- package/proc/errors.ts +1 -0
- package/proc/filesystem-codec.ts +39 -9
- package/proc/index.ts +11 -0
- package/proc/pty.ts +227 -0
- package/proc/worker-client.ts +28 -6
- package/proc/worker-pipes.ts +57 -0
- package/runtime.ts +12 -2
- package/search/engine.ts +49 -0
- package/testing/indexeddb-storage-browser.ts +173 -2
- package/tooling/program/program-transformer.json +1 -1
package/fs/service.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { WalkTable, type WalkTransaction } from "./walk.js";
|
|
2
|
+
import { LockContentionError, LockTable, type OfdLiveness } from "./locks.js";
|
|
3
|
+
import { readSharedOfdBuffer } from "../core/open-file.js";
|
|
2
4
|
import type { OperationBinding } from "../browser/operations/protocol.js";
|
|
3
5
|
import {
|
|
4
6
|
requestMemorySections,
|
|
@@ -41,7 +43,8 @@ import {
|
|
|
41
43
|
} from "./publication.js";
|
|
42
44
|
import { reportRuntimeStartup, type RuntimeStartupObserver } from "./startup.js";
|
|
43
45
|
import { MemoryBackend, type FilesystemBackend } from "./backend.js";
|
|
44
|
-
import type
|
|
46
|
+
import { POSIX_LOCK_COUNT, type SharedFilesystemState, type StagedWrite } from "./shared.js";
|
|
47
|
+
import { HEADER_WORDS } from "./storage-geometry.js";
|
|
45
48
|
import {
|
|
46
49
|
CAPABILITY_HEADER,
|
|
47
50
|
CAPABILITY_LENGTH,
|
|
@@ -126,9 +129,18 @@ interface WatchResponseSettlement {
|
|
|
126
129
|
readonly cancel: () => void;
|
|
127
130
|
readonly release: (() => void) | undefined;
|
|
128
131
|
}
|
|
132
|
+
interface LockSettlement {
|
|
133
|
+
readonly rollback: () => void;
|
|
134
|
+
readonly settle?: () => void;
|
|
135
|
+
}
|
|
129
136
|
interface Session {
|
|
130
137
|
watchTransactions: Map<number, WatchTransaction>;
|
|
131
138
|
watchResponses: Map<number, WatchResponseSettlement>;
|
|
139
|
+
lockTransactions: Map<number, LockSettlement>;
|
|
140
|
+
lockResponses: Map<number, LockSettlement>;
|
|
141
|
+
lockWaiters: Map<number, () => void>;
|
|
142
|
+
pendingOfdAdoptions: Set<Promise<void>>;
|
|
143
|
+
locksReleased?: boolean;
|
|
132
144
|
walks?: WalkTable;
|
|
133
145
|
walkTransactions: Map<number, WalkTransaction>;
|
|
134
146
|
walkResponses: Map<number, () => void>;
|
|
@@ -212,6 +224,11 @@ export class FilesystemService {
|
|
|
212
224
|
}
|
|
213
225
|
|
|
214
226
|
private sessions = new Set<Session>();
|
|
227
|
+
private sessionsByOwner = new Map<number, Session>();
|
|
228
|
+
private sessionsByPosixOwner = new Map<number, Set<Session>>();
|
|
229
|
+
private pendingPosixSuccessors = new Map<number, Set<number>>();
|
|
230
|
+
private sharedCells?: Int32Array;
|
|
231
|
+
private ownerStates?: Int32Array;
|
|
215
232
|
private requestBudget?: SharedArrayBuffer;
|
|
216
233
|
private readBudget?: WorkspaceReadBudget;
|
|
217
234
|
private queue = Promise.resolve();
|
|
@@ -225,6 +242,7 @@ export class FilesystemService {
|
|
|
225
242
|
private refusedConnections = new Set<FilesystemConnection>();
|
|
226
243
|
residentPublication: ResidentGrantAuthority | undefined;
|
|
227
244
|
private watchService?: WatchService;
|
|
245
|
+
private lockTable?: LockTable;
|
|
228
246
|
private disposingWatches = false;
|
|
229
247
|
constructor(private backend: FilesystemBackend) {}
|
|
230
248
|
attachSocketScope(
|
|
@@ -292,6 +310,10 @@ export class FilesystemService {
|
|
|
292
310
|
walkResponses: new Map(),
|
|
293
311
|
watchTransactions: new Map(),
|
|
294
312
|
watchResponses: new Map(),
|
|
313
|
+
lockTransactions: new Map(),
|
|
314
|
+
lockResponses: new Map(),
|
|
315
|
+
lockWaiters: new Map(),
|
|
316
|
+
pendingOfdAdoptions: new Set(),
|
|
295
317
|
requests: captureRequestMemory(connection.requests),
|
|
296
318
|
handles: new Map(),
|
|
297
319
|
capabilities: new Int32Array(connection.capabilities, 0, CAPABILITY_LENGTH),
|
|
@@ -315,9 +337,26 @@ export class FilesystemService {
|
|
|
315
337
|
...(connection.publication?.observer ? { publicationPins: new Set<number>() } : {}),
|
|
316
338
|
};
|
|
317
339
|
this.sessions.add(session);
|
|
340
|
+
this.sessionsByOwner.set(connection.owner, session);
|
|
341
|
+
this.sharedCells ??= new Int32Array(connection.shared, 0, HEADER_WORDS);
|
|
342
|
+
this.ownerStates ??= new Int32Array(connection.ownerStates);
|
|
343
|
+
const posixOwner = this.sessionPosixOwner(session);
|
|
344
|
+
let group = this.sessionsByPosixOwner.get(posixOwner);
|
|
345
|
+
if (!group) {
|
|
346
|
+
group = new Set();
|
|
347
|
+
this.sessionsByPosixOwner.set(posixOwner, group);
|
|
348
|
+
}
|
|
349
|
+
group.add(session);
|
|
350
|
+
const pendingSuccessors = this.pendingPosixSuccessors.get(posixOwner);
|
|
351
|
+
if (pendingSuccessors) {
|
|
352
|
+
pendingSuccessors.delete(connection.owner);
|
|
353
|
+
if (pendingSuccessors.size === 0) this.pendingPosixSuccessors.delete(posixOwner);
|
|
354
|
+
}
|
|
355
|
+
this.clearMismatchedPosixSuccessor(connection.owner, posixOwner);
|
|
318
356
|
for (const reservation of connection.reservations) {
|
|
319
357
|
const state = new Int32Array(reservation.state);
|
|
320
358
|
if (Atomics.compareExchange(state, 1, 0, 1) !== 0) continue;
|
|
359
|
+
let releaseOfdTransfer = reservation.ofd;
|
|
321
360
|
try {
|
|
322
361
|
if (!isRevoked(connection) && !session.handles.has(reservation.handle)) {
|
|
323
362
|
this.backend.retain(reservation.handle);
|
|
@@ -330,9 +369,22 @@ export class FilesystemService {
|
|
|
330
369
|
session.transient.set(reservation.handle, true);
|
|
331
370
|
const index = session.freeEntries.indexOf(reservation.targetKey);
|
|
332
371
|
if (index >= 0) session.freeEntries.splice(index, 1);
|
|
333
|
-
} else if (isRevoked(connection))
|
|
372
|
+
} else if (isRevoked(connection)) {
|
|
334
373
|
Atomics.store(session.capabilities, reservation.targetKey + 1, -1);
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
if (releaseOfdTransfer) {
|
|
377
|
+
this.adoptReservationOfd(
|
|
378
|
+
session,
|
|
379
|
+
reservation.handle,
|
|
380
|
+
releaseOfdTransfer,
|
|
381
|
+
reservation.state,
|
|
382
|
+
);
|
|
383
|
+
releaseOfdTransfer = undefined;
|
|
384
|
+
}
|
|
335
385
|
} finally {
|
|
386
|
+
if (releaseOfdTransfer && releaseOfdTransfer.byteLength >= 32)
|
|
387
|
+
Atomics.sub(new Int32Array(releaseOfdTransfer), 6, 1);
|
|
336
388
|
Atomics.sub(
|
|
337
389
|
new Int32Array(reservation.source, 0, CAPABILITY_LENGTH),
|
|
338
390
|
reservation.sourceKey + 2,
|
|
@@ -435,6 +487,7 @@ export class FilesystemService {
|
|
|
435
487
|
}
|
|
436
488
|
if ("discard" in data) {
|
|
437
489
|
session.reader?.cancel(data.discard);
|
|
490
|
+
this.cancelLockWaiter(session, data.discard);
|
|
438
491
|
this.enqueueMaintenance(session, () => this.discardResponse(session, data.discard));
|
|
439
492
|
return;
|
|
440
493
|
}
|
|
@@ -516,14 +569,22 @@ export class FilesystemService {
|
|
|
516
569
|
const state = new Int32Array(reservation.state);
|
|
517
570
|
if (Atomics.load(state, 0) || Atomics.compareExchange(state, 1, 0, 1))
|
|
518
571
|
throw new FilesystemError(11);
|
|
572
|
+
let releaseOfdTransfer = reservation.ofd;
|
|
519
573
|
try {
|
|
520
|
-
if (target.handles.has(reservation.handle))
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
574
|
+
if (!target.handles.has(reservation.handle)) {
|
|
575
|
+
this.backend.retain(reservation.handle);
|
|
576
|
+
target.handles.set(reservation.handle, reservation.targetKey);
|
|
577
|
+
target.transient.set(reservation.handle, true);
|
|
578
|
+
const free = target.freeEntries.indexOf(reservation.targetKey);
|
|
579
|
+
if (free >= 0) target.freeEntries.splice(free, 1);
|
|
580
|
+
}
|
|
581
|
+
if (releaseOfdTransfer) {
|
|
582
|
+
this.adoptReservationOfd(target, reservation.handle, releaseOfdTransfer, reservation.state);
|
|
583
|
+
releaseOfdTransfer = undefined;
|
|
584
|
+
}
|
|
526
585
|
} finally {
|
|
586
|
+
if (releaseOfdTransfer && releaseOfdTransfer.byteLength >= 32)
|
|
587
|
+
Atomics.sub(new Int32Array(releaseOfdTransfer), 6, 1);
|
|
527
588
|
Atomics.sub(source.capabilities, reservation.sourceKey + 2, 1);
|
|
528
589
|
}
|
|
529
590
|
}
|
|
@@ -716,6 +777,12 @@ export class FilesystemService {
|
|
|
716
777
|
const cancelWalk = session.walkResponses.get(id);
|
|
717
778
|
session.walkResponses.delete(id);
|
|
718
779
|
if (cancelledWalk) cancelWalk?.();
|
|
780
|
+
const lockSettlement = session.lockResponses.get(id);
|
|
781
|
+
session.lockResponses.delete(id);
|
|
782
|
+
if (lockSettlement) {
|
|
783
|
+
if (cancelledWalk) lockSettlement.rollback();
|
|
784
|
+
else lockSettlement.settle?.();
|
|
785
|
+
}
|
|
719
786
|
if (retained) releaseRequestBuffer(retained, REQUEST_SERVICE);
|
|
720
787
|
session.retainedRequests.delete(id);
|
|
721
788
|
const response = session.responses.get(id);
|
|
@@ -967,6 +1034,12 @@ export class FilesystemService {
|
|
|
967
1034
|
"readdirPage",
|
|
968
1035
|
"resolve",
|
|
969
1036
|
"realpath",
|
|
1037
|
+
"probe",
|
|
1038
|
+
"lockFlock",
|
|
1039
|
+
"lockRange",
|
|
1040
|
+
"lockGetRange",
|
|
1041
|
+
"lockReleaseOfd",
|
|
1042
|
+
"lockReleasePosix",
|
|
970
1043
|
].includes(operation)
|
|
971
1044
|
)
|
|
972
1045
|
this.check(session, a, held);
|
|
@@ -1147,6 +1220,345 @@ export class FilesystemService {
|
|
|
1147
1220
|
? execute()
|
|
1148
1221
|
: backend.shared.mutate(undefined, () => sessionRevoked(session), execute);
|
|
1149
1222
|
}
|
|
1223
|
+
private cancelLockWaiter(session: Session, id: number): void {
|
|
1224
|
+
const cancel = session.lockWaiters.get(id);
|
|
1225
|
+
if (!cancel) return;
|
|
1226
|
+
session.lockWaiters.delete(id);
|
|
1227
|
+
cancel();
|
|
1228
|
+
}
|
|
1229
|
+
private resolveLockIno(handle: number): number | Promise<number> {
|
|
1230
|
+
if (this.backend instanceof MemoryBackend) return this.backend.fs.stat(handle).ino;
|
|
1231
|
+
const result = this.backend.execute("stat", [handle], new Uint8Array());
|
|
1232
|
+
if (result instanceof Promise)
|
|
1233
|
+
return result.then((stat) => (stat as { readonly ino: number }).ino);
|
|
1234
|
+
return (result as { readonly ino: number }).ino;
|
|
1235
|
+
}
|
|
1236
|
+
private ensureLockTable(): LockTable {
|
|
1237
|
+
if (this.lockTable) return this.lockTable;
|
|
1238
|
+
this.lockTable = new LockTable({
|
|
1239
|
+
onPosixLockCountChange: (count) => {
|
|
1240
|
+
if (this.sharedCells) Atomics.store(this.sharedCells, POSIX_LOCK_COUNT, count);
|
|
1241
|
+
},
|
|
1242
|
+
});
|
|
1243
|
+
return this.lockTable;
|
|
1244
|
+
}
|
|
1245
|
+
private sessionPosixOwner(session: Session): number {
|
|
1246
|
+
const candidate = session.connection.posixOwner;
|
|
1247
|
+
if (
|
|
1248
|
+
candidate !== undefined &&
|
|
1249
|
+
Number.isInteger(candidate) &&
|
|
1250
|
+
candidate > 0 &&
|
|
1251
|
+
(!this.ownerStates || candidate < this.ownerStates.length)
|
|
1252
|
+
) {
|
|
1253
|
+
return candidate;
|
|
1254
|
+
}
|
|
1255
|
+
return session.connection.owner;
|
|
1256
|
+
}
|
|
1257
|
+
private hasLivePosixSession(posixOwner: number): boolean {
|
|
1258
|
+
const group = this.sessionsByPosixOwner.get(posixOwner);
|
|
1259
|
+
if (!group) return false;
|
|
1260
|
+
for (const member of group) {
|
|
1261
|
+
if (!member.closed && !member.locksReleased && !sessionRevoked(member)) return true;
|
|
1262
|
+
}
|
|
1263
|
+
return false;
|
|
1264
|
+
}
|
|
1265
|
+
private hasLivePendingPosixSuccessor(posixOwner: number): boolean {
|
|
1266
|
+
const pending = this.pendingPosixSuccessors.get(posixOwner);
|
|
1267
|
+
if (!pending) return false;
|
|
1268
|
+
for (const owner of Array.from(pending)) {
|
|
1269
|
+
if (this.isSessionLive(owner)) continue;
|
|
1270
|
+
if (this.ownerStates && Atomics.load(this.ownerStates, owner) === 0) continue;
|
|
1271
|
+
pending.delete(owner);
|
|
1272
|
+
}
|
|
1273
|
+
if (pending.size > 0) return true;
|
|
1274
|
+
this.pendingPosixSuccessors.delete(posixOwner);
|
|
1275
|
+
return false;
|
|
1276
|
+
}
|
|
1277
|
+
private isPosixOwnerAlive(posixOwner: number): boolean {
|
|
1278
|
+
return this.hasLivePosixSession(posixOwner) || this.hasLivePendingPosixSuccessor(posixOwner);
|
|
1279
|
+
}
|
|
1280
|
+
private clearMismatchedPosixSuccessor(connectedOwner: number, actualPosixOwner: number): void {
|
|
1281
|
+
if (this.pendingPosixSuccessors.size === 0) return;
|
|
1282
|
+
let removed = false;
|
|
1283
|
+
for (const [posixOwner, pending] of Array.from(this.pendingPosixSuccessors.entries())) {
|
|
1284
|
+
if (posixOwner === actualPosixOwner) continue;
|
|
1285
|
+
if (!pending.delete(connectedOwner)) continue;
|
|
1286
|
+
removed = true;
|
|
1287
|
+
if (pending.size === 0) this.pendingPosixSuccessors.delete(posixOwner);
|
|
1288
|
+
}
|
|
1289
|
+
if (removed) this.reapPendingPosixSuccessors();
|
|
1290
|
+
}
|
|
1291
|
+
private reapPendingPosixSuccessors(): void {
|
|
1292
|
+
if (this.pendingPosixSuccessors.size === 0) return;
|
|
1293
|
+
for (const posixOwner of Array.from(this.pendingPosixSuccessors.keys())) {
|
|
1294
|
+
if (this.isPosixOwnerAlive(posixOwner)) continue;
|
|
1295
|
+
this.lockTable?.releasePosixOwner(posixOwner);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
private adoptReservationOfd(
|
|
1299
|
+
session: Session,
|
|
1300
|
+
handle: number,
|
|
1301
|
+
ofdBuffer: SharedArrayBuffer,
|
|
1302
|
+
reservationState: SharedArrayBuffer,
|
|
1303
|
+
): void {
|
|
1304
|
+
const ofd = readSharedOfdBuffer(ofdBuffer);
|
|
1305
|
+
if (!ofd) {
|
|
1306
|
+
if (ofdBuffer.byteLength >= 32) Atomics.sub(new Int32Array(ofdBuffer), 6, 1);
|
|
1307
|
+
return;
|
|
1308
|
+
}
|
|
1309
|
+
const finish = (ino: number) => {
|
|
1310
|
+
try {
|
|
1311
|
+
this.ensureLockTable().adoptReservation(
|
|
1312
|
+
ino,
|
|
1313
|
+
handle,
|
|
1314
|
+
ofd.id,
|
|
1315
|
+
ofd.originOwner,
|
|
1316
|
+
ofdBuffer,
|
|
1317
|
+
session.connection.owner,
|
|
1318
|
+
reservationState,
|
|
1319
|
+
);
|
|
1320
|
+
} finally {
|
|
1321
|
+
Atomics.sub(new Int32Array(ofdBuffer), 6, 1);
|
|
1322
|
+
}
|
|
1323
|
+
};
|
|
1324
|
+
const resolved = this.resolveLockIno(handle);
|
|
1325
|
+
if (resolved instanceof Promise) {
|
|
1326
|
+
const pending = resolved
|
|
1327
|
+
.then(
|
|
1328
|
+
(ino) => {
|
|
1329
|
+
finish(ino);
|
|
1330
|
+
this.sweep();
|
|
1331
|
+
},
|
|
1332
|
+
() => {
|
|
1333
|
+
Atomics.sub(new Int32Array(ofdBuffer), 6, 1);
|
|
1334
|
+
this.sweep();
|
|
1335
|
+
},
|
|
1336
|
+
)
|
|
1337
|
+
.finally(() => {
|
|
1338
|
+
session.pendingOfdAdoptions.delete(pending);
|
|
1339
|
+
});
|
|
1340
|
+
session.pendingOfdAdoptions.add(pending);
|
|
1341
|
+
this.trackWork(session, pending);
|
|
1342
|
+
return;
|
|
1343
|
+
}
|
|
1344
|
+
finish(resolved);
|
|
1345
|
+
}
|
|
1346
|
+
private isSessionLive(owner: number): boolean {
|
|
1347
|
+
const session = this.sessionsByOwner.get(owner);
|
|
1348
|
+
if (!session) return false;
|
|
1349
|
+
return !session.closed && !session.locksReleased && !sessionRevoked(session);
|
|
1350
|
+
}
|
|
1351
|
+
private hasPendingHandleTransfer(owner: number, handle: number): boolean {
|
|
1352
|
+
const session = this.sessionsByOwner.get(owner);
|
|
1353
|
+
if (!session) return false;
|
|
1354
|
+
const key = session.handles.get(handle);
|
|
1355
|
+
return key !== undefined && Atomics.load(session.capabilities, key + 2) > 0;
|
|
1356
|
+
}
|
|
1357
|
+
private ofdLiveness(): OfdLiveness {
|
|
1358
|
+
return {
|
|
1359
|
+
isSessionLive: (owner) => this.isSessionLive(owner),
|
|
1360
|
+
hasPendingTransfer: (owner, handle) => this.hasPendingHandleTransfer(owner, handle),
|
|
1361
|
+
};
|
|
1362
|
+
}
|
|
1363
|
+
private applyLockOperation(
|
|
1364
|
+
session: Session,
|
|
1365
|
+
id: number,
|
|
1366
|
+
operation: string,
|
|
1367
|
+
args: any[],
|
|
1368
|
+
ino: number,
|
|
1369
|
+
): unknown {
|
|
1370
|
+
const table = this.ensureLockTable();
|
|
1371
|
+
const owner = session.connection.owner;
|
|
1372
|
+
const posixOwner = this.sessionPosixOwner(session);
|
|
1373
|
+
const handle = args[0] as number;
|
|
1374
|
+
if (operation === "lockFlock") {
|
|
1375
|
+
const [, ofdId, op] = args;
|
|
1376
|
+
if (typeof ofdId !== "string" || typeof op !== "number") throw new FilesystemError(28);
|
|
1377
|
+
const commit = table.flock(ino, ofdId, owner, op, true, handle);
|
|
1378
|
+
if (commit.rollback)
|
|
1379
|
+
session.lockTransactions.set(id, { rollback: commit.rollback, settle: commit.settle });
|
|
1380
|
+
return commit.errno;
|
|
1381
|
+
}
|
|
1382
|
+
if (operation === "lockRange") {
|
|
1383
|
+
const [, ofdRaw, pid, type, start, len, blocking] = args;
|
|
1384
|
+
const ofdId = ofdRaw === null ? undefined : ofdRaw;
|
|
1385
|
+
if (
|
|
1386
|
+
(ofdId !== undefined && typeof ofdId !== "string") ||
|
|
1387
|
+
typeof pid !== "number" ||
|
|
1388
|
+
!Number.isInteger(pid) ||
|
|
1389
|
+
typeof type !== "number" ||
|
|
1390
|
+
typeof start !== "number" ||
|
|
1391
|
+
typeof len !== "number" ||
|
|
1392
|
+
typeof blocking !== "boolean"
|
|
1393
|
+
)
|
|
1394
|
+
throw new FilesystemError(28);
|
|
1395
|
+
const commit = table.setRangeLock(
|
|
1396
|
+
ino,
|
|
1397
|
+
ofdId,
|
|
1398
|
+
owner,
|
|
1399
|
+
pid,
|
|
1400
|
+
{ type: type as 0 | 1 | 2, start, len },
|
|
1401
|
+
blocking,
|
|
1402
|
+
true,
|
|
1403
|
+
handle,
|
|
1404
|
+
posixOwner,
|
|
1405
|
+
);
|
|
1406
|
+
if (commit.rollback)
|
|
1407
|
+
session.lockTransactions.set(id, { rollback: commit.rollback, settle: commit.settle });
|
|
1408
|
+
return commit.errno;
|
|
1409
|
+
}
|
|
1410
|
+
if (operation === "lockGetRange") {
|
|
1411
|
+
const [, ofdRaw, type, start, len] = args;
|
|
1412
|
+
const ofdId = ofdRaw === null ? undefined : ofdRaw;
|
|
1413
|
+
if (
|
|
1414
|
+
(ofdId !== undefined && typeof ofdId !== "string") ||
|
|
1415
|
+
typeof type !== "number" ||
|
|
1416
|
+
typeof start !== "number" ||
|
|
1417
|
+
typeof len !== "number"
|
|
1418
|
+
)
|
|
1419
|
+
throw new FilesystemError(28);
|
|
1420
|
+
return table.getRangeLock(
|
|
1421
|
+
ino,
|
|
1422
|
+
ofdId,
|
|
1423
|
+
owner,
|
|
1424
|
+
{ type: type as 0 | 1 | 2, start, len },
|
|
1425
|
+
posixOwner,
|
|
1426
|
+
);
|
|
1427
|
+
}
|
|
1428
|
+
if (operation === "lockReleaseOfd") {
|
|
1429
|
+
if (typeof args[1] !== "string") throw new FilesystemError(28);
|
|
1430
|
+
table.releaseOfd(ino, args[1], owner, this.ofdLiveness(), args[2] === true);
|
|
1431
|
+
return 0;
|
|
1432
|
+
}
|
|
1433
|
+
if (operation === "lockReleasePosix") {
|
|
1434
|
+
table.releasePosix(ino, posixOwner);
|
|
1435
|
+
return 0;
|
|
1436
|
+
}
|
|
1437
|
+
throw new FilesystemError(58);
|
|
1438
|
+
}
|
|
1439
|
+
private lockRequest(
|
|
1440
|
+
session: Session,
|
|
1441
|
+
id: number,
|
|
1442
|
+
operation: string,
|
|
1443
|
+
args: any[],
|
|
1444
|
+
): unknown | Promise<unknown> {
|
|
1445
|
+
if (this.backend.supportsLocks === false) throw new FilesystemError(58);
|
|
1446
|
+
if (operation === "lockHandoffPosix") {
|
|
1447
|
+
const successorOwner = args[0];
|
|
1448
|
+
if (
|
|
1449
|
+
typeof successorOwner !== "number" ||
|
|
1450
|
+
!Number.isInteger(successorOwner) ||
|
|
1451
|
+
successorOwner <= 0 ||
|
|
1452
|
+
successorOwner === session.connection.owner ||
|
|
1453
|
+
(this.ownerStates && successorOwner >= this.ownerStates.length)
|
|
1454
|
+
) {
|
|
1455
|
+
throw new FilesystemError(28);
|
|
1456
|
+
}
|
|
1457
|
+
const posixOwner = this.sessionPosixOwner(session);
|
|
1458
|
+
const existing = this.sessionsByOwner.get(successorOwner);
|
|
1459
|
+
if (existing) {
|
|
1460
|
+
if (this.sessionPosixOwner(existing) !== posixOwner) throw new FilesystemError(2);
|
|
1461
|
+
return 0;
|
|
1462
|
+
}
|
|
1463
|
+
if (!this.ownerStates || Atomics.load(this.ownerStates, successorOwner) === 0) {
|
|
1464
|
+
let pending = this.pendingPosixSuccessors.get(posixOwner);
|
|
1465
|
+
if (!pending) {
|
|
1466
|
+
pending = new Set();
|
|
1467
|
+
this.pendingPosixSuccessors.set(posixOwner, pending);
|
|
1468
|
+
}
|
|
1469
|
+
pending.add(successorOwner);
|
|
1470
|
+
}
|
|
1471
|
+
return 0;
|
|
1472
|
+
}
|
|
1473
|
+
const handle = args[0];
|
|
1474
|
+
if (typeof handle !== "number") throw new FilesystemError(28);
|
|
1475
|
+
if (session.pendingOfdAdoptions.size > 0) {
|
|
1476
|
+
return this.trackRequestWork(
|
|
1477
|
+
session,
|
|
1478
|
+
Promise.all(Array.from(session.pendingOfdAdoptions)),
|
|
1479
|
+
).then(() => {
|
|
1480
|
+
if (sessionRevoked(session) || !this.sessions.has(session)) throw new FilesystemError(11);
|
|
1481
|
+
return this.lockRequest(session, id, operation, args);
|
|
1482
|
+
});
|
|
1483
|
+
}
|
|
1484
|
+
const resolved = this.resolveLockIno(handle);
|
|
1485
|
+
if (resolved instanceof Promise)
|
|
1486
|
+
return this.trackRequestWork(session, resolved).then((ino) => {
|
|
1487
|
+
if (sessionRevoked(session) || !this.sessions.has(session)) throw new FilesystemError(11);
|
|
1488
|
+
return this.applyLockOperation(session, id, operation, args, ino);
|
|
1489
|
+
});
|
|
1490
|
+
return this.applyLockOperation(session, id, operation, args, resolved);
|
|
1491
|
+
}
|
|
1492
|
+
private requestInactive(session: Session, buffer: FilesystemRequestBuffer): boolean {
|
|
1493
|
+
return (
|
|
1494
|
+
this.ownerStopping ||
|
|
1495
|
+
sessionRevoked(session) ||
|
|
1496
|
+
!this.sessions.has(session) ||
|
|
1497
|
+
requestCancelled(buffer)
|
|
1498
|
+
);
|
|
1499
|
+
}
|
|
1500
|
+
private holdRetryHandles(
|
|
1501
|
+
session: Session,
|
|
1502
|
+
bytes: Uint8Array,
|
|
1503
|
+
inputLength: number,
|
|
1504
|
+
prepared: PreparedRequest | undefined,
|
|
1505
|
+
): PreparedRequest {
|
|
1506
|
+
const next = prepared ?? { handles: [] };
|
|
1507
|
+
const input = unpack(bytes.subarray(0, inputLength));
|
|
1508
|
+
this.checkRequest(session, input.operation, input.args, next.handles);
|
|
1509
|
+
return next;
|
|
1510
|
+
}
|
|
1511
|
+
private tryHoldRetryHandles(
|
|
1512
|
+
session: Session,
|
|
1513
|
+
bytes: Uint8Array,
|
|
1514
|
+
inputLength: number,
|
|
1515
|
+
prepared: PreparedRequest | undefined,
|
|
1516
|
+
retryError: LockContentionError | undefined,
|
|
1517
|
+
):
|
|
1518
|
+
| { readonly ok: true; readonly prepared: PreparedRequest }
|
|
1519
|
+
| { readonly ok: false; readonly error: unknown } {
|
|
1520
|
+
try {
|
|
1521
|
+
return { ok: true, prepared: this.holdRetryHandles(session, bytes, inputLength, prepared) };
|
|
1522
|
+
} catch (error) {
|
|
1523
|
+
retryError?.cancel();
|
|
1524
|
+
return { ok: false, error };
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
private scheduleRetry(
|
|
1528
|
+
session: Session,
|
|
1529
|
+
id: number,
|
|
1530
|
+
buffer: FilesystemRequestBuffer,
|
|
1531
|
+
prepared: PreparedRequest,
|
|
1532
|
+
lockRetry: LockContentionError | undefined,
|
|
1533
|
+
dirtyRetry: number | undefined,
|
|
1534
|
+
): void {
|
|
1535
|
+
if (lockRetry !== undefined) {
|
|
1536
|
+
if (this.requestInactive(session, buffer)) lockRetry.cancel();
|
|
1537
|
+
session.lockWaiters.set(id, lockRetry.cancel);
|
|
1538
|
+
const wait = lockRetry.wait.then(() => {
|
|
1539
|
+
session.lockWaiters.delete(id);
|
|
1540
|
+
});
|
|
1541
|
+
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1542
|
+
return;
|
|
1543
|
+
}
|
|
1544
|
+
if (dirtyRetry !== undefined) {
|
|
1545
|
+
const wait = this.backend.waitForDirtyCapacity!(
|
|
1546
|
+
dirtyRetry,
|
|
1547
|
+
() => sessionRevoked(session) || requestCancelled(buffer),
|
|
1548
|
+
);
|
|
1549
|
+
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
private settleLock(session: Session, id: number, success: boolean): void {
|
|
1553
|
+
const settlement = session.lockTransactions.get(id);
|
|
1554
|
+
if (!settlement) return;
|
|
1555
|
+
session.lockTransactions.delete(id);
|
|
1556
|
+
if (!success) {
|
|
1557
|
+
settlement.rollback();
|
|
1558
|
+
return;
|
|
1559
|
+
}
|
|
1560
|
+
session.lockResponses.set(id, settlement);
|
|
1561
|
+
}
|
|
1150
1562
|
private settleWalk(session: Session, id: number, success: boolean): void {
|
|
1151
1563
|
const transaction = session.walkTransactions.get(id);
|
|
1152
1564
|
if (!transaction) return;
|
|
@@ -1184,6 +1596,7 @@ export class FilesystemService {
|
|
|
1184
1596
|
this.checkSocketTree(session, operation, args);
|
|
1185
1597
|
if (operation.startsWith("walk")) return this.walkRequest(session, id, operation, args);
|
|
1186
1598
|
if (operation.startsWith("watch")) return this.watchRequest(session, id, operation, args);
|
|
1599
|
+
if (operation.startsWith("lock")) return this.lockRequest(session, id, operation, args);
|
|
1187
1600
|
if (operation === "stageWrite") return this.stageWrite(session, id, a, b, c, data);
|
|
1188
1601
|
if (operation === "take") return this.takeResponseChunk(session, a, b, c);
|
|
1189
1602
|
if (operation === "retain") {
|
|
@@ -1383,10 +1796,10 @@ export class FilesystemService {
|
|
|
1383
1796
|
let committedWrite = false;
|
|
1384
1797
|
let admission: PublicationAdmission | undefined;
|
|
1385
1798
|
let dirtyRetry: number | undefined;
|
|
1799
|
+
let lockRetry: LockContentionError | undefined;
|
|
1386
1800
|
const epoch = session.epoch;
|
|
1387
1801
|
try {
|
|
1388
|
-
if (
|
|
1389
|
-
throw new FilesystemError(11);
|
|
1802
|
+
if (this.requestInactive(session, buffer)) throw new FilesystemError(11);
|
|
1390
1803
|
const { inputLength, dataLength } = requestLengths(buffer, bytes);
|
|
1391
1804
|
const input = decoded ?? unpack(bytes.subarray(0, inputLength));
|
|
1392
1805
|
const { operation, args } = input;
|
|
@@ -1439,14 +1852,18 @@ export class FilesystemService {
|
|
|
1439
1852
|
} catch (error) {
|
|
1440
1853
|
this.discardResponse(session, id);
|
|
1441
1854
|
const dirtyChange = this.dirtyRetryChange(error);
|
|
1442
|
-
|
|
1855
|
+
const retryError = error instanceof LockContentionError ? error : undefined;
|
|
1856
|
+
const retry =
|
|
1857
|
+
retryError || dirtyChange !== undefined
|
|
1858
|
+
? this.tryHoldRetryHandles(session, bytes, cells[1]!, prepared, retryError)
|
|
1859
|
+
: undefined;
|
|
1860
|
+
if (retry?.ok) {
|
|
1861
|
+
prepared = retry.prepared;
|
|
1862
|
+
lockRetry = retryError;
|
|
1443
1863
|
dirtyRetry = dirtyChange;
|
|
1444
|
-
prepared ??= { handles: [] };
|
|
1445
|
-
const input = unpack(bytes.subarray(0, cells[1]!));
|
|
1446
|
-
this.checkRequest(session, input.operation, input.args, prepared.handles);
|
|
1447
1864
|
preparing = true;
|
|
1448
1865
|
} else {
|
|
1449
|
-
cells[3] = errno(error);
|
|
1866
|
+
cells[3] = errno(retry ? retry.error : error);
|
|
1450
1867
|
state = cells[3] === 11 ? State.Cancelled : State.Failed;
|
|
1451
1868
|
}
|
|
1452
1869
|
} finally {
|
|
@@ -1464,6 +1881,7 @@ export class FilesystemService {
|
|
|
1464
1881
|
state = this.settleCancelledState(session, id, buffer, state, staging, committedWrite);
|
|
1465
1882
|
this.settleWalk(session, id, state === State.Complete);
|
|
1466
1883
|
this.settleWatch(session, id, state === State.Complete || state === State.Overflow);
|
|
1884
|
+
this.settleLock(session, id, state === State.Complete);
|
|
1467
1885
|
Atomics.store(cells, 0, state);
|
|
1468
1886
|
Atomics.notify(cells, 0);
|
|
1469
1887
|
wake(session.connection);
|
|
@@ -1472,18 +1890,12 @@ export class FilesystemService {
|
|
|
1472
1890
|
session.privateInputs.delete(id);
|
|
1473
1891
|
this.retainRequestReply(session, id, buffer);
|
|
1474
1892
|
session.connection.port.postMessage({ completed: id });
|
|
1475
|
-
this.
|
|
1893
|
+
this.sweepSessions(false);
|
|
1476
1894
|
} else {
|
|
1477
1895
|
session.inFlightWrites.delete(id);
|
|
1478
1896
|
}
|
|
1479
1897
|
}
|
|
1480
|
-
if (
|
|
1481
|
-
const wait = this.backend.waitForDirtyCapacity!(
|
|
1482
|
-
dirtyRetry,
|
|
1483
|
-
() => sessionRevoked(session) || requestCancelled(buffer),
|
|
1484
|
-
);
|
|
1485
|
-
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1486
|
-
}
|
|
1898
|
+
if (prepared) this.scheduleRetry(session, id, buffer, prepared, lockRetry, dirtyRetry);
|
|
1487
1899
|
}
|
|
1488
1900
|
private settleCancelledState(
|
|
1489
1901
|
session: Session,
|
|
@@ -1532,6 +1944,7 @@ export class FilesystemService {
|
|
|
1532
1944
|
session.responses.has(id) ||
|
|
1533
1945
|
session.walkResponses.has(id) ||
|
|
1534
1946
|
session.watchResponses.has(id) ||
|
|
1947
|
+
session.lockResponses.has(id) ||
|
|
1535
1948
|
session.stages.has(id)
|
|
1536
1949
|
) {
|
|
1537
1950
|
session.retainedRequests.set(id, buffer);
|
|
@@ -1940,6 +2353,9 @@ export class FilesystemService {
|
|
|
1940
2353
|
return { ...pin, bytes: Array.from(pin.bytes) };
|
|
1941
2354
|
}
|
|
1942
2355
|
sweep(): void {
|
|
2356
|
+
this.sweepSessions(true);
|
|
2357
|
+
}
|
|
2358
|
+
private sweepSessions(reapOfds: boolean): void {
|
|
1943
2359
|
if (this.shutdownOwner) return;
|
|
1944
2360
|
// A close can itself wait for a holder that dies later. Recover dead owners even
|
|
1945
2361
|
// on reentry, while keeping session close and release bookkeeping non-reentrant.
|
|
@@ -1950,6 +2366,10 @@ export class FilesystemService {
|
|
|
1950
2366
|
this.sweeping = true;
|
|
1951
2367
|
try {
|
|
1952
2368
|
for (const session of this.sessions) if (sessionRevoked(session)) this.close(session);
|
|
2369
|
+
if (reapOfds) {
|
|
2370
|
+
this.reapPendingPosixSuccessors();
|
|
2371
|
+
this.lockTable?.reapOfds(this.ofdLiveness());
|
|
2372
|
+
}
|
|
1953
2373
|
} finally {
|
|
1954
2374
|
this.sweeping = false;
|
|
1955
2375
|
}
|
|
@@ -2022,6 +2442,29 @@ export class FilesystemService {
|
|
|
2022
2442
|
this.cancelSessionWatchResponses(session);
|
|
2023
2443
|
this.watchService?.closeOwner(session.connection.owner);
|
|
2024
2444
|
this.backend.closeWatchOwner?.(session.connection.owner);
|
|
2445
|
+
session.locksReleased = true;
|
|
2446
|
+
const posixOwner = this.sessionPosixOwner(session);
|
|
2447
|
+
const posixGroup = this.sessionsByPosixOwner.get(posixOwner);
|
|
2448
|
+
if (posixGroup) {
|
|
2449
|
+
posixGroup.delete(session);
|
|
2450
|
+
if (posixGroup.size === 0) this.sessionsByPosixOwner.delete(posixOwner);
|
|
2451
|
+
}
|
|
2452
|
+
for (const cancel of Array.from(session.lockWaiters.values())) cancel();
|
|
2453
|
+
session.lockWaiters.clear();
|
|
2454
|
+
for (const id of Array.from(session.lockTransactions.keys()))
|
|
2455
|
+
this.settleLock(session, id, false);
|
|
2456
|
+
for (const [id, settlement] of Array.from(session.lockResponses.entries())) {
|
|
2457
|
+
const retained = session.retainedRequests.get(id);
|
|
2458
|
+
if (retained && requestCancelled(retained)) settlement.rollback();
|
|
2459
|
+
else settlement.settle?.();
|
|
2460
|
+
}
|
|
2461
|
+
session.lockResponses.clear();
|
|
2462
|
+
this.lockTable?.releaseSession(
|
|
2463
|
+
session.connection.owner,
|
|
2464
|
+
this.ofdLiveness(),
|
|
2465
|
+
posixOwner,
|
|
2466
|
+
!this.isPosixOwnerAlive(posixOwner),
|
|
2467
|
+
);
|
|
2025
2468
|
session.namespace?.close();
|
|
2026
2469
|
this.stopSessionReader(session);
|
|
2027
2470
|
if (!session.transportFailed) revokeFilesystem(session.connection);
|
|
@@ -2077,6 +2520,7 @@ export class FilesystemService {
|
|
|
2077
2520
|
const publication = session.connection.publication;
|
|
2078
2521
|
if (publication && !this.retirePublication(session)) return;
|
|
2079
2522
|
this.sessions.delete(session);
|
|
2523
|
+
this.sessionsByOwner.delete(session.connection.owner);
|
|
2080
2524
|
}
|
|
2081
2525
|
this.closeDisposedWatchAccount();
|
|
2082
2526
|
if (
|
|
@@ -2197,6 +2641,7 @@ export class FilesystemService {
|
|
|
2197
2641
|
dispose(): void {
|
|
2198
2642
|
if (this.shutdownOwner) return;
|
|
2199
2643
|
this.disposingWatches = true;
|
|
2644
|
+
this.lockTable?.dispose();
|
|
2200
2645
|
this.residentPublication?.dispose();
|
|
2201
2646
|
for (const session of this.sessions) this.close(session);
|
|
2202
2647
|
this.closeDisposedWatchAccount();
|
|
@@ -2220,6 +2665,7 @@ export class FilesystemService {
|
|
|
2220
2665
|
const owner = { done, sessions };
|
|
2221
2666
|
this.shutdownOwner = owner;
|
|
2222
2667
|
this.ownerStopping = true;
|
|
2668
|
+
this.lockTable?.dispose();
|
|
2223
2669
|
this.backend.beginShutdown?.();
|
|
2224
2670
|
for (const session of sessions) this.stopSessionReader(session);
|
|
2225
2671
|
const fences = sessions.map((session) => this.shutdownFence(session));
|