@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/locks.js
ADDED
|
@@ -0,0 +1,1112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
F_RDLCK,
|
|
3
|
+
F_UNLCK,
|
|
4
|
+
F_WRLCK,
|
|
5
|
+
LOCK_EX,
|
|
6
|
+
LOCK_NB,
|
|
7
|
+
LOCK_SH,
|
|
8
|
+
LOCK_UN
|
|
9
|
+
} from "../core/filesystem.js";
|
|
10
|
+
const EACCES = 2;
|
|
11
|
+
const EAGAIN = 6;
|
|
12
|
+
const EDEADLK = 16;
|
|
13
|
+
const EINVAL = 28;
|
|
14
|
+
const ENOLCK = 46;
|
|
15
|
+
const OFD_FLAG_ORIGIN_CLOSED = 2;
|
|
16
|
+
const DEFAULT_MAX_RANGE_LOCKS = 4096;
|
|
17
|
+
const DEFAULT_MAX_FLOCKS = 4096;
|
|
18
|
+
const DEFAULT_MAX_WAITERS = 512;
|
|
19
|
+
const DEFAULT_MAX_PENDING_TRANSACTIONS = 256;
|
|
20
|
+
|
|
21
|
+
export class LockContentionError extends Error {
|
|
22
|
+
wait;
|
|
23
|
+
cancel;
|
|
24
|
+
constructor(wait, cancel) {
|
|
25
|
+
super("advisory lock contended");
|
|
26
|
+
this.wait = wait;
|
|
27
|
+
this.cancel = cancel;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function normalizeFlockOp(operation) {
|
|
31
|
+
if (!Number.isInteger(operation) || operation < 0)
|
|
32
|
+
return null;
|
|
33
|
+
if ((operation & ~(LOCK_SH | LOCK_EX | LOCK_NB | LOCK_UN)) !== 0)
|
|
34
|
+
return null;
|
|
35
|
+
const nonblocking = (operation & LOCK_NB) !== 0;
|
|
36
|
+
const base = operation & ~LOCK_NB;
|
|
37
|
+
if (base !== LOCK_SH && base !== LOCK_EX && base !== LOCK_UN)
|
|
38
|
+
return null;
|
|
39
|
+
return { base, nonblocking };
|
|
40
|
+
}
|
|
41
|
+
function parseOfdId(ofdId) {
|
|
42
|
+
const colon = ofdId.indexOf(":");
|
|
43
|
+
if (colon <= 0 || colon === ofdId.length - 1)
|
|
44
|
+
return null;
|
|
45
|
+
const ownerStr = ofdId.slice(0, colon);
|
|
46
|
+
const seqStr = ofdId.slice(colon + 1);
|
|
47
|
+
if (!/^(?:0|[1-9][0-9]*)$/.test(ownerStr) || !/^[1-9][0-9]*$/.test(seqStr))
|
|
48
|
+
return null;
|
|
49
|
+
const owner = Number(ownerStr);
|
|
50
|
+
const seq = Number(seqStr);
|
|
51
|
+
if (!Number.isSafeInteger(owner) || !Number.isSafeInteger(seq))
|
|
52
|
+
return null;
|
|
53
|
+
return { owner, seq };
|
|
54
|
+
}
|
|
55
|
+
function rangeEndOf(start, len) {
|
|
56
|
+
if (!Number.isSafeInteger(start) || start < 0)
|
|
57
|
+
return -1;
|
|
58
|
+
if (!Number.isSafeInteger(len) || len < 0)
|
|
59
|
+
return -1;
|
|
60
|
+
if (len === 0)
|
|
61
|
+
return 1 / 0;
|
|
62
|
+
const end = start + len;
|
|
63
|
+
return Number.isSafeInteger(end) ? end : -1;
|
|
64
|
+
}
|
|
65
|
+
function rangesOverlap(aStart, aEnd, bStart, bEnd) {
|
|
66
|
+
return aStart < bEnd && bStart < aEnd;
|
|
67
|
+
}
|
|
68
|
+
function rangesConflict(aType, bType) {
|
|
69
|
+
return aType === F_WRLCK || bType === F_WRLCK;
|
|
70
|
+
}
|
|
71
|
+
function flockConflicts(existing, requested) {
|
|
72
|
+
return existing === "ex" || requested === "ex";
|
|
73
|
+
}
|
|
74
|
+
function posixOwnerKey(posixOwner) {
|
|
75
|
+
return `p:${posixOwner}`;
|
|
76
|
+
}
|
|
77
|
+
function ofdOwnerKey(ofdId) {
|
|
78
|
+
return `o:${ofdId}`;
|
|
79
|
+
}
|
|
80
|
+
function cloneRange(lock) {
|
|
81
|
+
return {
|
|
82
|
+
owner: lock.owner,
|
|
83
|
+
posixOwner: lock.posixOwner,
|
|
84
|
+
ofdId: lock.ofdId,
|
|
85
|
+
pid: lock.pid,
|
|
86
|
+
type: lock.type,
|
|
87
|
+
start: lock.start,
|
|
88
|
+
end: lock.end,
|
|
89
|
+
opId: lock.opId
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function coalesceOwned(owned) {
|
|
93
|
+
const sorted = owned.toSorted((left, right) => left.start - right.start);
|
|
94
|
+
const merged = [];
|
|
95
|
+
for (let i = 0;i < sorted.length; i++) {
|
|
96
|
+
const current = sorted[i];
|
|
97
|
+
const prev = merged[merged.length - 1];
|
|
98
|
+
if (prev && prev.type === current.type && prev.opId === current.opId && prev.end >= current.start) {
|
|
99
|
+
prev.end = Math.max(prev.end, current.end);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
merged.push(cloneRange(current));
|
|
103
|
+
}
|
|
104
|
+
return merged;
|
|
105
|
+
}
|
|
106
|
+
function computeRangeDelta(previousOwned, start, end) {
|
|
107
|
+
const sorted = previousOwned.toSorted((left, right) => left.start - right.start);
|
|
108
|
+
const delta = [];
|
|
109
|
+
let cursor = start;
|
|
110
|
+
for (let i = 0;i < sorted.length; i++) {
|
|
111
|
+
const entry = sorted[i];
|
|
112
|
+
if (entry.end <= cursor)
|
|
113
|
+
continue;
|
|
114
|
+
if (entry.start >= end)
|
|
115
|
+
break;
|
|
116
|
+
const ovStart = Math.max(entry.start, cursor);
|
|
117
|
+
const ovEnd = Math.min(entry.end, end);
|
|
118
|
+
if (ovStart > cursor)
|
|
119
|
+
delta.push({ start: cursor, end: ovStart, prevType: F_UNLCK, prevOpId: 0 });
|
|
120
|
+
delta.push({ start: ovStart, end: ovEnd, prevType: entry.type, prevOpId: entry.opId });
|
|
121
|
+
cursor = ovEnd;
|
|
122
|
+
}
|
|
123
|
+
if (cursor < end)
|
|
124
|
+
delta.push({ start: cursor, end, prevType: F_UNLCK, prevOpId: 0 });
|
|
125
|
+
return delta;
|
|
126
|
+
}
|
|
127
|
+
function spliceDeltaSegment(seg, cancelledDelta, out) {
|
|
128
|
+
let cursor = seg.start;
|
|
129
|
+
for (let i = 0;i < cancelledDelta.length; i++) {
|
|
130
|
+
const piece = cancelledDelta[i];
|
|
131
|
+
if (piece.end <= cursor)
|
|
132
|
+
continue;
|
|
133
|
+
if (piece.start >= seg.end)
|
|
134
|
+
break;
|
|
135
|
+
const ovStart = Math.max(cursor, piece.start);
|
|
136
|
+
const ovEnd = Math.min(seg.end, piece.end);
|
|
137
|
+
if (ovStart > cursor)
|
|
138
|
+
out.push({ start: cursor, end: ovStart, prevType: F_UNLCK, prevOpId: 0 });
|
|
139
|
+
out.push({ start: ovStart, end: ovEnd, prevType: piece.prevType, prevOpId: piece.prevOpId });
|
|
140
|
+
cursor = ovEnd;
|
|
141
|
+
}
|
|
142
|
+
if (cursor < seg.end)
|
|
143
|
+
out.push({ start: cursor, end: seg.end, prevType: F_UNLCK, prevOpId: 0 });
|
|
144
|
+
}
|
|
145
|
+
function addInterval(map, opId, start, end) {
|
|
146
|
+
let list = map.get(opId);
|
|
147
|
+
if (!list) {
|
|
148
|
+
list = [];
|
|
149
|
+
map.set(opId, list);
|
|
150
|
+
}
|
|
151
|
+
list.push({ start, end });
|
|
152
|
+
}
|
|
153
|
+
function intersectDeltaWithIntervals(delta, intervals) {
|
|
154
|
+
const spans = intervals.toSorted((left, right) => left.start - right.start);
|
|
155
|
+
const trimmed = [];
|
|
156
|
+
let reserve = 0;
|
|
157
|
+
for (let i = 0;i < spans.length; i++) {
|
|
158
|
+
const span = spans[i];
|
|
159
|
+
let nonUnlockInSpan = 0;
|
|
160
|
+
for (let j = 0;j < delta.length; j++) {
|
|
161
|
+
const piece = delta[j];
|
|
162
|
+
if (piece.end <= span.start)
|
|
163
|
+
continue;
|
|
164
|
+
if (piece.start >= span.end)
|
|
165
|
+
break;
|
|
166
|
+
if (piece.prevType !== F_UNLCK)
|
|
167
|
+
nonUnlockInSpan += 1;
|
|
168
|
+
trimmed.push({
|
|
169
|
+
start: Math.max(span.start, piece.start),
|
|
170
|
+
end: Math.min(span.end, piece.end),
|
|
171
|
+
prevType: piece.prevType,
|
|
172
|
+
prevOpId: piece.prevOpId
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
if (nonUnlockInSpan > 1)
|
|
176
|
+
reserve += nonUnlockInSpan - 1;
|
|
177
|
+
}
|
|
178
|
+
return { trimmed, reserve };
|
|
179
|
+
}
|
|
180
|
+
function rollbackReserveForDelta(delta) {
|
|
181
|
+
let nonUnlock = 0;
|
|
182
|
+
for (let i = 0;i < delta.length; i++) {
|
|
183
|
+
if (delta[i].prevType !== F_UNLCK)
|
|
184
|
+
nonUnlock += 1;
|
|
185
|
+
}
|
|
186
|
+
return nonUnlock > 1 ? nonUnlock - 1 : 0;
|
|
187
|
+
}
|
|
188
|
+
function spliceRangeDelta(targetDelta, cancelledOpId, cancelledDelta) {
|
|
189
|
+
const out = [];
|
|
190
|
+
for (let i = 0;i < targetDelta.length; i++) {
|
|
191
|
+
const seg = targetDelta[i];
|
|
192
|
+
if (seg.prevOpId !== cancelledOpId) {
|
|
193
|
+
out.push(seg);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
spliceDeltaSegment(seg, cancelledDelta, out);
|
|
197
|
+
}
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
function editOwnerRanges(ranges, owner, posixOwner, ofdId, pid, type, start, end, opId) {
|
|
201
|
+
const others = [];
|
|
202
|
+
const owned = [];
|
|
203
|
+
const previousOwned = [];
|
|
204
|
+
let relaxed = false;
|
|
205
|
+
for (let i = 0;i < ranges.length; i++) {
|
|
206
|
+
const entry = ranges[i];
|
|
207
|
+
if (entry.owner !== owner) {
|
|
208
|
+
others.push(entry);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
previousOwned.push(entry);
|
|
212
|
+
if (!rangesOverlap(entry.start, entry.end, start, end)) {
|
|
213
|
+
owned.push(entry);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (type === F_UNLCK || entry.type === F_WRLCK && type === F_RDLCK)
|
|
217
|
+
relaxed = true;
|
|
218
|
+
if (entry.start < start)
|
|
219
|
+
owned.push({ ...entry, end: start });
|
|
220
|
+
if (entry.end > end)
|
|
221
|
+
owned.push({ ...entry, start: end });
|
|
222
|
+
}
|
|
223
|
+
const delta = type === F_UNLCK ? [] : computeRangeDelta(previousOwned, start, end);
|
|
224
|
+
if (type !== F_UNLCK)
|
|
225
|
+
owned.push({ owner, posixOwner, ofdId, pid, type, start, end, opId });
|
|
226
|
+
const coalesced = coalesceOwned(owned);
|
|
227
|
+
return {
|
|
228
|
+
next: others.concat(coalesced),
|
|
229
|
+
delta,
|
|
230
|
+
relaxed,
|
|
231
|
+
previousCount: previousOwned.length,
|
|
232
|
+
nextCount: coalesced.length
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export class LockTable {
|
|
237
|
+
byIno = new Map;
|
|
238
|
+
posixInos = new Map;
|
|
239
|
+
ofdRecords = new Map;
|
|
240
|
+
ownerOfds = new Map;
|
|
241
|
+
waitingOn = new Map;
|
|
242
|
+
deadlockWaiters = new Map;
|
|
243
|
+
waitersBySession = new Map;
|
|
244
|
+
pendingOps = new Set;
|
|
245
|
+
flockOpPrev = new Map;
|
|
246
|
+
rangeOpDeltas = new Map;
|
|
247
|
+
maxRangeLocks;
|
|
248
|
+
maxFlocks;
|
|
249
|
+
maxWaiters;
|
|
250
|
+
maxPendingTransactions;
|
|
251
|
+
onPosixLockCountChange;
|
|
252
|
+
totalRangeLocks = 0;
|
|
253
|
+
reservedRangeRollbackSlots = 0;
|
|
254
|
+
totalPosixRangeLocks = 0;
|
|
255
|
+
totalFlocks = 0;
|
|
256
|
+
totalWaiters = 0;
|
|
257
|
+
nextWaiterId = 1;
|
|
258
|
+
nextOpId = 1;
|
|
259
|
+
constructor(options = {}) {
|
|
260
|
+
this.maxRangeLocks = options.maxRangeLocks ?? DEFAULT_MAX_RANGE_LOCKS;
|
|
261
|
+
this.maxFlocks = options.maxFlocks ?? DEFAULT_MAX_FLOCKS;
|
|
262
|
+
this.maxWaiters = options.maxWaiters ?? DEFAULT_MAX_WAITERS;
|
|
263
|
+
this.maxPendingTransactions = options.maxPendingTransactions ?? DEFAULT_MAX_PENDING_TRANSACTIONS;
|
|
264
|
+
this.onPosixLockCountChange = options.onPosixLockCountChange;
|
|
265
|
+
}
|
|
266
|
+
hasActivePosixLocks() {
|
|
267
|
+
return this.totalPosixRangeLocks > 0;
|
|
268
|
+
}
|
|
269
|
+
diagnostics() {
|
|
270
|
+
return {
|
|
271
|
+
inodes: this.byIno.size,
|
|
272
|
+
flocks: this.totalFlocks,
|
|
273
|
+
ranges: this.totalRangeLocks,
|
|
274
|
+
posixRanges: this.totalPosixRangeLocks,
|
|
275
|
+
waiters: this.totalWaiters,
|
|
276
|
+
ofdRecords: this.ofdRecords.size,
|
|
277
|
+
pendingTransactions: this.pendingOps.size
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
adoptReservation(ino, handle, ofdId, originOwner, ofdBuffer, targetOwner, reservationState) {
|
|
281
|
+
const existing = this.ofdRecords.get(ofdId);
|
|
282
|
+
if (existing && existing.ino !== ino)
|
|
283
|
+
return;
|
|
284
|
+
const record = this.ensureOfdRecord(ino, handle, ofdId, originOwner);
|
|
285
|
+
record.cells ??= new Int32Array(ofdBuffer);
|
|
286
|
+
this.indexOwnerOfd(originOwner, ofdId);
|
|
287
|
+
const state = new Int32Array(reservationState);
|
|
288
|
+
if (Atomics.load(state, 0) === 0) {
|
|
289
|
+
record.reservations.push({ targetOwner, state });
|
|
290
|
+
this.indexOwnerOfd(targetOwner, ofdId);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
flock(ino, ofdId, sessionOwner, operation, allowWait = false, handle = 0, trackTransaction = allowWait) {
|
|
294
|
+
const parsed = normalizeFlockOp(operation);
|
|
295
|
+
if (!parsed)
|
|
296
|
+
return { errno: EINVAL };
|
|
297
|
+
if (!this.isAuthorizedOfd(sessionOwner, ofdId, ino))
|
|
298
|
+
return { errno: EACCES };
|
|
299
|
+
if (parsed.base === LOCK_UN) {
|
|
300
|
+
this.removeFlock(ino, ofdId);
|
|
301
|
+
return { errno: 0 };
|
|
302
|
+
}
|
|
303
|
+
const mode = parsed.base === LOCK_EX ? "ex" : "sh";
|
|
304
|
+
const prior = this.byIno.get(ino)?.flocks.get(ofdId);
|
|
305
|
+
const previousMode = prior?.mode;
|
|
306
|
+
const previousOpId = prior?.opId ?? 0;
|
|
307
|
+
const converting = prior !== undefined && prior.mode !== mode;
|
|
308
|
+
if (converting)
|
|
309
|
+
this.detachFlockEntry(ino, ofdId);
|
|
310
|
+
const detachedOpId = converting ? previousOpId : 0;
|
|
311
|
+
const inode = this.byIno.get(ino);
|
|
312
|
+
const existing = inode?.flocks.get(ofdId);
|
|
313
|
+
const blockers = inode ? this.flockBlockers(inode, ofdId, mode) : [];
|
|
314
|
+
if (blockers.length > 0) {
|
|
315
|
+
this.discardFlockChain(detachedOpId);
|
|
316
|
+
if (parsed.nonblocking || !allowWait)
|
|
317
|
+
return { errno: EAGAIN };
|
|
318
|
+
if (this.totalWaiters >= this.maxWaiters)
|
|
319
|
+
return { errno: ENOLCK };
|
|
320
|
+
throw this.enqueueWaiter(ino, ofdOwnerKey(ofdId), sessionOwner, blockers, false);
|
|
321
|
+
}
|
|
322
|
+
if (!existing && this.totalFlocks >= this.maxFlocks || trackTransaction && this.pendingOps.size >= this.maxPendingTransactions) {
|
|
323
|
+
this.discardFlockChain(detachedOpId);
|
|
324
|
+
return { errno: ENOLCK };
|
|
325
|
+
}
|
|
326
|
+
const originOwner = parseOfdId(ofdId)?.owner ?? sessionOwner;
|
|
327
|
+
this.ensureOfdRecord(ino, handle, ofdId, originOwner);
|
|
328
|
+
this.indexOwnerOfd(sessionOwner, ofdId);
|
|
329
|
+
return this.installFlock(ino, ofdId, mode, existing, previousMode, previousOpId, trackTransaction);
|
|
330
|
+
}
|
|
331
|
+
releaseOfd(ino, ofdId, sessionOwner, liveness, inFlight = false) {
|
|
332
|
+
if (sessionOwner !== undefined && !this.isAuthorizedOfd(sessionOwner, ofdId, ino))
|
|
333
|
+
return;
|
|
334
|
+
const record = this.ofdRecords.get(ofdId);
|
|
335
|
+
if (!record || !liveness) {
|
|
336
|
+
this.dropOfdLocks(ino, ofdId);
|
|
337
|
+
if (record)
|
|
338
|
+
this.deleteOfdRecord(record);
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (sessionOwner !== undefined && sessionOwner === record.originOwner) {
|
|
342
|
+
record.originClosed = true;
|
|
343
|
+
record.pendingOriginTransfer = inFlight;
|
|
344
|
+
}
|
|
345
|
+
if (!this.isOfdAlive(record, liveness)) {
|
|
346
|
+
this.dropOfdLocks(record.ino, ofdId);
|
|
347
|
+
this.deleteOfdRecord(record);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
reapOfds(liveness, ofdIds) {
|
|
351
|
+
const candidates = ofdIds ? Array.from(ofdIds) : Array.from(this.ofdRecords.keys());
|
|
352
|
+
for (let i = 0;i < candidates.length; i++) {
|
|
353
|
+
const ofdId = candidates[i];
|
|
354
|
+
const record = this.ofdRecords.get(ofdId);
|
|
355
|
+
if (!record || this.isOfdAlive(record, liveness))
|
|
356
|
+
continue;
|
|
357
|
+
this.dropOfdLocks(record.ino, ofdId);
|
|
358
|
+
this.deleteOfdRecord(record);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
setRangeLock(ino, ofdId, sessionOwner, pid, range, blocking, allowWait = false, handle = 0, posixOwner = sessionOwner, trackTransaction = allowWait) {
|
|
362
|
+
if (range.type !== F_RDLCK && range.type !== F_WRLCK && range.type !== F_UNLCK)
|
|
363
|
+
return { errno: EINVAL };
|
|
364
|
+
const end = rangeEndOf(range.start, range.len);
|
|
365
|
+
if (end < 0)
|
|
366
|
+
return { errno: EINVAL };
|
|
367
|
+
if (ofdId !== undefined && !this.isAuthorizedOfd(sessionOwner, ofdId, ino))
|
|
368
|
+
return { errno: EACCES };
|
|
369
|
+
const effectivePosixOwner = ofdId === undefined ? posixOwner : undefined;
|
|
370
|
+
const owner = ofdId === undefined ? posixOwnerKey(posixOwner) : ofdOwnerKey(ofdId);
|
|
371
|
+
const inode = this.byIno.get(ino);
|
|
372
|
+
if (range.type === F_UNLCK)
|
|
373
|
+
return this.unlockRange(ino, inode, owner, effectivePosixOwner, ofdId, pid, range.start, end);
|
|
374
|
+
const blockers = inode ? this.rangeBlockers(inode, owner, range.type, range.start, end) : [];
|
|
375
|
+
if (blockers.length > 0) {
|
|
376
|
+
if (!blocking || !allowWait)
|
|
377
|
+
return { errno: EAGAIN };
|
|
378
|
+
const trackDeadlock = ofdId === undefined;
|
|
379
|
+
if (trackDeadlock && this.wouldDeadlock(owner, blockers))
|
|
380
|
+
return { errno: EDEADLK };
|
|
381
|
+
if (this.totalWaiters >= this.maxWaiters)
|
|
382
|
+
return { errno: ENOLCK };
|
|
383
|
+
throw this.enqueueWaiter(ino, owner, sessionOwner, blockers, trackDeadlock);
|
|
384
|
+
}
|
|
385
|
+
return this.installRangeLock(ino, owner, sessionOwner, effectivePosixOwner, ofdId, pid, range.type, range.start, end, handle, trackTransaction);
|
|
386
|
+
}
|
|
387
|
+
getRangeLock(ino, ofdId, sessionOwner, range, posixOwner = sessionOwner) {
|
|
388
|
+
if (range.type !== F_RDLCK && range.type !== F_WRLCK)
|
|
389
|
+
return EINVAL;
|
|
390
|
+
const end = rangeEndOf(range.start, range.len);
|
|
391
|
+
if (end < 0)
|
|
392
|
+
return EINVAL;
|
|
393
|
+
if (ofdId !== undefined && !this.isAuthorizedOfd(sessionOwner, ofdId, ino))
|
|
394
|
+
return EACCES;
|
|
395
|
+
const clear = { type: F_UNLCK, pid: 0, start: 0, len: 0 };
|
|
396
|
+
const inode = this.byIno.get(ino);
|
|
397
|
+
if (!inode)
|
|
398
|
+
return clear;
|
|
399
|
+
const owner = ofdId === undefined ? posixOwnerKey(posixOwner) : ofdOwnerKey(ofdId);
|
|
400
|
+
for (let i = 0;i < inode.ranges.length; i++) {
|
|
401
|
+
const entry = inode.ranges[i];
|
|
402
|
+
if (entry.owner === owner)
|
|
403
|
+
continue;
|
|
404
|
+
if (!rangesConflict(entry.type, range.type))
|
|
405
|
+
continue;
|
|
406
|
+
if (!rangesOverlap(entry.start, entry.end, range.start, end))
|
|
407
|
+
continue;
|
|
408
|
+
return {
|
|
409
|
+
type: entry.type,
|
|
410
|
+
pid: entry.ofdId === undefined ? entry.pid : -1,
|
|
411
|
+
start: entry.start,
|
|
412
|
+
len: entry.end === 1 / 0 ? 0 : entry.end - entry.start
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
return clear;
|
|
416
|
+
}
|
|
417
|
+
releasePosix(ino, posixOwner) {
|
|
418
|
+
this.unindexPosixIno(posixOwner, ino);
|
|
419
|
+
const owner = posixOwnerKey(posixOwner);
|
|
420
|
+
this.discardOwnerRangeOps(ino, owner);
|
|
421
|
+
const inode = this.byIno.get(ino);
|
|
422
|
+
if (!inode)
|
|
423
|
+
return;
|
|
424
|
+
const before = inode.ranges.length;
|
|
425
|
+
inode.ranges = inode.ranges.filter((entry) => entry.owner !== owner);
|
|
426
|
+
const removed = before - inode.ranges.length;
|
|
427
|
+
if (removed > 0) {
|
|
428
|
+
this.adjustRangeCounts(-removed, -removed);
|
|
429
|
+
this.wakeInode(ino, inode);
|
|
430
|
+
}
|
|
431
|
+
this.pruneInode(ino, inode);
|
|
432
|
+
}
|
|
433
|
+
releasePosixOwner(posixOwner) {
|
|
434
|
+
const inos = this.posixInos.get(posixOwner);
|
|
435
|
+
if (!inos)
|
|
436
|
+
return;
|
|
437
|
+
this.posixInos.delete(posixOwner);
|
|
438
|
+
for (const ino of Array.from(inos))
|
|
439
|
+
this.releasePosix(ino, posixOwner);
|
|
440
|
+
}
|
|
441
|
+
releaseSession(sessionOwner, liveness, posixOwner = sessionOwner, releasePosix = true) {
|
|
442
|
+
const waiters = this.waitersBySession.get(sessionOwner);
|
|
443
|
+
if (waiters) {
|
|
444
|
+
for (const waiter of Array.from(waiters))
|
|
445
|
+
waiter.cancel();
|
|
446
|
+
this.waitersBySession.delete(sessionOwner);
|
|
447
|
+
}
|
|
448
|
+
if (releasePosix)
|
|
449
|
+
this.releasePosixOwner(posixOwner);
|
|
450
|
+
const ofds = this.ownerOfds.get(sessionOwner);
|
|
451
|
+
if (!ofds)
|
|
452
|
+
return;
|
|
453
|
+
const snapshot = Array.from(ofds);
|
|
454
|
+
this.ownerOfds.delete(sessionOwner);
|
|
455
|
+
const effectiveLiveness = liveness ?? {
|
|
456
|
+
isSessionLive: (owner) => owner !== sessionOwner,
|
|
457
|
+
hasPendingTransfer: () => false
|
|
458
|
+
};
|
|
459
|
+
this.reapOfds(effectiveLiveness, snapshot);
|
|
460
|
+
}
|
|
461
|
+
dispose() {
|
|
462
|
+
for (const inode of Array.from(this.byIno.values())) {
|
|
463
|
+
for (const waiter of inode.waiters.slice())
|
|
464
|
+
waiter.cancel();
|
|
465
|
+
}
|
|
466
|
+
this.byIno.clear();
|
|
467
|
+
this.posixInos.clear();
|
|
468
|
+
this.ofdRecords.clear();
|
|
469
|
+
this.ownerOfds.clear();
|
|
470
|
+
this.waitingOn.clear();
|
|
471
|
+
this.deadlockWaiters.clear();
|
|
472
|
+
this.waitersBySession.clear();
|
|
473
|
+
this.pendingOps.clear();
|
|
474
|
+
this.flockOpPrev.clear();
|
|
475
|
+
this.rangeOpDeltas.clear();
|
|
476
|
+
this.totalRangeLocks = 0;
|
|
477
|
+
this.reservedRangeRollbackSlots = 0;
|
|
478
|
+
this.totalFlocks = 0;
|
|
479
|
+
this.totalWaiters = 0;
|
|
480
|
+
this.setPosixRangeCount(0);
|
|
481
|
+
}
|
|
482
|
+
setPosixRangeCount(next) {
|
|
483
|
+
if (this.totalPosixRangeLocks === next)
|
|
484
|
+
return;
|
|
485
|
+
this.totalPosixRangeLocks = next;
|
|
486
|
+
this.onPosixLockCountChange?.(next);
|
|
487
|
+
}
|
|
488
|
+
adjustRangeCounts(totalDelta, posixDelta) {
|
|
489
|
+
this.totalRangeLocks += totalDelta;
|
|
490
|
+
if (posixDelta !== 0)
|
|
491
|
+
this.setPosixRangeCount(this.totalPosixRangeLocks + posixDelta);
|
|
492
|
+
}
|
|
493
|
+
isAuthorizedOfd(sessionOwner, ofdId, ino) {
|
|
494
|
+
const existing = this.ofdRecords.get(ofdId);
|
|
495
|
+
if (existing && existing.ino !== ino)
|
|
496
|
+
return false;
|
|
497
|
+
if (sessionOwner === 0)
|
|
498
|
+
return ofdId.length > 0;
|
|
499
|
+
const parsed = parseOfdId(ofdId);
|
|
500
|
+
if (!parsed)
|
|
501
|
+
return false;
|
|
502
|
+
if (parsed.owner === sessionOwner)
|
|
503
|
+
return true;
|
|
504
|
+
return this.ownerOfds.get(sessionOwner)?.has(ofdId) === true;
|
|
505
|
+
}
|
|
506
|
+
unlockRange(ino, inode, owner, posixOwner, ofdId, pid, start, end) {
|
|
507
|
+
if (!inode)
|
|
508
|
+
return { errno: 0 };
|
|
509
|
+
const edited = editOwnerRanges(inode.ranges, owner, posixOwner, ofdId, pid, F_UNLCK, start, end, 0);
|
|
510
|
+
const deltaCount = edited.nextCount - edited.previousCount;
|
|
511
|
+
if (deltaCount > 0 && this.totalRangeLocks + this.reservedRangeRollbackSlots + deltaCount > this.maxRangeLocks)
|
|
512
|
+
return { errno: ENOLCK };
|
|
513
|
+
inode.ranges = edited.next;
|
|
514
|
+
this.adjustRangeCounts(deltaCount, posixOwner !== undefined ? deltaCount : 0);
|
|
515
|
+
if (posixOwner !== undefined && edited.nextCount === 0)
|
|
516
|
+
this.unindexPosixIno(posixOwner, ino);
|
|
517
|
+
this.recomputeOwnerRangeReserves(ino, owner, inode);
|
|
518
|
+
if (edited.relaxed)
|
|
519
|
+
this.wakeInode(ino, inode);
|
|
520
|
+
this.pruneInode(ino, inode);
|
|
521
|
+
return { errno: 0 };
|
|
522
|
+
}
|
|
523
|
+
isOfdAlive(record, liveness) {
|
|
524
|
+
let writeIndex = 0;
|
|
525
|
+
let dropped;
|
|
526
|
+
for (let i = 0;i < record.reservations.length; i++) {
|
|
527
|
+
const entry = record.reservations[i];
|
|
528
|
+
if (Atomics.load(entry.state, 0) !== 0 || !liveness.isSessionLive(entry.targetOwner)) {
|
|
529
|
+
(dropped ??= []).push(entry.targetOwner);
|
|
530
|
+
continue;
|
|
531
|
+
}
|
|
532
|
+
record.reservations[writeIndex++] = entry;
|
|
533
|
+
}
|
|
534
|
+
record.reservations.length = writeIndex;
|
|
535
|
+
if (dropped)
|
|
536
|
+
this.pruneDroppedOwners(record, dropped);
|
|
537
|
+
if (writeIndex > 0)
|
|
538
|
+
return true;
|
|
539
|
+
const originClosed = record.originClosed || record.cells !== undefined && (Atomics.load(record.cells, 7) & OFD_FLAG_ORIGIN_CLOSED) !== 0;
|
|
540
|
+
if (!originClosed && liveness.isSessionLive(record.originOwner))
|
|
541
|
+
return true;
|
|
542
|
+
if (record.cells !== undefined)
|
|
543
|
+
return Atomics.load(record.cells, 6) > 0;
|
|
544
|
+
if (record.originClosed) {
|
|
545
|
+
return record.pendingOriginTransfer && liveness.hasPendingTransfer(record.originOwner, record.handle);
|
|
546
|
+
}
|
|
547
|
+
return liveness.hasPendingTransfer(record.originOwner, record.handle);
|
|
548
|
+
}
|
|
549
|
+
pruneDroppedOwners(record, dropped) {
|
|
550
|
+
for (let i = 0;i < dropped.length; i++) {
|
|
551
|
+
const owner = dropped[i];
|
|
552
|
+
if (owner === record.originOwner)
|
|
553
|
+
continue;
|
|
554
|
+
if (record.reservations.some((res) => res.targetOwner === owner))
|
|
555
|
+
continue;
|
|
556
|
+
this.unindexOwnerOfd(owner, record.ofdId);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
dropOfdLocks(ino, ofdId) {
|
|
560
|
+
const key = ofdOwnerKey(ofdId);
|
|
561
|
+
this.discardOwnerRangeOps(ino, key);
|
|
562
|
+
const inode = this.byIno.get(ino);
|
|
563
|
+
if (!inode)
|
|
564
|
+
return;
|
|
565
|
+
let changed = false;
|
|
566
|
+
const flock = inode.flocks.get(ofdId);
|
|
567
|
+
if (flock) {
|
|
568
|
+
inode.flocks.delete(ofdId);
|
|
569
|
+
this.totalFlocks -= 1;
|
|
570
|
+
if (flock.opId !== 0)
|
|
571
|
+
this.discardFlockChain(flock.opId);
|
|
572
|
+
changed = true;
|
|
573
|
+
}
|
|
574
|
+
const before = inode.ranges.length;
|
|
575
|
+
inode.ranges = inode.ranges.filter((entry) => entry.owner !== key);
|
|
576
|
+
const removed = before - inode.ranges.length;
|
|
577
|
+
if (removed > 0) {
|
|
578
|
+
this.adjustRangeCounts(-removed, 0);
|
|
579
|
+
changed = true;
|
|
580
|
+
}
|
|
581
|
+
if (changed)
|
|
582
|
+
this.wakeInode(ino, inode);
|
|
583
|
+
this.pruneInode(ino, inode);
|
|
584
|
+
}
|
|
585
|
+
ensureOfdRecord(ino, handle, ofdId, originOwner) {
|
|
586
|
+
let record = this.ofdRecords.get(ofdId);
|
|
587
|
+
if (!record) {
|
|
588
|
+
record = {
|
|
589
|
+
ino,
|
|
590
|
+
handle,
|
|
591
|
+
ofdId,
|
|
592
|
+
originOwner,
|
|
593
|
+
originClosed: false,
|
|
594
|
+
pendingOriginTransfer: false,
|
|
595
|
+
reservations: []
|
|
596
|
+
};
|
|
597
|
+
this.ofdRecords.set(ofdId, record);
|
|
598
|
+
this.indexOwnerOfd(originOwner, ofdId);
|
|
599
|
+
}
|
|
600
|
+
return record;
|
|
601
|
+
}
|
|
602
|
+
deleteOfdRecord(record) {
|
|
603
|
+
this.ofdRecords.delete(record.ofdId);
|
|
604
|
+
this.unindexOwnerOfd(record.originOwner, record.ofdId);
|
|
605
|
+
for (let i = 0;i < record.reservations.length; i++)
|
|
606
|
+
this.unindexOwnerOfd(record.reservations[i].targetOwner, record.ofdId);
|
|
607
|
+
}
|
|
608
|
+
indexOwnerOfd(sessionOwner, ofdId) {
|
|
609
|
+
let set = this.ownerOfds.get(sessionOwner);
|
|
610
|
+
if (!set) {
|
|
611
|
+
set = new Set;
|
|
612
|
+
this.ownerOfds.set(sessionOwner, set);
|
|
613
|
+
}
|
|
614
|
+
set.add(ofdId);
|
|
615
|
+
}
|
|
616
|
+
unindexOwnerOfd(sessionOwner, ofdId) {
|
|
617
|
+
const set = this.ownerOfds.get(sessionOwner);
|
|
618
|
+
if (!set)
|
|
619
|
+
return;
|
|
620
|
+
set.delete(ofdId);
|
|
621
|
+
if (set.size === 0)
|
|
622
|
+
this.ownerOfds.delete(sessionOwner);
|
|
623
|
+
}
|
|
624
|
+
ensureInode(ino) {
|
|
625
|
+
let inode = this.byIno.get(ino);
|
|
626
|
+
if (!inode) {
|
|
627
|
+
inode = { flocks: new Map, ranges: [], waiters: [] };
|
|
628
|
+
this.byIno.set(ino, inode);
|
|
629
|
+
}
|
|
630
|
+
return inode;
|
|
631
|
+
}
|
|
632
|
+
indexPosixIno(posixOwner, ino) {
|
|
633
|
+
let set = this.posixInos.get(posixOwner);
|
|
634
|
+
if (!set) {
|
|
635
|
+
set = new Set;
|
|
636
|
+
this.posixInos.set(posixOwner, set);
|
|
637
|
+
}
|
|
638
|
+
set.add(ino);
|
|
639
|
+
}
|
|
640
|
+
unindexPosixIno(posixOwner, ino) {
|
|
641
|
+
const set = this.posixInos.get(posixOwner);
|
|
642
|
+
if (!set)
|
|
643
|
+
return;
|
|
644
|
+
set.delete(ino);
|
|
645
|
+
if (set.size === 0)
|
|
646
|
+
this.posixInos.delete(posixOwner);
|
|
647
|
+
}
|
|
648
|
+
flockBlockers(inode, ofdId, mode) {
|
|
649
|
+
const blockers = [];
|
|
650
|
+
for (const entry of inode.flocks.values()) {
|
|
651
|
+
if (entry.ofdId === ofdId)
|
|
652
|
+
continue;
|
|
653
|
+
if (flockConflicts(entry.mode, mode))
|
|
654
|
+
blockers.push(ofdOwnerKey(entry.ofdId));
|
|
655
|
+
}
|
|
656
|
+
return blockers;
|
|
657
|
+
}
|
|
658
|
+
rangeBlockers(inode, owner, type, start, end) {
|
|
659
|
+
const blockers = new Set;
|
|
660
|
+
for (let i = 0;i < inode.ranges.length; i++) {
|
|
661
|
+
const entry = inode.ranges[i];
|
|
662
|
+
if (entry.owner === owner)
|
|
663
|
+
continue;
|
|
664
|
+
if (!rangesConflict(entry.type, type))
|
|
665
|
+
continue;
|
|
666
|
+
if (rangesOverlap(entry.start, entry.end, start, end))
|
|
667
|
+
blockers.add(entry.owner);
|
|
668
|
+
}
|
|
669
|
+
return Array.from(blockers);
|
|
670
|
+
}
|
|
671
|
+
discardFlockChain(opId) {
|
|
672
|
+
let cursor = opId;
|
|
673
|
+
while (cursor !== 0) {
|
|
674
|
+
this.pendingOps.delete(cursor);
|
|
675
|
+
const record = this.flockOpPrev.get(cursor);
|
|
676
|
+
this.flockOpPrev.delete(cursor);
|
|
677
|
+
cursor = record?.prevOpId ?? 0;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
recomputeOwnerRangeReserves(ino, owner, inode) {
|
|
681
|
+
const liveIntervals = new Map;
|
|
682
|
+
if (inode) {
|
|
683
|
+
for (let i = 0;i < inode.ranges.length; i++) {
|
|
684
|
+
const entry = inode.ranges[i];
|
|
685
|
+
if (entry.owner !== owner || entry.opId === 0)
|
|
686
|
+
continue;
|
|
687
|
+
addInterval(liveIntervals, entry.opId, entry.start, entry.end);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
const ownerOps = [];
|
|
691
|
+
for (const [opId, record] of this.rangeOpDeltas) {
|
|
692
|
+
if (record.ino === ino && record.owner === owner)
|
|
693
|
+
ownerOps.push(opId);
|
|
694
|
+
}
|
|
695
|
+
ownerOps.sort((left, right) => right - left);
|
|
696
|
+
for (let i = 0;i < ownerOps.length; i++) {
|
|
697
|
+
const opId = ownerOps[i];
|
|
698
|
+
const record = this.rangeOpDeltas.get(opId);
|
|
699
|
+
const spans = liveIntervals.get(opId);
|
|
700
|
+
const intersected = spans ? intersectDeltaWithIntervals(record.delta, spans) : undefined;
|
|
701
|
+
if (!intersected || intersected.trimmed.length === 0) {
|
|
702
|
+
this.reservedRangeRollbackSlots -= record.reservedSlots;
|
|
703
|
+
this.rangeOpDeltas.delete(opId);
|
|
704
|
+
this.pendingOps.delete(opId);
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
record.delta = intersected.trimmed;
|
|
708
|
+
for (let j = 0;j < intersected.trimmed.length; j++) {
|
|
709
|
+
const piece = intersected.trimmed[j];
|
|
710
|
+
if (piece.prevOpId !== 0)
|
|
711
|
+
addInterval(liveIntervals, piece.prevOpId, piece.start, piece.end);
|
|
712
|
+
}
|
|
713
|
+
this.reservedRangeRollbackSlots += intersected.reserve - record.reservedSlots;
|
|
714
|
+
record.reservedSlots = intersected.reserve;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
discardOwnerRangeOps(ino, owner) {
|
|
718
|
+
for (const [opId, record] of this.rangeOpDeltas) {
|
|
719
|
+
if (record.ino !== ino || record.owner !== owner)
|
|
720
|
+
continue;
|
|
721
|
+
this.reservedRangeRollbackSlots -= record.reservedSlots;
|
|
722
|
+
this.rangeOpDeltas.delete(opId);
|
|
723
|
+
this.pendingOps.delete(opId);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
detachFlockEntry(ino, ofdId) {
|
|
727
|
+
const inode = this.byIno.get(ino);
|
|
728
|
+
const existing = inode?.flocks.get(ofdId);
|
|
729
|
+
if (!inode || !existing)
|
|
730
|
+
return;
|
|
731
|
+
inode.flocks.delete(ofdId);
|
|
732
|
+
this.totalFlocks -= 1;
|
|
733
|
+
this.wakeInode(ino, inode);
|
|
734
|
+
this.pruneInode(ino, inode);
|
|
735
|
+
}
|
|
736
|
+
installFlock(ino, ofdId, mode, existing, previousMode, previousOpId, trackTransaction) {
|
|
737
|
+
const inode = this.ensureInode(ino);
|
|
738
|
+
const opId = trackTransaction ? this.nextOpId++ : 0;
|
|
739
|
+
if (!trackTransaction && previousOpId !== 0)
|
|
740
|
+
this.discardFlockChain(previousOpId);
|
|
741
|
+
if (existing) {
|
|
742
|
+
existing.mode = mode;
|
|
743
|
+
existing.opId = opId;
|
|
744
|
+
} else {
|
|
745
|
+
inode.flocks.set(ofdId, { mode, ofdId, opId });
|
|
746
|
+
this.totalFlocks += 1;
|
|
747
|
+
}
|
|
748
|
+
if (!trackTransaction)
|
|
749
|
+
return { errno: 0 };
|
|
750
|
+
this.pendingOps.add(opId);
|
|
751
|
+
this.flockOpPrev.set(opId, { ino, ofdId, prevMode: previousMode, prevOpId: previousOpId });
|
|
752
|
+
return {
|
|
753
|
+
errno: 0,
|
|
754
|
+
rollback: () => this.rollbackFlock(ino, ofdId, opId),
|
|
755
|
+
settle: () => this.settleFlock(ino, ofdId, opId)
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
settleFlock(ino, ofdId, opId) {
|
|
759
|
+
if (!this.pendingOps.delete(opId))
|
|
760
|
+
return;
|
|
761
|
+
this.flockOpPrev.delete(opId);
|
|
762
|
+
const current = this.byIno.get(ino)?.flocks.get(ofdId);
|
|
763
|
+
if (current && current.opId === opId)
|
|
764
|
+
current.opId = 0;
|
|
765
|
+
for (const record of this.flockOpPrev.values()) {
|
|
766
|
+
if (record.prevOpId === opId)
|
|
767
|
+
record.prevOpId = 0;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
rollbackFlock(ino, ofdId, opId) {
|
|
771
|
+
if (!this.pendingOps.delete(opId))
|
|
772
|
+
return;
|
|
773
|
+
const myPrev = this.flockOpPrev.get(opId);
|
|
774
|
+
this.flockOpPrev.delete(opId);
|
|
775
|
+
if (!myPrev)
|
|
776
|
+
return;
|
|
777
|
+
for (const record of this.flockOpPrev.values()) {
|
|
778
|
+
if (record.prevOpId !== opId)
|
|
779
|
+
continue;
|
|
780
|
+
record.prevMode = myPrev.prevMode;
|
|
781
|
+
record.prevOpId = myPrev.prevOpId;
|
|
782
|
+
}
|
|
783
|
+
const inode = this.byIno.get(ino);
|
|
784
|
+
const current = inode?.flocks.get(ofdId);
|
|
785
|
+
if (!inode || !current || current.opId !== opId)
|
|
786
|
+
return;
|
|
787
|
+
if (myPrev.prevMode === undefined) {
|
|
788
|
+
inode.flocks.delete(ofdId);
|
|
789
|
+
this.totalFlocks -= 1;
|
|
790
|
+
this.wakeInode(ino, inode);
|
|
791
|
+
this.pruneInode(ino, inode);
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
if (this.flockBlockers(inode, ofdId, myPrev.prevMode).length > 0) {
|
|
795
|
+
current.opId = myPrev.prevOpId;
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
current.mode = myPrev.prevMode;
|
|
799
|
+
current.opId = myPrev.prevOpId;
|
|
800
|
+
this.wakeInode(ino, inode);
|
|
801
|
+
}
|
|
802
|
+
removeFlock(ino, ofdId) {
|
|
803
|
+
const inode = this.byIno.get(ino);
|
|
804
|
+
const existing = inode?.flocks.get(ofdId);
|
|
805
|
+
if (!inode || !existing)
|
|
806
|
+
return;
|
|
807
|
+
inode.flocks.delete(ofdId);
|
|
808
|
+
this.totalFlocks -= 1;
|
|
809
|
+
if (existing.opId !== 0)
|
|
810
|
+
this.discardFlockChain(existing.opId);
|
|
811
|
+
this.wakeInode(ino, inode);
|
|
812
|
+
this.pruneInode(ino, inode);
|
|
813
|
+
}
|
|
814
|
+
installRangeLock(ino, owner, sessionOwner, posixOwner, ofdId, pid, type, start, end, handle, trackTransaction) {
|
|
815
|
+
const existingInode = this.byIno.get(ino);
|
|
816
|
+
const opId = trackTransaction ? this.nextOpId++ : 0;
|
|
817
|
+
const edited = editOwnerRanges(existingInode?.ranges ?? [], owner, posixOwner, ofdId, pid, type, start, end, opId);
|
|
818
|
+
const deltaCount = edited.nextCount - edited.previousCount;
|
|
819
|
+
const reservedSlots = trackTransaction ? rollbackReserveForDelta(edited.delta) : 0;
|
|
820
|
+
if (this.totalRangeLocks + this.reservedRangeRollbackSlots + deltaCount + reservedSlots > this.maxRangeLocks)
|
|
821
|
+
return { errno: ENOLCK };
|
|
822
|
+
if (trackTransaction && this.pendingOps.size >= this.maxPendingTransactions)
|
|
823
|
+
return { errno: ENOLCK };
|
|
824
|
+
if (ofdId !== undefined) {
|
|
825
|
+
const originOwner = parseOfdId(ofdId)?.owner ?? sessionOwner;
|
|
826
|
+
this.ensureOfdRecord(ino, handle, ofdId, originOwner);
|
|
827
|
+
this.indexOwnerOfd(sessionOwner, ofdId);
|
|
828
|
+
}
|
|
829
|
+
const inode = existingInode ?? this.ensureInode(ino);
|
|
830
|
+
inode.ranges = edited.next;
|
|
831
|
+
this.adjustRangeCounts(deltaCount, posixOwner !== undefined ? deltaCount : 0);
|
|
832
|
+
if (posixOwner !== undefined)
|
|
833
|
+
this.indexPosixIno(posixOwner, ino);
|
|
834
|
+
if (edited.relaxed)
|
|
835
|
+
this.wakeInode(ino, inode);
|
|
836
|
+
if (!trackTransaction) {
|
|
837
|
+
this.recomputeOwnerRangeReserves(ino, owner, inode);
|
|
838
|
+
return { errno: 0 };
|
|
839
|
+
}
|
|
840
|
+
this.pendingOps.add(opId);
|
|
841
|
+
this.reservedRangeRollbackSlots += reservedSlots;
|
|
842
|
+
this.rangeOpDeltas.set(opId, { ino, owner, delta: edited.delta, reservedSlots });
|
|
843
|
+
this.recomputeOwnerRangeReserves(ino, owner, inode);
|
|
844
|
+
return {
|
|
845
|
+
errno: 0,
|
|
846
|
+
rollback: () => this.rollbackRange(ino, owner, posixOwner, ofdId, pid, opId),
|
|
847
|
+
settle: () => this.settleRange(ino, owner, posixOwner, opId)
|
|
848
|
+
};
|
|
849
|
+
}
|
|
850
|
+
clearSettledRangePrevOp(ino, owner, opId) {
|
|
851
|
+
for (const record of this.rangeOpDeltas.values()) {
|
|
852
|
+
if (record.ino !== ino || record.owner !== owner)
|
|
853
|
+
continue;
|
|
854
|
+
for (let i = 0;i < record.delta.length; i++) {
|
|
855
|
+
if (record.delta[i].prevOpId === opId)
|
|
856
|
+
record.delta[i].prevOpId = 0;
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
settleRange(ino, owner, posixOwner, opId) {
|
|
861
|
+
if (!this.pendingOps.delete(opId))
|
|
862
|
+
return;
|
|
863
|
+
const record = this.rangeOpDeltas.get(opId);
|
|
864
|
+
if (record) {
|
|
865
|
+
this.reservedRangeRollbackSlots -= record.reservedSlots;
|
|
866
|
+
this.rangeOpDeltas.delete(opId);
|
|
867
|
+
}
|
|
868
|
+
this.clearSettledRangePrevOp(ino, owner, opId);
|
|
869
|
+
const inode = this.byIno.get(ino);
|
|
870
|
+
if (!inode)
|
|
871
|
+
return;
|
|
872
|
+
const others = [];
|
|
873
|
+
const owned = [];
|
|
874
|
+
let touched = false;
|
|
875
|
+
for (let i = 0;i < inode.ranges.length; i++) {
|
|
876
|
+
const entry = inode.ranges[i];
|
|
877
|
+
if (entry.owner !== owner) {
|
|
878
|
+
others.push(entry);
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
if (entry.opId === opId) {
|
|
882
|
+
entry.opId = 0;
|
|
883
|
+
touched = true;
|
|
884
|
+
}
|
|
885
|
+
owned.push(entry);
|
|
886
|
+
}
|
|
887
|
+
if (!touched) {
|
|
888
|
+
this.recomputeOwnerRangeReserves(ino, owner, inode);
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
const coalesced = coalesceOwned(owned);
|
|
892
|
+
const deltaCount = coalesced.length - owned.length;
|
|
893
|
+
inode.ranges = others.concat(coalesced);
|
|
894
|
+
if (deltaCount !== 0)
|
|
895
|
+
this.adjustRangeCounts(deltaCount, posixOwner !== undefined ? deltaCount : 0);
|
|
896
|
+
this.recomputeOwnerRangeReserves(ino, owner, inode);
|
|
897
|
+
}
|
|
898
|
+
rollbackRange(ino, owner, posixOwner, ofdId, pid, opId) {
|
|
899
|
+
if (!this.pendingOps.delete(opId))
|
|
900
|
+
return;
|
|
901
|
+
const myRecord = this.rangeOpDeltas.get(opId);
|
|
902
|
+
this.rangeOpDeltas.delete(opId);
|
|
903
|
+
if (!myRecord)
|
|
904
|
+
return;
|
|
905
|
+
this.reservedRangeRollbackSlots -= myRecord.reservedSlots;
|
|
906
|
+
for (const record of this.rangeOpDeltas.values()) {
|
|
907
|
+
if (record.ino !== ino || record.owner !== owner)
|
|
908
|
+
continue;
|
|
909
|
+
record.delta = spliceRangeDelta(record.delta, opId, myRecord.delta);
|
|
910
|
+
}
|
|
911
|
+
const inode = this.byIno.get(ino);
|
|
912
|
+
if (!inode) {
|
|
913
|
+
this.recomputeOwnerRangeReserves(ino, owner, undefined);
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
const others = [];
|
|
917
|
+
const keptOwned = [];
|
|
918
|
+
const targetSegments = [];
|
|
919
|
+
let previousCount = 0;
|
|
920
|
+
for (let i = 0;i < inode.ranges.length; i++) {
|
|
921
|
+
const entry = inode.ranges[i];
|
|
922
|
+
if (entry.owner !== owner) {
|
|
923
|
+
others.push(entry);
|
|
924
|
+
continue;
|
|
925
|
+
}
|
|
926
|
+
previousCount += 1;
|
|
927
|
+
if (entry.opId === opId)
|
|
928
|
+
targetSegments.push(entry);
|
|
929
|
+
else
|
|
930
|
+
keptOwned.push(entry);
|
|
931
|
+
}
|
|
932
|
+
if (targetSegments.length === 0) {
|
|
933
|
+
this.recomputeOwnerRangeReserves(ino, owner, inode);
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
let woke = false;
|
|
937
|
+
for (let i = 0;i < targetSegments.length; i++) {
|
|
938
|
+
const seg = targetSegments[i];
|
|
939
|
+
if (this.applyDeltaToSegment(inode, seg, myRecord.delta, owner, posixOwner, ofdId, pid, keptOwned)) {
|
|
940
|
+
woke = true;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
const coalesced = coalesceOwned(keptOwned);
|
|
944
|
+
const deltaCount = coalesced.length - previousCount;
|
|
945
|
+
inode.ranges = others.concat(coalesced);
|
|
946
|
+
this.adjustRangeCounts(deltaCount, posixOwner !== undefined ? deltaCount : 0);
|
|
947
|
+
if (posixOwner !== undefined && coalesced.length === 0)
|
|
948
|
+
this.unindexPosixIno(posixOwner, ino);
|
|
949
|
+
this.recomputeOwnerRangeReserves(ino, owner, inode);
|
|
950
|
+
if (woke)
|
|
951
|
+
this.wakeInode(ino, inode);
|
|
952
|
+
this.pruneInode(ino, inode);
|
|
953
|
+
}
|
|
954
|
+
applyDeltaToSegment(inode, seg, delta, owner, posixOwner, ofdId, pid, out) {
|
|
955
|
+
let woke = false;
|
|
956
|
+
for (let j = 0;j < delta.length; j++) {
|
|
957
|
+
const piece = delta[j];
|
|
958
|
+
if (piece.end <= seg.start)
|
|
959
|
+
continue;
|
|
960
|
+
if (piece.start >= seg.end)
|
|
961
|
+
break;
|
|
962
|
+
const start = Math.max(seg.start, piece.start);
|
|
963
|
+
const end = Math.min(seg.end, piece.end);
|
|
964
|
+
if (piece.prevType === F_UNLCK) {
|
|
965
|
+
woke = true;
|
|
966
|
+
continue;
|
|
967
|
+
}
|
|
968
|
+
if (piece.prevType === F_RDLCK) {
|
|
969
|
+
if (seg.type === F_WRLCK)
|
|
970
|
+
woke = true;
|
|
971
|
+
out.push({
|
|
972
|
+
owner,
|
|
973
|
+
posixOwner,
|
|
974
|
+
ofdId,
|
|
975
|
+
pid,
|
|
976
|
+
type: F_RDLCK,
|
|
977
|
+
start,
|
|
978
|
+
end,
|
|
979
|
+
opId: piece.prevOpId
|
|
980
|
+
});
|
|
981
|
+
continue;
|
|
982
|
+
}
|
|
983
|
+
const canRestoreWrite = this.rangeBlockers(inode, owner, F_WRLCK, start, end).length === 0;
|
|
984
|
+
const restoredType = canRestoreWrite ? F_WRLCK : F_RDLCK;
|
|
985
|
+
out.push({
|
|
986
|
+
owner,
|
|
987
|
+
posixOwner,
|
|
988
|
+
ofdId,
|
|
989
|
+
pid,
|
|
990
|
+
type: restoredType,
|
|
991
|
+
start,
|
|
992
|
+
end,
|
|
993
|
+
opId: piece.prevOpId
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
return woke;
|
|
997
|
+
}
|
|
998
|
+
wouldDeadlock(waiterOwner, blockers) {
|
|
999
|
+
const visited = new Set;
|
|
1000
|
+
const stack = blockers.slice();
|
|
1001
|
+
while (stack.length > 0) {
|
|
1002
|
+
const current = stack.pop();
|
|
1003
|
+
if (current === waiterOwner)
|
|
1004
|
+
return true;
|
|
1005
|
+
if (visited.has(current))
|
|
1006
|
+
continue;
|
|
1007
|
+
visited.add(current);
|
|
1008
|
+
const next = this.waitingOn.get(current);
|
|
1009
|
+
if (!next)
|
|
1010
|
+
continue;
|
|
1011
|
+
for (const target of next)
|
|
1012
|
+
stack.push(target);
|
|
1013
|
+
}
|
|
1014
|
+
return false;
|
|
1015
|
+
}
|
|
1016
|
+
enqueueWaiter(ino, owner, sessionOwner, blockers, trackDeadlock) {
|
|
1017
|
+
const inode = this.ensureInode(ino);
|
|
1018
|
+
const deferred = Promise.withResolvers();
|
|
1019
|
+
const settle = () => {
|
|
1020
|
+
if (waiter.resolved)
|
|
1021
|
+
return;
|
|
1022
|
+
waiter.resolved = true;
|
|
1023
|
+
this.detachWaiter(waiter);
|
|
1024
|
+
deferred.resolve();
|
|
1025
|
+
};
|
|
1026
|
+
const waiter = {
|
|
1027
|
+
id: this.nextWaiterId++,
|
|
1028
|
+
ino,
|
|
1029
|
+
owner,
|
|
1030
|
+
sessionOwner,
|
|
1031
|
+
blockers: blockers.slice(),
|
|
1032
|
+
trackDeadlock,
|
|
1033
|
+
resolved: false,
|
|
1034
|
+
wake: settle,
|
|
1035
|
+
cancel: settle
|
|
1036
|
+
};
|
|
1037
|
+
inode.waiters.push(waiter);
|
|
1038
|
+
this.totalWaiters += 1;
|
|
1039
|
+
let sessionWaiters = this.waitersBySession.get(sessionOwner);
|
|
1040
|
+
if (!sessionWaiters) {
|
|
1041
|
+
sessionWaiters = new Set;
|
|
1042
|
+
this.waitersBySession.set(sessionOwner, sessionWaiters);
|
|
1043
|
+
}
|
|
1044
|
+
sessionWaiters.add(waiter);
|
|
1045
|
+
if (trackDeadlock)
|
|
1046
|
+
this.addDeadlockWaiter(waiter);
|
|
1047
|
+
return new LockContentionError(deferred.promise, settle);
|
|
1048
|
+
}
|
|
1049
|
+
addDeadlockWaiter(waiter) {
|
|
1050
|
+
let waiters = this.deadlockWaiters.get(waiter.owner);
|
|
1051
|
+
if (!waiters) {
|
|
1052
|
+
waiters = new Set;
|
|
1053
|
+
this.deadlockWaiters.set(waiter.owner, waiters);
|
|
1054
|
+
}
|
|
1055
|
+
waiters.add(waiter);
|
|
1056
|
+
let edges = this.waitingOn.get(waiter.owner);
|
|
1057
|
+
if (!edges) {
|
|
1058
|
+
edges = new Set;
|
|
1059
|
+
this.waitingOn.set(waiter.owner, edges);
|
|
1060
|
+
}
|
|
1061
|
+
for (let i = 0;i < waiter.blockers.length; i++)
|
|
1062
|
+
edges.add(waiter.blockers[i]);
|
|
1063
|
+
}
|
|
1064
|
+
removeDeadlockWaiter(waiter) {
|
|
1065
|
+
const waiters = this.deadlockWaiters.get(waiter.owner);
|
|
1066
|
+
if (!waiters)
|
|
1067
|
+
return;
|
|
1068
|
+
waiters.delete(waiter);
|
|
1069
|
+
if (waiters.size === 0) {
|
|
1070
|
+
this.deadlockWaiters.delete(waiter.owner);
|
|
1071
|
+
this.waitingOn.delete(waiter.owner);
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
const edges = new Set;
|
|
1075
|
+
for (const active of waiters) {
|
|
1076
|
+
for (let i = 0;i < active.blockers.length; i++)
|
|
1077
|
+
edges.add(active.blockers[i]);
|
|
1078
|
+
}
|
|
1079
|
+
this.waitingOn.set(waiter.owner, edges);
|
|
1080
|
+
}
|
|
1081
|
+
detachWaiter(waiter) {
|
|
1082
|
+
const inode = this.byIno.get(waiter.ino);
|
|
1083
|
+
if (inode) {
|
|
1084
|
+
const index = inode.waiters.indexOf(waiter);
|
|
1085
|
+
if (index >= 0) {
|
|
1086
|
+
inode.waiters.splice(index, 1);
|
|
1087
|
+
this.totalWaiters -= 1;
|
|
1088
|
+
}
|
|
1089
|
+
this.pruneInode(waiter.ino, inode);
|
|
1090
|
+
}
|
|
1091
|
+
const sessionWaiters = this.waitersBySession.get(waiter.sessionOwner);
|
|
1092
|
+
if (sessionWaiters) {
|
|
1093
|
+
sessionWaiters.delete(waiter);
|
|
1094
|
+
if (sessionWaiters.size === 0)
|
|
1095
|
+
this.waitersBySession.delete(waiter.sessionOwner);
|
|
1096
|
+
}
|
|
1097
|
+
if (waiter.trackDeadlock)
|
|
1098
|
+
this.removeDeadlockWaiter(waiter);
|
|
1099
|
+
}
|
|
1100
|
+
wakeInode(_ino, inode) {
|
|
1101
|
+
if (inode.waiters.length === 0)
|
|
1102
|
+
return;
|
|
1103
|
+
const pending = inode.waiters.slice();
|
|
1104
|
+
for (let i = 0;i < pending.length; i++)
|
|
1105
|
+
pending[i].wake();
|
|
1106
|
+
}
|
|
1107
|
+
pruneInode(ino, inode) {
|
|
1108
|
+
if (inode.flocks.size === 0 && inode.ranges.length === 0 && inode.waiters.length === 0) {
|
|
1109
|
+
this.byIno.delete(ino);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
}
|