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
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
|
/*
* 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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, Joyent, Inc.
* Copyright (c) 2017 by Delphix. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/t_lock.h>
#include <sys/systm.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/dnlc.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/vtrace.h>
#include <sys/bitmap.h>
#include <sys/var.h>
#include <sys/sysmacros.h>
#include <sys/kstat.h>
#include <sys/atomic.h>
#include <sys/taskq.h>
/*
* Directory name lookup cache.
* Based on code originally done by Robert Elz at Melbourne.
*
* Names found by directory scans are retained in a cache
* for future reference. Each hash chain is ordered by LRU
* Cache is indexed by hash value obtained from (vp, name)
* where the vp refers to the directory containing the name.
*/
/*
* We want to be able to identify files that are referenced only by the DNLC.
* When adding a reference from the DNLC, call VN_HOLD_DNLC instead of VN_HOLD,
* since multiple DNLC references should only be counted once in v_count. The
* VN_HOLD macro itself is aliased to VN_HOLD_CALLER in this file to help
* differentiate the behaviors. (Unfortunately it is not possible to #undef
* VN_HOLD and retain VN_HOLD_CALLER. Ideally a Makefile rule would grep
* uncommented C tokens to check that VN_HOLD is referenced only once in this
* file, to define VN_HOLD_CALLER.)
*/
#define VN_HOLD_CALLER VN_HOLD
#define VN_HOLD_DNLC(vp) { \
mutex_enter(&(vp)->v_lock); \
if ((vp)->v_count_dnlc == 0) { \
VN_HOLD_LOCKED(vp); \
} \
(vp)->v_count_dnlc++; \
mutex_exit(&(vp)->v_lock); \
}
#define VN_RELE_DNLC(vp) { \
vn_rele_dnlc(vp); \
}
/*
* Tunable nc_hashavelen is the average length desired for this chain, from
* which the size of the nc_hash table is derived at create time.
*/
#define NC_HASHAVELEN_DEFAULT 4
int nc_hashavelen = NC_HASHAVELEN_DEFAULT;
/*
* NC_MOVETOFRONT is the move-to-front threshold: if the hash lookup
* depth exceeds this value, we move the looked-up entry to the front of
* its hash chain. The idea is to make sure that the most frequently
* accessed entries are found most quickly (by keeping them near the
* front of their hash chains).
*/
#define NC_MOVETOFRONT 2
/*
*
* DNLC_MAX_RELE is used to size an array on the stack when releasing
* vnodes. This array is used rather than calling VN_RELE() inline because
* all dnlc locks must be dropped by that time in order to avoid a
* possible deadlock. This deadlock occurs when the dnlc holds the last
* reference to the vnode and so the VOP_INACTIVE vector is called which
* can in turn call back into the dnlc. A global array was used but had
* many problems:
* 1) Actually doesn't have an upper bound on the array size as
* entries can be added after starting the purge.
* 2) The locking scheme causes a hang.
* 3) Caused serialisation on the global lock.
* 4) The array was often unnecessarily huge.
*
* Note the current value 8 allows up to 4 cache entries (to be purged
* from each hash chain), before having to cycle around and retry.
* This ought to be ample given that nc_hashavelen is typically very small.
*/
#define DNLC_MAX_RELE 8 /* must be even */
/*
* Hash table of name cache entries for fast lookup, dynamically
* allocated at startup.
*/
nc_hash_t *nc_hash;
/*
* Rotors. Used to select entries on a round-robin basis.
*/
static nc_hash_t *dnlc_purge_fs1_rotor;
static nc_hash_t *dnlc_free_rotor;
/*
* # of dnlc entries (uninitialized)
*
* the initial value was chosen as being
* a random string of bits, probably not
* normally chosen by a systems administrator
*/
int ncsize = -1;
volatile uint32_t dnlc_nentries = 0; /* current num of name cache entries */
static int nc_hashsz; /* size of hash table */
static int nc_hashmask; /* size of hash table minus 1 */
/*
* The dnlc_reduce_cache() taskq queue is activated when there are
* ncsize name cache entries and if no parameter is provided, it reduces
* the size down to dnlc_nentries_low_water, which is by default one
* hundreth less (or 99%) of ncsize.
*
* If a parameter is provided to dnlc_reduce_cache(), then we reduce
* the size down based on ncsize_onepercent - where ncsize_onepercent
* is 1% of ncsize; however, we never let dnlc_reduce_cache() reduce
* the size below 3% of ncsize (ncsize_min_percent).
*/
#define DNLC_LOW_WATER_DIVISOR_DEFAULT 100
uint_t dnlc_low_water_divisor = DNLC_LOW_WATER_DIVISOR_DEFAULT;
uint_t dnlc_nentries_low_water;
int dnlc_reduce_idle = 1; /* no locking needed */
uint_t ncsize_onepercent;
uint_t ncsize_min_percent;
/*
* If dnlc_nentries hits dnlc_max_nentries (twice ncsize)
* then this means the dnlc_reduce_cache() taskq is failing to
* keep up. In this case we refuse to add new entries to the dnlc
* until the taskq catches up.
*/
uint_t dnlc_max_nentries; /* twice ncsize */
uint64_t dnlc_max_nentries_cnt = 0; /* statistic on times we failed */
/*
* Tunable to define when we should just remove items from
* the end of the chain.
*/
#define DNLC_LONG_CHAIN 8
uint_t dnlc_long_chain = DNLC_LONG_CHAIN;
/*
* ncstats has been deprecated, due to the integer size of the counters
* which can easily overflow in the dnlc.
* It is maintained (at some expense) for compatability.
* The preferred interface is the kstat accessible nc_stats below.
*/
struct ncstats ncstats;
struct nc_stats ncs = {
{ "hits", KSTAT_DATA_UINT64 },
{ "misses", KSTAT_DATA_UINT64 },
{ "negative_cache_hits", KSTAT_DATA_UINT64 },
{ "enters", KSTAT_DATA_UINT64 },
{ "double_enters", KSTAT_DATA_UINT64 },
{ "purge_total_entries", KSTAT_DATA_UINT64 },
{ "purge_all", KSTAT_DATA_UINT64 },
{ "purge_vp", KSTAT_DATA_UINT64 },
{ "purge_vfs", KSTAT_DATA_UINT64 },
{ "purge_fs1", KSTAT_DATA_UINT64 },
{ "pick_free", KSTAT_DATA_UINT64 },
{ "pick_heuristic", KSTAT_DATA_UINT64 },
{ "pick_last", KSTAT_DATA_UINT64 },
/* directory caching stats */
{ "dir_hits", KSTAT_DATA_UINT64 },
{ "dir_misses", KSTAT_DATA_UINT64 },
{ "dir_cached_current", KSTAT_DATA_UINT64 },
{ "dir_entries_cached_current", KSTAT_DATA_UINT64 },
{ "dir_cached_total", KSTAT_DATA_UINT64 },
{ "dir_start_no_memory", KSTAT_DATA_UINT64 },
{ "dir_add_no_memory", KSTAT_DATA_UINT64 },
{ "dir_add_abort", KSTAT_DATA_UINT64 },
{ "dir_add_max", KSTAT_DATA_UINT64 },
{ "dir_remove_entry_fail", KSTAT_DATA_UINT64 },
{ "dir_remove_space_fail", KSTAT_DATA_UINT64 },
{ "dir_update_fail", KSTAT_DATA_UINT64 },
{ "dir_fini_purge", KSTAT_DATA_UINT64 },
{ "dir_reclaim_last", KSTAT_DATA_UINT64 },
{ "dir_reclaim_any", KSTAT_DATA_UINT64 },
};
static int doingcache = 1;
vnode_t negative_cache_vnode;
/*
* Insert entry at the front of the queue
*/
#define nc_inshash(ncp, hp) \
{ \
(ncp)->hash_next = (hp)->hash_next; \
(ncp)->hash_prev = (ncache_t *)(hp); \
(hp)->hash_next->hash_prev = (ncp); \
(hp)->hash_next = (ncp); \
}
/*
* Remove entry from hash queue
*/
#define nc_rmhash(ncp) \
{ \
(ncp)->hash_prev->hash_next = (ncp)->hash_next; \
(ncp)->hash_next->hash_prev = (ncp)->hash_prev; \
(ncp)->hash_prev = NULL; \
(ncp)->hash_next = NULL; \
}
/*
* Free an entry.
*/
#define dnlc_free(ncp) \
{ \
kmem_free((ncp), NCACHE_SIZE((ncp)->namlen)); \
atomic_dec_32(&dnlc_nentries); \
}
/*
* Cached directory info.
* ======================
*/
/*
* Cached directory free space hash function.
* Needs the free space handle and the dcp to get the hash table size
* Returns the hash index.
*/
#define DDFHASH(handle, dcp) ((handle >> 2) & (dcp)->dc_fhash_mask)
/*
* Cached directory name entry hash function.
* Uses the name and returns in the input arguments the hash and the name
* length.
*/
#define DNLC_DIR_HASH(name, hash, namelen) \
{ \
char Xc; \
const char *Xcp; \
hash = *name; \
for (Xcp = (name + 1); (Xc = *Xcp) != 0; Xcp++) \
hash = (hash << 4) + hash + Xc; \
ASSERT((Xcp - (name)) <= ((1 << NBBY) - 1)); \
namelen = Xcp - (name); \
}
/* special dircache_t pointer to indicate error should be returned */
/*
* The anchor directory cache pointer can contain 3 types of values,
* 1) NULL: No directory cache
* 2) DC_RET_LOW_MEM (-1): There was a directory cache that found to be
* too big or a memory shortage occurred. This value remains in the
* pointer until a dnlc_dir_start() which returns the a DNOMEM error.
* This is kludgy but efficient and only visible in this source file.
* 3) A valid cache pointer.
*/
#define DC_RET_LOW_MEM (dircache_t *)1
#define VALID_DIR_CACHE(dcp) ((dircache_t *)(dcp) > DC_RET_LOW_MEM)
/* Tunables */
uint_t dnlc_dir_enable = 1; /* disable caching directories by setting to 0 */
uint_t dnlc_dir_min_size = 40; /* min no of directory entries before caching */
uint_t dnlc_dir_max_size = UINT_MAX; /* ditto maximum */
uint_t dnlc_dir_hash_size_shift = 3; /* 8 entries per hash bucket */
uint_t dnlc_dir_min_reclaim = 350000; /* approx 1MB of dcentrys */
/*
* dnlc_dir_hash_resize_shift determines when the hash tables
* get re-adjusted due to growth or shrinkage
* - currently 2 indicating that there can be at most 4
* times or at least one quarter the number of entries
* before hash table readjustment. Note that with
* dnlc_dir_hash_size_shift above set at 3 this would
* mean readjustment would occur if the average number
* of entries went above 32 or below 2
*/
uint_t dnlc_dir_hash_resize_shift = 2; /* readjust rate */
static kmem_cache_t *dnlc_dir_space_cache; /* free space entry cache */
static dchead_t dc_head; /* anchor of cached directories */
/* Prototypes */
static ncache_t *dnlc_get(uchar_t namlen);
static ncache_t *dnlc_search(vnode_t *dp, const char *name, uchar_t namlen,
int hash);
static void dnlc_dir_reclaim(void *unused);
static void dnlc_dir_abort(dircache_t *dcp);
static void dnlc_dir_adjust_fhash(dircache_t *dcp);
static void dnlc_dir_adjust_nhash(dircache_t *dcp);
static void do_dnlc_reduce_cache(void *);
/*
* Initialize the directory cache.
*/
void
dnlc_init()
{
nc_hash_t *hp;
kstat_t *ksp;
int i;
/*
* Set up the size of the dnlc (ncsize) and its low water mark.
*/
if (ncsize == -1) {
/* calculate a reasonable size for the low water */
dnlc_nentries_low_water = 4 * (v.v_proc + maxusers) + 320;
ncsize = dnlc_nentries_low_water +
(dnlc_nentries_low_water / dnlc_low_water_divisor);
} else {
/* don't change the user specified ncsize */
dnlc_nentries_low_water =
ncsize - (ncsize / dnlc_low_water_divisor);
}
if (ncsize <= 0) {
doingcache = 0;
dnlc_dir_enable = 0; /* also disable directory caching */
ncsize = 0;
cmn_err(CE_NOTE, "name cache (dnlc) disabled");
return;
}
dnlc_max_nentries = ncsize * 2;
ncsize_onepercent = ncsize / 100;
ncsize_min_percent = ncsize_onepercent * 3;
/*
* Initialise the hash table.
* Compute hash size rounding to the next power of two.
*/
nc_hashsz = ncsize / nc_hashavelen;
nc_hashsz = 1 << highbit(nc_hashsz);
nc_hashmask = nc_hashsz - 1;
nc_hash = kmem_zalloc(nc_hashsz * sizeof (*nc_hash), KM_SLEEP);
for (i = 0; i < nc_hashsz; i++) {
hp = (nc_hash_t *)&nc_hash[i];
mutex_init(&hp->hash_lock, NULL, MUTEX_DEFAULT, NULL);
hp->hash_next = (ncache_t *)hp;
hp->hash_prev = (ncache_t *)hp;
}
/*
* Initialize rotors
*/
dnlc_free_rotor = dnlc_purge_fs1_rotor = &nc_hash[0];
/*
* Set up the directory caching to use kmem_cache_alloc
* for its free space entries so that we can get a callback
* when the system is short on memory, to allow us to free
* up some memory. we don't use the constructor/deconstructor
* functions.
*/
dnlc_dir_space_cache = kmem_cache_create("dnlc_space_cache",
sizeof (dcfree_t), 0, NULL, NULL, dnlc_dir_reclaim, NULL,
NULL, 0);
/*
* Initialise the head of the cached directory structures
*/
mutex_init(&dc_head.dch_lock, NULL, MUTEX_DEFAULT, NULL);
dc_head.dch_next = (dircache_t *)&dc_head;
dc_head.dch_prev = (dircache_t *)&dc_head;
/*
* Put a hold on the negative cache vnode so that it never goes away
* (VOP_INACTIVE isn't called on it).
*/
vn_reinit(&negative_cache_vnode);
/*
* Initialise kstats - both the old compatability raw kind and
* the more extensive named stats.
*/
ksp = kstat_create("unix", 0, "ncstats", "misc", KSTAT_TYPE_RAW,
sizeof (struct ncstats), KSTAT_FLAG_VIRTUAL);
if (ksp) {
ksp->ks_data = (void *) &ncstats;
kstat_install(ksp);
}
ksp = kstat_create("unix", 0, "dnlcstats", "misc", KSTAT_TYPE_NAMED,
sizeof (ncs) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
if (ksp) {
ksp->ks_data = (void *) &ncs;
kstat_install(ksp);
}
}
/*
* Add a name to the directory cache.
*/
void
dnlc_enter(vnode_t *dp, const char *name, vnode_t *vp)
{
ncache_t *ncp;
nc_hash_t *hp;
uchar_t namlen;
int hash;
TRACE_0(TR_FAC_NFS, TR_DNLC_ENTER_START, "dnlc_enter_start:");
if (!doingcache) {
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_enter_end:(%S) %d", "not caching", 0);
return;
}
/*
* Get a new dnlc entry. Assume the entry won't be in the cache
* and initialize it now
*/
DNLCHASH(name, dp, hash, namlen);
if ((ncp = dnlc_get(namlen)) == NULL)
return;
ncp->dp = dp;
VN_HOLD_DNLC(dp);
ncp->vp = vp;
VN_HOLD_DNLC(vp);
bcopy(name, ncp->name, namlen);
ncp->hash = hash;
hp = &nc_hash[hash & nc_hashmask];
mutex_enter(&hp->hash_lock);
if (dnlc_search(dp, name, namlen, hash) != NULL) {
mutex_exit(&hp->hash_lock);
ncstats.dbl_enters++;
ncs.ncs_dbl_enters.value.ui64++;
VN_RELE_DNLC(dp);
VN_RELE_DNLC(vp);
dnlc_free(ncp); /* crfree done here */
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_enter_end:(%S) %d", "dbl enter", ncstats.dbl_enters);
return;
}
/*
* Insert back into the hash chain.
*/
nc_inshash(ncp, hp);
mutex_exit(&hp->hash_lock);
ncstats.enters++;
ncs.ncs_enters.value.ui64++;
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_enter_end:(%S) %d", "done", ncstats.enters);
}
/*
* Add a name to the directory cache.
*
* This function is basically identical with
* dnlc_enter(). The difference is that when the
* desired dnlc entry is found, the vnode in the
* ncache is compared with the vnode passed in.
*
* If they are not equal then the ncache is
* updated with the passed in vnode. Otherwise
* it just frees up the newly allocated dnlc entry.
*/
void
dnlc_update(vnode_t *dp, const char *name, vnode_t *vp)
{
ncache_t *ncp;
ncache_t *tcp;
vnode_t *tvp;
nc_hash_t *hp;
int hash;
uchar_t namlen;
TRACE_0(TR_FAC_NFS, TR_DNLC_ENTER_START, "dnlc_update_start:");
if (!doingcache) {
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_update_end:(%S) %d", "not caching", 0);
return;
}
/*
* Get a new dnlc entry and initialize it now.
* If we fail to get a new entry, call dnlc_remove() to purge
* any existing dnlc entry including negative cache (DNLC_NO_VNODE)
* entry.
* Failure to clear an existing entry could result in false dnlc
* lookup (negative/stale entry).
*/
DNLCHASH(name, dp, hash, namlen);
if ((ncp = dnlc_get(namlen)) == NULL) {
dnlc_remove(dp, name);
return;
}
ncp->dp = dp;
VN_HOLD_DNLC(dp);
ncp->vp = vp;
VN_HOLD_DNLC(vp);
bcopy(name, ncp->name, namlen);
ncp->hash = hash;
hp = &nc_hash[hash & nc_hashmask];
mutex_enter(&hp->hash_lock);
if ((tcp = dnlc_search(dp, name, namlen, hash)) != NULL) {
if (tcp->vp != vp) {
tvp = tcp->vp;
tcp->vp = vp;
mutex_exit(&hp->hash_lock);
VN_RELE_DNLC(tvp);
ncstats.enters++;
ncs.ncs_enters.value.ui64++;
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_update_end:(%S) %d", "done", ncstats.enters);
} else {
mutex_exit(&hp->hash_lock);
VN_RELE_DNLC(vp);
ncstats.dbl_enters++;
ncs.ncs_dbl_enters.value.ui64++;
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_update_end:(%S) %d",
"dbl enter", ncstats.dbl_enters);
}
VN_RELE_DNLC(dp);
dnlc_free(ncp); /* crfree done here */
return;
}
/*
* insert the new entry, since it is not in dnlc yet
*/
nc_inshash(ncp, hp);
mutex_exit(&hp->hash_lock);
ncstats.enters++;
ncs.ncs_enters.value.ui64++;
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_update_end:(%S) %d", "done", ncstats.enters);
}
/*
* Look up a name in the directory name cache.
*
* Return a doubly-held vnode if found: one hold so that it may
* remain in the cache for other users, the other hold so that
* the cache is not re-cycled and the identity of the vnode is
* lost before the caller can use the vnode.
*/
vnode_t *
dnlc_lookup(vnode_t *dp, const char *name)
{
ncache_t *ncp;
nc_hash_t *hp;
vnode_t *vp;
int hash, depth;
uchar_t namlen;
TRACE_2(TR_FAC_NFS, TR_DNLC_LOOKUP_START,
"dnlc_lookup_start:dp %x name %s", dp, name);
if (!doingcache) {
TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
"dnlc_lookup_end:%S %d vp %x name %s",
"not_caching", 0, NULL, name);
return (NULL);
}
DNLCHASH(name, dp, hash, namlen);
depth = 1;
hp = &nc_hash[hash & nc_hashmask];
mutex_enter(&hp->hash_lock);
for (ncp = hp->hash_next; ncp != (ncache_t *)hp;
ncp = ncp->hash_next) {
if (ncp->hash == hash && /* fast signature check */
ncp->dp == dp &&
ncp->namlen == namlen &&
bcmp(ncp->name, name, namlen) == 0) {
/*
* Move this entry to the head of its hash chain
* if it's not already close.
*/
if (depth > NC_MOVETOFRONT) {
ncache_t *next = ncp->hash_next;
ncache_t *prev = ncp->hash_prev;
prev->hash_next = next;
next->hash_prev = prev;
ncp->hash_next = next = hp->hash_next;
ncp->hash_prev = (ncache_t *)hp;
next->hash_prev = ncp;
hp->hash_next = ncp;
ncstats.move_to_front++;
}
/*
* Put a hold on the vnode now so its identity
* can't change before the caller has a chance to
* put a hold on it.
*/
vp = ncp->vp;
VN_HOLD_CALLER(vp);
mutex_exit(&hp->hash_lock);
ncstats.hits++;
ncs.ncs_hits.value.ui64++;
if (vp == DNLC_NO_VNODE) {
ncs.ncs_neg_hits.value.ui64++;
}
TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
"dnlc_lookup_end:%S %d vp %x name %s", "hit",
ncstats.hits, vp, name);
return (vp);
}
depth++;
}
mutex_exit(&hp->hash_lock);
ncstats.misses++;
ncs.ncs_misses.value.ui64++;
TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
"dnlc_lookup_end:%S %d vp %x name %s", "miss", ncstats.misses,
NULL, name);
return (NULL);
}
/*
* Remove an entry in the directory name cache.
*/
void
dnlc_remove(vnode_t *dp, const char *name)
{
ncache_t *ncp;
nc_hash_t *hp;
uchar_t namlen;
int hash;
if (!doingcache)
return;
DNLCHASH(name, dp, hash, namlen);
hp = &nc_hash[hash & nc_hashmask];
mutex_enter(&hp->hash_lock);
if (ncp = dnlc_search(dp, name, namlen, hash)) {
/*
* Free up the entry
*/
nc_rmhash(ncp);
mutex_exit(&hp->hash_lock);
VN_RELE_DNLC(ncp->vp);
VN_RELE_DNLC(ncp->dp);
dnlc_free(ncp);
return;
}
mutex_exit(&hp->hash_lock);
}
/*
* Purge the entire cache.
*/
void
dnlc_purge()
{
nc_hash_t *nch;
ncache_t *ncp;
int index;
int i;
vnode_t *nc_rele[DNLC_MAX_RELE];
if (!doingcache)
return;
ncstats.purges++;
ncs.ncs_purge_all.value.ui64++;
for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
index = 0;
mutex_enter(&nch->hash_lock);
ncp = nch->hash_next;
while (ncp != (ncache_t *)nch) {
ncache_t *np;
np = ncp->hash_next;
nc_rele[index++] = ncp->vp;
nc_rele[index++] = ncp->dp;
nc_rmhash(ncp);
dnlc_free(ncp);
ncp = np;
ncs.ncs_purge_total.value.ui64++;
if (index == DNLC_MAX_RELE)
break;
}
mutex_exit(&nch->hash_lock);
/* Release holds on all the vnodes now that we have no locks */
for (i = 0; i < index; i++) {
VN_RELE_DNLC(nc_rele[i]);
}
if (ncp != (ncache_t *)nch) {
nch--; /* Do current hash chain again */
}
}
}
/*
* Purge any cache entries referencing a vnode. Exit as soon as the dnlc
* reference count goes to zero (the caller still holds a reference).
*/
void
dnlc_purge_vp(vnode_t *vp)
{
nc_hash_t *nch;
ncache_t *ncp;
int index;
vnode_t *nc_rele[DNLC_MAX_RELE];
ASSERT(vp->v_count > 0);
if (vp->v_count_dnlc == 0) {
return;
}
if (!doingcache)
return;
ncstats.purges++;
ncs.ncs_purge_vp.value.ui64++;
for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
index = 0;
mutex_enter(&nch->hash_lock);
ncp = nch->hash_next;
while (ncp != (ncache_t *)nch) {
ncache_t *np;
np = ncp->hash_next;
if (ncp->dp == vp || ncp->vp == vp) {
nc_rele[index++] = ncp->vp;
nc_rele[index++] = ncp->dp;
nc_rmhash(ncp);
dnlc_free(ncp);
ncs.ncs_purge_total.value.ui64++;
if (index == DNLC_MAX_RELE) {
ncp = np;
break;
}
}
ncp = np;
}
mutex_exit(&nch->hash_lock);
/* Release holds on all the vnodes now that we have no locks */
while (index) {
VN_RELE_DNLC(nc_rele[--index]);
}
if (vp->v_count_dnlc == 0) {
return;
}
if (ncp != (ncache_t *)nch) {
nch--; /* Do current hash chain again */
}
}
}
/*
* Purge cache entries referencing a vfsp. Caller supplies a count
* of entries to purge; up to that many will be freed. A count of
* zero indicates that all such entries should be purged. Returns
* the number of entries that were purged.
*/
int
dnlc_purge_vfsp(vfs_t *vfsp, int count)
{
nc_hash_t *nch;
ncache_t *ncp;
int n = 0;
int index;
int i;
vnode_t *nc_rele[DNLC_MAX_RELE];
if (!doingcache)
return (0);
ncstats.purges++;
ncs.ncs_purge_vfs.value.ui64++;
for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
index = 0;
mutex_enter(&nch->hash_lock);
ncp = nch->hash_next;
while (ncp != (ncache_t *)nch) {
ncache_t *np;
np = ncp->hash_next;
ASSERT(ncp->dp != NULL);
ASSERT(ncp->vp != NULL);
if ((ncp->dp->v_vfsp == vfsp) ||
(ncp->vp->v_vfsp == vfsp)) {
n++;
nc_rele[index++] = ncp->vp;
nc_rele[index++] = ncp->dp;
nc_rmhash(ncp);
dnlc_free(ncp);
ncs.ncs_purge_total.value.ui64++;
if (index == DNLC_MAX_RELE) {
ncp = np;
break;
}
if (count != 0 && n >= count) {
break;
}
}
ncp = np;
}
mutex_exit(&nch->hash_lock);
/* Release holds on all the vnodes now that we have no locks */
for (i = 0; i < index; i++) {
VN_RELE_DNLC(nc_rele[i]);
}
if (count != 0 && n >= count) {
return (n);
}
if (ncp != (ncache_t *)nch) {
nch--; /* Do current hash chain again */
}
}
return (n);
}
/*
* Purge 1 entry from the dnlc that is part of the filesystem(s)
* represented by 'vop'. The purpose of this routine is to allow
* users of the dnlc to free a vnode that is being held by the dnlc.
*
* If we find a vnode that we release which will result in
* freeing the underlying vnode (count was 1), return 1, 0
* if no appropriate vnodes found.
*
* Note, vop is not the 'right' identifier for a filesystem.
*/
int
dnlc_fs_purge1(vnodeops_t *vop)
{
nc_hash_t *end;
nc_hash_t *hp;
ncache_t *ncp;
vnode_t *vp;
if (!doingcache)
return (0);
ncs.ncs_purge_fs1.value.ui64++;
/*
* Scan the dnlc entries looking for a likely candidate.
*/
hp = end = dnlc_purge_fs1_rotor;
do {
if (++hp == &nc_hash[nc_hashsz])
hp = nc_hash;
dnlc_purge_fs1_rotor = hp;
if (hp->hash_next == (ncache_t *)hp)
continue;
mutex_enter(&hp->hash_lock);
for (ncp = hp->hash_prev;
ncp != (ncache_t *)hp;
ncp = ncp->hash_prev) {
vp = ncp->vp;
if (!vn_has_cached_data(vp) && (vp->v_count == 1) &&
vn_matchops(vp, vop))
break;
}
if (ncp != (ncache_t *)hp) {
nc_rmhash(ncp);
mutex_exit(&hp->hash_lock);
VN_RELE_DNLC(ncp->dp);
VN_RELE_DNLC(vp)
dnlc_free(ncp);
ncs.ncs_purge_total.value.ui64++;
return (1);
}
mutex_exit(&hp->hash_lock);
} while (hp != end);
return (0);
}
/*
* Utility routine to search for a cache entry. Return the
* ncache entry if found, NULL otherwise.
*/
static ncache_t *
dnlc_search(vnode_t *dp, const char *name, uchar_t namlen, int hash)
{
nc_hash_t *hp;
ncache_t *ncp;
hp = &nc_hash[hash & nc_hashmask];
for (ncp = hp->hash_next; ncp != (ncache_t *)hp; ncp = ncp->hash_next) {
if (ncp->hash == hash &&
ncp->dp == dp &&
ncp->namlen == namlen &&
bcmp(ncp->name, name, namlen) == 0)
return (ncp);
}
return (NULL);
}
#if ((1 << NBBY) - 1) < (MAXNAMELEN - 1)
#error ncache_t name length representation is too small
#endif
void
dnlc_reduce_cache(void *reduce_percent)
{
if (dnlc_reduce_idle && (dnlc_nentries >= ncsize || reduce_percent)) {
dnlc_reduce_idle = 0;
if ((taskq_dispatch(system_taskq, do_dnlc_reduce_cache,
reduce_percent, TQ_NOSLEEP)) == TASKQID_INVALID)
dnlc_reduce_idle = 1;
}
}
/*
* Get a new name cache entry.
* If the dnlc_reduce_cache() taskq isn't keeping up with demand, or memory
* is short then just return NULL. If we're over ncsize then kick off a
* thread to free some in use entries down to dnlc_nentries_low_water.
* Caller must initialise all fields except namlen.
* Component names are defined to be less than MAXNAMELEN
* which includes a null.
*/
static ncache_t *
dnlc_get(uchar_t namlen)
{
ncache_t *ncp;
if (dnlc_nentries > dnlc_max_nentries) {
dnlc_max_nentries_cnt++; /* keep a statistic */
return (NULL);
}
ncp = kmem_alloc(NCACHE_SIZE(namlen), KM_NOSLEEP);
if (ncp == NULL) {
return (NULL);
}
ncp->namlen = namlen;
atomic_inc_32(&dnlc_nentries);
dnlc_reduce_cache(NULL);
return (ncp);
}
/*
* Taskq routine to free up name cache entries to reduce the
* cache size to the low water mark if "reduce_percent" is not provided.
* If "reduce_percent" is provided, reduce cache size by
* (ncsize_onepercent * reduce_percent).
*/
/*ARGSUSED*/
static void
do_dnlc_reduce_cache(void *reduce_percent)
{
nc_hash_t *hp = dnlc_free_rotor, *start_hp = hp;
vnode_t *vp;
ncache_t *ncp;
int cnt;
uint_t low_water = dnlc_nentries_low_water;
if (reduce_percent) {
uint_t reduce_cnt;
/*
* Never try to reduce the current number
* of cache entries below 3% of ncsize.
*/
if (dnlc_nentries <= ncsize_min_percent) {
dnlc_reduce_idle = 1;
return;
}
reduce_cnt = ncsize_onepercent *
(uint_t)(uintptr_t)reduce_percent;
if (reduce_cnt > dnlc_nentries ||
dnlc_nentries - reduce_cnt < ncsize_min_percent)
low_water = ncsize_min_percent;
else
low_water = dnlc_nentries - reduce_cnt;
}
do {
/*
* Find the first non empty hash queue without locking.
* Only look at each hash queue once to avoid an infinite loop.
*/
do {
if (++hp == &nc_hash[nc_hashsz])
hp = nc_hash;
} while (hp->hash_next == (ncache_t *)hp && hp != start_hp);
/* return if all hash queues are empty. */
if (hp->hash_next == (ncache_t *)hp) {
dnlc_reduce_idle = 1;
return;
}
mutex_enter(&hp->hash_lock);
for (cnt = 0, ncp = hp->hash_prev; ncp != (ncache_t *)hp;
ncp = ncp->hash_prev, cnt++) {
vp = ncp->vp;
/*
* A name cache entry with a reference count
* of one is only referenced by the dnlc.
* Also negative cache entries are purged first.
*/
if (!vn_has_cached_data(vp) &&
((vp->v_count == 1) || (vp == DNLC_NO_VNODE))) {
ncs.ncs_pick_heur.value.ui64++;
goto found;
}
/*
* Remove from the end of the chain if the
* chain is too long
*/
if (cnt > dnlc_long_chain) {
ncp = hp->hash_prev;
ncs.ncs_pick_last.value.ui64++;
vp = ncp->vp;
goto found;
}
}
/* check for race and continue */
if (hp->hash_next == (ncache_t *)hp) {
mutex_exit(&hp->hash_lock);
continue;
}
ncp = hp->hash_prev; /* pick the last one in the hash queue */
ncs.ncs_pick_last.value.ui64++;
vp = ncp->vp;
found:
/*
* Remove from hash chain.
*/
nc_rmhash(ncp);
mutex_exit(&hp->hash_lock);
VN_RELE_DNLC(vp);
VN_RELE_DNLC(ncp->dp);
dnlc_free(ncp);
} while (dnlc_nentries > low_water);
dnlc_free_rotor = hp;
dnlc_reduce_idle = 1;
}
/*
* Directory caching routines
* ==========================
*
* See dnlc.h for details of the interfaces below.
*/
/*
* Lookup up an entry in a complete or partial directory cache.
*/
dcret_t
dnlc_dir_lookup(dcanchor_t *dcap, const char *name, uint64_t *handle)
{
dircache_t *dcp;
dcentry_t *dep;
int hash;
int ret;
uchar_t namlen;
/*
* can test without lock as we are only a cache
*/
if (!VALID_DIR_CACHE(dcap->dca_dircache)) {
ncs.ncs_dir_misses.value.ui64++;
return (DNOCACHE);
}
if (!dnlc_dir_enable) {
return (DNOCACHE);
}
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_actime = ddi_get_lbolt64();
DNLC_DIR_HASH(name, hash, namlen);
dep = dcp->dc_namehash[hash & dcp->dc_nhash_mask];
while (dep != NULL) {
if ((dep->de_hash == hash) &&
(namlen == dep->de_namelen) &&
bcmp(dep->de_name, name, namlen) == 0) {
*handle = dep->de_handle;
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_hits.value.ui64++;
return (DFOUND);
}
dep = dep->de_next;
}
if (dcp->dc_complete) {
ret = DNOENT;
} else {
ret = DNOCACHE;
}
mutex_exit(&dcap->dca_lock);
return (ret);
} else {
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_misses.value.ui64++;
return (DNOCACHE);
}
}
/*
* Start a new directory cache. An estimate of the number of
* entries is provided to as a quick check to ensure the directory
* is cacheable.
*/
dcret_t
dnlc_dir_start(dcanchor_t *dcap, uint_t num_entries)
{
dircache_t *dcp;
if (!dnlc_dir_enable ||
(num_entries < dnlc_dir_min_size)) {
return (DNOCACHE);
}
if (num_entries > dnlc_dir_max_size) {
return (DTOOBIG);
}
mutex_enter(&dc_head.dch_lock);
mutex_enter(&dcap->dca_lock);
if (dcap->dca_dircache == DC_RET_LOW_MEM) {
dcap->dca_dircache = NULL;
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
return (DNOMEM);
}
/*
* Check if there's currently a cache.
* This probably only occurs on a race.
*/
if (dcap->dca_dircache != NULL) {
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
return (DNOCACHE);
}
/*
* Allocate the dircache struct, entry and free space hash tables.
* These tables are initially just one entry but dynamically resize
* when entries and free space are added or removed.
*/
if ((dcp = kmem_zalloc(sizeof (dircache_t), KM_NOSLEEP)) == NULL) {
goto error;
}
if ((dcp->dc_namehash = kmem_zalloc(sizeof (dcentry_t *),
KM_NOSLEEP)) == NULL) {
goto error;
}
if ((dcp->dc_freehash = kmem_zalloc(sizeof (dcfree_t *),
KM_NOSLEEP)) == NULL) {
goto error;
}
dcp->dc_anchor = dcap; /* set back pointer to anchor */
dcap->dca_dircache = dcp;
/* add into head of global chain */
dcp->dc_next = dc_head.dch_next;
dcp->dc_prev = (dircache_t *)&dc_head;
dcp->dc_next->dc_prev = dcp;
dc_head.dch_next = dcp;
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
ncs.ncs_cur_dirs.value.ui64++;
ncs.ncs_dirs_cached.value.ui64++;
return (DOK);
error:
if (dcp != NULL) {
if (dcp->dc_namehash) {
kmem_free(dcp->dc_namehash, sizeof (dcentry_t *));
}
kmem_free(dcp, sizeof (dircache_t));
}
/*
* Must also kmem_free dcp->dc_freehash if more error cases are added
*/
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
ncs.ncs_dir_start_nm.value.ui64++;
return (DNOCACHE);
}
/*
* Add a directopry entry to a partial or complete directory cache.
*/
dcret_t
dnlc_dir_add_entry(dcanchor_t *dcap, const char *name, uint64_t handle)
{
dircache_t *dcp;
dcentry_t **hp, *dep;
int hash;
uint_t capacity;
uchar_t namlen;
/*
* Allocate the dcentry struct, including the variable
* size name. Note, the null terminator is not copied.
*
* We do this outside the lock to avoid possible deadlock if
* dnlc_dir_reclaim() is called as a result of memory shortage.
*/
DNLC_DIR_HASH(name, hash, namlen);
dep = kmem_alloc(DCENTTRY_SIZE(namlen), KM_NOSLEEP);
if (dep == NULL) {
#ifdef DEBUG
/*
* The kmem allocator generates random failures for
* KM_NOSLEEP calls (see KMEM_RANDOM_ALLOCATION_FAILURE)
* So try again before we blow away a perfectly good cache.
* This is done not to cover an error but purely for
* performance running a debug kernel.
* This random error only occurs in debug mode.
*/
dep = kmem_alloc(DCENTTRY_SIZE(namlen), KM_NOSLEEP);
if (dep != NULL)
goto ok;
#endif
ncs.ncs_dir_add_nm.value.ui64++;
/*
* Free a directory cache. This may be the one we are
* called with.
*/
dnlc_dir_reclaim(NULL);
dep = kmem_alloc(DCENTTRY_SIZE(namlen), KM_NOSLEEP);
if (dep == NULL) {
/*
* still no memory, better delete this cache
*/
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dnlc_dir_abort(dcp);
dcap->dca_dircache = DC_RET_LOW_MEM;
}
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_addabort.value.ui64++;
return (DNOCACHE);
}
/*
* fall through as if the 1st kmem_alloc had worked
*/
}
#ifdef DEBUG
ok:
#endif
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
/*
* If the total number of entries goes above the max
* then free this cache
*/
if ((dcp->dc_num_entries + dcp->dc_num_free) >
dnlc_dir_max_size) {
mutex_exit(&dcap->dca_lock);
dnlc_dir_purge(dcap);
kmem_free(dep, DCENTTRY_SIZE(namlen));
ncs.ncs_dir_add_max.value.ui64++;
return (DTOOBIG);
}
dcp->dc_num_entries++;
capacity = (dcp->dc_nhash_mask + 1) << dnlc_dir_hash_size_shift;
if (dcp->dc_num_entries >=
(capacity << dnlc_dir_hash_resize_shift)) {
dnlc_dir_adjust_nhash(dcp);
}
hp = &dcp->dc_namehash[hash & dcp->dc_nhash_mask];
/*
* Initialise and chain in new entry
*/
dep->de_handle = handle;
dep->de_hash = hash;
/*
* Note de_namelen is a uchar_t to conserve space
* and alignment padding. The max length of any
* pathname component is defined as MAXNAMELEN
* which is 256 (including the terminating null).
* So provided this doesn't change, we don't include the null,
* we always use bcmp to compare strings, and we don't
* start storing full names, then we are ok.
* The space savings is worth it.
*/
dep->de_namelen = namlen;
bcopy(name, dep->de_name, namlen);
dep->de_next = *hp;
*hp = dep;
dcp->dc_actime = ddi_get_lbolt64();
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_num_ents.value.ui64++;
return (DOK);
} else {
mutex_exit(&dcap->dca_lock);
kmem_free(dep, DCENTTRY_SIZE(namlen));
return (DNOCACHE);
}
}
/*
* Add free space to a partial or complete directory cache.
*/
dcret_t
dnlc_dir_add_space(dcanchor_t *dcap, uint_t len, uint64_t handle)
{
dircache_t *dcp;
dcfree_t *dfp, **hp;
uint_t capacity;
/*
* We kmem_alloc outside the lock to avoid possible deadlock if
* dnlc_dir_reclaim() is called as a result of memory shortage.
*/
dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
if (dfp == NULL) {
#ifdef DEBUG
/*
* The kmem allocator generates random failures for
* KM_NOSLEEP calls (see KMEM_RANDOM_ALLOCATION_FAILURE)
* So try again before we blow away a perfectly good cache.
* This random error only occurs in debug mode
*/
dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
if (dfp != NULL)
goto ok;
#endif
ncs.ncs_dir_add_nm.value.ui64++;
/*
* Free a directory cache. This may be the one we are
* called with.
*/
dnlc_dir_reclaim(NULL);
dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
if (dfp == NULL) {
/*
* still no memory, better delete this cache
*/
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dnlc_dir_abort(dcp);
dcap->dca_dircache = DC_RET_LOW_MEM;
}
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_addabort.value.ui64++;
return (DNOCACHE);
}
/*
* fall through as if the 1st kmem_alloc had worked
*/
}
#ifdef DEBUG
ok:
#endif
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
if ((dcp->dc_num_entries + dcp->dc_num_free) >
dnlc_dir_max_size) {
mutex_exit(&dcap->dca_lock);
dnlc_dir_purge(dcap);
kmem_cache_free(dnlc_dir_space_cache, dfp);
ncs.ncs_dir_add_max.value.ui64++;
return (DTOOBIG);
}
dcp->dc_num_free++;
capacity = (dcp->dc_fhash_mask + 1) << dnlc_dir_hash_size_shift;
if (dcp->dc_num_free >=
(capacity << dnlc_dir_hash_resize_shift)) {
dnlc_dir_adjust_fhash(dcp);
}
/*
* Initialise and chain a new entry
*/
dfp->df_handle = handle;
dfp->df_len = len;
dcp->dc_actime = ddi_get_lbolt64();
hp = &(dcp->dc_freehash[DDFHASH(handle, dcp)]);
dfp->df_next = *hp;
*hp = dfp;
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_num_ents.value.ui64++;
return (DOK);
} else {
mutex_exit(&dcap->dca_lock);
kmem_cache_free(dnlc_dir_space_cache, dfp);
return (DNOCACHE);
}
}
/*
* Mark a directory cache as complete.
*/
void
dnlc_dir_complete(dcanchor_t *dcap)
{
dircache_t *dcp;
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_complete = B_TRUE;
}
mutex_exit(&dcap->dca_lock);
}
/*
* Internal routine to delete a partial or full directory cache.
* No additional locking needed.
*/
static void
dnlc_dir_abort(dircache_t *dcp)
{
dcentry_t *dep, *nhp;
dcfree_t *fep, *fhp;
uint_t nhtsize = dcp->dc_nhash_mask + 1; /* name hash table size */
uint_t fhtsize = dcp->dc_fhash_mask + 1; /* free hash table size */
uint_t i;
/*
* Free up the cached name entries and hash table
*/
for (i = 0; i < nhtsize; i++) { /* for each hash bucket */
nhp = dcp->dc_namehash[i];
while (nhp != NULL) { /* for each chained entry */
dep = nhp->de_next;
kmem_free(nhp, DCENTTRY_SIZE(nhp->de_namelen));
nhp = dep;
}
}
kmem_free(dcp->dc_namehash, sizeof (dcentry_t *) * nhtsize);
/*
* Free up the free space entries and hash table
*/
for (i = 0; i < fhtsize; i++) { /* for each hash bucket */
fhp = dcp->dc_freehash[i];
while (fhp != NULL) { /* for each chained entry */
fep = fhp->df_next;
kmem_cache_free(dnlc_dir_space_cache, fhp);
fhp = fep;
}
}
kmem_free(dcp->dc_freehash, sizeof (dcfree_t *) * fhtsize);
/*
* Finally free the directory cache structure itself
*/
ncs.ncs_dir_num_ents.value.ui64 -= (dcp->dc_num_entries +
dcp->dc_num_free);
kmem_free(dcp, sizeof (dircache_t));
ncs.ncs_cur_dirs.value.ui64--;
}
/*
* Remove a partial or complete directory cache
*/
void
dnlc_dir_purge(dcanchor_t *dcap)
{
dircache_t *dcp;
mutex_enter(&dc_head.dch_lock);
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (!VALID_DIR_CACHE(dcp)) {
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
return;
}
dcap->dca_dircache = NULL;
/*
* Unchain from global list
*/
dcp->dc_prev->dc_next = dcp->dc_next;
dcp->dc_next->dc_prev = dcp->dc_prev;
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
dnlc_dir_abort(dcp);
}
/*
* Remove an entry from a complete or partial directory cache.
* Return the handle if it's non null.
*/
dcret_t
dnlc_dir_rem_entry(dcanchor_t *dcap, const char *name, uint64_t *handlep)
{
dircache_t *dcp;
dcentry_t **prevpp, *te;
uint_t capacity;
int hash;
int ret;
uchar_t namlen;
if (!dnlc_dir_enable) {
return (DNOCACHE);
}
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_actime = ddi_get_lbolt64();
if (dcp->dc_nhash_mask > 0) { /* ie not minimum */
capacity = (dcp->dc_nhash_mask + 1) <<
dnlc_dir_hash_size_shift;
if (dcp->dc_num_entries <=
(capacity >> dnlc_dir_hash_resize_shift)) {
dnlc_dir_adjust_nhash(dcp);
}
}
DNLC_DIR_HASH(name, hash, namlen);
prevpp = &dcp->dc_namehash[hash & dcp->dc_nhash_mask];
while (*prevpp != NULL) {
if (((*prevpp)->de_hash == hash) &&
(namlen == (*prevpp)->de_namelen) &&
bcmp((*prevpp)->de_name, name, namlen) == 0) {
if (handlep != NULL) {
*handlep = (*prevpp)->de_handle;
}
te = *prevpp;
*prevpp = (*prevpp)->de_next;
kmem_free(te, DCENTTRY_SIZE(te->de_namelen));
/*
* If the total number of entries
* falls below half the minimum number
* of entries then free this cache.
*/
if (--dcp->dc_num_entries <
(dnlc_dir_min_size >> 1)) {
mutex_exit(&dcap->dca_lock);
dnlc_dir_purge(dcap);
} else {
mutex_exit(&dcap->dca_lock);
}
ncs.ncs_dir_num_ents.value.ui64--;
return (DFOUND);
}
prevpp = &((*prevpp)->de_next);
}
if (dcp->dc_complete) {
ncs.ncs_dir_reme_fai.value.ui64++;
ret = DNOENT;
} else {
ret = DNOCACHE;
}
mutex_exit(&dcap->dca_lock);
return (ret);
} else {
mutex_exit(&dcap->dca_lock);
return (DNOCACHE);
}
}
/*
* Remove free space of at least the given length from a complete
* or partial directory cache.
*/
dcret_t
dnlc_dir_rem_space_by_len(dcanchor_t *dcap, uint_t len, uint64_t *handlep)
{
dircache_t *dcp;
dcfree_t **prevpp, *tfp;
uint_t fhtsize; /* free hash table size */
uint_t i;
uint_t capacity;
int ret;
if (!dnlc_dir_enable) {
return (DNOCACHE);
}
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_actime = ddi_get_lbolt64();
if (dcp->dc_fhash_mask > 0) { /* ie not minimum */
capacity = (dcp->dc_fhash_mask + 1) <<
dnlc_dir_hash_size_shift;
if (dcp->dc_num_free <=
(capacity >> dnlc_dir_hash_resize_shift)) {
dnlc_dir_adjust_fhash(dcp);
}
}
/*
* Search for an entry of the appropriate size
* on a first fit basis.
*/
fhtsize = dcp->dc_fhash_mask + 1;
for (i = 0; i < fhtsize; i++) { /* for each hash bucket */
prevpp = &(dcp->dc_freehash[i]);
while (*prevpp != NULL) {
if ((*prevpp)->df_len >= len) {
*handlep = (*prevpp)->df_handle;
tfp = *prevpp;
*prevpp = (*prevpp)->df_next;
dcp->dc_num_free--;
mutex_exit(&dcap->dca_lock);
kmem_cache_free(dnlc_dir_space_cache,
tfp);
ncs.ncs_dir_num_ents.value.ui64--;
return (DFOUND);
}
prevpp = &((*prevpp)->df_next);
}
}
if (dcp->dc_complete) {
ret = DNOENT;
} else {
ret = DNOCACHE;
}
mutex_exit(&dcap->dca_lock);
return (ret);
} else {
mutex_exit(&dcap->dca_lock);
return (DNOCACHE);
}
}
/*
* Remove free space with the given handle from a complete or partial
* directory cache.
*/
dcret_t
dnlc_dir_rem_space_by_handle(dcanchor_t *dcap, uint64_t handle)
{
dircache_t *dcp;
dcfree_t **prevpp, *tfp;
uint_t capacity;
int ret;
if (!dnlc_dir_enable) {
return (DNOCACHE);
}
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_actime = ddi_get_lbolt64();
if (dcp->dc_fhash_mask > 0) { /* ie not minimum */
capacity = (dcp->dc_fhash_mask + 1) <<
dnlc_dir_hash_size_shift;
if (dcp->dc_num_free <=
(capacity >> dnlc_dir_hash_resize_shift)) {
dnlc_dir_adjust_fhash(dcp);
}
}
/*
* search for the exact entry
*/
prevpp = &(dcp->dc_freehash[DDFHASH(handle, dcp)]);
while (*prevpp != NULL) {
if ((*prevpp)->df_handle == handle) {
tfp = *prevpp;
*prevpp = (*prevpp)->df_next;
dcp->dc_num_free--;
mutex_exit(&dcap->dca_lock);
kmem_cache_free(dnlc_dir_space_cache, tfp);
ncs.ncs_dir_num_ents.value.ui64--;
return (DFOUND);
}
prevpp = &((*prevpp)->df_next);
}
if (dcp->dc_complete) {
ncs.ncs_dir_rems_fai.value.ui64++;
ret = DNOENT;
} else {
ret = DNOCACHE;
}
mutex_exit(&dcap->dca_lock);
return (ret);
} else {
mutex_exit(&dcap->dca_lock);
return (DNOCACHE);
}
}
/*
* Update the handle of an directory cache entry.
*/
dcret_t
dnlc_dir_update(dcanchor_t *dcap, const char *name, uint64_t handle)
{
dircache_t *dcp;
dcentry_t *dep;
int hash;
int ret;
uchar_t namlen;
if (!dnlc_dir_enable) {
return (DNOCACHE);
}
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_actime = ddi_get_lbolt64();
DNLC_DIR_HASH(name, hash, namlen);
dep = dcp->dc_namehash[hash & dcp->dc_nhash_mask];
while (dep != NULL) {
if ((dep->de_hash == hash) &&
(namlen == dep->de_namelen) &&
bcmp(dep->de_name, name, namlen) == 0) {
dep->de_handle = handle;
mutex_exit(&dcap->dca_lock);
return (DFOUND);
}
dep = dep->de_next;
}
if (dcp->dc_complete) {
ncs.ncs_dir_upd_fail.value.ui64++;
ret = DNOENT;
} else {
ret = DNOCACHE;
}
mutex_exit(&dcap->dca_lock);
return (ret);
} else {
mutex_exit(&dcap->dca_lock);
return (DNOCACHE);
}
}
void
dnlc_dir_fini(dcanchor_t *dcap)
{
dircache_t *dcp;
mutex_enter(&dc_head.dch_lock);
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
/*
* Unchain from global list
*/
ncs.ncs_dir_finipurg.value.ui64++;
dcp->dc_prev->dc_next = dcp->dc_next;
dcp->dc_next->dc_prev = dcp->dc_prev;
} else {
dcp = NULL;
}
dcap->dca_dircache = NULL;
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
mutex_destroy(&dcap->dca_lock);
if (dcp) {
dnlc_dir_abort(dcp);
}
}
/*
* Reclaim callback for dnlc directory caching.
* Invoked by the kernel memory allocator when memory gets tight.
* This is a pretty serious condition and can lead easily lead to system
* hangs if not enough space is returned.
*
* Deciding which directory (or directories) to purge is tricky.
* Purging everything is an overkill, but purging just the oldest used
* was found to lead to hangs. The largest cached directories use the
* most memory, but take the most effort to rebuild, whereas the smaller
* ones have little value and give back little space. So what to do?
*
* The current policy is to continue purging the oldest used directories
* until at least dnlc_dir_min_reclaim directory entries have been purged.
*/
/*ARGSUSED*/
static void
dnlc_dir_reclaim(void *unused)
{
dircache_t *dcp, *oldest;
uint_t dirent_cnt = 0;
mutex_enter(&dc_head.dch_lock);
while (dirent_cnt < dnlc_dir_min_reclaim) {
dcp = dc_head.dch_next;
oldest = NULL;
while (dcp != (dircache_t *)&dc_head) {
if (oldest == NULL) {
oldest = dcp;
} else {
if (dcp->dc_actime < oldest->dc_actime) {
oldest = dcp;
}
}
dcp = dcp->dc_next;
}
if (oldest == NULL) {
/* nothing to delete */
mutex_exit(&dc_head.dch_lock);
return;
}
/*
* remove from directory chain and purge
*/
oldest->dc_prev->dc_next = oldest->dc_next;
oldest->dc_next->dc_prev = oldest->dc_prev;
mutex_enter(&oldest->dc_anchor->dca_lock);
/*
* If this was the last entry then it must be too large.
* Mark it as such by saving a special dircache_t
* pointer (DC_RET_LOW_MEM) in the anchor. The error DNOMEM
* will be presented to the caller of dnlc_dir_start()
*/
if (oldest->dc_next == oldest->dc_prev) {
oldest->dc_anchor->dca_dircache = DC_RET_LOW_MEM;
ncs.ncs_dir_rec_last.value.ui64++;
} else {
oldest->dc_anchor->dca_dircache = NULL;
ncs.ncs_dir_recl_any.value.ui64++;
}
mutex_exit(&oldest->dc_anchor->dca_lock);
dirent_cnt += oldest->dc_num_entries;
dnlc_dir_abort(oldest);
}
mutex_exit(&dc_head.dch_lock);
}
/*
* Dynamically grow or shrink the size of the name hash table
*/
static void
dnlc_dir_adjust_nhash(dircache_t *dcp)
{
dcentry_t **newhash, *dep, **nhp, *tep;
uint_t newsize;
uint_t oldsize;
uint_t newsizemask;
int i;
/*
* Allocate new hash table
*/
newsize = dcp->dc_num_entries >> dnlc_dir_hash_size_shift;
newhash = kmem_zalloc(sizeof (dcentry_t *) * newsize, KM_NOSLEEP);
if (newhash == NULL) {
/*
* System is short on memory just return
* Note, the old hash table is still usable.
* This return is unlikely to repeatedy occur, because
* either some other directory caches will be reclaimed
* due to memory shortage, thus freeing memory, or this
* directory cahe will be reclaimed.
*/
return;
}
oldsize = dcp->dc_nhash_mask + 1;
dcp->dc_nhash_mask = newsizemask = newsize - 1;
/*
* Move entries from the old table to the new
*/
for (i = 0; i < oldsize; i++) { /* for each hash bucket */
dep = dcp->dc_namehash[i];
while (dep != NULL) { /* for each chained entry */
tep = dep;
dep = dep->de_next;
nhp = &newhash[tep->de_hash & newsizemask];
tep->de_next = *nhp;
*nhp = tep;
}
}
/*
* delete old hash table and set new one in place
*/
kmem_free(dcp->dc_namehash, sizeof (dcentry_t *) * oldsize);
dcp->dc_namehash = newhash;
}
/*
* Dynamically grow or shrink the size of the free space hash table
*/
static void
dnlc_dir_adjust_fhash(dircache_t *dcp)
{
dcfree_t **newhash, *dfp, **nhp, *tfp;
uint_t newsize;
uint_t oldsize;
int i;
/*
* Allocate new hash table
*/
newsize = dcp->dc_num_free >> dnlc_dir_hash_size_shift;
newhash = kmem_zalloc(sizeof (dcfree_t *) * newsize, KM_NOSLEEP);
if (newhash == NULL) {
/*
* System is short on memory just return
* Note, the old hash table is still usable.
* This return is unlikely to repeatedy occur, because
* either some other directory caches will be reclaimed
* due to memory shortage, thus freeing memory, or this
* directory cahe will be reclaimed.
*/
return;
}
oldsize = dcp->dc_fhash_mask + 1;
dcp->dc_fhash_mask = newsize - 1;
/*
* Move entries from the old table to the new
*/
for (i = 0; i < oldsize; i++) { /* for each hash bucket */
dfp = dcp->dc_freehash[i];
while (dfp != NULL) { /* for each chained entry */
tfp = dfp;
dfp = dfp->df_next;
nhp = &newhash[DDFHASH(tfp->df_handle, dcp)];
tfp->df_next = *nhp;
*nhp = tfp;
}
}
/*
* delete old hash table and set new one in place
*/
kmem_free(dcp->dc_freehash, sizeof (dcfree_t *) * oldsize);
dcp->dc_freehash = newhash;
}
|