@waffle-os/runtime 0.0.156 → 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 +71 -0
- package/core/index.ts +4 -0
- package/core/open-file.ts +212 -18
- package/core/wasi.ts +397 -46
- 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 +13 -0
- package/dist/core/open-file.js +166 -13
- package/dist/core/wasi.js +353 -27
- package/dist/fs/backend.js +29 -2
- package/dist/fs/client.js +199 -6
- 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 +109 -1
- package/dist/fs/multitab-backend.js +315 -195
- package/dist/fs/multitab-protocol.js +1 -0
- package/dist/fs/persistent.js +13 -6
- package/dist/fs/protocol.js +9 -3
- package/dist/fs/service.js +386 -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 +33 -1
- package/fs/client.ts +225 -6
- package/fs/index.ts +22 -1
- package/fs/indexeddb.ts +36 -2
- package/fs/locks.ts +1406 -0
- package/fs/memory.ts +114 -1
- package/fs/multitab-backend.ts +561 -303
- package/fs/multitab-protocol.ts +1 -0
- package/fs/persistent.ts +14 -3
- package/fs/protocol.ts +17 -5
- package/fs/service.ts +468 -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/persistent.js
CHANGED
|
@@ -57,8 +57,8 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
57
57
|
nonce = crypto.randomUUID();
|
|
58
58
|
renewing;
|
|
59
59
|
coldReads = new ColdReadCache;
|
|
60
|
-
constructor(storage, fs, reservation, index, intervalMs = 50, checkpointLimits, dirtyLimits = {}, startupRoot, sharedRoot) {
|
|
61
|
-
super(fs, reservation, startupRoot, undefined, sharedRoot);
|
|
60
|
+
constructor(storage, fs, reservation, index, intervalMs = 50, checkpointLimits, dirtyLimits = {}, startupRoot, sharedRoot, machineId) {
|
|
61
|
+
super(fs, reservation, startupRoot, undefined, sharedRoot, machineId);
|
|
62
62
|
this.storage = storage;
|
|
63
63
|
this.dirtyLimits = dirtyLimits;
|
|
64
64
|
this.initialWorkspaceId = reservation.workspace;
|
|
@@ -139,7 +139,10 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
139
139
|
return this.storage.terminalFailureReason?.();
|
|
140
140
|
}
|
|
141
141
|
setTerminalFailureListener(listener) {
|
|
142
|
-
this.storage.setTerminalFailureListener?.(
|
|
142
|
+
this.storage.setTerminalFailureListener?.((reason) => {
|
|
143
|
+
this.cancelTimer();
|
|
144
|
+
listener(reason);
|
|
145
|
+
});
|
|
143
146
|
}
|
|
144
147
|
hasPendingChanges() {
|
|
145
148
|
if (this.dirty.size > 0 || this.removed.size > 0 || this.shared.publication)
|
|
@@ -149,7 +152,11 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
149
152
|
startTimer(intervalMs) {
|
|
150
153
|
if (intervalMs > 0)
|
|
151
154
|
this.timer = setInterval(() => {
|
|
152
|
-
if (
|
|
155
|
+
if (this.stopped || this.isTerminallyFailed()) {
|
|
156
|
+
this.cancelTimer();
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (!this.pendingCheckpoints && this.hasPendingChanges())
|
|
153
160
|
this.checkpoint().catch(() => {});
|
|
154
161
|
}, intervalMs);
|
|
155
162
|
}
|
|
@@ -163,11 +170,11 @@ export class PersistentBackend extends MemoryBackend {
|
|
|
163
170
|
options.signal?.throwIfAborted();
|
|
164
171
|
validateReservation(index, reservation);
|
|
165
172
|
if (!index)
|
|
166
|
-
return new PersistentBackend(storage, new MemFS, reservation, undefined, options.intervalMs, options.checkpointLimits, options.dirtyLimits, options.startupRoot, options.sharedRoot);
|
|
173
|
+
return new PersistentBackend(storage, new MemFS, reservation, undefined, options.intervalMs, options.checkpointLimits, options.dirtyLimits, options.startupRoot, options.sharedRoot, options.machineId);
|
|
167
174
|
const fs = restoreTree(index);
|
|
168
175
|
const root = fs.root;
|
|
169
176
|
adoptInodes(root);
|
|
170
|
-
const backend = new PersistentBackend(storage, fs, reservation, index, 0, options.checkpointLimits, options.dirtyLimits, options.startupRoot, options.sharedRoot);
|
|
177
|
+
const backend = new PersistentBackend(storage, fs, reservation, index, 0, options.checkpointLimits, options.dirtyLimits, options.startupRoot, options.sharedRoot, options.machineId);
|
|
171
178
|
try {
|
|
172
179
|
if (!index.nodes[0]?.identity) {
|
|
173
180
|
await storage.flush();
|
package/dist/fs/protocol.js
CHANGED
|
@@ -170,23 +170,29 @@ function identityPublication(parent, publication, owner, construction) {
|
|
|
170
170
|
}
|
|
171
171
|
return domain;
|
|
172
172
|
}
|
|
173
|
-
export function createProcessFilesystemIdentity(parent) {
|
|
173
|
+
export function createProcessFilesystemIdentity(parent, posixOwner) {
|
|
174
174
|
const identity = createFilesystemIdentity(parent);
|
|
175
175
|
identity.revocations = identity.revocations.slice(0, 1);
|
|
176
|
+
if (posixOwner !== undefined)
|
|
177
|
+
identity.posixOwner = posixOwner;
|
|
176
178
|
return identity;
|
|
177
179
|
}
|
|
178
180
|
export function releaseFilesystemReservation(reservation) {
|
|
179
181
|
const state = new Int32Array(reservation.state);
|
|
180
182
|
if (Atomics.exchange(state, 0, 1))
|
|
181
|
-
return;
|
|
183
|
+
return false;
|
|
182
184
|
const target = new Int32ArrayConstructor(reservation.target, 0, CAPABILITY_LENGTH);
|
|
183
185
|
for (;; ) {
|
|
184
186
|
const count = Atomics.load(target, reservation.targetKey + 1);
|
|
185
187
|
if (count <= 0 || Atomics.compareExchange(target, reservation.targetKey + 1, count, count - 1) === count)
|
|
186
188
|
break;
|
|
187
189
|
}
|
|
188
|
-
if (Atomics.compareExchange(state, 1, 0, 2) === 0)
|
|
190
|
+
if (Atomics.compareExchange(state, 1, 0, 2) === 0) {
|
|
191
|
+
if (reservation.ofd)
|
|
192
|
+
Atomics.sub(new Int32ArrayConstructor(reservation.ofd), 6, 1);
|
|
189
193
|
Atomics.sub(new Int32ArrayConstructor(reservation.source, 0, CAPABILITY_LENGTH), reservation.sourceKey + 2, 1);
|
|
194
|
+
}
|
|
195
|
+
return true;
|
|
190
196
|
}
|
|
191
197
|
export function reserveIdentityCapability(source, handle, target) {
|
|
192
198
|
const section = new FilesystemSection(source);
|
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);
|
|
@@ -777,7 +833,12 @@ export class FilesystemService {
|
|
|
777
833
|
"readdirPage",
|
|
778
834
|
"resolve",
|
|
779
835
|
"realpath",
|
|
780
|
-
"probe"
|
|
836
|
+
"probe",
|
|
837
|
+
"lockFlock",
|
|
838
|
+
"lockRange",
|
|
839
|
+
"lockGetRange",
|
|
840
|
+
"lockReleaseOfd",
|
|
841
|
+
"lockReleasePosix"
|
|
781
842
|
].includes(operation))
|
|
782
843
|
this.check(session, a, held);
|
|
783
844
|
if (["create", "symlink", "link", "removePath", "rename"].includes(operation)) {
|
|
@@ -949,6 +1010,274 @@ export class FilesystemService {
|
|
|
949
1010
|
};
|
|
950
1011
|
return backend.shared.ownsServiceMutation ? execute() : backend.shared.mutate(undefined, () => sessionRevoked(session), execute);
|
|
951
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
|
+
}
|
|
952
1281
|
settleWalk(session, id, success) {
|
|
953
1282
|
const transaction = session.walkTransactions.get(id);
|
|
954
1283
|
if (!transaction)
|
|
@@ -984,6 +1313,8 @@ export class FilesystemService {
|
|
|
984
1313
|
return this.walkRequest(session, id, operation, args);
|
|
985
1314
|
if (operation.startsWith("watch"))
|
|
986
1315
|
return this.watchRequest(session, id, operation, args);
|
|
1316
|
+
if (operation.startsWith("lock"))
|
|
1317
|
+
return this.lockRequest(session, id, operation, args);
|
|
987
1318
|
if (operation === "stageWrite")
|
|
988
1319
|
return this.stageWrite(session, id, a, b, c, data);
|
|
989
1320
|
if (operation === "take")
|
|
@@ -1147,9 +1478,10 @@ export class FilesystemService {
|
|
|
1147
1478
|
let committedWrite = false;
|
|
1148
1479
|
let admission;
|
|
1149
1480
|
let dirtyRetry;
|
|
1481
|
+
let lockRetry;
|
|
1150
1482
|
const epoch = session.epoch;
|
|
1151
1483
|
try {
|
|
1152
|
-
if (
|
|
1484
|
+
if (this.requestInactive(session, buffer))
|
|
1153
1485
|
throw new FilesystemError(11);
|
|
1154
1486
|
const { inputLength, dataLength } = requestLengths(buffer, bytes);
|
|
1155
1487
|
const input = decoded ?? unpack(bytes.subarray(0, inputLength));
|
|
@@ -1191,14 +1523,15 @@ export class FilesystemService {
|
|
|
1191
1523
|
} catch (error) {
|
|
1192
1524
|
this.discardResponse(session, id);
|
|
1193
1525
|
const dirtyChange = this.dirtyRetryChange(error);
|
|
1194
|
-
|
|
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;
|
|
1195
1531
|
dirtyRetry = dirtyChange;
|
|
1196
|
-
prepared ??= { handles: [] };
|
|
1197
|
-
const input = unpack(bytes.subarray(0, cells[1]));
|
|
1198
|
-
this.checkRequest(session, input.operation, input.args, prepared.handles);
|
|
1199
1532
|
preparing = true;
|
|
1200
1533
|
} else {
|
|
1201
|
-
cells[3] = errno(error);
|
|
1534
|
+
cells[3] = errno(retry ? retry.error : error);
|
|
1202
1535
|
state = cells[3] === 11 ? State.Cancelled : State.Failed;
|
|
1203
1536
|
}
|
|
1204
1537
|
} finally {
|
|
@@ -1215,6 +1548,7 @@ export class FilesystemService {
|
|
|
1215
1548
|
state = this.settleCancelledState(session, id, buffer, state, staging, committedWrite);
|
|
1216
1549
|
this.settleWalk(session, id, state === State.Complete);
|
|
1217
1550
|
this.settleWatch(session, id, state === State.Complete || state === State.Overflow);
|
|
1551
|
+
this.settleLock(session, id, state === State.Complete);
|
|
1218
1552
|
Atomics.store(cells, 0, state);
|
|
1219
1553
|
Atomics.notify(cells, 0);
|
|
1220
1554
|
wake(session.connection);
|
|
@@ -1223,15 +1557,13 @@ export class FilesystemService {
|
|
|
1223
1557
|
session.privateInputs.delete(id);
|
|
1224
1558
|
this.retainRequestReply(session, id, buffer);
|
|
1225
1559
|
session.connection.port.postMessage({ completed: id });
|
|
1226
|
-
this.
|
|
1560
|
+
this.sweepSessions(false);
|
|
1227
1561
|
} else {
|
|
1228
1562
|
session.inFlightWrites.delete(id);
|
|
1229
1563
|
}
|
|
1230
1564
|
}
|
|
1231
|
-
if (
|
|
1232
|
-
|
|
1233
|
-
this.joinPreparation(session, id, buffer, prepared, wait);
|
|
1234
|
-
}
|
|
1565
|
+
if (prepared)
|
|
1566
|
+
this.scheduleRetry(session, id, buffer, prepared, lockRetry, dirtyRetry);
|
|
1235
1567
|
}
|
|
1236
1568
|
settleCancelledState(session, id, buffer, state, staging, committedWrite) {
|
|
1237
1569
|
if (sessionRevoked(session) || requestCancelled(buffer)) {
|
|
@@ -1268,7 +1600,7 @@ export class FilesystemService {
|
|
|
1268
1600
|
});
|
|
1269
1601
|
}
|
|
1270
1602
|
retainRequestReply(session, id, buffer) {
|
|
1271
|
-
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)) {
|
|
1272
1604
|
session.retainedRequests.set(id, buffer);
|
|
1273
1605
|
return;
|
|
1274
1606
|
}
|
|
@@ -1544,6 +1876,9 @@ export class FilesystemService {
|
|
|
1544
1876
|
return { ...pin, bytes: Array.from(pin.bytes) };
|
|
1545
1877
|
}
|
|
1546
1878
|
sweep() {
|
|
1879
|
+
this.sweepSessions(true);
|
|
1880
|
+
}
|
|
1881
|
+
sweepSessions(reapOfds) {
|
|
1547
1882
|
if (this.shutdownOwner)
|
|
1548
1883
|
return;
|
|
1549
1884
|
for (const session of this.sessions)
|
|
@@ -1556,6 +1891,10 @@ export class FilesystemService {
|
|
|
1556
1891
|
for (const session of this.sessions)
|
|
1557
1892
|
if (sessionRevoked(session))
|
|
1558
1893
|
this.close(session);
|
|
1894
|
+
if (reapOfds) {
|
|
1895
|
+
this.reapPendingPosixSuccessors();
|
|
1896
|
+
this.lockTable?.reapOfds(this.ofdLiveness());
|
|
1897
|
+
}
|
|
1559
1898
|
} finally {
|
|
1560
1899
|
this.sweeping = false;
|
|
1561
1900
|
}
|
|
@@ -1632,6 +1971,28 @@ export class FilesystemService {
|
|
|
1632
1971
|
this.cancelSessionWatchResponses(session);
|
|
1633
1972
|
this.watchService?.closeOwner(session.connection.owner);
|
|
1634
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));
|
|
1635
1996
|
session.namespace?.close();
|
|
1636
1997
|
this.stopSessionReader(session);
|
|
1637
1998
|
if (!session.transportFailed)
|
|
@@ -1678,6 +2039,7 @@ export class FilesystemService {
|
|
|
1678
2039
|
if (publication && !this.retirePublication(session))
|
|
1679
2040
|
return;
|
|
1680
2041
|
this.sessions.delete(session);
|
|
2042
|
+
this.sessionsByOwner.delete(session.connection.owner);
|
|
1681
2043
|
}
|
|
1682
2044
|
this.closeDisposedWatchAccount();
|
|
1683
2045
|
if (!this.sessions.size && this.backend instanceof MemoryBackend && this.backend.shared.publication?.stopped)
|
|
@@ -1778,6 +2140,7 @@ export class FilesystemService {
|
|
|
1778
2140
|
if (this.shutdownOwner)
|
|
1779
2141
|
return;
|
|
1780
2142
|
this.disposingWatches = true;
|
|
2143
|
+
this.lockTable?.dispose();
|
|
1781
2144
|
this.residentPublication?.dispose();
|
|
1782
2145
|
for (const session of this.sessions)
|
|
1783
2146
|
this.close(session);
|
|
@@ -1796,6 +2159,7 @@ export class FilesystemService {
|
|
|
1796
2159
|
const owner = { done, sessions };
|
|
1797
2160
|
this.shutdownOwner = owner;
|
|
1798
2161
|
this.ownerStopping = true;
|
|
2162
|
+
this.lockTable?.dispose();
|
|
1799
2163
|
this.backend.beginShutdown?.();
|
|
1800
2164
|
for (const session of sessions)
|
|
1801
2165
|
this.stopSessionReader(session);
|