@effect-vfs/nfs 0.1.0
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/LICENSE +22 -0
- package/README.md +93 -0
- package/dist/NfsServer.d.ts +693 -0
- package/dist/NfsServer.d.ts.map +1 -0
- package/dist/NfsServer.js +439 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/internal/export.d.ts +77 -0
- package/dist/internal/export.d.ts.map +1 -0
- package/dist/internal/export.js +224 -0
- package/dist/internal/lockRanges.d.ts +16 -0
- package/dist/internal/lockRanges.d.ts.map +1 -0
- package/dist/internal/lockRanges.js +12 -0
- package/dist/internal/nfs4.d.ts +194 -0
- package/dist/internal/nfs4.d.ts.map +1 -0
- package/dist/internal/nfs4.js +3561 -0
- package/dist/internal/recordMarking.d.ts +29 -0
- package/dist/internal/recordMarking.d.ts.map +1 -0
- package/dist/internal/recordMarking.js +127 -0
- package/dist/internal/rpc.d.ts +73 -0
- package/dist/internal/rpc.d.ts.map +1 -0
- package/dist/internal/rpc.js +161 -0
- package/dist/internal/server.d.ts +17 -0
- package/dist/internal/server.d.ts.map +1 -0
- package/dist/internal/server.js +78 -0
- package/dist/internal/xdr.d.ts +51 -0
- package/dist/internal/xdr.d.ts.map +1 -0
- package/dist/internal/xdr.js +178 -0
- package/package.json +54 -0
|
@@ -0,0 +1,3561 @@
|
|
|
1
|
+
import * as ByteSize from "effect/ByteSize";
|
|
2
|
+
import * as Crypto from "effect/Crypto";
|
|
3
|
+
import * as Deferred from "effect/Deferred";
|
|
4
|
+
import * as Duration from "effect/Duration";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Option from "effect/Option";
|
|
7
|
+
import * as Predicate from "effect/Predicate";
|
|
8
|
+
import * as Schedule from "effect/Schedule";
|
|
9
|
+
import * as Schema from "effect/Schema";
|
|
10
|
+
import * as Semaphore from "effect/Semaphore";
|
|
11
|
+
import { ExportCapacityError, InvalidNameError, validateName } from "./export.js";
|
|
12
|
+
import { lockRange, MAX_OFFSET, overlaps } from "./lockRanges.js";
|
|
13
|
+
import { RpcPolicyDenied } from "./rpc.js";
|
|
14
|
+
import { Reader, Writer, XdrDecodeError } from "./xdr.js";
|
|
15
|
+
/** @internal */
|
|
16
|
+
export const Status = {
|
|
17
|
+
OK: 0,
|
|
18
|
+
PERM: 1,
|
|
19
|
+
NOENT: 2,
|
|
20
|
+
IO: 5,
|
|
21
|
+
ACCESS: 13,
|
|
22
|
+
EXIST: 17,
|
|
23
|
+
NOTDIR: 20,
|
|
24
|
+
ISDIR: 21,
|
|
25
|
+
INVAL: 22,
|
|
26
|
+
FBIG: 27,
|
|
27
|
+
NOSPC: 28,
|
|
28
|
+
ROFS: 30,
|
|
29
|
+
NAMETOOLONG: 63,
|
|
30
|
+
NOTEMPTY: 66,
|
|
31
|
+
STALE: 70,
|
|
32
|
+
BADHANDLE: 10001,
|
|
33
|
+
BAD_COOKIE: 10003,
|
|
34
|
+
NOTSUPP: 10004,
|
|
35
|
+
TOOSMALL: 10005,
|
|
36
|
+
SERVERFAULT: 10006,
|
|
37
|
+
BADTYPE: 10007,
|
|
38
|
+
DELAY: 10008,
|
|
39
|
+
SAME: 10009,
|
|
40
|
+
EXPIRED: 10011,
|
|
41
|
+
LOCKED: 10012,
|
|
42
|
+
DENIED: 10010,
|
|
43
|
+
OPENMODE: 10038,
|
|
44
|
+
FHEXPIRED: 10014,
|
|
45
|
+
SHARE_DENIED: 10015,
|
|
46
|
+
CLID_INUSE: 10017,
|
|
47
|
+
NOFILEHANDLE: 10020,
|
|
48
|
+
MINOR_VERS_MISMATCH: 10021,
|
|
49
|
+
STALE_CLIENTID: 10022,
|
|
50
|
+
OLD_STATEID: 10024,
|
|
51
|
+
BAD_STATEID: 10025,
|
|
52
|
+
BAD_SEQID: 10026,
|
|
53
|
+
NOT_SAME: 10027,
|
|
54
|
+
LOCK_RANGE: 10028,
|
|
55
|
+
SYMLINK: 10029,
|
|
56
|
+
ATTRNOTSUPP: 10032,
|
|
57
|
+
BADOWNER: 10039,
|
|
58
|
+
NO_GRACE: 10033,
|
|
59
|
+
BADXDR: 10036,
|
|
60
|
+
LOCKS_HELD: 10037,
|
|
61
|
+
BADCHAR: 10040,
|
|
62
|
+
BADNAME: 10041,
|
|
63
|
+
OP_ILLEGAL: 10044,
|
|
64
|
+
BADSESSION: 10052,
|
|
65
|
+
BADSLOT: 10053,
|
|
66
|
+
COMPLETE_ALREADY: 10054,
|
|
67
|
+
CONN_NOT_BOUND_TO_SESSION: 10055,
|
|
68
|
+
SEQ_MISORDERED: 10063,
|
|
69
|
+
SEQUENCE_POS: 10064,
|
|
70
|
+
REQ_TOO_BIG: 10065,
|
|
71
|
+
REP_TOO_BIG: 10066,
|
|
72
|
+
REP_TOO_BIG_TO_CACHE: 10067,
|
|
73
|
+
RETRY_UNCACHED_REP: 10068,
|
|
74
|
+
TOO_MANY_OPS: 10070,
|
|
75
|
+
OP_NOT_IN_SESSION: 10071,
|
|
76
|
+
CLIENTID_BUSY: 10074,
|
|
77
|
+
SEQ_FALSE_RETRY: 10076,
|
|
78
|
+
BAD_HIGH_SLOT: 10077,
|
|
79
|
+
ENCR_ALG_UNSUPP: 10079,
|
|
80
|
+
NOT_ONLY_OP: 10081,
|
|
81
|
+
WRONG_TYPE: 10083
|
|
82
|
+
};
|
|
83
|
+
/** @internal */
|
|
84
|
+
export const Operation = {
|
|
85
|
+
ACCESS: 3,
|
|
86
|
+
CLOSE: 4,
|
|
87
|
+
COMMIT: 5,
|
|
88
|
+
CREATE: 6,
|
|
89
|
+
DELEGPURGE: 7,
|
|
90
|
+
DELEGRETURN: 8,
|
|
91
|
+
GETATTR: 9,
|
|
92
|
+
GETFH: 10,
|
|
93
|
+
LINK: 11,
|
|
94
|
+
LOCK: 12,
|
|
95
|
+
LOCKT: 13,
|
|
96
|
+
LOCKU: 14,
|
|
97
|
+
LOOKUP: 15,
|
|
98
|
+
LOOKUPP: 16,
|
|
99
|
+
NVERIFY: 17,
|
|
100
|
+
OPEN: 18,
|
|
101
|
+
OPENATTR: 19,
|
|
102
|
+
OPEN_CONFIRM: 20,
|
|
103
|
+
OPEN_DOWNGRADE: 21,
|
|
104
|
+
PUTFH: 22,
|
|
105
|
+
PUTPUBFH: 23,
|
|
106
|
+
PUTROOTFH: 24,
|
|
107
|
+
READ: 25,
|
|
108
|
+
READDIR: 26,
|
|
109
|
+
READLINK: 27,
|
|
110
|
+
REMOVE: 28,
|
|
111
|
+
RENAME: 29,
|
|
112
|
+
RENEW: 30,
|
|
113
|
+
RESTOREFH: 31,
|
|
114
|
+
SAVEFH: 32,
|
|
115
|
+
SECINFO: 33,
|
|
116
|
+
SETATTR: 34,
|
|
117
|
+
SETCLIENTID: 35,
|
|
118
|
+
SETCLIENTID_CONFIRM: 36,
|
|
119
|
+
VERIFY: 37,
|
|
120
|
+
WRITE: 38,
|
|
121
|
+
RELEASE_LOCKOWNER: 39,
|
|
122
|
+
BACKCHANNEL_CTL: 40,
|
|
123
|
+
BIND_CONN_TO_SESSION: 41,
|
|
124
|
+
EXCHANGE_ID: 42,
|
|
125
|
+
CREATE_SESSION: 43,
|
|
126
|
+
DESTROY_SESSION: 44,
|
|
127
|
+
FREE_STATEID: 45,
|
|
128
|
+
GET_DIR_DELEGATION: 46,
|
|
129
|
+
GETDEVICEINFO: 47,
|
|
130
|
+
GETDEVICELIST: 48,
|
|
131
|
+
LAYOUTCOMMIT: 49,
|
|
132
|
+
LAYOUTGET: 50,
|
|
133
|
+
LAYOUTRETURN: 51,
|
|
134
|
+
SECINFO_NO_NAME: 52,
|
|
135
|
+
SEQUENCE: 53,
|
|
136
|
+
SET_SSV: 54,
|
|
137
|
+
TEST_STATEID: 55,
|
|
138
|
+
WANT_DELEGATION: 56,
|
|
139
|
+
DESTROY_CLIENTID: 57,
|
|
140
|
+
RECLAIM_COMPLETE: 58,
|
|
141
|
+
ILLEGAL: 10044
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* NFSv4.0 operations that RFC 8881 Section 8.8 says an NFSv4.1 server MUST NOT
|
|
145
|
+
* implement and MUST answer with NFS4ERR_NOTSUPP.
|
|
146
|
+
*/
|
|
147
|
+
const mustNotImplementOperations = new Set([
|
|
148
|
+
Operation.OPEN_CONFIRM,
|
|
149
|
+
Operation.RENEW,
|
|
150
|
+
Operation.SETCLIENTID,
|
|
151
|
+
Operation.SETCLIENTID_CONFIRM,
|
|
152
|
+
Operation.RELEASE_LOCKOWNER
|
|
153
|
+
]);
|
|
154
|
+
/**
|
|
155
|
+
* OPTIONAL and RECOMMENDED operations this server does not implement. RFC 8881
|
|
156
|
+
* Section 17 requires NFS4ERR_NOTSUPP for them. Their arguments are not decoded
|
|
157
|
+
* because the compound stops at the failing operation.
|
|
158
|
+
*/
|
|
159
|
+
const unsupportedOptionalOperations = new Set([
|
|
160
|
+
Operation.DELEGPURGE,
|
|
161
|
+
Operation.DELEGRETURN,
|
|
162
|
+
Operation.OPENATTR,
|
|
163
|
+
Operation.GET_DIR_DELEGATION,
|
|
164
|
+
Operation.GETDEVICEINFO,
|
|
165
|
+
Operation.GETDEVICELIST,
|
|
166
|
+
Operation.LAYOUTCOMMIT,
|
|
167
|
+
Operation.LAYOUTGET,
|
|
168
|
+
Operation.LAYOUTRETURN,
|
|
169
|
+
Operation.WANT_DELEGATION
|
|
170
|
+
]);
|
|
171
|
+
/** channel_dir_from_server4 (RFC 8881 Section 18.34.2). */
|
|
172
|
+
const CDFS4_FORE = 1;
|
|
173
|
+
const CDFS4_BACK = 2;
|
|
174
|
+
const CDFS4_BOTH = 3;
|
|
175
|
+
/** channel_dir_from_client4 (RFC 8881 Section 18.34.1). */
|
|
176
|
+
const CDFC4_FORE = 0x1;
|
|
177
|
+
const CDFC4_BACK = 0x2;
|
|
178
|
+
const CDFC4_FORE_OR_BOTH = 0x3;
|
|
179
|
+
const CDFC4_BACK_OR_BOTH = 0x7;
|
|
180
|
+
/**
|
|
181
|
+
* Directions a connection carries for one session. A connection may carry both, and the same
|
|
182
|
+
* connection may serve several sessions (Section 2.10.3.1).
|
|
183
|
+
*/
|
|
184
|
+
const CHANNEL_FORE = 0x1;
|
|
185
|
+
const CHANNEL_BACK = 0x2;
|
|
186
|
+
/** csa_flags bit asking the server to bind the CREATE_SESSION connection to the backchannel. */
|
|
187
|
+
const CREATE_SESSION4_FLAG_CONN_BACK_CHAN = 0x2;
|
|
188
|
+
/** The callback RPC program's version is 1, per RFC 5661 erratum 2291; the RFC text says 4. */
|
|
189
|
+
const CALLBACK_RPC_VERSION = 1;
|
|
190
|
+
const CB_COMPOUND_PROCEDURE = 1;
|
|
191
|
+
const OP_CB_SEQUENCE = 11;
|
|
192
|
+
/**
|
|
193
|
+
* sr_status_flags bits for a backchannel the server cannot use (RFC 8881 Section 18.46.3).
|
|
194
|
+
* CB_PATH_DOWN_SESSION is the session-scoped form, which is what this server tracks: health is
|
|
195
|
+
* recorded per session backchannel, not per client ID.
|
|
196
|
+
*/
|
|
197
|
+
const SEQ4_STATUS_CB_PATH_DOWN_SESSION = 0x0000_0200;
|
|
198
|
+
/** RPCSEC_GSS in callback_sec_parms4; this server never issues the handles it would name. */
|
|
199
|
+
const RPCSEC_GSS = 6;
|
|
200
|
+
const SECINFO_STYLE4_CURRENT_FH = 0;
|
|
201
|
+
const SECINFO_STYLE4_PARENT = 1;
|
|
202
|
+
/**
|
|
203
|
+
* Smallest COMPOUND a session can carry: an RPC call header with AUTH_NONE plus a
|
|
204
|
+
* SEQUENCE-only compound. A fore channel that cannot fit it can never be used.
|
|
205
|
+
*/
|
|
206
|
+
const MIN_FORE_REQUEST_BYTES = 40 + 48;
|
|
207
|
+
/** Smallest reply a session can carry: an RPC reply header plus a SEQUENCE-only compound. */
|
|
208
|
+
const MIN_FORE_RESPONSE_BYTES = 24 + 12 + 8 + 36;
|
|
209
|
+
/** PERSIST, CONN_BACK_CHAN, and CONN_RDMA are the only defined csa_flags bits. */
|
|
210
|
+
const CREATE_SESSION4_KNOWN_FLAGS = 0x7;
|
|
211
|
+
const ACCESS4_READ = 0x01;
|
|
212
|
+
const ACCESS4_LOOKUP = 0x02;
|
|
213
|
+
const ACCESS4_MODIFY = 0x04;
|
|
214
|
+
const ACCESS4_EXTEND = 0x08;
|
|
215
|
+
const ACCESS4_DELETE = 0x10;
|
|
216
|
+
const ACCESS4_EXECUTE = 0x20;
|
|
217
|
+
const AUTH_NONE = 0;
|
|
218
|
+
const AUTH_SYS = 1;
|
|
219
|
+
/** RFC 5531 caps an `opaque_auth` body at 400 bytes, which bounds any credential a callback carries. */
|
|
220
|
+
const MAX_OPAQUE_AUTH_BYTES = 400;
|
|
221
|
+
/** The export accepts and generates only UTF-8 names (RFC 8881 Section 14.4). */
|
|
222
|
+
const FSCHARSET_CAP4_ALLOWS_ONLY_UTF8 = 0x2;
|
|
223
|
+
const SP4_NONE = 0;
|
|
224
|
+
const SP4_MACH_CRED = 1;
|
|
225
|
+
const SP4_SSV = 2;
|
|
226
|
+
const OPEN4_SHARE_ACCESS_READ = 0x0001;
|
|
227
|
+
const OPEN4_SHARE_ACCESS_WRITE = 0x0002;
|
|
228
|
+
const OPEN4_SHARE_ACCESS_MASK = 0x0003;
|
|
229
|
+
const OPEN4_SHARE_DENY_READ = 0x0001;
|
|
230
|
+
const OPEN4_SHARE_DENY_BOTH = 0x0003;
|
|
231
|
+
/** open_claim_type4 values that reclaim state after a restart or a delegation. */
|
|
232
|
+
const CLAIM_PREVIOUS = 1;
|
|
233
|
+
const CLAIM_DELEGATE_CUR = 2;
|
|
234
|
+
const CLAIM_DELEGATE_PREV = 3;
|
|
235
|
+
const CLAIM_DELEG_CUR_FH = 5;
|
|
236
|
+
const CLAIM_DELEG_PREV_FH = 6;
|
|
237
|
+
/**
|
|
238
|
+
* Worst-case encoded GETATTR result: the largest attribute set this server can return for a
|
|
239
|
+
* 25-byte filehandle and bounded capacity is about 360 bytes; recheck when attributes grow.
|
|
240
|
+
*/
|
|
241
|
+
const MAX_GETATTR_REPLY_BYTES = 512;
|
|
242
|
+
/** WRITE_LT and WRITEW_LT request exclusive ranges. */
|
|
243
|
+
const WRITE_LOCK_TYPES = new Set([2, 4]);
|
|
244
|
+
const OPEN4_SHARE_ACCESS_WANT_DELEG_MASK = 0xff00;
|
|
245
|
+
const OPEN4_SHARE_ACCESS_WANT_NO_DELEG = 0x0400;
|
|
246
|
+
const OPEN4_SHARE_ACCESS_WANT_CANCEL = 0x0500;
|
|
247
|
+
/** SIGNAL_DELEG_WHEN_RESRC_AVAIL and PUSH_DELEG_WHEN_UNCONTENDED are registration hints. */
|
|
248
|
+
const OPEN4_SHARE_ACCESS_WANT_HINT_MASK = 0x0003_0000;
|
|
249
|
+
const OPEN_DELEGATE_NONE = 0;
|
|
250
|
+
const OPEN_DELEGATE_NONE_EXT = 3;
|
|
251
|
+
const WND4_NOT_WANTED = 0;
|
|
252
|
+
const WND4_NOT_SUPP_FTYPE = 3;
|
|
253
|
+
const WND4_CANCELLED = 7;
|
|
254
|
+
const EXCHGID4_FLAG_USE_NON_PNFS = 0x0001_0000;
|
|
255
|
+
const EXCHGID4_FLAG_CONFIRMED_R = 0x8000_0000;
|
|
256
|
+
const EXCHGID4_ALLOWED_ARGUMENT_FLAGS = 0x4007_0103;
|
|
257
|
+
/** @internal */
|
|
258
|
+
export const nextSequenceId = (sequence) => (sequence + 1) >>> 0;
|
|
259
|
+
/** Replace an owner's byte state on a range, retaining sorted, disjoint intervals. */
|
|
260
|
+
const replaceLockRange = (held, range, type) => {
|
|
261
|
+
const next = [];
|
|
262
|
+
for (const entry of held) {
|
|
263
|
+
if (!overlaps(entry.range, range)) {
|
|
264
|
+
next.push(entry);
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (entry.range.offset < range.offset) {
|
|
268
|
+
next.push({ range: lockRange(entry.range.offset, range.offset - entry.range.offset), type: entry.type });
|
|
269
|
+
}
|
|
270
|
+
if (entry.range.end > range.end) {
|
|
271
|
+
next.push({
|
|
272
|
+
range: lockRange(range.end, entry.range.end === MAX_OFFSET + 1n ? MAX_OFFSET : entry.range.end - range.end),
|
|
273
|
+
type: entry.type
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (type !== undefined)
|
|
278
|
+
next.push({ range, type });
|
|
279
|
+
next.sort((a, b) => a.range.offset < b.range.offset ? -1 : a.range.offset > b.range.offset ? 1 : 0);
|
|
280
|
+
const merged = [];
|
|
281
|
+
for (const entry of next) {
|
|
282
|
+
const last = merged.at(-1);
|
|
283
|
+
if (last !== undefined && last.type === entry.type && last.range.end === entry.range.offset) {
|
|
284
|
+
merged[merged.length - 1] = {
|
|
285
|
+
range: lockRange(last.range.offset, entry.range.end === MAX_OFFSET + 1n ? MAX_OFFSET : entry.range.end - last.range.offset),
|
|
286
|
+
type: last.type
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
else
|
|
290
|
+
merged.push(entry);
|
|
291
|
+
}
|
|
292
|
+
return merged;
|
|
293
|
+
};
|
|
294
|
+
/** Adds `direction` to what `connection` already carries for `session` (Section 2.10.3.1). */
|
|
295
|
+
const associate = (session, connection, direction) => {
|
|
296
|
+
session.connections.set(connection, (session.connections.get(connection) ?? 0) | direction);
|
|
297
|
+
};
|
|
298
|
+
const empty = new Uint8Array();
|
|
299
|
+
const assertOptions = (options) => {
|
|
300
|
+
if (!Number.isSafeInteger(options.leaseDurationSeconds) || options.leaseDurationSeconds <= 0) {
|
|
301
|
+
throw new RangeError("leaseDurationSeconds must be a positive safe integer");
|
|
302
|
+
}
|
|
303
|
+
if (options.generation.length !== 16)
|
|
304
|
+
throw new RangeError("generation must contain exactly 16 bytes");
|
|
305
|
+
if ((options.storageGeneration ?? options.generation).length !== 16) {
|
|
306
|
+
throw new RangeError("storageGeneration must contain exactly 16 bytes");
|
|
307
|
+
}
|
|
308
|
+
for (const [name, value] of Object.entries(options.limits)) {
|
|
309
|
+
if (Predicate.isBigInt(value)) {
|
|
310
|
+
if (value <= 0n)
|
|
311
|
+
throw new RangeError(`${name} must be a positive byte size`);
|
|
312
|
+
}
|
|
313
|
+
else if (!Number.isSafeInteger(value) || value <= 0) {
|
|
314
|
+
throw new RangeError(`${name} must be a positive safe integer`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
const byteLength = (length) => ByteSize.bytes(length);
|
|
319
|
+
const addBytes = (left, right) => ByteSize.sum(left, right);
|
|
320
|
+
const subtractBytes = (left, right) => ByteSize.bytes(left - right);
|
|
321
|
+
const bytesKey = (bytes) => {
|
|
322
|
+
let result = "";
|
|
323
|
+
for (const byte of bytes)
|
|
324
|
+
result += byte.toString(16).padStart(2, "0");
|
|
325
|
+
return result;
|
|
326
|
+
};
|
|
327
|
+
const credentialsKey = (credentials) => !Predicate.isTagged(credentials, "Sys")
|
|
328
|
+
? "none"
|
|
329
|
+
: `sys:${credentials.uid}:${credentials.gid}:${credentials.machineName}:${credentials.supplementaryGroups.join(",")}`;
|
|
330
|
+
/**
|
|
331
|
+
* The principal of an RPC credential for client-record ownership. AUTH_SYS has no
|
|
332
|
+
* verified identity, so this only serializes EXCHANGE_ID and CREATE_SESSION
|
|
333
|
+
* ownership; it never grants VFS authority.
|
|
334
|
+
*/
|
|
335
|
+
const principalKey = (credentials) => !Predicate.isTagged(credentials, "Sys") ? "none" : `sys:${credentials.uid}`;
|
|
336
|
+
const sameRequest = (left, right) => {
|
|
337
|
+
if (left === undefined || left.length !== right.length)
|
|
338
|
+
return false;
|
|
339
|
+
for (let index = 0; index < left.length; index++)
|
|
340
|
+
if (left[index] !== right[index])
|
|
341
|
+
return false;
|
|
342
|
+
return true;
|
|
343
|
+
};
|
|
344
|
+
const bitmap = (reader, limit) => reader.array((item) => item.uint32(), limit);
|
|
345
|
+
const writeBitmap = (writer, words) => {
|
|
346
|
+
writer.array(words, (target, word) => target.uint32(word));
|
|
347
|
+
};
|
|
348
|
+
const attributesIn = (words) => {
|
|
349
|
+
const result = [];
|
|
350
|
+
for (let word = 0; word < words.length; word++) {
|
|
351
|
+
for (let bit = 0; bit < 32; bit++)
|
|
352
|
+
if (((words[word] >>> bit) & 1) !== 0)
|
|
353
|
+
result.push(word * 32 + bit);
|
|
354
|
+
}
|
|
355
|
+
return result;
|
|
356
|
+
};
|
|
357
|
+
const validateWritableAttributes = (words, bytes, limits) => {
|
|
358
|
+
const attributes = attributesIn(words);
|
|
359
|
+
if (attributes.some((attribute) => ![4, 33, 36, 37, 48, 54].includes(attribute)))
|
|
360
|
+
return;
|
|
361
|
+
const values = new Reader(bytes, limits);
|
|
362
|
+
for (const attribute of attributes) {
|
|
363
|
+
if (attribute === 4)
|
|
364
|
+
values.uint64();
|
|
365
|
+
else if (attribute === 33)
|
|
366
|
+
values.uint32();
|
|
367
|
+
else if (attribute === 36 || attribute === 37)
|
|
368
|
+
values.string(limits.maxStringBytes);
|
|
369
|
+
else if (attribute === 48 || attribute === 54) {
|
|
370
|
+
const how = values.uint32();
|
|
371
|
+
if (how === 1) {
|
|
372
|
+
values.uint64();
|
|
373
|
+
if (values.uint32() >= 1_000_000_000)
|
|
374
|
+
throw new XdrDecodeError("Invalid attribute nanoseconds");
|
|
375
|
+
}
|
|
376
|
+
else if (how !== 0)
|
|
377
|
+
throw new XdrDecodeError("Invalid set-time discriminant");
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
values.finish();
|
|
381
|
+
};
|
|
382
|
+
const readAttributes = (reader, limits) => {
|
|
383
|
+
const words = bitmap(reader, limits.maxBitmapWords);
|
|
384
|
+
const values = reader.opaque(limits.maxOpaqueBytes);
|
|
385
|
+
validateWritableAttributes(words, values, limits);
|
|
386
|
+
return { bitmap: words, values };
|
|
387
|
+
};
|
|
388
|
+
const isNumericOwner = Schema.is(Schema.String.check(Schema.isPattern(/^(0|[1-9][0-9]*)$/)));
|
|
389
|
+
const creationAttributes = Effect.fnUntraced(function* (create, limits, existing) {
|
|
390
|
+
const attributes = attributesIn(create.attrs.bitmap);
|
|
391
|
+
const supported = create.mode === 3 ? [4, 33, 36, 37] : [4, 33, 36, 37, 48, 54];
|
|
392
|
+
const supportedAttrs = attributes.every((attribute) => supported.includes(attribute));
|
|
393
|
+
if (!supportedAttrs && !(existing && create.mode === 0)) {
|
|
394
|
+
return yield* Effect.fail(create.mode === 3 ? Status.INVAL : Status.ATTRNOTSUPP);
|
|
395
|
+
}
|
|
396
|
+
const reader = new Reader(create.attrs.values, limits);
|
|
397
|
+
let mode = create.mode === 2 ? 0o600 : undefined;
|
|
398
|
+
let size;
|
|
399
|
+
const owner = {};
|
|
400
|
+
const times = {
|
|
401
|
+
access: { kind: "omit" },
|
|
402
|
+
modification: { kind: "omit" }
|
|
403
|
+
};
|
|
404
|
+
for (const attribute of attributes) {
|
|
405
|
+
// Unknown ignored attributes have no decoded width. Size can still be read
|
|
406
|
+
// when it precedes them, matching ordinary creation's existing-file behavior.
|
|
407
|
+
if (!supported.includes(attribute))
|
|
408
|
+
break;
|
|
409
|
+
if (attribute === 4)
|
|
410
|
+
size = reader.uint64();
|
|
411
|
+
else if (attribute === 33) {
|
|
412
|
+
const value = reader.uint32();
|
|
413
|
+
if (!existing) {
|
|
414
|
+
if (value > 0o7777)
|
|
415
|
+
return yield* Effect.fail(Status.INVAL);
|
|
416
|
+
mode = value;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
else if (attribute === 36 || attribute === 37) {
|
|
420
|
+
const value = reader.string(limits.maxStringBytes);
|
|
421
|
+
if (existing)
|
|
422
|
+
continue;
|
|
423
|
+
if (!isNumericOwner(value) || Number(value) > 0xffff_ffff)
|
|
424
|
+
return yield* Effect.fail(Status.BADOWNER);
|
|
425
|
+
if (attribute === 36)
|
|
426
|
+
owner.uid = Number(value);
|
|
427
|
+
else
|
|
428
|
+
owner.gid = Number(value);
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
const how = reader.uint32();
|
|
432
|
+
const time = how === 0 ? { kind: "now" } : {
|
|
433
|
+
kind: "value",
|
|
434
|
+
nanoseconds: BigInt.asIntN(64, reader.uint64()) * 1000000000n + BigInt(reader.uint32())
|
|
435
|
+
};
|
|
436
|
+
if (attribute === 48)
|
|
437
|
+
times.access = time;
|
|
438
|
+
else
|
|
439
|
+
times.modification = time;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
if (supportedAttrs)
|
|
443
|
+
reader.finish();
|
|
444
|
+
if (create.mode >= 2) {
|
|
445
|
+
// RFC 8881 18.16.4 permits timestamp storage. Both verifier halves enter
|
|
446
|
+
// the creation candidate so a crash cannot separate the file from its verifier.
|
|
447
|
+
const verifier = new Reader(create.verifier, limits);
|
|
448
|
+
times.access = { kind: "value", nanoseconds: BigInt(verifier.uint32()) * 1000000000n };
|
|
449
|
+
times.modification = { kind: "value", nanoseconds: BigInt(verifier.uint32()) * 1000000000n };
|
|
450
|
+
}
|
|
451
|
+
const settings = { times };
|
|
452
|
+
if (mode !== undefined) {
|
|
453
|
+
settings.mode = mode;
|
|
454
|
+
settings.exactMode = true;
|
|
455
|
+
}
|
|
456
|
+
if (size !== undefined)
|
|
457
|
+
settings.initialSize = size;
|
|
458
|
+
if (Object.keys(owner).length !== 0)
|
|
459
|
+
settings.owner = owner;
|
|
460
|
+
return { attributes, settings };
|
|
461
|
+
});
|
|
462
|
+
const setattrAttributes = Effect.fnUntraced(function* (attrs, limits) {
|
|
463
|
+
const attributes = attributesIn(attrs.bitmap);
|
|
464
|
+
if (attributes.some((attribute) => ![4, 33, 36, 37, 48, 54].includes(attribute))) {
|
|
465
|
+
return yield* Effect.fail(Status.ATTRNOTSUPP);
|
|
466
|
+
}
|
|
467
|
+
const reader = new Reader(attrs.values, limits);
|
|
468
|
+
const changes = [];
|
|
469
|
+
for (const attribute of attributes) {
|
|
470
|
+
if (attribute === 4)
|
|
471
|
+
changes.push({ kind: "size", attribute, value: reader.uint64() });
|
|
472
|
+
else if (attribute === 33) {
|
|
473
|
+
const value = reader.uint32();
|
|
474
|
+
if (value > 0o7777)
|
|
475
|
+
return yield* Effect.fail(Status.INVAL);
|
|
476
|
+
changes.push({ kind: "mode", attribute, value });
|
|
477
|
+
}
|
|
478
|
+
else if (attribute === 36 || attribute === 37) {
|
|
479
|
+
const value = reader.string(limits.maxStringBytes);
|
|
480
|
+
if (!isNumericOwner(value) || Number(value) > 0xffff_ffff)
|
|
481
|
+
return yield* Effect.fail(Status.BADOWNER);
|
|
482
|
+
changes.push({ kind: "owner", attribute, value: Number(value) });
|
|
483
|
+
}
|
|
484
|
+
else if (attribute === 48 || attribute === 54) {
|
|
485
|
+
const how = reader.uint32();
|
|
486
|
+
const value = how === 0 ? { kind: "now" } : {
|
|
487
|
+
kind: "value",
|
|
488
|
+
nanoseconds: BigInt.asIntN(64, reader.uint64()) * 1000000000n + BigInt(reader.uint32())
|
|
489
|
+
};
|
|
490
|
+
changes.push({ kind: "time", attribute, value });
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
reader.finish();
|
|
494
|
+
return changes;
|
|
495
|
+
});
|
|
496
|
+
const readStateOwner = (reader, limits) => {
|
|
497
|
+
reader.uint64();
|
|
498
|
+
return reader.opaque(limits.maxOwnerBytes);
|
|
499
|
+
};
|
|
500
|
+
const readChannelAttrs = (reader) => ({
|
|
501
|
+
headerPadding: reader.uint32(),
|
|
502
|
+
maxRequest: reader.uint32(),
|
|
503
|
+
maxResponse: reader.uint32(),
|
|
504
|
+
maxCachedResponse: reader.uint32(),
|
|
505
|
+
maxOperations: reader.uint32(),
|
|
506
|
+
maxRequests: reader.uint32(),
|
|
507
|
+
rdmaIrd: reader.array((item) => item.uint32(), 1)
|
|
508
|
+
});
|
|
509
|
+
const readCallbackSecurity = (reader, limits) => {
|
|
510
|
+
const flavor = reader.uint32();
|
|
511
|
+
if (flavor === 0)
|
|
512
|
+
return { flavor };
|
|
513
|
+
if (flavor === 1) {
|
|
514
|
+
const stamp = reader.uint32();
|
|
515
|
+
const machineName = reader.string(limits.maxStringBytes);
|
|
516
|
+
const uid = reader.uint32();
|
|
517
|
+
const gid = reader.uint32();
|
|
518
|
+
const groups = reader.array((item) => item.uint32(), limits.maxArrayElements);
|
|
519
|
+
return {
|
|
520
|
+
flavor,
|
|
521
|
+
credential: new Writer().uint32(stamp).string(machineName).uint32(uid).uint32(gid)
|
|
522
|
+
.array(groups, (item, group) => item.uint32(group))
|
|
523
|
+
.bytes()
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
if (flavor === 6) {
|
|
527
|
+
reader.uint32();
|
|
528
|
+
reader.opaque(limits.maxOpaqueBytes);
|
|
529
|
+
reader.opaque(limits.maxOpaqueBytes);
|
|
530
|
+
return { flavor };
|
|
531
|
+
}
|
|
532
|
+
throw new XdrDecodeError("Unsupported callback security flavor");
|
|
533
|
+
};
|
|
534
|
+
/**
|
|
535
|
+
* Picks the credential a callback will carry. AUTH_NONE is preferred when the client offered it,
|
|
536
|
+
* since it needs no identity; otherwise the client's own AUTH_SYS credential is used. A client
|
|
537
|
+
* that offered neither has authorized nothing this server can encode, and gets no callbacks.
|
|
538
|
+
*
|
|
539
|
+
* An AUTH_SYS credential is only usable if it fits RFC 5531's 400-byte `opaque_auth` limit. The
|
|
540
|
+
* callback parameters are decoded with the compound's own XDR limits, which are far looser, so an
|
|
541
|
+
* offered credential can be well-formed here and still be unsendable; that entry is skipped
|
|
542
|
+
* rather than emitted as a malformed callback.
|
|
543
|
+
*/
|
|
544
|
+
const chooseCallbackSecurity = (offered) => offered.find((entry) => entry.flavor === AUTH_NONE) ??
|
|
545
|
+
offered.find((entry) => entry.flavor === AUTH_SYS && (entry.credential?.length ?? 0) <= MAX_OPAQUE_AUTH_BYTES);
|
|
546
|
+
const writeChannelAttrs = (writer, attrs) => {
|
|
547
|
+
writer.uint32(attrs.headerPadding).uint32(attrs.maxRequest).uint32(attrs.maxResponse)
|
|
548
|
+
.uint32(attrs.maxCachedResponse).uint32(attrs.maxOperations).uint32(attrs.maxRequests)
|
|
549
|
+
.array(attrs.rdmaIrd, (item, value) => item.uint32(value));
|
|
550
|
+
};
|
|
551
|
+
/** state_protect_ops4: the operations a client wants enforced and allowed under the protection. */
|
|
552
|
+
const readStateProtectOps = (reader, limits) => {
|
|
553
|
+
bitmap(reader, limits.maxBitmapWords);
|
|
554
|
+
bitmap(reader, limits.maxBitmapWords);
|
|
555
|
+
};
|
|
556
|
+
const readStateProtection = (reader, limits) => {
|
|
557
|
+
const how = reader.uint32();
|
|
558
|
+
if (how === SP4_MACH_CRED) {
|
|
559
|
+
readStateProtectOps(reader, limits);
|
|
560
|
+
}
|
|
561
|
+
else if (how === SP4_SSV) {
|
|
562
|
+
// ssv_sp_parms4: ops, hash and encryption algorithm lists, window, and GSS handle count.
|
|
563
|
+
readStateProtectOps(reader, limits);
|
|
564
|
+
reader.array((item) => item.opaque(limits.maxOpaqueBytes), limits.maxArrayElements);
|
|
565
|
+
reader.array((item) => item.opaque(limits.maxOpaqueBytes), limits.maxArrayElements);
|
|
566
|
+
reader.uint32();
|
|
567
|
+
reader.uint32();
|
|
568
|
+
}
|
|
569
|
+
else if (how !== SP4_NONE) {
|
|
570
|
+
throw new XdrDecodeError("Invalid state protection discriminant");
|
|
571
|
+
}
|
|
572
|
+
return how;
|
|
573
|
+
};
|
|
574
|
+
const readLockType = (reader) => {
|
|
575
|
+
const lockType = reader.uint32();
|
|
576
|
+
if (lockType < 1 || lockType > 4)
|
|
577
|
+
throw new XdrDecodeError("Invalid lock type");
|
|
578
|
+
return lockType;
|
|
579
|
+
};
|
|
580
|
+
const decodeOperation = (code, reader, limits) => {
|
|
581
|
+
switch (code) {
|
|
582
|
+
case Operation.ACCESS:
|
|
583
|
+
return { kind: "Access", code, value: reader.uint32() };
|
|
584
|
+
case Operation.CLOSE:
|
|
585
|
+
return { kind: "Close", code, value: { sequence: reader.uint32(), stateid: reader.fixedOpaque(16) } };
|
|
586
|
+
case Operation.CREATE: {
|
|
587
|
+
const kind = reader.uint32();
|
|
588
|
+
const target = kind === 5 ? reader.string(limits.maxStringBytes) : undefined;
|
|
589
|
+
if (kind === 3 || kind === 4) {
|
|
590
|
+
reader.uint32();
|
|
591
|
+
reader.uint32();
|
|
592
|
+
}
|
|
593
|
+
const name = reader.opaque(limits.maxOpaqueBytes);
|
|
594
|
+
const attrs = readAttributes(reader, limits);
|
|
595
|
+
return { kind: "Create", code, value: { kind, name, attrs, target } };
|
|
596
|
+
}
|
|
597
|
+
case Operation.GETATTR:
|
|
598
|
+
return { kind: "Getattr", code, value: bitmap(reader, limits.maxBitmapWords) };
|
|
599
|
+
case Operation.GETFH:
|
|
600
|
+
return { kind: "Getfh", code, value: undefined };
|
|
601
|
+
case Operation.LOOKUPP:
|
|
602
|
+
return { kind: "Lookupp", code, value: undefined };
|
|
603
|
+
case Operation.PUTROOTFH:
|
|
604
|
+
return { kind: "Putrootfh", code, value: undefined };
|
|
605
|
+
case Operation.READLINK:
|
|
606
|
+
return { kind: "Readlink", code, value: undefined };
|
|
607
|
+
case Operation.RESTOREFH:
|
|
608
|
+
return { kind: "Restorefh", code, value: undefined };
|
|
609
|
+
case Operation.SAVEFH:
|
|
610
|
+
return { kind: "Savefh", code, value: undefined };
|
|
611
|
+
case Operation.LINK:
|
|
612
|
+
return { kind: "Link", code, value: reader.opaque(limits.maxOpaqueBytes) };
|
|
613
|
+
case Operation.LOOKUP:
|
|
614
|
+
return { kind: "Lookup", code, value: reader.opaque(limits.maxOpaqueBytes) };
|
|
615
|
+
case Operation.REMOVE:
|
|
616
|
+
return { kind: "Remove", code, value: reader.opaque(limits.maxOpaqueBytes) };
|
|
617
|
+
case Operation.OPEN: {
|
|
618
|
+
const sequence = reader.uint32();
|
|
619
|
+
const access = reader.uint32();
|
|
620
|
+
const deny = reader.uint32();
|
|
621
|
+
const client = reader.uint64();
|
|
622
|
+
const owner = reader.opaque(limits.maxOwnerBytes);
|
|
623
|
+
const openHow = reader.uint32();
|
|
624
|
+
let create;
|
|
625
|
+
if (openHow === 1) {
|
|
626
|
+
const createMode = reader.uint32();
|
|
627
|
+
let verifier = empty;
|
|
628
|
+
let attrs = { bitmap: [], values: empty };
|
|
629
|
+
if (createMode === 0 || createMode === 1) {
|
|
630
|
+
attrs = readAttributes(reader, limits);
|
|
631
|
+
}
|
|
632
|
+
else if (createMode === 2) {
|
|
633
|
+
verifier = reader.fixedOpaque(8);
|
|
634
|
+
}
|
|
635
|
+
else if (createMode === 3) {
|
|
636
|
+
verifier = reader.fixedOpaque(8);
|
|
637
|
+
attrs = readAttributes(reader, limits);
|
|
638
|
+
}
|
|
639
|
+
else {
|
|
640
|
+
throw new XdrDecodeError("Invalid OPEN create mode");
|
|
641
|
+
}
|
|
642
|
+
create = { mode: createMode, verifier, attrs };
|
|
643
|
+
}
|
|
644
|
+
else if (openHow !== 0) {
|
|
645
|
+
throw new XdrDecodeError("Invalid OPEN how discriminant");
|
|
646
|
+
}
|
|
647
|
+
const claim = reader.uint32();
|
|
648
|
+
let name = empty;
|
|
649
|
+
if (claim === 0 || claim === 3)
|
|
650
|
+
name = reader.opaque(limits.maxOpaqueBytes);
|
|
651
|
+
else if (claim === 1)
|
|
652
|
+
reader.uint32();
|
|
653
|
+
else if (claim === 2) {
|
|
654
|
+
reader.fixedOpaque(16);
|
|
655
|
+
name = reader.opaque(limits.maxOpaqueBytes);
|
|
656
|
+
}
|
|
657
|
+
else if (claim === 5)
|
|
658
|
+
reader.fixedOpaque(16);
|
|
659
|
+
else if (claim !== 4 && claim !== 6)
|
|
660
|
+
throw new XdrDecodeError("Invalid OPEN claim");
|
|
661
|
+
return { kind: "Open", code, value: { sequence, access, deny, client, owner, openHow, create, claim, name } };
|
|
662
|
+
}
|
|
663
|
+
case Operation.PUTFH:
|
|
664
|
+
return { kind: "Putfh", code, value: reader.opaque(limits.maxOpaqueBytes) };
|
|
665
|
+
case Operation.READ:
|
|
666
|
+
return {
|
|
667
|
+
code,
|
|
668
|
+
kind: "Read",
|
|
669
|
+
value: { stateid: reader.fixedOpaque(16), offset: reader.uint64(), count: reader.uint32() }
|
|
670
|
+
};
|
|
671
|
+
case Operation.READDIR:
|
|
672
|
+
return {
|
|
673
|
+
code,
|
|
674
|
+
kind: "Readdir",
|
|
675
|
+
value: {
|
|
676
|
+
cookie: reader.uint64(),
|
|
677
|
+
verifier: reader.fixedOpaque(8),
|
|
678
|
+
dircount: reader.uint32(),
|
|
679
|
+
maxcount: reader.uint32(),
|
|
680
|
+
attrs: bitmap(reader, limits.maxBitmapWords)
|
|
681
|
+
}
|
|
682
|
+
};
|
|
683
|
+
case Operation.RENAME:
|
|
684
|
+
return {
|
|
685
|
+
code,
|
|
686
|
+
kind: "Rename",
|
|
687
|
+
value: { oldName: reader.opaque(limits.maxOpaqueBytes), newName: reader.opaque(limits.maxOpaqueBytes) }
|
|
688
|
+
};
|
|
689
|
+
case Operation.SETATTR:
|
|
690
|
+
return {
|
|
691
|
+
code,
|
|
692
|
+
kind: "Setattr",
|
|
693
|
+
value: {
|
|
694
|
+
stateid: reader.fixedOpaque(16),
|
|
695
|
+
attrs: readAttributes(reader, limits)
|
|
696
|
+
}
|
|
697
|
+
};
|
|
698
|
+
case Operation.WRITE:
|
|
699
|
+
return {
|
|
700
|
+
code,
|
|
701
|
+
kind: "Write",
|
|
702
|
+
value: {
|
|
703
|
+
stateid: reader.fixedOpaque(16),
|
|
704
|
+
offset: reader.uint64(),
|
|
705
|
+
stable: reader.uint32(),
|
|
706
|
+
data: reader.opaque(limits.maxWriteBytes)
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
case Operation.EXCHANGE_ID: {
|
|
710
|
+
const verifier = reader.fixedOpaque(8);
|
|
711
|
+
const owner = reader.opaque(limits.maxOwnerBytes);
|
|
712
|
+
const flags = reader.uint32();
|
|
713
|
+
const protection = readStateProtection(reader, limits);
|
|
714
|
+
// eia_client_impl_id<1>: at most one implementation record (RFC 5662).
|
|
715
|
+
reader.array((item) => {
|
|
716
|
+
item.string(limits.maxStringBytes);
|
|
717
|
+
item.string(limits.maxStringBytes);
|
|
718
|
+
item.uint64();
|
|
719
|
+
item.uint32();
|
|
720
|
+
}, 1);
|
|
721
|
+
return { kind: "ExchangeId", code, value: { verifier, owner, flags, protection } };
|
|
722
|
+
}
|
|
723
|
+
case Operation.CREATE_SESSION: {
|
|
724
|
+
const client = reader.uint64();
|
|
725
|
+
const sequence = reader.uint32();
|
|
726
|
+
const flags = reader.uint32();
|
|
727
|
+
const fore = readChannelAttrs(reader);
|
|
728
|
+
const back = readChannelAttrs(reader);
|
|
729
|
+
const callbackProgram = reader.uint32();
|
|
730
|
+
const flavors = reader.array((item) => readCallbackSecurity(item, limits), limits.maxArrayElements);
|
|
731
|
+
return {
|
|
732
|
+
kind: "CreateSession",
|
|
733
|
+
code,
|
|
734
|
+
value: {
|
|
735
|
+
client,
|
|
736
|
+
sequence,
|
|
737
|
+
flags,
|
|
738
|
+
fore,
|
|
739
|
+
back,
|
|
740
|
+
callbackProgram,
|
|
741
|
+
security: flavors,
|
|
742
|
+
gssCallback: flavors.some((entry) => entry.flavor === RPCSEC_GSS)
|
|
743
|
+
}
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
case Operation.DESTROY_SESSION:
|
|
747
|
+
return { kind: "DestroySession", code, value: reader.fixedOpaque(16) };
|
|
748
|
+
case Operation.SEQUENCE:
|
|
749
|
+
return {
|
|
750
|
+
code,
|
|
751
|
+
kind: "Sequence",
|
|
752
|
+
value: {
|
|
753
|
+
session: reader.fixedOpaque(16),
|
|
754
|
+
sequence: reader.uint32(),
|
|
755
|
+
slot: reader.uint32(),
|
|
756
|
+
highest: reader.uint32(),
|
|
757
|
+
cache: reader.boolean()
|
|
758
|
+
}
|
|
759
|
+
};
|
|
760
|
+
case Operation.DESTROY_CLIENTID:
|
|
761
|
+
return { kind: "DestroyClient", code, value: reader.uint64() };
|
|
762
|
+
case Operation.RECLAIM_COMPLETE:
|
|
763
|
+
return { kind: "ReclaimComplete", code, value: reader.boolean() };
|
|
764
|
+
case Operation.COMMIT:
|
|
765
|
+
return { kind: "Commit", code, value: { offset: reader.uint64(), count: reader.uint32() } };
|
|
766
|
+
case Operation.LOCK: {
|
|
767
|
+
const lockType = readLockType(reader);
|
|
768
|
+
const reclaim = reader.boolean();
|
|
769
|
+
const offset = reader.uint64();
|
|
770
|
+
const length = reader.uint64();
|
|
771
|
+
let locker;
|
|
772
|
+
if (reader.boolean()) {
|
|
773
|
+
reader.uint32();
|
|
774
|
+
const stateid = reader.fixedOpaque(16);
|
|
775
|
+
reader.uint32();
|
|
776
|
+
locker = { kind: "new", stateid, owner: readStateOwner(reader, limits) };
|
|
777
|
+
}
|
|
778
|
+
else {
|
|
779
|
+
const stateid = reader.fixedOpaque(16);
|
|
780
|
+
reader.uint32();
|
|
781
|
+
locker = { kind: "existing", stateid };
|
|
782
|
+
}
|
|
783
|
+
return { kind: "Lock", code, value: { lockType, reclaim, offset, length, locker } };
|
|
784
|
+
}
|
|
785
|
+
case Operation.LOCKT: {
|
|
786
|
+
const lockType = readLockType(reader);
|
|
787
|
+
const offset = reader.uint64();
|
|
788
|
+
const length = reader.uint64();
|
|
789
|
+
const owner = readStateOwner(reader, limits);
|
|
790
|
+
return { kind: "Lockt", code, value: { lockType, offset, length, owner } };
|
|
791
|
+
}
|
|
792
|
+
case Operation.LOCKU: {
|
|
793
|
+
readLockType(reader);
|
|
794
|
+
reader.uint32();
|
|
795
|
+
const stateid = reader.fixedOpaque(16);
|
|
796
|
+
const offset = reader.uint64();
|
|
797
|
+
const length = reader.uint64();
|
|
798
|
+
return { kind: "Locku", code, value: { stateid, offset, length } };
|
|
799
|
+
}
|
|
800
|
+
case Operation.NVERIFY:
|
|
801
|
+
case Operation.VERIFY:
|
|
802
|
+
return {
|
|
803
|
+
kind: "Verify",
|
|
804
|
+
code,
|
|
805
|
+
value: { bitmap: bitmap(reader, limits.maxBitmapWords), values: reader.opaque(limits.maxOpaqueBytes) }
|
|
806
|
+
};
|
|
807
|
+
case Operation.OPEN_DOWNGRADE:
|
|
808
|
+
return {
|
|
809
|
+
kind: "OpenDowngrade",
|
|
810
|
+
code,
|
|
811
|
+
value: {
|
|
812
|
+
stateid: reader.fixedOpaque(16),
|
|
813
|
+
sequence: reader.uint32(),
|
|
814
|
+
access: reader.uint32(),
|
|
815
|
+
deny: reader.uint32()
|
|
816
|
+
}
|
|
817
|
+
};
|
|
818
|
+
case Operation.PUTPUBFH:
|
|
819
|
+
return { kind: "Putpubfh", code, value: undefined };
|
|
820
|
+
case Operation.SECINFO:
|
|
821
|
+
return { kind: "Secinfo", code, value: reader.opaque(limits.maxOpaqueBytes) };
|
|
822
|
+
case Operation.SECINFO_NO_NAME: {
|
|
823
|
+
const style = reader.uint32();
|
|
824
|
+
if (style !== SECINFO_STYLE4_CURRENT_FH && style !== SECINFO_STYLE4_PARENT) {
|
|
825
|
+
throw new XdrDecodeError("Invalid SECINFO_NO_NAME style");
|
|
826
|
+
}
|
|
827
|
+
return { kind: "SecinfoNoName", code, value: style };
|
|
828
|
+
}
|
|
829
|
+
case Operation.FREE_STATEID:
|
|
830
|
+
return { kind: "FreeStateid", code, value: reader.fixedOpaque(16) };
|
|
831
|
+
case Operation.BACKCHANNEL_CTL: {
|
|
832
|
+
const program = reader.uint32();
|
|
833
|
+
const flavors = reader.array((item) => readCallbackSecurity(item, limits), limits.maxArrayElements);
|
|
834
|
+
return {
|
|
835
|
+
kind: "BackchannelCtl",
|
|
836
|
+
code,
|
|
837
|
+
value: { program, security: flavors, gssCallback: flavors.some((entry) => entry.flavor === RPCSEC_GSS) }
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
case Operation.BIND_CONN_TO_SESSION: {
|
|
841
|
+
const session = reader.fixedOpaque(16);
|
|
842
|
+
const direction = reader.uint32();
|
|
843
|
+
// channel_dir_from_client4: FORE (1), BACK (2), FORE_OR_BOTH (3), or BACK_OR_BOTH (7).
|
|
844
|
+
if (direction < CDFC4_FORE || (direction > CDFC4_FORE_OR_BOTH && direction !== CDFC4_BACK_OR_BOTH)) {
|
|
845
|
+
throw new XdrDecodeError("Invalid channel direction");
|
|
846
|
+
}
|
|
847
|
+
reader.boolean();
|
|
848
|
+
return { kind: "BindConnToSession", code, value: { session, direction } };
|
|
849
|
+
}
|
|
850
|
+
case Operation.SET_SSV:
|
|
851
|
+
reader.opaque(limits.maxOpaqueBytes);
|
|
852
|
+
reader.opaque(limits.maxOpaqueBytes);
|
|
853
|
+
return { kind: "SetSsv", code, value: undefined };
|
|
854
|
+
case Operation.TEST_STATEID:
|
|
855
|
+
return {
|
|
856
|
+
kind: "TestStateid",
|
|
857
|
+
code,
|
|
858
|
+
value: reader.array((item) => item.fixedOpaque(16), limits.maxArrayElements)
|
|
859
|
+
};
|
|
860
|
+
default:
|
|
861
|
+
if (mustNotImplementOperations.has(code) || unsupportedOptionalOperations.has(code)) {
|
|
862
|
+
return { kind: "NotSupported", code, value: undefined };
|
|
863
|
+
}
|
|
864
|
+
return { kind: "Unknown", code, value: undefined };
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
const parseCompound = (bytes, limits) => {
|
|
868
|
+
if (byteLength(bytes.length) > limits.maxCompoundBytes)
|
|
869
|
+
throw new XdrDecodeError("COMPOUND exceeds byte limit");
|
|
870
|
+
const reader = new Reader(bytes, limits);
|
|
871
|
+
const tag = reader.opaque(limits.maxStringBytes);
|
|
872
|
+
const minor = reader.uint32();
|
|
873
|
+
const count = reader.uint32();
|
|
874
|
+
if (count > limits.maxOperations || count > limits.maxArrayElements) {
|
|
875
|
+
throw new XdrDecodeError("COMPOUND operation count exceeds its limit");
|
|
876
|
+
}
|
|
877
|
+
const operations = [];
|
|
878
|
+
let stopped = false;
|
|
879
|
+
for (let index = 0; index < count; index++) {
|
|
880
|
+
const code = reader.uint32();
|
|
881
|
+
const operation = decodeOperation_(code);
|
|
882
|
+
operations.push(operation);
|
|
883
|
+
// The compound fails at an undecoded operation, so later bytes are never interpreted.
|
|
884
|
+
if (operation.kind === "Unknown" || operation.kind === "NotSupported" || operation.kind === "Malformed") {
|
|
885
|
+
stopped = true;
|
|
886
|
+
break;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
if (!stopped)
|
|
890
|
+
reader.finish();
|
|
891
|
+
return { tag, minor, count, operations };
|
|
892
|
+
/**
|
|
893
|
+
* Section 15.1.1.1 defines NFS4ERR_BADXDR per operation: the operations before a
|
|
894
|
+
* malformed one are still processed and reported.
|
|
895
|
+
*/
|
|
896
|
+
function decodeOperation_(code) {
|
|
897
|
+
try {
|
|
898
|
+
return decodeOperation(code, reader, limits);
|
|
899
|
+
}
|
|
900
|
+
catch (error) {
|
|
901
|
+
if (!(error instanceof XdrDecodeError))
|
|
902
|
+
throw error;
|
|
903
|
+
return { kind: "Malformed", code, value: undefined };
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
};
|
|
907
|
+
const encodeCompound = (tag, parts, overallStatus = parts.find((part) => part.status !== Status.OK)?.status ?? Status.OK) => {
|
|
908
|
+
const status = overallStatus;
|
|
909
|
+
const writer = new Writer().uint32(status).opaque(tag).uint32(parts.length);
|
|
910
|
+
for (const part of parts) {
|
|
911
|
+
writer.uint32(part.code).uint32(part.status);
|
|
912
|
+
if (part.body !== undefined)
|
|
913
|
+
writer.fixedOpaque(part.body);
|
|
914
|
+
}
|
|
915
|
+
return writer.bytes();
|
|
916
|
+
};
|
|
917
|
+
const fsStatuses = {
|
|
918
|
+
NotFound: Status.NOENT,
|
|
919
|
+
AlreadyExists: Status.EXIST,
|
|
920
|
+
NotEmpty: Status.NOTEMPTY,
|
|
921
|
+
NotDirectory: Status.NOTDIR,
|
|
922
|
+
AccessDenied: Status.ACCESS,
|
|
923
|
+
InvalidHandle: Status.SERVERFAULT,
|
|
924
|
+
ForeignHandle: Status.SERVERFAULT,
|
|
925
|
+
InvalidReference: Status.SERVERFAULT,
|
|
926
|
+
ForeignReference: Status.SERVERFAULT,
|
|
927
|
+
StaleReference: Status.STALE,
|
|
928
|
+
ClosedCaller: Status.SERVERFAULT,
|
|
929
|
+
InvalidArgument: Status.INVAL,
|
|
930
|
+
InvalidPathEncoding: Status.INVAL,
|
|
931
|
+
PathTooLong: Status.NAMETOOLONG,
|
|
932
|
+
NoSpace: Status.NOSPC,
|
|
933
|
+
IsDirectory: Status.ISDIR,
|
|
934
|
+
FileTooLarge: Status.FBIG,
|
|
935
|
+
NoData: Status.SERVERFAULT,
|
|
936
|
+
StorageRejected: Status.IO,
|
|
937
|
+
OutcomeUnknown: Status.IO,
|
|
938
|
+
VolumeUnavailable: Status.IO,
|
|
939
|
+
VolumeBusy: Status.DELAY,
|
|
940
|
+
// NFSv4.1 has no LOOP status. The reference-based NFS read path never follows symbolic links.
|
|
941
|
+
SymlinkLoop: Status.INVAL,
|
|
942
|
+
UnrepresentableName: Status.INVAL
|
|
943
|
+
};
|
|
944
|
+
/** @internal */
|
|
945
|
+
export const failureForFs = (error) => Object.hasOwn(fsStatuses, error.code) ? fsStatuses[error.code] : Status.SERVERFAULT;
|
|
946
|
+
const encodeStatusBody = (build) => {
|
|
947
|
+
const writer = new Writer();
|
|
948
|
+
build(writer);
|
|
949
|
+
return writer.bytes();
|
|
950
|
+
};
|
|
951
|
+
const baseSupportedAttributes = [
|
|
952
|
+
0,
|
|
953
|
+
1,
|
|
954
|
+
2,
|
|
955
|
+
3,
|
|
956
|
+
4,
|
|
957
|
+
5,
|
|
958
|
+
6,
|
|
959
|
+
7,
|
|
960
|
+
8,
|
|
961
|
+
9,
|
|
962
|
+
10,
|
|
963
|
+
11,
|
|
964
|
+
16,
|
|
965
|
+
17,
|
|
966
|
+
19,
|
|
967
|
+
20,
|
|
968
|
+
26,
|
|
969
|
+
29,
|
|
970
|
+
30,
|
|
971
|
+
31,
|
|
972
|
+
33,
|
|
973
|
+
34,
|
|
974
|
+
35,
|
|
975
|
+
36,
|
|
976
|
+
37,
|
|
977
|
+
45,
|
|
978
|
+
47,
|
|
979
|
+
51,
|
|
980
|
+
52,
|
|
981
|
+
53,
|
|
982
|
+
55,
|
|
983
|
+
75,
|
|
984
|
+
76
|
|
985
|
+
];
|
|
986
|
+
const maxUint64 = 0xffffffffffffffffn;
|
|
987
|
+
const supportedAttributesFor = (export_) => {
|
|
988
|
+
const capacity = export_.capacity;
|
|
989
|
+
if (capacity === undefined)
|
|
990
|
+
return baseSupportedAttributes;
|
|
991
|
+
const { maxBytes, maxEntries } = capacity.limits;
|
|
992
|
+
const additional = [27];
|
|
993
|
+
if (maxEntries !== undefined)
|
|
994
|
+
additional.push(21, 22, 23);
|
|
995
|
+
if (maxBytes !== undefined && ByteSize.toBigInt(maxBytes) <= maxUint64)
|
|
996
|
+
additional.push(42, 43, 44);
|
|
997
|
+
return [...baseSupportedAttributes, ...additional].sort((left, right) => left - right);
|
|
998
|
+
};
|
|
999
|
+
const capacityAttributes = new Set([21, 22, 23, 42, 43, 44]);
|
|
1000
|
+
/** Attributes that VERIFY and NVERIFY may not compare (RFC 8881 Section 18.31.3). */
|
|
1001
|
+
const nonComparableAttributes = new Set([11, 48, 54]);
|
|
1002
|
+
const wordsFor = (attributes) => {
|
|
1003
|
+
if (attributes.length === 0)
|
|
1004
|
+
return [];
|
|
1005
|
+
const words = Array.from({ length: Math.floor(Math.max(...attributes) / 32) + 1 }).fill(0);
|
|
1006
|
+
for (const attribute of attributes) {
|
|
1007
|
+
const index = Math.floor(attribute / 32);
|
|
1008
|
+
words[index] = (words[index] | (1 << (attribute % 32))) >>> 0;
|
|
1009
|
+
}
|
|
1010
|
+
return words;
|
|
1011
|
+
};
|
|
1012
|
+
const requestedAttributes = (words) => {
|
|
1013
|
+
const result = [];
|
|
1014
|
+
for (let word = 0; word < words.length; word++) {
|
|
1015
|
+
for (let bit = 0; bit < 32; bit++)
|
|
1016
|
+
if (((words[word] >>> bit) & 1) !== 0)
|
|
1017
|
+
result.push(word * 32 + bit);
|
|
1018
|
+
}
|
|
1019
|
+
return result;
|
|
1020
|
+
};
|
|
1021
|
+
const isValidName = (name, maxNameBytes) => {
|
|
1022
|
+
try {
|
|
1023
|
+
validateName(name, maxNameBytes);
|
|
1024
|
+
return true;
|
|
1025
|
+
}
|
|
1026
|
+
catch {
|
|
1027
|
+
return false;
|
|
1028
|
+
}
|
|
1029
|
+
};
|
|
1030
|
+
const encodeTime = (writer, nanoseconds) => {
|
|
1031
|
+
let seconds = nanoseconds / 1000000000n;
|
|
1032
|
+
let nanos = nanoseconds % 1000000000n;
|
|
1033
|
+
if (nanos < 0) {
|
|
1034
|
+
seconds -= 1n;
|
|
1035
|
+
nanos += 1000000000n;
|
|
1036
|
+
}
|
|
1037
|
+
if (seconds < -0x8000000000000000n || seconds > 0x7fffffffffffffffn)
|
|
1038
|
+
return false;
|
|
1039
|
+
writer.uint64(BigInt.asUintN(64, seconds)).uint32(Number(nanos));
|
|
1040
|
+
return true;
|
|
1041
|
+
};
|
|
1042
|
+
const encodeAttributeValues = (requested, observation, filehandle, export_, options, supportedAttributes, usage) => {
|
|
1043
|
+
if (requested.some((attribute) => !supportedAttributes.includes(attribute)))
|
|
1044
|
+
return undefined;
|
|
1045
|
+
const values = new Writer();
|
|
1046
|
+
const metadata = observation.value;
|
|
1047
|
+
for (const attribute of requested) {
|
|
1048
|
+
switch (attribute) {
|
|
1049
|
+
case 0:
|
|
1050
|
+
writeBitmap(values, wordsFor(supportedAttributes));
|
|
1051
|
+
break;
|
|
1052
|
+
case 1:
|
|
1053
|
+
values.uint32(metadata.kind === "file" ? 1 : metadata.kind === "directory" ? 2 : 5);
|
|
1054
|
+
break;
|
|
1055
|
+
case 2:
|
|
1056
|
+
values.uint32(0x3);
|
|
1057
|
+
break;
|
|
1058
|
+
case 3:
|
|
1059
|
+
values.uint64(BigInt.asUintN(64, observation.revision));
|
|
1060
|
+
break;
|
|
1061
|
+
case 4:
|
|
1062
|
+
values.uint64(metadata.size);
|
|
1063
|
+
break;
|
|
1064
|
+
case 5:
|
|
1065
|
+
case 6:
|
|
1066
|
+
case 9:
|
|
1067
|
+
case 17:
|
|
1068
|
+
case 26:
|
|
1069
|
+
case 34:
|
|
1070
|
+
values.boolean(true);
|
|
1071
|
+
break;
|
|
1072
|
+
case 7:
|
|
1073
|
+
case 16:
|
|
1074
|
+
values.boolean(false);
|
|
1075
|
+
break;
|
|
1076
|
+
case 51:
|
|
1077
|
+
values.uint64(0n).uint32(1);
|
|
1078
|
+
break;
|
|
1079
|
+
case 76:
|
|
1080
|
+
values.uint32(FSCHARSET_CAP4_ALLOWS_ONLY_UTF8);
|
|
1081
|
+
break;
|
|
1082
|
+
case 8:
|
|
1083
|
+
values.uint64(export_.fsid[0]).uint64(export_.fsid[1]);
|
|
1084
|
+
break;
|
|
1085
|
+
case 10:
|
|
1086
|
+
values.uint32(options.leaseDurationSeconds);
|
|
1087
|
+
break;
|
|
1088
|
+
case 11:
|
|
1089
|
+
values.uint32(Status.OK);
|
|
1090
|
+
break;
|
|
1091
|
+
case 19:
|
|
1092
|
+
values.opaque(filehandle);
|
|
1093
|
+
break;
|
|
1094
|
+
case 20:
|
|
1095
|
+
case 55:
|
|
1096
|
+
values.uint64(BigInt.asUintN(64, metadata.ino));
|
|
1097
|
+
break;
|
|
1098
|
+
case 21:
|
|
1099
|
+
case 22:
|
|
1100
|
+
case 23: {
|
|
1101
|
+
const total = BigInt(export_.capacity.limits.maxEntries);
|
|
1102
|
+
values.uint64(attribute === 23 ? total : total - BigInt(usage.entries));
|
|
1103
|
+
break;
|
|
1104
|
+
}
|
|
1105
|
+
case 27:
|
|
1106
|
+
values.uint64(ByteSize.toBigInt(export_.capacity.limits.maxFileBytes));
|
|
1107
|
+
break;
|
|
1108
|
+
case 29:
|
|
1109
|
+
values.uint32(ByteSize.toNumberUnsafe(options.limits.maxNameBytes));
|
|
1110
|
+
break;
|
|
1111
|
+
case 30:
|
|
1112
|
+
values.uint64(options.limits.maxReadBytes);
|
|
1113
|
+
break;
|
|
1114
|
+
case 31:
|
|
1115
|
+
values.uint64(options.limits.maxWriteBytes);
|
|
1116
|
+
break;
|
|
1117
|
+
case 33:
|
|
1118
|
+
values.uint32(metadata.mode);
|
|
1119
|
+
break;
|
|
1120
|
+
case 35:
|
|
1121
|
+
values.uint32(metadata.nlink);
|
|
1122
|
+
break;
|
|
1123
|
+
case 36:
|
|
1124
|
+
values.string(String(metadata.uid));
|
|
1125
|
+
break;
|
|
1126
|
+
case 37:
|
|
1127
|
+
values.string(String(metadata.gid));
|
|
1128
|
+
break;
|
|
1129
|
+
case 42:
|
|
1130
|
+
case 43:
|
|
1131
|
+
case 44: {
|
|
1132
|
+
const total = ByteSize.toBigInt(export_.capacity.limits.maxBytes);
|
|
1133
|
+
values.uint64(attribute === 44 ? total : total - usage.usedBytes);
|
|
1134
|
+
break;
|
|
1135
|
+
}
|
|
1136
|
+
case 45:
|
|
1137
|
+
values.uint64(metadata.size);
|
|
1138
|
+
break;
|
|
1139
|
+
case 47:
|
|
1140
|
+
if (!encodeTime(values, metadata.atimeNs))
|
|
1141
|
+
return undefined;
|
|
1142
|
+
break;
|
|
1143
|
+
case 52:
|
|
1144
|
+
if (!encodeTime(values, metadata.ctimeNs))
|
|
1145
|
+
return undefined;
|
|
1146
|
+
break;
|
|
1147
|
+
case 53:
|
|
1148
|
+
if (!encodeTime(values, metadata.mtimeNs))
|
|
1149
|
+
return undefined;
|
|
1150
|
+
break;
|
|
1151
|
+
case 75:
|
|
1152
|
+
writeBitmap(values, options.writable ? wordsFor([4, 33, 36, 37]) : []);
|
|
1153
|
+
break;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
return values.bytes();
|
|
1157
|
+
};
|
|
1158
|
+
const encodeAttributes = (requested, observation, filehandle, export_, options, supportedAttributes, usage) => {
|
|
1159
|
+
const values = encodeAttributeValues(requested, observation, filehandle, export_, options, supportedAttributes, usage);
|
|
1160
|
+
if (values === undefined)
|
|
1161
|
+
return undefined;
|
|
1162
|
+
return encodeStatusBody((writer) => {
|
|
1163
|
+
writeBitmap(writer, wordsFor(requested));
|
|
1164
|
+
writer.opaque(values);
|
|
1165
|
+
});
|
|
1166
|
+
};
|
|
1167
|
+
/** Encodes a READDIR entry's attributes as only `rdattr_error` (RFC 8881 Section 18.23.3). */
|
|
1168
|
+
const encodeReaddirError = (status) => encodeStatusBody((writer) => {
|
|
1169
|
+
writeBitmap(writer, wordsFor([11]));
|
|
1170
|
+
writer.opaque(new Writer().uint32(status).bytes());
|
|
1171
|
+
});
|
|
1172
|
+
const supportedAccessMask = (kind) => {
|
|
1173
|
+
switch (kind) {
|
|
1174
|
+
case "directory":
|
|
1175
|
+
return ACCESS4_READ | ACCESS4_LOOKUP | ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_DELETE;
|
|
1176
|
+
case "file":
|
|
1177
|
+
return ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND | ACCESS4_EXECUTE;
|
|
1178
|
+
default:
|
|
1179
|
+
return ACCESS4_READ | ACCESS4_MODIFY | ACCESS4_EXTEND;
|
|
1180
|
+
}
|
|
1181
|
+
};
|
|
1182
|
+
/**
|
|
1183
|
+
* Evaluates mode bits against the decoded RPC identity for reporting only. The
|
|
1184
|
+
* identity never selects VFS authority; a read-only export grants no write-class
|
|
1185
|
+
* bit regardless of mode.
|
|
1186
|
+
*
|
|
1187
|
+
* In `read-only-local` this answer is advisory: OPEN and READ run through the
|
|
1188
|
+
* single privileged caller and perform no mode check, so a file ACCESS reports
|
|
1189
|
+
* as unreadable is still readable. That asymmetry is the profile's documented
|
|
1190
|
+
* boundary. `read-only-networked` maps the identity to a VFS caller and asks
|
|
1191
|
+
* that caller instead, so ACCESS and OPEN agree by construction.
|
|
1192
|
+
*/
|
|
1193
|
+
const grantedAccess = (supported, metadata, credentials) => {
|
|
1194
|
+
const mode = metadata.mode;
|
|
1195
|
+
const anyExecute = (mode & 0o111) !== 0;
|
|
1196
|
+
let read;
|
|
1197
|
+
let execute;
|
|
1198
|
+
if (Predicate.isTagged(credentials, "Sys") && credentials.uid === 0) {
|
|
1199
|
+
read = true;
|
|
1200
|
+
execute = metadata.kind === "directory" || anyExecute;
|
|
1201
|
+
}
|
|
1202
|
+
else {
|
|
1203
|
+
const shift = Predicate.isTagged(credentials, "Sys") && credentials.uid === metadata.uid
|
|
1204
|
+
? 6
|
|
1205
|
+
: Predicate.isTagged(credentials, "Sys") &&
|
|
1206
|
+
(credentials.gid === metadata.gid || credentials.supplementaryGroups.includes(metadata.gid))
|
|
1207
|
+
? 3
|
|
1208
|
+
: 0;
|
|
1209
|
+
read = ((mode >>> shift) & 0o4) !== 0;
|
|
1210
|
+
execute = ((mode >>> shift) & 0o1) !== 0;
|
|
1211
|
+
}
|
|
1212
|
+
let granted = 0;
|
|
1213
|
+
if (read)
|
|
1214
|
+
granted |= ACCESS4_READ;
|
|
1215
|
+
if (execute)
|
|
1216
|
+
granted |= metadata.kind === "directory" ? ACCESS4_LOOKUP : ACCESS4_EXECUTE;
|
|
1217
|
+
return granted & supported;
|
|
1218
|
+
};
|
|
1219
|
+
/**
|
|
1220
|
+
* Encodes an outbound RPC CALL for the callback program. The version is 1 per RFC 5661 erratum
|
|
1221
|
+
* 2291; the RFC text still prints 4, and a client listening on version 1 ignores anything else.
|
|
1222
|
+
* The credential is the one the client authorized in csa_sec_parms (Section 18.36.3), never a
|
|
1223
|
+
* flavor it did not offer.
|
|
1224
|
+
*/
|
|
1225
|
+
const encodeCallbackCall = (xid, program, procedure, security, body) => {
|
|
1226
|
+
const header = new Writer()
|
|
1227
|
+
.uint32(xid).uint32(0).uint32(2)
|
|
1228
|
+
.uint32(program).uint32(CALLBACK_RPC_VERSION).uint32(procedure)
|
|
1229
|
+
.uint32(security.flavor).opaque(security.credential ?? empty)
|
|
1230
|
+
.uint32(0).opaque(empty)
|
|
1231
|
+
.bytes();
|
|
1232
|
+
const message = new Uint8Array(header.length + body.length);
|
|
1233
|
+
message.set(header);
|
|
1234
|
+
message.set(body, header.length);
|
|
1235
|
+
return message;
|
|
1236
|
+
};
|
|
1237
|
+
/**
|
|
1238
|
+
* A CB_COMPOUND carrying CB_SEQUENCE alone. Section 20.9.3 requires CB_SEQUENCE to appear once and
|
|
1239
|
+
* first in every CB_COMPOUND, and erratum 6015 makes it REQUIRED rather than the OPTIONAL that
|
|
1240
|
+
* Table 17 prints. With no delegations or layouts to recall, this is the whole callback: it
|
|
1241
|
+
* exercises the backchannel and renews nothing else.
|
|
1242
|
+
*/
|
|
1243
|
+
const encodeCallbackSequence = (session, sequence, slot, highestSlot) => new Writer()
|
|
1244
|
+
.string("probe").uint32(1).uint32(0).uint32(1)
|
|
1245
|
+
.uint32(OP_CB_SEQUENCE)
|
|
1246
|
+
.fixedOpaque(session).uint32(sequence).uint32(slot).uint32(highestSlot).boolean(false)
|
|
1247
|
+
.array([], () => undefined)
|
|
1248
|
+
.bytes();
|
|
1249
|
+
/**
|
|
1250
|
+
* Decides whether a callback reply actually says the client handled the callback. An RPC REPLY is
|
|
1251
|
+
* not success on its own: the client's RPC layer answers PROG_UNAVAIL for a program it does not
|
|
1252
|
+
* serve, and AUTH_ERROR for a credential it will not take, both of which are well-formed replies
|
|
1253
|
+
* from a client with no working callback path. The CB_SEQUENCE result is checked too, because
|
|
1254
|
+
* Section 20.9.3 lets the client reject the slot or sequence it was given.
|
|
1255
|
+
*/
|
|
1256
|
+
const callbackAccepted = (reply, limits, session, slot, sequence) => {
|
|
1257
|
+
try {
|
|
1258
|
+
const reader = new Reader(reply, limits);
|
|
1259
|
+
reader.uint32();
|
|
1260
|
+
// RPC: REPLY, MSG_ACCEPTED, verifier, then SUCCESS.
|
|
1261
|
+
if (reader.uint32() !== 1)
|
|
1262
|
+
return false;
|
|
1263
|
+
if (reader.uint32() !== 0)
|
|
1264
|
+
return false;
|
|
1265
|
+
reader.uint32();
|
|
1266
|
+
reader.opaque(ByteSize.bytes(MAX_OPAQUE_AUTH_BYTES));
|
|
1267
|
+
if (reader.uint32() !== 0)
|
|
1268
|
+
return false;
|
|
1269
|
+
// CB_COMPOUND: an all-OK status, then CB_SEQUENCE first (Section 20.9.3).
|
|
1270
|
+
if (reader.uint32() !== Status.OK)
|
|
1271
|
+
return false;
|
|
1272
|
+
reader.string(limits.maxStringBytes);
|
|
1273
|
+
// Exactly one result: this server sends a CB_SEQUENCE-only CB_COMPOUND, so anything else is
|
|
1274
|
+
// not an answer to what it asked.
|
|
1275
|
+
if (reader.uint32() !== 1)
|
|
1276
|
+
return false;
|
|
1277
|
+
if (reader.uint32() !== OP_CB_SEQUENCE)
|
|
1278
|
+
return false;
|
|
1279
|
+
if (reader.uint32() !== Status.OK)
|
|
1280
|
+
return false;
|
|
1281
|
+
// The client echoes what it was given; anything else means it answered a different callback.
|
|
1282
|
+
const echoed = bytesKey(reader.fixedOpaque(16)) === bytesKey(session) &&
|
|
1283
|
+
reader.uint32() === sequence &&
|
|
1284
|
+
reader.uint32() === slot;
|
|
1285
|
+
// csr_highest_slotid and csr_target_highest_slotid are mandatory, and nothing may follow the
|
|
1286
|
+
// one result. Reading them out and finishing rejects a truncated or padded reply, which would
|
|
1287
|
+
// otherwise pass as a working callback path.
|
|
1288
|
+
reader.uint32();
|
|
1289
|
+
reader.uint32();
|
|
1290
|
+
reader.finish();
|
|
1291
|
+
return echoed;
|
|
1292
|
+
}
|
|
1293
|
+
catch (error) {
|
|
1294
|
+
if (error instanceof XdrDecodeError)
|
|
1295
|
+
return false;
|
|
1296
|
+
throw error;
|
|
1297
|
+
}
|
|
1298
|
+
};
|
|
1299
|
+
const makeOpaqueId = (generation, serial) => {
|
|
1300
|
+
const result = new Uint8Array(16);
|
|
1301
|
+
result.set(generation.subarray(0, 8), 0);
|
|
1302
|
+
new DataView(result.buffer).setBigUint64(8, serial);
|
|
1303
|
+
return result;
|
|
1304
|
+
};
|
|
1305
|
+
const makeStateId = (generation, serial, sequence) => {
|
|
1306
|
+
const id = new Uint8Array(16);
|
|
1307
|
+
new DataView(id.buffer).setUint32(0, sequence);
|
|
1308
|
+
id.set(generation.subarray(0, 4), 4);
|
|
1309
|
+
new DataView(id.buffer).setBigUint64(8, serial);
|
|
1310
|
+
return id;
|
|
1311
|
+
};
|
|
1312
|
+
const stateIdKey = (stateid) => bytesKey(stateid.subarray(4));
|
|
1313
|
+
const stateIdSequence = (stateid) => new DataView(stateid.buffer, stateid.byteOffset, stateid.byteLength).getUint32(0);
|
|
1314
|
+
const isAllZero = (bytes) => bytes.every((byte) => byte === 0);
|
|
1315
|
+
const isAllOnes = (bytes) => bytes.every((byte) => byte === 0xff);
|
|
1316
|
+
const isCurrentStateId = (stateid) => stateIdSequence(stateid) === 1 && isAllZero(stateid.subarray(4));
|
|
1317
|
+
const makeCookieVerifier = (generation, revision) => {
|
|
1318
|
+
const result = generation.slice(0, 8);
|
|
1319
|
+
const view = new DataView(result.buffer, result.byteOffset, result.byteLength);
|
|
1320
|
+
view.setBigUint64(0, view.getBigUint64(0) ^ BigInt.asUintN(64, revision));
|
|
1321
|
+
return result;
|
|
1322
|
+
};
|
|
1323
|
+
/**
|
|
1324
|
+
* Operations that change server state before the reply size is known. READ belongs here
|
|
1325
|
+
* because reading updates the file's access time, which a slot rollback cannot undo.
|
|
1326
|
+
*/
|
|
1327
|
+
const stateChangingKinds = new Set([
|
|
1328
|
+
"Read",
|
|
1329
|
+
"Create",
|
|
1330
|
+
"Link",
|
|
1331
|
+
"Remove",
|
|
1332
|
+
"Rename",
|
|
1333
|
+
"Setattr",
|
|
1334
|
+
"Write",
|
|
1335
|
+
"Commit",
|
|
1336
|
+
"Open",
|
|
1337
|
+
"Close",
|
|
1338
|
+
"OpenDowngrade",
|
|
1339
|
+
"Lock",
|
|
1340
|
+
"Locku",
|
|
1341
|
+
"FreeStateid",
|
|
1342
|
+
"ExchangeId",
|
|
1343
|
+
"CreateSession",
|
|
1344
|
+
"DestroySession",
|
|
1345
|
+
"DestroyClient",
|
|
1346
|
+
"ReclaimComplete",
|
|
1347
|
+
// BACKCHANNEL_CTL replaces the callback program and credential and re-arms the probe, so a
|
|
1348
|
+
// compound carrying it must be bounded by its worst case: otherwise an optimistic bound lets
|
|
1349
|
+
// the mutation happen and the reply overflow afterwards, and only SEQUENCE is rolled back.
|
|
1350
|
+
"BackchannelCtl"
|
|
1351
|
+
]);
|
|
1352
|
+
/**
|
|
1353
|
+
* Reply bound used by SEQUENCE before operation dispatch.
|
|
1354
|
+
* Variable-size results are bounded optimistically so small actual replies still fit a small
|
|
1355
|
+
* channel, and the slot is rolled back if the encoded reply proves too large. When the compound
|
|
1356
|
+
* also changes state, every variable result uses its worst case instead, because a rollback
|
|
1357
|
+
* cannot undo the state change.
|
|
1358
|
+
*/
|
|
1359
|
+
const replayReplyBound = (operations, tagBytes, limits, securityFlavorCount) => {
|
|
1360
|
+
let bytes = 12 + tagBytes + (4 - tagBytes % 4) % 4;
|
|
1361
|
+
const worstCase = operations.some((operation) => stateChangingKinds.has(operation.kind));
|
|
1362
|
+
const maxReadBytes = ByteSize.toNumberUnsafe(limits.maxReadBytes);
|
|
1363
|
+
const maxReaddirReplyBytes = ByteSize.toNumberUnsafe(limits.maxReaddirReplyBytes);
|
|
1364
|
+
const maxStringBytes = ByteSize.toNumberUnsafe(limits.maxStringBytes);
|
|
1365
|
+
const maxOwnerBytes = ByteSize.toNumberUnsafe(limits.maxOwnerBytes);
|
|
1366
|
+
// Each bound covers the 8-byte operation header plus the result body.
|
|
1367
|
+
for (const operation of operations) {
|
|
1368
|
+
switch (operation.kind) {
|
|
1369
|
+
case "Read":
|
|
1370
|
+
bytes += worstCase ? 16 + Math.min(operation.value.count, maxReadBytes) + 4 : 32;
|
|
1371
|
+
break;
|
|
1372
|
+
case "Readdir":
|
|
1373
|
+
bytes += worstCase ? 16 + Math.min(operation.value.maxcount, maxReaddirReplyBytes) + 8 : 32;
|
|
1374
|
+
break;
|
|
1375
|
+
case "Getattr":
|
|
1376
|
+
bytes += worstCase ? MAX_GETATTR_REPLY_BYTES : 32;
|
|
1377
|
+
break;
|
|
1378
|
+
case "Readlink":
|
|
1379
|
+
bytes += worstCase ? 12 + maxStringBytes + 4 : 32;
|
|
1380
|
+
break;
|
|
1381
|
+
case "Getfh":
|
|
1382
|
+
bytes += 64;
|
|
1383
|
+
break;
|
|
1384
|
+
case "Sequence":
|
|
1385
|
+
bytes += 44;
|
|
1386
|
+
break;
|
|
1387
|
+
case "ExchangeId":
|
|
1388
|
+
bytes += 8 + 8 + 4 + 4 + 4 + 8 + 20 + 20 + 4;
|
|
1389
|
+
break;
|
|
1390
|
+
case "CreateSession":
|
|
1391
|
+
bytes += 8 + 16 + 4 + 4 + 28 + 32;
|
|
1392
|
+
break;
|
|
1393
|
+
case "Open":
|
|
1394
|
+
bytes += 8 + 16 + 4 + 16 + 4 + 12 + 8;
|
|
1395
|
+
break;
|
|
1396
|
+
case "Create":
|
|
1397
|
+
bytes += 64;
|
|
1398
|
+
break;
|
|
1399
|
+
case "Rename":
|
|
1400
|
+
bytes += 56;
|
|
1401
|
+
break;
|
|
1402
|
+
case "Link":
|
|
1403
|
+
case "Remove":
|
|
1404
|
+
bytes += 32;
|
|
1405
|
+
break;
|
|
1406
|
+
case "Setattr":
|
|
1407
|
+
bytes += 32;
|
|
1408
|
+
break;
|
|
1409
|
+
case "Write":
|
|
1410
|
+
bytes += 24;
|
|
1411
|
+
break;
|
|
1412
|
+
case "Commit":
|
|
1413
|
+
bytes += 16;
|
|
1414
|
+
break;
|
|
1415
|
+
case "Close":
|
|
1416
|
+
case "OpenDowngrade":
|
|
1417
|
+
case "Lock":
|
|
1418
|
+
bytes += 8 + Math.max(16, 8 + 8 + 4 + 8 + 4 + maxOwnerBytes + 4);
|
|
1419
|
+
break;
|
|
1420
|
+
case "Lockt":
|
|
1421
|
+
bytes += 8 + 8 + 8 + 4 + 8 + 4 + maxOwnerBytes + 4;
|
|
1422
|
+
break;
|
|
1423
|
+
case "Locku":
|
|
1424
|
+
bytes += 8 + 16;
|
|
1425
|
+
break;
|
|
1426
|
+
case "TestStateid":
|
|
1427
|
+
bytes += 12 + 4 * operation.value.length;
|
|
1428
|
+
break;
|
|
1429
|
+
case "Secinfo":
|
|
1430
|
+
case "SecinfoNoName":
|
|
1431
|
+
bytes += 12 + 4 * securityFlavorCount;
|
|
1432
|
+
break;
|
|
1433
|
+
case "BindConnToSession":
|
|
1434
|
+
bytes += 32;
|
|
1435
|
+
break;
|
|
1436
|
+
default:
|
|
1437
|
+
bytes += 16;
|
|
1438
|
+
}
|
|
1439
|
+
if (bytes > Number.MAX_SAFE_INTEGER)
|
|
1440
|
+
return Number.MAX_SAFE_INTEGER;
|
|
1441
|
+
}
|
|
1442
|
+
return bytes;
|
|
1443
|
+
};
|
|
1444
|
+
/** @internal */
|
|
1445
|
+
export const makeNfs4Handler = (export_, options) => {
|
|
1446
|
+
assertOptions(options);
|
|
1447
|
+
const storageGeneration = options.storageGeneration ?? options.generation;
|
|
1448
|
+
const supportedAttributes = supportedAttributesFor(export_);
|
|
1449
|
+
const sampleUsage = (requested) => requested.some((attribute) => capacityAttributes.has(attribute))
|
|
1450
|
+
? export_.capacity.usage
|
|
1451
|
+
: Effect.succeed(null);
|
|
1452
|
+
return Effect.gen(function* () {
|
|
1453
|
+
const crypto = yield* Crypto.Crypto;
|
|
1454
|
+
const verifierInput = new Uint8Array(33);
|
|
1455
|
+
verifierInput[0] = 1;
|
|
1456
|
+
verifierInput.set(options.generation, 1);
|
|
1457
|
+
verifierInput.set(storageGeneration, 17);
|
|
1458
|
+
const writeVerifier = (yield* crypto.digest("SHA-256", verifierInput)).slice(0, 8);
|
|
1459
|
+
const handlerScope = yield* Effect.scope;
|
|
1460
|
+
const clients = new Map();
|
|
1461
|
+
const clientsByOwner = new Map();
|
|
1462
|
+
const sessions = new Map();
|
|
1463
|
+
const opens = new Map();
|
|
1464
|
+
const lockStates = new Map();
|
|
1465
|
+
let clientSerial = 1n;
|
|
1466
|
+
let sessionSerial = 1n;
|
|
1467
|
+
let openSerial = 1n;
|
|
1468
|
+
let lockCount = 0;
|
|
1469
|
+
const conflictingLock = (reference, client, ownerKey, range, type) => {
|
|
1470
|
+
for (const state of lockStates.values()) {
|
|
1471
|
+
if (state.open.reference !== reference || (state.client === client && state.ownerKey === ownerKey))
|
|
1472
|
+
continue;
|
|
1473
|
+
for (const entry of state.ranges) {
|
|
1474
|
+
if (overlaps(entry.range, range) && (WRITE_LOCK_TYPES.has(type) || WRITE_LOCK_TYPES.has(entry.type))) {
|
|
1475
|
+
return { state, entry };
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
return undefined;
|
|
1480
|
+
};
|
|
1481
|
+
const deniedLock = (code, conflict) => ({
|
|
1482
|
+
code,
|
|
1483
|
+
status: Status.DENIED,
|
|
1484
|
+
body: new Writer()
|
|
1485
|
+
.uint64(conflict.entry.range.offset)
|
|
1486
|
+
.uint64(conflict.entry.range.length)
|
|
1487
|
+
.uint32(conflict.entry.type)
|
|
1488
|
+
.uint64(conflict.state.client.id)
|
|
1489
|
+
.opaque(conflict.state.owner)
|
|
1490
|
+
.bytes()
|
|
1491
|
+
});
|
|
1492
|
+
let replayBytes = ByteSize.bytes(0);
|
|
1493
|
+
const maxRpcRequestBytes = ByteSize.min(options.limits.maxRecordBytes, options.limits.maxCompoundBytes);
|
|
1494
|
+
const maxRpcResponseBytes = ByteSize.min(options.limits.maxRecordBytes, options.limits.maxCompoundBytes);
|
|
1495
|
+
const rpcReplyOverheadBytes = ByteSize.bytes(24);
|
|
1496
|
+
const stateGate = Semaphore.makeUnsafe(1);
|
|
1497
|
+
/**
|
|
1498
|
+
* Callbacks awaiting a reply, keyed by RPC xid. Deliberately outside `stateGate`: a reply
|
|
1499
|
+
* arrives on the same connection that is running the read loop, so making the reply path take
|
|
1500
|
+
* the gate would deadlock against a compound that is holding it.
|
|
1501
|
+
*/
|
|
1502
|
+
const pendingCallbacks = new Map();
|
|
1503
|
+
let callbackXid = 1;
|
|
1504
|
+
/**
|
|
1505
|
+
* Sends one callback down every connection carrying a session's backchannel and answers
|
|
1506
|
+
* whether any of them accepted it. The carriers race rather than taking turns: a connection
|
|
1507
|
+
* whose write succeeds but whose client never replies, or which answers with an RPC
|
|
1508
|
+
* rejection, must not stop a healthy second connection from winning, and one deadline covers
|
|
1509
|
+
* the whole attempt because the write itself can block against a peer that stops reading.
|
|
1510
|
+
*/
|
|
1511
|
+
const callback = (session, procedure, body, accepted) => Effect.gen(function* () {
|
|
1512
|
+
const back = session.back;
|
|
1513
|
+
const security = back?.security;
|
|
1514
|
+
if (back === undefined || security === undefined)
|
|
1515
|
+
return false;
|
|
1516
|
+
// A connection that has gone away is only removed from the map once `disconnect` acquires
|
|
1517
|
+
// the state gate, so a stale entry can still be present.
|
|
1518
|
+
const carriers = [];
|
|
1519
|
+
for (const [connection, direction] of session.connections) {
|
|
1520
|
+
if ((direction & CHANNEL_BACK) !== 0)
|
|
1521
|
+
carriers.push(connection);
|
|
1522
|
+
}
|
|
1523
|
+
if (carriers.length === 0)
|
|
1524
|
+
return false;
|
|
1525
|
+
// ca_maxrequestsize bounds the whole RPC call the client will accept, not just its
|
|
1526
|
+
// CB_COMPOUND body, so the framed message is what gets measured. The xid does not change
|
|
1527
|
+
// the size, so any value serves for the measurement.
|
|
1528
|
+
if (encodeCallbackCall(0, back.program, procedure, security, body).length > back.attrs.maxRequest) {
|
|
1529
|
+
return false;
|
|
1530
|
+
}
|
|
1531
|
+
// A carrier that fails outright must not burn the whole deadline when it is the only one,
|
|
1532
|
+
// so the race also ends as soon as every carrier has definitively failed.
|
|
1533
|
+
let failed = 0;
|
|
1534
|
+
const exhausted = yield* Deferred.make();
|
|
1535
|
+
const noteFailure = Effect.sync(() => {
|
|
1536
|
+
failed++;
|
|
1537
|
+
if (failed >= carriers.length)
|
|
1538
|
+
Deferred.doneUnsafe(exhausted, Effect.succeed(false));
|
|
1539
|
+
});
|
|
1540
|
+
const attempt = (carrier) => Effect.gen(function* () {
|
|
1541
|
+
// The xid is masked because it is both a map key and a uint32 on the wire; an
|
|
1542
|
+
// unmasked counter would eventually throw instead of wrapping.
|
|
1543
|
+
const xid = callbackXid;
|
|
1544
|
+
callbackXid = (callbackXid + 1) >>> 0;
|
|
1545
|
+
const reply = yield* Deferred.make();
|
|
1546
|
+
pendingCallbacks.set(xid, { carrier, reply });
|
|
1547
|
+
return yield* Effect.ensuring(Effect.gen(function* () {
|
|
1548
|
+
const sent = yield* carrier.send(encodeCallbackCall(xid, back.program, procedure, security, body));
|
|
1549
|
+
// A carrier that cannot be written to, or whose client rejects the callback, loses
|
|
1550
|
+
// the race instead of ending it. The outer deadline bounds the wait when every
|
|
1551
|
+
// carrier does so.
|
|
1552
|
+
if (!sent) {
|
|
1553
|
+
yield* noteFailure;
|
|
1554
|
+
return yield* Effect.never;
|
|
1555
|
+
}
|
|
1556
|
+
const answered = yield* Deferred.await(reply);
|
|
1557
|
+
if (accepted(answered))
|
|
1558
|
+
return true;
|
|
1559
|
+
yield* noteFailure;
|
|
1560
|
+
return yield* Effect.never;
|
|
1561
|
+
}), Effect.sync(() => pendingCallbacks.delete(xid)));
|
|
1562
|
+
});
|
|
1563
|
+
return yield* Effect.raceAll([...carriers.map(attempt), Deferred.await(exhausted)]).pipe(Effect.timeoutOption(options.callbackTimeout), Effect.map((result) => Option.isSome(result) && result.value));
|
|
1564
|
+
});
|
|
1565
|
+
const revokeClient = (client) => Effect.gen(function* () {
|
|
1566
|
+
for (const [key, session] of sessions) {
|
|
1567
|
+
if (session.client !== client)
|
|
1568
|
+
continue;
|
|
1569
|
+
for (const slot of session.slots) {
|
|
1570
|
+
replayBytes = subtractBytes(replayBytes, slot.retainedBytes ?? ByteSize.bytes(0));
|
|
1571
|
+
}
|
|
1572
|
+
sessions.delete(key);
|
|
1573
|
+
}
|
|
1574
|
+
for (const [key, lock] of lockStates) {
|
|
1575
|
+
if (lock.client !== client)
|
|
1576
|
+
continue;
|
|
1577
|
+
lockCount -= lock.ranges.length;
|
|
1578
|
+
lockStates.delete(key);
|
|
1579
|
+
}
|
|
1580
|
+
for (const [key, open] of opens) {
|
|
1581
|
+
if (open.client !== client)
|
|
1582
|
+
continue;
|
|
1583
|
+
// Revocation is reached from EXCHANGE_ID, CREATE_SESSION and an expired SEQUENCE, which
|
|
1584
|
+
// are interruptible operations. Each handle's close and its removal from `opens` are one
|
|
1585
|
+
// region for the same reason as CLOSE: an interrupt between them would leave a closed
|
|
1586
|
+
// handle for the handler scope's finalizer to close a second time. The loop stays
|
|
1587
|
+
// interruptible between opens.
|
|
1588
|
+
yield* Effect.uninterruptible(open.close.pipe(Effect.tap(() => Effect.sync(() => opens.delete(key)))));
|
|
1589
|
+
}
|
|
1590
|
+
});
|
|
1591
|
+
const releaseCreateSessionReplay = (client) => {
|
|
1592
|
+
replayBytes = subtractBytes(replayBytes, client.createSessionReplay?.retainedBytes ?? ByteSize.bytes(0));
|
|
1593
|
+
client.createSessionReplay = undefined;
|
|
1594
|
+
};
|
|
1595
|
+
const removeClientRecord = (client) => {
|
|
1596
|
+
releaseCreateSessionReplay(client);
|
|
1597
|
+
clients.delete(client.id);
|
|
1598
|
+
if (clientsByOwner.get(client.owner) !== client)
|
|
1599
|
+
return;
|
|
1600
|
+
if (client.previous !== undefined && clients.has(client.previous.id)) {
|
|
1601
|
+
clientsByOwner.set(client.owner, client.previous);
|
|
1602
|
+
}
|
|
1603
|
+
else {
|
|
1604
|
+
clientsByOwner.delete(client.owner);
|
|
1605
|
+
}
|
|
1606
|
+
};
|
|
1607
|
+
const sweepExpired = Effect.gen(function* () {
|
|
1608
|
+
const now = options.now();
|
|
1609
|
+
for (const client of clients.values()) {
|
|
1610
|
+
if (now <= client.leaseExpiresAt)
|
|
1611
|
+
continue;
|
|
1612
|
+
yield* revokeClient(client);
|
|
1613
|
+
removeClientRecord(client);
|
|
1614
|
+
}
|
|
1615
|
+
});
|
|
1616
|
+
// Section 8.3: a lease expires on its own schedule, so reclamation cannot wait for a compound
|
|
1617
|
+
// from some other client. On an idle server that compound never arrives, and an abandoned
|
|
1618
|
+
// client's opens and replay budget would stay charged until the handler scope closed. Half the
|
|
1619
|
+
// lease bounds how long expired state outlives its lease by one tick.
|
|
1620
|
+
const sweepInterval = Duration.seconds(Math.max(1, options.leaseDurationSeconds / 2));
|
|
1621
|
+
yield* Effect.forkIn(
|
|
1622
|
+
// `Effect.uninterruptible` sits inside `withPermit`, never around it, so scope closure
|
|
1623
|
+
// interrupts a sweep that is still waiting for the gate rather than queueing behind it.
|
|
1624
|
+
Effect.repeat(stateGate.withPermit(Effect.uninterruptible(sweepExpired)), {
|
|
1625
|
+
schedule: Schedule.spaced(sweepInterval)
|
|
1626
|
+
}), handlerScope);
|
|
1627
|
+
yield* Effect.addFinalizer(() => Effect.forEach(opens.values(), (open) => open.close, { discard: true }));
|
|
1628
|
+
/** Reads only the leading SEQUENCE of a compound to find its session, tolerating later decode errors. */
|
|
1629
|
+
const sequenceSessionOf = (bytes) => {
|
|
1630
|
+
try {
|
|
1631
|
+
const reader = new Reader(bytes, options.limits);
|
|
1632
|
+
reader.opaque(options.limits.maxStringBytes);
|
|
1633
|
+
const minor = reader.uint32();
|
|
1634
|
+
const operationCount = reader.uint32();
|
|
1635
|
+
if (minor !== 1 || reader.uint32() !== Operation.SEQUENCE)
|
|
1636
|
+
return undefined;
|
|
1637
|
+
const session = sessions.get(bytesKey(reader.fixedOpaque(16)));
|
|
1638
|
+
return session === undefined ? undefined : { session, operationCount };
|
|
1639
|
+
}
|
|
1640
|
+
catch (error) {
|
|
1641
|
+
if (error instanceof XdrDecodeError)
|
|
1642
|
+
return undefined;
|
|
1643
|
+
throw error;
|
|
1644
|
+
}
|
|
1645
|
+
};
|
|
1646
|
+
/**
|
|
1647
|
+
* `restore` reopens the compound's own interrupt window. The caller runs the whole compound
|
|
1648
|
+
* uninterruptibly so the replay-slot commit below cannot be torn in half, but an operation
|
|
1649
|
+
* that stalls in the backing store must not hold the handler scope open forever, so each
|
|
1650
|
+
* operation is dispatched through it. The boundary is between operations: anything that
|
|
1651
|
+
* mutates server state guards itself within its own operation (see OPEN and CLOSE), and an
|
|
1652
|
+
* interrupt arriving between them rolls the slot back via `rollbackSequence` so nothing stays
|
|
1653
|
+
* charged for a reply that was never sent.
|
|
1654
|
+
*/
|
|
1655
|
+
const executeCompound = (call, export_, activeCaller, restore) => Effect.suspend(() => {
|
|
1656
|
+
let parsed;
|
|
1657
|
+
try {
|
|
1658
|
+
parsed = parseCompound(call.arguments, options.limits);
|
|
1659
|
+
}
|
|
1660
|
+
catch (error) {
|
|
1661
|
+
if (!(error instanceof XdrDecodeError))
|
|
1662
|
+
throw error;
|
|
1663
|
+
const tag = (() => {
|
|
1664
|
+
try {
|
|
1665
|
+
return new Reader(call.arguments, options.limits).opaque(options.limits.maxStringBytes);
|
|
1666
|
+
}
|
|
1667
|
+
catch (tagError) {
|
|
1668
|
+
if (!(tagError instanceof XdrDecodeError))
|
|
1669
|
+
throw tagError;
|
|
1670
|
+
return empty;
|
|
1671
|
+
}
|
|
1672
|
+
})();
|
|
1673
|
+
// Section 2.10.6.4: an oversized request is reported as such even when later
|
|
1674
|
+
// operations fail to decode.
|
|
1675
|
+
const oversized = sequenceSessionOf(call.arguments);
|
|
1676
|
+
if (oversized !== undefined &&
|
|
1677
|
+
(call.requestBytes ?? call.arguments.length) > oversized.session.fore.maxRequest) {
|
|
1678
|
+
return Effect.succeed(encodeCompound(tag, [{ code: Operation.SEQUENCE, status: Status.REQ_TOO_BIG }]));
|
|
1679
|
+
}
|
|
1680
|
+
if (oversized !== undefined && oversized.operationCount > oversized.session.fore.maxOperations) {
|
|
1681
|
+
return Effect.succeed(encodeCompound(tag, [{ code: Operation.SEQUENCE, status: Status.TOO_MANY_OPS }]));
|
|
1682
|
+
}
|
|
1683
|
+
return Effect.succeed(encodeCompound(tag, [], Status.BADXDR));
|
|
1684
|
+
}
|
|
1685
|
+
if (parsed.minor !== 1) {
|
|
1686
|
+
return Effect.succeed(encodeCompound(parsed.tag, [], Status.MINOR_VERS_MISMATCH));
|
|
1687
|
+
}
|
|
1688
|
+
const first = parsed.operations[0];
|
|
1689
|
+
const firstCode = first?.code;
|
|
1690
|
+
// Section 18.46.3: the operations that may start a compound without SEQUENCE.
|
|
1691
|
+
const isBootstrap = firstCode === Operation.EXCHANGE_ID || firstCode === Operation.CREATE_SESSION ||
|
|
1692
|
+
firstCode === Operation.DESTROY_SESSION || firstCode === Operation.DESTROY_CLIENTID ||
|
|
1693
|
+
firstCode === Operation.BIND_CONN_TO_SESSION;
|
|
1694
|
+
const isSoleBootstrap = parsed.operations.length === 1 && isBootstrap;
|
|
1695
|
+
if (first !== undefined && firstCode !== Operation.SEQUENCE && !isSoleBootstrap) {
|
|
1696
|
+
// Sections 15.2 and 18.52: an illegal opcode answers as OP_ILLEGAL whether or not a
|
|
1697
|
+
// session exists, and arguments are decoded before any session check.
|
|
1698
|
+
if (first.kind === "Unknown") {
|
|
1699
|
+
return Effect.succeed(encodeCompound(parsed.tag, [{ code: Operation.ILLEGAL, status: Status.OP_ILLEGAL }]));
|
|
1700
|
+
}
|
|
1701
|
+
if (first.kind === "Malformed") {
|
|
1702
|
+
return Effect.succeed(encodeCompound(parsed.tag, [{ code: first.code, status: Status.BADXDR }]));
|
|
1703
|
+
}
|
|
1704
|
+
// Sections 18.34.3, 18.35.3, 18.36.3, 18.37.3, and 18.50.3: these MUST be the only operation.
|
|
1705
|
+
if (isBootstrap) {
|
|
1706
|
+
return Effect.succeed(encodeCompound(parsed.tag, [{ code: firstCode, status: Status.NOT_ONLY_OP }]));
|
|
1707
|
+
}
|
|
1708
|
+
// Section 15.2 allows only NFS4ERR_NOTSUPP for the NFSv4.0 operations.
|
|
1709
|
+
if (mustNotImplementOperations.has(firstCode)) {
|
|
1710
|
+
return Effect.succeed(encodeCompound(parsed.tag, [{ code: firstCode, status: Status.NOTSUPP }]));
|
|
1711
|
+
}
|
|
1712
|
+
return Effect.succeed(encodeCompound(parsed.tag, [{ code: firstCode, status: Status.OP_NOT_IN_SESSION }]));
|
|
1713
|
+
}
|
|
1714
|
+
if (first?.kind === "Sequence") {
|
|
1715
|
+
const value = first.value;
|
|
1716
|
+
const session = sessions.get(bytesKey(value.session));
|
|
1717
|
+
const slot = session?.slots[value.slot];
|
|
1718
|
+
if (session !== undefined && slot !== undefined && value.sequence === slot.sequence &&
|
|
1719
|
+
slot.response !== undefined) {
|
|
1720
|
+
// Section 2.10.3.1 ties association to SEQUENCE being transmitted, not to the result,
|
|
1721
|
+
// so it happens before the retry is judged: even a false retry was transmitted here.
|
|
1722
|
+
associate(session, call.connection, CHANNEL_FORE);
|
|
1723
|
+
if (!sameRequest(slot.request, call.arguments) || slot.credentials !== credentialsKey(call.credentials)) {
|
|
1724
|
+
return Effect.succeed(encodeCompound(parsed.tag, [{ code: Operation.SEQUENCE, status: Status.SEQ_FALSE_RETRY }]));
|
|
1725
|
+
}
|
|
1726
|
+
// A policy may remap the same wire credential between attempts. Never replay a
|
|
1727
|
+
// response computed under a caller that no longer has this request's authority.
|
|
1728
|
+
if (slot.caller !== activeCaller)
|
|
1729
|
+
return Effect.fail(new RpcPolicyDenied());
|
|
1730
|
+
return Effect.succeed(new Uint8Array(slot.response));
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
let rollbackSequence;
|
|
1734
|
+
const mayChangeState = parsed.operations.some((operation) => stateChangingKinds.has(operation.kind));
|
|
1735
|
+
let consumedSequence = false;
|
|
1736
|
+
return Effect.gen(function* () {
|
|
1737
|
+
const parts = [];
|
|
1738
|
+
let current;
|
|
1739
|
+
let saved;
|
|
1740
|
+
// Section 16.2.3.1.2: the current and saved stateids travel with their filehandles.
|
|
1741
|
+
// `undefined` is the all-zeros special stateid.
|
|
1742
|
+
let currentStateid;
|
|
1743
|
+
let savedStateid;
|
|
1744
|
+
let activeSession;
|
|
1745
|
+
let activeSlot;
|
|
1746
|
+
let shouldCache = false;
|
|
1747
|
+
const rejectAfterExecution = (status) => {
|
|
1748
|
+
if (!mayChangeState) {
|
|
1749
|
+
rollbackSequence?.();
|
|
1750
|
+
}
|
|
1751
|
+
rollbackSequence = undefined;
|
|
1752
|
+
return encodeCompound(parsed.tag, [{ code: Operation.SEQUENCE, status }]);
|
|
1753
|
+
};
|
|
1754
|
+
for (let index = 0; index < parsed.operations.length; index++) {
|
|
1755
|
+
const operation = parsed.operations[index];
|
|
1756
|
+
let result;
|
|
1757
|
+
// Operations remain interruptible while waiting on the export. Their own commit
|
|
1758
|
+
// boundaries protect state changes; the consumed slot prevents rerunning them.
|
|
1759
|
+
result = yield* restore(execute(operation));
|
|
1760
|
+
parts.push(result);
|
|
1761
|
+
if (index === 0 && mayChangeState && activeSlot !== undefined && result.status === Status.OK) {
|
|
1762
|
+
// Publish a consumed-slot marker before any later operation can commit. An
|
|
1763
|
+
// interrupted or failed operation can leave this marker instead of its full reply.
|
|
1764
|
+
const second = parsed.operations[1];
|
|
1765
|
+
activeSlot.response = second === undefined
|
|
1766
|
+
? encodeCompound(parsed.tag, [result])
|
|
1767
|
+
: encodeCompound(parsed.tag, [
|
|
1768
|
+
result,
|
|
1769
|
+
{ code: second.code, status: Status.RETRY_UNCACHED_REP }
|
|
1770
|
+
]);
|
|
1771
|
+
activeSlot.request = new Uint8Array(call.arguments);
|
|
1772
|
+
activeSlot.credentials = credentialsKey(call.credentials);
|
|
1773
|
+
if (activeCaller === undefined)
|
|
1774
|
+
delete activeSlot.caller;
|
|
1775
|
+
else
|
|
1776
|
+
activeSlot.caller = activeCaller;
|
|
1777
|
+
consumedSequence = true;
|
|
1778
|
+
}
|
|
1779
|
+
if (result.status !== Status.OK)
|
|
1780
|
+
break;
|
|
1781
|
+
}
|
|
1782
|
+
const response = encodeCompound(parsed.tag, parts);
|
|
1783
|
+
const responseBytes = addBytes(byteLength(response.length), rpcReplyOverheadBytes);
|
|
1784
|
+
if (responseBytes > byteLength(activeSession?.fore.maxResponse ?? Number.MAX_SAFE_INTEGER)) {
|
|
1785
|
+
return rejectAfterExecution(Status.REP_TOO_BIG);
|
|
1786
|
+
}
|
|
1787
|
+
if (shouldCache &&
|
|
1788
|
+
responseBytes > byteLength(activeSession?.fore.maxCachedResponse ?? Number.MAX_SAFE_INTEGER)) {
|
|
1789
|
+
return rejectAfterExecution(Status.REP_TOO_BIG_TO_CACHE);
|
|
1790
|
+
}
|
|
1791
|
+
if (activeSlot !== undefined && shouldCache) {
|
|
1792
|
+
const retainedBytes = addBytes(byteLength(call.arguments.length), byteLength(response.length));
|
|
1793
|
+
if (retainedBytes > options.limits.maxReplayBytes ||
|
|
1794
|
+
addBytes(subtractBytes(replayBytes, activeSlot.retainedBytes ?? ByteSize.bytes(0)), retainedBytes) > options.limits.maxReplayBytes) {
|
|
1795
|
+
return rejectAfterExecution(Status.REP_TOO_BIG_TO_CACHE);
|
|
1796
|
+
}
|
|
1797
|
+
replayBytes = subtractBytes(replayBytes, activeSlot.retainedBytes ?? ByteSize.bytes(0));
|
|
1798
|
+
activeSlot.response = new Uint8Array(response);
|
|
1799
|
+
activeSlot.request = new Uint8Array(call.arguments);
|
|
1800
|
+
activeSlot.credentials = credentialsKey(call.credentials);
|
|
1801
|
+
if (activeCaller === undefined)
|
|
1802
|
+
delete activeSlot.caller;
|
|
1803
|
+
else
|
|
1804
|
+
activeSlot.caller = activeCaller;
|
|
1805
|
+
activeSlot.retainedBytes = retainedBytes;
|
|
1806
|
+
replayBytes = addBytes(replayBytes, retainedBytes);
|
|
1807
|
+
}
|
|
1808
|
+
else if (activeSlot !== undefined) {
|
|
1809
|
+
const sequencePart = parts[0];
|
|
1810
|
+
const second = parsed.operations[1];
|
|
1811
|
+
const replay = second === undefined
|
|
1812
|
+
? encodeCompound(parsed.tag, [sequencePart])
|
|
1813
|
+
: encodeCompound(parsed.tag, [
|
|
1814
|
+
sequencePart,
|
|
1815
|
+
{ code: second.code, status: Status.RETRY_UNCACHED_REP }
|
|
1816
|
+
]);
|
|
1817
|
+
const retainedBytes = addBytes(byteLength(call.arguments.length), byteLength(replay.length));
|
|
1818
|
+
if (addBytes(subtractBytes(replayBytes, activeSlot.retainedBytes ?? ByteSize.bytes(0)), retainedBytes) > options.limits.maxReplayBytes) {
|
|
1819
|
+
return rejectAfterExecution(Status.DELAY);
|
|
1820
|
+
}
|
|
1821
|
+
replayBytes = subtractBytes(replayBytes, activeSlot.retainedBytes ?? ByteSize.bytes(0));
|
|
1822
|
+
activeSlot.response = replay;
|
|
1823
|
+
activeSlot.request = new Uint8Array(call.arguments);
|
|
1824
|
+
activeSlot.credentials = credentialsKey(call.credentials);
|
|
1825
|
+
if (activeCaller === undefined)
|
|
1826
|
+
delete activeSlot.caller;
|
|
1827
|
+
else
|
|
1828
|
+
activeSlot.caller = activeCaller;
|
|
1829
|
+
activeSlot.retainedBytes = retainedBytes;
|
|
1830
|
+
replayBytes = addBytes(replayBytes, retainedBytes);
|
|
1831
|
+
}
|
|
1832
|
+
rollbackSequence = undefined;
|
|
1833
|
+
return response;
|
|
1834
|
+
function execute(operation) {
|
|
1835
|
+
const noCurrent = () => ({ code: operation.code, status: Status.NOFILEHANDLE });
|
|
1836
|
+
const mapFs = (effect) => effect.pipe(Effect.mapError(failureForFs));
|
|
1837
|
+
const withCurrent = (f) => current === undefined ? Effect.fail(Status.NOFILEHANDLE) : f(current);
|
|
1838
|
+
const statusResult = (effect, success = () => undefined) => effect.pipe(Effect.map((value) => {
|
|
1839
|
+
const body = success(value);
|
|
1840
|
+
return body === undefined
|
|
1841
|
+
? { code: operation.code, status: Status.OK }
|
|
1842
|
+
: { code: operation.code, status: Status.OK, body };
|
|
1843
|
+
}), Effect.catch((status) => Effect.succeed({ code: operation.code, status })));
|
|
1844
|
+
const requireAttributes = (attributes) => attributes === undefined ? Effect.fail(Status.SERVERFAULT) : Effect.succeed(attributes);
|
|
1845
|
+
/** Registers a filehandle only when the `filehandle` attribute (19) is requested. */
|
|
1846
|
+
const filehandleFor = (reference, requested) => requested.includes(19)
|
|
1847
|
+
? export_.handleFor(reference).pipe(Effect.mapError(() => Status.SERVERFAULT))
|
|
1848
|
+
: Effect.succeed(empty);
|
|
1849
|
+
/**
|
|
1850
|
+
* Requires a directory. Only operations whose Section 15.2 error list includes
|
|
1851
|
+
* NFS4ERR_SYMLINK may report a symbolic link as such; the others say NOTDIR.
|
|
1852
|
+
*/
|
|
1853
|
+
const requireDirectory = (reference, symlinkStatus) => mapFs(export_.observeMetadata(reference)).pipe(Effect.flatMap((observation) => observation.value.kind === "directory"
|
|
1854
|
+
? Effect.void
|
|
1855
|
+
: Effect.fail(observation.value.kind === "symlink" ? symlinkStatus : Status.NOTDIR)));
|
|
1856
|
+
const parentOfDirectory = (reference, symlinkStatus) => requireDirectory(reference, symlinkStatus).pipe(Effect.flatMap(() => parentOf(reference)));
|
|
1857
|
+
/** Requires a regular file, naming the offending type as Sections 18.16.4 and 18.22.3 do. */
|
|
1858
|
+
const requireRegularFile = (reference) => mapFs(export_.observeMetadata(reference)).pipe(Effect.flatMap((observation) => {
|
|
1859
|
+
switch (observation.value.kind) {
|
|
1860
|
+
case "file":
|
|
1861
|
+
return Effect.void;
|
|
1862
|
+
case "directory":
|
|
1863
|
+
return Effect.fail(Status.ISDIR);
|
|
1864
|
+
case "symlink":
|
|
1865
|
+
return Effect.fail(Status.SYMLINK);
|
|
1866
|
+
default:
|
|
1867
|
+
// The core has no other object kinds today; this keeps the switch exhaustive.
|
|
1868
|
+
return Effect.fail(Status.WRONG_TYPE);
|
|
1869
|
+
}
|
|
1870
|
+
}));
|
|
1871
|
+
/**
|
|
1872
|
+
* Validates a mutating operation's filehandles and names before the read-only
|
|
1873
|
+
* rejection, so structural errors keep their RFC 8881 precedence over NFS4ERR_ROFS.
|
|
1874
|
+
*/
|
|
1875
|
+
const rejectMutation = (names, savedRequirement, symlinkStatus) => {
|
|
1876
|
+
if (current === undefined)
|
|
1877
|
+
return Effect.succeed(noCurrent());
|
|
1878
|
+
if (savedRequirement !== "none" && saved === undefined)
|
|
1879
|
+
return Effect.succeed(noCurrent());
|
|
1880
|
+
const savedDirectory = saved;
|
|
1881
|
+
const checks = requireDirectory(current, symlinkStatus).pipe(Effect.flatMap(() => savedRequirement === "directory" && savedDirectory !== undefined
|
|
1882
|
+
? requireDirectory(savedDirectory, symlinkStatus)
|
|
1883
|
+
: Effect.void), Effect.flatMap(() => Effect.suspend(() => {
|
|
1884
|
+
for (const name of names) {
|
|
1885
|
+
try {
|
|
1886
|
+
validateName(name, options.limits.maxNameBytes);
|
|
1887
|
+
}
|
|
1888
|
+
catch (error) {
|
|
1889
|
+
if (error instanceof InvalidNameError)
|
|
1890
|
+
return Effect.fail(nameStatus(error));
|
|
1891
|
+
throw error;
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
return Effect.fail(Status.ROFS);
|
|
1895
|
+
})));
|
|
1896
|
+
return statusResult(checks);
|
|
1897
|
+
};
|
|
1898
|
+
const validName = (name) => Effect.try({
|
|
1899
|
+
try: () => {
|
|
1900
|
+
validateName(name, options.limits.maxNameBytes);
|
|
1901
|
+
},
|
|
1902
|
+
catch: (error) => error instanceof InvalidNameError ? nameStatus(error) : Status.SERVERFAULT
|
|
1903
|
+
});
|
|
1904
|
+
const writeChangeInfo = (writer, change) => {
|
|
1905
|
+
writer.boolean(true).uint64(BigInt.asUintN(64, change.before))
|
|
1906
|
+
.uint64(BigInt.asUintN(64, change.after));
|
|
1907
|
+
};
|
|
1908
|
+
switch (operation.kind) {
|
|
1909
|
+
case "ExchangeId": {
|
|
1910
|
+
const value = operation.value;
|
|
1911
|
+
if ((value.flags & ~EXCHGID4_ALLOWED_ARGUMENT_FLAGS) !== 0) {
|
|
1912
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
1913
|
+
}
|
|
1914
|
+
// Section 18.35.3: SP4_MACH_CRED requires an RPCSEC_GSS integrity-protected
|
|
1915
|
+
// EXCHANGE_ID, which AUTH_SYS cannot provide, and no SSV algorithm is offered.
|
|
1916
|
+
// These are the answers Linux nfsd gives in the same situation.
|
|
1917
|
+
if (value.protection === SP4_MACH_CRED) {
|
|
1918
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
1919
|
+
}
|
|
1920
|
+
if (value.protection === SP4_SSV) {
|
|
1921
|
+
return Effect.succeed({ code: operation.code, status: Status.ENCR_ALG_UNSUPP });
|
|
1922
|
+
}
|
|
1923
|
+
const owner = bytesKey(value.owner);
|
|
1924
|
+
const verifier = bytesKey(value.verifier);
|
|
1925
|
+
const principal = principalKey(call.credentials);
|
|
1926
|
+
const updateConfirmed = (value.flags & 0x4000_0000) !== 0;
|
|
1927
|
+
const confirmedRecord = [...clients.values()].find((candidate) => candidate.owner === owner && candidate.confirmed);
|
|
1928
|
+
let client;
|
|
1929
|
+
if (updateConfirmed) {
|
|
1930
|
+
// Section 18.35.4 cases 6 to 9.
|
|
1931
|
+
if (confirmedRecord === undefined) {
|
|
1932
|
+
return Effect.succeed({ code: operation.code, status: Status.NOENT });
|
|
1933
|
+
}
|
|
1934
|
+
if (confirmedRecord.verifier !== verifier) {
|
|
1935
|
+
return Effect.succeed({ code: operation.code, status: Status.NOT_SAME });
|
|
1936
|
+
}
|
|
1937
|
+
// Case 9 prescribes NFS4ERR_PERM even though the Section 15.2 list for
|
|
1938
|
+
// EXCHANGE_ID omits it; the normative case description wins.
|
|
1939
|
+
if (confirmedRecord.principal !== principal) {
|
|
1940
|
+
return Effect.succeed({ code: operation.code, status: Status.PERM });
|
|
1941
|
+
}
|
|
1942
|
+
client = confirmedRecord;
|
|
1943
|
+
}
|
|
1944
|
+
else if (confirmedRecord !== undefined && confirmedRecord.verifier === verifier &&
|
|
1945
|
+
confirmedRecord.principal === principal) {
|
|
1946
|
+
// Case 2: a retry or trunking probe against the confirmed record.
|
|
1947
|
+
client = confirmedRecord;
|
|
1948
|
+
}
|
|
1949
|
+
if (client === undefined) {
|
|
1950
|
+
return Effect.gen(function* () {
|
|
1951
|
+
const currentClient = clientsByOwner.get(owner);
|
|
1952
|
+
if (currentClient === undefined && clientsByOwner.size >= options.limits.maxClients) {
|
|
1953
|
+
return { code: operation.code, status: Status.DELAY };
|
|
1954
|
+
}
|
|
1955
|
+
let previous;
|
|
1956
|
+
if (confirmedRecord !== undefined && confirmedRecord.principal !== principal) {
|
|
1957
|
+
// Case 3: owner collision with another principal. Live state protects the
|
|
1958
|
+
// confirmed record; otherwise it is replaced outright.
|
|
1959
|
+
const hasState = [...sessions.values()].some((session) => session.client === confirmedRecord) ||
|
|
1960
|
+
[...opens.values()].some((open) => open.client === confirmedRecord);
|
|
1961
|
+
if (hasState && options.now() <= confirmedRecord.leaseExpiresAt) {
|
|
1962
|
+
return { code: operation.code, status: Status.CLID_INUSE };
|
|
1963
|
+
}
|
|
1964
|
+
yield* revokeClient(confirmedRecord);
|
|
1965
|
+
removeClientRecord(confirmedRecord);
|
|
1966
|
+
}
|
|
1967
|
+
else if (confirmedRecord !== undefined) {
|
|
1968
|
+
// Case 5: client restart. The confirmed record survives until CREATE_SESSION.
|
|
1969
|
+
if ([...clients.values()].filter((candidate) => !candidate.confirmed && candidate.previous !== undefined)
|
|
1970
|
+
.length >= options.limits.maxPendingClientReplacements &&
|
|
1971
|
+
!(currentClient !== undefined && !currentClient.confirmed)) {
|
|
1972
|
+
return { code: operation.code, status: Status.DELAY };
|
|
1973
|
+
}
|
|
1974
|
+
previous = confirmedRecord;
|
|
1975
|
+
}
|
|
1976
|
+
// Case 4: any unconfirmed record for this owner is replaced by a new client ID.
|
|
1977
|
+
const unconfirmed = clientsByOwner.get(owner);
|
|
1978
|
+
if (unconfirmed !== undefined && !unconfirmed.confirmed) {
|
|
1979
|
+
releaseCreateSessionReplay(unconfirmed);
|
|
1980
|
+
clients.delete(unconfirmed.id);
|
|
1981
|
+
}
|
|
1982
|
+
const created = {
|
|
1983
|
+
id: clientSerial++,
|
|
1984
|
+
owner,
|
|
1985
|
+
verifier,
|
|
1986
|
+
principal,
|
|
1987
|
+
previous,
|
|
1988
|
+
sequence: 1,
|
|
1989
|
+
leaseExpiresAt: options.now() + options.leaseDurationSeconds * 1000,
|
|
1990
|
+
reclaimed: false,
|
|
1991
|
+
confirmed: false,
|
|
1992
|
+
createSessionReplay: undefined
|
|
1993
|
+
};
|
|
1994
|
+
clients.set(created.id, created);
|
|
1995
|
+
clientsByOwner.set(owner, created);
|
|
1996
|
+
return exchangeIdResult(created);
|
|
1997
|
+
});
|
|
1998
|
+
}
|
|
1999
|
+
return Effect.succeed(exchangeIdResult(client));
|
|
2000
|
+
function exchangeIdResult(client) {
|
|
2001
|
+
const body = encodeStatusBody((writer) => {
|
|
2002
|
+
const flags = EXCHGID4_FLAG_USE_NON_PNFS |
|
|
2003
|
+
(client.confirmed ? EXCHGID4_FLAG_CONFIRMED_R : 0);
|
|
2004
|
+
writer.uint64(client.id).uint32(client.sequence).uint32(flags >>> 0).uint32(0);
|
|
2005
|
+
writer.uint64(export_.fsid[0]).opaque(options.generation).opaque(options.generation);
|
|
2006
|
+
writer.array([], () => undefined);
|
|
2007
|
+
});
|
|
2008
|
+
return { code: operation.code, status: Status.OK, body };
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
case "CreateSession": {
|
|
2012
|
+
const value = operation.value;
|
|
2013
|
+
const client = clients.get(value.client);
|
|
2014
|
+
if (client === undefined)
|
|
2015
|
+
return Effect.succeed({ code: operation.code, status: Status.STALE_CLIENTID });
|
|
2016
|
+
const replay = client.createSessionReplay;
|
|
2017
|
+
if (replay?.sequence === value.sequence) {
|
|
2018
|
+
// Section 18.36.4 phase 2: an equal csa_sequence identifies a retry, which may
|
|
2019
|
+
// arrive with or without a preceding SEQUENCE; the cached result is returned
|
|
2020
|
+
// before any argument validation.
|
|
2021
|
+
//
|
|
2022
|
+
// A retry usually arrives because the original reply was lost with the
|
|
2023
|
+
// connection. The client then holds a session it believes is bound to the
|
|
2024
|
+
// channels the cached reply names, so the replaying connection is associated
|
|
2025
|
+
// with exactly those directions.
|
|
2026
|
+
const replayed = replay.session === undefined ? undefined : sessions.get(bytesKey(replay.session));
|
|
2027
|
+
if (replayed !== undefined && replay.directions !== undefined) {
|
|
2028
|
+
associate(replayed, call.connection, replay.directions);
|
|
2029
|
+
}
|
|
2030
|
+
const result = replay.body === undefined
|
|
2031
|
+
? { code: operation.code, status: replay.status }
|
|
2032
|
+
: { code: operation.code, status: replay.status, body: new Uint8Array(replay.body) };
|
|
2033
|
+
return Effect.succeed(result);
|
|
2034
|
+
}
|
|
2035
|
+
if (value.sequence !== client.sequence) {
|
|
2036
|
+
return Effect.succeed({ code: operation.code, status: Status.SEQ_MISORDERED });
|
|
2037
|
+
}
|
|
2038
|
+
// Section 18.36.4 phase 2: a request with the expected csa_sequence consumes the
|
|
2039
|
+
// slot and its result is cached, whether or not a session is created. Two outcomes
|
|
2040
|
+
// leave the slot alone: NFS4ERR_DELAY asks for the same request again later, and
|
|
2041
|
+
// NFS4ERR_CLID_INUSE comes from a principal that does not own the record.
|
|
2042
|
+
const complete = (status, body, created) => {
|
|
2043
|
+
const previousRetainedBytes = client.createSessionReplay?.retainedBytes ?? ByteSize.bytes(0);
|
|
2044
|
+
const retainedBytes = byteLength(body?.length ?? 0);
|
|
2045
|
+
replayBytes = subtractBytes(replayBytes, previousRetainedBytes);
|
|
2046
|
+
replayBytes = addBytes(replayBytes, retainedBytes);
|
|
2047
|
+
client.sequence = nextSequenceId(client.sequence);
|
|
2048
|
+
let nextReplay = { sequence: value.sequence, status, retainedBytes };
|
|
2049
|
+
if (created !== undefined) {
|
|
2050
|
+
nextReplay = { ...nextReplay, session: created.session, directions: created.directions };
|
|
2051
|
+
}
|
|
2052
|
+
client.createSessionReplay = body === undefined
|
|
2053
|
+
? nextReplay
|
|
2054
|
+
: { ...nextReplay, body: new Uint8Array(body) };
|
|
2055
|
+
return body === undefined
|
|
2056
|
+
? { code: operation.code, status }
|
|
2057
|
+
: { code: operation.code, status, body };
|
|
2058
|
+
};
|
|
2059
|
+
return Effect.gen(function* () {
|
|
2060
|
+
const previousRetainedBytes = client.createSessionReplay?.retainedBytes ?? ByteSize.bytes(0);
|
|
2061
|
+
// Section 18.36.4 phase 3: an unconfirmed record belongs to the principal that
|
|
2062
|
+
// created it. A confirmed client may create sessions from any principal.
|
|
2063
|
+
if (!client.confirmed && client.principal !== principalKey(call.credentials)) {
|
|
2064
|
+
return { code: operation.code, status: Status.CLID_INUSE };
|
|
2065
|
+
}
|
|
2066
|
+
if ((value.flags & ~CREATE_SESSION4_KNOWN_FLAGS) !== 0)
|
|
2067
|
+
return complete(Status.INVAL);
|
|
2068
|
+
// Section 18.36.3: a callback security entry naming an RPCSEC_GSS handle the
|
|
2069
|
+
// server did not issue is NFS4ERR_NOENT.
|
|
2070
|
+
if (value.gssCallback)
|
|
2071
|
+
return complete(Status.NOENT);
|
|
2072
|
+
const requestedSlots = Math.max(1, value.fore.maxRequests);
|
|
2073
|
+
const slotCount = Math.min(requestedSlots, options.limits.maxSlotsPerSession);
|
|
2074
|
+
const fore = {
|
|
2075
|
+
headerPadding: 0,
|
|
2076
|
+
maxRequest: Math.min(value.fore.maxRequest, ByteSize.toNumberUnsafe(maxRpcRequestBytes)),
|
|
2077
|
+
maxResponse: Math.min(value.fore.maxResponse, ByteSize.toNumberUnsafe(maxRpcResponseBytes)),
|
|
2078
|
+
maxCachedResponse: Math.min(value.fore.maxCachedResponse, ByteSize.toNumberUnsafe(options.limits.maxReplayBytes)),
|
|
2079
|
+
maxOperations: Math.min(value.fore.maxOperations, options.limits.maxOperations),
|
|
2080
|
+
maxRequests: slotCount,
|
|
2081
|
+
rdmaIrd: []
|
|
2082
|
+
};
|
|
2083
|
+
const id = makeOpaqueId(options.generation, sessionSerial++);
|
|
2084
|
+
// Section 18.36.3: the backchannel exists only if the client asked for one. The
|
|
2085
|
+
// agreed flag must be echoed in csr_flags, because the client binds the
|
|
2086
|
+
// connection to the backchannel on the strength of that echo.
|
|
2087
|
+
const wantsBackChannel = (value.flags & CREATE_SESSION4_FLAG_CONN_BACK_CHAN) !== 0;
|
|
2088
|
+
// The server may use fewer slots than the client offered; it may not claim more.
|
|
2089
|
+
const backSlots = Math.max(1, Math.min(value.back.maxRequests, options.limits.maxSlotsPerSession));
|
|
2090
|
+
// Section 18.36.3: for the backchannel the server MUST NOT change
|
|
2091
|
+
// ca_maxoperations or ca_maxrequests, so the client's attributes are echoed
|
|
2092
|
+
// unchanged. The server's own slot table may still be smaller; that is an
|
|
2093
|
+
// internal limit on how many callbacks it issues, not a renegotiation.
|
|
2094
|
+
const back = { ...value.back, headerPadding: 0, rdmaIrd: [] };
|
|
2095
|
+
const agreedFlags = wantsBackChannel ? CREATE_SESSION4_FLAG_CONN_BACK_CHAN : 0;
|
|
2096
|
+
const body = encodeStatusBody((writer) => {
|
|
2097
|
+
writer.fixedOpaque(id).uint32(value.sequence).uint32(agreedFlags);
|
|
2098
|
+
writeChannelAttrs(writer, fore);
|
|
2099
|
+
writeChannelAttrs(writer, back);
|
|
2100
|
+
});
|
|
2101
|
+
// Section 18.36.3: a channel that can never carry a SEQUENCE compound in either
|
|
2102
|
+
// direction, or fewer than two operations, is too small to be used.
|
|
2103
|
+
if (fore.maxRequest < MIN_FORE_REQUEST_BYTES || fore.maxResponse < MIN_FORE_RESPONSE_BYTES ||
|
|
2104
|
+
value.back.maxRequest < MIN_FORE_REQUEST_BYTES ||
|
|
2105
|
+
value.back.maxResponse < MIN_FORE_RESPONSE_BYTES ||
|
|
2106
|
+
fore.maxOperations < 2) {
|
|
2107
|
+
return complete(Status.TOOSMALL);
|
|
2108
|
+
}
|
|
2109
|
+
// A backchannel the client asked for but sized so it can carry nothing is
|
|
2110
|
+
// rejected rather than quietly rounded up: the server must not send callbacks
|
|
2111
|
+
// on capacity the client never offered, and ca_maxrequests may not be changed
|
|
2112
|
+
// for the backchannel. One operation is enough, because the only callback this
|
|
2113
|
+
// server sends is a CB_SEQUENCE-only CB_COMPOUND.
|
|
2114
|
+
if (wantsBackChannel && (value.back.maxRequests < 1 || value.back.maxOperations < 1)) {
|
|
2115
|
+
return complete(Status.TOOSMALL);
|
|
2116
|
+
}
|
|
2117
|
+
// The cached reply is charged against the replay budget; reserve it from the encoded body.
|
|
2118
|
+
if (byteLength(body.length) >
|
|
2119
|
+
subtractBytes(options.limits.maxReplayBytes, subtractBytes(replayBytes, previousRetainedBytes))) {
|
|
2120
|
+
return { code: operation.code, status: Status.DELAY };
|
|
2121
|
+
}
|
|
2122
|
+
if (client.previous !== undefined) {
|
|
2123
|
+
const previousSessions = [...sessions.values()].filter((session) => session.client === client.previous).length;
|
|
2124
|
+
if (sessions.size - previousSessions >= options.limits.maxSessions) {
|
|
2125
|
+
return { code: operation.code, status: Status.DELAY };
|
|
2126
|
+
}
|
|
2127
|
+
yield* revokeClient(client.previous);
|
|
2128
|
+
removeClientRecord(client.previous);
|
|
2129
|
+
}
|
|
2130
|
+
else if (sessions.size >= options.limits.maxSessions) {
|
|
2131
|
+
return { code: operation.code, status: Status.DELAY };
|
|
2132
|
+
}
|
|
2133
|
+
// Section 18.36.3: the connection CREATE_SESSION arrived on is associated with
|
|
2134
|
+
// the session's fore channel without a further BIND_CONN_TO_SESSION.
|
|
2135
|
+
sessions.set(bytesKey(id), {
|
|
2136
|
+
id,
|
|
2137
|
+
client,
|
|
2138
|
+
fore,
|
|
2139
|
+
slots: Array.from({ length: slotCount }, () => ({ sequence: 0 })),
|
|
2140
|
+
// Section 2.10.3.1: the CREATE_SESSION connection is associated with the fore
|
|
2141
|
+
// channel, and with the backchannel too when one was agreed.
|
|
2142
|
+
connections: new Map([[
|
|
2143
|
+
call.connection,
|
|
2144
|
+
CHANNEL_FORE | (wantsBackChannel ? CHANNEL_BACK : 0)
|
|
2145
|
+
]]),
|
|
2146
|
+
back: wantsBackChannel
|
|
2147
|
+
? {
|
|
2148
|
+
program: value.callbackProgram,
|
|
2149
|
+
security: chooseCallbackSecurity(value.security),
|
|
2150
|
+
attrs: back,
|
|
2151
|
+
slots: Array.from({ length: backSlots }, () => ({ sequence: 0, busy: false })),
|
|
2152
|
+
// A client that authorized no encodable credential gets no callbacks, so
|
|
2153
|
+
// the path starts down rather than being probed with a flavor it never
|
|
2154
|
+
// offered.
|
|
2155
|
+
healthy: chooseCallbackSecurity(value.security) !== undefined,
|
|
2156
|
+
probed: false,
|
|
2157
|
+
arming: 0
|
|
2158
|
+
}
|
|
2159
|
+
: undefined
|
|
2160
|
+
});
|
|
2161
|
+
client.confirmed = true;
|
|
2162
|
+
client.leaseExpiresAt = options.now() + options.leaseDurationSeconds * 1000;
|
|
2163
|
+
return complete(Status.OK, body, {
|
|
2164
|
+
session: id,
|
|
2165
|
+
directions: CHANNEL_FORE | (wantsBackChannel ? CHANNEL_BACK : 0)
|
|
2166
|
+
});
|
|
2167
|
+
});
|
|
2168
|
+
}
|
|
2169
|
+
case "Sequence": {
|
|
2170
|
+
if (parts.length !== 0)
|
|
2171
|
+
return Effect.succeed({ code: operation.code, status: Status.SEQUENCE_POS });
|
|
2172
|
+
const value = operation.value;
|
|
2173
|
+
const session = sessions.get(bytesKey(value.session));
|
|
2174
|
+
if (session === undefined)
|
|
2175
|
+
return Effect.succeed({ code: operation.code, status: Status.BADSESSION });
|
|
2176
|
+
if (options.now() > session.client.leaseExpiresAt) {
|
|
2177
|
+
return revokeClient(session.client).pipe(Effect.as({ code: operation.code, status: Status.BADSESSION }));
|
|
2178
|
+
}
|
|
2179
|
+
// Section 2.10.3.1: under SP4_NONE the connection a SEQUENCE is transmitted on is
|
|
2180
|
+
// associated with the session's fore channel. Association follows transmission, so
|
|
2181
|
+
// it happens before the slot and size checks that may still reject this request.
|
|
2182
|
+
associate(session, call.connection, CHANNEL_FORE);
|
|
2183
|
+
if (value.slot >= session.slots.length) {
|
|
2184
|
+
return Effect.succeed({ code: operation.code, status: Status.BADSLOT });
|
|
2185
|
+
}
|
|
2186
|
+
if (value.highest >= session.slots.length) {
|
|
2187
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_HIGH_SLOT });
|
|
2188
|
+
}
|
|
2189
|
+
const slot = session.slots[value.slot];
|
|
2190
|
+
if ((call.requestBytes ?? call.arguments.length) > session.fore.maxRequest) {
|
|
2191
|
+
return Effect.succeed({ code: operation.code, status: Status.REQ_TOO_BIG });
|
|
2192
|
+
}
|
|
2193
|
+
// The header count is checked, so a malformed later operation cannot hide an
|
|
2194
|
+
// oversized compound.
|
|
2195
|
+
if (parsed.count > session.fore.maxOperations) {
|
|
2196
|
+
return Effect.succeed({ code: operation.code, status: Status.TOO_MANY_OPS });
|
|
2197
|
+
}
|
|
2198
|
+
const replyBound = replayReplyBound(parsed.operations, parsed.tag.length, options.limits, options.securityFlavors?.length ?? 2);
|
|
2199
|
+
const rpcReplyBound = addBytes(byteLength(replyBound), rpcReplyOverheadBytes);
|
|
2200
|
+
if (rpcReplyBound > byteLength(session.fore.maxResponse)) {
|
|
2201
|
+
return Effect.succeed({ code: operation.code, status: Status.REP_TOO_BIG });
|
|
2202
|
+
}
|
|
2203
|
+
if (value.cache && rpcReplyBound > byteLength(session.fore.maxCachedResponse)) {
|
|
2204
|
+
return Effect.succeed({ code: operation.code, status: Status.REP_TOO_BIG_TO_CACHE });
|
|
2205
|
+
}
|
|
2206
|
+
if (value.sequence !== nextSequenceId(slot.sequence)) {
|
|
2207
|
+
return Effect.succeed({ code: operation.code, status: Status.SEQ_MISORDERED });
|
|
2208
|
+
}
|
|
2209
|
+
const uncachedReplayBound = 12 + parsed.tag.length + (4 - parsed.tag.length % 4) % 4 +
|
|
2210
|
+
44 + (parsed.operations.length > 1 ? 8 : 0);
|
|
2211
|
+
const retainedBound = addBytes(byteLength(call.arguments.length), byteLength(value.cache ? replyBound : uncachedReplayBound));
|
|
2212
|
+
if (retainedBound > subtractBytes(options.limits.maxReplayBytes, subtractBytes(replayBytes, slot.retainedBytes ?? ByteSize.bytes(0)))) {
|
|
2213
|
+
// Replay memory is a server policy limit: a cached reply is "too big to cache",
|
|
2214
|
+
// and an uncached request can only be retried later.
|
|
2215
|
+
return Effect.succeed({
|
|
2216
|
+
code: operation.code,
|
|
2217
|
+
status: value.cache ? Status.REP_TOO_BIG_TO_CACHE : Status.DELAY
|
|
2218
|
+
});
|
|
2219
|
+
}
|
|
2220
|
+
const previousSlot = {
|
|
2221
|
+
sequence: slot.sequence,
|
|
2222
|
+
response: slot.response,
|
|
2223
|
+
request: slot.request,
|
|
2224
|
+
credentials: slot.credentials,
|
|
2225
|
+
caller: slot.caller,
|
|
2226
|
+
retainedBytes: slot.retainedBytes
|
|
2227
|
+
};
|
|
2228
|
+
rollbackSequence = () => {
|
|
2229
|
+
// Restore only this slot's accounting; other operations in the compound may have
|
|
2230
|
+
// changed the shared counter legitimately.
|
|
2231
|
+
replayBytes = subtractBytes(replayBytes, slot.retainedBytes ?? ByteSize.bytes(0));
|
|
2232
|
+
replayBytes = addBytes(replayBytes, previousSlot.retainedBytes ?? ByteSize.bytes(0));
|
|
2233
|
+
slot.sequence = previousSlot.sequence;
|
|
2234
|
+
if (previousSlot.response === undefined)
|
|
2235
|
+
delete slot.response;
|
|
2236
|
+
else
|
|
2237
|
+
slot.response = previousSlot.response;
|
|
2238
|
+
if (previousSlot.request === undefined)
|
|
2239
|
+
delete slot.request;
|
|
2240
|
+
else
|
|
2241
|
+
slot.request = previousSlot.request;
|
|
2242
|
+
if (previousSlot.credentials === undefined)
|
|
2243
|
+
delete slot.credentials;
|
|
2244
|
+
else
|
|
2245
|
+
slot.credentials = previousSlot.credentials;
|
|
2246
|
+
if (previousSlot.caller === undefined)
|
|
2247
|
+
delete slot.caller;
|
|
2248
|
+
else
|
|
2249
|
+
slot.caller = previousSlot.caller;
|
|
2250
|
+
if (previousSlot.retainedBytes === undefined)
|
|
2251
|
+
delete slot.retainedBytes;
|
|
2252
|
+
else
|
|
2253
|
+
slot.retainedBytes = previousSlot.retainedBytes;
|
|
2254
|
+
};
|
|
2255
|
+
replayBytes = subtractBytes(replayBytes, slot.retainedBytes ?? ByteSize.bytes(0));
|
|
2256
|
+
replayBytes = addBytes(replayBytes, retainedBound);
|
|
2257
|
+
slot.sequence = value.sequence;
|
|
2258
|
+
delete slot.response;
|
|
2259
|
+
delete slot.request;
|
|
2260
|
+
delete slot.credentials;
|
|
2261
|
+
delete slot.caller;
|
|
2262
|
+
slot.retainedBytes = retainedBound;
|
|
2263
|
+
session.client.leaseExpiresAt = options.now() + options.leaseDurationSeconds * 1000;
|
|
2264
|
+
activeSession = session;
|
|
2265
|
+
activeSlot = slot;
|
|
2266
|
+
shouldCache = value.cache;
|
|
2267
|
+
// Section 18.46.3: report a backchannel the server cannot use, so the client can
|
|
2268
|
+
// repair it with BIND_CONN_TO_SESSION or BACKCHANNEL_CTL.
|
|
2269
|
+
const statusFlags = session.back !== undefined && !session.back.healthy
|
|
2270
|
+
? SEQ4_STATUS_CB_PATH_DOWN_SESSION
|
|
2271
|
+
: 0;
|
|
2272
|
+
const body = encodeStatusBody((writer) => {
|
|
2273
|
+
writer.fixedOpaque(session.id).uint32(value.sequence).uint32(value.slot)
|
|
2274
|
+
.uint32(session.slots.length - 1).uint32(session.slots.length - 1).uint32(statusFlags);
|
|
2275
|
+
});
|
|
2276
|
+
// The callback path is probed once, on the first SEQUENCE rather than during
|
|
2277
|
+
// CREATE_SESSION, because only then is the client known to hold the session id
|
|
2278
|
+
// that CB_SEQUENCE carries. It is forked because the probe's own reply arrives on
|
|
2279
|
+
// this connection, whose read loop is busy with this compound until it returns.
|
|
2280
|
+
const back = session.back;
|
|
2281
|
+
if (back === undefined || back.probed) {
|
|
2282
|
+
return Effect.succeed({ code: operation.code, status: Status.OK, body });
|
|
2283
|
+
}
|
|
2284
|
+
back.probed = true;
|
|
2285
|
+
return Effect.forkIn(probe(session), handlerScope).pipe(Effect.as({ code: operation.code, status: Status.OK, body }));
|
|
2286
|
+
}
|
|
2287
|
+
case "ReclaimComplete": {
|
|
2288
|
+
if (activeSession === undefined) {
|
|
2289
|
+
return Effect.succeed({ code: operation.code, status: Status.BADSESSION });
|
|
2290
|
+
}
|
|
2291
|
+
if (operation.value) {
|
|
2292
|
+
// rca_one_fs applies to the current filehandle's file system only and does not
|
|
2293
|
+
// complete the global reclaim (RFC 8881 Section 18.51.3).
|
|
2294
|
+
return Effect.succeed(current === undefined ? noCurrent() : { code: operation.code, status: Status.OK });
|
|
2295
|
+
}
|
|
2296
|
+
if (activeSession.client.reclaimed) {
|
|
2297
|
+
return Effect.succeed({ code: operation.code, status: Status.COMPLETE_ALREADY });
|
|
2298
|
+
}
|
|
2299
|
+
activeSession.client.reclaimed = true;
|
|
2300
|
+
return Effect.succeed({ code: operation.code, status: Status.OK });
|
|
2301
|
+
}
|
|
2302
|
+
case "BindConnToSession": {
|
|
2303
|
+
// Section 18.34.3: MUST be the only operation.
|
|
2304
|
+
if (parsed.operations.length !== 1) {
|
|
2305
|
+
return Effect.succeed({ code: operation.code, status: Status.NOT_ONLY_OP });
|
|
2306
|
+
}
|
|
2307
|
+
const session = sessions.get(bytesKey(operation.value.session));
|
|
2308
|
+
if (session === undefined)
|
|
2309
|
+
return Effect.succeed({ code: operation.code, status: Status.BADSESSION });
|
|
2310
|
+
const requested = operation.value.direction;
|
|
2311
|
+
// Section 18.34.3 fixes what each request may be answered with: CDFC4_FORE MUST
|
|
2312
|
+
// get CDFS4_FORE, CDFC4_BACK MUST get CDFS4_BACK, CDFC4_FORE_OR_BOTH MUST get
|
|
2313
|
+
// FORE or BOTH, and CDFC4_BACK_OR_BOTH MUST get BACK or BOTH. A request that
|
|
2314
|
+
// cannot be answered that way demands a change the server cannot make, which is
|
|
2315
|
+
// NFS4ERR_INVAL. Only a session that negotiated a backchannel in CREATE_SESSION
|
|
2316
|
+
// has one to bind. The section does not name an error for that case; INVAL is
|
|
2317
|
+
// chosen because it is the error it uses for a channel change it cannot make, and
|
|
2318
|
+
// Section 15.2 lists it for this operation.
|
|
2319
|
+
const backAvailable = session.back !== undefined;
|
|
2320
|
+
if (!backAvailable && (requested === CDFC4_BACK || requested === CDFC4_BACK_OR_BOTH)) {
|
|
2321
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
2322
|
+
}
|
|
2323
|
+
const bound = requested === CDFC4_BACK
|
|
2324
|
+
? CHANNEL_BACK
|
|
2325
|
+
: requested === CDFC4_FORE
|
|
2326
|
+
? CHANNEL_FORE
|
|
2327
|
+
: CHANNEL_FORE | (backAvailable ? CHANNEL_BACK : 0);
|
|
2328
|
+
associate(session, call.connection, bound);
|
|
2329
|
+
// Section 18.34.4: a client whose backchannel lost its connections binds a new
|
|
2330
|
+
// one. Clearing `probed` is what makes the next SEQUENCE actually retry the
|
|
2331
|
+
// path; without it a recovered client stays marked down forever.
|
|
2332
|
+
if ((bound & CHANNEL_BACK) !== 0 && session.back !== undefined) {
|
|
2333
|
+
session.back.probed = false;
|
|
2334
|
+
session.back.arming++;
|
|
2335
|
+
}
|
|
2336
|
+
const answered = bound === CHANNEL_BACK
|
|
2337
|
+
? CDFS4_BACK
|
|
2338
|
+
: bound === CHANNEL_FORE
|
|
2339
|
+
? CDFS4_FORE
|
|
2340
|
+
: CDFS4_BOTH;
|
|
2341
|
+
return Effect.succeed({
|
|
2342
|
+
code: operation.code,
|
|
2343
|
+
status: Status.OK,
|
|
2344
|
+
body: new Writer().fixedOpaque(session.id).uint32(answered).boolean(false).bytes()
|
|
2345
|
+
});
|
|
2346
|
+
}
|
|
2347
|
+
case "BackchannelCtl": {
|
|
2348
|
+
// Section 18.33.3: an RPCSEC_GSS handle the server never issued is NFS4ERR_NOENT.
|
|
2349
|
+
// AUTH_NONE and AUTH_SYS parameters are accepted as Linux nfsd does.
|
|
2350
|
+
if (operation.value.gssCallback) {
|
|
2351
|
+
return Effect.succeed({ code: operation.code, status: Status.NOENT });
|
|
2352
|
+
}
|
|
2353
|
+
if (activeSession === undefined) {
|
|
2354
|
+
return Effect.succeed({ code: operation.code, status: Status.BADSESSION });
|
|
2355
|
+
}
|
|
2356
|
+
// Section 18.33.3 replaces the backchannel's callback program, so it needs a
|
|
2357
|
+
// backchannel to act on. It names no error for a session without one; INVAL is
|
|
2358
|
+
// chosen because Section 15.2 lists it for this operation.
|
|
2359
|
+
if (activeSession.back === undefined) {
|
|
2360
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
2361
|
+
}
|
|
2362
|
+
activeSession.back.program = operation.value.program;
|
|
2363
|
+
// Section 18.33.3 adds credentials rather than replacing them, but a re-offer is
|
|
2364
|
+
// the client's current authorization, so a newly offered flavor is adopted.
|
|
2365
|
+
const reoffered = chooseCallbackSecurity(operation.value.security);
|
|
2366
|
+
if (reoffered !== undefined)
|
|
2367
|
+
activeSession.back.security = reoffered;
|
|
2368
|
+
// A re-advertised program is the client repairing its callback service. Clearing
|
|
2369
|
+
// `probed` is what actually gives the new endpoint another chance: health alone
|
|
2370
|
+
// would be a claim no callback has tested. A path with no encodable credential
|
|
2371
|
+
// stays down, because nothing can be sent down it.
|
|
2372
|
+
activeSession.back.probed = false;
|
|
2373
|
+
activeSession.back.arming++;
|
|
2374
|
+
activeSession.back.healthy = activeSession.back.security !== undefined;
|
|
2375
|
+
return Effect.succeed({ code: operation.code, status: Status.OK });
|
|
2376
|
+
}
|
|
2377
|
+
case "DestroySession": {
|
|
2378
|
+
const key = bytesKey(operation.value);
|
|
2379
|
+
const session = sessions.get(key);
|
|
2380
|
+
if (session === undefined) {
|
|
2381
|
+
return Effect.succeed({ code: operation.code, status: Status.BADSESSION });
|
|
2382
|
+
}
|
|
2383
|
+
// Section 18.37.3: only the active session's own DESTROY_SESSION must be final;
|
|
2384
|
+
// another session's may appear in any position after SEQUENCE.
|
|
2385
|
+
if (session === activeSession &&
|
|
2386
|
+
parsed.operations[parsed.operations.length - 1] !== operation) {
|
|
2387
|
+
return Effect.succeed({ code: operation.code, status: Status.NOT_ONLY_OP });
|
|
2388
|
+
}
|
|
2389
|
+
// Section 18.37.3: "DESTROY_SESSION MUST be invoked on a connection that is
|
|
2390
|
+
// associated with the session being destroyed." Without this a second connection
|
|
2391
|
+
// could destroy a session it never carried, using only an observed session id.
|
|
2392
|
+
if (!session.connections.has(call.connection)) {
|
|
2393
|
+
return Effect.succeed({ code: operation.code, status: Status.CONN_NOT_BOUND_TO_SESSION });
|
|
2394
|
+
}
|
|
2395
|
+
for (const slot of session.slots) {
|
|
2396
|
+
replayBytes = subtractBytes(replayBytes, slot.retainedBytes ?? ByteSize.bytes(0));
|
|
2397
|
+
}
|
|
2398
|
+
session.connections.clear();
|
|
2399
|
+
sessions.delete(key);
|
|
2400
|
+
if (session === activeSession) {
|
|
2401
|
+
activeSlot = undefined;
|
|
2402
|
+
shouldCache = false;
|
|
2403
|
+
rollbackSequence = undefined;
|
|
2404
|
+
}
|
|
2405
|
+
return Effect.succeed({ code: operation.code, status: Status.OK });
|
|
2406
|
+
}
|
|
2407
|
+
case "DestroyClient": {
|
|
2408
|
+
const client = clients.get(operation.value);
|
|
2409
|
+
if (client === undefined) {
|
|
2410
|
+
return Effect.succeed({ code: operation.code, status: Status.STALE_CLIENTID });
|
|
2411
|
+
}
|
|
2412
|
+
const hasSession = [...sessions.values()].some((session) => session.client === client);
|
|
2413
|
+
const hasOpen = [...opens.values()].some((open) => open.client === client);
|
|
2414
|
+
if (hasSession || hasOpen) {
|
|
2415
|
+
return Effect.succeed({ code: operation.code, status: Status.CLIENTID_BUSY });
|
|
2416
|
+
}
|
|
2417
|
+
removeClientRecord(client);
|
|
2418
|
+
return Effect.succeed({ code: operation.code, status: Status.OK });
|
|
2419
|
+
}
|
|
2420
|
+
case "Putrootfh":
|
|
2421
|
+
case "Putpubfh":
|
|
2422
|
+
// The public filehandle is the root filehandle (RFC 8881 Section 18.20.3).
|
|
2423
|
+
return statusResult(mapFs(export_.root), (reference) => {
|
|
2424
|
+
setCurrent(reference);
|
|
2425
|
+
return undefined;
|
|
2426
|
+
});
|
|
2427
|
+
case "Putfh":
|
|
2428
|
+
return export_.resolve(operation.value).pipe(Effect.map((reference) => {
|
|
2429
|
+
setCurrent(reference);
|
|
2430
|
+
return { code: operation.code, status: Status.OK };
|
|
2431
|
+
}), Effect.catch((error) => Effect.succeed({
|
|
2432
|
+
code: operation.code,
|
|
2433
|
+
status: error.reason === "WrongGeneration"
|
|
2434
|
+
? Status.FHEXPIRED
|
|
2435
|
+
: error.reason === "Stale"
|
|
2436
|
+
? Status.STALE
|
|
2437
|
+
: error.reason === "Unavailable"
|
|
2438
|
+
? Status.SERVERFAULT
|
|
2439
|
+
: Status.BADHANDLE
|
|
2440
|
+
})));
|
|
2441
|
+
case "Getfh":
|
|
2442
|
+
if (current === undefined)
|
|
2443
|
+
return Effect.succeed(noCurrent());
|
|
2444
|
+
return export_.handleFor(current).pipe(Effect.map((handle) => ({
|
|
2445
|
+
code: operation.code,
|
|
2446
|
+
status: Status.OK,
|
|
2447
|
+
body: new Writer().opaque(handle).bytes()
|
|
2448
|
+
})), Effect.orElseSucceed(() => ({ code: operation.code, status: Status.SERVERFAULT })));
|
|
2449
|
+
case "Savefh":
|
|
2450
|
+
if (current === undefined)
|
|
2451
|
+
return Effect.succeed(noCurrent());
|
|
2452
|
+
saved = current;
|
|
2453
|
+
savedStateid = currentStateid;
|
|
2454
|
+
return Effect.succeed({ code: operation.code, status: Status.OK });
|
|
2455
|
+
case "Restorefh":
|
|
2456
|
+
if (saved === undefined)
|
|
2457
|
+
return Effect.succeed(noCurrent());
|
|
2458
|
+
current = saved;
|
|
2459
|
+
currentStateid = savedStateid;
|
|
2460
|
+
return Effect.succeed({ code: operation.code, status: Status.OK });
|
|
2461
|
+
case "Lookup":
|
|
2462
|
+
// Section 15.1.2.8: a symbolic link as the current filehandle is NFS4ERR_SYMLINK.
|
|
2463
|
+
return statusResult(withCurrent((reference) => requireDirectory(reference, Status.SYMLINK).pipe(Effect.andThen(export_.lookup(reference, operation.value).pipe(Effect.mapError(nameStatus))))), (reference) => {
|
|
2464
|
+
setCurrent(reference);
|
|
2465
|
+
return undefined;
|
|
2466
|
+
});
|
|
2467
|
+
case "Secinfo":
|
|
2468
|
+
// SECINFO consumes the current filehandle (RFC 8881 Section 18.29.3).
|
|
2469
|
+
return statusResult(withCurrent((reference) => export_.lookup(reference, operation.value).pipe(Effect.mapError(nameStatus))), () => {
|
|
2470
|
+
setCurrent(undefined);
|
|
2471
|
+
return new Writer().array(options.securityFlavors ?? [AUTH_SYS, AUTH_NONE], (writer, flavor) => writer.uint32(flavor)).bytes();
|
|
2472
|
+
});
|
|
2473
|
+
case "Lookupp":
|
|
2474
|
+
return statusResult(withCurrent((reference) => parentOfDirectory(reference, Status.SYMLINK)), (reference) => {
|
|
2475
|
+
setCurrent(reference);
|
|
2476
|
+
return undefined;
|
|
2477
|
+
});
|
|
2478
|
+
case "SecinfoNoName":
|
|
2479
|
+
// Like SECINFO, this consumes the current filehandle (RFC 8881 Section 18.45.3).
|
|
2480
|
+
// Its error list has NOTDIR but not SYMLINK.
|
|
2481
|
+
return statusResult(withCurrent((reference) => operation.value === SECINFO_STYLE4_PARENT
|
|
2482
|
+
? parentOfDirectory(reference, Status.NOTDIR)
|
|
2483
|
+
: Effect.succeed(reference)), () => {
|
|
2484
|
+
setCurrent(undefined);
|
|
2485
|
+
return new Writer().array(options.securityFlavors ?? [AUTH_SYS, AUTH_NONE], (writer, flavor) => writer.uint32(flavor)).bytes();
|
|
2486
|
+
});
|
|
2487
|
+
case "Getattr": {
|
|
2488
|
+
if (current === undefined) {
|
|
2489
|
+
return Effect.succeed(noCurrent());
|
|
2490
|
+
}
|
|
2491
|
+
const reference = current;
|
|
2492
|
+
const requested = requestedAttributes(operation.value);
|
|
2493
|
+
const supportedRequested = requested.filter((attribute) => supportedAttributes.includes(attribute));
|
|
2494
|
+
const attributes = filehandleFor(reference, supportedRequested).pipe(Effect.flatMap((filehandle) => mapFs(export_.observeMetadata(reference)).pipe(Effect.flatMap((observation) => mapFs(sampleUsage(supportedRequested)).pipe(Effect.flatMap((usage) => requireAttributes(encodeAttributes(supportedRequested, observation, filehandle, export_, options, supportedAttributes, usage))))))));
|
|
2495
|
+
return statusResult(attributes, (value) => value);
|
|
2496
|
+
}
|
|
2497
|
+
case "Verify": {
|
|
2498
|
+
if (current === undefined)
|
|
2499
|
+
return Effect.succeed(noCurrent());
|
|
2500
|
+
const reference = current;
|
|
2501
|
+
const requested = requestedAttributes(operation.value.bitmap);
|
|
2502
|
+
// Write-only attributes are INVAL before the supported-set check (Section 18.31.3).
|
|
2503
|
+
if (requested.some((attribute) => nonComparableAttributes.has(attribute))) {
|
|
2504
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
2505
|
+
}
|
|
2506
|
+
if (requested.some((attribute) => !supportedAttributes.includes(attribute))) {
|
|
2507
|
+
return Effect.succeed({ code: operation.code, status: Status.ATTRNOTSUPP });
|
|
2508
|
+
}
|
|
2509
|
+
const comparison = filehandleFor(reference, requested).pipe(Effect.flatMap((filehandle) => mapFs(export_.observeMetadata(reference)).pipe(Effect.flatMap((observation) => mapFs(sampleUsage(requested)).pipe(Effect.flatMap((usage) => requireAttributes(encodeAttributeValues(requested, observation, filehandle, export_, options, supportedAttributes, usage))))))), Effect.flatMap((actual) => {
|
|
2510
|
+
const same = sameRequest(actual, operation.value.values);
|
|
2511
|
+
if (operation.code === Operation.VERIFY) {
|
|
2512
|
+
return same ? Effect.void : Effect.fail(Status.NOT_SAME);
|
|
2513
|
+
}
|
|
2514
|
+
return same ? Effect.fail(Status.SAME) : Effect.void;
|
|
2515
|
+
}));
|
|
2516
|
+
return statusResult(comparison);
|
|
2517
|
+
}
|
|
2518
|
+
case "Access": {
|
|
2519
|
+
if (current === undefined)
|
|
2520
|
+
return Effect.succeed(noCurrent());
|
|
2521
|
+
const requested = operation.value;
|
|
2522
|
+
const reference = current;
|
|
2523
|
+
return statusResult(Effect.gen(function* () {
|
|
2524
|
+
const observation = yield* mapFs(export_.observeMetadata(reference));
|
|
2525
|
+
const supported = requested & supportedAccessMask(observation.value.kind);
|
|
2526
|
+
let granted = activeCaller === undefined
|
|
2527
|
+
? grantedAccess(supported, observation.value, call.credentials)
|
|
2528
|
+
: 0;
|
|
2529
|
+
if (activeCaller !== undefined) {
|
|
2530
|
+
for (const [flag, bit] of [
|
|
2531
|
+
[ACCESS4_READ, 0o4],
|
|
2532
|
+
[observation.value.kind === "directory" ? ACCESS4_LOOKUP : ACCESS4_EXECUTE, 0o1]
|
|
2533
|
+
]) {
|
|
2534
|
+
if ((supported & flag) === 0)
|
|
2535
|
+
continue;
|
|
2536
|
+
const allowed = yield* activeCaller.accessReference(reference, bit).pipe(Effect.as(true), Effect.catchTag("FsError", (error) => error.code === "AccessDenied"
|
|
2537
|
+
? Effect.succeed(false)
|
|
2538
|
+
: Effect.fail(failureForFs(error))));
|
|
2539
|
+
if (allowed)
|
|
2540
|
+
granted |= flag;
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
return { supported, granted };
|
|
2544
|
+
}), ({ supported, granted }) => new Writer().uint32(supported).uint32(granted).bytes());
|
|
2545
|
+
}
|
|
2546
|
+
case "Commit": {
|
|
2547
|
+
if (current === undefined)
|
|
2548
|
+
return Effect.succeed(noCurrent());
|
|
2549
|
+
// Every writable mutation is committed before publication. There is no unstable
|
|
2550
|
+
// range to flush, but a failed provider must not produce a success reply.
|
|
2551
|
+
return statusResult(requireRegularFile(current), () => new Writer().fixedOpaque(writeVerifier).bytes());
|
|
2552
|
+
}
|
|
2553
|
+
case "Readlink":
|
|
2554
|
+
// Section 18.24.4: an object that is not a symbolic link is NFS4ERR_WRONG_TYPE.
|
|
2555
|
+
return statusResult(withCurrent((reference) => mapFs(export_.observeMetadata(reference)).pipe(Effect.filterOrFail((observation) => observation.value.kind === "symlink", () => Status.WRONG_TYPE), Effect.andThen(mapFs(export_.readLink(reference))), Effect.filterOrFail((target) => byteLength(target.length) <= options.limits.maxStringBytes, () => Status.SERVERFAULT))), (target) => new Writer().opaque(target).bytes());
|
|
2556
|
+
case "Readdir": {
|
|
2557
|
+
if (current === undefined)
|
|
2558
|
+
return Effect.succeed(noCurrent());
|
|
2559
|
+
const directory = current;
|
|
2560
|
+
const value = operation.value;
|
|
2561
|
+
return mapFs(export_.observeDirectory(directory)).pipe(Effect.flatMap((observation) => Effect.gen(function* () {
|
|
2562
|
+
const requested = requestedAttributes(value.attrs);
|
|
2563
|
+
const supportedRequested = requested.filter((attribute) => supportedAttributes.includes(attribute));
|
|
2564
|
+
const usage = yield* mapFs(sampleUsage(supportedRequested));
|
|
2565
|
+
const verifier = makeCookieVerifier(storageGeneration, observation.revision);
|
|
2566
|
+
if (value.cookie !== 0n && bytesKey(value.verifier) !== bytesKey(verifier)) {
|
|
2567
|
+
return { code: operation.code, status: Status.NOT_SAME };
|
|
2568
|
+
}
|
|
2569
|
+
if (value.cookie === 1n || value.cookie === 2n) {
|
|
2570
|
+
return { code: operation.code, status: Status.BAD_COOKIE };
|
|
2571
|
+
}
|
|
2572
|
+
const start = value.cookie === 0n ? 0 : Number(value.cookie - 2n);
|
|
2573
|
+
if (!Number.isSafeInteger(start) || start < 0 || start > observation.value.length) {
|
|
2574
|
+
return { code: operation.code, status: Status.BAD_COOKIE };
|
|
2575
|
+
}
|
|
2576
|
+
const writer = new Writer().fixedOpaque(verifier);
|
|
2577
|
+
let count = 0;
|
|
2578
|
+
let directoryBytes = 0;
|
|
2579
|
+
const responseLimit = Math.min(value.maxcount, ByteSize.toNumberUnsafe(options.limits.maxReaddirReplyBytes));
|
|
2580
|
+
if (responseLimit < 16) {
|
|
2581
|
+
return { code: operation.code, status: Status.TOOSMALL };
|
|
2582
|
+
}
|
|
2583
|
+
for (let item = start; item < observation.value.length && count < options.limits.maxReaddirEntries; item++) {
|
|
2584
|
+
const entry = observation.value[item];
|
|
2585
|
+
if (!isValidName(entry.name, options.limits.maxNameBytes)) {
|
|
2586
|
+
return { code: operation.code, status: Status.INVAL };
|
|
2587
|
+
}
|
|
2588
|
+
const attrs = supportedRequested.length === 0
|
|
2589
|
+
? new Writer().uint32(0).uint32(0).bytes()
|
|
2590
|
+
: yield* filehandleFor(entry.reference, supportedRequested).pipe(Effect.flatMap((handle) => mapFs(export_.observeMetadata(entry.reference)).pipe(Effect.flatMap((metadata) => requireAttributes(encodeAttributes(supportedRequested, metadata, handle, export_, options, supportedAttributes, usage))))),
|
|
2591
|
+
// With rdattr_error requested, a failing entry reports its own error
|
|
2592
|
+
// instead of failing the whole READDIR (RFC 8881 Section 18.23.3).
|
|
2593
|
+
Effect.catch((status) => supportedRequested.includes(11)
|
|
2594
|
+
? Effect.succeed(encodeReaddirError(status))
|
|
2595
|
+
: Effect.fail(status)));
|
|
2596
|
+
const encoded = new Writer().boolean(true).uint64(BigInt(item + 3)).opaque(entry.name)
|
|
2597
|
+
.fixedOpaque(attrs).bytes();
|
|
2598
|
+
const entryDirectoryBytes = 12 + entry.name.length + (4 - entry.name.length % 4) % 4;
|
|
2599
|
+
if ((value.dircount !== 0 && directoryBytes + entryDirectoryBytes > value.dircount) ||
|
|
2600
|
+
writer.length + encoded.length + 8 > responseLimit) {
|
|
2601
|
+
if (count === 0) {
|
|
2602
|
+
return { code: operation.code, status: Status.TOOSMALL };
|
|
2603
|
+
}
|
|
2604
|
+
break;
|
|
2605
|
+
}
|
|
2606
|
+
writer.fixedOpaque(encoded);
|
|
2607
|
+
directoryBytes += entryDirectoryBytes;
|
|
2608
|
+
count += 1;
|
|
2609
|
+
}
|
|
2610
|
+
writer.boolean(false).boolean(start + count >= observation.value.length);
|
|
2611
|
+
return { code: operation.code, status: Status.OK, body: writer.bytes() };
|
|
2612
|
+
})), Effect.catch((status) => Effect.succeed({ code: operation.code, status })));
|
|
2613
|
+
}
|
|
2614
|
+
case "Open": {
|
|
2615
|
+
if (activeSession === undefined) {
|
|
2616
|
+
return Effect.succeed({ code: operation.code, status: Status.BADSESSION });
|
|
2617
|
+
}
|
|
2618
|
+
if (current === undefined)
|
|
2619
|
+
return Effect.succeed(noCurrent());
|
|
2620
|
+
const directory = current;
|
|
2621
|
+
const value = operation.value;
|
|
2622
|
+
// The owner's clientid MAY hold any value; the client ID comes from the session
|
|
2623
|
+
// (RFC 8881 Section 18.16.3), so it is never checked.
|
|
2624
|
+
// No state survives a restart and no delegation is ever granted, so a reclaim finds
|
|
2625
|
+
// no grace period and a delegation stateid can never be valid (Section 15.1.9.3).
|
|
2626
|
+
if (value.claim === CLAIM_PREVIOUS || value.claim === CLAIM_DELEGATE_PREV ||
|
|
2627
|
+
value.claim === CLAIM_DELEG_PREV_FH) {
|
|
2628
|
+
return Effect.succeed({ code: operation.code, status: Status.NO_GRACE });
|
|
2629
|
+
}
|
|
2630
|
+
if (value.claim === CLAIM_DELEGATE_CUR || value.claim === CLAIM_DELEG_CUR_FH) {
|
|
2631
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
2632
|
+
}
|
|
2633
|
+
// share_access carries the access mode in its low bits and optional delegation
|
|
2634
|
+
// "want" hints above them (RFC 8881 Section 18.16.3). Hints are honored with an
|
|
2635
|
+
// extended no-delegation answer because this server grants no delegations.
|
|
2636
|
+
const accessMode = value.access & OPEN4_SHARE_ACCESS_MASK;
|
|
2637
|
+
const delegationWant = value.access & OPEN4_SHARE_ACCESS_WANT_DELEG_MASK;
|
|
2638
|
+
const unknownBits = value.access &
|
|
2639
|
+
~(OPEN4_SHARE_ACCESS_MASK | OPEN4_SHARE_ACCESS_WANT_DELEG_MASK | OPEN4_SHARE_ACCESS_WANT_HINT_MASK);
|
|
2640
|
+
if (accessMode === 0 || unknownBits !== 0 || delegationWant > OPEN4_SHARE_ACCESS_WANT_CANCEL ||
|
|
2641
|
+
value.deny > OPEN4_SHARE_DENY_BOTH) {
|
|
2642
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
2643
|
+
}
|
|
2644
|
+
const wantsWrite = (accessMode & OPEN4_SHARE_ACCESS_WRITE) !== 0;
|
|
2645
|
+
if (value.openHow !== 0) {
|
|
2646
|
+
// Section 18.16.3: OPEN4_CREATE needs CLAIM_NULL here (the delegation claims
|
|
2647
|
+
// were answered above). Structural errors keep their precedence over ROFS.
|
|
2648
|
+
if (value.claim !== 0)
|
|
2649
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
2650
|
+
if (!options.writable)
|
|
2651
|
+
return rejectMutation([value.name], "none", Status.SYMLINK);
|
|
2652
|
+
return Effect.uninterruptibleMask((restore) => Effect.gen(function* () {
|
|
2653
|
+
yield* restore(requireDirectory(directory, Status.SYMLINK));
|
|
2654
|
+
const create = value.create;
|
|
2655
|
+
const found = yield* restore(export_.lookup(directory, value.name).pipe(Effect.catch((error) => error instanceof InvalidNameError || error.code !== "NotFound"
|
|
2656
|
+
? Effect.fail(nameStatus(error)) :
|
|
2657
|
+
Effect.void)));
|
|
2658
|
+
const observed = found === undefined
|
|
2659
|
+
? undefined
|
|
2660
|
+
: yield* restore(mapFs(export_.observeMetadata(found)));
|
|
2661
|
+
if (observed !== undefined && create.mode === 1)
|
|
2662
|
+
return yield* Effect.fail(Status.EXIST);
|
|
2663
|
+
// Existing-file creates ignore initial values; the attribute mask and
|
|
2664
|
+
// XDR structure still obey the selected create mode's restrictions.
|
|
2665
|
+
const attrs = yield* creationAttributes(create, options.limits, observed !== undefined);
|
|
2666
|
+
if (observed !== undefined) {
|
|
2667
|
+
if (create.mode >= 2) {
|
|
2668
|
+
const times = attrs.settings.times;
|
|
2669
|
+
if (observed.value.kind !== "file" || times.access.kind !== "value" ||
|
|
2670
|
+
times.modification.kind !== "value" ||
|
|
2671
|
+
observed.value.atimeNs !== times.access.nanoseconds ||
|
|
2672
|
+
observed.value.mtimeNs !== times.modification.nanoseconds)
|
|
2673
|
+
return yield* Effect.fail(Status.EXIST);
|
|
2674
|
+
}
|
|
2675
|
+
if (observed.value.kind !== "file") {
|
|
2676
|
+
return yield* Effect.fail(observed.value.kind === "symlink" ? Status.SYMLINK : Status.ISDIR);
|
|
2677
|
+
}
|
|
2678
|
+
}
|
|
2679
|
+
const owner = bytesKey(value.owner);
|
|
2680
|
+
const existing = found === undefined ?
|
|
2681
|
+
undefined :
|
|
2682
|
+
[...opens.values()].find((open) => open.client === activeSession.client && open.owner === owner && open.reference === found);
|
|
2683
|
+
if (found !== undefined && [...opens.values()].some((open) => open.reference === found &&
|
|
2684
|
+
((open.deny & accessMode) !== 0 || (value.deny & open.access) !== 0))) {
|
|
2685
|
+
return yield* Effect.fail(Status.SHARE_DENIED);
|
|
2686
|
+
}
|
|
2687
|
+
if (existing === undefined && opens.size >= options.limits.maxOpens) {
|
|
2688
|
+
return yield* Effect.fail(Status.DELAY);
|
|
2689
|
+
}
|
|
2690
|
+
const truncate = create.mode === 0 && attrs.settings.initialSize === 0n && observed !== undefined;
|
|
2691
|
+
if (truncate && !wantsWrite)
|
|
2692
|
+
return yield* Effect.fail(Status.INVAL);
|
|
2693
|
+
const opened = yield* restore(export_.openChild(directory, value.name, {
|
|
2694
|
+
...attrs.settings,
|
|
2695
|
+
access: accessMode === 3 ? "readWrite" : wantsWrite ? "write" : "read",
|
|
2696
|
+
create: found === undefined ? "exclusive" : "ifMissing",
|
|
2697
|
+
followFinalSymlink: false,
|
|
2698
|
+
truncate,
|
|
2699
|
+
expectedChild: found === undefined ? null : {
|
|
2700
|
+
reference: found,
|
|
2701
|
+
revision: observed.revision,
|
|
2702
|
+
atimeNs: observed.value.atimeNs,
|
|
2703
|
+
mtimeNs: observed.value.mtimeNs
|
|
2704
|
+
}
|
|
2705
|
+
}).pipe(Effect.mapError((error) => {
|
|
2706
|
+
if (error instanceof ExportCapacityError)
|
|
2707
|
+
return Status.DELAY;
|
|
2708
|
+
if (error instanceof InvalidNameError)
|
|
2709
|
+
return nameStatus(error);
|
|
2710
|
+
if (error.code === "StaleReference")
|
|
2711
|
+
return Status.DELAY;
|
|
2712
|
+
return failureForFs(error);
|
|
2713
|
+
})));
|
|
2714
|
+
let id;
|
|
2715
|
+
if (existing === undefined) {
|
|
2716
|
+
id = makeStateId(options.generation, openSerial++, 1);
|
|
2717
|
+
opens.set(stateIdKey(id), {
|
|
2718
|
+
id,
|
|
2719
|
+
sequence: 1,
|
|
2720
|
+
access: accessMode,
|
|
2721
|
+
deny: value.deny,
|
|
2722
|
+
owner,
|
|
2723
|
+
client: activeSession.client,
|
|
2724
|
+
reference: opened.reference,
|
|
2725
|
+
readFile: accessMode & OPEN4_SHARE_ACCESS_READ ? opened.handle : undefined,
|
|
2726
|
+
writeFile: wantsWrite ? opened.handle : undefined,
|
|
2727
|
+
close: opened.close
|
|
2728
|
+
});
|
|
2729
|
+
}
|
|
2730
|
+
else {
|
|
2731
|
+
const needsRead = (accessMode & OPEN4_SHARE_ACCESS_READ) !== 0 &&
|
|
2732
|
+
existing.readFile === undefined;
|
|
2733
|
+
const needsWrite = wantsWrite && existing.writeFile === undefined;
|
|
2734
|
+
if (needsRead || needsWrite) {
|
|
2735
|
+
if (needsRead)
|
|
2736
|
+
existing.readFile = opened.handle;
|
|
2737
|
+
if (needsWrite)
|
|
2738
|
+
existing.writeFile = opened.handle;
|
|
2739
|
+
existing.close = opened.close.pipe(Effect.andThen(existing.close));
|
|
2740
|
+
}
|
|
2741
|
+
else
|
|
2742
|
+
yield* opened.close;
|
|
2743
|
+
existing.access |= accessMode;
|
|
2744
|
+
existing.deny |= value.deny;
|
|
2745
|
+
advanceStateId(existing);
|
|
2746
|
+
id = existing.id;
|
|
2747
|
+
}
|
|
2748
|
+
current = opened.reference;
|
|
2749
|
+
const applied = opened.created ? attrs.attributes : truncate ? [4] : [];
|
|
2750
|
+
return openResult(id, opened.directory.before, true, opened.directory.after, create.mode >= 2 ? [...applied, 47, 53] : applied);
|
|
2751
|
+
})).pipe(Effect.catch((status) => Effect.succeed({ code: operation.code, status })));
|
|
2752
|
+
}
|
|
2753
|
+
const target = value.claim === 4
|
|
2754
|
+
? Effect.succeed({ revision: 0n, reference: directory })
|
|
2755
|
+
: requireDirectory(directory, Status.SYMLINK).pipe(Effect.andThen(mapFs(export_.observeMetadata(directory))), Effect.flatMap((directoryObservation) => export_.lookup(directory, value.name).pipe(Effect.mapError(nameStatus), Effect.map((reference) => ({ revision: directoryObservation.revision, reference })))));
|
|
2756
|
+
return target.pipe(Effect.tap(({ reference }) => requireRegularFile(reference)),
|
|
2757
|
+
// Write access is refused only once the target is known to be a regular file.
|
|
2758
|
+
Effect.tap(() => wantsWrite && !options.writable ? Effect.fail(Status.ROFS) : Effect.void), Effect.flatMap(({ revision, reference }) => Effect.uninterruptibleMask((restore) => Effect.suspend(() => {
|
|
2759
|
+
const owner = bytesKey(value.owner);
|
|
2760
|
+
const existing = [...opens.values()].find((open) => open.client === activeSession.client && open.owner === owner && open.reference === reference);
|
|
2761
|
+
// Section 9.7 checks every open, including this open-owner's own state.
|
|
2762
|
+
const denied = [...opens.values()].some((open) => open.reference === reference &&
|
|
2763
|
+
((open.deny & accessMode) !== 0 || (value.deny & open.access) !== 0));
|
|
2764
|
+
if (denied) {
|
|
2765
|
+
return Effect.succeed({ code: operation.code, status: Status.SHARE_DENIED });
|
|
2766
|
+
}
|
|
2767
|
+
if (existing !== undefined) {
|
|
2768
|
+
// The same open-owner upgrades its reservation (Section 9.7).
|
|
2769
|
+
// Its mapped caller may have lost access since the earlier OPEN.
|
|
2770
|
+
const nextAccess = existing.access | accessMode;
|
|
2771
|
+
const permission = activeCaller === undefined ?
|
|
2772
|
+
Effect.void :
|
|
2773
|
+
mapFs(activeCaller.accessReference(reference, (accessMode & 1 ? 0o4 : 0) | (accessMode & 2 ? 0o2 : 0)));
|
|
2774
|
+
const addedAccess = nextAccess & ~existing.access;
|
|
2775
|
+
const heldHandle = addedAccess === OPEN4_SHARE_ACCESS_READ
|
|
2776
|
+
? existing.readFile
|
|
2777
|
+
: existing.writeFile;
|
|
2778
|
+
const replacement = addedAccess === 0 || heldHandle !== undefined ?
|
|
2779
|
+
Effect.succeed(null) :
|
|
2780
|
+
mapFs(export_.open(reference, addedAccess === OPEN4_SHARE_ACCESS_READ ? "read" : "write"));
|
|
2781
|
+
let transferred = false;
|
|
2782
|
+
return restore(permission).pipe(Effect.andThen(Effect.acquireUseRelease(restore(replacement), (opened) => opened === null ? Effect.void : Effect.sync(() => {
|
|
2783
|
+
const previousClose = existing.close;
|
|
2784
|
+
if (addedAccess === OPEN4_SHARE_ACCESS_READ)
|
|
2785
|
+
existing.readFile = opened.handle;
|
|
2786
|
+
if (addedAccess === OPEN4_SHARE_ACCESS_WRITE)
|
|
2787
|
+
existing.writeFile = opened.handle;
|
|
2788
|
+
existing.close = opened.close.pipe(Effect.andThen(previousClose));
|
|
2789
|
+
transferred = true;
|
|
2790
|
+
}), (opened) => opened === null || transferred ? Effect.void : opened.close)), Effect.map(() => {
|
|
2791
|
+
existing.access = nextAccess;
|
|
2792
|
+
existing.deny |= value.deny;
|
|
2793
|
+
advanceStateId(existing);
|
|
2794
|
+
current = reference;
|
|
2795
|
+
return openResult(existing.id, revision, value.claim === 4);
|
|
2796
|
+
}));
|
|
2797
|
+
}
|
|
2798
|
+
if (opens.size >= options.limits.maxOpens) {
|
|
2799
|
+
return Effect.succeed({ code: operation.code, status: Status.DELAY });
|
|
2800
|
+
}
|
|
2801
|
+
const serial = openSerial++;
|
|
2802
|
+
return restore(mapFs(export_.open(reference, accessMode === 3 ? "readWrite" : wantsWrite ? "write" : "read"))).pipe(Effect.map((opened) => {
|
|
2803
|
+
const id = makeStateId(options.generation, serial, 1);
|
|
2804
|
+
opens.set(stateIdKey(id), {
|
|
2805
|
+
id,
|
|
2806
|
+
sequence: 1,
|
|
2807
|
+
access: accessMode,
|
|
2808
|
+
deny: value.deny,
|
|
2809
|
+
owner,
|
|
2810
|
+
client: activeSession.client,
|
|
2811
|
+
reference,
|
|
2812
|
+
readFile: (accessMode & OPEN4_SHARE_ACCESS_READ) !== 0 ? opened.handle : undefined,
|
|
2813
|
+
writeFile: wantsWrite ? opened.handle : undefined,
|
|
2814
|
+
close: opened.close
|
|
2815
|
+
});
|
|
2816
|
+
current = reference;
|
|
2817
|
+
return openResult(id, revision, value.claim === 4);
|
|
2818
|
+
}));
|
|
2819
|
+
}))), Effect.catch((status) => Effect.succeed({ code: operation.code, status })));
|
|
2820
|
+
function openResult(id, revision, atomic, after = revision, attributes = []) {
|
|
2821
|
+
currentStateid = id;
|
|
2822
|
+
const body = encodeStatusBody((writer) => {
|
|
2823
|
+
writer.fixedOpaque(id).boolean(atomic)
|
|
2824
|
+
.uint64(BigInt.asUintN(64, revision))
|
|
2825
|
+
.uint64(BigInt.asUintN(64, after)).uint32(0);
|
|
2826
|
+
writeBitmap(writer, wordsFor(attributes));
|
|
2827
|
+
if (delegationWant === 0) {
|
|
2828
|
+
writer.uint32(OPEN_DELEGATE_NONE);
|
|
2829
|
+
}
|
|
2830
|
+
else {
|
|
2831
|
+
// Section 18.16.3: a want that is not satisfied MUST be answered with
|
|
2832
|
+
// OPEN_DELEGATE_NONE_EXT and a reason. NOT_WANTED and CANCELLED carry no body,
|
|
2833
|
+
// and NOT_SUPP_FTYPE is the honest reason for a server without delegations.
|
|
2834
|
+
const why = delegationWant === OPEN4_SHARE_ACCESS_WANT_NO_DELEG
|
|
2835
|
+
? WND4_NOT_WANTED
|
|
2836
|
+
: delegationWant === OPEN4_SHARE_ACCESS_WANT_CANCEL
|
|
2837
|
+
? WND4_CANCELLED
|
|
2838
|
+
: WND4_NOT_SUPP_FTYPE;
|
|
2839
|
+
writer.uint32(OPEN_DELEGATE_NONE_EXT).uint32(why);
|
|
2840
|
+
}
|
|
2841
|
+
});
|
|
2842
|
+
return { code: operation.code, status: Status.OK, body };
|
|
2843
|
+
}
|
|
2844
|
+
}
|
|
2845
|
+
case "Read": {
|
|
2846
|
+
// RFC 8881 Section 18.22.3 lets the server return fewer bytes than requested.
|
|
2847
|
+
const value = {
|
|
2848
|
+
...operation.value,
|
|
2849
|
+
count: Math.min(operation.value.count, ByteSize.toNumberUnsafe(options.limits.maxReadBytes))
|
|
2850
|
+
};
|
|
2851
|
+
if (current === undefined)
|
|
2852
|
+
return Effect.succeed(noCurrent());
|
|
2853
|
+
const readPermission = activeCaller === undefined
|
|
2854
|
+
? Effect.void
|
|
2855
|
+
: activeCaller.accessReference(current, 0o4).pipe(Effect.mapError(failureForFs));
|
|
2856
|
+
if (isAllZero(value.stateid) || isAllOnes(value.stateid)) {
|
|
2857
|
+
const reference = current;
|
|
2858
|
+
// Section 9.1.2: the anonymous stateid must still respect a deny-read share
|
|
2859
|
+
// reservation; the READ-bypass stateid (all ones) may ignore it.
|
|
2860
|
+
const denied = isAllZero(value.stateid) &&
|
|
2861
|
+
[...opens.values()].some((open) => open.reference === reference && (open.deny & OPEN4_SHARE_DENY_READ) !== 0);
|
|
2862
|
+
return requireRegularFile(reference).pipe(Effect.andThen(denied ? Effect.fail(Status.LOCKED) : Effect.void), Effect.andThen(readPermission), Effect.andThen(Effect.acquireUseRelease(mapFs(export_.open(reference)), (opened) => readFrom(opened.handle), (opened) => opened.close)), Effect.catch((status) => Effect.succeed({ code: operation.code, status })));
|
|
2863
|
+
}
|
|
2864
|
+
const effectiveStateid = isCurrentStateId(value.stateid) ? currentStateid : value.stateid;
|
|
2865
|
+
if (effectiveStateid === undefined) {
|
|
2866
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
2867
|
+
}
|
|
2868
|
+
const stateKey = stateIdKey(effectiveStateid);
|
|
2869
|
+
const lock = lockStates.get(stateKey);
|
|
2870
|
+
const open = opens.get(stateKey) ?? lock?.open;
|
|
2871
|
+
if (open === undefined)
|
|
2872
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
2873
|
+
const suppliedSequence = stateIdSequence(effectiveStateid);
|
|
2874
|
+
const stateSequence = lock?.sequence ?? open.sequence;
|
|
2875
|
+
if (suppliedSequence !== 0 && suppliedSequence < stateSequence) {
|
|
2876
|
+
return Effect.succeed({ code: operation.code, status: Status.OLD_STATEID });
|
|
2877
|
+
}
|
|
2878
|
+
if (suppliedSequence > stateSequence) {
|
|
2879
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
2880
|
+
}
|
|
2881
|
+
if (activeSession === undefined || current === undefined || open.client !== activeSession.client ||
|
|
2882
|
+
open.reference !== current) {
|
|
2883
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
2884
|
+
}
|
|
2885
|
+
if ((open.access & OPEN4_SHARE_ACCESS_READ) === 0) {
|
|
2886
|
+
return Effect.succeed({ code: operation.code, status: Status.OPENMODE });
|
|
2887
|
+
}
|
|
2888
|
+
if (open.readFile === undefined) {
|
|
2889
|
+
return Effect.succeed({ code: operation.code, status: Status.SERVERFAULT });
|
|
2890
|
+
}
|
|
2891
|
+
return readPermission.pipe(Effect.andThen(readFrom(open.readFile)), Effect.catch((status) => Effect.succeed({ code: operation.code, status })));
|
|
2892
|
+
function readFrom(file) {
|
|
2893
|
+
return file.pread(value.count, value.offset).pipe(Effect.flatMap((data) => file.stat.pipe(Effect.map((metadata) => ({ data, metadata })))), Effect.map(({ data, metadata }) => ({
|
|
2894
|
+
code: operation.code,
|
|
2895
|
+
status: Status.OK,
|
|
2896
|
+
body: new Writer().boolean(value.offset + BigInt(data.length) >= metadata.size).opaque(data)
|
|
2897
|
+
.bytes()
|
|
2898
|
+
})), Effect.catch((error) => Effect.succeed({ code: operation.code, status: failureForFs(error) })));
|
|
2899
|
+
}
|
|
2900
|
+
}
|
|
2901
|
+
case "OpenDowngrade": {
|
|
2902
|
+
if (current === undefined)
|
|
2903
|
+
return Effect.succeed(noCurrent());
|
|
2904
|
+
const value = operation.value;
|
|
2905
|
+
// Section 16.2.3: the special current stateid refers to a preceding OPEN.
|
|
2906
|
+
const stateid = isCurrentStateId(value.stateid) ? currentStateid : value.stateid;
|
|
2907
|
+
if (stateid === undefined)
|
|
2908
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
2909
|
+
const open = opens.get(stateIdKey(stateid));
|
|
2910
|
+
if (open === undefined)
|
|
2911
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
2912
|
+
const stateidStatus = checkOpenStateId(stateid, open);
|
|
2913
|
+
if (stateidStatus !== Status.OK)
|
|
2914
|
+
return Effect.succeed({ code: operation.code, status: stateidStatus });
|
|
2915
|
+
// Section 18.18.3: delegation want bits are masked off, and the new modes must be
|
|
2916
|
+
// non-empty subsets of what is held.
|
|
2917
|
+
const access = value.access & ~OPEN4_SHARE_ACCESS_WANT_DELEG_MASK;
|
|
2918
|
+
if (access === 0 || (access & ~open.access) !== 0 || (value.deny & ~open.deny) !== 0) {
|
|
2919
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
2920
|
+
}
|
|
2921
|
+
open.access = access;
|
|
2922
|
+
open.deny = value.deny;
|
|
2923
|
+
advanceStateId(open);
|
|
2924
|
+
currentStateid = open.id;
|
|
2925
|
+
return Effect.succeed({
|
|
2926
|
+
code: operation.code,
|
|
2927
|
+
status: Status.OK,
|
|
2928
|
+
body: new Writer().fixedOpaque(open.id).bytes()
|
|
2929
|
+
});
|
|
2930
|
+
}
|
|
2931
|
+
case "FreeStateid": {
|
|
2932
|
+
if (activeSession === undefined) {
|
|
2933
|
+
return Effect.succeed({ code: operation.code, status: Status.BADSESSION });
|
|
2934
|
+
}
|
|
2935
|
+
const open = isSpecialStateId(operation.value) ? undefined : opens.get(stateIdKey(operation.value));
|
|
2936
|
+
if (open === undefined && !isSpecialStateId(operation.value)) {
|
|
2937
|
+
const key = stateIdKey(operation.value);
|
|
2938
|
+
const lock = lockStates.get(key);
|
|
2939
|
+
if (lock !== undefined && lock.client === activeSession.client) {
|
|
2940
|
+
const status = checkStateIdSequence(operation.value, lock);
|
|
2941
|
+
if (status !== Status.OK)
|
|
2942
|
+
return Effect.succeed({ code: operation.code, status });
|
|
2943
|
+
if (lock.ranges.length !== 0) {
|
|
2944
|
+
return Effect.succeed({ code: operation.code, status: Status.LOCKS_HELD });
|
|
2945
|
+
}
|
|
2946
|
+
lockStates.delete(key);
|
|
2947
|
+
return Effect.succeed({ code: operation.code, status: Status.OK });
|
|
2948
|
+
}
|
|
2949
|
+
}
|
|
2950
|
+
// An open stateid still backs a live open, so it cannot be freed (Section 18.38.3).
|
|
2951
|
+
return Effect.succeed({
|
|
2952
|
+
code: operation.code,
|
|
2953
|
+
status: open === undefined || open.client !== activeSession.client
|
|
2954
|
+
? Status.BAD_STATEID
|
|
2955
|
+
: Status.LOCKS_HELD
|
|
2956
|
+
});
|
|
2957
|
+
}
|
|
2958
|
+
case "TestStateid": {
|
|
2959
|
+
if (activeSession === undefined) {
|
|
2960
|
+
return Effect.succeed({ code: operation.code, status: Status.BADSESSION });
|
|
2961
|
+
}
|
|
2962
|
+
const session = activeSession;
|
|
2963
|
+
// TEST_STATEID checks stateids alone; it does not involve the current filehandle.
|
|
2964
|
+
const results = operation.value.map((stateid) => {
|
|
2965
|
+
if (isSpecialStateId(stateid))
|
|
2966
|
+
return Status.BAD_STATEID;
|
|
2967
|
+
const open = opens.get(stateIdKey(stateid));
|
|
2968
|
+
if (open === undefined) {
|
|
2969
|
+
const lock = lockStates.get(stateIdKey(stateid));
|
|
2970
|
+
return lock === undefined || lock.client !== session.client
|
|
2971
|
+
? Status.BAD_STATEID
|
|
2972
|
+
: checkStateIdSequence(stateid, lock);
|
|
2973
|
+
}
|
|
2974
|
+
if (open.client !== session.client)
|
|
2975
|
+
return Status.BAD_STATEID;
|
|
2976
|
+
return checkStateIdSequence(stateid, open);
|
|
2977
|
+
});
|
|
2978
|
+
return Effect.succeed({
|
|
2979
|
+
code: operation.code,
|
|
2980
|
+
status: Status.OK,
|
|
2981
|
+
body: new Writer().array(results, (writer, status) => writer.uint32(status)).bytes()
|
|
2982
|
+
});
|
|
2983
|
+
}
|
|
2984
|
+
case "SetSsv":
|
|
2985
|
+
// State protection is always SP4_NONE (RFC 8881 Section 18.47.3).
|
|
2986
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
2987
|
+
case "Lock": {
|
|
2988
|
+
if (current === undefined)
|
|
2989
|
+
return Effect.succeed(noCurrent());
|
|
2990
|
+
const value = operation.value;
|
|
2991
|
+
const range = lockRange(value.offset, value.length);
|
|
2992
|
+
return requireRegularFile(current).pipe(Effect.andThen(Effect.sync(() => {
|
|
2993
|
+
if (range === undefined)
|
|
2994
|
+
return { code: operation.code, status: Status.INVAL };
|
|
2995
|
+
if (value.reclaim)
|
|
2996
|
+
return { code: operation.code, status: Status.NO_GRACE };
|
|
2997
|
+
if (WRITE_LOCK_TYPES.has(value.lockType) && !options.writable) {
|
|
2998
|
+
return { code: operation.code, status: Status.ROFS };
|
|
2999
|
+
}
|
|
3000
|
+
if (activeSession === undefined)
|
|
3001
|
+
return { code: operation.code, status: Status.BADSESSION };
|
|
3002
|
+
const client = activeSession.client;
|
|
3003
|
+
const locker = value.locker;
|
|
3004
|
+
let lock;
|
|
3005
|
+
let created = false;
|
|
3006
|
+
if (locker.kind === "new") {
|
|
3007
|
+
const stateid = isCurrentStateId(locker.stateid) ? currentStateid : locker.stateid;
|
|
3008
|
+
if (stateid === undefined)
|
|
3009
|
+
return { code: operation.code, status: Status.BAD_STATEID };
|
|
3010
|
+
const open = opens.get(stateIdKey(stateid));
|
|
3011
|
+
if (open === undefined)
|
|
3012
|
+
return { code: operation.code, status: Status.BAD_STATEID };
|
|
3013
|
+
const status = checkOpenStateId(stateid, open);
|
|
3014
|
+
if (status !== Status.OK)
|
|
3015
|
+
return { code: operation.code, status };
|
|
3016
|
+
if ((open.access & (WRITE_LOCK_TYPES.has(value.lockType) ? 2 : 1)) === 0) {
|
|
3017
|
+
return { code: operation.code, status: Status.OPENMODE };
|
|
3018
|
+
}
|
|
3019
|
+
const ownerKey = bytesKey(locker.owner);
|
|
3020
|
+
lock = [...lockStates.values()].find((entry) => entry.client === client && entry.ownerKey === ownerKey && entry.open === open);
|
|
3021
|
+
if (lock === undefined) {
|
|
3022
|
+
if (lockStates.size >= options.limits.maxLockOwners) {
|
|
3023
|
+
return { code: operation.code, status: Status.DELAY };
|
|
3024
|
+
}
|
|
3025
|
+
const id = makeStateId(options.generation, openSerial++, 0);
|
|
3026
|
+
lock = {
|
|
3027
|
+
id,
|
|
3028
|
+
sequence: 0,
|
|
3029
|
+
client: activeSession.client,
|
|
3030
|
+
owner: locker.owner,
|
|
3031
|
+
ownerKey,
|
|
3032
|
+
open,
|
|
3033
|
+
ranges: []
|
|
3034
|
+
};
|
|
3035
|
+
created = true;
|
|
3036
|
+
}
|
|
3037
|
+
}
|
|
3038
|
+
else {
|
|
3039
|
+
const stateid = isCurrentStateId(locker.stateid) ? currentStateid : locker.stateid;
|
|
3040
|
+
if (stateid === undefined)
|
|
3041
|
+
return { code: operation.code, status: Status.BAD_STATEID };
|
|
3042
|
+
lock = lockStates.get(stateIdKey(stateid));
|
|
3043
|
+
if (lock === undefined)
|
|
3044
|
+
return { code: operation.code, status: Status.BAD_STATEID };
|
|
3045
|
+
const status = checkLockStateId(stateid, lock);
|
|
3046
|
+
if (status !== Status.OK)
|
|
3047
|
+
return { code: operation.code, status };
|
|
3048
|
+
if ((lock.open.access & (WRITE_LOCK_TYPES.has(value.lockType) ? 2 : 1)) === 0) {
|
|
3049
|
+
return { code: operation.code, status: Status.OPENMODE };
|
|
3050
|
+
}
|
|
3051
|
+
}
|
|
3052
|
+
const conflict = conflictingLock(lock.open.reference, lock.client, lock.ownerKey, range, value.lockType);
|
|
3053
|
+
if (conflict !== undefined)
|
|
3054
|
+
return deniedLock(operation.code, conflict);
|
|
3055
|
+
const ownerStates = [...lockStates.values()].filter((state) => state.client === lock.client && state.ownerKey === lock.ownerKey &&
|
|
3056
|
+
state.open.reference === lock.open.reference);
|
|
3057
|
+
if (created)
|
|
3058
|
+
ownerStates.push(lock);
|
|
3059
|
+
const updates = ownerStates.map((state) => ({
|
|
3060
|
+
state,
|
|
3061
|
+
next: replaceLockRange(state.ranges, range, state === lock ? (WRITE_LOCK_TYPES.has(value.lockType) ? 2 : 1) : undefined)
|
|
3062
|
+
}));
|
|
3063
|
+
const nextCount = updates.reduce((count, update) => count + update.next.length - update.state.ranges.length, lockCount);
|
|
3064
|
+
if (nextCount > options.limits.maxLocks) {
|
|
3065
|
+
return { code: operation.code, status: Status.DELAY };
|
|
3066
|
+
}
|
|
3067
|
+
if (created)
|
|
3068
|
+
lockStates.set(stateIdKey(lock.id), lock);
|
|
3069
|
+
const ownNext = updates.find((update) => update.state === lock).next;
|
|
3070
|
+
const changed = lock.ranges.length !== ownNext.length ||
|
|
3071
|
+
lock.ranges.some((entry, index) => entry.range.offset !== ownNext[index].range.offset ||
|
|
3072
|
+
entry.range.end !== ownNext[index].range.end ||
|
|
3073
|
+
entry.type !== ownNext[index].type);
|
|
3074
|
+
lockCount = nextCount;
|
|
3075
|
+
for (const update of updates) {
|
|
3076
|
+
update.state.ranges.splice(0, update.state.ranges.length, ...update.next);
|
|
3077
|
+
}
|
|
3078
|
+
if (changed)
|
|
3079
|
+
advanceStateId(lock);
|
|
3080
|
+
currentStateid = lock.id;
|
|
3081
|
+
return { code: operation.code, status: Status.OK, body: new Writer().fixedOpaque(lock.id).bytes() };
|
|
3082
|
+
})), Effect.catch((status) => Effect.succeed({ code: operation.code, status })));
|
|
3083
|
+
}
|
|
3084
|
+
case "Lockt": {
|
|
3085
|
+
if (current === undefined)
|
|
3086
|
+
return Effect.succeed(noCurrent());
|
|
3087
|
+
const reference = current;
|
|
3088
|
+
const value = operation.value;
|
|
3089
|
+
const range = lockRange(value.offset, value.length);
|
|
3090
|
+
return requireRegularFile(reference).pipe(Effect.map(() => {
|
|
3091
|
+
if (range === undefined)
|
|
3092
|
+
return { code: operation.code, status: Status.INVAL };
|
|
3093
|
+
if (WRITE_LOCK_TYPES.has(value.lockType) && !options.writable) {
|
|
3094
|
+
return { code: operation.code, status: Status.ROFS };
|
|
3095
|
+
}
|
|
3096
|
+
if (activeSession === undefined)
|
|
3097
|
+
return { code: operation.code, status: Status.BADSESSION };
|
|
3098
|
+
const conflict = conflictingLock(reference, activeSession.client, bytesKey(value.owner), range, value.lockType);
|
|
3099
|
+
return conflict === undefined
|
|
3100
|
+
? { code: operation.code, status: Status.OK }
|
|
3101
|
+
: deniedLock(operation.code, conflict);
|
|
3102
|
+
}), Effect.catch((status) => Effect.succeed({ code: operation.code, status })));
|
|
3103
|
+
}
|
|
3104
|
+
case "Locku": {
|
|
3105
|
+
if (current === undefined)
|
|
3106
|
+
return Effect.succeed(noCurrent());
|
|
3107
|
+
const value = operation.value;
|
|
3108
|
+
const range = lockRange(value.offset, value.length);
|
|
3109
|
+
if (range === undefined)
|
|
3110
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
3111
|
+
const stateid = isCurrentStateId(value.stateid) ? currentStateid : value.stateid;
|
|
3112
|
+
if (stateid === undefined)
|
|
3113
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
3114
|
+
const lock = lockStates.get(stateIdKey(stateid));
|
|
3115
|
+
if (lock === undefined)
|
|
3116
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
3117
|
+
const status = checkLockStateId(stateid, lock);
|
|
3118
|
+
if (status !== Status.OK)
|
|
3119
|
+
return Effect.succeed({ code: operation.code, status });
|
|
3120
|
+
const ownerStates = [...lockStates.values()].filter((state) => state.client === lock.client && state.ownerKey === lock.ownerKey &&
|
|
3121
|
+
state.open.reference === lock.open.reference);
|
|
3122
|
+
if (!ownerStates.some((state) => state.ranges.some((entry) => overlaps(entry.range, range)))) {
|
|
3123
|
+
return Effect.succeed({ code: operation.code, status: Status.LOCK_RANGE });
|
|
3124
|
+
}
|
|
3125
|
+
const updates = ownerStates.map((state) => ({ state, next: replaceLockRange(state.ranges, range) }));
|
|
3126
|
+
const nextCount = updates.reduce((count, update) => count + update.next.length - update.state.ranges.length, lockCount);
|
|
3127
|
+
if (nextCount > options.limits.maxLocks) {
|
|
3128
|
+
return Effect.succeed({ code: operation.code, status: Status.DELAY });
|
|
3129
|
+
}
|
|
3130
|
+
lockCount = nextCount;
|
|
3131
|
+
for (const update of updates) {
|
|
3132
|
+
update.state.ranges.splice(0, update.state.ranges.length, ...update.next);
|
|
3133
|
+
}
|
|
3134
|
+
advanceStateId(lock);
|
|
3135
|
+
currentStateid = lock.id;
|
|
3136
|
+
return Effect.succeed({
|
|
3137
|
+
code: operation.code,
|
|
3138
|
+
status: Status.OK,
|
|
3139
|
+
body: new Writer().fixedOpaque(lock.id).bytes()
|
|
3140
|
+
});
|
|
3141
|
+
}
|
|
3142
|
+
case "Close": {
|
|
3143
|
+
if (current === undefined)
|
|
3144
|
+
return Effect.succeed(noCurrent());
|
|
3145
|
+
const value = operation.value;
|
|
3146
|
+
// Section 16.2.3: the special current stateid refers to a preceding OPEN.
|
|
3147
|
+
const stateid = isCurrentStateId(value.stateid) ? currentStateid : value.stateid;
|
|
3148
|
+
if (stateid === undefined)
|
|
3149
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
3150
|
+
const key = stateIdKey(stateid);
|
|
3151
|
+
const open = opens.get(key);
|
|
3152
|
+
if (open === undefined)
|
|
3153
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
3154
|
+
const stateidStatus = checkOpenStateId(stateid, open);
|
|
3155
|
+
if (stateidStatus !== Status.OK)
|
|
3156
|
+
return Effect.succeed({ code: operation.code, status: stateidStatus });
|
|
3157
|
+
if ([...lockStates.values()].some((lock) => lock.open === open && lock.ranges.length > 0)) {
|
|
3158
|
+
return Effect.succeed({ code: operation.code, status: Status.LOCKS_HELD });
|
|
3159
|
+
}
|
|
3160
|
+
const closedStateid = new Uint8Array(open.id);
|
|
3161
|
+
new DataView(closedStateid.buffer).setUint32(0, open.sequence + 1);
|
|
3162
|
+
// Closing the handle and dropping it from `opens` are one region. An interrupt
|
|
3163
|
+
// delivered between them would leave a closed handle in the map for the handler
|
|
3164
|
+
// scope's finalizer to close a second time.
|
|
3165
|
+
return Effect.uninterruptible(open.close.pipe(Effect.tap(() => Effect.sync(() => {
|
|
3166
|
+
opens.delete(key);
|
|
3167
|
+
for (const [lockKey, lock] of lockStates) {
|
|
3168
|
+
if (lock.open === open)
|
|
3169
|
+
lockStates.delete(lockKey);
|
|
3170
|
+
}
|
|
3171
|
+
currentStateid = closedStateid;
|
|
3172
|
+
})))).pipe(Effect.as({
|
|
3173
|
+
code: operation.code,
|
|
3174
|
+
status: Status.OK,
|
|
3175
|
+
body: new Writer().fixedOpaque(closedStateid).bytes()
|
|
3176
|
+
}));
|
|
3177
|
+
}
|
|
3178
|
+
case "Write": {
|
|
3179
|
+
if (current === undefined)
|
|
3180
|
+
return Effect.succeed(noCurrent());
|
|
3181
|
+
if (!options.writable)
|
|
3182
|
+
return Effect.succeed({ code: operation.code, status: Status.ROFS });
|
|
3183
|
+
const value = operation.value;
|
|
3184
|
+
const reference = current;
|
|
3185
|
+
const stateid = isCurrentStateId(value.stateid) ? currentStateid : value.stateid;
|
|
3186
|
+
if (value.stable > 2)
|
|
3187
|
+
return Effect.succeed({ code: operation.code, status: Status.INVAL });
|
|
3188
|
+
if (stateid === undefined || isAllZero(stateid) || isAllOnes(stateid)) {
|
|
3189
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
3190
|
+
}
|
|
3191
|
+
const key = stateIdKey(stateid);
|
|
3192
|
+
const lock = lockStates.get(key);
|
|
3193
|
+
const open = opens.get(key) ?? lock?.open;
|
|
3194
|
+
if (open === undefined)
|
|
3195
|
+
return Effect.succeed({ code: operation.code, status: Status.BAD_STATEID });
|
|
3196
|
+
const stateStatus = lock === undefined
|
|
3197
|
+
? checkOpenStateId(stateid, open)
|
|
3198
|
+
: checkLockStateId(stateid, lock);
|
|
3199
|
+
if (stateStatus !== Status.OK)
|
|
3200
|
+
return Effect.succeed({ code: operation.code, status: stateStatus });
|
|
3201
|
+
if ((open.access & OPEN4_SHARE_ACCESS_WRITE) === 0) {
|
|
3202
|
+
return Effect.succeed({ code: operation.code, status: Status.OPENMODE });
|
|
3203
|
+
}
|
|
3204
|
+
if (open.writeFile === undefined) {
|
|
3205
|
+
return Effect.succeed({ code: operation.code, status: Status.SERVERFAULT });
|
|
3206
|
+
}
|
|
3207
|
+
const writeFile = open.writeFile;
|
|
3208
|
+
return statusResult(requireRegularFile(reference).pipe(Effect.andThen(activeCaller === undefined
|
|
3209
|
+
? Effect.void
|
|
3210
|
+
: activeCaller.accessReference(reference, 0o2).pipe(Effect.mapError(failureForFs))), Effect.andThen(mapFs(writeFile.pwrite(value.data, value.offset)))), (count) => new Writer().uint32(count).uint32(2).fixedOpaque(writeVerifier).bytes());
|
|
3211
|
+
}
|
|
3212
|
+
case "Setattr": {
|
|
3213
|
+
if (current === undefined)
|
|
3214
|
+
return Effect.succeed(noCurrent());
|
|
3215
|
+
if (!options.writable)
|
|
3216
|
+
return Effect.succeed({ code: operation.code, status: Status.ROFS });
|
|
3217
|
+
const reference = current;
|
|
3218
|
+
const value = operation.value;
|
|
3219
|
+
return Effect.gen(function* () {
|
|
3220
|
+
const applied = [];
|
|
3221
|
+
const reply = (status) => ({
|
|
3222
|
+
code: operation.code,
|
|
3223
|
+
status,
|
|
3224
|
+
body: encodeStatusBody((writer) => writeBitmap(writer, wordsFor(applied)))
|
|
3225
|
+
});
|
|
3226
|
+
const decoded = yield* setattrAttributes(value.attrs, options.limits).pipe(Effect.map((changes) => ({ changes })), Effect.catch((status) => Effect.succeed({ status })));
|
|
3227
|
+
if ("status" in decoded)
|
|
3228
|
+
return reply(decoded.status);
|
|
3229
|
+
if (decoded.changes.some((change) => change.kind === "size")) {
|
|
3230
|
+
const stateid = isCurrentStateId(value.stateid) ? currentStateid : value.stateid;
|
|
3231
|
+
if (stateid === undefined || isAllZero(stateid) || isAllOnes(stateid)) {
|
|
3232
|
+
return reply(Status.BAD_STATEID);
|
|
3233
|
+
}
|
|
3234
|
+
const key = stateIdKey(stateid);
|
|
3235
|
+
const lock = lockStates.get(key);
|
|
3236
|
+
const open = opens.get(key) ?? lock?.open;
|
|
3237
|
+
if (open === undefined || open.reference !== reference)
|
|
3238
|
+
return reply(Status.BAD_STATEID);
|
|
3239
|
+
const stateStatus = lock === undefined
|
|
3240
|
+
? checkOpenStateId(stateid, open)
|
|
3241
|
+
: checkLockStateId(stateid, lock);
|
|
3242
|
+
if (stateStatus !== Status.OK)
|
|
3243
|
+
return reply(stateStatus);
|
|
3244
|
+
if ((open.access & OPEN4_SHARE_ACCESS_WRITE) === 0)
|
|
3245
|
+
return reply(Status.OPENMODE);
|
|
3246
|
+
if ([...opens.values()].some((state) => state.reference === reference && (state.deny & OPEN4_SHARE_ACCESS_WRITE) !== 0))
|
|
3247
|
+
return reply(Status.SHARE_DENIED);
|
|
3248
|
+
}
|
|
3249
|
+
for (const change of decoded.changes) {
|
|
3250
|
+
const mutation = change.kind === "size"
|
|
3251
|
+
? export_.truncate(reference, change.value)
|
|
3252
|
+
: change.kind === "mode"
|
|
3253
|
+
? export_.chmod(reference, change.value)
|
|
3254
|
+
: change.kind === "owner"
|
|
3255
|
+
? export_.chown(reference, change.attribute === 36 ? { uid: change.value } : { gid: change.value })
|
|
3256
|
+
: export_.utimes(reference, {
|
|
3257
|
+
access: change.attribute === 48 ? change.value : { kind: "omit" },
|
|
3258
|
+
modification: change.attribute === 54 ? change.value : { kind: "omit" }
|
|
3259
|
+
});
|
|
3260
|
+
const status = yield* mutation.pipe(Effect.as(Status.OK), Effect.catch((error) => Effect.succeed(change.kind === "owner" && error.code === "AccessDenied" ? Status.PERM : failureForFs(error))));
|
|
3261
|
+
if (status !== Status.OK)
|
|
3262
|
+
return reply(status);
|
|
3263
|
+
applied.push(change.attribute);
|
|
3264
|
+
}
|
|
3265
|
+
return reply(Status.OK);
|
|
3266
|
+
});
|
|
3267
|
+
}
|
|
3268
|
+
// Section 15.2 lists NFS4ERR_SYMLINK for LINK but not for CREATE, REMOVE, or RENAME.
|
|
3269
|
+
case "Create": {
|
|
3270
|
+
if (current === undefined)
|
|
3271
|
+
return Effect.succeed(noCurrent());
|
|
3272
|
+
if (!options.writable)
|
|
3273
|
+
return rejectMutation([operation.value.name], "none", Status.NOTDIR);
|
|
3274
|
+
const value = operation.value;
|
|
3275
|
+
const parent = current;
|
|
3276
|
+
return statusResult(Effect.gen(function* () {
|
|
3277
|
+
yield* requireDirectory(parent, Status.NOTDIR);
|
|
3278
|
+
yield* validName(value.name);
|
|
3279
|
+
if (value.kind !== 2 && value.kind !== 5)
|
|
3280
|
+
return yield* Effect.fail(Status.BADTYPE);
|
|
3281
|
+
const attributes = attributesIn(value.attrs.bitmap);
|
|
3282
|
+
const supported = value.kind === 2 ? [33, 48, 54] : [48, 54];
|
|
3283
|
+
if (!attributes.every((attribute) => supported.includes(attribute))) {
|
|
3284
|
+
return yield* Effect.fail(Status.ATTRNOTSUPP);
|
|
3285
|
+
}
|
|
3286
|
+
const reader = new Reader(value.attrs.values, options.limits);
|
|
3287
|
+
let mode;
|
|
3288
|
+
const times = {
|
|
3289
|
+
access: { kind: "omit" },
|
|
3290
|
+
modification: { kind: "omit" }
|
|
3291
|
+
};
|
|
3292
|
+
for (const attribute of attributes) {
|
|
3293
|
+
if (attribute === 33) {
|
|
3294
|
+
mode = reader.uint32();
|
|
3295
|
+
if (mode > 0o7777)
|
|
3296
|
+
return yield* Effect.fail(Status.INVAL);
|
|
3297
|
+
}
|
|
3298
|
+
else {
|
|
3299
|
+
const how = reader.uint32();
|
|
3300
|
+
const time = how === 0 ? { kind: "now" } : {
|
|
3301
|
+
kind: "value",
|
|
3302
|
+
nanoseconds: BigInt.asIntN(64, reader.uint64()) * 1000000000n + BigInt(reader.uint32())
|
|
3303
|
+
};
|
|
3304
|
+
if (attribute === 48)
|
|
3305
|
+
times.access = time;
|
|
3306
|
+
else
|
|
3307
|
+
times.modification = time;
|
|
3308
|
+
}
|
|
3309
|
+
}
|
|
3310
|
+
reader.finish();
|
|
3311
|
+
const result = yield* (value.kind === 2
|
|
3312
|
+
? export_.mkdir(parent, value.name, mode === undefined ? { times } : { mode, times, exactMode: true })
|
|
3313
|
+
: export_.symlink(value.target, parent, value.name, { times })).pipe(Effect.mapError((error) => error instanceof ExportCapacityError ?
|
|
3314
|
+
Status.DELAY :
|
|
3315
|
+
error instanceof InvalidNameError
|
|
3316
|
+
? nameStatus(error)
|
|
3317
|
+
: failureForFs(error)));
|
|
3318
|
+
setCurrent(result.reference);
|
|
3319
|
+
return { change: result.directory, attributes };
|
|
3320
|
+
}), ({ change, attributes }) => encodeStatusBody((writer) => {
|
|
3321
|
+
writeChangeInfo(writer, change);
|
|
3322
|
+
writeBitmap(writer, wordsFor(attributes));
|
|
3323
|
+
}));
|
|
3324
|
+
}
|
|
3325
|
+
case "Remove": {
|
|
3326
|
+
if (current === undefined)
|
|
3327
|
+
return Effect.succeed(noCurrent());
|
|
3328
|
+
if (!options.writable)
|
|
3329
|
+
return rejectMutation([operation.value], "none", Status.NOTDIR);
|
|
3330
|
+
const directory = current;
|
|
3331
|
+
return statusResult(Effect.gen(function* () {
|
|
3332
|
+
yield* requireDirectory(directory, Status.NOTDIR);
|
|
3333
|
+
yield* validName(operation.value);
|
|
3334
|
+
return yield* mapFs(export_.remove(directory, operation.value));
|
|
3335
|
+
}), (change) => encodeStatusBody((writer) => writeChangeInfo(writer, change)));
|
|
3336
|
+
}
|
|
3337
|
+
case "Rename": {
|
|
3338
|
+
if (current === undefined || saved === undefined)
|
|
3339
|
+
return Effect.succeed(noCurrent());
|
|
3340
|
+
if (!options.writable) {
|
|
3341
|
+
return rejectMutation([operation.value.oldName, operation.value.newName], "directory", Status.NOTDIR);
|
|
3342
|
+
}
|
|
3343
|
+
const destination = current;
|
|
3344
|
+
const source = saved;
|
|
3345
|
+
return statusResult(Effect.gen(function* () {
|
|
3346
|
+
yield* requireDirectory(destination, Status.NOTDIR);
|
|
3347
|
+
yield* requireDirectory(source, Status.NOTDIR);
|
|
3348
|
+
yield* validName(operation.value.oldName);
|
|
3349
|
+
yield* validName(operation.value.newName);
|
|
3350
|
+
return yield* export_.rename(source, operation.value.oldName, destination, operation.value.newName)
|
|
3351
|
+
.pipe(Effect.mapError((error) => error.code === "IsDirectory" || error.code === "NotDirectory" || error.code === "NotEmpty"
|
|
3352
|
+
? Status.EXIST
|
|
3353
|
+
: failureForFs(error)));
|
|
3354
|
+
}), (change) => encodeStatusBody((writer) => {
|
|
3355
|
+
const sourceChange = Predicate.isTagged(change, "SameDirectory")
|
|
3356
|
+
? change.directory
|
|
3357
|
+
: change.sourceDirectory;
|
|
3358
|
+
const destinationChange = Predicate.isTagged(change, "SameDirectory")
|
|
3359
|
+
? change.directory
|
|
3360
|
+
: change.destinationDirectory;
|
|
3361
|
+
writeChangeInfo(writer, sourceChange);
|
|
3362
|
+
writeChangeInfo(writer, destinationChange);
|
|
3363
|
+
}));
|
|
3364
|
+
}
|
|
3365
|
+
case "Link": {
|
|
3366
|
+
if (current === undefined || saved === undefined)
|
|
3367
|
+
return Effect.succeed(noCurrent());
|
|
3368
|
+
if (!options.writable)
|
|
3369
|
+
return rejectMutation([operation.value], "object", Status.SYMLINK);
|
|
3370
|
+
const destination = current;
|
|
3371
|
+
const source = saved;
|
|
3372
|
+
return statusResult(Effect.gen(function* () {
|
|
3373
|
+
yield* requireDirectory(destination, Status.SYMLINK);
|
|
3374
|
+
yield* validName(operation.value);
|
|
3375
|
+
return yield* mapFs(export_.link(source, destination, operation.value));
|
|
3376
|
+
}), (result) => encodeStatusBody((writer) => writeChangeInfo(writer, result.directory)));
|
|
3377
|
+
}
|
|
3378
|
+
case "NotSupported":
|
|
3379
|
+
return Effect.succeed({ code: operation.code, status: Status.NOTSUPP });
|
|
3380
|
+
case "Unknown":
|
|
3381
|
+
return Effect.succeed({ code: Operation.ILLEGAL, status: Status.OP_ILLEGAL });
|
|
3382
|
+
case "Malformed":
|
|
3383
|
+
return Effect.succeed({ code: operation.code, status: Status.BADXDR });
|
|
3384
|
+
}
|
|
3385
|
+
}
|
|
3386
|
+
/** Sets the current filehandle without a returned stateid (RFC 8881 Section 16.2.3.1.2). */
|
|
3387
|
+
function setCurrent(reference) {
|
|
3388
|
+
current = reference;
|
|
3389
|
+
currentStateid = undefined;
|
|
3390
|
+
}
|
|
3391
|
+
function parentOf(reference) {
|
|
3392
|
+
// The root has no parent in this export (RFC 8881 Section 18.14.3).
|
|
3393
|
+
return export_.parent(reference).pipe(Effect.mapError(failureForFs), Effect.filterOrFail((parent) => parent !== reference, () => Status.NOENT));
|
|
3394
|
+
}
|
|
3395
|
+
function nameStatus(error) {
|
|
3396
|
+
if (error instanceof InvalidNameError) {
|
|
3397
|
+
// RFC 8881 Section 14.5: reserved components are BADNAME, over-long names NAMETOOLONG,
|
|
3398
|
+
// valid UTF-8 the file system cannot store (a slash or NUL) BADCHAR, and other
|
|
3399
|
+
// invalid names INVAL.
|
|
3400
|
+
switch (error.reason) {
|
|
3401
|
+
case "Reserved":
|
|
3402
|
+
return Status.BADNAME;
|
|
3403
|
+
case "TooLong":
|
|
3404
|
+
return Status.NAMETOOLONG;
|
|
3405
|
+
case "ForbiddenByte":
|
|
3406
|
+
return Status.BADCHAR;
|
|
3407
|
+
default:
|
|
3408
|
+
return Status.INVAL;
|
|
3409
|
+
}
|
|
3410
|
+
}
|
|
3411
|
+
return failureForFs(error);
|
|
3412
|
+
}
|
|
3413
|
+
function isSpecialStateId(stateid) {
|
|
3414
|
+
return isAllZero(stateid) || isAllOnes(stateid) || isCurrentStateId(stateid);
|
|
3415
|
+
}
|
|
3416
|
+
function checkStateIdSequence(stateid, open) {
|
|
3417
|
+
const suppliedSequence = stateIdSequence(stateid);
|
|
3418
|
+
if (suppliedSequence !== 0 && suppliedSequence < open.sequence)
|
|
3419
|
+
return Status.OLD_STATEID;
|
|
3420
|
+
if (suppliedSequence > open.sequence)
|
|
3421
|
+
return Status.BAD_STATEID;
|
|
3422
|
+
return Status.OK;
|
|
3423
|
+
}
|
|
3424
|
+
function checkOpenStateId(stateid, open) {
|
|
3425
|
+
const sequenceStatus = checkStateIdSequence(stateid, open);
|
|
3426
|
+
if (sequenceStatus !== Status.OK)
|
|
3427
|
+
return sequenceStatus;
|
|
3428
|
+
if (activeSession === undefined || current === undefined || open.client !== activeSession.client ||
|
|
3429
|
+
open.reference !== current) {
|
|
3430
|
+
return Status.BAD_STATEID;
|
|
3431
|
+
}
|
|
3432
|
+
return Status.OK;
|
|
3433
|
+
}
|
|
3434
|
+
function checkLockStateId(stateid, lock) {
|
|
3435
|
+
const sequenceStatus = checkStateIdSequence(stateid, lock);
|
|
3436
|
+
if (sequenceStatus !== Status.OK)
|
|
3437
|
+
return sequenceStatus;
|
|
3438
|
+
if (activeSession === undefined || current === undefined || lock.client !== activeSession.client ||
|
|
3439
|
+
lock.open.reference !== current) {
|
|
3440
|
+
return Status.BAD_STATEID;
|
|
3441
|
+
}
|
|
3442
|
+
return Status.OK;
|
|
3443
|
+
}
|
|
3444
|
+
function advanceStateId(open) {
|
|
3445
|
+
open.sequence += 1;
|
|
3446
|
+
open.id = makeStateId(options.generation, new DataView(open.id.buffer, open.id.byteOffset + 8, 8).getBigUint64(0), open.sequence);
|
|
3447
|
+
}
|
|
3448
|
+
}).pipe(Effect.onInterrupt(() => Effect.sync(() => {
|
|
3449
|
+
if (!consumedSequence)
|
|
3450
|
+
rollbackSequence?.();
|
|
3451
|
+
})));
|
|
3452
|
+
}).pipe(Effect.orDie);
|
|
3453
|
+
/**
|
|
3454
|
+
* Takes the next free backchannel slot. Section 2.10.6.1 requires slot state even for
|
|
3455
|
+
* callbacks, so a slot in flight is never reused for a second concurrent callback.
|
|
3456
|
+
*/
|
|
3457
|
+
const takeCallbackSlot = (back) => {
|
|
3458
|
+
const index = back.slots.findIndex((slot) => !slot.busy);
|
|
3459
|
+
if (index < 0)
|
|
3460
|
+
return undefined;
|
|
3461
|
+
back.slots[index].busy = true;
|
|
3462
|
+
return index;
|
|
3463
|
+
};
|
|
3464
|
+
const probe = (session) => Effect.gen(function* () {
|
|
3465
|
+
const back = session.back;
|
|
3466
|
+
if (back === undefined)
|
|
3467
|
+
return false;
|
|
3468
|
+
// The verdict is only published if the path has not been re-armed meanwhile, so a slow
|
|
3469
|
+
// probe cannot overwrite a newer one's result.
|
|
3470
|
+
const arming = back.arming;
|
|
3471
|
+
const slot = takeCallbackSlot(back);
|
|
3472
|
+
if (slot === undefined) {
|
|
3473
|
+
// Every slot is in flight. This probe never ran, so it must not consume the arming:
|
|
3474
|
+
// otherwise a re-armed path would be left untested forever.
|
|
3475
|
+
back.probed = false;
|
|
3476
|
+
return false;
|
|
3477
|
+
}
|
|
3478
|
+
// Section 2.10.6.1.3: the slot's sequence ID advances only when the callback is
|
|
3479
|
+
// answered NFS4_OK. Advancing it on a timeout would leave the client expecting the
|
|
3480
|
+
// previous value and answering the next callback NFS4ERR_SEQ_MISORDERED forever.
|
|
3481
|
+
const sequence = nextSequenceId(back.slots[slot].sequence);
|
|
3482
|
+
// Section 20.9.3: csa_highest_slotid is the highest slot the server will use.
|
|
3483
|
+
const body = encodeCallbackSequence(session.id, sequence, slot, back.slots.length - 1);
|
|
3484
|
+
// Section 18.46.3 has SEQUENCE report an unusable callback path. A reply counts only if
|
|
3485
|
+
// the client actually handled the callback: PROG_UNAVAIL, AUTH_ERROR and a rejected
|
|
3486
|
+
// CB_SEQUENCE are all well-formed replies from a client with no working path.
|
|
3487
|
+
const accepted = yield* Effect.ensuring(callback(session, CB_COMPOUND_PROCEDURE, body, (reply) => callbackAccepted(reply, options.limits, session.id, slot, sequence)), Effect.sync(() => {
|
|
3488
|
+
back.slots[slot].busy = false;
|
|
3489
|
+
}));
|
|
3490
|
+
// A re-arm while this probe was in flight means its verdict is about a path the client
|
|
3491
|
+
// has already replaced; the probe it triggered decides instead.
|
|
3492
|
+
if (back.arming !== arming)
|
|
3493
|
+
return accepted;
|
|
3494
|
+
back.healthy = accepted;
|
|
3495
|
+
if (accepted)
|
|
3496
|
+
back.slots[slot].sequence = sequence;
|
|
3497
|
+
return accepted;
|
|
3498
|
+
});
|
|
3499
|
+
return {
|
|
3500
|
+
compound: (call) => stateGate.withPermit(
|
|
3501
|
+
// A mask rather than a blanket `uninterruptible`: the sweep and the replay-slot commit
|
|
3502
|
+
// stay atomic, while `executeCompound` reopens the window around each operation so a
|
|
3503
|
+
// compound stalled in the backing store cannot hold scope closure open indefinitely.
|
|
3504
|
+
Effect.gen(function* () {
|
|
3505
|
+
const activeCaller = options.callerFor === undefined ? undefined : yield* options.callerFor(call);
|
|
3506
|
+
if (activeCaller === null)
|
|
3507
|
+
return yield* new RpcPolicyDenied();
|
|
3508
|
+
const activeExport = activeCaller === undefined ? export_ : export_.withCaller(activeCaller);
|
|
3509
|
+
return yield* Effect.uninterruptibleMask((restore) => sweepExpired.pipe(Effect.andThen(executeCompound(call, activeExport, activeCaller, restore))));
|
|
3510
|
+
})),
|
|
3511
|
+
callbackReply: (connection, message) => Effect.sync(() => {
|
|
3512
|
+
if (message.length < 4)
|
|
3513
|
+
return;
|
|
3514
|
+
const xid = new DataView(message.buffer, message.byteOffset, message.byteLength).getUint32(0);
|
|
3515
|
+
const waiting = pendingCallbacks.get(xid);
|
|
3516
|
+
// The reply must arrive on the connection the callback went out on. Otherwise any peer
|
|
3517
|
+
// that guessed a live xid could answer another session's callback and make that
|
|
3518
|
+
// session's backchannel look healthy.
|
|
3519
|
+
if (waiting === undefined || waiting.carrier !== connection)
|
|
3520
|
+
return;
|
|
3521
|
+
pendingCallbacks.delete(xid);
|
|
3522
|
+
Deferred.doneUnsafe(waiting.reply, Effect.succeed(message));
|
|
3523
|
+
}),
|
|
3524
|
+
probeBackChannel: (id) => Effect.suspend(() => {
|
|
3525
|
+
const session = sessions.get(bytesKey(id));
|
|
3526
|
+
return session === undefined ? Effect.succeed(false) : probe(session);
|
|
3527
|
+
}),
|
|
3528
|
+
disconnect: (connection) =>
|
|
3529
|
+
// Deliberately outside `stateGate`. This runs from a connection finalizer, which
|
|
3530
|
+
// `Effect.ensuring` executes uninterruptibly, and `Semaphore.withPermits` waits via
|
|
3531
|
+
// `restore`, which returns to that enclosing uninterruptible status. Taking the gate here
|
|
3532
|
+
// would let one in-flight compound hold up every departing connection with no interrupt
|
|
3533
|
+
// path out of the wait. Safe without it: the body below is a single synchronous mutation,
|
|
3534
|
+
// so it can only interleave with a compound at that compound's own yield points, and both
|
|
3535
|
+
// of its effects are already tolerated there — `callback` snapshots the carriers before it
|
|
3536
|
+
// yields, and `back.arming` exists so a probe in flight discards its stale verdict.
|
|
3537
|
+
Effect.sync(() => {
|
|
3538
|
+
// Section 2.10.5: losing one connection does not end a session that others still
|
|
3539
|
+
// reach, and never ends the lease, which expires on its own schedule. Section 18.37.3
|
|
3540
|
+
// keeps the lease tied to the client ID even across an explicit DESTROY_SESSION, so a
|
|
3541
|
+
// client that never comes back is reclaimed by the sweeper above rather than here.
|
|
3542
|
+
for (const session of sessions.values()) {
|
|
3543
|
+
if (!session.connections.delete(connection) || session.back === undefined)
|
|
3544
|
+
continue;
|
|
3545
|
+
let carries = false;
|
|
3546
|
+
for (const direction of session.connections.values()) {
|
|
3547
|
+
if ((direction & CHANNEL_BACK) !== 0)
|
|
3548
|
+
carries = true;
|
|
3549
|
+
}
|
|
3550
|
+
// A backchannel with no connection left cannot be reached, and a connection that
|
|
3551
|
+
// binds it later deserves a fresh probe rather than the old verdict.
|
|
3552
|
+
if (!carries) {
|
|
3553
|
+
session.back.healthy = false;
|
|
3554
|
+
session.back.probed = false;
|
|
3555
|
+
session.back.arming++;
|
|
3556
|
+
}
|
|
3557
|
+
}
|
|
3558
|
+
})
|
|
3559
|
+
};
|
|
3560
|
+
});
|
|
3561
|
+
};
|