All files / json-pack/src/nfs/v4/server/operations/node Nfsv4OperationsNode.ts

72.09% Statements 726/1007
57.34% Branches 164/286
80% Functions 48/60
75.4% Lines 696/923

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577  26x 26x                           26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x   26x 26x 26x 26x 26x                                                                   26x                   284x     284x   284x           284x     284x         284x   284x   284x     284x   284x   284x             284x               284x 284x 284x 284x 284x 284x 284x           284x 82x 82x 82x                                               218x                         242x 242x 14x 5x                   443x 443x           658x       210x 210x 210x       208x 30x 30x 27x   205x       107x       216x 216x 216x 216x 216x 216x                     104x                   12x 12x       100x       90x       90x 90x 90x 73x 73x 73x 73x 73x   90x       13x 13x                   124x 124x 49x 49x 29x 23x   105x                                                                                                                                                                                                                                       4x 4x             419x 419x                 137x 137x 137x 137x 137x 137x       83x 83x 83x 83x 83x             1x 1x 1x       20x 20x 20x       619x 619x 224x               146x 146x 146x 146x 146x 146x   146x 146x         146x 146x 146x 146x 146x 142x   4x       142x 142x       1x 1x 1x 1x   1x 1x         1x 1x 1x 1x 1x       49x 49x 49x   49x 43x 43x 43x           49x             49x 49x       3x 3x 3x   3x 3x       3x 3x 3x 3x 3x 3x 3x 3x   3x 1x 1x   3x 1x 1x   3x 1x 1x   3x 1x     1x 1x     3x 1x 1x   3x 3x       19x 19x 19x 19x   19x 19x       19x 18x 18x 18x 18x   18x 17x 17x 17x 17x   1x 1x 1x 1x 1x     17x 17x       17x 17x 17x 17x 17x         17x 17x 17x 33x 33x 33x 33x   33x 33x       33x 33x 33x 33x 33x 33x 1x 1x   32x 32x 32x   17x 17x 17x       218x 218x 218x 218x 218x 218x 218x 218x 207x 207x 207x   11x 11x 1x       1x   10x 1x     217x 1x   216x 216x 216x 216x 216x 1x 1x       215x 215x 215x 215x 215x 215x 209x 1x 1x 1x 1x   208x   6x 6x 1x 1x 1x 1x                   213x 3x 3x 3x 3x 3x   210x 210x 210x 210x 36x 36x 36x       210x 88x 122x 60x   62x   210x 210x 210x 210x 210x                   210x 210x 210x 210x 210x 210x 210x 210x 210x 210x 210x 210x 210x 210x                     2x             3x 3x 3x 2x 2x 2x 2x 1x 1x 1x 1x                                                           5x 5x 5x 4x 4x 4x 4x 3x 2x 2x 2x   1x 1x                                                 108x 108x 108x 1x   107x 107x     107x 107x 107x 1x   106x         106x 106x 106x 106x 106x               106x 106x 98x   106x 106x 106x 106x 106x                                           106x 106x 106x 20x 20x   20x 22x 20x 20x     20x     20x 20x     20x 20x 20x               20x     20x 2x 1x   1x     18x 18x 1x 1x 1x   17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x   86x 86x 86x 86x 86x 86x     86x 86x 86x     86x 86x 86x 1x   85x 1x 1x           1x 1x       84x 84x 84x               84x 11x 11x 11x   73x 73x 73x     73x 73x                         73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x 73x       22x 22x 22x 22x 7x 7x 7x   15x       13x 13x 13x 12x 12x 12x 12x 12x 12x 12x 12x 12x     12x 2x 1x   1x   10x 10x 10x 10x 10x 10x   10x 10x 10x 10x 10x 10x                                               30x 30x 30x 30x     30x 30x 30x 30x         30x 30x 30x 30x 30x 30x         30x 30x               4x 4x 4x 4x 3x 3x   1x 1x         26x 26x 26x 26x 21x 9x   12x   19x 19x 19x 19x 19x 19x   7x 7x         16x 16x 16x 16x 16x 16x 16x 16x 16x     15x 15x 15x         15x 15x 14x 14x 14x 14x 14x 14x 14x   1x 1x 1x         62x 62x 62x 61x   61x 61x 61x 61x         61x 61x     61x 61x   59x     61x 61x 61x         61x 61x                                         2x 1x     1x 1x 1x 1x     1x 1x 1x                                   1x               19x 18x 18x 18x 18x 18x 18x 18x 14x 14x 14x 14x 14x 14x 14x               14x 4x 2x 2x   2x 2x 2x 2x 2x 2x 2x               2x         1x 1x     17x 17x 17x 17x 17x 17x 17x 17x 17x   1x 1x         4x 3x 3x 3x 3x 3x 3x 3x 3x 3x               3x 2x 2x 2x 2x       2x               2x 1x 1x               63x 62x 62x 62x 62x 62x 62x         62x 83x 83x 2628x 2628x 73x 73x   7x 7x 7x     6x 6x 6x 6x   6x     6x 6x 6x 6x   6x     43x 43x 43x     5x 5x 5x 5x 5x   5x     5x 5x 5x 5x 5x   5x                                                           1x         61x 6x 6x 6x 6x   61x 5x 5x 5x 5x   61x 61x 61x   61x 61x 61x 61x               3x 2x 2x 2x 2x 2x 2x 1x 1x              
import type {Stats, Dirent} from 'node:fs';
import * as NodePath from 'node:path';
import {randomBytes} from 'node:crypto';
import {
  Nfsv4Access,
  Nfsv4Const,
  Nfsv4Stat,
  Nfsv4OpenAccess,
  Nfsv4OpenClaimType,
  Nfsv4DelegType,
  Nfsv4LockType,
  Nfsv4OpenFlags,
  Nfsv4FType,
  Nfsv4CreateMode,
} from '../../../constants';
import type {Nfsv4OperationCtx, Nfsv4Operations} from '../Nfsv4Operations';
import * as msg from '../../../messages';
import * as struct from '../../../structs';
import {cmpUint8Array} from '@jsonjoy.com/buffers/lib/cmpUint8Array';
import {ClientRecord} from '../ClientRecord';
import {OpenFileState} from '../OpenFileState';
import {OpenOwnerState} from '../OpenOwnerState';
import {LockOwnerState} from '../LockOwnerState';
import {ByteRangeLock} from '../ByteRangeLock';
import {LockStateid} from '../LockStateid';
import {FilesystemStats} from '../FilesystemStats';
import {FileHandleMapper, ROOT_FH} from './fh';
import {isErrCode, normalizeNodeFsError} from './util';
import {Nfsv4StableHow, Nfsv4Attr} from '../../../constants';
import {encodeAttrs} from './attrs';
import {parseBitmask, requiresLstat, attrNumsToBitmap, requiresFsStats} from '../../../attributes';
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
import {XdrEncoder} from '../../../../../xdr/XdrEncoder';
import {XdrDecoder} from '../../../../../xdr/XdrDecoder';
 
export interface Nfsv4OperationsNodeOpts {
  /** Node.js `fs` module. */
  fs: typeof import('node:fs');
 
  /**
   * Absolute path to the root directory to serve. This is some directory on the
   * host filesystem that the NFS server will use as its root.
   */
  dir: string;
 
  /**
   * Maximum number of confirmed clients to allow.
   * @default 1000
   */
  maxClients?: number;
 
  /**
   * Maximum number of pending clients to allow.
   * @default 1000
   */
  maxPendingClients?: number;
 
  /**
   * Optional function to provide filesystem statistics.
   * If not provided, defaults to 2TB available space and 2M available inodes.
   */
  fsStats?: () => Promise<FilesystemStats>;
}
 
/**
 * NFS v4 Operations implementation for Node.js `fs` filesystem.
 */
export class Nfsv4OperationsNode implements Nfsv4Operations {
  protected readonly fs: typeof import('node:fs');
  protected readonly promises: typeof import('node:fs')['promises'];
  protected dir: string;
 
  /**
   * Lease time in seconds.
   * Per RFC 7530 §9.5, this is the time a client has to renew its lease
   * before the server may reclaim its state. Default is 90 seconds.
   */
  protected readonly leaseTime: number = 90;
 
  /** Confirmed clients. */
  protected clients: Map<bigint, ClientRecord> = new Map();
  /** Clients pending SETCLIENTID_CONFIRM confirmation. */
  protected pendingClients: Map<bigint, ClientRecord> = new Map();
  /** Maximum number of client records to keep. */
  protected maxClients;
  /** Maximum number of pending client records to keep. */
  protected maxPendingClients;
  /** Next client ID to assign. */
  protected nextClientId = 1n;
 
  /** Boot stamp, identifies server instance, 16 bits. */
  protected bootStamp: number = Math.round(Math.random() * 0xffff);
 
  protected readonly fh: FileHandleMapper;
 
  /** Next stateid sequence number. */
  protected nextStateidSeqid = 1;
  /** Map from stateid (as string key) to open file state. */
  protected openFiles: Map<string, OpenFileState> = new Map();
  /** Map from open-owner key to owner state. */
  protected openOwners: Map<string, OpenOwnerState> = new Map();
 
  /** Map from lock key to byte-range lock. */
  protected locks: Map<string, ByteRangeLock> = new Map();
  /** Map from lock-owner key to lock-owner state. */
  protected lockOwners: Map<string, LockOwnerState> = new Map();
  /** Map from lock stateid 'other' field to lock stateid state. Per RFC 7530, one stateid per lock-owner per file. */
  protected lockStateids: Map<string, LockStateid> = new Map();
 
  /**
   * Server-wide monotonic change counter for directory change_info.
   * Incremented on every mutating operation (RENAME, REMOVE, CREATE, etc.).
   * Used to populate change_info4 before/after values for client cache validation.
   */
  protected changeCounter: bigint = 0n;
 
  /**
   * Function to retrieve filesystem statistics.
   */
  protected fsStats: () => Promise<FilesystemStats>;
 
  constructor(opts: Nfsv4OperationsNodeOpts) {
    this.fs = opts.fs;
    this.promises = this.fs.promises;
    this.dir = opts.dir;
    this.fh = new FileHandleMapper(this.bootStamp, this.dir);
    this.maxClients = opts.maxClients ?? 1000;
    this.maxPendingClients = opts.maxPendingClients ?? 1000;
    this.fsStats = opts.fsStats ?? this.defaultFsStats;
  }
 
  /**
   * Default filesystem statistics: 2TB available space, 2M available inodes.
   */
  protected defaultFsStats = async (): Promise<FilesystemStats> => {
    const twoTB = BigInt(2 * 1024 * 1024 * 1024 * 1024); // 2TB
    const twoM = BigInt(2 * 1000 * 1000); // 2M inodes
    return new FilesystemStats(twoTB, twoTB, twoTB * 2n, twoM, twoM, twoM * 2n);
  };
 
  protected findClientByIdString(
    map: Map<bigint, ClientRecord>,
    clientIdString: Uint8Array,
  ): [bigint, ClientRecord] | undefined {
    for (const entry of map.entries()) Iif (cmpUint8Array(entry[1].clientIdString, clientIdString)) return entry;
    return;
  }
 
  protected enforceClientLimit(): void {
    Iif (this.clients.size <= this.maxClients) return;
    const firstKey = this.clients.keys().next().value;
    Iif (firstKey !== undefined) this.clients.delete(firstKey);
  }
 
  protected enforcePendingClientLimit(): void {
    Iif (this.pendingClients.size < this.maxPendingClients) return;
    const firstKey = this.pendingClients.keys().next().value;
    Iif (firstKey !== undefined) this.pendingClients.delete(firstKey);
  }
 
  protected makeOpenOwnerKey(clientid: bigint, owner: Uint8Array): string {
    return `${clientid}:${Buffer.from(owner).toString('hex')}`;
  }
 
  /**
   * Validates a seqid from a client request against the owner's current seqid.
   * Per RFC 7530 §9.1.7, the server expects seqid = last_seqid + 1 for new operations,
   * or seqid = last_seqid for replayed requests (idempotent retry).
   *
   * @param requestSeqid - seqid from the client request
   * @param ownerSeqid - current seqid stored for the owner
   * @returns 'valid' if seqid matches expected next value, 'replay' if it matches last value, 'invalid' otherwise
   */
  protected validateSeqid(requestSeqid: number, ownerSeqid: number): 'valid' | 'replay' | 'invalid' {
    const nextSeqid = ownerSeqid === 0xffffffff ? 1 : ownerSeqid + 1;
    if (requestSeqid === nextSeqid) return 'valid';
    if (requestSeqid === ownerSeqid) return 'replay';
    return 'invalid';
  }
 
  /**
   * Renews the lease for a client.
   * Per RFC 7530 §9.5, any stateful operation renews the client's lease.
   *
   * @param clientid - The client ID whose lease should be renewed
   */
  protected renewClientLease(clientid: bigint): void {
    const client = this.clients.get(clientid);
    Iif (client) {
      client.lastRenew = Date.now();
    }
  }
 
  protected makeStateidKey(stateid: struct.Nfsv4Stateid): string {
    return `${stateid.seqid}:${Buffer.from(stateid.other).toString('hex')}`;
  }
 
  protected createStateid(): struct.Nfsv4Stateid {
    const seqid = this.nextStateidSeqid++;
    const other = randomBytes(12);
    return new struct.Nfsv4Stateid(seqid, other);
  }
 
  protected canAccessFile(path: string, shareAccess: number, shareDeny: number): boolean {
    for (const openFile of this.openFiles.values()) {
      Iif (openFile.path !== path) continue;
      if ((openFile.shareDeny & shareAccess) !== 0) return false;
      Iif ((shareDeny & openFile.shareAccess) !== 0) return false;
    }
    return true;
  }
 
  protected makeLockOwnerKey(clientid: bigint, owner: Uint8Array): string {
    return `${clientid}:${Buffer.from(owner).toString('hex')}`;
  }
 
  protected makeOpenRequestKey(ownerKey: string, currentPath: string, request: msg.Nfsv4OpenRequest): string {
    const writer = new Writer(256);
    const encoder = new XdrEncoder(writer);
    request.encode(encoder);
    const requestBytes = writer.flush();
    const requestHex = Buffer.from(requestBytes).toString('hex');
    return `OPEN:${ownerKey}:${currentPath}:${requestHex}`;
  }
 
  protected makeLockRequestKey(
    lockOwnerKey: string,
    filePath: string,
    locktype: number,
    offset: bigint,
    length: bigint,
    seqid: number,
  ): string {
    return `LOCK:${lockOwnerKey}:${filePath}:${locktype}:${offset.toString()}:${length.toString()}:${seqid}`;
  }
 
  protected makeLockuRequestKey(
    lockOwnerKey: string,
    stateid: struct.Nfsv4Stateid,
    offset: bigint,
    length: bigint,
    seqid: number,
  ): string {
    const stateidKey = this.makeStateidKey(stateid);
    return `LOCKU:${lockOwnerKey}:${stateidKey}:${offset.toString()}:${length.toString()}:${seqid}`;
  }
 
  protected makeLockKey(stateid: struct.Nfsv4Stateid, offset: bigint, length: bigint): string {
    return `${this.makeStateidKey(stateid)}:${offset}:${length}`;
  }
 
  protected makeLockStateidKey(lockOwnerKey: string, path: string): string {
    return `${lockOwnerKey}:${path}`;
  }
 
  protected getOrCreateLockStateid(lockOwnerKey: string, path: string): LockStateid {
    const key = this.makeLockStateidKey(lockOwnerKey, path);
    let lockStateid = this.lockStateids.get(key);
    if (!lockStateid) {
      const other = randomBytes(12);
      lockStateid = new LockStateid(other, 1, lockOwnerKey, path);
      this.lockStateids.set(key, lockStateid);
      const otherKey = Buffer.from(other).toString('hex');
      this.lockStateids.set(otherKey, lockStateid);
    }
    return lockStateid;
  }
 
  protected findLockStateidByOther(other: Uint8Array): LockStateid | undefined {
    const otherKey = Buffer.from(other).toString('hex');
    return this.lockStateids.get(otherKey);
  }
 
  protected hasConflictingLock(
    path: string,
    locktype: number,
    offset: bigint,
    length: bigint,
    ownerKey: string,
  ): boolean {
    const isWriteLock = locktype === Nfsv4LockType.WRITE_LT;
    for (const lock of this.locks.values()) {
      Iif (lock.path !== path) continue;
      if (!lock.overlaps(offset, length)) continue;
      if (lock.lockOwnerKey === ownerKey) continue;
      if (isWriteLock || lock.locktype === Nfsv4LockType.WRITE_LT) return true;
    }
    return false;
  }
 
  /**
   * Establishes client ID or updates callback information.
   * Returns a client ID and confirmation verifier for SETCLIENTID_CONFIRM.
   */
  public async SETCLIENTID(
    request: msg.Nfsv4SetclientidRequest,
    ctx: Nfsv4OperationCtx,
  ): Promise<msg.Nfsv4SetclientidResponse> {
    const principal = ctx.getPrincipal();
    const verifier = request.client.verifier.data;
    const clientIdString = request.client.id;
    const callback = request.callback;
    const callbackIdent = request.callbackIdent;
    const confirmedClientEntry = this.findClientByIdString(this.clients, clientIdString);
    let clientid: bigint = 0n;
    if (confirmedClientEntry) {
      const existingRecord = confirmedClientEntry[1];
      Iif (existingRecord.principal !== principal) return new msg.Nfsv4SetclientidResponse(Nfsv4Stat.NFS4ERR_CLID_INUSE);
      this.pendingClients.delete(clientid);
      clientid = confirmedClientEntry[0];
      const verifierMatch = cmpUint8Array(existingRecord.verifier, verifier);
      if (verifierMatch) {
        // The client is re-registering with the same ID string and verifier.
        // Update callback information, return existing client ID and issue
        // new confirm verifier.
      } else {
        // The client is re-registering with the same ID string but different verifier.
        clientid = this.nextClientId++;
      }
    } else {
      const pendingClientEntry = this.findClientByIdString(this.pendingClients, clientIdString);
      Iif (pendingClientEntry) {
        const existingRecord = pendingClientEntry[1];
        Iif (existingRecord.principal !== principal)
          return new msg.Nfsv4SetclientidResponse(Nfsv4Stat.NFS4ERR_CLID_INUSE);
        const verifierMatch = cmpUint8Array(existingRecord.verifier, verifier);
        Iif (verifierMatch && existingRecord.cache) {
          // The client is re-registering with the same ID string and verifier.
          // Return cached response.
          return existingRecord.cache;
        }
      }
      // New client ID string. Create new client record.
      clientid = this.nextClientId++;
    }
    const setclientidConfirm = randomBytes(8);
    const verifierStruct = new struct.Nfsv4Verifier(setclientidConfirm);
    const body = new msg.Nfsv4SetclientidResOk(clientid, verifierStruct);
    const response = new msg.Nfsv4SetclientidResponse(Nfsv4Stat.NFS4_OK, body);
    const newRecord = new ClientRecord(
      principal,
      verifier,
      clientIdString,
      callback,
      callbackIdent,
      setclientidConfirm,
      response,
    );
 
    // Remove any existing pending records with same ID string.
    for (const [id, entry] of this.pendingClients.entries())
      Iif (cmpUint8Array(entry.clientIdString, clientIdString)) this.pendingClients.delete(id);
    this.enforcePendingClientLimit();
    this.pendingClients.set(clientid, newRecord);
 
    return response;
  }
 
  /**
   * Confirms a client ID established by SETCLIENTID.
   * Transitions unconfirmed client record to confirmed state.
   */
  public async SETCLIENTID_CONFIRM(
    request: msg.Nfsv4SetclientidConfirmRequest,
    ctx: Nfsv4OperationCtx,
  ): Promise<msg.Nfsv4SetclientidConfirmResponse> {
    const {clients, pendingClients} = this;
    const clientid = request.clientid;
    const setclientidConfirm = request.setclientidConfirm.data;
    const pendingRecord = pendingClients.get(clientid);
    Iif (!pendingRecord) {
      const confirmedRecord = this.clients.get(clientid);
      Iif (confirmedRecord && cmpUint8Array(confirmedRecord.setclientidConfirm, setclientidConfirm))
        return new msg.Nfsv4SetclientidConfirmResponse(Nfsv4Stat.NFS4_OK);
      return new msg.Nfsv4SetclientidConfirmResponse(Nfsv4Stat.NFS4ERR_STALE_CLIENTID);
    }
    const principal = ctx.getPrincipal();
    Iif (pendingRecord.principal !== principal)
      return new msg.Nfsv4SetclientidConfirmResponse(Nfsv4Stat.NFS4ERR_CLID_INUSE);
    Iif (!cmpUint8Array(pendingRecord.setclientidConfirm, setclientidConfirm))
      return new msg.Nfsv4SetclientidConfirmResponse(Nfsv4Stat.NFS4ERR_STALE_CLIENTID);
    const oldConfirmed = this.findClientByIdString(this.clients, pendingRecord.clientIdString);
    Iif (oldConfirmed) {
      const clientid2 = oldConfirmed[0];
      this.clients.delete(clientid2);
      pendingClients.delete(clientid2);
    }
    this.clients.delete(clientid);
    pendingClients.delete(clientid);
 
    // Remove any existing pending records with same ID string.
    const clientIdString = pendingRecord.clientIdString;
    for (const [id, entry] of pendingClients.entries())
      Iif (cmpUint8Array(entry.clientIdString, clientIdString)) pendingClients.delete(id);
    for (const [id, entry] of clients.entries())
      Iif (cmpUint8Array(entry.clientIdString, clientIdString)) clients.delete(id);
 
    this.enforceClientLimit();
    clients.set(clientid, pendingRecord);
    return new msg.Nfsv4SetclientidConfirmResponse(Nfsv4Stat.NFS4_OK);
  }
 
  public async ILLEGAL(request: msg.Nfsv4IllegalRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4IllegalResponse> {
    ctx.connection.logger.log('ILLEGAL', request);
    return new msg.Nfsv4IllegalResponse(Nfsv4Stat.NFS4ERR_OP_ILLEGAL);
  }
 
  public async PUTROOTFH(
    request: msg.Nfsv4PutrootfhRequest,
    ctx: Nfsv4OperationCtx,
  ): Promise<msg.Nfsv4PutrootfhResponse> {
    ctx.cfh = ROOT_FH;
    return new msg.Nfsv4PutrootfhResponse(Nfsv4Stat.NFS4_OK);
  }
 
  public async PUTPUBFH(request: msg.Nfsv4PutpubfhRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4PutpubfhResponse> {
    ctx.cfh = ROOT_FH;
    return new msg.Nfsv4PutpubfhResponse(Nfsv4Stat.NFS4_OK);
  }
 
  public async PUTFH(request: msg.Nfsv4PutfhRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4PutfhResponse> {
    const fh = request.object.data;
    Iif (fh.length > Nfsv4Const.FHSIZE) throw Nfsv4Stat.NFS4ERR_BADHANDLE;
    const valid = this.fh.validate(fh);
    Iif (!valid) throw Nfsv4Stat.NFS4ERR_BADHANDLE;
    ctx.cfh = fh;
    return new msg.Nfsv4PutfhResponse(Nfsv4Stat.NFS4_OK);
  }
 
  public async GETFH(request: msg.Nfsv4GetfhRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4GetfhResponse> {
    const cfh = ctx.cfh;
    Iif (!cfh) throw Nfsv4Stat.NFS4ERR_NOFILEHANDLE;
    const fh = new struct.Nfsv4Fh(cfh);
    const body = new msg.Nfsv4GetfhResOk(fh);
    return new msg.Nfsv4GetfhResponse(Nfsv4Stat.NFS4_OK, body);
  }
 
  public async RESTOREFH(
    request: msg.Nfsv4RestorefhRequest,
    ctx: Nfsv4OperationCtx,
  ): Promise<msg.Nfsv4RestorefhResponse> {
    Iif (!ctx.sfh) throw Nfsv4Stat.NFS4ERR_RESTOREFH;
    ctx.cfh = ctx.sfh;
    return new msg.Nfsv4RestorefhResponse(Nfsv4Stat.NFS4_OK);
  }
 
  public async SAVEFH(request: msg.Nfsv4SavefhRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4SavefhResponse> {
    Iif (!ctx.cfh) throw Nfsv4Stat.NFS4ERR_NOFILEHANDLE;
    ctx.sfh = ctx.cfh;
    return new msg.Nfsv4SavefhResponse(Nfsv4Stat.NFS4_OK);
  }
 
  private absolutePath(path: string): string {
    const dir = this.dir;
    if (path === dir) return dir;
    if (path.startsWith(dir + NodePath.sep) || path.startsWith(dir + '/')) return path;
    const absolutePath = NodePath.join(dir, path);
    Iif (absolutePath.length < dir.length) throw Nfsv4Stat.NFS4ERR_NOENT;
    Iif (!absolutePath.startsWith(dir)) throw Nfsv4Stat.NFS4ERR_NOENT;
    return absolutePath;
  }
 
  public async LOOKUP(request: msg.Nfsv4LookupRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4LookupResponse> {
    const fh = this.fh;
    const currentPath = fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    const component = request.objname;
    Iif (component.length === 0) throw Nfsv4Stat.NFS4ERR_INVAL;
    const promises = this.promises;
    let stats: Stats;
    try {
      stats = await promises.stat(currentPathAbsolute);
    } catch (err: unknown) {
      Iif (isErrCode('ENOENT', err)) throw Nfsv4Stat.NFS4ERR_NOENT;
      throw Nfsv4Stat.NFS4ERR_IO;
    }
    Iif (stats.isSymbolicLink()) throw Nfsv4Stat.NFS4ERR_SYMLINK;
    Iif (!stats.isDirectory()) throw Nfsv4Stat.NFS4ERR_NOTDIR;
    const targetAbsolutePath = NodePath.join(currentPathAbsolute, component);
    try {
      const targetStats = await promises.stat(targetAbsolutePath);
      Iif (!targetStats) throw Nfsv4Stat.NFS4ERR_NOENT;
    } catch (err: any) {
      if (isErrCode('ENOENT', err)) throw Nfsv4Stat.NFS4ERR_NOENT;
      Iif (isErrCode('EACCES', err)) throw Nfsv4Stat.NFS4ERR_ACCESS;
      throw Nfsv4Stat.NFS4ERR_IO;
    }
    fh.setCfh(ctx, targetAbsolutePath);
    return new msg.Nfsv4LookupResponse(Nfsv4Stat.NFS4_OK);
  }
 
  public async LOOKUPP(request: msg.Nfsv4LookuppRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4LookuppResponse> {
    const fh = this.fh;
    const currentPath = fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    const promises = this.promises;
    let stats: Stats;
    try {
      stats = await promises.stat(currentPathAbsolute);
    } catch (err: any) {
      Iif (isErrCode('ENOENT', err)) throw Nfsv4Stat.NFS4ERR_NOENT;
      throw Nfsv4Stat.NFS4ERR_IO;
    }
    Iif (!stats.isDirectory()) throw Nfsv4Stat.NFS4ERR_NOTDIR;
    const parentAbsolutePath = NodePath.dirname(currentPathAbsolute);
    Iif (parentAbsolutePath.length < this.dir.length) throw Nfsv4Stat.NFS4ERR_NOENT;
    fh.setCfh(ctx, parentAbsolutePath);
    return new msg.Nfsv4LookuppResponse(Nfsv4Stat.NFS4_OK);
  }
 
  public async GETATTR(request: msg.Nfsv4GetattrRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4GetattrResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    const requestedAttrNums = parseBitmask(request.attrRequest.mask);
    let stats: Stats | undefined;
    if (requiresLstat(requestedAttrNums)) {
      try {
        Iif (ctx.connection.debug) ctx.connection.logger.log('lstat', currentPathAbsolute);
        stats = await this.promises.lstat(currentPathAbsolute);
      } catch (error: unknown) {
        throw normalizeNodeFsError(error, ctx.connection.logger);
      }
    }
    let fsStats: FilesystemStats | undefined;
    Iif (requiresFsStats(requestedAttrNums)) {
      try {
        fsStats = await this.fsStats();
      } catch (error: unknown) {
        ctx.connection.logger.error(error);
      }
    }
    const attrs = encodeAttrs(request.attrRequest, stats, currentPath, ctx.cfh!, this.leaseTime, fsStats);
    return new msg.Nfsv4GetattrResponse(Nfsv4Stat.NFS4_OK, new msg.Nfsv4GetattrResOk(attrs));
  }
 
  public async ACCESS(request: msg.Nfsv4AccessRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4AccessResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    const promises = this.promises;
    let stats: Stats;
    try {
      stats = await promises.lstat(currentPathAbsolute);
    } catch (error: unknown) {
      throw normalizeNodeFsError(error, ctx.connection.logger);
    }
    const requestedAccess = request.access;
    const isDirectory = stats.isDirectory();
    const mode = stats.mode;
    let supported = 0;
    let access = 0;
    if (requestedAccess & Nfsv4Access.ACCESS4_READ) {
      supported |= Nfsv4Access.ACCESS4_READ;
      if (mode & 0o444) access |= Nfsv4Access.ACCESS4_READ;
    }
    if (requestedAccess & Nfsv4Access.ACCESS4_LOOKUP) {
      supported |= Nfsv4Access.ACCESS4_LOOKUP;
      if (isDirectory && mode & 0o111) access |= Nfsv4Access.ACCESS4_LOOKUP;
    }
    if (requestedAccess & Nfsv4Access.ACCESS4_MODIFY) {
      supported |= Nfsv4Access.ACCESS4_MODIFY;
      if (mode & 0o222) access |= Nfsv4Access.ACCESS4_MODIFY;
    }
    if (requestedAccess & Nfsv4Access.ACCESS4_EXTEND) {
      supported |= Nfsv4Access.ACCESS4_EXTEND;
      if (mode & 0o222) access |= Nfsv4Access.ACCESS4_EXTEND;
    }
    if (requestedAccess & Nfsv4Access.ACCESS4_DELETE) {
      Iif (!isDirectory) {
        supported |= 0;
      } else {
        supported |= Nfsv4Access.ACCESS4_DELETE;
        if (mode & 0o222) access |= Nfsv4Access.ACCESS4_DELETE;
      }
    }
    if (requestedAccess & Nfsv4Access.ACCESS4_EXECUTE) {
      supported |= Nfsv4Access.ACCESS4_EXECUTE;
      Iif (!isDirectory && mode & 0o111) access |= Nfsv4Access.ACCESS4_EXECUTE;
    }
    const body = new msg.Nfsv4AccessResOk(supported, access);
    return new msg.Nfsv4AccessResponse(Nfsv4Stat.NFS4_OK, body);
  }
 
  public async READDIR(request: msg.Nfsv4ReaddirRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4ReaddirResponse> {
    const fh = this.fh;
    const currentPath = fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    const promises = this.promises;
    let stats: Stats;
    try {
      stats = await promises.lstat(currentPathAbsolute);
    } catch (error: unknown) {
      throw normalizeNodeFsError(error, ctx.connection.logger);
    }
    if (!stats.isDirectory()) throw Nfsv4Stat.NFS4ERR_NOTDIR;
    const cookie = request.cookie;
    const requestedCookieverf = request.cookieverf.data;
    const maxcount = request.maxcount;
    const attrRequest = request.attrRequest;
    let cookieverf: Uint8Array;
    if (cookie === 0n) {
      cookieverf = new Uint8Array(8);
      const changeTime = BigInt(Math.floor(stats.mtimeMs * 1000000));
      const view = new DataView(cookieverf.buffer);
      view.setBigUint64(0, changeTime, false);
    } else {
      cookieverf = new Uint8Array(8);
      const changeTime = BigInt(Math.floor(stats.mtimeMs * 1000000));
      const view = new DataView(cookieverf.buffer);
      view.setBigUint64(0, changeTime, false);
      if (!cmpUint8Array(requestedCookieverf, cookieverf)) throw Nfsv4Stat.NFS4ERR_NOT_SAME;
    }
    let dirents: Dirent[];
    try {
      dirents = await promises.readdir(currentPathAbsolute, {withFileTypes: true});
    } catch (error: unknown) {
      throw normalizeNodeFsError(error, ctx.connection.logger);
    }
    const entries: struct.Nfsv4Entry[] = [];
    let totalBytes = 0;
    const overheadPerEntry = 32;
    let startIndex = 0;
    Iif (cookie > 0n) {
      startIndex = Number(cookie) - 2;
      Iif (startIndex < 0) startIndex = 0;
      Iif (startIndex > dirents.length) startIndex = dirents.length;
    }
    let eof = true;
    const fsStats = await this.fsStats();
    for (let i = startIndex; i < dirents.length; i++) {
      const dirent = dirents[i];
      const name = dirent.name;
      const entryCookie = BigInt(i + 3);
      const entryPath = NodePath.join(currentPathAbsolute, name);
      let entryStats: Stats | undefined;
      try {
        entryStats = await promises.lstat(entryPath);
      } catch (_error: unknown) {
        continue;
      }
      const entryFh = fh.encode(entryPath);
      const attrs = encodeAttrs(attrRequest, entryStats, entryPath, entryFh, this.leaseTime, fsStats);
      const nameBytes = Buffer.byteLength(name, 'utf8');
      const attrBytes = attrs.attrVals.length;
      const entryBytes = overheadPerEntry + nameBytes + attrBytes;
      if (totalBytes + entryBytes > maxcount && entries.length > 0) {
        eof = false;
        break;
      }
      const entry = new struct.Nfsv4Entry(entryCookie, name, attrs);
      entries.push(entry);
      totalBytes += entryBytes;
    }
    const cookieverf2 = new struct.Nfsv4Verifier(cookieverf);
    const body = new msg.Nfsv4ReaddirResOk(cookieverf2, entries, eof);
    return new msg.Nfsv4ReaddirResponse(Nfsv4Stat.NFS4_OK, body);
  }
 
  public async OPEN(request: msg.Nfsv4OpenRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4OpenResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    const ownerKey = this.makeOpenOwnerKey(request.owner.clientid, request.owner.owner);
    this.renewClientLease(request.owner.clientid);
    let ownerState = this.openOwners.get(ownerKey);
    let replayCandidate = false;
    let previousSeqid = ownerState?.seqid ?? 0;
    if (!ownerState) {
      ownerState = new OpenOwnerState(request.owner.clientid, request.owner.owner, 0);
      this.openOwners.set(ownerKey, ownerState);
      previousSeqid = 0;
    } else {
      const seqidValidation = this.validateSeqid(request.seqid, ownerState.seqid);
      if (seqidValidation === 'invalid') {
        Iif (request.seqid === 0) {
          ownerState.seqid = 0;
          previousSeqid = 0;
        } else {
          return new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4ERR_BAD_SEQID);
        }
      } else if (seqidValidation === 'replay') {
        replayCandidate = true;
      }
    }
    if (request.claim.claimType !== Nfsv4OpenClaimType.CLAIM_NULL) {
      return new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4ERR_NOTSUPP);
    }
    const claimNull = request.claim.claim as struct.Nfsv4OpenClaimNull;
    const filename = claimNull.file;
    const filePath = NodePath.join(currentPathAbsolute, filename);
    const requestKey = this.makeOpenRequestKey(ownerKey, filePath, request);
    if (replayCandidate) {
      if (ownerState.lastRequestKey === requestKey && ownerState.lastResponse) {
        return ownerState.lastResponse;
      }
      return new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4ERR_BAD_SEQID);
    }
    ownerState.seqid = request.seqid;
    const opentype = request.openhow.opentype;
    const isCreate = opentype === Nfsv4OpenFlags.OPEN4_CREATE;
    let fileExists = false;
    try {
      const stats = await this.promises.lstat(filePath);
      if (!stats.isFile()) {
        const response = new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4ERR_ISDIR);
        ownerState.lastResponse = response;
        ownerState.lastRequestKey = requestKey;
        return response;
      }
      fileExists = true;
    } catch (err) {
      if (isErrCode('ENOENT', err)) {
        if (!isCreate) {
          const response = new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4ERR_NOENT);
          ownerState.lastResponse = response;
          ownerState.lastRequestKey = requestKey;
          return response;
        }
      } else E{
        const status = normalizeNodeFsError(err, ctx.connection.logger);
        const response = new msg.Nfsv4OpenResponse(status);
        ownerState.lastResponse = response;
        ownerState.lastRequestKey = requestKey;
        return response;
      }
    }
    if (fileExists && !this.canAccessFile(filePath, request.shareAccess, request.shareDeny)) {
      ownerState.seqid = previousSeqid;
      const response = new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4ERR_SHARE_DENIED);
      ownerState.lastResponse = response;
      ownerState.lastRequestKey = requestKey;
      return response;
    }
    let flags = 0;
    const isWrite = (request.shareAccess & Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_WRITE) !== 0;
    const isRead = (request.shareAccess & Nfsv4OpenAccess.OPEN4_SHARE_ACCESS_READ) !== 0;
    if (isCreate) {
      flags = this.fs.constants.O_CREAT;
      const createHow = request.openhow.how;
      Iif (createHow && createHow.mode === Nfsv4CreateMode.EXCLUSIVE4) {
        flags |= this.fs.constants.O_EXCL;
      }
    }
    if (isRead && isWrite) {
      flags |= this.fs.constants.O_RDWR;
    } else if (isWrite) {
      flags |= this.fs.constants.O_WRONLY;
    } else {
      flags |= this.fs.constants.O_RDONLY;
    }
    try {
      const fd = await this.promises.open(filePath, flags, 0o644);
      const stateid = this.createStateid();
      const stateidKey = this.makeStateidKey(stateid);
      const openFile = new OpenFileState(
        stateid,
        filePath,
        fd,
        request.shareAccess,
        request.shareDeny,
        ownerKey,
        ownerState.seqid,
        false,
      );
      this.openFiles.set(stateidKey, openFile);
      ownerState.opens.add(stateidKey);
      const fh = this.fh.encode(filePath);
      ctx.cfh = fh;
      const before = this.changeCounter;
      const after = ++this.changeCounter;
      const cinfo = new struct.Nfsv4ChangeInfo(true, before, after);
      const attrset = new struct.Nfsv4Bitmap([]);
      const delegation = new struct.Nfsv4OpenDelegation(Nfsv4DelegType.OPEN_DELEGATE_NONE);
      const resok = new msg.Nfsv4OpenResOk(stateid, cinfo, 0, attrset, delegation);
      const response = new msg.Nfsv4OpenResponse(Nfsv4Stat.NFS4_OK, resok);
      ownerState.lastResponse = response;
      ownerState.lastRequestKey = requestKey;
      return response;
    } catch (err) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      const response = new msg.Nfsv4OpenResponse(status);
      ownerState.lastResponse = response;
      ownerState.lastRequestKey = requestKey;
      return response;
    }
  }
 
  public async OPENATTR(request: msg.Nfsv4OpenattrRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4OpenattrResponse> {
    return new msg.Nfsv4OpenattrResponse(Nfsv4Stat.NFS4ERR_NOTSUPP);
  }
 
  public async OPEN_CONFIRM(
    request: msg.Nfsv4OpenConfirmRequest,
    ctx: Nfsv4OperationCtx,
  ): Promise<msg.Nfsv4OpenConfirmResponse> {
    const stateidKey = this.makeStateidKey(request.openStateid);
    const openFile = this.openFiles.get(stateidKey);
    if (!openFile) throw Nfsv4Stat.NFS4ERR_BAD_STATEID;
    const ownerState = this.openOwners.get(openFile.openOwnerKey);
    Iif (!ownerState) throw Nfsv4Stat.NFS4ERR_BAD_STATEID;
    const seqidValidation = this.validateSeqid(request.seqid, ownerState.seqid);
    if (seqidValidation === 'invalid') throw Nfsv4Stat.NFS4ERR_BAD_SEQID;
    if (seqidValidation === 'replay') {
      const newStateid = new struct.Nfsv4Stateid(openFile.stateid.seqid, openFile.stateid.other);
      const resok = new msg.Nfsv4OpenConfirmResOk(newStateid);
      return new msg.Nfsv4OpenConfirmResponse(Nfsv4Stat.NFS4_OK, resok);
    }
    ownerState.seqid = request.seqid;
    openFile.confirmed = true;
    const newSeqid = this.nextStateidSeqid++;
    const newStateid = new struct.Nfsv4Stateid(newSeqid, openFile.stateid.other);
    const oldKey = stateidKey;
    const newKey = this.makeStateidKey(newStateid);
    const updatedOpenFile = new OpenFileState(
      newStateid,
      openFile.path,
      openFile.fd,
      openFile.shareAccess,
      openFile.shareDeny,
      openFile.openOwnerKey,
      ownerState.seqid,
      true,
    );
    this.openFiles.delete(oldKey);
    this.openFiles.set(newKey, updatedOpenFile);
    ownerState.opens.delete(oldKey);
    ownerState.opens.add(newKey);
    const resok = new msg.Nfsv4OpenConfirmResOk(newStateid);
    return new msg.Nfsv4OpenConfirmResponse(Nfsv4Stat.NFS4_OK, resok);
  }
 
  public async OPEN_DOWNGRADE(
    request: msg.Nfsv4OpenDowngradeRequest,
    ctx: Nfsv4OperationCtx,
  ): Promise<msg.Nfsv4OpenDowngradeResponse> {
    const stateidKey = this.makeStateidKey(request.openStateid);
    const openFile = this.openFiles.get(stateidKey);
    if (!openFile) throw Nfsv4Stat.NFS4ERR_BAD_STATEID;
    const ownerState = this.openOwners.get(openFile.openOwnerKey);
    Iif (!ownerState) throw Nfsv4Stat.NFS4ERR_BAD_STATEID;
    const seqidValidation = this.validateSeqid(request.seqid, ownerState.seqid);
    if (seqidValidation === 'invalid') throw Nfsv4Stat.NFS4ERR_BAD_SEQID;
    if (seqidValidation === 'replay') {
      const newStateid = new struct.Nfsv4Stateid(openFile.stateid.seqid, openFile.stateid.other);
      const resok = new msg.Nfsv4OpenDowngradeResOk(newStateid);
      return new msg.Nfsv4OpenDowngradeResponse(Nfsv4Stat.NFS4_OK, resok);
    }
    ownerState.seqid = request.seqid;
    if ((request.shareAccess & ~openFile.shareAccess) !== 0) throw Nfsv4Stat.NFS4ERR_INVAL;
    Iif ((request.shareDeny & ~openFile.shareDeny) !== 0) throw Nfsv4Stat.NFS4ERR_INVAL;
    const newSeqid = this.nextStateidSeqid++;
    const newStateid = new struct.Nfsv4Stateid(newSeqid, openFile.stateid.other);
    const oldKey = stateidKey;
    const newKey = this.makeStateidKey(newStateid);
    const updatedOpenFile = new OpenFileState(
      newStateid,
      openFile.path,
      openFile.fd,
      request.shareAccess,
      request.shareDeny,
      openFile.openOwnerKey,
      ownerState.seqid,
      openFile.confirmed,
    );
    this.openFiles.delete(oldKey);
    this.openFiles.set(newKey, updatedOpenFile);
    ownerState.opens.delete(oldKey);
    ownerState.opens.add(newKey);
    const resok = new msg.Nfsv4OpenDowngradeResOk(newStateid);
    return new msg.Nfsv4OpenDowngradeResponse(Nfsv4Stat.NFS4_OK, resok);
  }
 
  public async CLOSE(request: msg.Nfsv4CloseRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4CloseResponse> {
    const stateidKey = this.makeStateidKey(request.openStateid);
    const openFile = this.openFiles.get(stateidKey);
    if (!openFile) {
      return new msg.Nfsv4CloseResponse(Nfsv4Stat.NFS4_OK, new msg.Nfsv4CloseResOk(request.openStateid));
    }
    const ownerState = this.openOwners.get(openFile.openOwnerKey);
    Iif (!ownerState) {
      return new msg.Nfsv4CloseResponse(Nfsv4Stat.NFS4ERR_BAD_STATEID);
    }
    this.renewClientLease(ownerState.clientid);
    const seqidValidation = this.validateSeqid(request.seqid, ownerState.seqid);
    if (seqidValidation === 'invalid') {
      return new msg.Nfsv4CloseResponse(Nfsv4Stat.NFS4ERR_BAD_SEQID);
    }
    Iif (seqidValidation === 'replay') {
      const newStateid = new struct.Nfsv4Stateid(openFile.stateid.seqid, openFile.stateid.other);
      const resok = new msg.Nfsv4CloseResOk(newStateid);
      return new msg.Nfsv4CloseResponse(Nfsv4Stat.NFS4_OK, resok);
    }
    ownerState.seqid = request.seqid;
    try {
      const handle = openFile.fd as any;
      if (handle && typeof handle.close === 'function') {
        await handle.close();
      }
    } catch (err) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      Iif (status !== Nfsv4Stat.NFS4ERR_NOENT) {
        return new msg.Nfsv4CloseResponse(status);
      }
    }
    ownerState.opens.delete(stateidKey);
    if (ownerState.opens.size === 0) {
      this.openOwners.delete(openFile.openOwnerKey);
    }
    this.openFiles.delete(stateidKey);
    const newSeqid = this.nextStateidSeqid++;
    const newStateid = new struct.Nfsv4Stateid(newSeqid, openFile.stateid.other);
    const resok = new msg.Nfsv4CloseResOk(newStateid);
    return new msg.Nfsv4CloseResponse(Nfsv4Stat.NFS4_OK, resok);
  }
 
  public async SECINFO(request: msg.Nfsv4SecinfoRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4SecinfoResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    const filePath = NodePath.join(currentPathAbsolute, request.name);
    try {
      await this.promises.lstat(filePath);
    } catch (err) {
      Iif (isErrCode(err, 'ENOENT')) {
        return new msg.Nfsv4SecinfoResponse(Nfsv4Stat.NFS4ERR_NOENT);
      }
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4SecinfoResponse(status);
    }
    const flavors: struct.Nfsv4SecInfoFlavor[] = [new struct.Nfsv4SecInfoFlavor(1)];
    const resok = new msg.Nfsv4SecinfoResOk(flavors);
    return new msg.Nfsv4SecinfoResponse(Nfsv4Stat.NFS4_OK, resok);
  }
 
  public async LOCK(request: msg.Nfsv4LockRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4LockResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const {locktype, offset, length, locker} = request;
    if (!locker.newLockOwner) {
      const existingOwner = locker.owner as struct.Nfsv4LockExistingOwner;
      const stateidKey = this.makeStateidKey(existingOwner.lockStateid);
      let existingLockOwnerKey: string | undefined;
      for (const lock of this.locks.values()) {
        if (this.makeStateidKey(lock.stateid) === stateidKey) {
          existingLockOwnerKey = lock.lockOwnerKey;
          break;
        }
      }
      Iif (!existingLockOwnerKey) {
        return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_STATEID);
      }
      const lockOwnerState = this.lockOwners.get(existingLockOwnerKey);
      Iif (!lockOwnerState) {
        return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_STATEID);
      }
      this.renewClientLease(lockOwnerState.clientid);
      const seqidValidation = this.validateSeqid(existingOwner.lockSeqid, lockOwnerState.seqid);
      const requestKey = this.makeLockRequestKey(
        existingLockOwnerKey,
        currentPath,
        locktype,
        offset,
        length,
        existingOwner.lockSeqid,
      );
      Iif (seqidValidation === 'invalid') {
        return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_SEQID);
      }
      if (seqidValidation === 'replay') {
        if (lockOwnerState.lastRequestKey !== requestKey) {
          return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_SEQID);
        }
        if (lockOwnerState.lastResponse) return lockOwnerState.lastResponse;
        return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_SEQID);
      }
      lockOwnerState.seqid = existingOwner.lockSeqid;
      if (this.hasConflictingLock(currentPath, locktype, offset, length, existingLockOwnerKey)) {
        const conflictOwner = new struct.Nfsv4LockOwner(BigInt(0), new Uint8Array());
        const denied = new msg.Nfsv4LockResDenied(offset, length, locktype, conflictOwner);
        return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_DENIED, undefined, denied);
      }
      const lockStateid = this.getOrCreateLockStateid(existingLockOwnerKey, currentPath);
      const stateid = lockStateid.incrementAndGetStateid();
      const lock = new ByteRangeLock(stateid, currentPath, locktype, offset, length, existingLockOwnerKey);
      const lockKey = this.makeLockKey(stateid, offset, length);
      this.locks.set(lockKey, lock);
      lockOwnerState.locks.add(lockKey);
      const resok = new msg.Nfsv4LockResOk(stateid);
      const response = new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4_OK, resok);
      lockOwnerState.lastResponse = response;
      lockOwnerState.lastRequestKey = requestKey;
      return response;
    }
    const newOwner = locker.owner as struct.Nfsv4LockNewOwner;
    const openToLock = newOwner.openToLockOwner;
    const lockOwnerData = openToLock.lockOwner;
    const openStateidKey = this.makeStateidKey(openToLock.openStateid);
    const openFile = this.openFiles.get(openStateidKey);
    Iif (!openFile) {
      return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_STATEID);
    }
    const openOwnerKey = openFile.openOwnerKey;
    const openOwnerState = this.openOwners.get(openOwnerKey);
    Iif (!openOwnerState) {
      return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_STATEID);
    }
    this.renewClientLease(lockOwnerData.clientid);
    const seqidValidation = this.validateSeqid(openToLock.openSeqid, openOwnerState.seqid);
    if (seqidValidation === 'invalid') {
      return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_SEQID);
    }
    if (seqidValidation === 'replay') {
      for (const [_key, lock] of this.locks.entries()) {
        if (
          lock.lockOwnerKey === this.makeLockOwnerKey(lockOwnerData.clientid, lockOwnerData.owner) &&
          lock.path === currentPath &&
          lock.offset === offset &&
          lock.length === length
        ) {
          const resok = new msg.Nfsv4LockResOk(lock.stateid);
          return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4_OK, resok);
        }
      }
    }
    openOwnerState.seqid = openToLock.openSeqid;
    const lockOwnerKey = this.makeLockOwnerKey(lockOwnerData.clientid, lockOwnerData.owner);
    const lockRequestKey = this.makeLockRequestKey(
      lockOwnerKey,
      currentPath,
      locktype,
      offset,
      length,
      openToLock.lockSeqid,
    );
    if (this.hasConflictingLock(currentPath, locktype, offset, length, lockOwnerKey)) {
      const conflictOwner = new struct.Nfsv4LockOwner(BigInt(0), new Uint8Array());
      const denied = new msg.Nfsv4LockResDenied(offset, length, locktype, conflictOwner);
      return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_DENIED, undefined, denied);
    }
    let lockOwnerState = this.lockOwners.get(lockOwnerKey);
    if (!lockOwnerState) {
      Iif (openToLock.lockSeqid !== 0) {
        return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_SEQID);
      }
      lockOwnerState = new LockOwnerState(lockOwnerData.clientid, lockOwnerData.owner, 0);
      this.lockOwners.set(lockOwnerKey, lockOwnerState);
    } else E{
      const lockSeqidValidation = this.validateSeqid(openToLock.lockSeqid, lockOwnerState.seqid);
      Iif (lockSeqidValidation === 'invalid') {
        return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_SEQID);
      }
      Iif (lockSeqidValidation === 'replay') {
        Iif (lockOwnerState.lastRequestKey === lockRequestKey && lockOwnerState.lastResponse) {
          return lockOwnerState.lastResponse;
        }
        return new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4ERR_BAD_SEQID);
      }
    }
    lockOwnerState.seqid = openToLock.lockSeqid;
    const lockStateid = this.getOrCreateLockStateid(lockOwnerKey, currentPath);
    const stateid = lockStateid.incrementAndGetStateid();
    const lock = new ByteRangeLock(stateid, currentPath, locktype, offset, length, lockOwnerKey);
    const lockKey = this.makeLockKey(stateid, offset, length);
    this.locks.set(lockKey, lock);
    lockOwnerState.locks.add(lockKey);
    const resok = new msg.Nfsv4LockResOk(stateid);
    const response = new msg.Nfsv4LockResponse(Nfsv4Stat.NFS4_OK, resok);
    lockOwnerState.lastResponse = response;
    lockOwnerState.lastRequestKey = lockRequestKey;
    return response;
  }
 
  public async LOCKT(request: msg.Nfsv4LocktRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4LocktResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const {locktype, offset, length, owner} = request;
    const ownerKey = this.makeLockOwnerKey(owner.clientid, owner.owner);
    if (this.hasConflictingLock(currentPath, locktype, offset, length, ownerKey)) {
      const conflictOwner = new struct.Nfsv4LockOwner(BigInt(0), new Uint8Array());
      const denied = new msg.Nfsv4LocktResDenied(offset, length, locktype, conflictOwner);
      return new msg.Nfsv4LocktResponse(Nfsv4Stat.NFS4ERR_DENIED, denied);
    }
    return new msg.Nfsv4LocktResponse(Nfsv4Stat.NFS4_OK);
  }
 
  public async LOCKU(request: msg.Nfsv4LockuRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4LockuResponse> {
    const {lockStateid, offset, length, seqid} = request;
    const lockStateidState = this.findLockStateidByOther(lockStateid.other);
    if (!lockStateidState) throw Nfsv4Stat.NFS4ERR_BAD_STATEID;
    const ownerKey = lockStateidState.lockOwnerKey;
    const lockOwnerState = this.lockOwners.get(ownerKey);
    Iif (!lockOwnerState) throw Nfsv4Stat.NFS4ERR_BAD_STATEID;
    this.renewClientLease(lockOwnerState.clientid);
    const currentPath = this.fh.currentPath(ctx);
    Iif (lockStateidState.path !== currentPath) throw Nfsv4Stat.NFS4ERR_BAD_STATEID;
    const requestKey = this.makeLockuRequestKey(ownerKey, lockStateid, offset, length, seqid);
    const seqidValidation = this.validateSeqid(seqid, lockOwnerState.seqid);
    Iif (seqidValidation === 'invalid') {
      throw Nfsv4Stat.NFS4ERR_BAD_SEQID;
    }
    if (seqidValidation === 'replay') {
      if (lockOwnerState.lastRequestKey === requestKey && lockOwnerState.lastResponse) {
        return lockOwnerState.lastResponse;
      }
      throw Nfsv4Stat.NFS4ERR_BAD_SEQID;
    }
    lockOwnerState.seqid = seqid;
    const lockKey = this.makeLockKey(lockStateid, offset, length);
    const lock = this.locks.get(lockKey);
    if (lock) {
      this.locks.delete(lockKey);
      lockOwnerState.locks.delete(lockKey);
    }
    const stateid = lockStateidState.incrementAndGetStateid();
    const resok = new msg.Nfsv4LockuResOk(stateid);
    const response = new msg.Nfsv4LockuResponse(Nfsv4Stat.NFS4_OK, resok);
    lockOwnerState.lastResponse = response;
    lockOwnerState.lastRequestKey = requestKey;
    return response;
  }
 
  public async RELEASE_LOCKOWNER(
    request: msg.Nfsv4ReleaseLockOwnerRequest,
    ctx: Nfsv4OperationCtx,
  ): Promise<msg.Nfsv4ReleaseLockOwnerResponse> {
    const {lockOwner} = request;
    const ownerKey = this.makeLockOwnerKey(lockOwner.clientid, lockOwner.owner);
    const lockOwnerState = this.lockOwners.get(ownerKey);
    Iif (!lockOwnerState) throw Nfsv4Stat.NFS4ERR_BAD_STATEID;
    for (const lockKey of lockOwnerState.locks) this.locks.delete(lockKey);
    this.lockOwners.delete(ownerKey);
    return new msg.Nfsv4ReleaseLockOwnerResponse(Nfsv4Stat.NFS4_OK);
  }
 
  public async RENEW(request: msg.Nfsv4RenewRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4RenewResponse> {
    const clientid = request.clientid;
    const client = this.clients.get(clientid);
    Iif (!client) throw Nfsv4Stat.NFS4ERR_STALE_CLIENTID;
    return new msg.Nfsv4RenewResponse(Nfsv4Stat.NFS4_OK);
  }
 
  public async READ(request: msg.Nfsv4ReadRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4ReadResponse> {
    const stateidKey = this.makeStateidKey(request.stateid);
    const openFile = this.openFiles.get(stateidKey);
    Iif (!openFile) return new msg.Nfsv4ReadResponse(Nfsv4Stat.NFS4ERR_BAD_STATEID);
    const fdHandle = openFile.fd as any;
    // If we have an fd-like handle, use its .read; otherwise open the path
    let fd: any;
    let openedHere = false;
    try {
      if (fdHandle && typeof fdHandle.read === 'function') {
        fd = fdHandle;
      } else E{
        fd = await this.promises.open(openFile.path, this.fs.constants.O_RDONLY);
        openedHere = true;
      }
      const buf = Buffer.alloc(request.count);
      const {bytesRead} = await fd.read(buf, 0, request.count, Number(request.offset));
      const eof = bytesRead < request.count;
      const data = buf.slice(0, bytesRead);
      const resok = new msg.Nfsv4ReadResOk(eof, data);
      return new msg.Nfsv4ReadResponse(Nfsv4Stat.NFS4_OK, resok);
    } catch (err: unknown) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4ReadResponse(status);
    } finally {
      try {
        Iif (openedHere && fd && typeof fd.close === 'function') await fd.close();
      } catch (_e) {
        /* ignore close errors */
      }
    }
  }
 
  public async READLINK(request: msg.Nfsv4ReadlinkRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4ReadlinkResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    try {
      const target = await this.promises.readlink(currentPathAbsolute);
      const resok = new msg.Nfsv4ReadlinkResOk(target);
      return new msg.Nfsv4ReadlinkResponse(Nfsv4Stat.NFS4_OK, resok);
    } catch (err: unknown) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4ReadlinkResponse(status);
    }
  }
 
  public async REMOVE(request: msg.Nfsv4RemoveRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4RemoveResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const targetPath = this.absolutePath(NodePath.join(currentPath, request.target));
    try {
      const stats = await this.promises.lstat(targetPath);
      if (stats.isDirectory()) {
        await this.promises.rmdir(targetPath);
      } else {
        await this.promises.unlink(targetPath);
      }
      this.fh.remove(targetPath);
      const before = this.changeCounter;
      const after = ++this.changeCounter;
      const cinfo = new struct.Nfsv4ChangeInfo(true, before, after);
      const resok = new msg.Nfsv4RemoveResOk(cinfo);
      return new msg.Nfsv4RemoveResponse(Nfsv4Stat.NFS4_OK, resok);
    } catch (err: unknown) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4RemoveResponse(status);
    }
  }
 
  public async RENAME(request: msg.Nfsv4RenameRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4RenameResponse> {
    const savedPath = this.fh.savedPath(ctx);
    const currentPath = this.fh.currentPath(ctx);
    const savedPathAbsolute = this.absolutePath(savedPath);
    const currentPathAbsolute = this.absolutePath(currentPath);
    const oldFull = NodePath.join(savedPathAbsolute, request.oldname);
    const newFull = NodePath.join(currentPathAbsolute, request.newname);
    Iif (oldFull.length < this.dir.length || newFull.length < this.dir.length) throw Nfsv4Stat.NFS4ERR_NOENT;
    Iif (!oldFull.startsWith(this.dir)) return new msg.Nfsv4RenameResponse(Nfsv4Stat.NFS4ERR_NOENT);
    if (!newFull.startsWith(this.dir)) return new msg.Nfsv4RenameResponse(Nfsv4Stat.NFS4ERR_NOENT);
    let oldPath: string;
    let newPath: string;
    try {
      oldPath = this.absolutePath(oldFull);
      newPath = this.absolutePath(newFull);
    } catch (e: any) {
      const status = typeof e === 'number' ? e : Nfsv4Stat.NFS4ERR_NOENT;
      return new msg.Nfsv4RenameResponse(status);
    }
    try {
      await this.promises.rename(oldPath, newPath);
      this.fh.rename(oldPath, newPath);
      const before = this.changeCounter;
      const after = ++this.changeCounter;
      const sourceCinfo = new struct.Nfsv4ChangeInfo(true, before, after);
      const targetCinfo = new struct.Nfsv4ChangeInfo(true, before, after);
      const resok = new msg.Nfsv4RenameResOk(sourceCinfo, targetCinfo);
      return new msg.Nfsv4RenameResponse(Nfsv4Stat.NFS4_OK, resok);
    } catch (err: unknown) {
      Iif (isErrCode('EXDEV', err)) return new msg.Nfsv4RenameResponse(Nfsv4Stat.NFS4ERR_XDEV);
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4RenameResponse(status);
    }
  }
 
  public async WRITE(request: msg.Nfsv4WriteRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4WriteResponse> {
    const stateidKey = this.makeStateidKey(request.stateid);
    const openFile = this.openFiles.get(stateidKey);
    if (!openFile) return new msg.Nfsv4WriteResponse(Nfsv4Stat.NFS4ERR_BAD_STATEID);
    const fdHandle = openFile.fd as any;
    let fd: any;
    let openedHere = false;
    try {
      if (fdHandle && typeof fdHandle.write === 'function') {
        fd = fdHandle;
      } else E{
        fd = await this.promises.open(openFile.path, this.fs.constants.O_RDWR);
        openedHere = true;
      }
      const buffer = Buffer.from(request.data);
      const {bytesWritten} = await fd.write(buffer, 0, buffer.length, Number(request.offset));
      // Handle stable flag
      const committed =
        request.stable === Nfsv4StableHow.UNSTABLE4 ? Nfsv4StableHow.UNSTABLE4 : Nfsv4StableHow.FILE_SYNC4;
      if (request.stable === Nfsv4StableHow.FILE_SYNC4 || request.stable === Nfsv4StableHow.DATA_SYNC4) {
        // fd.datasync or fd.sync
        if (typeof fd.datasync === 'function') await fd.datasync();
        else IEif (typeof fd.sync === 'function') await fd.sync();
      }
      const verifier = new struct.Nfsv4Verifier(randomBytes(8));
      const resok = new msg.Nfsv4WriteResOk(bytesWritten, committed, verifier);
      return new msg.Nfsv4WriteResponse(Nfsv4Stat.NFS4_OK, resok);
    } catch (err: unknown) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4WriteResponse(status);
    } finally {
      try {
        Iif (openedHere && fd && typeof fd.close === 'function') await fd.close();
      } catch (_e) {
        /* ignore close errors */
      }
    }
  }
 
  public async DELEGPURGE(
    request: msg.Nfsv4DelegpurgeRequest,
    ctx: Nfsv4OperationCtx,
  ): Promise<msg.Nfsv4DelegpurgeResponse> {
    return new msg.Nfsv4DelegpurgeResponse(Nfsv4Stat.NFS4ERR_NOTSUPP);
  }
 
  public async DELEGRETURN(
    request: msg.Nfsv4DelegreturnRequest,
    ctx: Nfsv4OperationCtx,
  ): Promise<msg.Nfsv4DelegreturnResponse> {
    return new msg.Nfsv4DelegreturnResponse(Nfsv4Stat.NFS4ERR_NOTSUPP);
  }
  public async COMMIT(request: msg.Nfsv4CommitRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4CommitResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    // If there is an open file corresponding to this path, prefer that fd
    let fd: any;
    for (const openFile of this.openFiles.values()) {
      if (openFile.path === currentPathAbsolute) {
        fd = openFile.fd as any;
        break;
      }
    }
    try {
      if (fd && typeof fd.datasync === 'function') {
        await fd.datasync();
      } else Eif (fd && typeof fd.sync === 'function') {
        await fd.sync();
      } else {
        // fallback: open and fdatasync
        const handle = await this.promises.open(currentPathAbsolute, this.fs.constants.O_RDONLY);
        try {
          if (typeof handle.datasync === 'function') await handle.datasync();
          else Iif (typeof handle.sync === 'function') await handle.sync();
        } finally {
          try {
            await handle.close();
          } catch (_e) {
            /* ignore */
          }
        }
      }
      // Return OK; no specific commit verifier currently persisted
      return new msg.Nfsv4CommitResponse(Nfsv4Stat.NFS4_OK);
    } catch (err: unknown) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4CommitResponse(status);
    }
  }
 
  public async CREATE(request: msg.Nfsv4CreateRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4CreateResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    const name = request.objname;
    const createPath = NodePath.join(currentPathAbsolute, name);
    Iif (createPath.length < this.dir.length) throw Nfsv4Stat.NFS4ERR_NOENT;
    try {
      const objType = request.objtype.type;
      if (objType === Nfsv4FType.NF4DIR) {
        let mode = 0o777;
        try {
          if (request.createattrs && request.createattrs.attrmask) {
            const dec = new XdrDecoder();
            dec.reader.reset(request.createattrs.attrVals);
            const maskSet = parseBitmask(request.createattrs.attrmask.mask);
            Iif (maskSet.has(Nfsv4Attr.FATTR4_MODE)) {
              const m = dec.readUnsignedInt();
              mode = m & 0o7777;
            }
          }
        } catch (_e) {
          // ignore parsing errors, fall back to default mode
        }
        await this.promises.mkdir(createPath, mode);
      } else if (objType === Nfsv4FType.NF4LNK) {
        const linkTarget = (request.objtype.objtype as struct.Nfsv4CreateTypeLink).linkdata;
        await this.promises.symlink(linkTarget, createPath);
      } else {
        let mode = 0o666;
        try {
          if (request.createattrs && request.createattrs.attrmask) {
            const dec = new XdrDecoder();
            dec.reader.reset(request.createattrs.attrVals);
            const maskSet = parseBitmask(request.createattrs.attrmask.mask);
            Iif (maskSet.has(Nfsv4Attr.FATTR4_MODE)) {
              const m = dec.readUnsignedInt();
              mode = m & 0o7777;
            }
          }
        } catch (_e) {
          // ignore parsing errors, fall back to default mode
        }
        const fd = await this.promises.open(
          createPath,
          this.fs.constants.O_CREAT | this.fs.constants.O_EXCL | this.fs.constants.O_RDWR,
          mode,
        );
        try {
          await fd.close();
        } catch {}
      }
      const _stats = await this.promises.stat(createPath);
      const fh = this.fh.encode(createPath);
      ctx.cfh = fh;
      const before = this.changeCounter;
      const after = ++this.changeCounter;
      const cinfo = new struct.Nfsv4ChangeInfo(true, before, after);
      const attrset = new struct.Nfsv4Bitmap([]);
      const resok = new msg.Nfsv4CreateResOk(cinfo, attrset);
      return new msg.Nfsv4CreateResponse(Nfsv4Stat.NFS4_OK, resok);
    } catch (err: unknown) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4CreateResponse(status);
    }
  }
 
  public async LINK(request: msg.Nfsv4LinkRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4LinkResponse> {
    const savedPath = this.fh.savedPath(ctx);
    const existingPath = this.absolutePath(savedPath);
    const currentPath = this.fh.currentPath(ctx);
    const newPath = this.absolutePath(NodePath.join(currentPath, request.newname));
    try {
      await this.promises.link(existingPath, newPath);
      const before = this.changeCounter;
      const after = ++this.changeCounter;
      const resok = new msg.Nfsv4LinkResOk(new struct.Nfsv4ChangeInfo(true, before, after));
      return new msg.Nfsv4LinkResponse(Nfsv4Stat.NFS4_OK, resok);
    } catch (err: unknown) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4LinkResponse(status);
    }
  }
 
  public async NVERIFY(request: msg.Nfsv4NverifyRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4NverifyResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    try {
      const stats = await this.promises.lstat(currentPathAbsolute);
      const fsStats = await this.fsStats();
      // request.objAttributes is a Nfsv4Fattr: use its attrmask when asking
      // encodeAttrs to serialize the server's current attributes and compare
      // raw attrVals bytes.
      const attrs = encodeAttrs(
        request.objAttributes.attrmask,
        stats,
        currentPathAbsolute,
        ctx.cfh!,
        this.leaseTime,
        fsStats,
      );
      if (cmpUint8Array(attrs.attrVals, request.objAttributes.attrVals))
        return new msg.Nfsv4NverifyResponse(Nfsv4Stat.NFS4ERR_NOT_SAME);
      return new msg.Nfsv4NverifyResponse(Nfsv4Stat.NFS4_OK);
    } catch (err: unknown) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4NverifyResponse(status);
    }
  }
 
  public async SETATTR(request: msg.Nfsv4SetattrRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4SetattrResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    try {
      const inFattr = request.objAttributes;
      const dec = new XdrDecoder();
      dec.reader.reset(inFattr.attrVals);
      const mask = inFattr.attrmask.mask;
      let atime: Date | undefined;
      let mtime: Date | undefined;
      let uid: number | undefined;
      let gid: number | undefined;
      for (let i = 0; i < mask.length; i++) {
        const word = mask[i];
        for (let bit = 0; bit < 32; bit++) {
          const bitMask = 1 << bit;
          if (!(word & bitMask)) continue;
          const attrNum = i * 32 + bit;
          switch (attrNum) {
            case Nfsv4Attr.FATTR4_MODE: {
              const mode = dec.readUnsignedInt();
              await this.promises.chmod(currentPathAbsolute, mode & 0o7777);
              break;
            }
            case Nfsv4Attr.FATTR4_OWNER: {
              const owner = dec.readString();
              const parsedUid = parseInt(owner, 10);
              if (!Number.isNaN(parsedUid)) {
                uid = parsedUid;
              }
              break;
            }
            case Nfsv4Attr.FATTR4_OWNER_GROUP: {
              const group = dec.readString();
              const parsedGid = parseInt(group, 10);
              if (!Number.isNaN(parsedGid)) {
                gid = parsedGid;
              }
              break;
            }
            case Nfsv4Attr.FATTR4_SIZE: {
              const size = dec.readUnsignedHyper();
              await this.promises.truncate(currentPathAbsolute, Number(size));
              break;
            }
            case Nfsv4Attr.FATTR4_TIME_ACCESS_SET: {
              const setIt = dec.readUnsignedInt();
              if (setIt === 1) {
                const seconds = Number(dec.readHyper());
                const nseconds = dec.readUnsignedInt();
                atime = new Date(seconds * 1000 + nseconds / 1000000);
              }
              break;
            }
            case Nfsv4Attr.FATTR4_TIME_MODIFY_SET: {
              const setIt = dec.readUnsignedInt();
              if (setIt === 1) {
                const seconds = Number(dec.readHyper());
                const nseconds = dec.readUnsignedInt();
                mtime = new Date(seconds * 1000 + nseconds / 1000000);
              }
              break;
            }
            case Nfsv4Attr.FATTR4_FILEHANDLE: {
              // read and ignore
              dec.readVarlenArray(() => dec.readUnsignedInt());
              break;
            }
            case Nfsv4Attr.FATTR4_SUPPORTED_ATTRS: {
              const len = dec.readUnsignedInt();
              for (let j = 0; j < len; j++) dec.readUnsignedInt();
              break;
            }
            case Nfsv4Attr.FATTR4_TYPE: {
              dec.readUnsignedInt();
              break;
            }
            case Nfsv4Attr.FATTR4_FILEID:
            case Nfsv4Attr.FATTR4_SPACE_USED:
            case Nfsv4Attr.FATTR4_CHANGE: {
              dec.readUnsignedHyper();
              break;
            }
            case Nfsv4Attr.FATTR4_TIME_ACCESS:
            case Nfsv4Attr.FATTR4_TIME_MODIFY:
            case Nfsv4Attr.FATTR4_TIME_METADATA: {
              dec.readHyper();
              dec.readUnsignedInt();
              break;
            }
            default: {
              return new msg.Nfsv4SetattrResponse(Nfsv4Stat.NFS4ERR_ATTRNOTSUPP);
            }
          }
        }
      }
      if (uid !== undefined || gid !== undefined) {
        const stats = await this.promises.lstat(currentPathAbsolute);
        const uidToSet = uid !== undefined ? uid : stats.uid;
        const gidToSet = gid !== undefined ? gid : stats.gid;
        await this.promises.chown(currentPathAbsolute, uidToSet, gidToSet);
      }
      if (atime || mtime) {
        const stats = await this.promises.lstat(currentPathAbsolute);
        const atimeToSet = atime || stats.atime;
        const mtimeToSet = mtime || stats.mtime;
        await this.promises.utimes(currentPathAbsolute, atimeToSet, mtimeToSet);
      }
      const stats = await this.promises.lstat(currentPathAbsolute);
      const fh = this.fh.encode(currentPath);
      const fsStats = await this.fsStats();
      // Return updated mode and size attributes
      const returnMask = new struct.Nfsv4Bitmap(attrNumsToBitmap([Nfsv4Attr.FATTR4_MODE, Nfsv4Attr.FATTR4_SIZE]));
      const _fattr = encodeAttrs(returnMask, stats, currentPath, fh, this.leaseTime, fsStats);
      const resok = new msg.Nfsv4SetattrResOk(returnMask);
      return new msg.Nfsv4SetattrResponse(Nfsv4Stat.NFS4_OK, resok);
    } catch (err: unknown) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4SetattrResponse(status);
    }
  }
 
  public async VERIFY(request: msg.Nfsv4VerifyRequest, ctx: Nfsv4OperationCtx): Promise<msg.Nfsv4VerifyResponse> {
    const currentPath = this.fh.currentPath(ctx);
    const currentPathAbsolute = this.absolutePath(currentPath);
    try {
      const stats = await this.promises.lstat(currentPathAbsolute);
      const fsStats = await this.fsStats();
      const attrs = encodeAttrs(request.objAttributes.attrmask, stats, currentPath, ctx.cfh!, this.leaseTime, fsStats);
      if (cmpUint8Array(attrs.attrVals, request.objAttributes.attrVals))
        return new msg.Nfsv4VerifyResponse(Nfsv4Stat.NFS4_OK);
      return new msg.Nfsv4VerifyResponse(Nfsv4Stat.NFS4ERR_NOT_SAME);
    } catch (err: unknown) {
      const status = normalizeNodeFsError(err, ctx.connection.logger);
      return new msg.Nfsv4VerifyResponse(status);
    }
  }
}