@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/dist/fs/service.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { WalkTable } from "./walk.js";
|
|
2
|
+
import { LockContentionError, LockTable } from "./locks.js";
|
|
3
|
+
import { readSharedOfdBuffer } from "../core/open-file.js";
|
|
2
4
|
import {
|
|
3
5
|
requestMemorySections,
|
|
4
6
|
waitForRequestQuiescence,
|
|
@@ -36,6 +38,8 @@ import {
|
|
|
36
38
|
} from "./publication.js";
|
|
37
39
|
import { reportRuntimeStartup } from "./startup.js";
|
|
38
40
|
import { MemoryBackend } from "./backend.js";
|
|
41
|
+
import { POSIX_LOCK_COUNT } from "./shared.js";
|
|
42
|
+
import { HEADER_WORDS } from "./storage-geometry.js";
|
|
39
43
|
import {
|
|
40
44
|
CAPABILITY_HEADER,
|
|
41
45
|
CAPABILITY_LENGTH,
|
|
@@ -110,6 +114,11 @@ export class FilesystemService {
|
|
|
110
114
|
connectObserved = (owner, connection, acceptResolve) => owner.#connect(connection, acceptResolve);
|
|
111
115
|
}
|
|
112
116
|
sessions = new Set;
|
|
117
|
+
sessionsByOwner = new Map;
|
|
118
|
+
sessionsByPosixOwner = new Map;
|
|
119
|
+
pendingPosixSuccessors = new Map;
|
|
120
|
+
sharedCells;
|
|
121
|
+
ownerStates;
|
|
113
122
|
requestBudget;
|
|
114
123
|
readBudget;
|
|
115
124
|
queue = Promise.resolve();
|
|
@@ -123,6 +132,7 @@ export class FilesystemService {
|
|
|
123
132
|
refusedConnections = new Set;
|
|
124
133
|
residentPublication;
|
|
125
134
|
watchService;
|
|
135
|
+
lockTable;
|
|
126
136
|
disposingWatches = false;
|
|
127
137
|
constructor(backend) {
|
|
128
138
|
this.backend = backend;
|
|
@@ -188,6 +198,10 @@ export class FilesystemService {
|
|
|
188
198
|
walkResponses: new Map,
|
|
189
199
|
watchTransactions: new Map,
|
|
190
200
|
watchResponses: new Map,
|
|
201
|
+
lockTransactions: new Map,
|
|
202
|
+
lockResponses: new Map,
|
|
203
|
+
lockWaiters: new Map,
|
|
204
|
+
pendingOfdAdoptions: new Set,
|
|
191
205
|
requests: captureRequestMemory(connection.requests),
|
|
192
206
|
handles: new Map,
|
|
193
207
|
capabilities: new Int32Array(connection.capabilities, 0, CAPABILITY_LENGTH),
|
|
@@ -208,10 +222,28 @@ export class FilesystemService {
|
|
|
208
222
|
...connection.publication?.observer ? { publicationPins: new Set } : {}
|
|
209
223
|
};
|
|
210
224
|
this.sessions.add(session);
|
|
225
|
+
this.sessionsByOwner.set(connection.owner, session);
|
|
226
|
+
this.sharedCells ??= new Int32Array(connection.shared, 0, HEADER_WORDS);
|
|
227
|
+
this.ownerStates ??= new Int32Array(connection.ownerStates);
|
|
228
|
+
const posixOwner = this.sessionPosixOwner(session);
|
|
229
|
+
let group = this.sessionsByPosixOwner.get(posixOwner);
|
|
230
|
+
if (!group) {
|
|
231
|
+
group = new Set;
|
|
232
|
+
this.sessionsByPosixOwner.set(posixOwner, group);
|
|
233
|
+
}
|
|
234
|
+
group.add(session);
|
|
235
|
+
const pendingSuccessors = this.pendingPosixSuccessors.get(posixOwner);
|
|
236
|
+
if (pendingSuccessors) {
|
|
237
|
+
pendingSuccessors.delete(connection.owner);
|
|
238
|
+
if (pendingSuccessors.size === 0)
|
|
239
|
+
this.pendingPosixSuccessors.delete(posixOwner);
|
|
240
|
+
}
|
|
241
|
+
this.clearMismatchedPosixSuccessor(connection.owner, posixOwner);
|
|
211
242
|
for (const reservation of connection.reservations) {
|
|
212
243
|
const state = new Int32Array(reservation.state);
|
|
213
244
|
if (Atomics.compareExchange(state, 1, 0, 1) !== 0)
|
|
214
245
|
continue;
|
|
246
|
+
let releaseOfdTransfer = reservation.ofd;
|
|
215
247
|
try {
|
|
216
248
|
if (!isRevoked(connection) && !session.handles.has(reservation.handle)) {
|
|
217
249
|
this.backend.retain(reservation.handle);
|
|
@@ -225,9 +257,17 @@ export class FilesystemService {
|
|
|
225
257
|
const index = session.freeEntries.indexOf(reservation.targetKey);
|
|
226
258
|
if (index >= 0)
|
|
227
259
|
session.freeEntries.splice(index, 1);
|
|
228
|
-
} else if (isRevoked(connection))
|
|
260
|
+
} else if (isRevoked(connection)) {
|
|
229
261
|
Atomics.store(session.capabilities, reservation.targetKey + 1, -1);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (releaseOfdTransfer) {
|
|
265
|
+
this.adoptReservationOfd(session, reservation.handle, releaseOfdTransfer, reservation.state);
|
|
266
|
+
releaseOfdTransfer = undefined;
|
|
267
|
+
}
|
|
230
268
|
} finally {
|
|
269
|
+
if (releaseOfdTransfer && releaseOfdTransfer.byteLength >= 32)
|
|
270
|
+
Atomics.sub(new Int32Array(releaseOfdTransfer), 6, 1);
|
|
231
271
|
Atomics.sub(new Int32Array(reservation.source, 0, CAPABILITY_LENGTH), reservation.sourceKey + 2, 1);
|
|
232
272
|
}
|
|
233
273
|
}
|
|
@@ -319,6 +359,7 @@ export class FilesystemService {
|
|
|
319
359
|
}
|
|
320
360
|
if ("discard" in data) {
|
|
321
361
|
session.reader?.cancel(data.discard);
|
|
362
|
+
this.cancelLockWaiter(session, data.discard);
|
|
322
363
|
this.enqueueMaintenance(session, () => this.discardResponse(session, data.discard));
|
|
323
364
|
return;
|
|
324
365
|
}
|
|
@@ -379,16 +420,23 @@ export class FilesystemService {
|
|
|
379
420
|
const state = new Int32Array(reservation.state);
|
|
380
421
|
if (Atomics.load(state, 0) || Atomics.compareExchange(state, 1, 0, 1))
|
|
381
422
|
throw new FilesystemError(11);
|
|
423
|
+
let releaseOfdTransfer = reservation.ofd;
|
|
382
424
|
try {
|
|
383
|
-
if (target.handles.has(reservation.handle))
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
425
|
+
if (!target.handles.has(reservation.handle)) {
|
|
426
|
+
this.backend.retain(reservation.handle);
|
|
427
|
+
target.handles.set(reservation.handle, reservation.targetKey);
|
|
428
|
+
target.transient.set(reservation.handle, true);
|
|
429
|
+
const free = target.freeEntries.indexOf(reservation.targetKey);
|
|
430
|
+
if (free >= 0)
|
|
431
|
+
target.freeEntries.splice(free, 1);
|
|
432
|
+
}
|
|
433
|
+
if (releaseOfdTransfer) {
|
|
434
|
+
this.adoptReservationOfd(target, reservation.handle, releaseOfdTransfer, reservation.state);
|
|
435
|
+
releaseOfdTransfer = undefined;
|
|
436
|
+
}
|
|
391
437
|
} finally {
|
|
438
|
+
if (releaseOfdTransfer && releaseOfdTransfer.byteLength >= 32)
|
|
439
|
+
Atomics.sub(new Int32Array(releaseOfdTransfer), 6, 1);
|
|
392
440
|
Atomics.sub(source.capabilities, reservation.sourceKey + 2, 1);
|
|
393
441
|
}
|
|
394
442
|
}
|
|
@@ -535,6 +583,14 @@ export class FilesystemService {
|
|
|
535
583
|
session.walkResponses.delete(id);
|
|
536
584
|
if (cancelledWalk)
|
|
537
585
|
cancelWalk?.();
|
|
586
|
+
const lockSettlement = session.lockResponses.get(id);
|
|
587
|
+
session.lockResponses.delete(id);
|
|
588
|
+
if (lockSettlement) {
|
|
589
|
+
if (cancelledWalk)
|
|
590
|
+
lockSettlement.rollback();
|
|
591
|
+
else
|
|
592
|
+
lockSettlement.settle?.();
|
|
593
|
+
}
|
|
538
594
|
if (retained)
|
|
539
595
|
releaseRequestBuffer(retained, REQUEST_SERVICE);
|
|
540
596
|
session.retainedRequests.delete(id);
|
|
@@ -776,7 +832,13 @@ export class FilesystemService {
|
|
|
776
832
|
"readdir",
|
|
777
833
|
"readdirPage",
|
|
778
834
|
"resolve",
|
|
779
|
-
"realpath"
|
|
835
|
+
"realpath",
|
|
836
|
+
"probe",
|
|
837
|
+
"lockFlock",
|
|
838
|
+
"lockRange",
|
|
839
|
+
"lockGetRange",
|
|
840
|
+
"lockReleaseOfd",
|
|
841
|
+
"lockReleasePosix"
|
|
780
842
|
].includes(operation))
|
|
781
843
|
this.check(session, a, held);
|
|
782
844
|
if (["create", "symlink", "link", "removePath", "rename"].includes(operation)) {
|
|
@@ -948,6 +1010,274 @@ export class FilesystemService {
|
|
|
948
1010
|
};
|
|
949
1011
|
return backend.shared.ownsServiceMutation ? execute() : backend.shared.mutate(undefined, () => sessionRevoked(session), execute);
|
|
950
1012
|
}
|
|
1013
|
+
cancelLockWaiter(session, id) {
|
|
1014
|
+
const cancel = session.lockWaiters.get(id);
|
|
1015
|
+
if (!cancel)
|
|
1016
|
+
return;
|
|
1017
|
+
session.lockWaiters.delete(id);
|
|
1018
|
+
cancel();
|
|
1019
|
+
}
|
|
1020
|
+
resolveLockIno(handle) {
|
|
1021
|
+
if (this.backend instanceof MemoryBackend)
|
|
1022
|
+
return this.backend.fs.stat(handle).ino;
|
|
1023
|
+
const result = this.backend.execute("stat", [handle], new Uint8Array);
|
|
1024
|
+
if (result instanceof Promise)
|
|
1025
|
+
return result.then((stat) => stat.ino);
|
|
1026
|
+
return result.ino;
|
|
1027
|
+
}
|
|
1028
|
+
ensureLockTable() {
|
|
1029
|
+
if (this.lockTable)
|
|
1030
|
+
return this.lockTable;
|
|
1031
|
+
this.lockTable = new LockTable({
|
|
1032
|
+
onPosixLockCountChange: (count) => {
|
|
1033
|
+
if (this.sharedCells)
|
|
1034
|
+
Atomics.store(this.sharedCells, POSIX_LOCK_COUNT, count);
|
|
1035
|
+
}
|
|
1036
|
+
});
|
|
1037
|
+
return this.lockTable;
|
|
1038
|
+
}
|
|
1039
|
+
sessionPosixOwner(session) {
|
|
1040
|
+
const candidate = session.connection.posixOwner;
|
|
1041
|
+
if (candidate !== undefined && Number.isInteger(candidate) && candidate > 0 && (!this.ownerStates || candidate < this.ownerStates.length)) {
|
|
1042
|
+
return candidate;
|
|
1043
|
+
}
|
|
1044
|
+
return session.connection.owner;
|
|
1045
|
+
}
|
|
1046
|
+
hasLivePosixSession(posixOwner) {
|
|
1047
|
+
const group = this.sessionsByPosixOwner.get(posixOwner);
|
|
1048
|
+
if (!group)
|
|
1049
|
+
return false;
|
|
1050
|
+
for (const member of group) {
|
|
1051
|
+
if (!member.closed && !member.locksReleased && !sessionRevoked(member))
|
|
1052
|
+
return true;
|
|
1053
|
+
}
|
|
1054
|
+
return false;
|
|
1055
|
+
}
|
|
1056
|
+
hasLivePendingPosixSuccessor(posixOwner) {
|
|
1057
|
+
const pending = this.pendingPosixSuccessors.get(posixOwner);
|
|
1058
|
+
if (!pending)
|
|
1059
|
+
return false;
|
|
1060
|
+
for (const owner of Array.from(pending)) {
|
|
1061
|
+
if (this.isSessionLive(owner))
|
|
1062
|
+
continue;
|
|
1063
|
+
if (this.ownerStates && Atomics.load(this.ownerStates, owner) === 0)
|
|
1064
|
+
continue;
|
|
1065
|
+
pending.delete(owner);
|
|
1066
|
+
}
|
|
1067
|
+
if (pending.size > 0)
|
|
1068
|
+
return true;
|
|
1069
|
+
this.pendingPosixSuccessors.delete(posixOwner);
|
|
1070
|
+
return false;
|
|
1071
|
+
}
|
|
1072
|
+
isPosixOwnerAlive(posixOwner) {
|
|
1073
|
+
return this.hasLivePosixSession(posixOwner) || this.hasLivePendingPosixSuccessor(posixOwner);
|
|
1074
|
+
}
|
|
1075
|
+
clearMismatchedPosixSuccessor(connectedOwner, actualPosixOwner) {
|
|
1076
|
+
if (this.pendingPosixSuccessors.size === 0)
|
|
1077
|
+
return;
|
|
1078
|
+
let removed = false;
|
|
1079
|
+
for (const [posixOwner, pending] of Array.from(this.pendingPosixSuccessors.entries())) {
|
|
1080
|
+
if (posixOwner === actualPosixOwner)
|
|
1081
|
+
continue;
|
|
1082
|
+
if (!pending.delete(connectedOwner))
|
|
1083
|
+
continue;
|
|
1084
|
+
removed = true;
|
|
1085
|
+
if (pending.size === 0)
|
|
1086
|
+
this.pendingPosixSuccessors.delete(posixOwner);
|
|
1087
|
+
}
|
|
1088
|
+
if (removed)
|
|
1089
|
+
this.reapPendingPosixSuccessors();
|
|
1090
|
+
}
|
|
1091
|
+
reapPendingPosixSuccessors() {
|
|
1092
|
+
if (this.pendingPosixSuccessors.size === 0)
|
|
1093
|
+
return;
|
|
1094
|
+
for (const posixOwner of Array.from(this.pendingPosixSuccessors.keys())) {
|
|
1095
|
+
if (this.isPosixOwnerAlive(posixOwner))
|
|
1096
|
+
continue;
|
|
1097
|
+
this.lockTable?.releasePosixOwner(posixOwner);
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
adoptReservationOfd(session, handle, ofdBuffer, reservationState) {
|
|
1101
|
+
const ofd = readSharedOfdBuffer(ofdBuffer);
|
|
1102
|
+
if (!ofd) {
|
|
1103
|
+
if (ofdBuffer.byteLength >= 32)
|
|
1104
|
+
Atomics.sub(new Int32Array(ofdBuffer), 6, 1);
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
const finish = (ino) => {
|
|
1108
|
+
try {
|
|
1109
|
+
this.ensureLockTable().adoptReservation(ino, handle, ofd.id, ofd.originOwner, ofdBuffer, session.connection.owner, reservationState);
|
|
1110
|
+
} finally {
|
|
1111
|
+
Atomics.sub(new Int32Array(ofdBuffer), 6, 1);
|
|
1112
|
+
}
|
|
1113
|
+
};
|
|
1114
|
+
const resolved = this.resolveLockIno(handle);
|
|
1115
|
+
if (resolved instanceof Promise) {
|
|
1116
|
+
const pending = resolved.then((ino) => {
|
|
1117
|
+
finish(ino);
|
|
1118
|
+
this.sweep();
|
|
1119
|
+
}, () => {
|
|
1120
|
+
Atomics.sub(new Int32Array(ofdBuffer), 6, 1);
|
|
1121
|
+
this.sweep();
|
|
1122
|
+
}).finally(() => {
|
|
1123
|
+
session.pendingOfdAdoptions.delete(pending);
|
|
1124
|
+
});
|
|
1125
|
+
session.pendingOfdAdoptions.add(pending);
|
|
1126
|
+
this.trackWork(session, pending);
|
|
1127
|
+
return;
|
|
1128
|
+
}
|
|
1129
|
+
finish(resolved);
|
|
1130
|
+
}
|
|
1131
|
+
isSessionLive(owner) {
|
|
1132
|
+
const session = this.sessionsByOwner.get(owner);
|
|
1133
|
+
if (!session)
|
|
1134
|
+
return false;
|
|
1135
|
+
return !session.closed && !session.locksReleased && !sessionRevoked(session);
|
|
1136
|
+
}
|
|
1137
|
+
hasPendingHandleTransfer(owner, handle) {
|
|
1138
|
+
const session = this.sessionsByOwner.get(owner);
|
|
1139
|
+
if (!session)
|
|
1140
|
+
return false;
|
|
1141
|
+
const key = session.handles.get(handle);
|
|
1142
|
+
return key !== undefined && Atomics.load(session.capabilities, key + 2) > 0;
|
|
1143
|
+
}
|
|
1144
|
+
ofdLiveness() {
|
|
1145
|
+
return {
|
|
1146
|
+
isSessionLive: (owner) => this.isSessionLive(owner),
|
|
1147
|
+
hasPendingTransfer: (owner, handle) => this.hasPendingHandleTransfer(owner, handle)
|
|
1148
|
+
};
|
|
1149
|
+
}
|
|
1150
|
+
applyLockOperation(session, id, operation, args, ino) {
|
|
1151
|
+
const table = this.ensureLockTable();
|
|
1152
|
+
const owner = session.connection.owner;
|
|
1153
|
+
const posixOwner = this.sessionPosixOwner(session);
|
|
1154
|
+
const handle = args[0];
|
|
1155
|
+
if (operation === "lockFlock") {
|
|
1156
|
+
const [, ofdId, op] = args;
|
|
1157
|
+
if (typeof ofdId !== "string" || typeof op !== "number")
|
|
1158
|
+
throw new FilesystemError(28);
|
|
1159
|
+
const commit = table.flock(ino, ofdId, owner, op, true, handle);
|
|
1160
|
+
if (commit.rollback)
|
|
1161
|
+
session.lockTransactions.set(id, { rollback: commit.rollback, settle: commit.settle });
|
|
1162
|
+
return commit.errno;
|
|
1163
|
+
}
|
|
1164
|
+
if (operation === "lockRange") {
|
|
1165
|
+
const [, ofdRaw, pid, type, start, len, blocking] = args;
|
|
1166
|
+
const ofdId = ofdRaw === null ? undefined : ofdRaw;
|
|
1167
|
+
if (ofdId !== undefined && typeof ofdId !== "string" || typeof pid !== "number" || !Number.isInteger(pid) || typeof type !== "number" || typeof start !== "number" || typeof len !== "number" || typeof blocking !== "boolean")
|
|
1168
|
+
throw new FilesystemError(28);
|
|
1169
|
+
const commit = table.setRangeLock(ino, ofdId, owner, pid, { type, start, len }, blocking, true, handle, posixOwner);
|
|
1170
|
+
if (commit.rollback)
|
|
1171
|
+
session.lockTransactions.set(id, { rollback: commit.rollback, settle: commit.settle });
|
|
1172
|
+
return commit.errno;
|
|
1173
|
+
}
|
|
1174
|
+
if (operation === "lockGetRange") {
|
|
1175
|
+
const [, ofdRaw, type, start, len] = args;
|
|
1176
|
+
const ofdId = ofdRaw === null ? undefined : ofdRaw;
|
|
1177
|
+
if (ofdId !== undefined && typeof ofdId !== "string" || typeof type !== "number" || typeof start !== "number" || typeof len !== "number")
|
|
1178
|
+
throw new FilesystemError(28);
|
|
1179
|
+
return table.getRangeLock(ino, ofdId, owner, { type, start, len }, posixOwner);
|
|
1180
|
+
}
|
|
1181
|
+
if (operation === "lockReleaseOfd") {
|
|
1182
|
+
if (typeof args[1] !== "string")
|
|
1183
|
+
throw new FilesystemError(28);
|
|
1184
|
+
table.releaseOfd(ino, args[1], owner, this.ofdLiveness(), args[2] === true);
|
|
1185
|
+
return 0;
|
|
1186
|
+
}
|
|
1187
|
+
if (operation === "lockReleasePosix") {
|
|
1188
|
+
table.releasePosix(ino, posixOwner);
|
|
1189
|
+
return 0;
|
|
1190
|
+
}
|
|
1191
|
+
throw new FilesystemError(58);
|
|
1192
|
+
}
|
|
1193
|
+
lockRequest(session, id, operation, args) {
|
|
1194
|
+
if (this.backend.supportsLocks === false)
|
|
1195
|
+
throw new FilesystemError(58);
|
|
1196
|
+
if (operation === "lockHandoffPosix") {
|
|
1197
|
+
const successorOwner = args[0];
|
|
1198
|
+
if (typeof successorOwner !== "number" || !Number.isInteger(successorOwner) || successorOwner <= 0 || successorOwner === session.connection.owner || this.ownerStates && successorOwner >= this.ownerStates.length) {
|
|
1199
|
+
throw new FilesystemError(28);
|
|
1200
|
+
}
|
|
1201
|
+
const posixOwner = this.sessionPosixOwner(session);
|
|
1202
|
+
const existing = this.sessionsByOwner.get(successorOwner);
|
|
1203
|
+
if (existing) {
|
|
1204
|
+
if (this.sessionPosixOwner(existing) !== posixOwner)
|
|
1205
|
+
throw new FilesystemError(2);
|
|
1206
|
+
return 0;
|
|
1207
|
+
}
|
|
1208
|
+
if (!this.ownerStates || Atomics.load(this.ownerStates, successorOwner) === 0) {
|
|
1209
|
+
let pending = this.pendingPosixSuccessors.get(posixOwner);
|
|
1210
|
+
if (!pending) {
|
|
1211
|
+
pending = new Set;
|
|
1212
|
+
this.pendingPosixSuccessors.set(posixOwner, pending);
|
|
1213
|
+
}
|
|
1214
|
+
pending.add(successorOwner);
|
|
1215
|
+
}
|
|
1216
|
+
return 0;
|
|
1217
|
+
}
|
|
1218
|
+
const handle = args[0];
|
|
1219
|
+
if (typeof handle !== "number")
|
|
1220
|
+
throw new FilesystemError(28);
|
|
1221
|
+
if (session.pendingOfdAdoptions.size > 0) {
|
|
1222
|
+
return this.trackRequestWork(session, Promise.all(Array.from(session.pendingOfdAdoptions))).then(() => {
|
|
1223
|
+
if (sessionRevoked(session) || !this.sessions.has(session))
|
|
1224
|
+
throw new FilesystemError(11);
|
|
1225
|
+
return this.lockRequest(session, id, operation, args);
|
|
1226
|
+
});
|
|
1227
|
+
}
|
|
1228
|
+
const resolved = this.resolveLockIno(handle);
|
|
1229
|
+
if (resolved instanceof Promise)
|
|
1230
|
+
return this.trackRequestWork(session, resolved).then((ino) => {
|
|
1231
|
+
if (sessionRevoked(session) || !this.sessions.has(session))
|
|
1232
|
+
throw new FilesystemError(11);
|
|
1233
|
+
return this.applyLockOperation(session, id, operation, args, ino);
|
|
1234
|
+
});
|
|
1235
|
+
return this.applyLockOperation(session, id, operation, args, resolved);
|
|
1236
|
+
}
|
|
1237
|
+
requestInactive(session, buffer) {
|
|
1238
|
+
return this.ownerStopping || sessionRevoked(session) || !this.sessions.has(session) || requestCancelled(buffer);
|
|
1239
|
+
}
|
|
1240
|
+
holdRetryHandles(session, bytes, inputLength, prepared) {
|
|
1241
|
+
const next = prepared ?? { handles: [] };
|
|
1242
|
+
const input = unpack(bytes.subarray(0, inputLength));
|
|
1243
|
+
this.checkRequest(session, input.operation, input.args, next.handles);
|
|
1244
|
+
return next;
|
|
1245
|
+
}
|
|
1246
|
+
tryHoldRetryHandles(session, bytes, inputLength, prepared, retryError) {
|
|
1247
|
+
try {
|
|
1248
|
+
return { ok: true, prepared: this.holdRetryHandles(session, bytes, inputLength, prepared) };
|
|
1249
|
+
} catch (error) {
|
|
1250
|
+
retryError?.cancel();
|
|
1251
|
+
return { ok: false, error };
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
scheduleRetry(session, id, buffer, prepared, lockRetry, dirtyRetry) {
|
|
1255
|
+
if (lockRetry !== undefined) {
|
|
1256
|
+
if (this.requestInactive(session, buffer))
|
|
1257
|
+
lockRetry.cancel();
|
|
1258
|
+
session.lockWaiters.set(id, lockRetry.cancel);
|
|
1259
|
+
const wait = lockRetry.wait.then(() => {
|
|
1260
|
+
session.lockWaiters.delete(id);
|
|
1261
|
+
});
|
|
1262
|
+
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1263
|
+
return;
|
|
1264
|
+
}
|
|
1265
|
+
if (dirtyRetry !== undefined) {
|
|
1266
|
+
const wait = this.backend.waitForDirtyCapacity(dirtyRetry, () => sessionRevoked(session) || requestCancelled(buffer));
|
|
1267
|
+
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
settleLock(session, id, success) {
|
|
1271
|
+
const settlement = session.lockTransactions.get(id);
|
|
1272
|
+
if (!settlement)
|
|
1273
|
+
return;
|
|
1274
|
+
session.lockTransactions.delete(id);
|
|
1275
|
+
if (!success) {
|
|
1276
|
+
settlement.rollback();
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
session.lockResponses.set(id, settlement);
|
|
1280
|
+
}
|
|
951
1281
|
settleWalk(session, id, success) {
|
|
952
1282
|
const transaction = session.walkTransactions.get(id);
|
|
953
1283
|
if (!transaction)
|
|
@@ -983,6 +1313,8 @@ export class FilesystemService {
|
|
|
983
1313
|
return this.walkRequest(session, id, operation, args);
|
|
984
1314
|
if (operation.startsWith("watch"))
|
|
985
1315
|
return this.watchRequest(session, id, operation, args);
|
|
1316
|
+
if (operation.startsWith("lock"))
|
|
1317
|
+
return this.lockRequest(session, id, operation, args);
|
|
986
1318
|
if (operation === "stageWrite")
|
|
987
1319
|
return this.stageWrite(session, id, a, b, c, data);
|
|
988
1320
|
if (operation === "take")
|
|
@@ -1146,9 +1478,10 @@ export class FilesystemService {
|
|
|
1146
1478
|
let committedWrite = false;
|
|
1147
1479
|
let admission;
|
|
1148
1480
|
let dirtyRetry;
|
|
1481
|
+
let lockRetry;
|
|
1149
1482
|
const epoch = session.epoch;
|
|
1150
1483
|
try {
|
|
1151
|
-
if (
|
|
1484
|
+
if (this.requestInactive(session, buffer))
|
|
1152
1485
|
throw new FilesystemError(11);
|
|
1153
1486
|
const { inputLength, dataLength } = requestLengths(buffer, bytes);
|
|
1154
1487
|
const input = decoded ?? unpack(bytes.subarray(0, inputLength));
|
|
@@ -1190,14 +1523,15 @@ export class FilesystemService {
|
|
|
1190
1523
|
} catch (error) {
|
|
1191
1524
|
this.discardResponse(session, id);
|
|
1192
1525
|
const dirtyChange = this.dirtyRetryChange(error);
|
|
1193
|
-
|
|
1526
|
+
const retryError = error instanceof LockContentionError ? error : undefined;
|
|
1527
|
+
const retry = retryError || dirtyChange !== undefined ? this.tryHoldRetryHandles(session, bytes, cells[1], prepared, retryError) : undefined;
|
|
1528
|
+
if (retry?.ok) {
|
|
1529
|
+
prepared = retry.prepared;
|
|
1530
|
+
lockRetry = retryError;
|
|
1194
1531
|
dirtyRetry = dirtyChange;
|
|
1195
|
-
prepared ??= { handles: [] };
|
|
1196
|
-
const input = unpack(bytes.subarray(0, cells[1]));
|
|
1197
|
-
this.checkRequest(session, input.operation, input.args, prepared.handles);
|
|
1198
1532
|
preparing = true;
|
|
1199
1533
|
} else {
|
|
1200
|
-
cells[3] = errno(error);
|
|
1534
|
+
cells[3] = errno(retry ? retry.error : error);
|
|
1201
1535
|
state = cells[3] === 11 ? State.Cancelled : State.Failed;
|
|
1202
1536
|
}
|
|
1203
1537
|
} finally {
|
|
@@ -1214,6 +1548,7 @@ export class FilesystemService {
|
|
|
1214
1548
|
state = this.settleCancelledState(session, id, buffer, state, staging, committedWrite);
|
|
1215
1549
|
this.settleWalk(session, id, state === State.Complete);
|
|
1216
1550
|
this.settleWatch(session, id, state === State.Complete || state === State.Overflow);
|
|
1551
|
+
this.settleLock(session, id, state === State.Complete);
|
|
1217
1552
|
Atomics.store(cells, 0, state);
|
|
1218
1553
|
Atomics.notify(cells, 0);
|
|
1219
1554
|
wake(session.connection);
|
|
@@ -1222,15 +1557,13 @@ export class FilesystemService {
|
|
|
1222
1557
|
session.privateInputs.delete(id);
|
|
1223
1558
|
this.retainRequestReply(session, id, buffer);
|
|
1224
1559
|
session.connection.port.postMessage({ completed: id });
|
|
1225
|
-
this.
|
|
1560
|
+
this.sweepSessions(false);
|
|
1226
1561
|
} else {
|
|
1227
1562
|
session.inFlightWrites.delete(id);
|
|
1228
1563
|
}
|
|
1229
1564
|
}
|
|
1230
|
-
if (
|
|
1231
|
-
|
|
1232
|
-
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1233
|
-
}
|
|
1565
|
+
if (prepared)
|
|
1566
|
+
this.scheduleRetry(session, id, buffer, prepared, lockRetry, dirtyRetry);
|
|
1234
1567
|
}
|
|
1235
1568
|
settleCancelledState(session, id, buffer, state, staging, committedWrite) {
|
|
1236
1569
|
if (sessionRevoked(session) || requestCancelled(buffer)) {
|
|
@@ -1267,7 +1600,7 @@ export class FilesystemService {
|
|
|
1267
1600
|
});
|
|
1268
1601
|
}
|
|
1269
1602
|
retainRequestReply(session, id, buffer) {
|
|
1270
|
-
if (session.responses.has(id) || session.walkResponses.has(id) || session.watchResponses.has(id) || session.stages.has(id)) {
|
|
1603
|
+
if (session.responses.has(id) || session.walkResponses.has(id) || session.watchResponses.has(id) || session.lockResponses.has(id) || session.stages.has(id)) {
|
|
1271
1604
|
session.retainedRequests.set(id, buffer);
|
|
1272
1605
|
return;
|
|
1273
1606
|
}
|
|
@@ -1543,6 +1876,9 @@ export class FilesystemService {
|
|
|
1543
1876
|
return { ...pin, bytes: Array.from(pin.bytes) };
|
|
1544
1877
|
}
|
|
1545
1878
|
sweep() {
|
|
1879
|
+
this.sweepSessions(true);
|
|
1880
|
+
}
|
|
1881
|
+
sweepSessions(reapOfds) {
|
|
1546
1882
|
if (this.shutdownOwner)
|
|
1547
1883
|
return;
|
|
1548
1884
|
for (const session of this.sessions)
|
|
@@ -1555,6 +1891,10 @@ export class FilesystemService {
|
|
|
1555
1891
|
for (const session of this.sessions)
|
|
1556
1892
|
if (sessionRevoked(session))
|
|
1557
1893
|
this.close(session);
|
|
1894
|
+
if (reapOfds) {
|
|
1895
|
+
this.reapPendingPosixSuccessors();
|
|
1896
|
+
this.lockTable?.reapOfds(this.ofdLiveness());
|
|
1897
|
+
}
|
|
1558
1898
|
} finally {
|
|
1559
1899
|
this.sweeping = false;
|
|
1560
1900
|
}
|
|
@@ -1631,6 +1971,28 @@ export class FilesystemService {
|
|
|
1631
1971
|
this.cancelSessionWatchResponses(session);
|
|
1632
1972
|
this.watchService?.closeOwner(session.connection.owner);
|
|
1633
1973
|
this.backend.closeWatchOwner?.(session.connection.owner);
|
|
1974
|
+
session.locksReleased = true;
|
|
1975
|
+
const posixOwner = this.sessionPosixOwner(session);
|
|
1976
|
+
const posixGroup = this.sessionsByPosixOwner.get(posixOwner);
|
|
1977
|
+
if (posixGroup) {
|
|
1978
|
+
posixGroup.delete(session);
|
|
1979
|
+
if (posixGroup.size === 0)
|
|
1980
|
+
this.sessionsByPosixOwner.delete(posixOwner);
|
|
1981
|
+
}
|
|
1982
|
+
for (const cancel of Array.from(session.lockWaiters.values()))
|
|
1983
|
+
cancel();
|
|
1984
|
+
session.lockWaiters.clear();
|
|
1985
|
+
for (const id of Array.from(session.lockTransactions.keys()))
|
|
1986
|
+
this.settleLock(session, id, false);
|
|
1987
|
+
for (const [id, settlement] of Array.from(session.lockResponses.entries())) {
|
|
1988
|
+
const retained = session.retainedRequests.get(id);
|
|
1989
|
+
if (retained && requestCancelled(retained))
|
|
1990
|
+
settlement.rollback();
|
|
1991
|
+
else
|
|
1992
|
+
settlement.settle?.();
|
|
1993
|
+
}
|
|
1994
|
+
session.lockResponses.clear();
|
|
1995
|
+
this.lockTable?.releaseSession(session.connection.owner, this.ofdLiveness(), posixOwner, !this.isPosixOwnerAlive(posixOwner));
|
|
1634
1996
|
session.namespace?.close();
|
|
1635
1997
|
this.stopSessionReader(session);
|
|
1636
1998
|
if (!session.transportFailed)
|
|
@@ -1677,6 +2039,7 @@ export class FilesystemService {
|
|
|
1677
2039
|
if (publication && !this.retirePublication(session))
|
|
1678
2040
|
return;
|
|
1679
2041
|
this.sessions.delete(session);
|
|
2042
|
+
this.sessionsByOwner.delete(session.connection.owner);
|
|
1680
2043
|
}
|
|
1681
2044
|
this.closeDisposedWatchAccount();
|
|
1682
2045
|
if (!this.sessions.size && this.backend instanceof MemoryBackend && this.backend.shared.publication?.stopped)
|
|
@@ -1777,6 +2140,7 @@ export class FilesystemService {
|
|
|
1777
2140
|
if (this.shutdownOwner)
|
|
1778
2141
|
return;
|
|
1779
2142
|
this.disposingWatches = true;
|
|
2143
|
+
this.lockTable?.dispose();
|
|
1780
2144
|
this.residentPublication?.dispose();
|
|
1781
2145
|
for (const session of this.sessions)
|
|
1782
2146
|
this.close(session);
|
|
@@ -1795,6 +2159,7 @@ export class FilesystemService {
|
|
|
1795
2159
|
const owner = { done, sessions };
|
|
1796
2160
|
this.shutdownOwner = owner;
|
|
1797
2161
|
this.ownerStopping = true;
|
|
2162
|
+
this.lockTable?.dispose();
|
|
1798
2163
|
this.backend.beginShutdown?.();
|
|
1799
2164
|
for (const session of sessions)
|
|
1800
2165
|
this.stopSessionReader(session);
|
package/dist/fs/shared.js
CHANGED
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
PAGE_CELLS,
|
|
39
39
|
SLOT_WORDS,
|
|
40
40
|
HEADER_WORDS,
|
|
41
|
+
MACHINE_ID_OFFSET,
|
|
41
42
|
SLOT_OFFSET,
|
|
42
43
|
TABLE_OWNER_OFFSET,
|
|
43
44
|
PAGE_CELL_OFFSET,
|
|
@@ -45,6 +46,7 @@ import {
|
|
|
45
46
|
DATA_OFFSET,
|
|
46
47
|
SHARED_ROOT_BYTES
|
|
47
48
|
} from "./storage-geometry.js";
|
|
49
|
+
import { validateMachineId } from "../core/devices.js";
|
|
48
50
|
import { SHARED_SEGMENT_BYTES } from "./shared-capacity.js";
|
|
49
51
|
export { SLOT_WORDS } from "./storage-geometry.js";
|
|
50
52
|
const MAGIC = 1113015891;
|
|
@@ -61,6 +63,7 @@ const FREED_REVISION_LOW = 7;
|
|
|
61
63
|
const FREED_REVISION_HIGH = 8;
|
|
62
64
|
const REGISTERED_SLOTS = 11;
|
|
63
65
|
const CHANGE_SEQUENCE = 12;
|
|
66
|
+
export const POSIX_LOCK_COUNT = 13;
|
|
64
67
|
const UNLINK_GENERATION = 31;
|
|
65
68
|
const PHYSICAL_HINT = 27;
|
|
66
69
|
const TABLE_HINT = 28;
|
|
@@ -84,6 +87,47 @@ const REVISION = 10;
|
|
|
84
87
|
const REVISION_STATE = 11;
|
|
85
88
|
const RESIDENT = 1;
|
|
86
89
|
const copyBytes = Uint8Array.prototype.set;
|
|
90
|
+
const HEX_DIGITS = "0123456789abcdef";
|
|
91
|
+
function hexWord(word) {
|
|
92
|
+
let out = "";
|
|
93
|
+
for (let shift = 28;shift >= 0; shift -= 4)
|
|
94
|
+
out += HEX_DIGITS[word >>> shift & 15];
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
export function machineIdFromWorkspace(workspaceId) {
|
|
98
|
+
return validateMachineId(workspaceId.replaceAll("-", "").toLowerCase());
|
|
99
|
+
}
|
|
100
|
+
export function writeSharedMachineId(cells, machineId) {
|
|
101
|
+
const valid = validateMachineId(machineId);
|
|
102
|
+
const seqWord = MACHINE_ID_OFFSET + 4;
|
|
103
|
+
const current = Atomics.load(cells, seqWord) >>> 0;
|
|
104
|
+
const base = current & ~1;
|
|
105
|
+
Atomics.store(cells, seqWord, base + 1 | 0);
|
|
106
|
+
for (let i = 0;i < 4; i++) {
|
|
107
|
+
const word = Number.parseInt(valid.slice(i * 8, i * 8 + 8), 16) | 0;
|
|
108
|
+
Atomics.store(cells, MACHINE_ID_OFFSET + i, word);
|
|
109
|
+
}
|
|
110
|
+
const nextEven = base + 2 | 0;
|
|
111
|
+
Atomics.store(cells, seqWord, nextEven === 0 ? 2 : nextEven);
|
|
112
|
+
}
|
|
113
|
+
export function readSharedMachineId(cells) {
|
|
114
|
+
const seqWord = MACHINE_ID_OFFSET + 4;
|
|
115
|
+
for (let attempt = 0;attempt < 64; attempt++) {
|
|
116
|
+
const seq1 = atomicsLoad(cells, seqWord);
|
|
117
|
+
if (seq1 === 0)
|
|
118
|
+
return;
|
|
119
|
+
if ((seq1 & 1) !== 0)
|
|
120
|
+
continue;
|
|
121
|
+
const w0 = atomicsLoad(cells, MACHINE_ID_OFFSET);
|
|
122
|
+
const w1 = atomicsLoad(cells, MACHINE_ID_OFFSET + 1);
|
|
123
|
+
const w2 = atomicsLoad(cells, MACHINE_ID_OFFSET + 2);
|
|
124
|
+
const w3 = atomicsLoad(cells, MACHINE_ID_OFFSET + 3);
|
|
125
|
+
const seq2 = atomicsLoad(cells, seqWord);
|
|
126
|
+
if (seq1 === seq2)
|
|
127
|
+
return hexWord(w0) + hexWord(w1) + hexWord(w2) + hexWord(w3);
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
87
131
|
|
|
88
132
|
export class MissingSegmentError extends Error {
|
|
89
133
|
}
|
|
@@ -217,6 +261,12 @@ export class SharedFilesystemState {
|
|
|
217
261
|
setClientMode(mode) {
|
|
218
262
|
Atomics.store(this.cells, CLIENT_MODE, mode | 0);
|
|
219
263
|
}
|
|
264
|
+
setMachineId(machineId) {
|
|
265
|
+
writeSharedMachineId(this.cells, machineId);
|
|
266
|
+
}
|
|
267
|
+
getMachineId() {
|
|
268
|
+
return readSharedMachineId(this.cells);
|
|
269
|
+
}
|
|
220
270
|
isDirectDisabled() {
|
|
221
271
|
return (atomicsLoad(this.cells, CLIENT_MODE) & CLIENT_MODE_NO_DIRECT_IO) !== 0;
|
|
222
272
|
}
|
|
@@ -234,7 +284,7 @@ export class SharedFilesystemState {
|
|
|
234
284
|
Atomics.store(this.cells, PHYSICAL_HINT, 0);
|
|
235
285
|
Atomics.store(this.cells, TABLE_HINT, 0);
|
|
236
286
|
Atomics.store(this.cells, PINNED_PAGES, 0);
|
|
237
|
-
for (let i = 32;i <
|
|
287
|
+
for (let i = 32;i < MACHINE_ID_OFFSET; i++)
|
|
238
288
|
Atomics.store(this.cells, i, 0);
|
|
239
289
|
this.owners.fill(0);
|
|
240
290
|
this.slots.fill(0);
|
|
@@ -1943,6 +1993,9 @@ export class SharedFilesystemState {
|
|
|
1943
1993
|
unlinkGeneration() {
|
|
1944
1994
|
return Atomics.load(this.cells, UNLINK_GENERATION);
|
|
1945
1995
|
}
|
|
1996
|
+
posixLockCount() {
|
|
1997
|
+
return atomicsLoad(this.cells, POSIX_LOCK_COUNT);
|
|
1998
|
+
}
|
|
1946
1999
|
continueUnlinks(previous) {
|
|
1947
2000
|
const floor = previous.unlinkGeneration() + 1;
|
|
1948
2001
|
for (;; ) {
|
|
@@ -17,6 +17,8 @@ export const ROOT_CELL_BASE = TABLE_BLOCKS * TABLE_ENTRIES;
|
|
|
17
17
|
export const PAGE_CELLS = ROOT_CELL_BASE + MAX_FILES * FILE_ROOT_WORDS;
|
|
18
18
|
export const SLOT_WORDS = 28;
|
|
19
19
|
const DIRTY_HEADER_WORDS = 48;
|
|
20
|
+
const DIRTY_LEDGER_WORDS = 40;
|
|
21
|
+
export const MACHINE_ID_OFFSET = 32 + DIRTY_LEDGER_WORDS;
|
|
20
22
|
export const HEADER_WORDS = 32 + DIRTY_HEADER_WORDS;
|
|
21
23
|
export const WATCH_CAPACITY = 128;
|
|
22
24
|
export const WATCH_RECORD_WORDS = 288;
|