|
root / base / usr / src / uts / common / fs / smbsrv / smb_ofile.c
smb_ofile.c C 1959 lines 49.8 KB
   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
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2011-2020 Tintri by DDN, Inc. All rights reserved.
 * Copyright 2016 Syneto S.R.L. All rights reserved.
 * Copyright (c) 2016 by Delphix. All rights reserved.
 * Copyright 2021-2023 RackTop Systems, Inc.
 */

/*
 * General Structures Layout
 * -------------------------
 *
 * This is a simplified diagram showing the relationship between most of the
 * main structures.
 *
 * +-------------------+
 * |     SMB_INFO      |
 * +-------------------+
 *          |
 *          |
 *          v
 * +-------------------+       +-------------------+      +-------------------+
 * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
 * +-------------------+       +-------------------+      +-------------------+
 *   |          |
 *   |          |
 *   |          v
 *   |  +-------------------+     +-------------------+   +-------------------+
 *   |  |       USER        |<--->|       USER        |...|       USER        |
 *   |  +-------------------+     +-------------------+   +-------------------+
 *   |
 *   |
 *   v
 * +-------------------+       +-------------------+      +-------------------+
 * |       TREE        |<----->|       TREE        |......|       TREE        |
 * +-------------------+       +-------------------+      +-------------------+
 *      |         |
 *      |         |
 *      |         v
 *      |     +-------+       +-------+      +-------+
 *      |     | OFILE |<----->| OFILE |......| OFILE |
 *      |     +-------+       +-------+      +-------+
 *      |
 *      |
 *      v
 *  +-------+       +------+      +------+
 *  | ODIR  |<----->| ODIR |......| ODIR |
 *  +-------+       +------+      +------+
 *
 *
 * Ofile State Machine
 * ------------------
 *
 *    +-------------------------+	 T0
 *    |  SMB_OFILE_STATE_OPEN   |<--+-------- Creation/Allocation
 *    +-------------------------+   |
 *	    |		|	    | T5
 *	    |		|	+---------------------------+
 *	    |		|	| SMB_OFILE_STATE_RECONNECT |
 *	    |		|	+---------------------------+
 *	    |		|	    ^
 *	    |		v	    |
 *	    |   +---------------+   |
 *	    |   | STATE_SAVE_DH |   |
 *	    |   | STATE_SAVING  |   |
 *	    |   +---------------+   |
 *	    |		|	    | T4
 *	    | T1	| T3	+--------------------------+
 *	    |		+------>| SMB_OFILE_STATE_ORPHANED |
 *	    v			+--------------------------+
 *    +-------------------------+   |		|
 *    | SMB_OFILE_STATE_CLOSING |<--+ T6	| T7
 *    +-------------------------+		|
 *	    |		^			v
 *	    | T2	| T8	+-------------------------+
 *	    |		+-------| SMB_OFILE_STATE_EXPIRED |
 *	    v			+-------------------------+
 *    +-------------------------+
 *    | SMB_OFILE_STATE_CLOSED  |----------> Deletion/Free
 *    +-------------------------+    T9
 *
 * SMB_OFILE_STATE_OPEN
 *
 *    While in this state:
 *      - The ofile is queued in the list of ofiles of its tree.
 *      - References will be given out if the ofile is looked up.
 *
 * SMB_OFILE_STATE_SAVE_DH
 *
 *    Similar to state _CLOSING, but instead of deleting the ofile,
 *    it leaves the ofile in state _ORPHANED (for later reclaim).
 *    Will move to _SAVING after last ref, then _ORPHANED.
 *
 *    While in this state:
 *	- The ofile has been marked for preservation during a
 *	  walk of the tree ofile list to close multiple files.
 *	- References will not be given out if the ofile is looked up,
 *	  except for oplock break processing.
 *	- Still affects Sharing Violation rules
 *
 * SMB_OFILE_STATE_SAVING
 *
 *    Transient state used to keep oplock break processing out
 *    while the ofile moves to state _ORPHANED.
 *
 *    While in this state:
 *	- References will not be given out if the ofile is looked up,
 *	  except for oplock break processing.
 *	- Still affects Sharing Violation rules
 *
 * SMB_OFILE_STATE_CLOSING
 *
 *    Close has been requested.  Stay in this state until the last
 *    ref. is gone, then move to state _CLOSED
 *
 *    While in this state:
 *      - The ofile is queued in the list of ofiles of its tree.
 *      - References will not be given out if the ofile is looked up.
 *      - The file is closed and the locks held are being released.
 *      - The resources associated with the ofile remain.
 *
 * SMB_OFILE_STATE_CLOSED
 *
 *    While in this state:
 *      - The ofile is queued in the list of ofiles of its tree.
 *      - References will not be given out if the ofile is looked up.
 *      - The resources associated with the ofile remain.
 *
 * SMB_OFILE_STATE_ORPHANED
 *
 *    While in this state:
 *      - The ofile is queued in the list of ofiles of its tree.
 *      - Can be reclaimed by the original owner
 *      - References will not be given out if the ofile is looked up.
 *      - All the tree, user, and session "up" pointers are NULL!
 *      - Will eventually be "expired" if not reclaimed
 *      - Can be closed if its oplock is broken
 *      - Still affects Sharing Violation rules
 *
 * SMB_OFILE_STATE_EXPIRED
 *
 *    While in this state:
 *      - The ofile is queued in the list of ofiles of its tree.
 *      - References will not be given out if the ofile is looked up.
 *      - The ofile has not been reclaimed and will soon be closed,
 *        due to, for example, the durable handle timer expiring, or its
 *        oplock being broken.
 *      - Cannot be reclaimed at this point
 *
 * SMB_OFILE_STATE_RECONNECT
 *
 *    Transient state used to keep oplock break processing out
 *    while the ofile moves from state _ORPHANED to _OPEN.
 *
 *    While in this state:
 *      - The ofile is being reclaimed; do not touch it.
 *      - References will not be given out if the ofile is looked up.
 *      - Still affects Sharing Violation rules
 *	- see smb2_dh_reconnect() for which members need to be avoided
 *
 * Transition T0
 *
 *    This transition occurs in smb_ofile_open(). A new ofile is created and
 *    added to the list of ofiles of a tree.
 *
 * Transition T1
 *
 *    This transition occurs in smb_ofile_close(). Note that this only happens
 *    when we determine that an ofile should be closed in spite of its durable
 *    handle properties.
 *
 * Transition T2
 *
 *    This transition occurs in smb_ofile_release(). The resources associated
 *    with the ofile are freed as well as the ofile structure. For the
 *    transition to occur, the ofile must be in the SMB_OFILE_STATE_CLOSED
 *    state and the reference count be zero.
 *
 * Transition T3
 *
 *    This transition occurs in smb_ofile_orphan_dh(). It happens during an
 *    smb2 logoff, or during a session disconnect when certain conditions are
 *    met. The ofile and structures above it will be kept around until the ofile
 *    either gets reclaimed, expires after f_timeout_offset nanoseconds, or its
 *    oplock is broken.
 *
 * Transition T4
 *
 *    This transition occurs in smb2_dh_reconnect(). An smb2 create request
 *    with a DURABLE_HANDLE_RECONNECT(_V2) create context has been
 *    recieved from the original owner. If leases are supported or it's
 *    RECONNECT_V2, reconnect is subject to additional conditions. The ofile
 *    will be unwired from the old, disconnected session, tree, and user,
 *    and wired up to its new context.
 *
 * Transition T5
 *
 *    This transition occurs in smb2_dh_reconnect(). The ofile has been
 *    successfully reclaimed.
 *
 * Transition T6
 *
 *    This transition occurs in smb_ofile_close(). The ofile has been orphaned
 *    while some thread was blocked, and that thread closes the ofile. Can only
 *    happen when the ofile is orphaned due to an SMB2 LOGOFF request.
 *
 * Transition T7
 *
 *    This transition occurs in smb_session_durable_timers() and
 *    smb_oplock_send_break(). The ofile will soon be closed.
 *    In the former case, f_timeout_offset nanoseconds have passed since
 *    the ofile was orphaned. In the latter, an oplock break occured
 *    on the ofile while it was orphaned.
 *
 * Transition T8
 *
 *    This transition occurs in smb_ofile_close().
 *
 * Transition T9
 *
 *    This transition occurs in smb_ofile_delete().
 *
 * Comments
 * --------
 *
 *    The state machine of the ofile structures is controlled by 3 elements:
 *      - The list of ofiles of the tree it belongs to.
 *      - The mutex embedded in the structure itself.
 *      - The reference count.
 *
 *    There's a mutex embedded in the ofile structure used to protect its fields
 *    and there's a lock embedded in the list of ofiles of a tree. To
 *    increment or to decrement the reference count the mutex must be entered.
 *    To insert the ofile into the list of ofiles of the tree and to remove
 *    the ofile from it, the lock must be entered in RW_WRITER mode.
 *
 *    Rules of access to a ofile structure:
 *
 *    1) In order to avoid deadlocks, when both (mutex and lock of the ofile
 *       list) have to be entered, the lock must be entered first. Additionally,
 *       f_mutex must not be held when removing the ofile from sv_persistid_ht.
 *
 *    2) All actions applied to an ofile require a reference count.
 *
 *    3) There are 2 ways of getting a reference count. One is when the ofile
 *       is opened. The other one when the ofile is looked up. This translates
 *       into 2 functions: smb_ofile_open() and smb_ofile_lookup_by_fid().
 *
 *    It should be noted that the reference count of an ofile registers the
 *    number of references to the ofile in other structures (such as an smb
 *    request). The reference count is not incremented in these 2 instances:
 *
 *    1) The ofile is open. An ofile is anchored by its state. If there's
 *       no activity involving an ofile currently open, the reference count
 *       of that ofile is zero.
 *
 *    2) The ofile is queued in the list of ofiles of its tree. The fact of
 *       being queued in that list is NOT registered by incrementing the
 *       reference count.
 */
#include <smbsrv/smb2_kproto.h>
#include <smbsrv/smb_fsops.h>
#include <sys/time.h>
#include <sys/random.h>

static boolean_t smb_ofile_is_open_locked(smb_ofile_t *);
static void smb_ofile_delete(void *arg);
static void smb_ofile_save_dh(void *arg);

static int smb_ofile_netinfo_encode(smb_ofile_t *, uint8_t *, size_t,
    uint32_t *);
static int smb_ofile_netinfo_init(smb_ofile_t *, smb_netfileinfo_t *);
static void smb_ofile_netinfo_fini(smb_netfileinfo_t *);

/*
 * The uniq_fid is a CIFS-server-wide unique identifier for an ofile
 * which is used to uniquely identify open instances for the
 * VFS share reservation and POSIX locks.
 */
static volatile uint32_t smb_fids = 0;
#define	SMB_UNIQ_FID()	atomic_inc_32_nv(&smb_fids)

/*
 * AVL Comparator for t_ofile_list
 *
 * NB: can only access f_fid because smb_ofile_lookup_by_fid
 * doesn't pass in an entire smb_ofile_t.
 */
int
smb_ofile_avl_compare(const void *v1, const void *v2)
{
	const smb_ofile_t *of1 = v1;
	const smb_ofile_t *of2 = v2;

	return (AVL_CMP(of1->f_fid, of2->f_fid));
}

/*
 * smb_ofile_alloc
 * Allocate an ofile and fill in it's "up" pointers, but
 * do NOT link it into the tree's list of ofiles or the
 * node's list of ofiles.  An ofile in this state is a
 * "proposed" open passed to the oplock break code.
 *
 * If we don't get as far as smb_ofile_open with this OF,
 * call smb_ofile_free() to free this object.
 *
 * Note: The following sr members may be null during
 * persistent handle import: session, uid_usr, tid_tree
 */
smb_ofile_t *
smb_ofile_alloc(
    smb_request_t	*sr,
    smb_arg_open_t	*op,
    smb_node_t		*node, /* optional (may be NULL) */
    uint16_t		ftype,
    uint16_t		tree_fid)
{
	smb_user_t	*user = sr->uid_user;	/* optional */
	smb_tree_t	*tree = sr->tid_tree;	/* optional */
	smb_ofile_t	*of;

	of = kmem_cache_alloc(smb_cache_ofile, KM_SLEEP);
	bzero(of, sizeof (smb_ofile_t));
	of->f_magic = SMB_OFILE_MAGIC;

	mutex_init(&of->f_mutex, NULL, MUTEX_DEFAULT, NULL);
	list_create(&of->f_notify.nc_waiters, sizeof (smb_request_t),
	    offsetof(smb_request_t, sr_waiters));
	mutex_init(&of->dh_nvlock, NULL, MUTEX_DEFAULT, NULL);

	of->f_state = SMB_OFILE_STATE_ALLOC;
	of->f_refcnt = 1;
	of->f_ftype = ftype;
	of->f_fid = tree_fid;
	/* of->f_persistid see smb2_create */
	of->f_uniqid = SMB_UNIQ_FID();
	of->f_opened_by_pid = sr->smb_pid;
	of->f_granted_access = op->desired_access;
	of->f_share_access = op->share_access;
	of->f_create_options = op->create_options;
	if (user != NULL) {
		if ((op->create_options & FILE_OPEN_FOR_BACKUP_INTENT) != 0)
			of->f_cr = smb_user_getprivcred(user);
		else
			of->f_cr = user->u_cred;
		crhold(of->f_cr);
	}
	of->f_server = sr->sr_server;
	of->f_session = sr->session;	/* may be NULL */

	(void) memset(of->f_lock_seq, -1, SMB_OFILE_LSEQ_MAX);

	of->f_mode = smb_fsop_amask_to_omode(of->f_granted_access);
	if ((of->f_granted_access & FILE_DATA_ALL) == FILE_EXECUTE)
		of->f_flags |= SMB_OFLAGS_EXECONLY;

	/*
	 * In case a lease is requested, copy the lease keys now so
	 * any oplock breaks during open don't break those on our
	 * other handles that might have the same lease.
	 */
	bcopy(op->lease_key, of->TargetOplockKey, SMB_LEASE_KEY_SZ);
	bcopy(op->parent_lease_key, of->ParentOplockKey, SMB_LEASE_KEY_SZ);

	/*
	 * grab a ref for of->f_user and of->f_tree
	 * We know the user and tree must be "live" because
	 * this SR holds references to them.  The node ref. is
	 * held by our caller, until smb_ofile_open puts this
	 * ofile on the node ofile list with smb_node_add_ofile.
	 */
	if (user != NULL) {
		smb_user_hold_internal(user);
		of->f_user = user;
	}
	if (tree != NULL) {
		smb_tree_hold_internal(tree);
		of->f_tree = tree;
	}
	of->f_node = node;	/* may be NULL */

	return (of);
}

/*
 * smb_ofile_open
 *
 * Complete an open on an ofile that was previously allocated by
 * smb_ofile_alloc, by putting it on the tree ofile list and
 * (if it's a file) the node ofile list.
 */
void
smb_ofile_open(
    smb_request_t	*sr,
    smb_arg_open_t	*op,
    smb_ofile_t		*of)
{
	smb_tree_t	*tree = sr->tid_tree;
	smb_node_t	*node = of->f_node;

	ASSERT(of->f_state == SMB_OFILE_STATE_ALLOC);
	ASSERT(of->f_fid != 0);

	of->f_state = SMB_OFILE_STATE_OPEN;

	switch (of->f_ftype) {
	case SMB_FTYPE_BYTE_PIPE:
	case SMB_FTYPE_MESG_PIPE:
		/* See smb_opipe_open. */
		of->f_pipe = op->pipe;
		smb_server_inc_pipes(of->f_server);
		break;
	case SMB_FTYPE_DISK:
	case SMB_FTYPE_PRINTER:
		/* Regular file, not a pipe */
		ASSERT(node != NULL);

		smb_node_inc_open_ofiles(node);
		smb_node_add_ofile(node, of);
		smb_node_ref(node);
		smb_server_inc_files(of->f_server);
		break;
	default:
		ASSERT(0);
	}
	ASSERT(of->f_fid != 0);
	smb_lavl_enter(&tree->t_ofile_list, RW_WRITER);
	smb_lavl_insert(&tree->t_ofile_list, of);
	smb_lavl_exit(&tree->t_ofile_list);
	atomic_inc_32(&tree->t_open_files);
	atomic_inc_32(&of->f_session->s_file_cnt);

}

/*
 * smb_ofile_close
 *
 * Incoming states: (where from)
 *   SMB_OFILE_STATE_OPEN  protocol close, smb_ofile_drop
 *   SMB_OFILE_STATE_EXPIRED  called via smb2_dh_expire
 *   SMB_OFILE_STATE_ORPHANED  smb2_dh_shutdown
 *
 * Not that this enters the of->node->n_ofile_list rwlock as reader,
 * (via smb_oplock_close) so this must not be called while holding
 * that rwlock.
 */
void
smb_ofile_close(smb_ofile_t *of, int32_t mtime_sec)
{
	smb_attr_t *pa;

	SMB_OFILE_VALID(of);

	if (of->f_ftype == SMB_FTYPE_DISK) {
		smb_oplock_close(of);
	}

	mutex_enter(&of->f_mutex);
	ASSERT(of->f_refcnt);

	switch (of->f_state) {
	case SMB_OFILE_STATE_OPEN:
	case SMB_OFILE_STATE_ORPHANED:
	case SMB_OFILE_STATE_EXPIRED:
		of->f_state = SMB_OFILE_STATE_CLOSING;
		mutex_exit(&of->f_mutex);
		break;
	default:
		mutex_exit(&of->f_mutex);
		return;
	}

	/*
	 * Only one thread here (the one that that set f_state closing)
	 */
	switch (of->f_ftype) {
	case SMB_FTYPE_BYTE_PIPE:
	case SMB_FTYPE_MESG_PIPE:
		smb_opipe_close(of);
		smb_server_dec_pipes(of->f_server);
		break;

	case SMB_FTYPE_DISK:
		if (of->dh_persist)
			smb2_dh_close_persistent(of);
		if (of->f_persistid != 0)
			smb_ofile_del_persistid(of);
		/* FALLTHROUGH */

	case SMB_FTYPE_PRINTER: /* or FTYPE_DISK */
		/*
		 * In here we make changes to of->f_pending_attr
		 * while not holding of->f_mutex.  This is OK
		 * because we've changed f_state to CLOSING,
		 * so no more threads will take this path.
		 */
		pa = &of->f_pending_attr;
		if (mtime_sec != 0) {
			pa->sa_vattr.va_mtime.tv_sec = mtime_sec;
			pa->sa_mask |= SMB_AT_MTIME;
		}

		if (of->f_flags & SMB_OFLAGS_SET_DELETE_ON_CLOSE) {
			/* We delete using the on-disk name. */
			uint32_t flags = SMB_CASE_SENSITIVE;
			(void) smb_node_set_delete_on_close(of->f_node,
			    of->f_cr, flags);
		}
		smb_fsop_unshrlock(of->f_cr, of->f_node, of->f_uniqid);
		smb_node_destroy_lock_by_ofile(of->f_node, of);

		if (smb_node_is_file(of->f_node)) {
			(void) smb_fsop_close(of->f_node, of->f_mode,
			    of->f_cr);
		} else {
			/*
			 * If there was an odir, close it.
			 */
			if (of->f_odir != NULL)
				smb_odir_close(of->f_odir);
			/*
			 * Cancel any notify change requests that
			 * might be watching this open file (dir),
			 * and unsubscribe it from node events.
			 *
			 * Can't hold f_mutex when calling smb_notify_ofile.
			 * Don't really need it when unsubscribing, but
			 * harmless, and consistent with subscribing.
			 */
			if (of->f_notify.nc_subscribed)
				smb_notify_ofile(of,
				    FILE_ACTION_HANDLE_CLOSED, NULL);
			mutex_enter(&of->f_mutex);
			if (of->f_notify.nc_subscribed) {
				of->f_notify.nc_subscribed = B_FALSE;
				smb_node_fcn_unsubscribe(of->f_node);
				of->f_notify.nc_filter = 0;
			}
			mutex_exit(&of->f_mutex);
		}
		if (smb_node_dec_open_ofiles(of->f_node) == 0) {
			/*
			 * Last close.  If we're not deleting
			 * the file, apply any pending attrs.
			 * Leave allocsz zero when no open files,
			 * just to avoid confusion, because it's
			 * only updated when there are opens.
			 * XXX: Just do this on _every_ close.
			 */
			mutex_enter(&of->f_node->n_mutex);
			if (of->f_node->flags & NODE_FLAGS_DELETE_ON_CLOSE) {
				smb_node_delete_on_close(of->f_node);
				pa->sa_mask = 0;
			}
			of->f_node->n_allocsz = 0;
			mutex_exit(&of->f_node->n_mutex);
		}
		if (pa->sa_mask != 0) {
			/*
			 * Commit any pending attributes from
			 * the ofile we're closing.  Note that
			 * we pass NULL as the ofile to setattr
			 * so it will write to the file system
			 * and not keep anything on the ofile.
			 */
			(void) smb_node_setattr(NULL, of->f_node,
			    of->f_cr, NULL, pa);
		}
		/*
		 * Don't want the performance cost of generating
		 * change notify events on every write.  Instead:
		 * Keep track of the fact that we have written
		 * data via this handle, and do change notify
		 * work on the first write, and during close.
		 */
		if (of->f_written) {
			smb_node_notify_modified(of->f_node);
		}

		smb_server_dec_files(of->f_server);
		break;
	}

	/*
	 * Keep f_state == SMB_OFILE_STATE_CLOSING
	 * until the last ref. is dropped, in
	 * smb_ofile_release()
	 */
}

uint32_t smb2_savedh_timeout = 60;	/* sec. */

/*
 * "Destructor" function for smb_ofile_close_all, and
 * smb_ofile_close_all_by_pid, called after the llist lock
 * for tree list has been exited.  Our job is to either
 * close this ofile, or (if durable) set state _SAVE_DH.
 *
 * The next interesting thing happens when the last ref.
 * on this ofile calls smb_ofile_release(), where we
 * eihter delete the ofile, or (if durable) leave it
 * in the persistid hash table for possible reclaim.
 * (state becomes "orphaned").
 *
 * In case references dont't ever leave (eg. ref leak bug)
 * arrange for smb2_durable_timers() to move this ofile from
 * state SAVE_DH to state CLOSING.  The dh_expire_time set
 * on SAVE_DH ofiles here is temporary, and replaced with
 * the negotiated expiration time when it becomes ORPHANED.
 *
 * This is run via smb_llist_post (after smb_llist_exit)
 * because smb_ofile_close can block, and we'd rather not
 * block while holding the ofile list as reader.
 */
static void
smb_ofile_drop(void *arg)
{
	smb_ofile_t	*of = arg;

	SMB_OFILE_VALID(of);

	mutex_enter(&of->f_mutex);
	switch (of->f_state) {
	case SMB_OFILE_STATE_OPEN:
		/* DH checks under mutex. */
		if (of->f_ftype == SMB_FTYPE_DISK &&
		    of->dh_vers != SMB2_NOT_DURABLE &&
		    smb_dh_should_save(of)) {
			/*
			 * Tell smb_ofile_release() to
			 * make this an _ORPHANED DH.
			 */
			of->f_state = SMB_OFILE_STATE_SAVE_DH;
			of->dh_expire_time = gethrtime() +
			    SEC2NSEC(smb2_savedh_timeout);
			mutex_exit(&of->f_mutex);
			break;
		}
		/* OK close it. */
		mutex_exit(&of->f_mutex);
		smb_ofile_close(of, 0);
		break;

	default:
		/* Something else closed it already. */
		mutex_exit(&of->f_mutex);
		break;
	}

	/*
	 * Release the ref acquired during the traversal loop.
	 * Note that on the last ref, this ofile will be
	 * removed from the tree list etc.
	 * See: smb_llist_post, smb_ofile_delete
	 */
	smb_ofile_release(of);
}

/*
 * smb_ofile_close_all
 *
 *
 */
void
smb_ofile_close_all(
    smb_tree_t		*tree,
    uint32_t		pid)
{
	smb_ofile_t	*of;
	smb_lavl_t	*la;

	ASSERT(tree);
	ASSERT(tree->t_magic == SMB_TREE_MAGIC);

	la = &tree->t_ofile_list;

	smb_lavl_enter(la, RW_READER);
	for (of = smb_lavl_first(la);
	    of != NULL;
	    of = smb_lavl_next(la, of)) {
		ASSERT(of->f_magic == SMB_OFILE_MAGIC);
		ASSERT(of->f_tree == tree);
		if (pid != 0 && of->f_opened_by_pid != pid)
			continue;
		if (smb_ofile_hold(of)) {
			smb_lavl_post(la, of, smb_ofile_drop);
		}
	}

	/*
	 * Drop the lock and process the llist dtor queue.
	 * Calls smb_ofile_drop on ofiles that were open.
	 */
	smb_lavl_exit(la);
}

/*
 * If the enumeration request is for ofile data, handle it here.
 * Otherwise, return.
 *
 * This function should be called with a hold on the ofile.
 */
int
smb_ofile_enum(smb_ofile_t *of, smb_svcenum_t *svcenum)
{
	uint8_t *pb;
	uint_t nbytes;
	int rc;

	ASSERT(of);
	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
	ASSERT(of->f_refcnt);

	if (svcenum->se_type != SMB_SVCENUM_TYPE_FILE)
		return (0);

	if (svcenum->se_nskip > 0) {
		svcenum->se_nskip--;
		return (0);
	}

	if (svcenum->se_nitems >= svcenum->se_nlimit) {
		svcenum->se_nitems = svcenum->se_nlimit;
		return (0);
	}

	pb = &svcenum->se_buf[svcenum->se_bused];

	rc = smb_ofile_netinfo_encode(of, pb, svcenum->se_bavail,
	    &nbytes);
	if (rc == 0) {
		svcenum->se_bavail -= nbytes;
		svcenum->se_bused += nbytes;
		svcenum->se_nitems++;
	}

	return (rc);
}

/*
 * Take a reference on an open file, in any of the states:
 *   RECONNECT, SAVE_DH, OPEN, ORPHANED, EXPIRED.
 * Return TRUE if ref taken.  Used for oplock breaks.
 *
 * Note: When the oplock break code calls this, it holds the
 * node ofile list lock and node oplock mutex.  When we see
 * an ofile in states RECONNECT or SAVING, we know the ofile
 * is gaining or losing it's tree, and that happens quickly,
 * so we just wait for that work to finish.  However, the
 * waiting for state transitions here means we have to be
 * careful not to re-enter the node list lock or otherwise
 * block on things that could cause a deadlock.  Waiting
 * just on of->f_mutex here is OK.
 */
boolean_t
smb_ofile_hold_olbrk(smb_ofile_t *of)
{
	boolean_t ret = B_FALSE;

	ASSERT(of);
	ASSERT(of->f_magic == SMB_OFILE_MAGIC);

	mutex_enter(&of->f_mutex);

again:
	switch (of->f_state) {
	case SMB_OFILE_STATE_RECONNECT:
	case SMB_OFILE_STATE_SAVING:
		cv_wait(&of->f_cv, &of->f_mutex);
		goto again;

	case SMB_OFILE_STATE_OPEN:
	case SMB_OFILE_STATE_SAVE_DH:
	case SMB_OFILE_STATE_ORPHANED:
	case SMB_OFILE_STATE_EXPIRED:
		of->f_refcnt++;
		ret = B_TRUE;
		break;

	/*
	 * This is called only when an ofile has an oplock,
	 * so if we come across states that should not have
	 * an oplock, let's debug how that happened.
	 */
	default:
		ASSERT(0);
		break;
	}
	mutex_exit(&of->f_mutex);

	return (ret);
}

/*
 * Take a reference on an open file.
 */
boolean_t
smb_ofile_hold(smb_ofile_t *of)
{
	ASSERT(of);
	ASSERT(of->f_magic == SMB_OFILE_MAGIC);

	mutex_enter(&of->f_mutex);

	if (of->f_state != SMB_OFILE_STATE_OPEN) {
		mutex_exit(&of->f_mutex);
		return (B_FALSE);
	}
	of->f_refcnt++;

	mutex_exit(&of->f_mutex);
	return (B_TRUE);
}

/*
 * Void arg variant of smb_ofile_release for use with smb_llist_post.
 * This is needed because smb_ofile_release may need to enter the
 * smb_llist as writer when it drops the last reference, so when
 * we're in the llist as reader, use smb_llist_post with this
 * function to arrange for the release call at llist_exit.
 */
void
smb_ofile_release_LL(void *arg)
{
	smb_ofile_t	*of = arg;

	SMB_OFILE_VALID(of);
	smb_ofile_release(of);
}

/*
 * Release a reference on a file.  If the reference count falls to
 * zero and the file has been closed, post the object for deletion.
 * Object deletion is deferred to avoid modifying a list while an
 * iteration may be in progress.
 *
 * We're careful to avoid dropping f_session etc. until the last
 * reference goes away.  The oplock break code depends on that
 * not changing while it holds a ref. on an ofile.
 */
void
smb_ofile_release(smb_ofile_t *of)
{
	smb_tree_t *tree = of->f_tree;
	boolean_t delete = B_FALSE;

	SMB_OFILE_VALID(of);

	mutex_enter(&of->f_mutex);
	ASSERT(of->f_refcnt > 0);
	of->f_refcnt--;

	switch (of->f_state) {
	case SMB_OFILE_STATE_OPEN:
	case SMB_OFILE_STATE_ORPHANED:
	case SMB_OFILE_STATE_EXPIRED:
		break;

	case SMB_OFILE_STATE_SAVE_DH:
		ASSERT(tree != NULL);
		if (of->f_refcnt == 0) {
			of->f_state = SMB_OFILE_STATE_SAVING;
			smb_lavl_post(&tree->t_ofile_list, of,
			    smb_ofile_save_dh);
		}
		break;

	case SMB_OFILE_STATE_CLOSING:
		/* Note, tree == NULL on _ORPHANED */
		if (of->f_refcnt == 0) {
			of->f_state = SMB_OFILE_STATE_CLOSED;
			if (tree == NULL) {
				/* Skip smb_llist_post */
				delete = B_TRUE;
				break;
			}
			smb_lavl_post(&tree->t_ofile_list, of,
			    smb_ofile_delete);
		}
		break;

	default:
		ASSERT(0);
		break;
	}
	mutex_exit(&of->f_mutex);

	/*
	 * When we drop the last ref. on an expired DH, it's no longer
	 * in any tree, so skip the smb_llist_post and just call
	 * smb_ofile_delete directly.
	 */
	if (delete) {
		smb_ofile_delete(of);
	}
}

/*
 * smb_ofile_lookup_by_fid
 *
 * Find the open file whose fid matches the one specified in the request.
 * If we can't find the fid or the shares (trees) don't match, we have a
 * bad fid.
 */
smb_ofile_t *
smb_ofile_lookup_by_fid(
    smb_request_t	*sr,
    uint16_t		fid)
{
	smb_tree_t	*tree = sr->tid_tree;
	smb_lavl_t	*lavl;
	smb_ofile_t	*of;
	uint16_t	cmp_fid;

	CTASSERT(offsetof(smb_ofile_t, f_fid) == 0);
	ASSERT(tree->t_magic == SMB_TREE_MAGIC);

	cmp_fid = fid;

	lavl = &tree->t_ofile_list;

	smb_lavl_enter(lavl, RW_READER);
	of = avl_find(&lavl->la_tree, &cmp_fid, NULL);
	if (of == NULL)
		goto out;

	/*
	 * Only allow use of a given FID with the same UID that
	 * was used to open it.  MS-CIFS 3.3.5.14
	 */
	if (of->f_user != sr->uid_user) {
		of = NULL;
		goto out;
	}

	/* inline smb_ofile_hold() */
	mutex_enter(&of->f_mutex);
	if (of->f_state != SMB_OFILE_STATE_OPEN) {
		mutex_exit(&of->f_mutex);
		of = NULL;
		goto out;
	}
	of->f_refcnt++;
	mutex_exit(&of->f_mutex);

out:
	smb_lavl_exit(lavl);
	return (of);
}

/*
 * smb_ofile_lookup_by_uniqid
 *
 * Find the open file whose uniqid matches the one specified in the request.
 */
smb_ofile_t *
smb_ofile_lookup_by_uniqid(smb_tree_t *tree, uint32_t uniqid)
{
	smb_lavl_t	*lavl;
	smb_ofile_t	*of;

	ASSERT(tree->t_magic == SMB_TREE_MAGIC);

	lavl = &tree->t_ofile_list;
	smb_lavl_enter(lavl, RW_READER);
	of = smb_lavl_first(lavl);

	while (of) {
		ASSERT(of->f_magic == SMB_OFILE_MAGIC);
		ASSERT(of->f_tree == tree);

		if (of->f_uniqid == uniqid) {
			if (smb_ofile_hold(of)) {
				smb_lavl_exit(lavl);
				return (of);
			}
		}

		of = smb_lavl_next(lavl, of);
	}

	smb_lavl_exit(lavl);
	return (NULL);
}

/*
 * Durable ID (or persistent ID)
 */

static smb_ofile_t *
smb_ofile_hold_cb(smb_ofile_t *of)
{
	smb_ofile_t *ret = of;

	mutex_enter(&of->f_mutex);
	if (of->f_state == SMB_OFILE_STATE_ORPHANED)
		/* inline smb_ofile_hold() */
		of->f_refcnt++;
	else
		ret = NULL;

	mutex_exit(&of->f_mutex);
	return (ret);
}

/*
 * Lookup an ofile by persistent ID, and return ONLY if in state ORPHANED
 * This is used by SMB2 create "reclaim".
 */
smb_ofile_t *
smb_ofile_lookup_by_persistid(smb_request_t *sr, uint64_t persistid)
{
	smb_hash_t *hash;
	smb_bucket_t *bucket;
	smb_llist_t *ll;
	smb_ofile_t *of;
	uint_t idx;

	if (persistid == 0)
		return (NULL);

	hash = sr->sr_server->sv_persistid_ht;
	idx = smb_hash_uint64(hash, persistid);
	bucket = &hash->buckets[idx];
	ll = &bucket->b_list;

	smb_llist_enter(ll, RW_READER);
	of = smb_llist_head(ll);
	while (of != NULL) {
		if (of->f_persistid == persistid)
			break;
		of = smb_llist_next(ll, of);
	}
	if (of != NULL)
		of = smb_ofile_hold_cb(of);
	smb_llist_exit(ll);

	return (of);
}

/*
 * Create a (unique) durable/persistent ID for a new ofile,
 * and add this ofile to the persistid hash table.  This ID
 * is referred to as the persistent ID in the protocol spec,
 * so that's what we call it too, though the persistence may
 * vary.  "Durable" handles are persistent across reconnects
 * but not server reboots.  Persistent handles are persistent
 * across server reboots too.
 *
 * Note that persistent IDs need to be unique for the lifetime of
 * any given ofile.  For normal (non-persistent) ofiles we can just
 * use a persistent ID derived from the ofile memory address, as
 * these don't ever live beyond the current OS boot lifetime.
 *
 * Persistent handles are re-imported after server restart, and
 * generally have a different memory address after import than
 * they had in the previous OS boot lifetime, so for these we
 * use a randomly assigned value that won't conflict with any
 * non-persistent (durable) handles.  Ensuring that a randomly
 * generated ID is unique requires a search of the ofiles in one
 * hash bucket, which we'd rather avoid for non-persistent opens.
 *
 * The solution used here is to divide the persistent ID space
 * in half (odd and even values) where durable opens use an ID
 * derived from the ofile address (which is always even), and
 * persistent opens use an ID generated randomly (always odd).
 *
 * smb_ofile_set_persistid_dh() sets a durable handle ID and
 * smb_ofile_set_persistid_ph() sets a persistent handle ID.
 */
void
smb_ofile_set_persistid_dh(smb_ofile_t *of)
{
	smb_hash_t *hash = of->f_server->sv_persistid_ht;
	smb_bucket_t *bucket;
	smb_llist_t *ll;
	uint64_t persistid;
	uint_t idx;

	persistid = (uintptr_t)of;
	/* Avoid showing object addresses */
	persistid ^= ((uintptr_t)&smb_cache_ofile);
	/* make sure it's even */
	persistid &= ~((uint64_t)1);

	idx = smb_hash_uint64(hash, persistid);
	bucket = &hash->buckets[idx];
	ll = &bucket->b_list;
	smb_llist_enter(ll, RW_WRITER);
	if (of->f_persistid == 0) {
		of->f_persistid = persistid;
		smb_llist_insert_tail(ll, of);
	}
	smb_llist_exit(ll);
}

void
smb_ofile_set_persistid_ph(smb_ofile_t *of)
{
	uint64_t persistid;
	int rc;

top:
	(void) random_get_pseudo_bytes((uint8_t *)&persistid,
	    sizeof (persistid));
	if (persistid == 0) {
		cmn_err(CE_NOTE, "random gave all zeros!");
		goto top;
	}
	/* make sure it's odd */
	persistid |= (uint64_t)1;

	/*
	 * Try inserting with this persistent ID.
	 */
	rc = smb_ofile_insert_persistid(of, persistid);
	if (rc == EEXIST)
		goto top;
	if (rc != 0) {
		cmn_err(CE_NOTE, "set persistid rc=%d", rc);
	}
}

/*
 * Insert an ofile into the persistid hash table.
 * If the persistent ID is in use, error.
 */
int
smb_ofile_insert_persistid(smb_ofile_t *new_of, uint64_t persistid)
{
	smb_hash_t *hash = new_of->f_server->sv_persistid_ht;
	smb_bucket_t *bucket;
	smb_llist_t *ll;
	smb_ofile_t *of;
	uint_t idx;

	ASSERT(persistid != 0);

	/*
	 * Look to see if this key alreay exists.
	 */
	idx = smb_hash_uint64(hash, persistid);
	bucket = &hash->buckets[idx];
	ll = &bucket->b_list;

	smb_llist_enter(ll, RW_WRITER);
	of = smb_llist_head(ll);
	while (of != NULL) {
		if (of->f_persistid == persistid) {
			/* already in use */
			smb_llist_exit(ll);
			return (EEXIST);
		}
		of = smb_llist_next(ll, of);
	}

	/* Not found, so OK to insert. */
	if (new_of->f_persistid == 0) {
		new_of->f_persistid = persistid;
		smb_llist_insert_tail(ll, new_of);
	}
	smb_llist_exit(ll);

	return (0);
}

void
smb_ofile_del_persistid(smb_ofile_t *of)
{
	smb_hash_t *hash = of->f_server->sv_persistid_ht;
	smb_bucket_t *bucket;
	smb_llist_t *ll;
	uint_t idx;

	idx = smb_hash_uint64(hash, of->f_persistid);
	bucket = &hash->buckets[idx];
	ll = &bucket->b_list;
	smb_llist_enter(ll, RW_WRITER);
	if (of->f_persistid != 0) {
		smb_llist_remove(ll, of);
		of->f_persistid = 0;
	}
	smb_llist_exit(ll);
}

/*
 * Disallow NetFileClose on certain ofiles to avoid side-effects.
 * Closing a tree root is not allowed: use NetSessionDel or NetShareDel.
 * Closing SRVSVC connections is not allowed because this NetFileClose
 * request may depend on this ofile.
 */
boolean_t
smb_ofile_disallow_fclose(smb_ofile_t *of)
{
	ASSERT(of);
	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
	ASSERT(of->f_refcnt);

	switch (of->f_ftype) {
	case SMB_FTYPE_DISK:
		ASSERT(of->f_tree);
		return (of->f_node == of->f_tree->t_snode);

	case SMB_FTYPE_MESG_PIPE:
		ASSERT(of->f_pipe);
		if (smb_strcasecmp(of->f_pipe->p_name, "SRVSVC", 0) == 0)
			return (B_TRUE);
		break;
	default:
		break;
	}

	return (B_FALSE);
}

/*
 * smb_ofile_set_flags
 *
 * Return value:
 *
 *	Current flags value
 *
 */
void
smb_ofile_set_flags(
    smb_ofile_t		*of,
    uint32_t		flags)
{
	ASSERT(of);
	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
	ASSERT(of->f_refcnt);

	mutex_enter(&of->f_mutex);
	of->f_flags |= flags;
	mutex_exit(&of->f_mutex);
}

/*
 * smb_ofile_seek
 *
 * Return value:
 *
 *	0		Success
 *	EINVAL		Unknown mode
 *	EOVERFLOW	offset too big
 *
 */
int
smb_ofile_seek(
    smb_ofile_t		*of,
    ushort_t		mode,
    int32_t		off,
    uint32_t		*retoff)
{
	u_offset_t	newoff = 0;
	int		rc = 0;
	smb_attr_t	attr;

	ASSERT(of);
	ASSERT(of->f_magic == SMB_OFILE_MAGIC);
	ASSERT(of->f_refcnt);

	mutex_enter(&of->f_mutex);
	switch (mode) {
	case SMB_SEEK_SET:
		if (off < 0)
			newoff = 0;
		else
			newoff = (u_offset_t)off;
		break;

	case SMB_SEEK_CUR:
		if (off < 0 && (-off) > of->f_seek_pos)
			newoff = 0;
		else
			newoff = of->f_seek_pos + (u_offset_t)off;
		break;

	case SMB_SEEK_END:
		bzero(&attr, sizeof (smb_attr_t));
		attr.sa_mask |= SMB_AT_SIZE;
		rc = smb_fsop_getattr(NULL, zone_kcred(), of->f_node, &attr);
		if (rc != 0) {
			mutex_exit(&of->f_mutex);
			return (rc);
		}
		if (off < 0 && (-off) > attr.sa_vattr.va_size)
			newoff = 0;
		else
			newoff = attr.sa_vattr.va_size + (u_offset_t)off;
		break;

	default:
		mutex_exit(&of->f_mutex);
		return (EINVAL);
	}

	/*
	 * See comments at the beginning of smb_seek.c.
	 * If the offset is greater than UINT_MAX, we will return an error.
	 */

	if (newoff > UINT_MAX) {
		rc = EOVERFLOW;
	} else {
		of->f_seek_pos = newoff;
		*retoff = (uint32_t)newoff;
	}
	mutex_exit(&of->f_mutex);
	return (rc);
}

/*
 * smb_ofile_flush
 *
 * If writes on this file are not synchronous, flush it using the NFSv3
 * commit interface.
 *
 * XXX - todo: Flush named pipe should drain writes.
 */
void
smb_ofile_flush(struct smb_request *sr, struct smb_ofile *of)
{
	switch (of->f_ftype) {
	case SMB_FTYPE_DISK:
		if ((of->f_node->flags & NODE_FLAGS_WRITE_THROUGH) == 0)
			(void) smb_fsop_commit(sr, of->f_cr, of->f_node);
		break;
	default:
		break;
	}
}

/*
 * smb_ofile_is_open
 */
boolean_t
smb_ofile_is_open(smb_ofile_t *of)
{
	boolean_t	rc;

	SMB_OFILE_VALID(of);

	mutex_enter(&of->f_mutex);
	rc = smb_ofile_is_open_locked(of);
	mutex_exit(&of->f_mutex);
	return (rc);
}

/* *************************** Static Functions ***************************** */

/*
 * Determine whether or not an ofile is open.
 * This function must be called with the mutex held.
 */
static boolean_t
smb_ofile_is_open_locked(smb_ofile_t *of)
{
	ASSERT(MUTEX_HELD(&of->f_mutex));

	switch (of->f_state) {
	case SMB_OFILE_STATE_OPEN:
	case SMB_OFILE_STATE_SAVE_DH:
	case SMB_OFILE_STATE_SAVING:
	case SMB_OFILE_STATE_ORPHANED:
	case SMB_OFILE_STATE_RECONNECT:
		return (B_TRUE);

	case SMB_OFILE_STATE_CLOSING:
	case SMB_OFILE_STATE_CLOSED:
	case SMB_OFILE_STATE_EXPIRED:
		return (B_FALSE);

	default:
		ASSERT(0);
		return (B_FALSE);
	}
}

/*
 * smb_ofile_save_dh
 *
 * Called via smb_llist_post (after smb_llist_exit) when the last ref.
 * on this ofile has gone, and this ofile is a "durable handle" (DH)
 * that has state we've decided to save.
 *
 * This does parts of what smb_ofile_delete would do, including:
 * remove the ofile from the tree ofile list and related.
 *
 * We leave the ofile in state ORPHANED, ready for reconnect
 * or expiration via smb2_dh_expire (see smb_ofile_delete).
 */
static void
smb_ofile_save_dh(void *arg)
{
	smb_ofile_t	*of = (smb_ofile_t *)arg;
	smb_tree_t	*tree = of->f_tree;

	SMB_OFILE_VALID(of);
	ASSERT(of->f_refcnt == 0);
	ASSERT(of->f_ftype == SMB_FTYPE_DISK);
	ASSERT(of->f_state == SMB_OFILE_STATE_SAVING);

	atomic_dec_32(&of->f_session->s_file_cnt);
	atomic_dec_32(&of->f_tree->t_open_files);
	smb_lavl_enter(&tree->t_ofile_list, RW_WRITER);
	smb_lavl_remove(&tree->t_ofile_list, of);
	smb_lavl_exit(&tree->t_ofile_list);

	/*
	 * This ofile is no longer on t_ofile_list, however...
	 *
	 * This is called via smb_llist_post, which means it may run
	 * BEFORE smb_ofile_release drops f_mutex (if another thread
	 * flushes the delete queue before we do).  Synchronize.
	 */
	mutex_enter(&of->f_mutex);
	DTRACE_PROBE1(ofile__exit, smb_ofile_t *, of);
	mutex_exit(&of->f_mutex);

	/*
	 * Keep f_notify state, lease, and
	 * keep on node ofile list.
	 * Keep of->f_cr until reclaim.
	 */

	ASSERT(of->f_fid != 0);
	smb_idpool_free(&tree->t_fid_pool, of->f_fid);
	of->f_fid = 0;
	smb_tree_release(of->f_tree);
	of->f_tree = NULL;
	smb_user_release(of->f_user);
	of->f_user = NULL;
	of->f_session = NULL;

	/*
	 * Make it "orphaned" so it can now be reclaimed.
	 * Note that smb_ofile_hold_olbrk() may have blocked
	 * for state SMB_OFILE_STATE_SAVING, so wake it.
	 */
	mutex_enter(&of->f_mutex);
	of->dh_expire_time = gethrtime() + of->dh_timeout_offset;
	of->f_state = SMB_OFILE_STATE_ORPHANED;
	cv_broadcast(&of->f_cv);
	mutex_exit(&of->f_mutex);
}

/*
 * Delete an ofile.
 *
 * Approximately the inverse of smb_ofile_alloc()
 * Called via smb_llist_post (after smb_llist_exit)
 * when the last ref. on this ofile has gone.
 *
 * Normally,this removes the ofile from the tree list and
 * then frees resources held on the ofile.  However, when
 * we're expiring an orphaned durable handle, the linkage
 * into the tree lists etc. have already been destroyed.
 * This case is distinguished by of->f_tree == NULL.
 */
static void
smb_ofile_delete(void *arg)
{
	smb_ofile_t	*of = (smb_ofile_t *)arg;
	smb_tree_t	*tree = of->f_tree;

	SMB_OFILE_VALID(of);
	ASSERT(of->f_refcnt == 0);
	ASSERT(of->f_state == SMB_OFILE_STATE_CLOSED);

	if (tree != NULL) {
		ASSERT(of->f_user != NULL);
		ASSERT(of->f_session != NULL);
		atomic_dec_32(&of->f_session->s_file_cnt);
		atomic_dec_32(&of->f_tree->t_open_files);
		smb_lavl_enter(&tree->t_ofile_list, RW_WRITER);
		smb_lavl_remove(&tree->t_ofile_list, of);
		smb_lavl_exit(&tree->t_ofile_list);
	}

	/*
	 * Remove this ofile from the node's n_ofile_list so it
	 * can't be found by list walkers like notify or oplock.
	 * Keep the node ref. until later in this function so
	 * of->f_node remains valid while we destroy the ofile.
	 */
	if (of->f_ftype == SMB_FTYPE_DISK ||
	    of->f_ftype == SMB_FTYPE_PRINTER) {
		smb_node_t *node = of->f_node;

		/*
		 * Oplock cleanup should have made sure that
		 * excl_open does not point to this ofile.
		 */
		VERIFY(node->n_oplock.excl_open != of);

		/*
		 * Note smb_ofile_close did smb_node_dec_open_ofiles()
		 */
		smb_node_rem_ofile(node, of);
	}

	/*
	 * This ofile is no longer on any lists, however...
	 *
	 * This is called via smb_llist_post, which means it may run
	 * BEFORE smb_ofile_release drops f_mutex (if another thread
	 * flushes the delete queue before we do).  Synchronize.
	 */
	mutex_enter(&of->f_mutex);
	of->f_state = SMB_OFILE_STATE_ALLOC;
	DTRACE_PROBE1(ofile__exit, smb_ofile_t *, of);
	mutex_exit(&of->f_mutex);

	switch (of->f_ftype) {
	case SMB_FTYPE_BYTE_PIPE:
	case SMB_FTYPE_MESG_PIPE:
		smb_opipe_dealloc(of->f_pipe);
		of->f_pipe = NULL;
		break;
	case SMB_FTYPE_DISK:
		ASSERT(of->f_notify.nc_subscribed == B_FALSE);
		MBC_FLUSH(&of->f_notify.nc_buffer);
		if (of->f_odir != NULL)
			smb_odir_release(of->f_odir);
		if (of->f_lease != NULL) {
			smb2_lease_rele(of->f_lease);
			of->f_lease = NULL;
		}
		/* FALLTHROUGH */
	case SMB_FTYPE_PRINTER:
		/*
		 * Did smb_node_rem_ofile above.
		 */
		ASSERT(of->f_node != NULL);
		smb_node_release(of->f_node);
		break;
	default:
		ASSERT(!"f_ftype");
		break;
	}

	smb_ofile_free(of);
}

void
smb_ofile_free(smb_ofile_t *of)
{
	smb_tree_t	*tree = of->f_tree;

	ASSERT(of->f_state == SMB_OFILE_STATE_ALLOC);

	/* Make sure it's not in the persistid hash. */
	VERIFY(of->f_persistid == 0);

	if (tree != NULL) {
		if (of->f_fid != 0)
			smb_idpool_free(&tree->t_fid_pool, of->f_fid);
		smb_tree_release(of->f_tree);
		smb_user_release(of->f_user);
	}

	if (of->f_cr != NULL)
		crfree(of->f_cr);

	of->f_magic = (uint32_t)~SMB_OFILE_MAGIC;
	list_destroy(&of->f_notify.nc_waiters);
	mutex_destroy(&of->dh_nvlock);
	mutex_destroy(&of->f_mutex);
	kmem_cache_free(smb_cache_ofile, of);
}

/*
 * smb_ofile_access
 *
 * This function will check to see if the access requested is granted.
 * Returns NT status codes.
 */
uint32_t
smb_ofile_access(smb_ofile_t *of, cred_t *cr, uint32_t access)
{

	if ((of == NULL) || (cr == zone_kcred()))
		return (NT_STATUS_SUCCESS);

	/*
	 * If the request is for something
	 * I don't grant it is an error
	 */
	if (~(of->f_granted_access) & access) {
		if (!(of->f_granted_access & ACCESS_SYSTEM_SECURITY) &&
		    (access & ACCESS_SYSTEM_SECURITY)) {
			return (NT_STATUS_PRIVILEGE_NOT_HELD);
		}
		return (NT_STATUS_ACCESS_DENIED);
	}

	return (NT_STATUS_SUCCESS);
}

/*
 * smb_ofile_share_check
 *
 * Check if ofile was opened with share access NONE (0).
 * Returns: B_TRUE  - share access non-zero
 *          B_FALSE - share access NONE
 */
boolean_t
smb_ofile_share_check(smb_ofile_t *of)
{
	return (!SMB_DENY_ALL(of->f_share_access));
}

/*
 * check file sharing rules for current open request
 * against existing open instances of the same file
 *
 * Returns NT_STATUS_SHARING_VIOLATION if there is any
 * sharing conflict, otherwise returns NT_STATUS_SUCCESS.
 */
uint32_t
smb_ofile_open_check(smb_ofile_t *of, uint32_t desired_access,
    uint32_t share_access)
{
	uint32_t ret;

	ASSERT(of->f_magic == SMB_OFILE_MAGIC);

	mutex_enter(&of->f_mutex);

	if (!smb_ofile_is_open_locked(of)) {
		ret = NT_STATUS_INVALID_HANDLE;
		goto out;
	}

	/* if it's just meta data */
	if ((of->f_granted_access & FILE_DATA_ALL) == 0) {
		ret = NT_STATUS_SUCCESS;
		goto out;
	}

	/*
	 * Check requested share access against the
	 * open granted (desired) access
	 */
	if (SMB_DENY_DELETE(share_access) && (of->f_granted_access & DELETE)) {
		ret = NT_STATUS_SHARING_VIOLATION;
		goto out;
	}

	if (SMB_DENY_READ(share_access) &&
	    (of->f_granted_access & (FILE_READ_DATA | FILE_EXECUTE))) {
		ret = NT_STATUS_SHARING_VIOLATION;
		goto out;
	}

	if (SMB_DENY_WRITE(share_access) &&
	    (of->f_granted_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
		ret = NT_STATUS_SHARING_VIOLATION;
		goto out;
	}

	/* check requested desired access against the open share access */
	if (SMB_DENY_DELETE(of->f_share_access) && (desired_access & DELETE)) {
		ret = NT_STATUS_SHARING_VIOLATION;
		goto out;
	}

	if (SMB_DENY_READ(of->f_share_access) &&
	    (desired_access & (FILE_READ_DATA | FILE_EXECUTE))) {
		ret = NT_STATUS_SHARING_VIOLATION;
		goto out;
	}

	if (SMB_DENY_WRITE(of->f_share_access) &&
	    (desired_access & (FILE_WRITE_DATA | FILE_APPEND_DATA))) {
		ret = NT_STATUS_SHARING_VIOLATION;
		goto out;
	}

	ret = NT_STATUS_SUCCESS;
out:
	mutex_exit(&of->f_mutex);
	return (ret);
}

/*
 * smb_ofile_rename_check
 *
 * This does the work described in MS-FSA 2.1.5.1.2.2 (Algorithm
 * to Check Sharing Access to an Existing Stream or Directory),
 * where the "open in-progress" has DesiredAccess = DELETE and
 * SharingMode = SHARE_READ | SHARE_WRITE | SHARE_DELETE.
 */

uint32_t
smb_ofile_rename_check(smb_ofile_t *of)
{
	uint32_t ret;

	ASSERT(of->f_magic == SMB_OFILE_MAGIC);

	mutex_enter(&of->f_mutex);

	if (!smb_ofile_is_open_locked(of)) {
		ret = NT_STATUS_INVALID_HANDLE;
		goto out;
	}

	if ((of->f_granted_access & FILE_DATA_ALL) == 0) {
		ret = NT_STATUS_SUCCESS;
		goto out;
	}

	if ((of->f_share_access & FILE_SHARE_DELETE) == 0) {
		ret = NT_STATUS_SHARING_VIOLATION;
		goto out;
	}

	ret = NT_STATUS_SUCCESS;
out:
	mutex_exit(&of->f_mutex);
	return (ret);
}

/*
 * smb_ofile_delete_check
 *
 * An open file can be deleted only if opened for
 * accessing meta data. Share modes aren't important
 * in this case.
 *
 * NOTE: there is another mechanism for deleting an
 * open file that NT clients usually use.
 * That's setting "Delete on close" flag for an open
 * file.  In this way the file will be deleted after
 * last close. This flag can be set by SmbTrans2SetFileInfo
 * with FILE_DISPOSITION_INFO information level.
 * For setting this flag, the file should be opened by
 * DELETE access in the FID that is passed in the Trans2
 * request.
 */

uint32_t
smb_ofile_delete_check(smb_ofile_t *of)
{
	uint32_t ret;

	ASSERT(of->f_magic == SMB_OFILE_MAGIC);

	mutex_enter(&of->f_mutex);

	if (!smb_ofile_is_open_locked(of)) {
		ret = NT_STATUS_INVALID_HANDLE;
		goto out;
	}

	if (of->f_granted_access &
	    (FILE_READ_DATA | FILE_WRITE_DATA |
	    FILE_APPEND_DATA | FILE_EXECUTE | DELETE)) {
		ret = NT_STATUS_SHARING_VIOLATION;
		goto out;
	}

	ret = NT_STATUS_SUCCESS;
out:
	mutex_exit(&of->f_mutex);
	return (ret);
}

cred_t *
smb_ofile_getcred(smb_ofile_t *of)
{
	return (of->f_cr);
}

/*
 * smb_ofile_set_delete_on_close
 *
 * Set the DeleteOnClose flag on the smb file. When the file is closed,
 * the flag will be transferred to the smb node, which will commit the
 * delete operation and inhibit subsequent open requests.
 *
 * When DeleteOnClose is set on an smb_node, the common open code will
 * reject subsequent open requests for the file. Observation of Windows
 * 2000 indicates that subsequent opens should be allowed (assuming
 * there would be no sharing violation) until the file is closed using
 * the fid on which the DeleteOnClose was requested.
 */
void
smb_ofile_set_delete_on_close(smb_request_t *sr, smb_ofile_t *of)
{
	uint32_t	status;

	/*
	 * Break any oplock handle caching.
	 */
	status = smb_oplock_break_SETINFO(of->f_node, of,
	    FileDispositionInformation);
	if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS) {
		if (sr->session->dialect >= SMB_VERS_2_BASE)
			(void) smb2sr_go_async(sr);
		(void) smb_oplock_wait_break(sr, of->f_node, 0);
	}

	mutex_enter(&of->f_mutex);
	of->f_flags |= SMB_OFLAGS_SET_DELETE_ON_CLOSE;
	mutex_exit(&of->f_mutex);
}

/*
 * Encode open file information into a buffer; needed in user space to
 * support RPC requests.
 */
static int
smb_ofile_netinfo_encode(smb_ofile_t *of, uint8_t *buf, size_t buflen,
    uint32_t *nbytes)
{
	smb_netfileinfo_t	fi;
	int			rc;

	rc = smb_ofile_netinfo_init(of, &fi);
	if (rc == 0) {
		rc = smb_netfileinfo_encode(&fi, buf, buflen, nbytes);
		smb_ofile_netinfo_fini(&fi);
	}

	return (rc);
}

static int
smb_ofile_netinfo_init(smb_ofile_t *of, smb_netfileinfo_t *fi)
{
	smb_user_t	*user;
	smb_tree_t	*tree;
	smb_node_t	*node;
	char		*path;
	char		*buf;
	int		rc;

	ASSERT(of);
	user = of->f_user;
	tree = of->f_tree;
	ASSERT(user);
	ASSERT(tree);

	buf = kmem_zalloc(MAXPATHLEN, KM_SLEEP);

	switch (of->f_ftype) {
	case SMB_FTYPE_DISK:
		node = of->f_node;
		ASSERT(node);

		fi->fi_permissions = of->f_granted_access;
		fi->fi_numlocks = smb_lock_get_lock_count(node, of);

		path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);

		if (node != tree->t_snode) {
			rc = smb_node_getshrpath(node, tree, path, MAXPATHLEN);
			if (rc != 0)
				(void) strlcpy(path, node->od_name, MAXPATHLEN);
		}

		(void) snprintf(buf, MAXPATHLEN, "%s:%s", tree->t_sharename,
		    path);
		kmem_free(path, MAXPATHLEN);
		break;

	case SMB_FTYPE_MESG_PIPE:
		ASSERT(of->f_pipe);

		fi->fi_permissions = FILE_READ_DATA | FILE_WRITE_DATA |
		    FILE_EXECUTE;
		fi->fi_numlocks = 0;
		(void) snprintf(buf, MAXPATHLEN, "\\PIPE\\%s",
		    of->f_pipe->p_name);
		break;

	default:
		kmem_free(buf, MAXPATHLEN);
		return (-1);
	}

	fi->fi_fid = of->f_fid;
	fi->fi_uniqid = of->f_uniqid;
	fi->fi_pathlen = strlen(buf) + 1;
	fi->fi_path = smb_mem_strdup(buf);
	kmem_free(buf, MAXPATHLEN);

	fi->fi_namelen = user->u_domain_len + user->u_name_len + 2;
	fi->fi_username = kmem_alloc(fi->fi_namelen, KM_SLEEP);
	(void) snprintf(fi->fi_username, fi->fi_namelen, "%s\\%s",
	    user->u_domain, user->u_name);
	return (0);
}

static void
smb_ofile_netinfo_fini(smb_netfileinfo_t *fi)
{
	if (fi == NULL)
		return;

	if (fi->fi_path)
		smb_mem_free(fi->fi_path);
	if (fi->fi_username)
		kmem_free(fi->fi_username, fi->fi_namelen);

	bzero(fi, sizeof (smb_netfileinfo_t));
}

/*
 * A query of user and group quotas may span multiple requests.
 * f_quota_resume is used to determine where the query should
 * be resumed, in a subsequent request. f_quota_resume contains
 * the SID of the last quota entry returned to the client.
 */
void
smb_ofile_set_quota_resume(smb_ofile_t *ofile, char *resume)
{
	ASSERT(ofile);
	mutex_enter(&ofile->f_mutex);
	if (resume == NULL)
		bzero(ofile->f_quota_resume, SMB_SID_STRSZ);
	else
		(void) strlcpy(ofile->f_quota_resume, resume, SMB_SID_STRSZ);
	mutex_exit(&ofile->f_mutex);
}

void
smb_ofile_get_quota_resume(smb_ofile_t *ofile, char *buf, int bufsize)
{
	ASSERT(ofile);
	mutex_enter(&ofile->f_mutex);
	(void) strlcpy(buf, ofile->f_quota_resume, bufsize);
	mutex_exit(&ofile->f_mutex);
}