summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/brand/lx/sysfs/lx_sysvnops.c
blob: 10c99baa7b7c1b0a11649cb2e492b06b0029e0d5 (plain)
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
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2017 Joyent, Inc.
 */

/*
 * lx_sysfs -- a Linux-compatible /sys for the LX brand
 */

#include <vm/seg_vn.h>
#include <sys/sdt.h>
#include <sys/strlog.h>
#include <sys/stropts.h>
#include <sys/cmn_err.h>
#include <sys/lx_brand.h>
#include <sys/x86_archext.h>
#include <sys/archsystm.h>
#include <sys/fp.h>
#include <sys/pool_pset.h>
#include <sys/pset.h>
#include <sys/zone.h>
#include <sys/pghw.h>
#include <sys/vfs_opreg.h>
#include <sys/param.h>
#include <sys/utsname.h>
#include <sys/lx_misc.h>
#include <sys/brand.h>
#include <sys/cred_impl.h>
#include <sys/tihdr.h>
#include <sys/sunddi.h>
#include <sys/vnode.h>
#include <sys/netstack.h>
#include <sys/ethernet.h>
#include <inet/ip_arp.h>

#include "lx_sysfs.h"

/*
 * Pointer to the vnode ops vector for this fs.
 * This is instantiated in lxsys_init() in lx_sysvfsops.c
 */
vnodeops_t *lxsys_vnodeops;

static int lxsys_open(vnode_t **, int, cred_t *, caller_context_t *);
static int lxsys_close(vnode_t *, int, int, offset_t, cred_t *,
    caller_context_t *);
static int lxsys_read(vnode_t *, uio_t *, int, cred_t *, caller_context_t *);
static int lxsys_getattr(vnode_t *, vattr_t *, int, cred_t *,
    caller_context_t *);
static int lxsys_access(vnode_t *, int, int, cred_t *, caller_context_t *);
static int lxsys_lookup(vnode_t *, char *, vnode_t **,
    pathname_t *, int, vnode_t *, cred_t *, caller_context_t *, int *,
    pathname_t *);
static int lxsys_readdir(vnode_t *, uio_t *, cred_t *, int *,
    caller_context_t *, int);
static int lxsys_readlink(vnode_t *, uio_t *, cred_t *, caller_context_t *);
static int lxsys_cmp(vnode_t *, vnode_t *, caller_context_t *);
static int lxsys_sync(void);
static void lxsys_inactive(vnode_t *, cred_t *, caller_context_t *);

static vnode_t *lxsys_lookup_static(lxsys_node_t *, char *);
static vnode_t *lxsys_lookup_class_netdir(lxsys_node_t *, char *);
static vnode_t *lxsys_lookup_devices_virtual_netdir(lxsys_node_t *, char *);
static vnode_t *lxsys_lookup_blockdir(lxsys_node_t *, char *);
static vnode_t *lxsys_lookup_devices_zfsdir(lxsys_node_t *, char *);
static vnode_t *lxsys_lookup_devices_syscpu(lxsys_node_t *, char *);
static vnode_t *lxsys_lookup_devices_syscpuinfo(lxsys_node_t *, char *);
static vnode_t *lxsys_lookup_devices_sysnode(lxsys_node_t *, char *);

static int lxsys_read_static(lxsys_node_t *, lxsys_uiobuf_t *);
static int lxsys_read_devices_virtual_net(lxsys_node_t *, lxsys_uiobuf_t *);
static int lxsys_read_devices_zfs_block(lxsys_node_t *, lxsys_uiobuf_t *);
static int lxsys_read_devices_syscpu(lxsys_node_t *, lxsys_uiobuf_t *);
static int lxsys_read_devices_sysnode(lxsys_node_t *, lxsys_uiobuf_t *);

static int lxsys_readdir_devices_syscpu(lxsys_node_t *, uio_t *, int *);
static int lxsys_readdir_devices_syscpuinfo(lxsys_node_t *, uio_t *, int *);
static int lxsys_readdir_devices_sysnode(lxsys_node_t *, uio_t *, int *);
static int lxsys_readdir_static(lxsys_node_t *, uio_t *, int *);
static int lxsys_readdir_class_netdir(lxsys_node_t *, uio_t *, int *);
static int lxsys_readdir_devices_virtual_netdir(lxsys_node_t *, uio_t *, int *);
static int lxsys_readdir_blockdir(lxsys_node_t *, uio_t *, int *);
static int lxsys_readdir_devices_zfsdir(lxsys_node_t *, uio_t *, int *);

static int lxsys_readlink_class_net(lxsys_node_t *, char *, size_t);
static int lxsys_readlink_block(lxsys_node_t *, char *, size_t);

/*
 * The lx /sys vnode operations vector
 */
const fs_operation_def_t lxsys_vnodeops_template[] = {
	VOPNAME_OPEN,		{ .vop_open = lxsys_open },
	VOPNAME_CLOSE,		{ .vop_close = lxsys_close },
	VOPNAME_READ,		{ .vop_read = lxsys_read },
	VOPNAME_GETATTR,	{ .vop_getattr = lxsys_getattr },
	VOPNAME_ACCESS,		{ .vop_access = lxsys_access },
	VOPNAME_LOOKUP,		{ .vop_lookup = lxsys_lookup },
	VOPNAME_READDIR,	{ .vop_readdir = lxsys_readdir },
	VOPNAME_READLINK,	{ .vop_readlink = lxsys_readlink },
	VOPNAME_FSYNC,		{ .error = lxsys_sync },
	VOPNAME_SEEK,		{ .error = lxsys_sync },
	VOPNAME_INACTIVE,	{ .vop_inactive = lxsys_inactive },
	VOPNAME_CMP,		{ .vop_cmp = lxsys_cmp },
	NULL,			NULL
};

typedef enum lxsys_cpu_state {
	LXSYS_CPU_ON,		/* online */
	LXSYS_CPU_OFF,		/* offline */
	LXSYS_CPU_ANY,		/* don't care */
} lxsys_cpu_state_t;

static void lxsys_format_cpu(char *, int, lxsys_cpu_state_t);

/*
 * Sysfs Inode format:
 * 0000AABBBBCC
 *
 * AA - TYPE
 * BBBB - INSTANCE
 * CC - ENDPOINT
 *
 * Where TYPE is one of:
 * 1 - SYS_STATIC
 * 2 - SYS_CLASS_NET
 * 3 - SYS_DEV_NET
 * 4 - SYS_BLOCK
 * 5 - SYS_DEV_ZFS
 * 6 - SYS_DEV_SYS_CPU
 * 7 - SYS_DEV_SYS_CPUINFO
 * 8 - SYS_DEV_SYS_NODE
 *
 * Static entries will have assigned INSTANCE identifiers:
 * - 0x00: /sys
 * - 0x01: /sys/class
 * - 0x02: /sys/devices
 * - 0x03: /sys/fs
 * - 0x04: /sys/class/net
 * - 0x05: /sys/devices/virtual
 * - 0x06: /sys/devices/system
 * - 0x07: /sys/fs/cgroup
 * - 0x08: /sys/devices/virtual/net
 * - 0x09: /sys/block
 * - 0x0a: /sys/devices/zfs
 * - 0x0b: /sys/devices/system/cpu
 * - 0x0c: /sys/devices/system/node
 * - 0x0d: /sys/bus
 *
 * Dynamic /sys/class/net/<interface> symlinks will use an INSTANCE derived
 * from the corresonding ifindex.
 *
 * Dynamic /sys/devices/virtual/net/<interface>/<entries> directories will use
 * an INSTANCE derived from the ifindex and statically assigned ENDPOINT IDs
 * for the contained entries.
 *
 * Dynamic /sys/block/<dev> symlinks will use an INSTANCE derived from the
 * device major and instance from records listed in kstat or zvols.
 *
 * Dynamic /sys/devices/zfs/<dev> directories will use an INSTANCE derived from
 * the emulated minor number.
 *
 * Semi-static/Dynamic /sys/devices/system/cpu contains the fixed 'kernel_max',
 * 'offline', 'online', 'possible', and 'present' files, and a dynamic set of
 * cpuN subdirectories. All of these are dynamic nodes.
 *
 * Static /sys/devices/system/node/node0 currently only contains a
 * static cpulist file, but will likely need future dynamic entries for cpuN
 * symlinks, and perhaps other static files. By only providing 'node0' we
 * pretend that there is only a single NUMA node available to a zone (trying to
 * be NUMA-aware inside a zone is generally not going to work anyway).
 * If dynamic entries are added under node0, it must be converted to the
 * semi-static/dynamic approach as used under /sys/devices/system/cpu.
 *
 * The dyn_ino_type table must be updated whenever a new static instance is
 * defined.
 */

#define	LXSYS_INST_CLASSDIR			0x1
#define	LXSYS_INST_DEVICESDIR			0x2
#define	LXSYS_INST_FSDIR			0x3
#define	LXSYS_INST_CLASS_NETDIR			0x4
#define	LXSYS_INST_DEVICES_VIRTUALDIR		0x5
#define	LXSYS_INST_DEVICES_SYSTEMDIR		0x6
#define	LXSYS_INST_FS_CGROUPDIR			0x7
#define	LXSYS_INST_DEVICES_VIRTUAL_NETDIR	0x8
#define	LXSYS_INST_BLOCKDIR			0x9
#define	LXSYS_INST_DEVICES_ZFSDIR		0xa
#define	LXSYS_INST_DEVICES_SYSCPU		0xb
#define	LXSYS_INST_DEVICES_SYSNODE		0xc
#define	LXSYS_INST_BUSDIR			0xd
#define	LXSYS_INST_MAX				LXSYS_INST_BUSDIR /* limit */

/*
 * These are of dynamic type (LXSYS_DEV_SYS_CPU), but essentially fixed
 * instances. Under /sys/devices/system/cpu we have: kernel_max, offline,
 * online, possible and present. We also have a dynamic set of cpuN subdirs.
 * The cpuN subdirs are actually of type LXSYS_DEV_SYS_CPUINFO, so we can use
 * the following instance IDs for the fixed files.
 */
#define	LXSYS_INST_DEV_SYSCPU_KMAX		0x1
#define	LXSYS_INST_DEV_SYSCPU_OFFLINE		0x2
#define	LXSYS_INST_DEV_SYSCPU_ONLINE		0x3
#define	LXSYS_INST_DEV_SYSCPU_POSSIBLE		0x4
#define	LXSYS_INST_DEV_SYSCPU_PRESENT		0x5

/*
 * This array is used for directory inode correction in lxsys_readdir_common
 * when a directory's static-type entry is actually a dynamic-type.
 */
static int dyn_ino_type [] = {
	0,				/* invalid */
	0,				/* LXSYS_INST_CLASSDIR */
	0,				/* LXSYS_INST_DEVICESDIR */
	0,				/* LXSYS_INST_FSDIR */
	LXSYS_CLASS_NET,		/* LXSYS_INST_CLASS_NETDIR */
	0,				/* LXSYS_INST_DEVICES_VIRTUALDIR */
	0,				/* LXSYS_INST_DEVICES_SYSTEMDIR */
	0,				/* LXSYS_INST_FS_CGROUPDIR */
	LXSYS_DEV_NET,			/* LXSYS_INST_DEV_VIRTUAL_NETDIR */
	LXSYS_BLOCK,			/* LXSYS_INST_BLOCKDIR */
	LXSYS_DEV_ZFS,			/* LXSYS_INST_DEVICES_ZFSDIR */
	LXSYS_DEV_SYS_CPU,		/* LXSYS_INST_DEVICES_SYSCPU */
	LXSYS_DEV_SYS_NODE,		/* LXSYS_INST_DEV_SYSNODE */
	0,				/* LXSYS_INST_BUSDIR */
};
#define	DYN_INO_LEN \
	(sizeof (dyn_ino_type) / sizeof ((dyn_ino_type)[0]))

/*
 * file contents of an lx /sys directory.
 */
static lxsys_dirent_t dirlist_root[] = {
	{ LXSYS_INST_BLOCKDIR,		"block" },
	{ LXSYS_INST_BUSDIR,		"bus" },
	{ LXSYS_INST_CLASSDIR,		"class" },
	{ LXSYS_INST_DEVICESDIR,	"devices" },
	{ LXSYS_INST_FSDIR,		"fs" }
};
static lxsys_dirent_t dirlist_class[] = {
	{ LXSYS_INST_CLASS_NETDIR,	"net" }
};
static lxsys_dirent_t dirlist_fs[] = {
	{ LXSYS_INST_FS_CGROUPDIR,	"cgroup" }
};
static lxsys_dirent_t dirlist_devices[] = {
	{ LXSYS_INST_DEVICES_SYSTEMDIR,		"system" },
	{ LXSYS_INST_DEVICES_VIRTUALDIR,	"virtual" },
	{ LXSYS_INST_DEVICES_ZFSDIR,		"zfs" }
};
static lxsys_dirent_t dirlist_devices_virtual[] = {
	{ LXSYS_INST_DEVICES_VIRTUAL_NETDIR,	"net" }
};

static lxsys_dirent_t dirlist_devices_system[] = {
	{ LXSYS_INST_DEVICES_SYSCPU,	"cpu" },
	{ LXSYS_INST_DEVICES_SYSNODE,	"node" }
};

#define	LXSYS_ENDP_NET_ADDRESS	1
#define	LXSYS_ENDP_NET_ADDRLEN	2
#define	LXSYS_ENDP_NET_FLAGS	3
#define	LXSYS_ENDP_NET_IFINDEX	4
#define	LXSYS_ENDP_NET_MTU	5
#define	LXSYS_ENDP_NET_TXQLEN	6
#define	LXSYS_ENDP_NET_TYPE	7

#define	LXSYS_ENDP_BLOCK_DEVICE	1

#define	LXSYS_ENDP_NODE_CPULIST	1
#define	LXSYS_ENDP_NODE_CPUMAP	2

static lxsys_dirent_t dirlist_devices_virtual_net[] = {
	{ LXSYS_ENDP_NET_ADDRESS,	"address" },
	{ LXSYS_ENDP_NET_ADDRLEN,	"addr_len" },
	{ LXSYS_ENDP_NET_FLAGS,		"flags" },
	{ LXSYS_ENDP_NET_IFINDEX,	"ifindex" },
	{ LXSYS_ENDP_NET_MTU,		"mtu" },
	{ LXSYS_ENDP_NET_TXQLEN,	"tx_queue_len" },
	{ LXSYS_ENDP_NET_TYPE,		"type" }
};

static lxsys_dirent_t dirlist_devices_zfs_block[] = {
	{ LXSYS_ENDP_BLOCK_DEVICE,	"device" }
};

static lxsys_dirent_t dirlist_devices_sysnode[] = {
	{ LXSYS_ENDP_NODE_CPULIST,	"cpulist" },
	{ LXSYS_ENDP_NODE_CPUMAP,	"cpumap" }
};

#define	SYSDIRLISTSZ(l)	(sizeof (l) / sizeof ((l)[0]))

#define	SYSDLENT(i, l)	{ i, l, SYSDIRLISTSZ(l) }
static lxsys_dirlookup_t lxsys_dirlookup[] = {
	SYSDLENT(LXSYS_INST_ROOT, dirlist_root),
	SYSDLENT(LXSYS_INST_CLASSDIR, dirlist_class),
	SYSDLENT(LXSYS_INST_FSDIR, dirlist_fs),
	{ LXSYS_INST_FS_CGROUPDIR, NULL, 0 },
	SYSDLENT(LXSYS_INST_DEVICESDIR, dirlist_devices),
	SYSDLENT(LXSYS_INST_DEVICES_SYSTEMDIR, dirlist_devices_system),
	SYSDLENT(LXSYS_INST_DEVICES_VIRTUALDIR, dirlist_devices_virtual),
	SYSDLENT(LXSYS_INST_DEVICES_SYSNODE, dirlist_devices_sysnode),
	{ LXSYS_INST_BUSDIR, NULL, 0 },
};


/*
 * Array of lookup functions, indexed by lx /sys file type.
 */
static vnode_t *(*lxsys_lookup_function[LXSYS_MAXTYPE])() = {
	NULL,					/* LXSYS_NONE		*/
	lxsys_lookup_static,			/* LXSYS_STATIC		*/
	lxsys_lookup_class_netdir,		/* LXSYS_CLASS_NET	*/
	lxsys_lookup_devices_virtual_netdir,	/* LXSYS_DEV_NET	*/
	lxsys_lookup_blockdir,			/* LXSYS_BLOCK		*/
	lxsys_lookup_devices_zfsdir,		/* LXSYS_DEV_ZFS	*/
	lxsys_lookup_devices_syscpu,		/* LXSYS_DEV_SYS_CPU	*/
	lxsys_lookup_devices_syscpuinfo,	/* LXSYS_DEV_SYS_CPUINFO */
	lxsys_lookup_devices_sysnode,		/* LXSYS_DEV_SYS_NODE	*/
};

/*
 * Array of readdir functions, indexed by /sys file type.
 */
static int (*lxsys_readdir_function[LXSYS_MAXTYPE])() = {
	NULL,					/* LXSYS_NONE		*/
	lxsys_readdir_static,			/* LXSYS_STATIC		*/
	lxsys_readdir_class_netdir,		/* LXSYS_CLASS_NET	*/
	lxsys_readdir_devices_virtual_netdir,	/* LXSYS_DEV_NET	*/
	lxsys_readdir_blockdir,			/* LXSYS_BLOCK		*/
	lxsys_readdir_devices_zfsdir,		/* LXSYS_DEV_ZFS	*/
	lxsys_readdir_devices_syscpu,		/* LXSYS_DEV_SYS_CPU	*/
	lxsys_readdir_devices_syscpuinfo,	/* LXSYS_DEV_SYS_CPUINFO */
	lxsys_readdir_devices_sysnode,		/* LXSYS_DEV_SYS_NODE	*/
};

/*
 * Array of read functions, indexed by /sys file type.
 */
static int (*lxsys_read_function[LXSYS_MAXTYPE])() = {
	NULL,					/* LXSYS_NONE		*/
	lxsys_read_static,			/* LXSYS_STATIC		*/
	NULL,					/* LXSYS_CLASS_NET	*/
	lxsys_read_devices_virtual_net,		/* LXSYS_DEV_NET	*/
	NULL,					/* LXSYS_BLOCK		*/
	lxsys_read_devices_zfs_block,		/* LXSYS_DEV_ZFS	*/
	lxsys_read_devices_syscpu,		/* LXSYS_DEV_SYS_CPU	*/
	NULL,					/* LXSYS_DEV_SYS_CPUINFO */
	lxsys_read_devices_sysnode,		/* LXSYS_DEV_SYS_NODE	*/
};

/*
 * Array of readlink functions, indexed by /sys file type.
 */
static int (*lxsys_readlink_function[LXSYS_MAXTYPE])() = {
	NULL,					/* LXSYS_NONE		*/
	NULL,					/* LXSYS_STATIC		*/
	lxsys_readlink_class_net,		/* LXSYS_CLASS_NET	*/
	NULL,					/* LXSYS_DEV_NET	*/
	lxsys_readlink_block,			/* LXSYS_BLOCK		*/
	NULL,					/* LXSYS_DEV_ZFS	*/
	NULL,					/* LXSYS_DEV_SYS_CPU	*/
	NULL,					/* LXSYS_DEV_SYS_CPUINFO */
	NULL,					/* LXSYS_DEV_SYS_NODE	*/
};

/*
 * Given one of our inodes, return the vnode type.
 *
 * lxsys_getnode will always set the vnode type to VDIR. It expects the
 * caller (normally the lookup functions) to fix the type. Those same rules are
 * encoded here for our inode-to-type translation.
 */
int
lxsys_ino_get_type(ino_t ino)
{
	lxsys_nodetype_t type;
	unsigned int instance;
	unsigned int endpoint;

	type = (ino & 0xff000000) >> 24;
	instance = (ino & 0xffff00) >> 8;
	endpoint = (ino & 0xff);

	if (instance > LXSYS_INST_MAX)
		return (VNON);

	/* Validate non-static node types */
	if (type != LXSYS_STATIC &&
	    (type <= LXSYS_STATIC || type >= LXSYS_MAXTYPE)) {
		return (VNON);
	}

	if (type != LXSYS_STATIC) {
		/* Non-static node types */
		switch (type) {
		case LXSYS_CLASS_NET:
			if (instance != 0) {
				return (VLNK);
			}
			break;
		case LXSYS_DEV_NET:
			/*
			 * /sys/devices/virtual/net usually has the eth0 and
			 * lo directories. Each network device directory is an
			 * instances with a 0 endpoint. The files within
			 * that directory have a non-0 endpoint.
			 */
			if (endpoint != 0) {
				return (VREG);
			}
			break;
		case LXSYS_BLOCK:
			if (instance != 0) {
				return (VLNK);
			}
			break;
		case LXSYS_DEV_ZFS:
			/*
			 * /sys/devices/zfs usually has the zfsds0 directory
			 * instance with a 0 endpoint. The device file within
			 * that directory has a non-0 endpoint.
			 */
			if (endpoint != 0) {
				return (VREG);
			}
			break;
		case LXSYS_DEV_SYS_CPU:
			if (instance != 0) {
				return (VREG);
			}
			break;
		case LXSYS_DEV_SYS_CPUINFO:
			/*
			 * There is an instance of /sys/devices/system/cpu/cpuN
			 * for each CPU. These have an instance per CPU and
			 * currently the endpoint is 0 since there is nothing
			 * underneath the cpuN subdirectories. Future
			 * regular file entries are likely to be added there.
			 */
			if (endpoint != 0) {
				return (VREG);
			}
			break;
		case LXSYS_DEV_SYS_NODE:
			/*
			 * /sys/devices/system/node has the node0 directory
			 * instance with a 0 endpoint. The cpulist file within
			 * that directory has a non-0 endpoint.
			 */
			if (endpoint != 0) {
				return (VREG);
			}
			break;
		default:
			break;
		}
	}
	return (VDIR);
}

/*
 * lxsys_open(): Vnode operation for VOP_OPEN()
 */
/* ARGSUSED */
static int
lxsys_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
{
	/*
	 * We only allow reading in this file system
	 */
	if (flag & FWRITE)
		return (EROFS);

	return (0);
}


/*
 * lxsys_close(): Vnode operation for VOP_CLOSE()
 */
/* ARGSUSED */
static int
lxsys_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
    caller_context_t *ct)
{
	return (0);
}


/*
 * lxsys_read(): Vnode operation for VOP_READ()
 * All we currently have in this fs are directories.
 */
/* ARGSUSED */
static int
lxsys_read(vnode_t *vp, uio_t *uiop, int ioflag, cred_t *cr,
    caller_context_t *ct)
{
	lxsys_node_t *lnp = VTOLXS(vp);
	lxsys_nodetype_t type = lnp->lxsys_type;
	int (*rlfunc)();
	int error;
	lxsys_uiobuf_t *luio;

	VERIFY(type > LXSYS_NONE && type < LXSYS_MAXTYPE);

	if (vp->v_type == VDIR) {
		return (EISDIR);
	}

	rlfunc = lxsys_read_function[type];
	if (rlfunc != NULL) {
		luio = lxsys_uiobuf_new(uiop);
		if ((error = rlfunc(lnp, luio)) == 0) {
			error = lxsys_uiobuf_flush(luio);
		}
		lxsys_uiobuf_free(luio);
	} else {
		error = EIO;
	}

	return (error);
}

/*
 * lxsys_getattr(): Vnode operation for VOP_GETATTR()
 */
/* ARGSUSED */
static int
lxsys_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
    caller_context_t *ct)
{
	register lxsys_node_t *lxsnp = VTOLXS(vp);

	/* Default attributes, that may be overridden below */
	bzero(vap, sizeof (*vap));
	vap->va_atime = vap->va_mtime = vap->va_ctime = lxsnp->lxsys_time;
	vap->va_nlink = 1;
	vap->va_type = vp->v_type;
	vap->va_mode = lxsnp->lxsys_mode;
	vap->va_fsid = vp->v_vfsp->vfs_dev;
	vap->va_blksize = DEV_BSIZE;
	vap->va_uid = lxsnp->lxsys_uid;
	vap->va_gid = lxsnp->lxsys_gid;
	vap->va_nodeid = lxsnp->lxsys_ino;

	vap->va_nblocks = (fsblkcnt64_t)btod(vap->va_size);
	return (0);
}

/*
 * lxsys_access(): Vnode operation for VOP_ACCESS()
 */
/* ARGSUSED */
static int
lxsys_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
{
	lxsys_node_t *lxsnp = VTOLXS(vp);
	int shift = 0;

	/*
	 * Although our lx sysfs is basically a read only file system, Linux
	 * expects it to be writable so we can't just error if (mode & VWRITE).
	 */

	/* If user is root allow access regardless of permission bits */
	if (secpolicy_proc_access(cr) == 0)
		return (0);

	/*
	 * Access check is based on only one of owner, group, public.  If not
	 * owner, then check group.  If not a member of the group, then check
	 * public access.
	 */
	if (crgetuid(cr) != lxsnp->lxsys_uid) {
		shift += 3;
		if (!groupmember((uid_t)lxsnp->lxsys_gid, cr))
			shift += 3;
	}

	mode &= ~(lxsnp->lxsys_mode << shift);

	if (mode == 0)
		return (0);

	return (EACCES);
}

/*
 * lxsys_lookup(): Vnode operation for VOP_LOOKUP()
 */
/* ARGSUSED */
static int
lxsys_lookup(vnode_t *dp, char *comp, vnode_t **vpp, pathname_t *pathp,
    int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
    int *direntflags, pathname_t *realpnp)
{
	lxsys_node_t *lxsnp = VTOLXS(dp);
	lxsys_nodetype_t type = lxsnp->lxsys_type;
	int error;

	VERIFY(dp->v_type == VDIR);
	VERIFY(type > LXSYS_NONE && type < LXSYS_MAXTYPE);

	/*
	 * restrict lookup permission to owner or root
	 */
	if ((error = lxsys_access(dp, VEXEC, 0, cr, ct)) != 0) {
		return (error);
	}

	/*
	 * Just return the parent vnode if that's where we are trying to go.
	 */
	if (strcmp(comp, "..") == 0) {
		VN_HOLD(lxsnp->lxsys_parentvp);
		*vpp = lxsnp->lxsys_parentvp;
		return (0);
	}

	/*
	 * Special handling for directory searches.  Note: null component name
	 * denotes that the current directory is being searched.
	 */
	if ((dp->v_type == VDIR) && (*comp == '\0' || strcmp(comp, ".") == 0)) {
		VN_HOLD(dp);
		*vpp = dp;
		return (0);
	}

	*vpp = (lxsys_lookup_function[type](lxsnp, comp));
	return ((*vpp == NULL) ? ENOENT : 0);
}

static lxsys_node_t *
lxsys_lookup_disk(lxsys_node_t *ldp, char *comp, lxsys_nodetype_t type)
{
	lxsys_node_t *lnp = NULL;
	lx_zone_data_t *lxzdata;
	lx_virt_disk_t *vd;

	lxzdata = ztolxzd(curproc->p_zone);
	if (lxzdata == NULL)
		return (NULL);
	ASSERT(lxzdata->lxzd_vdisks != NULL);

	vd = list_head(lxzdata->lxzd_vdisks);
	while (vd != NULL) {
		int inst = getminor(vd->lxvd_emul_dev) & 0xffff;

		if (strcmp(vd->lxvd_name, comp) == 0 && inst != 0) {
			lnp = lxsys_getnode(ldp->lxsys_vnode, type, inst, 0);
			break;
		}

		vd = list_next(lxzdata->lxzd_vdisks, vd);
	}

	return (lnp);
}

static vnode_t *
lxsys_lookup_static(lxsys_node_t *ldp, char *comp)
{
	lxsys_dirent_t *dirent = NULL;
	int i, len = 0;

	for (i = 0; i < SYSDIRLISTSZ(lxsys_dirlookup); i++) {
		if (ldp->lxsys_instance == lxsys_dirlookup[i].dl_instance) {
			dirent = lxsys_dirlookup[i].dl_list;
			len = lxsys_dirlookup[i].dl_length;
			break;
		}
	}
	if (dirent == NULL) {
		return (NULL);
	}

	for (i = 0; i < len; i++) {
		if (strncmp(comp, dirent[i].d_name, MAXPATHLEN) == 0) {
			lxsys_nodetype_t node_type = ldp->lxsys_type;
			unsigned int node_instance = 0;
			lxsys_node_t *lnp;

			switch (dirent[i].d_idnum) {
			case LXSYS_INST_BLOCKDIR:
				node_type = LXSYS_BLOCK;
				break;
			case LXSYS_INST_CLASS_NETDIR:
				node_type = LXSYS_CLASS_NET;
				break;
			case LXSYS_INST_DEVICES_VIRTUAL_NETDIR:
				node_type = LXSYS_DEV_NET;
				break;
			case LXSYS_INST_DEVICES_ZFSDIR:
				node_type = LXSYS_DEV_ZFS;
				break;
			case LXSYS_INST_DEVICES_SYSCPU:
				node_type = LXSYS_DEV_SYS_CPU;
				break;
			case LXSYS_INST_DEVICES_SYSNODE:
				node_type = LXSYS_DEV_SYS_NODE;
				break;
			default:
				/* Another static node */
				node_instance = dirent[i].d_idnum;
			}
			if (node_type == LXSYS_STATIC) {
				lnp = lxsys_getnode_static(ldp->lxsys_vnode,
				    node_instance);
			} else {
				lnp = lxsys_getnode(ldp->lxsys_vnode,
				    node_type, node_instance, 0);
			}
			return (lnp->lxsys_vnode);
		}
	}
	return (NULL);
}

static vnode_t *
lxsys_lookup_class_netdir(lxsys_node_t *ldp, char *comp)
{
	vnode_t *result = NULL;
	lxsys_node_t *lnp;
	netstack_t *ns;
	ip_stack_t *ipst;
	avl_tree_t *phytree;
	phyint_t *phyi;
	char ifname[LIFNAMSIZ];

	if (ldp->lxsys_type != LXSYS_CLASS_NET ||
	    ldp->lxsys_instance != 0) {
		/* Lookups only allowed at directory level */
		return (NULL);
	}

	(void) strncpy(ifname, comp, LIFNAMSIZ);
	lx_ifname_convert(ifname, LX_IF_TONATIVE);

	if ((ns = lxsys_netstack(ldp)) == NULL) {
		return (NULL);
	}
	ipst = ns->netstack_ip;
	rw_enter(&ipst->ips_ill_g_lock, RW_READER);

	phytree = &ipst->ips_phyint_g_list->phyint_list_avl_by_name;
	phyi = avl_find(phytree, ifname, NULL);
	if (phyi != NULL) {
		lnp = lxsys_getnode(ldp->lxsys_vnode, ldp->lxsys_type,
		    phyi->phyint_ifindex, 0);
		result = lnp->lxsys_vnode;
		result->v_type = VLNK;
	}

	rw_exit(&ipst->ips_ill_g_lock);
	netstack_rele(ns);

	return (result);
}

static vnode_t *
lxsys_lookup_devices_virtual_netdir(lxsys_node_t *ldp, char *comp)
{
	lxsys_node_t *lnp;

	if (ldp->lxsys_instance == 0) {
		/* top-level interface listing */
		vnode_t *result = NULL;
		netstack_t *ns;
		ip_stack_t *ipst;
		avl_tree_t *phytree;
		phyint_t *phyi;
		char ifname[LIFNAMSIZ];

		(void) strncpy(ifname, comp, LIFNAMSIZ);
		lx_ifname_convert(ifname, LX_IF_TONATIVE);

		if ((ns = lxsys_netstack(ldp)) == NULL) {
			return (NULL);
		}
		ipst = ns->netstack_ip;
		rw_enter(&ipst->ips_ill_g_lock, RW_READER);

		phytree = &ipst->ips_phyint_g_list->phyint_list_avl_by_name;
		phyi = avl_find(phytree, ifname, NULL);
		if (phyi != NULL) {
			lnp = lxsys_getnode(ldp->lxsys_vnode, ldp->lxsys_type,
			    phyi->phyint_ifindex, 0);
			result = lnp->lxsys_vnode;
		}

		rw_exit(&ipst->ips_ill_g_lock);
		netstack_rele(ns);

		return (result);
	} else if (ldp->lxsys_endpoint == 0) {
		/* interface-level sub-item listing */
		int i, size;
		lxsys_dirent_t *dirent;

		size = SYSDIRLISTSZ(dirlist_devices_virtual_net);
		for (i = 0; i < size; i++) {
			dirent = &dirlist_devices_virtual_net[i];
			if (strncmp(comp, dirent->d_name, LXSNSIZ) == 0) {
				lnp = lxsys_getnode(ldp->lxsys_vnode,
				    ldp->lxsys_type, ldp->lxsys_instance,
				    dirent->d_idnum);
				lnp->lxsys_vnode->v_type = VREG;
				lnp->lxsys_mode = 0444;
				return (lnp->lxsys_vnode);
			}
		}
	}

	return (NULL);
}

static vnode_t *
lxsys_lookup_blockdir(lxsys_node_t *ldp, char *comp)
{
	lxsys_node_t *lnp;

	if (ldp->lxsys_instance == 0) {
		/* top-level dev listing */
		lnp = lxsys_lookup_disk(ldp, comp, LXSYS_BLOCK);

		if (lnp != NULL) {
			lnp->lxsys_vnode->v_type = VLNK;
			return (lnp->lxsys_vnode);
		}
	}

	return (NULL);
}

static vnode_t *
lxsys_lookup_devices_zfsdir(lxsys_node_t *ldp, char *comp)
{
	lxsys_node_t *lnp;

	if (ldp->lxsys_instance == 0) {
		/* top-level dev listing */
		lnp = lxsys_lookup_disk(ldp, comp, LXSYS_DEV_ZFS);

		if (lnp != NULL) {
			return (lnp->lxsys_vnode);
		}
	} else if (ldp->lxsys_endpoint == 0) {
		/* disk-level sub-item listing */
		int i, size;
		lxsys_dirent_t *dirent;

		/*
		 * All of these entries currently look like regular files
		 * but on a real Linux system some will be subdirs. This should
		 * be fixed when we populate the directory for real.
		 */
		size = SYSDIRLISTSZ(dirlist_devices_zfs_block);
		for (i = 0; i < size; i++) {
			dirent = &dirlist_devices_zfs_block[i];
			if (strncmp(comp, dirent->d_name, LXSNSIZ) == 0) {
				lnp = lxsys_getnode(ldp->lxsys_vnode,
				    ldp->lxsys_type, ldp->lxsys_instance,
				    dirent->d_idnum);
				lnp->lxsys_vnode->v_type = VREG;
				lnp->lxsys_mode = 0444;
				return (lnp->lxsys_vnode);
			}
		}
	}

	return (NULL);
}

static vnode_t *
lxsys_lookup_devices_syscpu(lxsys_node_t *ldp, char *comp)
{
	lxsys_node_t *lnp = NULL;

	if (ldp->lxsys_instance == 0) {
		/* top-level cpu listing */

		/* If fixed entry */
		if (strcmp(comp, "kernel_max") == 0) {
			lnp = lxsys_getnode(ldp->lxsys_vnode, LXSYS_DEV_SYS_CPU,
			    LXSYS_INST_DEV_SYSCPU_KMAX, 0);
			lnp->lxsys_vnode->v_type = VREG;
			lnp->lxsys_mode = 0444;
		} else if (strcmp(comp, "offline") == 0) {
			lnp = lxsys_getnode(ldp->lxsys_vnode, LXSYS_DEV_SYS_CPU,
			    LXSYS_INST_DEV_SYSCPU_OFFLINE, 0);
			lnp->lxsys_vnode->v_type = VREG;
			lnp->lxsys_mode = 0444;
		} else if (strcmp(comp, "online") == 0) {
			lnp = lxsys_getnode(ldp->lxsys_vnode, LXSYS_DEV_SYS_CPU,
			    LXSYS_INST_DEV_SYSCPU_ONLINE, 0);
			lnp->lxsys_vnode->v_type = VREG;
			lnp->lxsys_mode = 0444;
		} else if (strcmp(comp, "possible") == 0) {
			lnp = lxsys_getnode(ldp->lxsys_vnode, LXSYS_DEV_SYS_CPU,
			    LXSYS_INST_DEV_SYSCPU_POSSIBLE, 0);
			lnp->lxsys_vnode->v_type = VREG;
			lnp->lxsys_mode = 0444;
		} else if (strcmp(comp, "present") == 0) {
			lnp = lxsys_getnode(ldp->lxsys_vnode, LXSYS_DEV_SYS_CPU,
			    LXSYS_INST_DEV_SYSCPU_PRESENT, 0);
			lnp->lxsys_vnode->v_type = VREG;
			lnp->lxsys_mode = 0444;
		} else {
			/* Else dynamic cpuN entry */
			cpuset_t *avail;	/* all installed CPUs */
			uint_t i, avlo, avhi;

			avail = cpuset_alloc(KM_SLEEP);
			cpuset_all(avail);

			/* Take a snapshot of the available set */
			mutex_enter(&cpu_lock);
			cpuset_and(avail, &cpu_available);
			mutex_exit(&cpu_lock);

			cpuset_bounds(avail, &avlo, &avhi);

			for (i = avlo; i <= avhi; i++) {
				char cpunm[16];

				if (!cpu_in_set(avail, i))
					continue;

				(void) snprintf(cpunm, sizeof (cpunm), "cpu%u",
				    i);

				if (strcmp(comp, cpunm) == 0) {
					lnp = lxsys_getnode(ldp->lxsys_vnode,
					    LXSYS_DEV_SYS_CPUINFO, i + 1, 0);
					break;
				}
			}
			cpuset_free(avail);
		}

		if (lnp != NULL) {
			return (lnp->lxsys_vnode);
		}
	} else if (ldp->lxsys_endpoint == 0) {
		/* cpu-level sub-item listing, currently empty */
		/* EMPTY */
	}

	return (NULL);
}

/* ARGSUSED */
static vnode_t *
lxsys_lookup_devices_syscpuinfo(lxsys_node_t *ldp, char *comp)
{
	return (NULL);
}

static vnode_t *
lxsys_lookup_devices_sysnode(lxsys_node_t *ldp, char *comp)
{
	lxsys_node_t *lnp = NULL;

	if (ldp->lxsys_instance == 0) {
		/*
		 * The system is presently represented as a single node,
		 * regardless of any NUMA topology which exists.
		 * The instances are offset by 1 to account for the top level
		 * directory occupying instance 0.
		 */
		if (strcmp(comp, "node0") == 0) {
			lnp = lxsys_getnode(ldp->lxsys_vnode, ldp->lxsys_type,
			    1, 0);
			return (lnp->lxsys_vnode);
		}
	} else {
		/* interface-level sub-item listing */
		int i, size;
		lxsys_dirent_t *dirent;

		size = SYSDIRLISTSZ(dirlist_devices_sysnode);
		for (i = 0; i < size; i++) {
			dirent = &dirlist_devices_sysnode[i];
			if (strncmp(comp, dirent->d_name, LXSNSIZ) == 0) {
				lnp = lxsys_getnode(ldp->lxsys_vnode,
				    ldp->lxsys_type, ldp->lxsys_instance,
				    dirent->d_idnum);
				lnp->lxsys_vnode->v_type = VREG;
				lnp->lxsys_mode = 0444;
				return (lnp->lxsys_vnode);
			}
		}
	}

	return (NULL);
}

static int
lxsys_read_devices_virtual_net(lxsys_node_t *lnp, lxsys_uiobuf_t *luio)
{
	netstack_t *ns;
	ill_t *ill;
	uint_t ifindex = lnp->lxsys_instance;
	uint8_t *addr;
	uint64_t flags;
	int error = 0;

	if (ifindex == 0 || lnp->lxsys_endpoint == 0) {
		return (EISDIR);
	}

	if ((ns = lxsys_netstack(lnp)) == NULL) {
		return (EIO);
	}

	ill = lxsys_find_ill(ns->netstack_ip, ifindex);
	if (ill == NULL) {
		netstack_rele(ns);
		return (EIO);
	}

	switch (lnp->lxsys_endpoint) {
	case LXSYS_ENDP_NET_ADDRESS:
		if (ill->ill_phys_addr_length != ETHERADDRL) {
			lxsys_uiobuf_printf(luio, "00:00:00:00:00:00\n");
			break;
		}
		addr = ill->ill_phys_addr;
		lxsys_uiobuf_printf(luio,
		    "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",
		    addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
		break;
	case LXSYS_ENDP_NET_ADDRLEN:
		lxsys_uiobuf_printf(luio, "%u\n",
		    IS_LOOPBACK(ill) ? ETHERADDRL : ill->ill_phys_addr_length);
		break;
	case LXSYS_ENDP_NET_FLAGS:
		flags = (ill->ill_flags | ill->ill_ipif->ipif_flags |
		    ill->ill_phyint->phyint_flags) & 0xffff;
		lx_ifflags_convert(&flags, LX_IF_FROMNATIVE);
		lxsys_uiobuf_printf(luio, "0x%x\n", flags);
		break;
	case LXSYS_ENDP_NET_IFINDEX:
		lxsys_uiobuf_printf(luio, "%u\n", ifindex);
		break;
	case LXSYS_ENDP_NET_MTU:
		lxsys_uiobuf_printf(luio, "%u\n", ill->ill_mtu);
		break;
	case LXSYS_ENDP_NET_TXQLEN:
		/* perpetuate the txqlen lie */
		if (IS_LOOPBACK(ill)) {
			lxsys_uiobuf_printf(luio, "0\n");
		} else {
			lxsys_uiobuf_printf(luio, "1\n");
		}
		break;
	case LXSYS_ENDP_NET_TYPE:
		lxsys_uiobuf_printf(luio, "%u\n",
		    IS_LOOPBACK(ill) ? LX_ARPHRD_LOOPBACK :
		    arp_hw_type(ill->ill_mactype));
		break;
	default:
		error = EIO;
	}

	ill_refrele(ill);
	netstack_rele(ns);
	return (error);
}

/* ARGSUSED1 */
static int
lxsys_read_devices_zfs_block(lxsys_node_t *lnp, lxsys_uiobuf_t *luio)
{
	uint_t dskindex = lnp->lxsys_instance;

	if (dskindex == 0 || lnp->lxsys_endpoint == 0) {
		return (EISDIR);
	}

	return (EIO);
}

/*
 * In the Linux src tree, see ABI/stable/sysfs-devices-node.
 *
 * For the 'cpumap' file, each CPU is treated as a bit, then those are
 * accumulated and printed as a hex digit, with CPU0 as the rightmost bit.
 * Each set of 8 digits (i.e. 32 CPUs) is then delimited with a comma.
 * Since we are emulating a single NUMA group, all of our CPUs will be listed
 * in this file. For example, a 48 CPU system would look like:
 *     00000000,00000000,00000000,00000000,00000000,00000000,0000ffff,ffffffff
 * It comes out this way because 'kernel_max' is NCPU, which is currently
 * defined to be 256.
 */
static int
lxsys_read_devices_sysnode(lxsys_node_t *lnp, lxsys_uiobuf_t *luio)
{
	if (lnp->lxsys_instance == 1) {
		char outbuf[256];

		if (lnp->lxsys_endpoint == LXSYS_ENDP_NODE_CPULIST) {
			/* Show the range of CPUs */
			lxsys_format_cpu(outbuf, sizeof (outbuf),
			    LXSYS_CPU_ANY);
		} else if (lnp->lxsys_endpoint == LXSYS_ENDP_NODE_CPUMAP) {
			int i;
			uint_t j, ndigits;
			cpuset_t *avail;	/* all installed CPUs */

			avail = cpuset_alloc(KM_SLEEP);
			cpuset_all(avail);

			/* Take a snapshot of the available set */
			mutex_enter(&cpu_lock);
			cpuset_and(avail, &cpu_available);
			mutex_exit(&cpu_lock);

			outbuf[0] = '\0';
			ndigits = 0;
			for (i = NCPU - 1; i >= 0; i -= 4) {
				char buf[8];
				int cnt = 3;
				uint_t digit = 0;

				for (j = i; cnt >= 0; j--, cnt--) {
					if (cpu_in_set(avail, j))
						digit |= 1 << cnt;
				}
				(void) snprintf(buf, sizeof (buf), "%x", digit);
				if (ndigits == 8) {
					(void) strlcat(outbuf, ",",
					    sizeof (outbuf));
					ndigits = 0;
				}
				(void) strlcat(outbuf, buf, sizeof (outbuf));
				ndigits++;
			}

			cpuset_free(avail);
		} else {
			return (EISDIR);
		}

		lxsys_uiobuf_printf(luio, "%s\n", outbuf);
		return (0);
	}
	return (EISDIR);
}

static void
lxsys_format_range(char *buf, int blen, boolean_t *first, uint_t start,
    uint_t cnt)
{
	char tmp[256];
	char *delim;

	if (cnt == 0)
		return;

	if (*first) {
		*first = B_FALSE;
		delim = "";
	} else {
		delim = ",";
	}
	if (cnt > 1) {
		(void) snprintf(tmp, sizeof (tmp), "%s%u-%u", delim, start,
		    start + cnt - 1);
	} else {
		(void) snprintf(tmp, sizeof (tmp), "%s%u", delim, start);
	}
	(void) strlcat(buf, tmp, blen);
}

/*
 * Format a string of which CPUs are online, offline, or don't care (depending
 * on chk_state), and which would be formatted like this:
 *    0-31
 * or
 *    0-12,14,20-31
 */
static void
lxsys_format_cpu(char *buf, int blen, lxsys_cpu_state_t chk_state)
{
	uint_t start, cnt, avlo, avhi;
	boolean_t first = B_TRUE;
	cpuset_t *active;	/* CPUs online */
	cpuset_t *avail;	/* all installed CPUs */

	active = cpuset_alloc(KM_SLEEP);
	avail = cpuset_alloc(KM_SLEEP);
	cpuset_all(active);
	cpuset_all(avail);

	/* Take a snapshot of the available and active sets */
	mutex_enter(&cpu_lock);
	cpuset_and(avail, &cpu_available);
	cpuset_and(active, &cpu_active_set);
	mutex_exit(&cpu_lock);

	cpuset_bounds(avail, &avlo, &avhi);

	buf[0] = '\0';
	if (chk_state == LXSYS_CPU_ANY) {
		start = avlo;
		cnt = avhi + 1;
	} else {
		uint_t i;
		boolean_t incl_cpu = B_TRUE;

		start = 0;
		cnt = 0;
		for (i = avlo; i <= avhi; i++) {
			if (chk_state == LXSYS_CPU_ON) {
				if (!cpu_in_set(active, i))
					incl_cpu = B_FALSE;
			} else {
				if (cpu_in_set(active, i))
					incl_cpu = B_FALSE;
			}

			if (incl_cpu && cpu_in_set(avail, i)) {
				cnt++;
			} else {
				/*
				 * Note: this may print nothing if our 'cnt'
				 * is 0, but we advance 'start' properly so we
				 * handle the next range of elements we're
				 * looking for.
				 */
				lxsys_format_range(buf, blen, &first, start,
				    cnt);
				start += cnt + 1;
				cnt = 0;
				incl_cpu = B_TRUE;
			}
		}
	}

	cpuset_free(avail);
	cpuset_free(active);

	lxsys_format_range(buf, blen, &first, start, cnt);
}

static int
lxsys_read_devices_syscpu(lxsys_node_t *lnp, lxsys_uiobuf_t *luio)
{
	uint_t inst = lnp->lxsys_instance;
	char outbuf[256];

	/*
	 * For 'kernel_max', 'offline', 'online', 'possible', and 'present',
	 * see the Documentaion/cputopology.txt file in the Linux src tree.
	 */
	if (inst == LXSYS_INST_DEV_SYSCPU_KMAX) {
		lxsys_uiobuf_printf(luio, "%d\n", NCPU - 1);
		return (0);
	}

	if (inst == LXSYS_INST_DEV_SYSCPU_OFFLINE) {
		lxsys_format_cpu(outbuf, sizeof (outbuf), LXSYS_CPU_OFF);
		lxsys_uiobuf_printf(luio, "%s\n", outbuf);
		return (0);
	}

	if (inst == LXSYS_INST_DEV_SYSCPU_ONLINE) {
		lxsys_format_cpu(outbuf, sizeof (outbuf), LXSYS_CPU_ON);
		lxsys_uiobuf_printf(luio, "%s\n", outbuf);
		return (0);
	}

	if (inst == LXSYS_INST_DEV_SYSCPU_POSSIBLE ||
	    inst == LXSYS_INST_DEV_SYSCPU_PRESENT) {
		lxsys_format_cpu(outbuf, sizeof (outbuf), LXSYS_CPU_ANY);
		lxsys_uiobuf_printf(luio, "%s\n", outbuf);
		return (0);
	}

	/* All other nodes are directories */
	return (EISDIR);
}

/* ARGSUSED */
static int
lxsys_read_static(lxsys_node_t *lnp, lxsys_uiobuf_t *luio)
{
	/* All static nodes are directories */
	return (EISDIR);
}

/*
 * lxsys_readdir(): Vnode operation for VOP_READDIR()
 */
/* ARGSUSED */
static int
lxsys_readdir(vnode_t *dp, uio_t *uiop, cred_t *cr, int *eofp,
    caller_context_t *ct, int flags)
{
	lxsys_node_t *lxsnp = VTOLXS(dp);
	lxsys_nodetype_t type = lxsnp->lxsys_type;
	ssize_t uresid;
	off_t uoffset;
	int error, leof;

	ASSERT(dp->v_type == VDIR);
	VERIFY(type > LXSYS_NONE && type < LXSYS_MAXTYPE);

	/*
	 * restrict readdir permission to owner or root
	 */
	if ((error = lxsys_access(dp, VREAD, 0, cr, ct)) != 0)
		return (error);

	uoffset = uiop->uio_offset;
	uresid = uiop->uio_resid;

	/* can't do negative reads */
	if (uoffset < 0 || uresid <= 0)
		return (EINVAL);

	/* can't read directory entries that don't exist! */
	if (uoffset % LXSYS_SDSIZE)
		return (ENOENT);

	/* Free lower functions from having to check eofp == NULL */
	if (eofp == NULL) {
		eofp = &leof;
	}

	return (lxsys_readdir_function[lxsnp->lxsys_type](lxsnp, uiop, eofp));
}

static int
lxsys_dirent_out(dirent64_t *d, ushort_t n, struct uio *uio)
{
	int error;
	off_t offset = uio->uio_offset;

	/*
	 * uiomove() updates both uiop->uio_resid and uiop->uio_offset by the
	 * same amount.  But we want uiop->uio_offset to change in increments
	 * of LXSYS_SDSIZE, which is different from the number of bytes being
	 * returned to the user.  To accomplish this, we set uiop->uio_offset
	 * separately on success, overriding what uiomove() does.
	 */
	d->d_off = (off64_t)(offset + LXSYS_SDSIZE);
	d->d_reclen = n;
	if ((error = uiomove(d, n, UIO_READ, uio)) != 0) {
		return (error);
	}
	uio->uio_offset = offset + LXSYS_SDSIZE;
	return (0);
}

/*
 * This has the common logic for returning directory entries
 */
static int
lxsys_readdir_common(lxsys_node_t *lxsnp, uio_t *uiop, int *eofp,
    lxsys_dirent_t *dirtab, int dirtablen)
{
	/* bp holds one dirent64 structure */
	longlong_t bp[DIRENT64_RECLEN(LXSNSIZ) / sizeof (longlong_t)];
	dirent64_t *dirent = (dirent64_t *)bp;
	ssize_t oresid;	/* save a copy for testing later */
	ssize_t uresid;

	oresid = uiop->uio_resid;

	/* clear out the dirent buffer */
	bzero(bp, sizeof (bp));

	/* Satisfy user request */
	while ((uresid = uiop->uio_resid) > 0) {
		int dirindex;
		off_t uoffset;
		int reclen;
		int error;

		uoffset = uiop->uio_offset;
		dirindex  = (uoffset / LXSYS_SDSIZE) - 2;

		if (uoffset == 0) {

			dirent->d_ino = lxsnp->lxsys_ino;
			dirent->d_name[0] = '.';
			dirent->d_name[1] = '\0';
			reclen = DIRENT64_RECLEN(1);

		} else if (uoffset == LXSYS_SDSIZE) {

			dirent->d_ino = lxsys_parentinode(lxsnp);
			dirent->d_name[0] = '.';
			dirent->d_name[1] = '.';
			dirent->d_name[2] = '\0';
			reclen = DIRENT64_RECLEN(2);

		} else if (dirindex >= 0 && dirindex < dirtablen) {

			int slen = strlen(dirtab[dirindex].d_name);
			int idnum, ino_type = 0;

			idnum = dirtab[dirindex].d_idnum;
			if (idnum > 0 && idnum < DYN_INO_LEN)
				ino_type = dyn_ino_type[idnum];

			if (ino_type != 0) {
				/*
				 * Correct the inode for static directories
				 * which contain non-static lxsys_nodetype_t's.
				 */
				dirent->d_ino = lxsys_inode(ino_type, 0, 0);
				DTRACE_PROBE3(lxsys__fix__inode,
				    char *, dirtab[dirindex].d_name,
				    int, ino_type, int, dirent->d_ino);
			} else {
				dirent->d_ino = lxsys_inode(LXSYS_STATIC,
				    idnum, 0);
			}

			(void) strcpy(dirent->d_name, dirtab[dirindex].d_name);
			reclen = DIRENT64_RECLEN(slen);

		} else {
			/* Run out of table entries */
			*eofp = 1;
			return (0);
		}

		/*
		 * If the size of the data to transfer is greater than the
		 * user-provided buffer, we cannot continue.
		 */
		if (reclen > uresid) {
			/* Error if no entries have been returned yet. */
			if (uresid == oresid) {
				return (EINVAL);
			}
			break;
		}

		if ((error = lxsys_dirent_out(dirent, reclen, uiop)) != 0) {
			return (error);
		}
	}

	/* Have run out of space, but could have just done last table entry */
	*eofp = (uiop->uio_offset >= ((dirtablen+2) * LXSYS_SDSIZE)) ?  1 : 0;
	return (0);
}

static int
lxsys_readdir_subdir(lxsys_node_t *lxsnp, uio_t *uiop, int *eofp,
    lxsys_dirent_t *dirtab, int dirtablen)
{
	/* bp holds one dirent64 structure */
	longlong_t bp[DIRENT64_RECLEN(LXSNSIZ) / sizeof (longlong_t)];
	dirent64_t *dirent = (dirent64_t *)bp;
	ssize_t oresid;	/* save a copy for testing later */
	ssize_t uresid;

	VERIFY(dirtab != NULL || dirtablen == 0);

	oresid = uiop->uio_resid;

	/* clear out the dirent buffer */
	bzero(bp, sizeof (bp));

	/* Satisfy user request */
	while ((uresid = uiop->uio_resid) > 0) {
		int dirindex;
		off_t uoffset;
		int reclen;
		int error;

		uoffset = uiop->uio_offset;
		dirindex  = (uoffset / LXSYS_SDSIZE) - 2;

		if (uoffset == 0) {

			dirent->d_ino = lxsnp->lxsys_ino;
			dirent->d_name[0] = '.';
			dirent->d_name[1] = '\0';
			reclen = DIRENT64_RECLEN(1);

		} else if (uoffset == LXSYS_SDSIZE) {

			dirent->d_ino = lxsys_parentinode(lxsnp);
			dirent->d_name[0] = '.';
			dirent->d_name[1] = '.';
			dirent->d_name[2] = '\0';
			reclen = DIRENT64_RECLEN(2);

		} else if (dirindex >= 0 && dirindex < dirtablen) {

			int slen = strlen(dirtab[dirindex].d_name);

			dirent->d_ino = lxsys_inode(lxsnp->lxsys_type,
			    lxsnp->lxsys_instance, dirtab[dirindex].d_idnum);
			(void) strcpy(dirent->d_name, dirtab[dirindex].d_name);
			reclen = DIRENT64_RECLEN(slen);

		} else {
			/* Run out of table entries */
			*eofp = 1;
			return (0);
		}

		/*
		 * If the size of the data to transfer is greater than the
		 * user-provided buffer, we cannot continue.
		 */
		if (reclen > uresid) {
			/* Error if no entries have been returned yet. */
			if (uresid == oresid) {
				return (EINVAL);
			}
			break;
		}

		if ((error = lxsys_dirent_out(dirent, reclen, uiop)) != 0) {
			return (error);
		}
	}

	/* Have run out of space, but could have just done last table entry */
	*eofp = (uiop->uio_offset >= ((dirtablen+2) * LXSYS_SDSIZE)) ?  1 : 0;
	return (0);
}

static int
lxsys_readdir_ifaces(lxsys_node_t *ldp, struct uio *uiop, int *eofp,
    lxsys_nodetype_t type)
{
	longlong_t bp[DIRENT64_RECLEN(LXSNSIZ) / sizeof (longlong_t)];
	dirent64_t *dirent = (dirent64_t *)bp;
	ssize_t oresid, uresid;
	netstack_t *ns;
	ip_stack_t *ipst;
	avl_tree_t *phytree;
	phyint_t *phyi;
	int error, i;


	/* Emit "." and ".." entries */
	oresid = uiop->uio_resid;
	error = lxsys_readdir_common(ldp, uiop, eofp, NULL, 0);
	if (error != 0 || *eofp == 0) {
		return (error);
	}

	if ((ns = lxsys_netstack(ldp)) == NULL) {
		*eofp = 1;
		return (0);
	}
	ipst = ns->netstack_ip;

	rw_enter(&ipst->ips_ill_g_lock, RW_READER);
	phytree = &ipst->ips_phyint_g_list->phyint_list_avl_by_index;
	phyi = avl_first(phytree);
	if (phyi == NULL) {
		*eofp = 1;
	}
	bzero(bp, sizeof (bp));

	/*
	 * Skip records we have already passed with the offset.
	 * This accounts for the two "." and ".." records already seen.
	 */
	for (i = (uiop->uio_offset/LXSYS_SDSIZE) - 2; i > 0; i--) {
		if ((phyi = avl_walk(phytree, phyi, AVL_AFTER)) == NULL) {
			*eofp = 1;
			break;
		}
	}

	while ((uresid = uiop->uio_resid) > 0 && phyi != NULL) {
		uint_t ifindex;
		int reclen;

		ifindex = phyi->phyint_ifindex;
		(void) strncpy(dirent->d_name, phyi->phyint_name, LIFNAMSIZ);
		lx_ifname_convert(dirent->d_name, LX_IF_FROMNATIVE);
		dirent->d_ino = lxsys_inode(type, ifindex, 0);
		reclen = DIRENT64_RECLEN(strlen(dirent->d_name));

		if (reclen > uresid) {
			if (uresid == oresid) {
				/* Not enough space for one record */
				error = EINVAL;
			}
			break;
		}
		if ((error = lxsys_dirent_out(dirent, reclen, uiop)) != 0) {
			break;
		}

		if ((phyi = avl_walk(phytree, phyi, AVL_AFTER)) == NULL) {
			*eofp = 1;
			break;
		}
	}

	rw_exit(&ipst->ips_ill_g_lock);
	netstack_rele(ns);
	return (error);
}

static int
lxsys_readdir_disks(lxsys_node_t *ldp, struct uio *uiop, int *eofp,
    lxsys_nodetype_t type)
{
	longlong_t bp[DIRENT64_RECLEN(LXSNSIZ) / sizeof (longlong_t)];
	dirent64_t *dirent = (dirent64_t *)bp;
	ssize_t oresid, uresid;
	int skip, error;
	int reclen;
	uint_t instance;
	lx_zone_data_t *lxzdata;
	lx_virt_disk_t *vd;

	/* Emit "." and ".." entries */
	oresid = uiop->uio_resid;
	error = lxsys_readdir_common(ldp, uiop, eofp, NULL, 0);
	if (error != 0 || *eofp == 0) {
		return (error);
	}

	skip = (uiop->uio_offset/LXSYS_SDSIZE) - 2;

	lxzdata = ztolxzd(curproc->p_zone);
	if (lxzdata == NULL)
		return (EINVAL);
	ASSERT(lxzdata->lxzd_vdisks != NULL);

	vd = list_head(lxzdata->lxzd_vdisks);
	while (vd != NULL) {
		if (skip > 0) {
			skip--;
			goto next;
		}

		if (strnlen(vd->lxvd_name, sizeof (vd->lxvd_name)) > LXSNSIZ)
			goto next;

		(void) strncpy(dirent->d_name, vd->lxvd_name, LXSNSIZ);

		instance = getminor(vd->lxvd_emul_dev) & 0xffff;
		if (instance == 0)
			goto next;

		dirent->d_ino = lxsys_inode(type, instance, 0);
		reclen = DIRENT64_RECLEN(strlen(dirent->d_name));

		uresid = uiop->uio_resid;
		if (reclen > uresid) {
			if (uresid == oresid) {
				/* Not enough space for one record */
				error = EINVAL;
			}
			break;
		}
		if ((error = lxsys_dirent_out(dirent, reclen, uiop)) != 0) {
			break;
		}

next:
		vd = list_next(lxzdata->lxzd_vdisks, vd);
	}

	/* Indicate EOF if we reached the end of the virtual disks. */
	if (vd == NULL) {
		*eofp = 1;
	}

	return (error);
}


static int
lxsys_readdir_static(lxsys_node_t *lnp, uio_t *uiop, int *eofp)
{
	lxsys_dirent_t *dirent = NULL;
	int i, len = 0;
	boolean_t found = B_FALSE;

	for (i = 0; i < SYSDIRLISTSZ(lxsys_dirlookup); i++) {
		if (lnp->lxsys_instance == lxsys_dirlookup[i].dl_instance) {
			dirent = lxsys_dirlookup[i].dl_list;
			len = lxsys_dirlookup[i].dl_length;
			found = B_TRUE;
			break;
		}
	}

	if (!found) {
		return (ENOTDIR);
	}

	return (lxsys_readdir_common(lnp, uiop, eofp, dirent, len));
}

static int
lxsys_readdir_class_netdir(lxsys_node_t *lnp, uio_t *uiop, int *eofp)
{
	if (lnp->lxsys_type != LXSYS_CLASS_NET ||
	    lnp->lxsys_instance != 0) {
		/*
		 * Since /sys/class/net contains only symlinks, readdir
		 * operations should not be performed anywhere except the top
		 * level (instance == 0).
		 */
		return (ENOTDIR);
	}

	return (lxsys_readdir_ifaces(lnp, uiop, eofp, LXSYS_CLASS_NET));
}

static int
lxsys_readdir_devices_virtual_netdir(lxsys_node_t *lnp, uio_t *uiop, int *eofp)
{
	int error;

	if (lnp->lxsys_instance == 0) {
		/* top-level interface listing */
		error = lxsys_readdir_ifaces(lnp, uiop, eofp,
		    LXSYS_DEV_NET);
	} else if (lnp->lxsys_endpoint == 0) {
		/* interface-level sub-item listing */
		error = lxsys_readdir_subdir(lnp, uiop, eofp,
		    dirlist_devices_virtual_net,
		    SYSDIRLISTSZ(dirlist_devices_virtual_net));
	} else {
		/* there shouldn't be subdirs below this */
		error = ENOTDIR;
	}

	return (error);
}

static int
lxsys_readdir_blockdir(lxsys_node_t *lnp, uio_t *uiop, int *eofp)
{
	if (lnp->lxsys_type != LXSYS_BLOCK ||
	    lnp->lxsys_instance != 0) {
		/*
		 * Since /sys/block contains only symlinks, readdir operations
		 * should not be performed anywhere except the top level
		 * (instance == 0).
		 */
		return (ENOTDIR);
	}

	return (lxsys_readdir_disks(lnp, uiop, eofp, LXSYS_BLOCK));
}

static int
lxsys_readdir_devices_zfsdir(lxsys_node_t *lnp, uio_t *uiop, int *eofp)
{
	int error;

	if (lnp->lxsys_instance == 0) {
		/* top-level dev listing */
		error = lxsys_readdir_disks(lnp, uiop, eofp,
		    LXSYS_DEV_ZFS);
	} else if (lnp->lxsys_endpoint == 0) {
		/* disk-level sub-item listing */
		error = lxsys_readdir_subdir(lnp, uiop, eofp,
		    dirlist_devices_zfs_block,
		    SYSDIRLISTSZ(dirlist_devices_zfs_block));
	} else {
		/*
		 * Currently there shouldn't be subdirs below this but
		 * on a real Linux system some will be subdirs. This should
		 * be fixed when we populate the directory for real.
		 */
		error = ENOTDIR;
	}

	return (error);
}

/* Handle fixed entries within the cpu directory. */
static int
lxsys_do_sub_cpu(struct uio *uiop, ssize_t oresid, dirent64_t *dirent,
    char *nm, int inst, int *errp)
{
	int reclen;
	ssize_t uresid;

	(void) strncpy(dirent->d_name, nm, LXSNSIZ);

	dirent->d_ino = lxsys_inode(LXSYS_DEV_SYS_CPU, inst, 0);
	reclen = DIRENT64_RECLEN(strlen(dirent->d_name));

	uresid = uiop->uio_resid;
	if (reclen > uresid) {
		if (uresid == oresid) {
			/* Not enough space for one record */
			*errp = EINVAL;
		}
		return (-1);
	}
	if ((*errp = lxsys_dirent_out(dirent, reclen, uiop)) != 0) {
		return (-1);
	}

	return (0);
}

static int
lxsys_readdir_cpu(lxsys_node_t *ldp, struct uio *uiop, int *eofp)
{
	longlong_t bp[DIRENT64_RECLEN(LXSNSIZ) / sizeof (longlong_t)];
	dirent64_t *dirent = (dirent64_t *)bp;
	ssize_t oresid, uresid;
	int skip, error;
	int reclen;
	cpuset_t *avail;
	uint_t i, avlo, avhi;

	/* Emit "." and ".." entries */
	oresid = uiop->uio_resid;
	error = lxsys_readdir_common(ldp, uiop, eofp, NULL, 0);
	if (error != 0 || *eofp == 0) {
		return (error);
	}

	skip = (uiop->uio_offset/LXSYS_SDSIZE) - 2;

	/* Fixed entries */
	if (skip > 0) {
		skip--;
	} else {
		if (lxsys_do_sub_cpu(uiop, oresid, dirent, "kernel_max",
		    LXSYS_INST_DEV_SYSCPU_KMAX, &error) != 0)
			goto done;

		if (lxsys_do_sub_cpu(uiop, oresid, dirent, "offline",
		    LXSYS_INST_DEV_SYSCPU_OFFLINE, &error) != 0)
			goto done;

		if (lxsys_do_sub_cpu(uiop, oresid, dirent, "online",
		    LXSYS_INST_DEV_SYSCPU_ONLINE, &error) != 0)
			goto done;

		if (lxsys_do_sub_cpu(uiop, oresid, dirent, "possible",
		    LXSYS_INST_DEV_SYSCPU_POSSIBLE, &error) != 0)
			goto done;

		if (lxsys_do_sub_cpu(uiop, oresid, dirent, "present",
		    LXSYS_INST_DEV_SYSCPU_PRESENT, &error) != 0)
			goto done;
	}

	avail = cpuset_alloc(KM_SLEEP);
	cpuset_all(avail);

	/* Take a snapshot of the available set */
	mutex_enter(&cpu_lock);
	cpuset_and(avail, &cpu_available);
	mutex_exit(&cpu_lock);

	cpuset_bounds(avail, &avlo, &avhi);

	/* Output dynamic CPU info */
	for (i = avlo; i <= avhi; i++) {
		char cpunm[16];

		if (skip > 0) {
			skip--;
			continue;
		}

		if (!cpu_in_set(avail, i))
			continue;

		(void) snprintf(cpunm, sizeof (cpunm), "cpu%u", i);
		(void) strncpy(dirent->d_name, cpunm, LXSNSIZ);

		dirent->d_ino = lxsys_inode(LXSYS_DEV_SYS_CPUINFO, i + 1, 0);
		reclen = DIRENT64_RECLEN(strlen(dirent->d_name));

		uresid = uiop->uio_resid;
		if (reclen > uresid) {
			if (uresid == oresid) {
				/* Not enough space for one record */
				error = EINVAL;
			}
			break;
		}
		if ((error = lxsys_dirent_out(dirent, reclen, uiop)) != 0) {
			break;
		}
	}
	cpuset_free(avail);

	/* Indicate EOF if we reached the end of the CPU list. */
	if (i == avhi) {
		*eofp = 1;
	}

done:
	return (error);
}

static int
lxsys_readdir_devices_syscpu(lxsys_node_t *lnp, uio_t *uiop, int *eofp)
{
	int error;

	if (lnp->lxsys_instance == 0) {
		/* top-level cpu listing */
		error = lxsys_readdir_cpu(lnp, uiop, eofp);
	} else if (lnp->lxsys_endpoint == 0) {
		/* cpu-level sub-item listing */
		error = lxsys_readdir_subdir(lnp, uiop, eofp, NULL, 0);
	} else {
		/*
		 * Currently there shouldn't be subdirs below this but
		 * on a real Linux system some will be subdirs. This should
		 * be fixed when we populate the directory for real.
		 */
		error = ENOTDIR;
	}

	return (error);
}

static int
lxsys_readdir_devices_syscpuinfo(lxsys_node_t *lnp, uio_t *uiop, int *eofp)
{
	int error;

	if (lnp->lxsys_type != LXSYS_DEV_SYS_CPUINFO) {
		/*
		 * Since /sys/devices/system/cpu/cpuN is empty, readdir
		 * operations should not be performed anywhere except the top
		 * level.
		 */
		return (ENOTDIR);
	}

	/*
	 * Emit "." and ".." entries
	 * All cpuN directories are currently empty.
	 */
	error = lxsys_readdir_common(lnp, uiop, eofp, NULL, 0);
	if (error != 0 || *eofp == 0) {
		return (error);
	}

	/* Indicate EOF */
	*eofp = 1;

	return (error);
}

static int
lxsys_readdir_devices_sysnode(lxsys_node_t *lnp, uio_t *uiop, int *eofp)
{
	int error;

	if (lnp->lxsys_instance == 0) {
		/* top-level node listing */
		longlong_t bp[DIRENT64_RECLEN(LXSNSIZ) / sizeof (longlong_t)];
		dirent64_t *dirent = (dirent64_t *)bp;
		ssize_t oresid, uresid;
		int reclen, skip;

		/* Emit "." and ".." entries */
		oresid = uiop->uio_resid;
		error = lxsys_readdir_common(lnp, uiop, eofp, NULL, 0);
		if (error != 0 || *eofp == 0) {
			return (error);
		}
		skip = (uiop->uio_offset/LXSYS_SDSIZE) - 2;

		/* Fixed entries */
		if (skip > 0) {
			skip--;
		} else {
			(void) strncpy(dirent->d_name, "node0", LXSNSIZ);

			dirent->d_ino = lxsys_inode(LXSYS_DEV_SYS_NODE,
			    1, 0);
			reclen = DIRENT64_RECLEN(strlen(dirent->d_name));

			uresid = uiop->uio_resid;
			if (reclen > uresid) {
				if (uresid == oresid) {
					/* Not enough space for one record */
					return (EINVAL);
				}
				return (0);
			}
			error = lxsys_dirent_out(dirent, reclen, uiop);
		}
		/* Indicate EOF */
		if (error == 0) {
			*eofp = 1;
		}
	} else if (lnp->lxsys_endpoint == 0) {
		/* node-level sub-item listing */
		error = lxsys_readdir_subdir(lnp, uiop, eofp,
		    dirlist_devices_sysnode,
		    SYSDIRLISTSZ(dirlist_devices_sysnode));
	} else {
		/* there shouldn't be subdirs below this */
		error = ENOTDIR;
	}

	return (error);
}

/*
 * lxsys_readlink(): Vnode operation for VOP_READLINK()
 */
/* ARGSUSED */
static int
lxsys_readlink(vnode_t *vp, uio_t *uiop, cred_t *cr, caller_context_t *ct)
{
	char buf[MAXPATHLEN + 1];
	lxsys_node_t *lnp = VTOLXS(vp);
	lxsys_nodetype_t type = lnp->lxsys_type;
	int (*rlfunc)();
	int error;

	VERIFY(type > LXSYS_NONE && type < LXSYS_MAXTYPE);

	if (vp->v_type != VLNK) {
		return (EINVAL);
	}

	rlfunc = lxsys_readlink_function[lnp->lxsys_type];
	if (rlfunc != NULL) {
		if ((error = rlfunc(lnp, buf, sizeof (buf))) == 0) {
			error = uiomove(buf, strlen(buf), UIO_READ, uiop);
		}
	} else {
		error = EINVAL;
	}

	return (error);
}


static int
lxsys_readlink_class_net(lxsys_node_t *lnp, char *buf, size_t len)
{
	netstack_t *ns;
	ip_stack_t *ipst;
	avl_tree_t *phytree;
	phyint_t *phyi;
	uint_t ifindex;
	char ifname[LIFNAMSIZ];
	int error = EINVAL;

	if ((ifindex = lnp->lxsys_instance) == 0) {
		return (error);
	}

	if ((ns = lxsys_netstack(lnp)) == NULL) {
		return (error);
	}
	ipst = ns->netstack_ip;
	rw_enter(&ipst->ips_ill_g_lock, RW_READER);

	phytree = &ipst->ips_phyint_g_list->phyint_list_avl_by_index;
	phyi = avl_find(phytree, &ifindex, NULL);
	if (phyi != NULL) {
		(void) strncpy(ifname, phyi->phyint_name, LIFNAMSIZ);
		lx_ifname_convert(ifname, LX_IF_FROMNATIVE);
		(void) snprintf(buf, len, "/sys/devices/virtual/net/%s",
		    ifname);
		error = 0;
	}

	rw_exit(&ipst->ips_ill_g_lock);
	netstack_rele(ns);
	return (error);
}

static int
lxsys_readlink_block(lxsys_node_t *lnp, char *buf, size_t len)
{
	int inst, error = EINVAL;
	lx_zone_data_t *lxzdata;
	lx_virt_disk_t *vd;

	if ((inst = lnp->lxsys_instance) == 0) {
		return (error);
	}

	lxzdata = ztolxzd(curproc->p_zone);
	if (lxzdata == NULL)
		return (error);
	ASSERT(lxzdata->lxzd_vdisks != NULL);

	vd = list_head(lxzdata->lxzd_vdisks);
	while (vd != NULL) {
		int vinst = getminor(vd->lxvd_emul_dev) & 0xffff;

		if (vinst == inst) {
			(void) snprintf(buf, len,
			    "../devices/zfs/%s", vd->lxvd_name);
			error = 0;
			break;
		}
		vd = list_next(lxzdata->lxzd_vdisks, vd);
	}

	return (error);
}

/*
 * lxsys_inactive(): Vnode operation for VOP_INACTIVE()
 * Vnode is no longer referenced, deallocate the file
 * and all its resources.
 */
/* ARGSUSED */
static void
lxsys_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
{
	lxsys_freenode(VTOLXS(vp));
}

/*
 * lxsys_sync(): Vnode operation for VOP_SYNC()
 */
static int
lxsys_sync()
{
	/*
	 * Nothing to sync but this function must never fail
	 */
	return (0);
}

/*
 * lxsys_cmp(): Vnode operation for VOP_CMP()
 */
static int
lxsys_cmp(vnode_t *vp1, vnode_t *vp2, caller_context_t *ct)
{
	if (vn_matchops(vp1, lxsys_vnodeops) ||
	    vn_matchops(vp2, lxsys_vnodeops))
		return (vp1 == vp2);
	return (VOP_CMP(vp1, vp2, ct));
}