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
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* General Structures Layout
* -------------------------
*
* This is a simplified diagram showing the relationship between most of the
* main structures.
*
* +-------------------+
* | SMB_SERVER |
* +-------------------+
* |
* |
* v
* +-------------------+ +-------------------+ +-------------------+
* | SESSION |<----->| SESSION |......| SESSION |
* +-------------------+ +-------------------+ +-------------------+
* |
* |
* v
* +-------------------+ +-------------------+ +-------------------+
* | USER |<----->| USER |......| USER |
* +-------------------+ +-------------------+ +-------------------+
* |
* |
* v
* +-------------------+ +-------------------+ +-------------------+
* | TREE |<----->| TREE |......| TREE |
* +-------------------+ +-------------------+ +-------------------+
* | |
* | |
* | v
* | +-------+ +-------+ +-------+
* | | OFILE |<----->| OFILE |......| OFILE |
* | +-------+ +-------+ +-------+
* |
* |
* v
* +-------+ +------+ +------+
* | ODIR |<----->| ODIR |......| ODIR |
* +-------+ +------+ +------+
*
*
* Module Interface Overview
* -------------------------
*
*
* +===================================+
* | smbd daemon |
* +===================================+
* | | ^
* | | |
* User | | |
* -----------|--------------|----------------|--------------------------------
* Kernel | | |
* | | |
* | | |
* +=========|==============|================|=================+
* | v v | |
* | +-----------+ +--------------------+ +------------------+ |
* | | IO | | Kernel Door Server | | User Door Servers| |
* | | Interface | | Interface | | Interface | |
* | +-----------+ +--------------------+ +------------------+ |
* | | | ^ ^ |
* | v v | | | +=========+
* | +-----------------------------------+ | | | |
* | + SMB Server Management (this file) |<------------------| ZFS |
* | +-----------------------------------+ | | | |
* | | | | Module |
* | +-----------------------------------+ | | | |
* | + SMB Server Internal Layers |------+ | +=========+
* | +-----------------------------------+ |
* | |
* | |
* +===========================================================+
*
*
* Server State Machine
* --------------------
* |
* | T0
* |
* v
* +-----------------------------+
* | SMB_SERVER_STATE_CREATED |
* +-----------------------------+
* |
* | T1
* |
* v
* +-----------------------------+
* | SMB_SERVER_STATE_CONFIGURED |
* +-----------------------------+
* |
* | T2
* |
* v
* +-----------------------------+
* | SMB_SERVER_STATE_RUNNING |
* +-----------------------------+
* |
* | T3
* |
* v
* +-----------------------------+
* | SMB_SERVER_STATE_DELETING |
* +-----------------------------+
* |
* |
* |
* v
*
* States
* ------
*
* SMB_SERVER_STATE_CREATED
*
* This is the state of the server just after creation.
*
* SMB_SERVER_STATE_CONFIGURED
*
* The server has been configured.
*
* SMB_SERVER_STATE_RUNNING
*
* The server has been started. While in this state the threads listening on
* the sockets car be started. The smbd daemon does so through an Ioctl:
*
* smb_drv_ioctl(SMB_IOC_NBT_LISTEN) --> smb_server_nbt_listen()
* smb_drv_ioctl(SMB_IOC_TCP_LISTEN) --> smb_server_nbt_listen()
*
* When a client establishes a connection the thread listening leaves
* temporarily the kernel. While in user space it creates a thread for the
* new session. It then returns to kernel with the result of the thread
* creation. If the creation failed the new session context is destroyed
* before returning listening.
*
* The new created thread enters the kernel though an Ioctl:
*
* smb_drv_ioctl(SMB_IOC_NBT_RECEIVE) --> smb_server_nbt_receive()
* smb_drv_ioctl(SMB_IOC_TCP_RECEIVE) --> smb_server_tcp_receive()
*
* SMB_SERVER_STATE_STOPPING
*
* The threads listening on the NBT and TCP sockets are being terminated.
*
*
* Transitions
* -----------
*
* Transition T0
*
* The daemon smbd triggers its creation by opening the smbsrv device. If
* the zone where the daemon lives doesn't have an smb server yet it is
* created.
*
* smb_drv_open() --> smb_server_create()
*
* Transition T1
*
* This transition occurs in smb_server_configure(). It is triggered by the
* daemon through an Ioctl.
*
* smb_drv_ioctl(SMB_IOC_CONFIG) --> smb_server_configure()
*
* Transition T2
*
* This transition occurs in smb_server_start(). It is triggered by the
* daemon through an Ioctl.
*
* smb_drv_ioctl(SMB_IOC_START) --> smb_server_start()
*
* Transition T3
*
* This transition occurs in smb_server_delete(). It is triggered by the
* daemon when closing the smbsrv device
*
* smb_drv_close() --> smb_server_delete()
*
* Comments
* --------
*
* This files assumes that there will one SMB server per zone. For now the
* smb server works only in global zone. There's nothing in this file preventing
* an smb server from being created in a non global zone. That limitation is
* enforced in user space.
*/
#include <sys/strsubr.h>
#include <sys/cmn_err.h>
#include <sys/priv.h>
#include <sys/socketvar.h>
#include <sys/zone.h>
#include <smbsrv/smb_kproto.h>
#include <smbsrv/netbios.h>
#include <smbsrv/smb_incl.h>
#include <smbsrv/cifs.h>
#include <smbsrv/smb_fsops.h>
#include <smbsrv/smb_share.h>
#include <smbsrv/smb_door_svc.h>
#include <smbsrv/smb_kstat.h>
extern void smb_dispatch_kstat_init(void);
extern void smb_dispatch_kstat_fini(void);
extern void smb_reply_notify_change_request(smb_request_t *);
static int smb_server_kstat_init(smb_server_t *);
static void smb_server_kstat_fini(smb_server_t *);
static int smb_server_kstat_update_info(kstat_t *, int);
static void smb_server_timers(smb_thread_t *, void *);
static int smb_server_listen(smb_server_t *, smb_listener_daemon_t *,
in_port_t, int, int);
static int smb_server_lookup(smb_server_t **);
static void smb_server_release(smb_server_t *);
static void smb_server_store_cfg(smb_server_t *, smb_ioc_cfg_t *);
static void smb_server_stop(smb_server_t *);
static int smb_server_fsop_start(smb_server_t *);
static void smb_server_fsop_stop(smb_server_t *);
static void smb_server_disconnect_share(char *, smb_server_t *);
static void smb_server_thread_unexport(smb_thread_t *, void *);
static void smb_server_enum_private(smb_session_list_t *, smb_svcenum_t *);
static int smb_server_sesion_disconnect(smb_session_list_t *, const char *,
const char *);
static int smb_server_fclose(smb_session_list_t *, uint32_t);
static smb_llist_t smb_servers;
/*
* *****************************************************************************
* **************** Functions called from the device interface *****************
* *****************************************************************************
*
* These functions typically have to determine the relevant smb server
* to which the call applies.
*/
/*
* smb_server_svc_init
*
* This function must be called from smb_drv_attach().
*/
int
smb_server_svc_init(void)
{
int rc = 0;
while (rc == 0) {
if (rc = smb_mbc_init())
continue;
if (rc = smb_vop_init())
continue;
if (rc = smb_node_init())
continue;
if (rc = smb_fem_init())
continue;
if (rc = smb_user_init())
continue;
if (rc = smb_notify_init())
continue;
if (rc = smb_net_init())
continue;
smb_llist_constructor(&smb_servers, sizeof (smb_server_t),
offsetof(smb_server_t, sv_lnd));
return (0);
}
smb_net_fini();
smb_notify_fini();
smb_user_fini();
smb_fem_fini();
smb_node_fini();
smb_vop_fini();
smb_mbc_fini();
return (rc);
}
/*
* smb_server_svc_fini
*
* This function must called from smb_drv_detach(). It will fail if servers
* still exist.
*/
int
smb_server_svc_fini(void)
{
int rc = EBUSY;
if (smb_llist_get_count(&smb_servers) == 0) {
smb_net_fini();
smb_notify_fini();
smb_user_fini();
smb_fem_fini();
smb_node_fini();
smb_vop_fini();
smb_mbc_fini();
smb_llist_destructor(&smb_servers);
rc = 0;
}
return (rc);
}
/*
* smb_server_create
*
* This function will fail if there's already a server associated with the
* caller's zone.
*/
int
smb_server_create(void)
{
zoneid_t zid;
smb_server_t *sv;
zid = getzoneid();
smb_llist_enter(&smb_servers, RW_WRITER);
sv = smb_llist_head(&smb_servers);
while (sv) {
ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
if (sv->sv_zid == zid) {
smb_llist_exit(&smb_servers);
return (EPERM);
}
sv = smb_llist_next(&smb_servers, sv);
}
sv = kmem_zalloc(sizeof (smb_server_t), KM_NOSLEEP);
if (sv == NULL) {
smb_llist_exit(&smb_servers);
return (ENOMEM);
}
smb_llist_constructor(&sv->sv_vfs_list, sizeof (smb_vfs_t),
offsetof(smb_vfs_t, sv_lnd));
smb_slist_constructor(&sv->sv_unexport_list, sizeof (smb_unexport_t),
offsetof(smb_unexport_t, ux_lnd));
smb_session_list_constructor(&sv->sv_nbt_daemon.ld_session_list);
smb_session_list_constructor(&sv->sv_tcp_daemon.ld_session_list);
sv->si_cache_unexport = kmem_cache_create("smb_unexport_cache",
sizeof (smb_unexport_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
sv->si_cache_vfs = kmem_cache_create("smb_vfs_cache",
sizeof (smb_vfs_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
sv->si_cache_request = kmem_cache_create("smb_request_cache",
sizeof (smb_request_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
sv->si_cache_session = kmem_cache_create("smb_session_cache",
sizeof (smb_session_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
sv->si_cache_user = kmem_cache_create("smb_user_cache",
sizeof (smb_user_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
sv->si_cache_tree = kmem_cache_create("smb_tree_cache",
sizeof (smb_tree_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
sv->si_cache_ofile = kmem_cache_create("smb_ofile_cache",
sizeof (smb_ofile_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
sv->si_cache_odir = kmem_cache_create("smb_odir_cache",
sizeof (smb_odir_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
smb_thread_init(&sv->si_thread_timers,
"smb_timers", smb_server_timers, sv,
NULL, NULL);
smb_thread_init(&sv->si_thread_unexport, "smb_thread_unexport",
smb_server_thread_unexport, sv, NULL, NULL);
sv->sv_pid = curproc->p_pid;
smb_kdoor_clnt_init();
smb_opipe_door_init();
(void) smb_server_kstat_init(sv);
mutex_init(&sv->sv_mutex, NULL, MUTEX_DEFAULT, NULL);
cv_init(&sv->sv_cv, NULL, CV_DEFAULT, NULL);
sv->sv_state = SMB_SERVER_STATE_CREATED;
sv->sv_magic = SMB_SERVER_MAGIC;
sv->sv_zid = zid;
smb_llist_insert_tail(&smb_servers, sv);
smb_llist_exit(&smb_servers);
return (0);
}
/*
* smb_server_delete
*
* This function will delete the server passed in. It will make sure that all
* activity associated that server has ceased before destroying it.
*/
int
smb_server_delete(void)
{
smb_server_t *sv;
smb_unexport_t *ux;
int rc;
rc = smb_server_lookup(&sv);
if (rc != 0)
return (rc);
mutex_enter(&sv->sv_mutex);
switch (sv->sv_state) {
case SMB_SERVER_STATE_RUNNING:
{
boolean_t nbt = B_FALSE;
boolean_t tcp = B_FALSE;
if (sv->sv_nbt_daemon.ld_kth) {
tsignal(sv->sv_nbt_daemon.ld_kth, SIGINT);
nbt = B_TRUE;
}
if (sv->sv_tcp_daemon.ld_kth) {
tsignal(sv->sv_tcp_daemon.ld_kth, SIGINT);
tcp = B_TRUE;
}
sv->sv_state = SMB_SERVER_STATE_DELETING;
mutex_exit(&sv->sv_mutex);
if (nbt)
thread_join(sv->sv_nbt_daemon.ld_ktdid);
if (tcp)
thread_join(sv->sv_tcp_daemon.ld_ktdid);
mutex_enter(&sv->sv_mutex);
break;
}
case SMB_SERVER_STATE_CONFIGURED:
case SMB_SERVER_STATE_CREATED:
sv->sv_state = SMB_SERVER_STATE_DELETING;
break;
default:
ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
mutex_exit(&sv->sv_mutex);
smb_server_release(sv);
return (ENOTTY);
}
ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
sv->sv_refcnt--;
while (sv->sv_refcnt)
cv_wait(&sv->sv_cv, &sv->sv_mutex);
mutex_exit(&sv->sv_mutex);
smb_llist_enter(&smb_servers, RW_WRITER);
smb_llist_remove(&smb_servers, sv);
smb_llist_exit(&smb_servers);
smb_server_stop(sv);
rw_destroy(&sv->sv_cfg_lock);
smb_opipe_door_fini();
smb_kdoor_clnt_fini();
smb_server_kstat_fini(sv);
smb_llist_destructor(&sv->sv_vfs_list);
while ((ux = list_head(&sv->sv_unexport_list.sl_list)) != NULL) {
smb_slist_remove(&sv->sv_unexport_list, ux);
kmem_cache_free(sv->si_cache_unexport, ux);
}
smb_slist_destructor(&sv->sv_unexport_list);
kmem_cache_destroy(sv->si_cache_unexport);
kmem_cache_destroy(sv->si_cache_vfs);
kmem_cache_destroy(sv->si_cache_request);
kmem_cache_destroy(sv->si_cache_session);
kmem_cache_destroy(sv->si_cache_user);
kmem_cache_destroy(sv->si_cache_tree);
kmem_cache_destroy(sv->si_cache_ofile);
kmem_cache_destroy(sv->si_cache_odir);
smb_thread_destroy(&sv->si_thread_timers);
smb_thread_destroy(&sv->si_thread_unexport);
mutex_destroy(&sv->sv_mutex);
cv_destroy(&sv->sv_cv);
sv->sv_magic = 0;
kmem_free(sv, sizeof (smb_server_t));
return (0);
}
/*
* smb_server_configure
*/
int
smb_server_configure(smb_ioc_cfg_t *ioc)
{
int rc = 0;
smb_server_t *sv;
rc = smb_server_lookup(&sv);
if (rc)
return (rc);
mutex_enter(&sv->sv_mutex);
switch (sv->sv_state) {
case SMB_SERVER_STATE_CREATED:
smb_server_store_cfg(sv, ioc);
sv->sv_state = SMB_SERVER_STATE_CONFIGURED;
break;
case SMB_SERVER_STATE_CONFIGURED:
smb_server_store_cfg(sv, ioc);
break;
case SMB_SERVER_STATE_RUNNING:
rw_enter(&sv->sv_cfg_lock, RW_WRITER);
smb_server_store_cfg(sv, ioc);
rw_exit(&sv->sv_cfg_lock);
break;
default:
ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
rc = EFAULT;
break;
}
mutex_exit(&sv->sv_mutex);
smb_server_release(sv);
return (rc);
}
/*
* smb_server_start
*/
int
smb_server_start(smb_ioc_start_t *ioc)
{
int rc = 0;
smb_server_t *sv;
rc = smb_server_lookup(&sv);
if (rc)
return (rc);
mutex_enter(&sv->sv_mutex);
switch (sv->sv_state) {
case SMB_SERVER_STATE_CONFIGURED:
sv->sv_thread_pool = taskq_create("smb_workers",
sv->sv_cfg.skc_maxworkers, SMB_WORKER_PRIORITY,
sv->sv_cfg.skc_maxworkers, INT_MAX,
TASKQ_DYNAMIC|TASKQ_PREPOPULATE);
sv->sv_session = smb_session_create(NULL, 0, sv, 0);
if (sv->sv_thread_pool == NULL || sv->sv_session == NULL) {
rc = ENOMEM;
break;
}
if (rc = smb_server_fsop_start(sv))
break;
ASSERT(sv->sv_lmshrd == NULL);
sv->sv_lmshrd = smb_kshare_init(ioc->lmshrd);
if (sv->sv_lmshrd == NULL)
break;
if (rc = smb_kdoor_clnt_open(ioc->udoor))
break;
if (rc = smb_thread_start(&sv->si_thread_timers))
break;
if (rc = smb_thread_start(&sv->si_thread_unexport))
break;
if (rc = smb_opipe_door_open(ioc->opipe)) {
cmn_err(CE_WARN, "Cannot open opipe door");
break;
}
(void) oem_language_set("english");
sv->sv_state = SMB_SERVER_STATE_RUNNING;
mutex_exit(&sv->sv_mutex);
smb_server_release(sv);
return (0);
default:
ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
(sv->sv_state == SMB_SERVER_STATE_RUNNING) ||
(sv->sv_state == SMB_SERVER_STATE_DELETING));
mutex_exit(&sv->sv_mutex);
smb_server_release(sv);
return (ENOTTY);
}
smb_server_stop(sv);
mutex_exit(&sv->sv_mutex);
smb_server_release(sv);
return (rc);
}
/*
* smb_server_nbt_listen: SMB-over-NetBIOS service
*
* Traditional SMB service over NetBIOS (port 139), which requires
* that a NetBIOS session be established.
*/
int
smb_server_nbt_listen(smb_ioc_listen_t *ioc)
{
smb_server_t *sv;
int rc;
rc = smb_server_lookup(&sv);
if (rc)
return (rc);
mutex_enter(&sv->sv_mutex);
switch (sv->sv_state) {
case SMB_SERVER_STATE_RUNNING:
if ((sv->sv_nbt_daemon.ld_kth != NULL) &&
(sv->sv_nbt_daemon.ld_kth != curthread)) {
mutex_exit(&sv->sv_mutex);
smb_server_release(sv);
return (EACCES);
} else {
sv->sv_nbt_daemon.ld_kth = curthread;
sv->sv_nbt_daemon.ld_ktdid = curthread->t_did;
}
break;
default:
ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
(sv->sv_state == SMB_SERVER_STATE_CONFIGURED) ||
(sv->sv_state == SMB_SERVER_STATE_DELETING));
mutex_exit(&sv->sv_mutex);
smb_server_release(sv);
return (EFAULT);
}
mutex_exit(&sv->sv_mutex);
/*
* netbios must be ipv4
*/
rc = smb_server_listen(sv, &sv->sv_nbt_daemon, IPPORT_NETBIOS_SSN,
AF_INET, ioc->error);
if (rc) {
mutex_enter(&sv->sv_mutex);
sv->sv_nbt_daemon.ld_kth = NULL;
mutex_exit(&sv->sv_mutex);
}
smb_server_release(sv);
return (rc);
}
int
smb_server_tcp_listen(smb_ioc_listen_t *ioc)
{
smb_server_t *sv;
int rc;
rc = smb_server_lookup(&sv);
if (rc)
return (rc);
mutex_enter(&sv->sv_mutex);
switch (sv->sv_state) {
case SMB_SERVER_STATE_RUNNING:
if ((sv->sv_tcp_daemon.ld_kth) &&
(sv->sv_tcp_daemon.ld_kth != curthread)) {
mutex_exit(&sv->sv_mutex);
smb_server_release(sv);
return (EACCES);
} else {
sv->sv_tcp_daemon.ld_kth = curthread;
sv->sv_tcp_daemon.ld_ktdid = curthread->t_did;
}
break;
default:
ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
(sv->sv_state == SMB_SERVER_STATE_CONFIGURED) ||
(sv->sv_state == SMB_SERVER_STATE_DELETING));
mutex_exit(&sv->sv_mutex);
smb_server_release(sv);
return (EFAULT);
}
mutex_exit(&sv->sv_mutex);
if (sv->sv_cfg.skc_ipv6_enable)
rc = smb_server_listen(sv, &sv->sv_tcp_daemon,
IPPORT_SMB, AF_INET6, ioc->error);
else
rc = smb_server_listen(sv, &sv->sv_tcp_daemon,
IPPORT_SMB, AF_INET, ioc->error);
if (rc) {
mutex_enter(&sv->sv_mutex);
sv->sv_tcp_daemon.ld_kth = NULL;
mutex_exit(&sv->sv_mutex);
}
smb_server_release(sv);
return (rc);
}
/*
* smb_server_nbt_receive
*/
int
smb_server_nbt_receive(void)
{
int rc;
smb_server_t *sv;
if ((rc = smb_server_lookup(&sv)) == 0) {
rc = smb_session_daemon(&sv->sv_nbt_daemon.ld_session_list);
smb_server_release(sv);
}
return (rc);
}
/*
* smb_server_tcp_receive
*/
int
smb_server_tcp_receive(void)
{
int rc;
smb_server_t *sv;
if ((rc = smb_server_lookup(&sv)) == 0) {
rc = smb_session_daemon(&sv->sv_tcp_daemon.ld_session_list);
smb_server_release(sv);
}
return (rc);
}
int
smb_server_set_gmtoff(smb_ioc_gmt_t *ioc)
{
int rc;
smb_server_t *sv;
if ((rc = smb_server_lookup(&sv)) == 0) {
sv->si_gmtoff = ioc->offset;
smb_server_release(sv);
}
return (rc);
}
int
smb_server_numopen(smb_ioc_opennum_t *ioc)
{
smb_server_t *sv;
int rc;
if ((rc = smb_server_lookup(&sv)) == 0) {
ioc->open_users = sv->sv_open_users;
ioc->open_trees = sv->sv_open_trees;
ioc->open_files = sv->sv_open_files;
smb_server_release(sv);
}
return (rc);
}
/*
* Enumerate objects within the server. The svcenum provides the
* enumeration context, i.e. what the caller want to get back.
*/
int
smb_server_enum(smb_ioc_svcenum_t *ioc)
{
smb_svcenum_t *svcenum = &ioc->svcenum;
smb_server_t *sv;
smb_session_list_t *se;
int rc;
switch (svcenum->se_type) {
case SMB_SVCENUM_TYPE_USER:
case SMB_SVCENUM_TYPE_TREE:
case SMB_SVCENUM_TYPE_FILE:
break;
default:
return (EINVAL);
}
if ((rc = smb_server_lookup(&sv)) != 0)
return (rc);
svcenum->se_bavail = svcenum->se_buflen;
svcenum->se_bused = 0;
svcenum->se_nitems = 0;
se = &sv->sv_nbt_daemon.ld_session_list;
smb_server_enum_private(se, svcenum);
se = &sv->sv_tcp_daemon.ld_session_list;
smb_server_enum_private(se, svcenum);
smb_server_release(sv);
return (0);
}
/*
* Look for sessions to disconnect by client and user name.
*/
int
smb_server_session_close(smb_ioc_session_t *ioc)
{
smb_session_list_t *se;
smb_server_t *sv;
int nbt_cnt;
int tcp_cnt;
int rc;
if ((rc = smb_server_lookup(&sv)) != 0)
return (rc);
se = &sv->sv_nbt_daemon.ld_session_list;
nbt_cnt = smb_server_sesion_disconnect(se, ioc->client, ioc->username);
se = &sv->sv_tcp_daemon.ld_session_list;
tcp_cnt = smb_server_sesion_disconnect(se, ioc->client, ioc->username);
smb_server_release(sv);
if ((nbt_cnt == 0) && (tcp_cnt == 0))
return (ENOENT);
return (0);
}
/*
* Close a file by uniqid.
*/
int
smb_server_file_close(smb_ioc_fileid_t *ioc)
{
uint32_t uniqid = ioc->uniqid;
smb_session_list_t *se;
smb_server_t *sv;
int rc;
if ((rc = smb_server_lookup(&sv)) != 0)
return (rc);
se = &sv->sv_nbt_daemon.ld_session_list;
rc = smb_server_fclose(se, uniqid);
if (rc == ENOENT) {
se = &sv->sv_tcp_daemon.ld_session_list;
rc = smb_server_fclose(se, uniqid);
}
smb_server_release(sv);
return (rc);
}
/*
* These functions determine the relevant smb server to which the call apply.
*/
uint32_t
smb_server_get_session_count(void)
{
smb_server_t *sv;
uint32_t counter = 0;
if (smb_server_lookup(&sv))
return (0);
rw_enter(&sv->sv_nbt_daemon.ld_session_list.se_lock, RW_READER);
counter = sv->sv_nbt_daemon.ld_session_list.se_act.count;
rw_exit(&sv->sv_nbt_daemon.ld_session_list.se_lock);
rw_enter(&sv->sv_tcp_daemon.ld_session_list.se_lock, RW_READER);
counter += sv->sv_tcp_daemon.ld_session_list.se_act.count;
rw_exit(&sv->sv_tcp_daemon.ld_session_list.se_lock);
smb_server_release(sv);
return (counter);
}
/*
* smb_server_disconnect_share
*
* Disconnects the specified share. This function should be called after the
* share passed in has been made unavailable by the "share manager".
*/
static void
smb_server_disconnect_share(char *sharename, smb_server_t *sv)
{
smb_session_disconnect_share(&sv->sv_nbt_daemon.ld_session_list,
sharename);
smb_session_disconnect_share(&sv->sv_tcp_daemon.ld_session_list,
sharename);
}
/*
* smb_server_share_export()
*
* This function handles kernel processing at share enable time.
*
* At share-enable time (LMSHRD_ADD), the file system corresponding to
* the share is checked for characteristics that are required for SMB
* sharing. If this check passes, then a hold is taken on the root vnode
* of the file system (or a reference count on the corresponding smb_vfs_t
* is bumped), preventing an unmount. (See smb_vfs_hold()).
*/
int
smb_server_share_export(smb_ioc_share_t *ioc)
{
smb_server_t *sv;
int error = 0;
smb_node_t *fnode = NULL;
smb_node_t *dnode;
char last_comp[MAXNAMELEN];
smb_request_t *sr;
if (smb_server_lookup(&sv))
return (EINVAL);
mutex_enter(&sv->sv_mutex);
switch (sv->sv_state) {
case SMB_SERVER_STATE_RUNNING:
break;
default:
mutex_exit(&sv->sv_mutex);
return (ENOTACTIVE);
}
mutex_exit(&sv->sv_mutex);
sr = smb_request_alloc(sv->sv_session, 0);
if (sr == NULL) {
smb_server_release(sv);
return (ENOMEM);
}
sr->user_cr = kcred;
error = smb_pathname_reduce(sr, kcred, ioc->path,
NULL, NULL, &dnode, last_comp);
if (error) {
smb_request_free(sr);
smb_server_release(sv);
return (error);
}
error = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS,
sv->si_root_smb_node, dnode, last_comp, &fnode);
smb_node_release(dnode);
if (error) {
smb_request_free(sr);
smb_server_release(sv);
return (error);
}
ASSERT(fnode->vp && fnode->vp->v_vfsp);
#ifdef SMB_ENFORCE_NODEV
if (vfs_optionisset(fnode->vp->v_vfsp, MNTOPT_NODEVICES, NULL) == 0) {
smb_node_release(fnode);
smb_request_free(sr);
smb_server_release(sv);
return (EINVAL);
}
#endif /* SMB_ENFORCE_NODEV */
if (!smb_vfs_hold(sv, fnode->vp->v_vfsp))
error = ENOMEM;
/*
* The refcount on the smb_vfs has been incremented.
* If it wasn't already, a hold has also been taken
* on the root vnode of the file system.
*/
smb_node_release(fnode);
smb_request_free(sr);
smb_server_release(sv);
return (error);
}
/*
* smb_server_share_unexport()
*
* This function is invoked when a share is disabled to disconnect trees
* and close files. Cleaning up may involve VOP and/or VFS calls, which
* may conflict/deadlock with stuck threads if something is amiss with the
* file system. Queueing the request for asynchronous processing allows the
* call to return immediately so that, if the unshare is being done in the
* context of a forced unmount, the forced unmount will always be able to
* proceed (unblocking stuck I/O and eventually allowing all blocked unshare
* processes to complete).
*
* The path lookup to find the root vnode of the VFS in question and the
* release of this vnode are done synchronously prior to any associated
* unmount. Doing these asynchronous to an associated unmount could run
* the risk of a spurious EBUSY for a standard unmount or an EIO during
* the path lookup due to a forced unmount finishing first.
*/
int
smb_server_share_unexport(smb_ioc_share_t *ioc)
{
smb_server_t *sv;
smb_request_t *sr;
smb_unexport_t *ux;
smb_node_t *fnode = NULL;
smb_node_t *dnode;
char last_comp[MAXNAMELEN];
int rc;
if ((rc = smb_server_lookup(&sv)))
return (rc);
mutex_enter(&sv->sv_mutex);
switch (sv->sv_state) {
case SMB_SERVER_STATE_RUNNING:
break;
default:
mutex_exit(&sv->sv_mutex);
return (ENOTACTIVE);
}
mutex_exit(&sv->sv_mutex);
sr = smb_request_alloc(sv->sv_session, 0);
if (sr == NULL) {
smb_server_release(sv);
return (ENOMEM);
}
sr->user_cr = kcred;
rc = smb_pathname_reduce(sr, kcred, ioc->path, NULL, NULL,
&dnode, last_comp);
if (rc) {
smb_request_free(sr);
smb_server_release(sv);
return (rc);
}
rc = smb_fsop_lookup(sr, kcred, SMB_FOLLOW_LINKS, sv->si_root_smb_node,
dnode, last_comp, &fnode);
smb_node_release(dnode);
smb_request_free(sr);
if (rc) {
smb_server_release(sv);
return (rc);
}
ASSERT(fnode->vp && fnode->vp->v_vfsp);
smb_vfs_rele(sv, fnode->vp->v_vfsp);
smb_node_release(fnode);
ux = kmem_cache_alloc(sv->si_cache_unexport, KM_SLEEP);
(void) strlcpy(ux->ux_sharename, ioc->name, MAXNAMELEN);
smb_slist_insert_tail(&sv->sv_unexport_list, ux);
smb_thread_signal(&sv->si_thread_unexport);
smb_server_release(sv);
return (0);
}
/*
* smb_server_thread_unexport
*
* This function processes the unexport event list and disconnects shares
* asynchronously. The function executes as a zone-specific thread.
*
* The server arg passed in is safe to use without a reference count, because
* the server cannot be deleted until smb_thread_stop()/destroy() return,
* which is also when the thread exits.
*/
static void
smb_server_thread_unexport(smb_thread_t *thread, void *arg)
{
smb_server_t *sv = (smb_server_t *)arg;
smb_unexport_t *ux;
while (smb_thread_continue(thread)) {
while ((ux = list_head(&sv->sv_unexport_list.sl_list))
!= NULL) {
smb_slist_remove(&sv->sv_unexport_list, ux);
smb_server_disconnect_share(ux->ux_sharename, sv);
kmem_cache_free(sv->si_cache_unexport, ux);
}
}
}
/*
* This is a special interface that will be utilized by ZFS to cause a share to
* be added/removed.
*
* arg is either a lmshare_info_t or share_name from userspace.
* It will need to be copied into the kernel. It is lmshare_info_t
* for add operations and share_name for delete operations.
*/
int
smb_server_share(void *arg, boolean_t add_share)
{
smb_server_t *sv;
int rc;
rc = smb_server_lookup(&sv);
if (rc == 0) {
mutex_enter(&sv->sv_mutex);
switch (sv->sv_state) {
case SMB_SERVER_STATE_RUNNING:
mutex_exit(&sv->sv_mutex);
(void) smb_kshare_upcall(sv->sv_lmshrd, arg, add_share);
break;
default:
mutex_exit(&sv->sv_mutex);
break;
}
smb_server_release(sv);
}
return (0);
}
/*
* *****************************************************************************
* **************** Functions called from the internal layers ******************
* *****************************************************************************
*
* These functions are provided the relevant smb server by the caller.
*/
void
smb_server_reconnection_check(smb_server_t *sv, smb_session_t *session)
{
ASSERT(sv == session->s_server);
smb_session_reconnection_check(&sv->sv_nbt_daemon.ld_session_list,
session);
smb_session_reconnection_check(&sv->sv_tcp_daemon.ld_session_list,
session);
}
void
smb_server_get_cfg(smb_server_t *sv, smb_kmod_cfg_t *cfg)
{
rw_enter(&sv->sv_cfg_lock, RW_READER);
bcopy(&sv->sv_cfg, cfg, sizeof (*cfg));
rw_exit(&sv->sv_cfg_lock);
}
/*
* *****************************************************************************
* *************************** Static Functions ********************************
* *****************************************************************************
*/
static void
smb_server_timers(smb_thread_t *thread, void *arg)
{
smb_server_t *sv = (smb_server_t *)arg;
ASSERT(sv != NULL);
while (smb_thread_continue_timedwait(thread, 1 /* Seconds */)) {
smb_session_timers(&sv->sv_nbt_daemon.ld_session_list);
smb_session_timers(&sv->sv_tcp_daemon.ld_session_list);
}
}
/*
* smb_server_kstat_init
*/
static int
smb_server_kstat_init(smb_server_t *sv)
{
(void) snprintf(sv->sv_ksp_name, sizeof (sv->sv_ksp_name), "%s%d",
SMBSRV_KSTAT_NAME, sv->sv_zid);
sv->sv_ksp = kstat_create(SMBSRV_KSTAT_MODULE, 0, sv->sv_ksp_name,
SMBSRV_KSTAT_CLASS, KSTAT_TYPE_NAMED,
sizeof (sv->sv_ks_data) / sizeof (kstat_named_t),
KSTAT_FLAG_VIRTUAL);
if (sv->sv_ksp) {
(void) strlcpy(sv->sv_ks_data.open_files.name, "open_files",
sizeof (sv->sv_ks_data.open_files.name));
sv->sv_ks_data.open_files.data_type = KSTAT_DATA_UINT32;
(void) strlcpy(sv->sv_ks_data.open_trees.name, "connections",
sizeof (sv->sv_ks_data.open_trees.name));
sv->sv_ks_data.open_trees.data_type = KSTAT_DATA_UINT32;
(void) strlcpy(sv->sv_ks_data.open_users.name, "sessions",
sizeof (sv->sv_ks_data.open_users.name));
sv->sv_ks_data.open_users.data_type = KSTAT_DATA_UINT32;
mutex_init(&sv->sv_ksp_mutex, NULL, MUTEX_DEFAULT, NULL);
sv->sv_ksp->ks_lock = &sv->sv_ksp_mutex;
sv->sv_ksp->ks_data = (void *)&sv->sv_ks_data;
sv->sv_ksp->ks_update = smb_server_kstat_update_info;
kstat_install(sv->sv_ksp);
}
/* create and initialize smb kstats - smb_dispatch stats */
smb_dispatch_kstat_init();
return (0);
}
/*
* smb_server_kstat_fini
*/
static void
smb_server_kstat_fini(smb_server_t *sv)
{
if (sv->sv_ksp) {
kstat_delete(sv->sv_ksp);
mutex_destroy(&sv->sv_ksp_mutex);
sv->sv_ksp = NULL;
}
smb_dispatch_kstat_fini();
}
/* ARGSUSED */
static int
smb_server_kstat_update_info(kstat_t *ksp, int rw)
{
smb_server_t *sv;
if (rw == KSTAT_WRITE) {
return (EACCES);
} else {
ASSERT(MUTEX_HELD(ksp->ks_lock));
_NOTE(LINTED("pointer cast may result in improper alignment"))
sv = (smb_server_t *)((uint8_t *)(ksp->ks_data) -
offsetof(smb_server_t, sv_ks_data));
ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
sv->sv_ks_data.open_files.value.ui32 = sv->sv_open_files;
sv->sv_ks_data.open_trees.value.ui32 = sv->sv_open_trees;
sv->sv_ks_data.open_users.value.ui32 = sv->sv_open_users;
}
return (0);
}
/*
* smb_server_stop
*
* The mutex of the server must have been entered before calling this function.
*/
static void
smb_server_stop(smb_server_t *sv)
{
ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
smb_opipe_door_close();
smb_thread_stop(&sv->si_thread_timers);
smb_thread_stop(&sv->si_thread_unexport);
smb_kdoor_clnt_close();
smb_kshare_fini(sv->sv_lmshrd);
sv->sv_lmshrd = NULL;
smb_server_fsop_stop(sv);
if (sv->sv_session) {
smb_session_delete(sv->sv_session);
sv->sv_session = NULL;
}
if (sv->sv_thread_pool) {
taskq_destroy(sv->sv_thread_pool);
sv->sv_thread_pool = NULL;
}
}
static int
smb_server_listen(
smb_server_t *sv,
smb_listener_daemon_t *ld,
in_port_t port,
int family,
int pthread_create_error)
{
int rc;
ksocket_t s_so;
const uint32_t on = 1;
const uint32_t off = 0;
smb_session_t *session;
if (pthread_create_error) {
/*
* Delete the last session created. The user space thread
* creation failed.
*/
smb_session_list_delete_tail(&ld->ld_session_list);
}
if (ld->ld_so == NULL) {
/* First time listener */
if (family == AF_INET) {
ld->ld_sin.sin_family = (uint32_t)family;
ld->ld_sin.sin_port = htons(port);
ld->ld_sin.sin_addr.s_addr = htonl(INADDR_ANY);
} else {
ld->ld_sin6.sin6_family = (uint32_t)family;
ld->ld_sin6.sin6_port = htons(port);
(void) memset(&ld->ld_sin6.sin6_addr.s6_addr, 0,
sizeof (ld->ld_sin6.sin6_addr.s6_addr));
}
ld->ld_so = smb_socreate(family, SOCK_STREAM, 0);
if (ld->ld_so) {
(void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET,
SO_MAC_EXEMPT, &off, sizeof (off), CRED());
(void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET,
SO_REUSEADDR, &on, sizeof (on), CRED());
if (family == AF_INET) {
rc = ksocket_bind(ld->ld_so,
(struct sockaddr *)&ld->ld_sin,
sizeof (ld->ld_sin), CRED());
} else {
rc = ksocket_bind(ld->ld_so,
(struct sockaddr *)&ld->ld_sin6,
sizeof (ld->ld_sin6), CRED());
}
if (rc == 0) {
rc = ksocket_listen(ld->ld_so, 20, CRED());
if (rc < 0) {
cmn_err(CE_WARN,
"Port %d: listen failed", port);
smb_soshutdown(ld->ld_so);
smb_sodestroy(ld->ld_so);
ld->ld_so = NULL;
return (rc);
}
} else {
cmn_err(CE_WARN,
"Port %d: bind failed", port);
smb_soshutdown(ld->ld_so);
smb_sodestroy(ld->ld_so);
ld->ld_so = NULL;
return (rc);
}
} else {
cmn_err(CE_WARN,
"Port %d: socket create failed", port);
return (ENOMEM);
}
}
DTRACE_PROBE1(so__wait__accept, struct sonode *, ld->ld_so);
for (;;) {
rc = ksocket_accept(ld->ld_so, NULL, NULL, &s_so, CRED());
if (rc == 0) {
uint32_t txbuf_size = 128*1024;
uint32_t on = 1;
DTRACE_PROBE1(so__accept, struct sonode *, s_so);
(void) ksocket_setsockopt(s_so, IPPROTO_TCP,
TCP_NODELAY, &on, sizeof (on), CRED());
(void) ksocket_setsockopt(s_so, SOL_SOCKET,
SO_KEEPALIVE, &on, sizeof (on), CRED());
(void) ksocket_setsockopt(s_so, SOL_SOCKET, SO_SNDBUF,
(const void *)&txbuf_size, sizeof (txbuf_size),
CRED());
/*
* Create a session for this connection.
*/
session = smb_session_create(s_so, port, sv, family);
if (session) {
smb_session_list_append(&ld->ld_session_list,
session);
break;
} else {
smb_soshutdown(s_so);
smb_sodestroy(s_so);
}
continue;
}
smb_session_list_signal(&ld->ld_session_list);
smb_soshutdown(ld->ld_so);
smb_sodestroy(ld->ld_so);
ld->ld_so = NULL;
break;
}
return (rc);
}
/*
* smb_server_lookup
*
* This function tries to find the server associated with the zone of the
* caller.
*/
static int
smb_server_lookup(smb_server_t **psv)
{
zoneid_t zid;
smb_server_t *sv;
zid = getzoneid();
smb_llist_enter(&smb_servers, RW_READER);
sv = smb_llist_head(&smb_servers);
while (sv) {
ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
if (sv->sv_zid == zid) {
mutex_enter(&sv->sv_mutex);
if (sv->sv_state != SMB_SERVER_STATE_DELETING) {
sv->sv_refcnt++;
mutex_exit(&sv->sv_mutex);
smb_llist_exit(&smb_servers);
*psv = sv;
return (0);
}
mutex_exit(&sv->sv_mutex);
break;
}
sv = smb_llist_next(&smb_servers, sv);
}
smb_llist_exit(&smb_servers);
return (EPERM);
}
/*
* smb_server_release
*
* This function decrements the reference count of the server and signals its
* condition variable if the state of the server is SMB_SERVER_STATE_DELETING.
*/
static void
smb_server_release(smb_server_t *sv)
{
ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
mutex_enter(&sv->sv_mutex);
ASSERT(sv->sv_refcnt);
sv->sv_refcnt--;
if ((sv->sv_refcnt == 0) && (sv->sv_state == SMB_SERVER_STATE_DELETING))
cv_signal(&sv->sv_cv);
mutex_exit(&sv->sv_mutex);
}
/*
* Enumerate the users associated with a session list.
*/
static void
smb_server_enum_private(smb_session_list_t *se, smb_svcenum_t *svcenum)
{
smb_session_t *sn;
smb_llist_t *ulist;
smb_user_t *user;
int rc = 0;
rw_enter(&se->se_lock, RW_READER);
sn = list_head(&se->se_act.lst);
while (sn != NULL) {
ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
ulist = &sn->s_user_list;
smb_llist_enter(ulist, RW_READER);
user = smb_llist_head(ulist);
while (user != NULL) {
if (smb_user_hold(user)) {
rc = smb_user_enum(user, svcenum);
smb_user_release(user);
}
user = smb_llist_next(ulist, user);
}
smb_llist_exit(ulist);
if (rc != 0)
break;
sn = list_next(&se->se_act.lst, sn);
}
rw_exit(&se->se_lock);
}
/*
* Disconnect sessions associated with the specified client and username.
* Empty strings are treated as wildcards.
*/
static int
smb_server_sesion_disconnect(smb_session_list_t *se,
const char *client, const char *name)
{
smb_session_t *sn;
smb_llist_t *ulist;
smb_user_t *user;
boolean_t match;
int count = 0;
rw_enter(&se->se_lock, RW_READER);
sn = list_head(&se->se_act.lst);
while (sn != NULL) {
ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
if ((*client != '\0') && (!smb_session_isclient(sn, client))) {
sn = list_next(&se->se_act.lst, sn);
continue;
}
ulist = &sn->s_user_list;
smb_llist_enter(ulist, RW_READER);
user = smb_llist_head(ulist);
while (user != NULL) {
if (smb_user_hold(user)) {
match = (*name == '\0');
if (!match)
match = smb_user_namecmp(user, name);
if (match) {
smb_llist_exit(ulist);
smb_user_logoff(user);
++count;
smb_user_release(user);
smb_llist_enter(ulist, RW_READER);
user = smb_llist_head(ulist);
continue;
}
smb_user_release(user);
}
user = smb_llist_next(ulist, user);
}
smb_llist_exit(ulist);
sn = list_next(&se->se_act.lst, sn);
}
rw_exit(&se->se_lock);
return (count);
}
/*
* Close a file by its unique id.
*/
static int
smb_server_fclose(smb_session_list_t *se, uint32_t uniqid)
{
smb_session_t *sn;
smb_llist_t *ulist;
smb_user_t *user;
int rc = ENOENT;
rw_enter(&se->se_lock, RW_READER);
sn = list_head(&se->se_act.lst);
while ((sn != NULL) && (rc == ENOENT)) {
ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
ulist = &sn->s_user_list;
smb_llist_enter(ulist, RW_READER);
user = smb_llist_head(ulist);
while ((user != NULL) && (rc == ENOENT)) {
if (smb_user_hold(user)) {
rc = smb_user_fclose(user, uniqid);
smb_user_release(user);
}
user = smb_llist_next(ulist, user);
}
smb_llist_exit(ulist);
sn = list_next(&se->se_act.lst, sn);
}
rw_exit(&se->se_lock);
return (rc);
}
static void
smb_server_store_cfg(smb_server_t *sv, smb_ioc_cfg_t *ioc)
{
if (ioc->maxconnections == 0)
ioc->maxconnections = 0xFFFFFFFF;
smb_session_correct_keep_alive_values(
&sv->sv_nbt_daemon.ld_session_list, ioc->keepalive);
smb_session_correct_keep_alive_values(
&sv->sv_tcp_daemon.ld_session_list, ioc->keepalive);
sv->sv_cfg.skc_maxworkers = ioc->maxworkers;
sv->sv_cfg.skc_maxconnections = ioc->maxconnections;
sv->sv_cfg.skc_keepalive = ioc->keepalive;
sv->sv_cfg.skc_restrict_anon = ioc->restrict_anon;
sv->sv_cfg.skc_signing_enable = ioc->signing_enable;
sv->sv_cfg.skc_signing_required = ioc->signing_required;
sv->sv_cfg.skc_oplock_enable = ioc->oplock_enable;
sv->sv_cfg.skc_sync_enable = ioc->sync_enable;
sv->sv_cfg.skc_secmode = ioc->secmode;
sv->sv_cfg.skc_ipv6_enable = ioc->ipv6_enable;
(void) strlcpy(sv->sv_cfg.skc_nbdomain, ioc->nbdomain,
sizeof (sv->sv_cfg.skc_nbdomain));
(void) strlcpy(sv->sv_cfg.skc_fqdn, ioc->fqdn,
sizeof (sv->sv_cfg.skc_fqdn));
(void) strlcpy(sv->sv_cfg.skc_hostname, ioc->hostname,
sizeof (sv->sv_cfg.skc_hostname));
(void) strlcpy(sv->sv_cfg.skc_system_comment, ioc->system_comment,
sizeof (sv->sv_cfg.skc_system_comment));
}
static int
smb_server_fsop_start(smb_server_t *sv)
{
int error;
error = smb_node_root_init(rootdir, sv, &sv->si_root_smb_node);
if (error != 0)
sv->si_root_smb_node = NULL;
return (error);
}
static void
smb_server_fsop_stop(smb_server_t *sv)
{
if (sv->si_root_smb_node != NULL) {
smb_vfs_rele_all(sv);
smb_node_release(sv->si_root_smb_node);
sv->si_root_smb_node = NULL;
}
}
|