|
root / base / usr / src / cmd / nscd / cache.c
cache.c C 2455 lines 58.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
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
/*
 * 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) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2012 Milan Jurik. All rights reserved.
 * Copyright (c) 2016 by Delphix. All rights reserved.
 * Copyright 2018 Joyent, Inc.
 */

/*
 * Cache routines for nscd
 */
#include <assert.h>
#include <errno.h>
#include <memory.h>
#include <signal.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <ucred.h>
#include <nss_common.h>
#include <locale.h>
#include <ctype.h>
#include <strings.h>
#include <string.h>
#include <umem.h>
#include <fcntl.h>
#include "cache.h"
#include "nscd_door.h"
#include "nscd_log.h"
#include "nscd_config.h"
#include "nscd_frontend.h"
#include "nscd_switch.h"

#define	SUCCESS		0
#define	NOTFOUND	-1
#define	SERVERERROR	-2
#define	NOSERVER	-3
#define	CONTINUE	-4

static nsc_db_t *nsc_get_db(nsc_ctx_t *, int);
static nscd_rc_t lookup_cache(nsc_lookup_args_t *, nscd_cfg_cache_t *,
		nss_XbyY_args_t *, char *, nsc_entry_t **);
static uint_t reap_cache(nsc_ctx_t *, uint_t, uint_t);
static void delete_entry(nsc_db_t *, nsc_ctx_t *, nsc_entry_t *);
static void print_stats(nscd_cfg_stat_cache_t *);
static void print_cfg(nscd_cfg_cache_t *);
static int lookup_int(nsc_lookup_args_t *, int);

#ifdef	NSCD_DEBUG
static void print_entry(nsc_db_t *, time_t, nsc_entry_t *);
static void avl_dump(nsc_db_t *, time_t);
static void hash_dump(nsc_db_t *, time_t);
#endif	/* NSCD_DEBUG */
static nsc_entry_t *hash_find(nsc_db_t *, nsc_entry_t *, uint_t *, nscd_bool_t);

static void queue_adjust(nsc_db_t *, nsc_entry_t *);
static void queue_remove(nsc_db_t *, nsc_entry_t *);
#ifdef	NSCD_DEBUG
static void queue_dump(nsc_db_t *, time_t);
#endif	/* NSCD_DEBUG */

static int launch_update(nsc_lookup_args_t *);
static void *do_update(void *);
static void getxy_keepalive(nsc_ctx_t *, nsc_db_t *, int, int);

static void ctx_info(nsc_ctx_t *);
static void ctx_info_nolock(nsc_ctx_t *);
static void ctx_invalidate(nsc_ctx_t *);

static void nsc_db_str_key_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
static void nsc_db_int_key_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
static void nsc_db_any_key_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);

static int nsc_db_cis_key_compar(const void *, const void *);
static int nsc_db_ces_key_compar(const void *, const void *);
static int nsc_db_int_key_compar(const void *, const void *);

static uint_t nsc_db_cis_key_gethash(nss_XbyY_key_t *, int);
static uint_t nsc_db_ces_key_gethash(nss_XbyY_key_t *, int);
static uint_t nsc_db_int_key_gethash(nss_XbyY_key_t *, int);

static umem_cache_t	*nsc_entry_cache;

static nsc_ctx_t *init_cache_ctx(int);
static void *reaper(void *);
static void *revalidate(void *);

static nss_status_t
dup_packed_buffer(void *src, void *dst)
{
	nsc_lookup_args_t	*s = (nsc_lookup_args_t *)src;
	nsc_entry_t		*d = (nsc_entry_t *)dst;
	nss_pheader_t		*sphdr = (nss_pheader_t *)s->buffer;
	nss_pheader_t		*dphdr = (nss_pheader_t *)d->buffer;
	int			slen, new_pbufsiz = 0;

	if (NSCD_GET_STATUS(sphdr) != NSS_SUCCESS) {

		/* no result, copy header only (status, errno, etc) */
		slen = sphdr->data_off;
	} else {
		/*
		 * lookup result returned, data to copy is the packed
		 * header plus result (add 1 for the terminating NULL
		 * just in case)
		 */
		slen = sphdr->data_off + sphdr->data_len + 1;
	}

	/* allocate cache packed buffer */
	if (dphdr != NULL && d->bufsize <= slen && d->bufsize != 0) {
		/* old buffer too small, free it */
		free(dphdr);
		d->buffer = NULL;
		d->bufsize = 0;
		dphdr = NULL;
	}
	if (dphdr == NULL) {
		/* get new buffer */
		dphdr = calloc(1, slen + 1);
		if (dphdr == NULL)
			return (NSS_ERROR);
		d->buffer = dphdr;
		d->bufsize = slen + 1;
		new_pbufsiz = slen + 1;
	}

	(void) memcpy(dphdr, sphdr, slen);
	if (new_pbufsiz != 0)
		dphdr->pbufsiz = new_pbufsiz;

	return (NSS_SUCCESS);
}

char *cache_name[CACHE_CTX_COUNT] = {
	NSS_DBNAM_PASSWD,
	NSS_DBNAM_GROUP,
	NSS_DBNAM_HOSTS,
	NSS_DBNAM_IPNODES,
	NSS_DBNAM_EXECATTR,
	NSS_DBNAM_PROFATTR,
	NSS_DBNAM_USERATTR,
	NSS_DBNAM_ETHERS,
	NSS_DBNAM_RPC,
	NSS_DBNAM_PROTOCOLS,
	NSS_DBNAM_NETWORKS,
	NSS_DBNAM_BOOTPARAMS,
	NSS_DBNAM_AUTHATTR,
	NSS_DBNAM_SERVICES,
	NSS_DBNAM_NETMASKS,
	NSS_DBNAM_PRINTERS,
	NSS_DBNAM_PROJECT,
	NSS_DBNAM_TSOL_TP,
	NSS_DBNAM_TSOL_RH
};

typedef void (*cache_init_ctx_t)(nsc_ctx_t *);
static cache_init_ctx_t cache_init_ctx[CACHE_CTX_COUNT] = {
	passwd_init_ctx,
	group_init_ctx,
	host_init_ctx,
	ipnode_init_ctx,
	exec_init_ctx,
	prof_init_ctx,
	user_init_ctx,
	ether_init_ctx,
	rpc_init_ctx,
	proto_init_ctx,
	net_init_ctx,
	bootp_init_ctx,
	auth_init_ctx,
	serv_init_ctx,
	netmask_init_ctx,
	printer_init_ctx,
	project_init_ctx,
	tnrhtp_init_ctx,
	tnrhdb_init_ctx
};

nsc_ctx_t *cache_ctx_p[CACHE_CTX_COUNT] = { 0 };
static nscd_cfg_stat_cache_t	null_stats = { 0 };
static nscd_cfg_global_cache_t	global_cfg;

/*
 * Given database name 'dbname' find cache index
 */
int
get_cache_idx(char *dbname)
{
	int	i;
	char	*nsc_name;

	for (i = 0; i < CACHE_CTX_COUNT; i++) {
		nsc_name = cache_name[i];
		if (strcmp(nsc_name, dbname) == 0)
			return (i);
	}
	return (-1);
}

/*
 * Given database name 'dbname' retrieve cache context,
 * if not created yet, allocate and initialize it.
 */
static nscd_rc_t
get_cache_ctx(char *dbname, nsc_ctx_t **ctx)
{
	int	i;

	*ctx = NULL;

	i = get_cache_idx(dbname);
	if (i == -1)
		return (NSCD_INVALID_ARGUMENT);
	if ((*ctx = cache_ctx_p[i]) == NULL) {
		*ctx = init_cache_ctx(i);
		if (*ctx == NULL)
			return (NSCD_NO_MEMORY);
	}

	return (NSCD_SUCCESS);
}

/*
 * Generate a log string to identify backend operation in debug logs
 */
static void
nsc_db_str_key_getlogstr(char *name, char *whoami, size_t len,
    nss_XbyY_args_t *argp)
{
	(void) snprintf(whoami, len, "%s [key=%s]", name, argp->key.name);
}


static void
nsc_db_int_key_getlogstr(char *name, char *whoami, size_t len,
    nss_XbyY_args_t *argp)
{
	(void) snprintf(whoami, len, "%s [key=%d]", name, argp->key.number);
}

/*ARGSUSED*/
static void
nsc_db_any_key_getlogstr(char *name, char *whoami, size_t len,
    nss_XbyY_args_t *argp)
{
	(void) snprintf(whoami, len, "%s", name);
}


/*
 * Returns cache based on dbop
 */
static nsc_db_t *
nsc_get_db(nsc_ctx_t *ctx, int dbop)
{
	int	i;

	for (i = 0; i < ctx->db_count; i++) {
		if (ctx->nsc_db[i] && dbop == ctx->nsc_db[i]->dbop)
			return (ctx->nsc_db[i]);
	}
	return (NULL);
}


/*
 * integer compare routine for _NSC_DB_INT_KEY
 */
static int
nsc_db_int_key_compar(const void *n1, const void *n2)
{
	nsc_entry_t	*e1, *e2;

	e1 = (nsc_entry_t *)n1;
	e2 = (nsc_entry_t *)n2;
	return (_NSC_INT_KEY_CMP(e1->key.number, e2->key.number));
}


/*
 * case sensitive name compare routine for _NSC_DB_CES_KEY
 */
static int
nsc_db_ces_key_compar(const void *n1, const void *n2)
{
	nsc_entry_t	*e1, *e2;
	int		res, l1, l2;

	e1 = (nsc_entry_t *)n1;
	e2 = (nsc_entry_t *)n2;
	l1 = strlen(e1->key.name);
	l2 = strlen(e2->key.name);
	res = strncmp(e1->key.name, e2->key.name, (l1 > l2)?l1:l2);
	return (_NSC_INT_KEY_CMP(res, 0));
}


/*
 * case insensitive name compare routine _NSC_DB_CIS_KEY
 */
static int
nsc_db_cis_key_compar(const void *n1, const void *n2)
{
	nsc_entry_t	*e1, *e2;
	int		res, l1, l2;

	e1 = (nsc_entry_t *)n1;
	e2 = (nsc_entry_t *)n2;
	l1 = strlen(e1->key.name);
	l2 = strlen(e2->key.name);
	res = strncasecmp(e1->key.name, e2->key.name, (l1 > l2)?l1:l2);
	return (_NSC_INT_KEY_CMP(res, 0));
}

/*
 * macro used to generate elf hashes for strings
 */
#define	_NSC_ELF_STR_GETHASH(func, str, htsize, hval) \
	hval = 0; \
	while (*str) { \
		uint_t  g; \
		hval = (hval << 4) + func(*str++); \
		if ((g = (hval & 0xf0000000)) != 0) \
			hval ^= g >> 24; \
		hval &= ~g; \
	} \
	hval %= htsize;


/*
 * cis hash function
 */
uint_t
cis_gethash(const char *key, int htsize)
{
	uint_t	hval;
	if (key == NULL)
		return (0);
	_NSC_ELF_STR_GETHASH(tolower, key, htsize, hval);
	return (hval);
}


/*
 * ces hash function
 */
uint_t
ces_gethash(const char *key, int htsize)
{
	uint_t	hval;
	if (key == NULL)
		return (0);
	_NSC_ELF_STR_GETHASH(, key, htsize, hval);
	return (hval);
}


/*
 * one-at-a-time hash function
 */
uint_t
db_gethash(const void *key, int len, int htsize)
{
	uint_t	hval, i;
	const char *str = key;

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

	for (hval = 0, i = 0; i < len; i++) {
		hval += str[i];
		hval += (hval << 10);
		hval ^= (hval >> 6);
	}
	hval += (hval << 3);
	hval ^= (hval >> 11);
	hval += (hval << 15);
	return (hval % htsize);
}


/*
 * case insensitive name gethash routine _NSC_DB_CIS_KEY
 */
static uint_t
nsc_db_cis_key_gethash(nss_XbyY_key_t *key, int htsize)
{
	return (cis_gethash(key->name, htsize));
}


/*
 * case sensitive name gethash routine _NSC_DB_CES_KEY
 */
static uint_t
nsc_db_ces_key_gethash(nss_XbyY_key_t *key, int htsize)
{
	return (ces_gethash(key->name, htsize));
}


/*
 * integer gethash routine _NSC_DB_INT_KEY
 */
static uint_t
nsc_db_int_key_gethash(nss_XbyY_key_t *key, int htsize)
{
	return (db_gethash(&key->number, sizeof (key->number), htsize));
}


/*
 * Find entry in the hash table
 * if cmp == nscd_true)
 *	return entry only if the keys match
 * else
 *	return entry in the hash location without checking the keys
 *
 */
static nsc_entry_t *
hash_find(nsc_db_t *nscdb, nsc_entry_t *entry, uint_t *hash,
    nscd_bool_t cmp)
{

	nsc_entry_t	*hashentry;

	if (nscdb->gethash)
		*hash = nscdb->gethash(&entry->key, nscdb->htsize);
	else
		return (NULL);

	hashentry = nscdb->htable[*hash];
	if (cmp == nscd_false || hashentry == NULL)
		return (hashentry);
	if (nscdb->compar) {
		if (nscdb->compar(entry, hashentry) == 0)
			return (hashentry);
	}
	return (NULL);
}


#define	HASH_REMOVE(nscdb, entry, hash, cmp) \
	if (nscdb->htable) { \
		if (entry == hash_find(nscdb, entry, &hash, cmp)) \
			nscdb->htable[hash] = NULL; \
	}


#define	HASH_INSERT(nscdb, entry, hash, cmp) \
	if (nscdb->htable) { \
		(void) hash_find(nscdb, entry, &hash, cmp); \
		nscdb->htable[hash] = entry; \
	}


#ifdef	NSCD_DEBUG
static void
print_entry(nsc_db_t *nscdb, time_t now, nsc_entry_t *entry)
{
	nss_XbyY_args_t args;
	char		whoami[512];

	switch (entry->stats.status) {
	case ST_NEW_ENTRY:
		(void) fprintf(stdout, gettext("\t status: new entry\n"));
		return;
	case ST_UPDATE_PENDING:
		(void) fprintf(stdout, gettext("\t status: update pending\n"));
		return;
	case ST_LOOKUP_PENDING:
		(void) fprintf(stdout, gettext("\t status: lookup pending\n"));
		return;
	case ST_DISCARD:
		(void) fprintf(stdout, gettext("\t status: discarded entry\n"));
		return;
	default:
		if (entry->stats.timestamp < now)
			(void) fprintf(stdout,
			    gettext("\t status: expired (%d seconds ago)\n"),
			    now - entry->stats.timestamp);
		else
			(void) fprintf(stdout, gettext(
			    "\t status: valid (expiry in %d seconds)\n"),
			    entry->stats.timestamp - now);
		break;
	}
	(void) fprintf(stdout, gettext("\t hits: %u\n"), entry->stats.hits);
	args.key = entry->key;
	(void) nscdb->getlogstr(nscdb->name, whoami, sizeof (whoami), &args);
	(void) fprintf(stdout, "\t %s\n", whoami);
}
#endif	/* NSCD_DEBUG */

static void
print_stats(nscd_cfg_stat_cache_t *statsp)
{

	(void) fprintf(stdout, gettext("\n\t STATISTICS:\n"));
	(void) fprintf(stdout, gettext("\t positive hits: %lu\n"),
	    statsp->pos_hits);
	(void) fprintf(stdout, gettext("\t negative hits: %lu\n"),
	    statsp->neg_hits);
	(void) fprintf(stdout, gettext("\t positive misses: %lu\n"),
	    statsp->pos_misses);
	(void) fprintf(stdout, gettext("\t negative misses: %lu\n"),
	    statsp->neg_misses);
	(void) fprintf(stdout, gettext("\t total entries: %lu\n"),
	    statsp->entries);
	(void) fprintf(stdout, gettext("\t queries queued: %lu\n"),
	    statsp->wait_count);
	(void) fprintf(stdout, gettext("\t queries dropped: %lu\n"),
	    statsp->drop_count);
	(void) fprintf(stdout, gettext("\t cache invalidations: %lu\n"),
	    statsp->invalidate_count);

	_NSC_GET_HITRATE(statsp);
	(void) fprintf(stdout, gettext("\t cache hit rate: %.1f\n"),
	    statsp->hitrate);
}


static void
print_cfg(nscd_cfg_cache_t *cfgp)
{
	(void) fprintf(stdout, gettext("\n\t CONFIG:\n"));
	(void) fprintf(stdout, gettext("\t enabled: %s\n"),
	    yes_no(cfgp->enable));
	(void) fprintf(stdout, gettext("\t per user cache: %s\n"),
	    yes_no(cfgp->per_user));
	(void) fprintf(stdout, gettext("\t avoid name service: %s\n"),
	    yes_no(cfgp->avoid_ns));
	(void) fprintf(stdout, gettext("\t check file: %s\n"),
	    yes_no(cfgp->check_files));
	(void) fprintf(stdout, gettext("\t check file interval: %d\n"),
	    cfgp->check_interval);
	(void) fprintf(stdout, gettext("\t positive ttl: %d\n"),
	    cfgp->pos_ttl);
	(void) fprintf(stdout, gettext("\t negative ttl: %d\n"),
	    cfgp->neg_ttl);
	(void) fprintf(stdout, gettext("\t keep hot count: %d\n"),
	    cfgp->keephot);
	(void) fprintf(stdout, gettext("\t hint size: %d\n"),
	    cfgp->hint_size);
	(void) fprintf(stdout, gettext("\t max entries: %lu%s"),
	    cfgp->maxentries, cfgp->maxentries?"\n":" (unlimited)\n");
}


#ifdef	NSCD_DEBUG
static void
hash_dump(nsc_db_t *nscdb, time_t now)
{
	nsc_entry_t	*entry;
	int		i;

	(void) fprintf(stdout, gettext("\n\nHASH TABLE:\n"));
	for (i = 0; i < nscdb->htsize; i++) {
		if ((entry = nscdb->htable[i]) != NULL) {
			(void) fprintf(stdout, "hash[%d]:\n", i);
			print_entry(nscdb, now, entry);
		}
	}
}
#endif	/* NSCD_DEBUG */


#ifdef	NSCD_DEBUG
static void
avl_dump(nsc_db_t *nscdb, time_t now)
{
	nsc_entry_t	*entry;
	int		i;

	(void) fprintf(stdout, gettext("\n\nAVL TREE:\n"));
	for (entry = avl_first(&nscdb->tree), i = 0; entry != NULL;
	    entry = avl_walk(&nscdb->tree, entry, AVL_AFTER)) {
		(void) fprintf(stdout, "avl node[%d]:\n", i++);
		print_entry(nscdb, now, entry);
	}
}
#endif	/* NSCD_DEBUG */


#ifdef	NSCD_DEBUG
static void
queue_dump(nsc_db_t *nscdb, time_t now)
{
	nsc_entry_t	*entry;
	int		i;

	(void) fprintf(stdout,
	    gettext("\n\nCACHE [name=%s, nodes=%lu]:\n"),
	    nscdb->name, avl_numnodes(&nscdb->tree));

	(void) fprintf(stdout,
	    gettext("Starting with the most recently accessed:\n"));

	for (entry = nscdb->qtail, i = 0; entry; entry = entry->qnext) {
		(void) fprintf(stdout, "entry[%d]:\n", i++);
		print_entry(nscdb, now, entry);
	}
}
#endif	/* NSCD_DEBUG */

static void
queue_remove(nsc_db_t *nscdb, nsc_entry_t *entry)
{

	if (nscdb->qtail == entry)
		nscdb->qtail = entry->qnext;
	else
		entry->qprev->qnext = entry->qnext;

	if (nscdb->qhead == entry)
		nscdb->qhead = entry->qprev;
	else
		entry->qnext->qprev = entry->qprev;

	if (nscdb->reap_node == entry)
		nscdb->reap_node = entry->qnext;
	entry->qnext = entry->qprev = NULL;
}


static void
queue_adjust(nsc_db_t *nscdb, nsc_entry_t *entry)
{

#ifdef NSCD_DEBUG
	assert(nscdb->qtail || entry->qnext == NULL &&
	    entry->qprev == NULL);

	assert(nscdb->qtail && nscdb->qhead ||
	    nscdb->qtail == NULL && nscdb->qhead == NULL);

	assert(entry->qprev || entry->qnext == NULL ||
	    nscdb->qtail == entry);
#endif /* NSCD_DEBUG */

	/* already in the desired position */
	if (nscdb->qtail == entry)
		return;

	/* new queue */
	if (nscdb->qtail == NULL) {
		nscdb->qhead = nscdb->qtail = entry;
		return;
	}

	/* new entry (prev == NULL AND tail != entry) */
	if (entry->qprev == NULL) {
		nscdb->qtail->qprev = entry;
		entry->qnext = nscdb->qtail;
		nscdb->qtail = entry;
		return;
	}

	/* existing entry */
	if (nscdb->reap_node == entry)
		nscdb->reap_node = entry->qnext;
	if (nscdb->qhead == entry)
		nscdb->qhead = entry->qprev;
	else
		entry->qnext->qprev = entry->qprev;
	entry->qprev->qnext = entry->qnext;
	entry->qprev = NULL;
	entry->qnext = nscdb->qtail;
	nscdb->qtail->qprev = entry;
	nscdb->qtail = entry;
}


/*
 * Init cache
 */
nscd_rc_t
init_cache(int debug_level)
{
	int cflags;

	cflags = (debug_level > 0)?0:UMC_NODEBUG;
	nsc_entry_cache = umem_cache_create("nsc_entry_cache",
	    sizeof (nsc_entry_t), 0, NULL, NULL, NULL, NULL, NULL, cflags);
	if (nsc_entry_cache == NULL)
		return (NSCD_NO_MEMORY);
	return (NSCD_SUCCESS);
}


/*
 * Create cache
 */
nsc_db_t *
make_cache(enum db_type dbtype, int dbop, char *name,
    int (*compar) (const void *, const void *),
    void (*getlogstr)(char *, char *, size_t, nss_XbyY_args_t *),
    uint_t (*gethash)(nss_XbyY_key_t *, int),
    enum hash_type httype, int htsize)
{
	nsc_db_t	*nscdb;
	char		*me = "make_cache";

	nscdb = (nsc_db_t *)malloc(sizeof (*nscdb));
	if (nscdb == NULL) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
		(me, "%s: memory allocation failure\n", name);
		goto out;
	}
	(void) memset(nscdb, 0, sizeof (*nscdb));

	nscdb->dbop = dbop;
	nscdb->name = name;
	nscdb->db_type = dbtype;

	/* Assign compare routine */
	if (compar == NULL) {
		if (_NSC_DB_CES_KEY(nscdb))
			nscdb->compar = nsc_db_ces_key_compar;
		else if (_NSC_DB_CIS_KEY(nscdb))
			nscdb->compar = nsc_db_cis_key_compar;
		else if (_NSC_DB_INT_KEY(nscdb))
			nscdb->compar = nsc_db_int_key_compar;
		else
			assert(0);
	} else {
		nscdb->compar = compar;
	}

	/* The cache is an AVL tree */
	avl_create(&nscdb->tree, nscdb->compar, sizeof (nsc_entry_t),
	    offsetof(nsc_entry_t, avl_link));

	/* Assign log routine */
	if (getlogstr == NULL) {
		if (_NSC_DB_STR_KEY(nscdb))
			nscdb->getlogstr = nsc_db_str_key_getlogstr;
		else if (_NSC_DB_INT_KEY(nscdb))
			nscdb->getlogstr = nsc_db_int_key_getlogstr;
		else
			nscdb->getlogstr = nsc_db_any_key_getlogstr;
	} else {
		nscdb->getlogstr = getlogstr;
	}

	/* The AVL tree based cache uses a hash table for quick access */
	if (htsize != 0) {
		/* Determine hash table size based on type */
		nscdb->hash_type = httype;
		if (htsize < 0) {
			switch (httype) {
			case nsc_ht_power2:
				htsize = _NSC_INIT_HTSIZE_POWER2;
				break;
			case nsc_ht_prime:
			case nsc_ht_default:
			default:
				htsize = _NSC_INIT_HTSIZE_PRIME;
			}
		}
		nscdb->htsize = htsize;

		/* Create the hash table */
		nscdb->htable = calloc(htsize, sizeof (*(nscdb->htable)));
		if (nscdb->htable == NULL) {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
			(me, "%s: memory allocation failure\n", name);
			goto out;
		}

		/* Assign gethash routine */
		if (gethash == NULL) {
			if (_NSC_DB_CES_KEY(nscdb))
				nscdb->gethash = nsc_db_ces_key_gethash;
			else if (_NSC_DB_CIS_KEY(nscdb))
				nscdb->gethash = nsc_db_cis_key_gethash;
			else if (_NSC_DB_INT_KEY(nscdb))
				nscdb->gethash = nsc_db_int_key_gethash;
			else
				assert(0);
		} else {
			nscdb->gethash = gethash;
		}
	}

	(void) mutex_init(&nscdb->db_mutex, USYNC_THREAD, NULL);
	return (nscdb);

out:
	if (nscdb->htable)
		free(nscdb->htable);
	if (nscdb)
		free(nscdb);
	return (NULL);
}


/*
 * verify
 */
/* ARGSUSED */
nscd_rc_t
_nscd_cfg_cache_verify(
	void				*data,
	struct nscd_cfg_param_desc	*pdesc,
	nscd_cfg_id_t			*nswdb,
	nscd_cfg_flag_t			dflag,
	nscd_cfg_error_t		**errorp,
	void				**cookie)
{

	return (NSCD_SUCCESS);
}

/*
 * notify
 */
/* ARGSUSED */
nscd_rc_t
_nscd_cfg_cache_notify(
	void				*data,
	struct nscd_cfg_param_desc	*pdesc,
	nscd_cfg_id_t			*nswdb,
	nscd_cfg_flag_t			dflag,
	nscd_cfg_error_t		**errorp,
	void				**cookie)
{
	nsc_ctx_t	*ctx;
	void		*dp;
	int		i;

	/* group data */
	if (_nscd_cfg_flag_is_set(dflag, NSCD_CFG_DFLAG_GROUP)) {
		if (_nscd_cfg_flag_is_set(pdesc->pflag,
		    NSCD_CFG_PFLAG_GLOBAL)) {
			/* global config */
			global_cfg = *(nscd_cfg_global_cache_t *)data;
		} else if (_nscd_cfg_flag_is_set(dflag,
		    NSCD_CFG_DFLAG_SET_ALL_DB)) {
			/* non-global config for all dbs */
			for (i = 0; i < CACHE_CTX_COUNT; i++) {
				ctx = cache_ctx_p[i];
				if (ctx == NULL)
					return (NSCD_CTX_NOT_FOUND);
				(void) rw_wrlock(&ctx->cfg_rwlp);
				ctx->cfg = *(nscd_cfg_cache_t *)data;
				ctx->cfg_mtime = time(NULL);
				(void) rw_unlock(&ctx->cfg_rwlp);
			}
		} else {
			/* non-global config for a specific db */

			/* ignore non-caching databases */
			if (get_cache_ctx(nswdb->name, &ctx) != NSCD_SUCCESS)
				return (NSCD_SUCCESS);
			(void) rw_wrlock(&ctx->cfg_rwlp);
			ctx->cfg = *(nscd_cfg_cache_t *)data;
			ctx->cfg_mtime = time(NULL);
			(void) rw_unlock(&ctx->cfg_rwlp);
		}
		return (NSCD_SUCCESS);
	}

	/* individual data */
	if (_nscd_cfg_flag_is_set(pdesc->pflag, NSCD_CFG_PFLAG_GLOBAL)) {
		/* global config */
		dp = (char *)&global_cfg + pdesc->p_offset;
		(void) memcpy(dp, data, pdesc->p_size);
	} else if (_nscd_cfg_flag_is_set(dflag,
	    NSCD_CFG_DFLAG_SET_ALL_DB)) {
		/* non-global config for all dbs */
		for (i = 0; i < CACHE_CTX_COUNT; i++) {
			ctx = cache_ctx_p[i];
			if (ctx == NULL)
				return (NSCD_CTX_NOT_FOUND);
			dp = (char *)&ctx->cfg + pdesc->p_offset;
			(void) rw_wrlock(&ctx->cfg_rwlp);
			(void) memcpy(dp, data, pdesc->p_size);
			ctx->cfg_mtime = time(NULL);
			(void) rw_unlock(&ctx->cfg_rwlp);
		}
	} else {
		/* non-global config for a specific db */

		/* ignore non-caching databases */
		if (get_cache_ctx(nswdb->name, &ctx) != NSCD_SUCCESS)
			return (NSCD_SUCCESS);
		dp = (char *)&ctx->cfg + pdesc->p_offset;
		(void) rw_wrlock(&ctx->cfg_rwlp);
		(void) memcpy(dp, data, pdesc->p_size);
		ctx->cfg_mtime = time(NULL);
		(void) rw_unlock(&ctx->cfg_rwlp);
	}
	return (NSCD_SUCCESS);
}


/*
 * get stat
 */
/* ARGSUSED */
nscd_rc_t
_nscd_cfg_cache_get_stat(
	void				**stat,
	struct nscd_cfg_stat_desc	*sdesc,
	nscd_cfg_id_t			*nswdb,
	nscd_cfg_flag_t			*dflag,
	void				(**free_stat)(void *stat),
	nscd_cfg_error_t		**errorp)
{
	nscd_cfg_stat_cache_t	*statsp, stats;
	nsc_ctx_t		*ctx;
	int			i;
	nscd_rc_t		rc;

	statsp = calloc(1, sizeof (*statsp));
	if (statsp == NULL)
		return (NSCD_NO_MEMORY);

	if (_nscd_cfg_flag_is_set(sdesc->sflag, NSCD_CFG_SFLAG_GLOBAL)) {
		for (i = 0; i < CACHE_CTX_COUNT; i++) {
			if (cache_ctx_p[i] == NULL)
				stats = null_stats;
			else {
				(void) mutex_lock(&cache_ctx_p[i]->stats_mutex);
				stats = cache_ctx_p[i]->stats;
				(void) mutex_unlock(
				    &cache_ctx_p[i]->stats_mutex);
			}
			statsp->pos_hits += stats.pos_hits;
			statsp->neg_hits += stats.neg_hits;
			statsp->pos_misses += stats.pos_misses;
			statsp->neg_misses += stats.neg_misses;
			statsp->entries += stats.entries;
			statsp->drop_count += stats.drop_count;
			statsp->wait_count += stats.wait_count;
			statsp->invalidate_count +=
			    stats.invalidate_count;
		}
	} else {
		if ((rc = get_cache_ctx(nswdb->name, &ctx)) != NSCD_SUCCESS) {
			free(statsp);
			return (rc);
		}
		(void) mutex_lock(&ctx->stats_mutex);
		*statsp = ctx->stats;
		(void) mutex_unlock(&ctx->stats_mutex);
	}

	_NSC_GET_HITRATE(statsp);
	*stat = statsp;
	return (NSCD_SUCCESS);
}

/*
 * This function should only be called when nscd is
 * not a daemon.
 */
void
nsc_info(nsc_ctx_t *ctx, char *dbname, nscd_cfg_cache_t cfg[],
    nscd_cfg_stat_cache_t stats[])
{
	int		i;
	char		*me = "nsc_info";
	nsc_ctx_t	*ctx1;
	nsc_ctx_t	ctx2;
	nscd_rc_t	rc;

	if (ctx) {
		ctx_info(ctx);
		return;
	}

	if (dbname) {
		rc = get_cache_ctx(dbname, &ctx1);
		if (rc == NSCD_INVALID_ARGUMENT) {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
			(me, "%s: no cache context found\n", dbname);
			return;
		} else if (rc == NSCD_NO_MEMORY) {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
	(me, "%s: unable to create cache context - no memory\n",
	    dbname);
			return;
		}
		ctx_info(ctx1);
		return;
	}

	if (cfg == NULL || stats == NULL)
		return;

	for (i = 0; i < CACHE_CTX_COUNT; i++) {

		ctx2.dbname = cache_name[i];
		ctx2.cfg = cfg[i];
		ctx2.stats = stats[i];
		ctx_info_nolock(&ctx2);
	}
}

static void
ctx_info_nolock(nsc_ctx_t *ctx)
{
	nscd_cfg_cache_t	cfg;
	nscd_cfg_stat_cache_t	stats;

	cfg = ctx->cfg;
	(void) fprintf(stdout, gettext("\n\nCACHE: %s\n"), ctx->dbname);
	(void) print_cfg(&cfg);

	if (cfg.enable == nscd_false)
		return;

	stats = ctx->stats;
	(void) print_stats(&stats);
}

static void
ctx_info(nsc_ctx_t *ctx)
{
	nscd_cfg_cache_t	cfg;
	nscd_cfg_stat_cache_t	stats;

	(void) rw_rdlock(&ctx->cfg_rwlp);
	cfg = ctx->cfg;
	(void) rw_unlock(&ctx->cfg_rwlp);
	(void) fprintf(stdout, gettext("\n\nCACHE: %s\n"), ctx->dbname);
	(void) print_cfg(&cfg);

	if (cfg.enable == nscd_false)
		return;

	(void) mutex_lock(&ctx->stats_mutex);
	stats = ctx->stats;
	(void) mutex_unlock(&ctx->stats_mutex);
	(void) print_stats(&stats);
}

#ifdef	NSCD_DEBUG
/*
 * This function should only be called when nscd is
 * not a daemon.
 */
int
nsc_dump(char *dbname, int dbop)
{
	nsc_ctx_t	*ctx;
	nsc_db_t	*nscdb;
	nscd_bool_t	enabled;
	time_t		now;
	char		*me = "nsc_dump";
	int		i;

	if ((i = get_cache_idx(dbname)) == -1) {
		(void) fprintf(stdout, gettext("invalid cache name\n"));

		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
		(me, "%s: invalid cache name\n", dbname);
		return (NSCD_CACHE_INVALID_CACHE_NAME);
	}

	if ((ctx = cache_ctx_p[i]) == NULL)  {
		(void) fprintf(stdout, gettext("no cache context\n"));

		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
		(me, "%s: no cache context\n", dbname);
		return (NSCD_CACHE_NO_CACHE_CTX);
	}

	now = time(NULL);
	(void) rw_rdlock(&ctx->cfg_rwlp);
	enabled = ctx->cfg.enable;
	(void) rw_unlock(&ctx->cfg_rwlp);

	if (enabled == nscd_false)
		return (NSCD_CACHE_DISABLED);

	nscdb = nsc_get_db(ctx, dbop);
	if (nscdb == NULL) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
		(me, "%s:%d: no cache found\n", dbname, dbop);
		return (NSCD_CACHE_NO_CACHE_FOUND);
	}

	(void) mutex_lock(&nscdb->db_mutex);
	(void) queue_dump(nscdb, now);
	(void) hash_dump(nscdb, now);
	(void) avl_dump(nscdb, now);
	(void) mutex_unlock(&nscdb->db_mutex);
	return (NSCD_SUCCESS);
}
#endif	/* NSCD_DEBUG */

/*
 * These macros are for exclusive use of nsc_lookup
 */
#define	NSC_LOOKUP_LOG(loglevel, fmt) \
	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_##loglevel) \
		(me, fmt, whoami);

static int
nsc_lookup_no_cache(nsc_lookup_args_t *largs, const char *str)
{
	char *me = "nsc_lookup_no_cache";
	nss_status_t status;

	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "%s: name service lookup (bypassing cache)\n", str);
	nss_psearch(largs->buffer, largs->bufsize);
	status = NSCD_GET_STATUS(largs->buffer);
	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "%s: name service lookup status = %d\n", str, status);
	if (status == NSS_SUCCESS) {
		return (SUCCESS);
	} else if (status == NSS_NOTFOUND) {
		return (NOTFOUND);
	} else {
		return (SERVERERROR);
	}
}

/*
 * This function starts the revalidation and reaper threads
 * for a cache
 */
static void
start_threads(nsc_ctx_t *ctx)
{
	int	errnum;
	char	*me = "start_threads";

	/*
	 *  kick off the revalidate thread (if necessary)
	 */
	if (ctx->revalidate_on != nscd_true) {
		if (thr_create(NULL, 0, revalidate, ctx, 0, NULL) != 0) {
			errnum = errno;
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
			(me, "thr_create (revalidate thread for %s): %s\n",
			    ctx->dbname, strerror(errnum));
			exit(1);
		}
		ctx->revalidate_on = nscd_true;
	}

	/*
	 *  kick off the reaper thread (if necessary)
	 */
	if (ctx->reaper_on != nscd_true) {
		if (thr_create(NULL, 0, reaper, ctx, 0, NULL) != 0) {
			errnum = errno;
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
			(me, "thr_create (reaper thread for %s): %s\n",
			    ctx->dbname, strerror(errnum));
			exit(1);
		}
		ctx->reaper_on = nscd_true;
	}
}

/*
 * Examine the packed buffer, see if the front-end parameters
 * indicate that the caller specified nsswitch config should be
 * used for the lookup. Return 1 if yes, otherwise 0.
 */
static int
nsw_config_in_phdr(void *buf)
{
	nss_pheader_t		*pbuf = (nss_pheader_t *)buf;
	nssuint_t		off;
	nss_dbd_t		*pdbd;
	char			*me = "nsw_config_in_phdr";

	off = pbuf->dbd_off;
	if (off == 0)
		return (0);
	pdbd = (nss_dbd_t *)((void *)((char *)pbuf + off));
	if (pdbd->o_default_config == 0)
		return (0);

	if ((enum nss_dbp_flags)pdbd->flags & NSS_USE_DEFAULT_CONFIG) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "use caller specified nsswitch config\n");
		return (1);
	} else
		return (0);
}

static nss_status_t
copy_result(void *rbuf, void *cbuf)
{
	nss_pheader_t	*rphdr = (nss_pheader_t *)rbuf;
	nss_pheader_t	*cphdr = (nss_pheader_t *)cbuf;
	char		*me = "copy_result";

	/* return NSS_ERROR if not enough room to copy result */
	if (cphdr->data_len + 1 > rphdr->data_len) {
		NSCD_SET_STATUS(rphdr, NSS_ERROR, ERANGE);
		return (NSS_ERROR);
	} else {
		char	*dst;

		if (cphdr->data_len == 0)
			return (NSS_SUCCESS);

		dst = (char *)rphdr + rphdr->data_off;
		(void) memcpy(dst, (char *)cphdr + cphdr->data_off,
		    cphdr->data_len);
		rphdr->data_len = cphdr->data_len;
		/* some frontend code expects a terminating NULL char */
		*(dst + rphdr->data_len) = '\0';

		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "cache data (len = %lld): >>%s<<\n",
		    cphdr->data_len, (char *)cphdr + cphdr->data_off);

		return (NSS_SUCCESS);
	}
}

static int
get_dns_ttl(void *pbuf, char *dbname)
{
	nss_pheader_t	*phdr = (nss_pheader_t *)pbuf;
	int		ttl;
	char		*me = "get_dns_ttl";

	/* if returned, dns ttl is stored in the extended data area */
	if (phdr->ext_off == 0)
		return (-1);

	if (strcmp(dbname, NSS_DBNAM_HOSTS) != 0 &&
	    strcmp(dbname, NSS_DBNAM_IPNODES) != 0)
		return (-1);

	ttl = *(nssuint_t *)((void *)((char *)pbuf + phdr->ext_off));

	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
	(me, "dns ttl is %d seconds\n", ttl);

	return (ttl);
}

static int
check_config(nsc_lookup_args_t *largs, nscd_cfg_cache_t *cfgp,
    char *whoami, int flag)
{
	nsc_db_t	*nscdb;
	nsc_ctx_t	*ctx;
	char		*me = "check_config";

	ctx = largs->ctx;
	nscdb = largs->nscdb;

	/* see if the cached config needs update */
	if (nscdb->cfg_mtime != ctx->cfg_mtime) {
		(void) rw_rdlock(&ctx->cfg_rwlp);
		nscdb->cfg = ctx->cfg;
		nscdb->cfg_mtime = ctx->cfg_mtime;
		(void) rw_unlock(&ctx->cfg_rwlp);
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "config for context %s, database %s updated\n",
		    ctx->dbname, nscdb->name);
	}
	*cfgp = nscdb->cfg;

	if (cfgp->enable == nscd_false) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
			(me, "%s: cache disabled\n", ctx->dbname);

		if (UPDATEBIT & flag)
			return (NOTFOUND);
		else
			return (nsc_lookup_no_cache(largs, whoami));
	}

	/*
	 * if caller requests lookup using its
	 * own nsswitch config, bypass cache
	 */
	if (nsw_config_in_phdr(largs->buffer))
		return (nsc_lookup_no_cache(largs, whoami));

	/* no need of cache if we are dealing with 0 ttls */
	if (cfgp->pos_ttl <= 0 && cfgp->neg_ttl <= 0) {
		if (flag & UPDATEBIT)
			return (NOTFOUND);
		else if (cfgp->avoid_ns == nscd_true)
			return (SERVERERROR);
		return (nsc_lookup_no_cache(largs, whoami));
	}

	return (CONTINUE);
}

/*
 * Invalidate cache if database file has been modified.
 * See check_files config param for details.
 */
static void
check_db_file(nsc_ctx_t *ctx, nscd_cfg_cache_t cfg,
    char *whoami, time_t now)
{
	struct stat	buf;
	nscd_bool_t	file_modified = nscd_false;
	char		*me = "check_db_file";

	if (cfg.check_interval != 0 &&
	    (now - ctx->file_chktime) < cfg.check_interval)
		return;

	ctx->file_chktime = now;
	if (stat(ctx->file_name, &buf) == 0) {
		if (ctx->file_mtime == 0) {
			(void) mutex_lock(&ctx->file_mutex);
			if (ctx->file_mtime == 0) {
				ctx->file_mtime = buf.st_mtime;
				ctx->file_size = buf.st_size;
				ctx->file_ino = buf.st_ino;
			}
			(void) mutex_unlock(&ctx->file_mutex);
		} else if (ctx->file_mtime < buf.st_mtime ||
		    ctx->file_size != buf.st_size ||
		    ctx->file_ino != buf.st_ino) {
			(void) mutex_lock(&ctx->file_mutex);
			if (ctx->file_mtime < buf.st_mtime ||
			    ctx->file_size != buf.st_size ||
			    ctx->file_ino != buf.st_ino) {
				file_modified = nscd_true;
				ctx->file_mtime = buf.st_mtime;
				ctx->file_size = buf.st_size;
				ctx->file_ino = buf.st_ino;
			}
			(void) mutex_unlock(&ctx->file_mutex);
		}
	}

	if (file_modified == nscd_true) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "%s: file %s has been modified - invalidating cache\n",
		    whoami, ctx->file_name);
		ctx_invalidate(ctx);
	}
}

static int
lookup_int(nsc_lookup_args_t *largs, int flag)
{
	nsc_ctx_t		*ctx;
	nsc_db_t		*nscdb;
	nscd_cfg_cache_t	cfg;
	nsc_entry_t		*this_entry;
	nsc_entry_stat_t	*this_stats;
	nsc_action_t		next_action;
	nss_status_t		status;
	nscd_bool_t		delete;
	nscd_rc_t		rc;
	char			*dbname;
	int			dbop, errnum;
	int			cfg_rc;
	nss_XbyY_args_t		args;
	char			whoami[128];
	time_t			now = time(NULL); /* current time */
	char			*me = "lookup_int";

	/* extract dbop, dbname, key and cred */
	status = nss_packed_getkey(largs->buffer, largs->bufsize, &dbname,
	    &dbop, &args);
	if (status != NSS_SUCCESS) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
			(me, "nss_packed_getkey failure (%d)\n", status);
		return (SERVERERROR);
	}

	/* get the cache context */
	if (largs->ctx == NULL) {
		if (get_cache_ctx(dbname, &largs->ctx) != NSCD_SUCCESS) {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
				(me, "%s: no cache context found\n", dbname);

			if (UPDATEBIT & flag)
				return (NOTFOUND);
			else
				return (nsc_lookup_no_cache(largs, dbname));
		}
	}
	ctx = largs->ctx;

	if (largs->nscdb == NULL) {
		if ((largs->nscdb = nsc_get_db(ctx, dbop)) == NULL) {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
				(me, "%s:%d: no cache found\n",
				    dbname, dbop);

			if (UPDATEBIT & flag)
				return (NOTFOUND);
			else
				return (nsc_lookup_no_cache(largs, dbname));
		}
	}

	nscdb = largs->nscdb;

	_NSCD_LOG_IF(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ALL) {
		(void) nscdb->getlogstr(nscdb->name, whoami,
		    sizeof (whoami), &args);
	}

	if (UPDATEBIT & flag) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
			(me, "%s: refresh start\n", whoami);
	} else {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
			(me, "%s: lookup start\n", whoami);
	}

	cfg_rc = check_config(largs, &cfg, whoami, flag);
	if (cfg_rc != CONTINUE)
		return (cfg_rc);

	/*
	 * Invalidate cache if file has been modified.
	 */
	if (cfg.check_files == nscd_true)
		check_db_file(ctx, cfg, whoami, now);

	(void) mutex_lock(&nscdb->db_mutex);

	/* Lookup the cache table */
	for (;;) {
		delete = nscd_false;
		rc = lookup_cache(largs, &cfg, &args, whoami, &this_entry);
		if (rc != NSCD_SUCCESS) {
			(void) mutex_unlock(&nscdb->db_mutex);

			/* Either no entry and avoid name service */
			if (rc == NSCD_DB_ENTRY_NOT_FOUND ||
			    rc == NSCD_INVALID_ARGUMENT)
				return (NOTFOUND);

			/* OR memory error */
			return (SERVERERROR);
		}

		/* get the stats from the entry */
		this_stats = &this_entry->stats;

		/*
		 * What should we do next ?
		 */
		switch (this_stats->status) {
		case ST_NEW_ENTRY:
			delete = nscd_true;
			next_action = _NSC_NSLOOKUP;
			break;
		case ST_UPDATE_PENDING:
			if (flag & UPDATEBIT) {
				(void) mutex_unlock(&nscdb->db_mutex);
				return (NOTFOUND);
			} else if (this_stats->timestamp < now)
				next_action = _NSC_WAIT;
			else
				next_action = _NSC_USECACHED;
			break;
		case ST_LOOKUP_PENDING:
			if (flag & UPDATEBIT) {
				(void) mutex_unlock(&nscdb->db_mutex);
				return (NOTFOUND);
			}
			next_action = _NSC_WAIT;
			break;
		case ST_DISCARD:
			if (cfg.avoid_ns == nscd_true) {
				(void) mutex_unlock(&nscdb->db_mutex);
				return (NOTFOUND);
			}
			/* otherwise reuse the entry */
			(void) memset(this_stats, 0, sizeof (*this_stats));
			next_action = _NSC_NSLOOKUP;
			break;
		default:
			if (cfg.avoid_ns == nscd_true)
				next_action = _NSC_USECACHED;
			else if ((flag & UPDATEBIT) ||
			    (this_stats->timestamp < now)) {
				_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
			(me, "%s: cached entry needs to be updated\n",
			    whoami);
				next_action = _NSC_NSLOOKUP;
			} else
				next_action = _NSC_USECACHED;
			break;
		}

		if (next_action == _NSC_WAIT) {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
			(me, "%s: need to wait\n", whoami);

			/* do we have clearance ? */
			if (_nscd_get_clearance(&ctx->throttle_sema) != 0) {
				/* nope. quit */
				(void) mutex_lock(&ctx->stats_mutex);
				ctx->stats.drop_count++;
				(void) mutex_unlock(&ctx->stats_mutex);
				_NSCD_LOG(NSCD_LOG_CACHE,
				    NSCD_LOG_LEVEL_DEBUG_6)
				(me, "%s: throttling load\n", whoami);
				(void) mutex_unlock(&nscdb->db_mutex);
				NSC_LOOKUP_LOG(WARNING,
				    "%s: no clearance to wait\n");
				return (NOSERVER);
			}
			/* yes can wait */
			(void) nscd_wait(ctx, nscdb, this_entry);
			(void) _nscd_release_clearance(&ctx->throttle_sema);
			continue;
		}

		break;
	}


	if (!(UPDATEBIT & flag))
		this_stats->hits++;		/* update hit count */

	if (next_action == _NSC_NSLOOKUP) {

		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "%s: name service lookup required\n", whoami);

		if (_nscd_get_clearance(&ctx->throttle_sema) != 0) {
			if (delete == nscd_true)
				delete_entry(nscdb, ctx, this_entry);
			else
				this_stats->status = ST_DISCARD;
			(void) mutex_lock(&ctx->stats_mutex);
			ctx->stats.drop_count++;
			(void) mutex_unlock(&ctx->stats_mutex);
			(void) mutex_unlock(&nscdb->db_mutex);
			NSC_LOOKUP_LOG(WARNING,
			    "%s: no clearance for lookup\n");
			return (NOSERVER);
		}

		/* block any threads accessing this entry */
		this_stats->status = (flag & UPDATEBIT) ?
		    ST_UPDATE_PENDING : ST_LOOKUP_PENDING;

		/* release lock and do name service lookup */
		(void) mutex_unlock(&nscdb->db_mutex);
		nss_psearch(largs->buffer, largs->bufsize);
		status = NSCD_GET_STATUS(largs->buffer);
		(void) mutex_lock(&nscdb->db_mutex);
		this_stats->status = 0;
		(void) _nscd_release_clearance(&ctx->throttle_sema);

		/* signal waiting threads */
		(void) nscd_signal(ctx, nscdb, this_entry);

		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "%s: name service lookup status = %d\n",
		    whoami, status);

		if (status == NSS_SUCCESS) {
			int ttl;

			/*
			 * data found in name service
			 * update cache
			 */
			status = dup_packed_buffer(largs, this_entry);
			if (status != NSS_SUCCESS) {
				delete_entry(nscdb, ctx, this_entry);
				(void) mutex_unlock(&nscdb->db_mutex);
				NSC_LOOKUP_LOG(ERROR,
				    "%s: failed to update cache\n");
				return (SERVERERROR);
			}

			/*
			 * store unpacked key in cache
			 */
			status = nss_packed_getkey(this_entry->buffer,
			    this_entry->bufsize,
			    &dbname, &dbop, &args);
			if (status != NSS_SUCCESS) {
				delete_entry(nscdb, ctx, this_entry);
				(void) mutex_unlock(&nscdb->db_mutex);
				NSC_LOOKUP_LOG(ERROR,
				    "%s: failed to extract key\n");
				return (SERVERERROR);
			}
			this_entry->key = args.key; /* struct copy */

			/* update +ve miss count */
			if (!(UPDATEBIT & flag)) {
				(void) mutex_lock(&ctx->stats_mutex);
				ctx->stats.pos_misses++;
				(void) mutex_unlock(&ctx->stats_mutex);
			}

			/* update +ve ttl */
			ttl = get_dns_ttl(largs->buffer, dbname);
			/* honor the dns ttl less than postive ttl */
			if (ttl < 0 || ttl > cfg.pos_ttl)
				ttl = cfg.pos_ttl;
			this_stats->timestamp = time(NULL) + ttl;

			/*
			 * start the revalidation and reaper threads
			 * if not already started
			 */
			start_threads(ctx);

			(void) mutex_unlock(&nscdb->db_mutex);
			NSC_LOOKUP_LOG(DEBUG,
			    "%s: cache updated with positive entry\n");
			return (SUCCESS);
		} else if (status == NSS_NOTFOUND) {
			/*
			 * data not found in name service
			 * update cache
			 */
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG_6)
			(me, "%s: name service lookup failed\n", whoami);

			if (NSCD_GET_ERRNO(largs->buffer) == ERANGE) {
				delete_entry(nscdb, ctx, this_entry);
				(void) mutex_unlock(&nscdb->db_mutex);
				NSC_LOOKUP_LOG(DEBUG,
				    "%s: ERANGE, cache not updated "
				    "with negative entry\n");
				return (NOTFOUND);
			}

			status = dup_packed_buffer(largs, this_entry);
			if (status != NSS_SUCCESS) {
				delete_entry(nscdb, ctx, this_entry);
				(void) mutex_unlock(&nscdb->db_mutex);
				NSC_LOOKUP_LOG(ERROR,
				    "%s: failed to update cache\n");
				return (SERVERERROR);
			}

			/* store unpacked key in cache */
			status = nss_packed_getkey(this_entry->buffer,
			    this_entry->bufsize,
			    &dbname, &dbop, &args);
			if (status != NSS_SUCCESS) {
				delete_entry(nscdb, ctx, this_entry);
				(void) mutex_unlock(&nscdb->db_mutex);
				NSC_LOOKUP_LOG(ERROR,
				    "%s: failed to extract key\n");
				return (SERVERERROR);
			}
			this_entry->key = args.key; /* struct copy */

			/* update -ve ttl */
			this_stats->timestamp = time(NULL) + cfg.neg_ttl;

			/* update -ve miss count */
			if (!(UPDATEBIT & flag)) {
				(void) mutex_lock(&ctx->stats_mutex);
				ctx->stats.neg_misses++;
				(void) mutex_unlock(&ctx->stats_mutex);
			}

			/*
			 * start the revalidation and reaper threads
			 * if not already started
			 */
			start_threads(ctx);

			(void) mutex_unlock(&nscdb->db_mutex);
			NSC_LOOKUP_LOG(DEBUG,
			    "%s: cache updated with negative entry\n");
			return (NOTFOUND);
		} else {
			/*
			 * name service lookup failed
			 */
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG_6)
			(me, "%s: name service lookup failed\n", whoami);

			errnum = NSCD_GET_ERRNO(largs->buffer);
			if (delete == nscd_true)
				delete_entry(nscdb, ctx, this_entry);
			else
				this_stats->status = ST_DISCARD;

			(void) mutex_unlock(&nscdb->db_mutex);
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
			(me, "%s: name service lookup failed "
			    "(status=%d, errno=%d)\n",
			    whoami, status, errnum);

			return (SERVERERROR);
		}
	} else if (next_action == _NSC_USECACHED) {
		/*
		 * found entry in cache
		 */
		if (UPDATEBIT & flag) {
			(void) mutex_unlock(&nscdb->db_mutex);
			NSC_LOOKUP_LOG(DEBUG, "%s: no need to update\n");
			return (SUCCESS);
		}

		if (NSCD_GET_STATUS((nss_pheader_t *)this_entry->buffer) ==
		    NSS_SUCCESS) {
			/* positive hit */
			(void) mutex_lock(&ctx->stats_mutex);
			ctx->stats.pos_hits++;
			(void) mutex_unlock(&ctx->stats_mutex);

			/* update response buffer */
			if (copy_result(largs->buffer,
			    this_entry->buffer) != NSS_SUCCESS) {
				(void) mutex_unlock(&nscdb->db_mutex);
				NSC_LOOKUP_LOG(ERROR,
				    "%s: response buffer insufficient\n");
				return (SERVERERROR);
			}

			(void) mutex_unlock(&nscdb->db_mutex);
			NSC_LOOKUP_LOG(DEBUG,
			    "%s: positive entry in cache\n");
			return (SUCCESS);
		} else {
			/* negative hit */
			(void) mutex_lock(&ctx->stats_mutex);
			ctx->stats.neg_hits++;
			(void) mutex_unlock(&ctx->stats_mutex);

			NSCD_SET_STATUS((nss_pheader_t *)largs->buffer,
			    NSCD_GET_STATUS(this_entry->buffer),
			    NSCD_GET_ERRNO(this_entry->buffer));
			NSCD_SET_HERRNO((nss_pheader_t *)largs->buffer,
			    NSCD_GET_HERRNO(this_entry->buffer));

			(void) mutex_unlock(&nscdb->db_mutex);
			NSC_LOOKUP_LOG(DEBUG,
			    "%s: negative entry in cache\n");
			return (NOTFOUND);
		}
	}

	(void) mutex_unlock(&nscdb->db_mutex);
	NSC_LOOKUP_LOG(ERROR, "%s: cache backend failure\n");
	return (SERVERERROR);
}

/*
 * NSCD cache backend lookup function
 */
/*ARGSUSED*/
void
nsc_lookup(nsc_lookup_args_t *largs, int flag)
{

	nss_pheader_t	*phdr = (nss_pheader_t *)largs->buffer;
	int		rc;

	rc = lookup_int(largs, 0);

	if (NSCD_GET_STATUS(phdr) == NSS_TRYLOCAL)
		return;

	switch (rc) {

	case SUCCESS:
		NSCD_SET_STATUS(phdr, NSS_SUCCESS, 0);
		break;

	case NOTFOUND:
		NSCD_SET_STATUS(phdr, NSS_NOTFOUND, -1);
		break;

	case SERVERERROR:
		/*
		 * status and errno should have been set in the phdr,
		 * if not, set status to NSS_ERROR
		 */
		if (NSCD_STATUS_IS_OK(phdr)) {
			NSCD_SET_STATUS(phdr, NSS_ERROR, 0);
		}
		break;

	case NOSERVER:
		NSCD_SET_STATUS(phdr, NSS_TRYLOCAL, -1);
		break;
	}
}


static nsc_ctx_t *
init_cache_ctx(int i)
{
	nsc_ctx_t	*ctx;

	ctx = calloc(1, sizeof (nsc_ctx_t));
	if (ctx == NULL)
		return (NULL);

	/* init locks and semaphores */
	(void) mutex_init(&ctx->file_mutex, USYNC_THREAD, NULL);
	(void) rwlock_init(&ctx->cfg_rwlp, USYNC_THREAD, NULL);
	(void) mutex_init(&ctx->stats_mutex, USYNC_THREAD, NULL);
	(void) _nscd_init_cache_sema(&ctx->throttle_sema, cache_name[i]);
	cache_init_ctx[i](ctx);
	cache_ctx_p[i] = ctx;

	return (ctx);
}


static void *
revalidate(void *arg)
{
	nsc_ctx_t *ctx = arg;

	(void) thr_setname(thr_self(), "revalidate");

	for (;;) {
		int i, slp, interval, count;

		(void) rw_rdlock(&ctx->cfg_rwlp);
		slp = ctx->cfg.pos_ttl;
		count = ctx->cfg.keephot;
		(void) rw_unlock(&ctx->cfg_rwlp);

		if (slp < 60)
			slp = 60;
		if (count != 0) {
			interval = (slp/2)/count;
			if (interval == 0)
				interval = 1;
			(void) sleep(slp*2/3);
			for (i = 0; i < ctx->db_count; i++) {
				getxy_keepalive(ctx, ctx->nsc_db[i],
				    count, interval);
			}
		} else {
			(void) sleep(slp);
		}
	}
	return (NULL);
}


static void
getxy_keepalive(nsc_ctx_t *ctx, nsc_db_t *nscdb, int keep, int interval)
{
	nsc_keephot_t		*table;
	nsc_entry_t		*entry, *ptr;
	int			i;
	nsc_lookup_args_t	*largs;
	nss_pheader_t		*phdr;
	int			bufsiz;
	char			*me = "getxy_keepalive";

	/* we won't be here if keep == 0 so need to check that */

	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
	(me, "%s: keep alive\n", nscdb->name);

	if ((table = maken(keep)) == NULL) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
			(me, "memory allocation failure\n");
		exit(1);
	}

	(void) mutex_lock(&nscdb->db_mutex);
	entry = nscdb->qtail;
	while (entry != NULL) {
		/* leave pending calls alone */
		if (!(entry->stats.status & ST_PENDING)) {
			/* do_revalidate */
			(void) insertn(table, entry->stats.hits, entry);
		}
		entry = entry->qnext;
	}
	for (i = 1; i <= keep; i++) {
		if (table[i].ptr == NULL)
			continue;
		ptr = (nsc_entry_t *)table[i].ptr;
		phdr = (nss_pheader_t *)ptr->buffer;
		if (NSCD_GET_STATUS(phdr) == NSS_SUCCESS)
			/*
			 * for positive cache, in addition to the packed
			 * header size, allocate twice the size of the
			 * existing result (in case the result grows
			 * larger) plus 2K (for the file/compat backend to
			 * process a possible large entry in the /etc files)
			 */
			bufsiz = phdr->data_off + 2 * phdr->data_len + 2048;
		else
			/*
			 * for negative cache, allocate 8K buffer to
			 * hold result in case the next lookup may
			 * return something (in addition to the
			 * packed header size)
			 */
			bufsiz = phdr->data_off + 8096;
		table[i].ptr = malloc(bufsiz);
		if (table[i].ptr == NULL) {
			(void) mutex_unlock(&nscdb->db_mutex);
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
				(me, "memory allocation failure\n");
			exit(1);
		}
		(void) memcpy(table[i].ptr, ptr->buffer,  ptr->bufsize);
		((nss_pheader_t *)table[i].ptr)->pbufsiz = bufsiz;
		table[i].num = bufsiz;
	}
	(void) mutex_unlock(&nscdb->db_mutex);

	/* launch update thread for each keep hot entry */
	for (i = keep; i > 0; i--) {
		if (table[i].ptr == NULL)
			continue; /* unused slot in table */
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "%s: launching update\n", nscdb->name);
		largs = (nsc_lookup_args_t *)malloc(sizeof (*largs));
		if (largs == NULL) {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
				(me, "memory allocation failure\n");
			exit(1);
		}
		largs->buffer = table[i].ptr;
		largs->bufsize = table[i].num;
		largs->ctx = ctx;
		largs->nscdb = nscdb;
		if (launch_update(largs) < 0)
			exit(1);
		(void) sleep(interval);
	}

	/*
	 * The update thread will handle freeing of buffer and largs.
	 * Free the table here.
	 */
	free(table);
}


static int
launch_update(nsc_lookup_args_t *in)
{
	char	*me = "launch_update";
	int	errnum;

	errnum = thr_create(NULL, 0, do_update, in, 0|THR_DETACHED, NULL);
	if (errnum != 0) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
		(me, "%s: thread creation failure (%d)\n",
		    in->nscdb->name, errnum);
		return (-1);
	}
	return (0);
}


static void *
do_update(void *arg)
{
	nsc_lookup_args_t *in = arg;
	nss_pheader_t	*phdr = (nss_pheader_t *)in->buffer;

	(void) thr_setname(thr_self(), "do_update");

	/* update the length of the data buffer */
	phdr->data_len = phdr->pbufsiz - phdr->data_off;

	(void) lookup_int(in, UPDATEBIT);
	if (in->buffer)
		free(in->buffer);
	free(in);
	return (NULL);
}


/*
 * Invalidate cache
 */
void
nsc_invalidate(nsc_ctx_t *ctx, char *dbname, nsc_ctx_t **ctxs)
{
	int	i;
	char	*me = "nsc_invalidate";

	if (ctx) {
		ctx_invalidate(ctx);
		return;
	}

	if (dbname) {
		if ((i = get_cache_idx(dbname)) == -1) {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
			(me, "%s: invalid cache name\n", dbname);
			return;
		}
		if ((ctx = cache_ctx_p[i]) == NULL)  {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_WARNING)
			(me, "%s: no cache context found\n",
			    dbname);
			return;
		}
		ctx_invalidate(ctx);
		return;
	}

	if (ctxs == NULL)
		ctxs =  cache_ctx_p;

	for (i = 0; i < CACHE_CTX_COUNT; i++) {
		if (ctxs[i] != NULL)
		ctx_invalidate(ctxs[i]);
	}
}


/*
 * Invalidate cache by context
 */
static void
ctx_invalidate(nsc_ctx_t *ctx)
{
	int		i;
	nsc_entry_t	*entry;
	char		*me = "ctx_invalidate";

	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
	(me, "%s: invalidate cache\n", ctx->dbname);

	for (i = 0; i < ctx->db_count; i++) {
		if (ctx->nsc_db[i] == NULL)
			continue;
		(void) mutex_lock(&ctx->nsc_db[i]->db_mutex);
		entry = ctx->nsc_db[i]->qtail;
		while (entry != NULL) {
			/* leave pending calls alone */
			if (!(entry->stats.status & ST_PENDING))
				entry->stats.status = ST_DISCARD;
			entry = entry->qnext;
		}
		(void) mutex_unlock(&ctx->nsc_db[i]->db_mutex);
	}

	(void) mutex_lock(&ctx->stats_mutex);
	ctx->stats.invalidate_count++;
	(void) mutex_unlock(&ctx->stats_mutex);
}


/*
 * Free nsc_entry_t
 *
 * Pre-reqs:
 * nscdb->db_mutex lock must be held before calling this function
 */
static void
delete_entry(nsc_db_t *nscdb, nsc_ctx_t *ctx, nsc_entry_t *entry)
{
	uint_t		hash;

	avl_remove(&nscdb->tree, entry);
	HASH_REMOVE(nscdb, entry, hash, nscd_false);
	queue_remove(nscdb, entry);
	if (entry->buffer != NULL) {
		free(entry->buffer);
		entry->buffer = NULL;
	}
	umem_cache_free(nsc_entry_cache, entry);
	(void) mutex_lock(&ctx->stats_mutex);
	ctx->stats.entries--;
	(void) mutex_unlock(&ctx->stats_mutex);
}


static nscd_rc_t
lookup_cache(nsc_lookup_args_t *largs, nscd_cfg_cache_t *cfgp,
    nss_XbyY_args_t *argp, char *whoami, nsc_entry_t **entry)
{
	nsc_db_t	*nscdb;
	nsc_ctx_t	*ctx;
	uint_t		hash;
	avl_index_t	pos;
	ulong_t		nentries;
	nsc_entry_t	find_entry, *node;
	char		*me = "lookup_cache";

	ctx = largs->ctx;
	nscdb = largs->nscdb;

	/* set the search key */
	find_entry.key = argp->key;	/* struct copy (not deep) */

	/* lookup the hash table ==> O(1) */
	if (nscdb->htable) {
		*entry = hash_find(nscdb, &find_entry, &hash, nscd_true);
		if (*entry != NULL) {
			(void) queue_adjust(nscdb, *entry);
			return (NSCD_SUCCESS);
		}
	}

	/* if not found, lookup the AVL tree ==> O(log n) */
	*entry = (nsc_entry_t *)avl_find(&nscdb->tree, &find_entry, &pos);
	if (*entry != NULL) {
		(void) queue_adjust(nscdb, *entry);
		/* move it to the hash table */
		if (nscdb->htable) {
			if (nscdb->htable[hash] == NULL ||
			    (*entry)->stats.hits >=
			    nscdb->htable[hash]->stats.hits) {
				nscdb->htable[hash] = *entry;
			}
		}
		return (NSCD_SUCCESS);
	}

	/* entry not found in the cache */
	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "%s: cache miss\n", whoami);

	if (cfgp->avoid_ns == nscd_true) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
			(me, "%s: avoid name service\n", whoami);
		return (NSCD_DB_ENTRY_NOT_FOUND);
	}

	/* allocate memory for new entry (stub) */
	*entry = (nsc_entry_t *)umem_cache_alloc(nsc_entry_cache,
	    UMEM_DEFAULT);
	if (*entry == NULL) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
			(me, "%s: memory allocation failure\n", whoami);
		return (NSCD_NO_MEMORY);
	}
	(void) memset(*entry, 0, sizeof (**entry));

	/*
	 * Note that the actual data for the key is stored within
	 * the largs->buffer (input buffer to nsc_lookup).
	 * find_entry.key only contains pointers to this data.
	 *
	 * If largs->buffer will be re-allocated by nss_psearch
	 * then (*entry)->key will have dangling pointers.
	 * In such case, the following assignment needs to be
	 * replaced by code that duplicates the key.
	 */
	(*entry)->key = find_entry.key;

	/*
	 * Add the entry to the cache.
	 */
	avl_insert(&nscdb->tree, *entry, pos);	/* O(log n) */
	(void) queue_adjust(nscdb, *entry);	/* constant */
	if (nscdb->htable)			/* constant */
		nscdb->htable[hash] = *entry;
	(*entry)->stats.status = ST_NEW_ENTRY;

	(void) mutex_lock(&ctx->stats_mutex);
	nentries = ++(ctx->stats.entries);
	(void) mutex_unlock(&ctx->stats_mutex);

	/* Have we exceeded max entries ? */
	if (cfgp->maxentries > 0 && nentries > cfgp->maxentries) {
		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
			(me, "%s: maximum entries exceeded -- "
			    "deleting least recently used entry\n",
			    whoami);

		node = nscdb->qhead;
		while (node != NULL && node != *entry) {
			if (node->stats.status == ST_DISCARD ||
			    !(node->stats.status & ST_PENDING)) {
				delete_entry(nscdb, ctx, node);
				break;
			}
			node = node->qprev;
		}

		/*
		 * It's okay if we were not able to find one to delete.
		 * The reaper (when invoked) will return the cache to a
		 * safe level.
		 */
	}

	return (NSCD_SUCCESS);
}

static void *
reaper(void *arg)
{
	nsc_ctx_t	*ctx = arg;
	uint_t		ttl, extra_sleep, total_sleep, intervals;
	uint_t		nodes_per_interval, seconds_per_interval;
	ulong_t		nsc_entries;
	char		*me = "reaper";

	(void) thr_setname(thr_self(), me);

	for (;;) {
		(void) mutex_lock(&ctx->stats_mutex);
		nsc_entries = ctx->stats.entries;
		(void) mutex_unlock(&ctx->stats_mutex);

		(void) rw_rdlock(&ctx->cfg_rwlp);
		ttl = ctx->cfg.pos_ttl;
		(void) rw_unlock(&ctx->cfg_rwlp);

		if (nsc_entries == 0) {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
				(me, "%s: nothing to reap\n", ctx->dbname);

			/* sleep for atleast 60 seconds */
			if (ttl < 60)
				ttl = 60;
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
			(me, "%s: sleep %d\n", ctx->dbname, ttl);
			(void) sleep(ttl);
			continue;
		}

		if (ttl < 32) ttl = 32;
		if (ttl > (1<<28)) ttl = 1<<28;

		/*
		 * minimum nodes_per_interval = 256 or 1<<8
		 * maximum nodes_per_interval = nsc_entries
		 * minimum seconds_per_interval = 32 or 1<<5
		 * maximum_seconds_per_interval = ttl
		 */
		if (nsc_entries <= ttl) {
			intervals = (nsc_entries >> 8) + 1;
			seconds_per_interval = ttl / intervals;
			nodes_per_interval = 256;
		} else {
			intervals = (ttl >> 5) + 1;
			seconds_per_interval = 32;
			nodes_per_interval = nsc_entries / intervals;
			if (nodes_per_interval < 256)
				nodes_per_interval = 256;
		}

		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
			(me, "%s: total entries = %d, "
			    "seconds per interval = %d, "
			    "nodes per interval = %d\n",
			    ctx->dbname, nsc_entries, seconds_per_interval,
			    nodes_per_interval);
		total_sleep = reap_cache(ctx, nodes_per_interval,
		    seconds_per_interval);
		extra_sleep = 1 + ttl - total_sleep;
		if (extra_sleep > 0)
			(void) sleep(extra_sleep);
	}
	return (NULL);
}


static uint_t
reap_cache(nsc_ctx_t *ctx, uint_t nodes_per_interval,
    uint_t seconds_per_interval)
{
	uint_t		nodes_togo, total_sleep;
	time_t		now;
	nsc_entry_t	*node, *next_node;
	nsc_db_t	*nscdb;
	uint_t		primes[] = {_NSC_HTSIZE_PRIMES};
	ulong_t		count, nentries, maxentries;
	int		i, slot, value, newhtsize;
	char		*me = "reap_cache";

	count = 0;
	total_sleep = 0;
	nodes_togo = nodes_per_interval;
	now = time(NULL);

	for (i = 0; i < ctx->db_count; i++) {
		nscdb = ctx->nsc_db[i];
		(void) mutex_lock(&nscdb->db_mutex);
		nscdb->reap_node = nscdb->qtail;
		while (nscdb->reap_node != NULL) {
			if (nodes_togo == 0) {
				(void) mutex_unlock(&nscdb->db_mutex);
				(void) sleep(seconds_per_interval);
				total_sleep += seconds_per_interval;
				nodes_togo = nodes_per_interval;
				now = time(NULL);
				(void) mutex_lock(&nscdb->db_mutex);
			}
			/* delete ST_DISCARD and expired nodes */
			if ((node = nscdb->reap_node) == NULL)
				break;
			if (node->stats.status == ST_DISCARD ||
			    (!(node->stats.status & ST_PENDING) &&
			    node->stats.timestamp < now)) {
				/*
				 * Delete entry if its discard flag is
				 * set OR if it has expired. Entries
				 * with pending updates are not
				 * deleted.
				 * nscdb->reap_node will be adjusted
				 * by delete_entry()
				 */
				delete_entry(nscdb, ctx, node);
				count++;
			} else {
				nscdb->reap_node = node->qnext;
			}
			nodes_togo--;
		}

		if (nscdb->htsize == 0) {
			(void) mutex_unlock(&nscdb->db_mutex);
			continue;
		}

		/*
		 * Dynamic adjustment of hash table size.
		 *
		 * Hash table size is roughly 1/8th of the
		 * total entries. However the size is changed
		 * only when the number of entries double or
		 * reduced by half
		 */
		nentries = avl_numnodes(&nscdb->tree);
		for (slot = 0, value = _NSC_INIT_HTSIZE_SLOT_VALUE;
		    slot < _NSC_HTSIZE_NUM_SLOTS && nentries > value;
		    value = (value << 1) + 1, slot++)
			;
		if (nscdb->hash_type == nsc_ht_power2)
			newhtsize = _NSC_INIT_HTSIZE_POWER2 << slot;
		else
			newhtsize = primes[slot];

		/* Recommended size is same as the current size. Done */
		if (nscdb->htsize == newhtsize) {
			(void) mutex_unlock(&nscdb->db_mutex);
			continue;
		}

		_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
			(me, "%s: resizing hash table from %d to %d\n",
			    nscdb->name, nscdb->htsize, newhtsize);

		/*
		 * Dump old hashes because it would be time
		 * consuming to rehash them.
		 */
		(void) free(nscdb->htable);
		nscdb->htable = calloc(newhtsize, sizeof (*(nscdb->htable)));
		if (nscdb->htable == NULL) {
			_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_ERROR)
				(me, "%s: memory allocation failure\n",
				    nscdb->name);
			/* -1 to try later */
			nscdb->htsize = -1;
		} else {
			nscdb->htsize = newhtsize;
		}
		(void) mutex_unlock(&nscdb->db_mutex);
	}

	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "%s: reaped %lu entries\n", ctx->dbname, count);

	/*
	 * if cache is almost full then reduce it to a safe level by
	 * evicting LRU entries
	 */

	(void) rw_rdlock(&ctx->cfg_rwlp);
	maxentries = ctx->cfg.maxentries;
	(void) rw_unlock(&ctx->cfg_rwlp);

	/* No limit on number of entries. Done */
	if (maxentries == 0)
		goto out;

	(void) mutex_lock(&ctx->stats_mutex);
	nentries = ctx->stats.entries;
	(void) mutex_unlock(&ctx->stats_mutex);

	/* what is the percentage of cache used ? */
	value = (nentries * 100) / maxentries;
	if (value < _NSC_EVICTION_START_LEVEL)
		goto out;

	/*
	 * cache needs to be reduced to a safe level
	 */
	value -= _NSC_EVICTION_SAFE_LEVEL;
	for (i = 0, count = 0; i < ctx->db_count; i++) {
		/*
		 * Reduce each subcache by 'value' percent
		 */
		nscdb = ctx->nsc_db[i];
		(void) mutex_lock(&nscdb->db_mutex);
		nodes_togo = (value * avl_numnodes(&nscdb->tree)) / 100;

		/* Start from LRU entry i.e queue head */
		next_node = nscdb->qhead;
		while (nodes_togo > 0 && next_node != NULL) {
			node = next_node;
			next_node = next_node->qprev;
			if (node->stats.status == ST_DISCARD ||
			    !(node->stats.status & ST_PENDING)) {
				/* Leave nodes with pending updates alone  */
				delete_entry(nscdb, ctx, node);
				count++;
				nodes_togo--;
			}
		}
		(void) mutex_unlock(&nscdb->db_mutex);
	}

	_NSCD_LOG(NSCD_LOG_CACHE, NSCD_LOG_LEVEL_DEBUG)
		(me, "%s: evicted %lu LRU entries\n", ctx->dbname, count);

out:
	return (total_sleep);
}