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
|
/*
* Copyright (c) 2000-2001 Boris Popov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Boris Popov.
* 4. Neither the name of the author nor the names of any co-contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: smb_conn.c,v 1.27.166.1 2005/05/27 02:35:29 lindak Exp $
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2018 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Connection engine.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/vnode.h>
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/socketvar.h>
#include <sys/cred.h>
#include <netinet/in.h>
#include <inet/ip.h>
#include <inet/ip6.h>
#include <sys/cmn_err.h>
#include <sys/thread.h>
#include <sys/atomic.h>
#include <sys/u8_textprep.h>
#include <netsmb/smb_osdep.h>
#include <netsmb/smb.h>
#include <netsmb/smb2.h>
#include <netsmb/smb_conn.h>
#include <netsmb/smb_subr.h>
#include <netsmb/smb_tran.h>
#include <netsmb/smb_pass.h>
static struct smb_connobj smb_vclist;
void smb_co_init(struct smb_connobj *cp, int level, char *objname);
void smb_co_done(struct smb_connobj *cp);
void smb_co_hold(struct smb_connobj *cp);
void smb_co_rele(struct smb_connobj *cp);
void smb_co_kill(struct smb_connobj *cp);
static void smb_vc_free(struct smb_connobj *cp);
static void smb_vc_gone(struct smb_connobj *cp);
static void smb_share_free(struct smb_connobj *cp);
static void smb_share_gone(struct smb_connobj *cp);
static void smb_fh_free(struct smb_connobj *cp);
static void smb_fh_gone(struct smb_connobj *cp);
int
smb_sm_init(void)
{
smb_co_init(&smb_vclist, SMBL_SM, "smbsm");
return (0);
}
int
smb_sm_idle(void)
{
int error = 0;
SMB_CO_LOCK(&smb_vclist);
if (smb_vclist.co_usecount > 1) {
SMBSDEBUG("%d connections still active\n",
smb_vclist.co_usecount - 1);
error = EBUSY;
}
SMB_CO_UNLOCK(&smb_vclist);
return (error);
}
void
smb_sm_done(void)
{
/*
* Why are we not iterating on smb_vclist here?
* Because the caller has just called smb_sm_idle() to
* make sure we have no VCs before calling this.
*/
smb_co_done(&smb_vclist);
}
/*
* Common code for connection object
*/
/*ARGSUSED*/
void
smb_co_init(struct smb_connobj *cp, int level, char *objname)
{
mutex_init(&cp->co_lock, objname, MUTEX_DRIVER, NULL);
cp->co_level = level;
cp->co_usecount = 1;
SLIST_INIT(&cp->co_children);
}
/*
* Called just before free of an object
* of which smb_connobj is a part, i.e.
* _vc_free, _share_free, also sm_done.
*/
void
smb_co_done(struct smb_connobj *cp)
{
ASSERT(SLIST_EMPTY(&cp->co_children));
mutex_destroy(&cp->co_lock);
}
static void
smb_co_addchild(
struct smb_connobj *parent,
struct smb_connobj *child)
{
/*
* Set the child's pointer to the parent.
* No references yet, so no need to lock.
*/
ASSERT(child->co_usecount == 1);
child->co_parent = parent;
/*
* Add the child to the parent's list of
* children, and in-line smb_co_hold
*/
ASSERT(MUTEX_HELD(&parent->co_lock));
parent->co_usecount++;
SLIST_INSERT_HEAD(&parent->co_children, child, co_next);
}
void
smb_co_hold(struct smb_connobj *cp)
{
SMB_CO_LOCK(cp);
cp->co_usecount++;
SMB_CO_UNLOCK(cp);
}
/*
* Called via smb_vc_rele, smb_share_rele
*/
void
smb_co_rele(struct smb_connobj *co)
{
struct smb_connobj *parent;
int old_flags;
SMB_CO_LOCK(co);
/*
* When VC usecount goes from 2 to 1, signal the iod_idle CV.
* It's unfortunate to have object type-specific logic here,
* but it's hard to do this anywhere else.
*/
if (co->co_level == SMBL_VC && co->co_usecount == 2) {
smb_vc_t *vcp = CPTOVC(co);
cv_signal(&vcp->iod_idle);
}
if (co->co_usecount > 1) {
co->co_usecount--;
SMB_CO_UNLOCK(co);
return;
}
ASSERT(co->co_usecount == 1);
co->co_usecount = 0;
/*
* This list of children should be empty now.
* Check this while we're still linked, so
* we have a better chance of debugging.
*/
ASSERT(SLIST_EMPTY(&co->co_children));
/*
* OK, this element is going away.
*
* We need to drop the lock on this CO so we can take the
* parent CO lock. The _GONE flag prevents this CO from
* getting new references before we can unlink it from the
* parent list.
*
* The _GONE flag is also used to ensure that the co_gone
* function is called only once. Note that smb_co_kill may
* do this before we get here. If we find that the _GONE
* flag was not already set, then call the co_gone hook
* (smb_share_gone, smb_vc_gone) which will disconnect
* the share or the VC, respectively.
*
* Note the old: smb_co_gone(co, scred);
* is now in-line here.
*/
old_flags = co->co_flags;
co->co_flags |= SMBO_GONE;
SMB_CO_UNLOCK(co);
if ((old_flags & SMBO_GONE) == 0 && co->co_gone)
co->co_gone(co);
/*
* If we have a parent (only smb_vclist does not)
* then unlink from parent's list of children.
* We have the only reference to the child.
*/
parent = co->co_parent;
if (parent) {
SMB_CO_LOCK(parent);
ASSERT(SLIST_FIRST(&parent->co_children));
if (SLIST_FIRST(&parent->co_children)) {
SLIST_REMOVE(&parent->co_children, co,
smb_connobj, co_next);
}
SMB_CO_UNLOCK(parent);
}
/*
* Now it's safe to free the CO
*/
if (co->co_free) {
co->co_free(co);
}
/*
* Finally, if the CO had a parent, decrement
* the parent's hold count for the lost child.
*/
if (parent) {
/*
* Recursive call here (easier for debugging).
* Can only go two levels.
*/
smb_co_rele(parent);
}
}
/*
* Do just the first part of what co_gone does,
* i.e. tree disconnect, or disconnect a VC.
* This is used to forcibly close things.
*/
void
smb_co_kill(struct smb_connobj *co)
{
int old_flags;
SMB_CO_LOCK(co);
old_flags = co->co_flags;
co->co_flags |= SMBO_GONE;
SMB_CO_UNLOCK(co);
/*
* Do the same "call only once" logic here as in
* smb_co_rele, though it's probably not possible
* for this to be called after smb_co_rele.
*/
if ((old_flags & SMBO_GONE) == 0 && co->co_gone)
co->co_gone(co);
/* XXX: Walk list of children and kill those too? */
}
/*
* Session objects, which are referred to as "VC" for
* "virtual cirtuit". This has nothing to do with the
* CIFS notion of a "virtual cirtuit". See smb_conn.h
*/
void
smb_vc_hold(struct smb_vc *vcp)
{
smb_co_hold(VCTOCP(vcp));
}
void
smb_vc_rele(struct smb_vc *vcp)
{
smb_co_rele(VCTOCP(vcp));
}
void
smb_vc_kill(struct smb_vc *vcp)
{
smb_co_kill(VCTOCP(vcp));
}
/*
* Normally called via smb_vc_rele()
* after co_usecount drops to zero.
* Also called via: smb_vc_kill()
*
* Shutdown the VC to this server,
* invalidate shares linked with it.
*/
/*ARGSUSED*/
static void
smb_vc_gone(struct smb_connobj *cp)
{
struct smb_vc *vcp = CPTOVC(cp);
/*
* Was smb_vc_disconnect(vcp);
*/
smb_iod_disconnect(vcp);
}
/*
* The VC has no more references. Free it.
* No locks needed here.
*/
static void
smb_vc_free(struct smb_connobj *cp)
{
struct smb_vc *vcp = CPTOVC(cp);
/*
* The _gone call should have emptied the request list,
* but let's make sure, as requests may have references
* to this VC without taking a hold. (The hold is the
* responsibility of threads placing requests.)
*/
ASSERT(vcp->iod_rqlist.tqh_first == NULL);
if (vcp->vc_tdata)
SMB_TRAN_DONE(vcp);
/*
* We are not using the iconv routines here. So commenting them for now.
* REVISIT.
*/
#ifdef NOTYETDEFINED
if (vcp->vc_tolower)
iconv_close(vcp->vc_tolower);
if (vcp->vc_toupper)
iconv_close(vcp->vc_toupper);
if (vcp->vc_tolocal)
iconv_close(vcp->vc_tolocal);
if (vcp->vc_toserver)
iconv_close(vcp->vc_toserver);
#endif
if (vcp->vc_mackey != NULL)
kmem_free(vcp->vc_mackey, vcp->vc_mackeylen);
if (vcp->vc_ssnkey != NULL)
kmem_free(vcp->vc_ssnkey, vcp->vc_ssnkeylen);
cv_destroy(&vcp->iod_muxwait);
cv_destroy(&vcp->iod_idle);
rw_destroy(&vcp->iod_rqlock);
cv_destroy(&vcp->vc_statechg);
smb_co_done(VCTOCP(vcp));
kmem_free(vcp, sizeof (*vcp));
}
/*ARGSUSED*/
int
smb_vc_create(smbioc_ossn_t *ossn, smb_cred_t *scred, smb_vc_t **vcpp)
{
static char objtype[] = "smb_vc";
cred_t *cr = scred->scr_cred;
struct smb_vc *vcp;
int error = 0;
ASSERT(MUTEX_HELD(&smb_vclist.co_lock));
vcp = kmem_zalloc(sizeof (struct smb_vc), KM_SLEEP);
smb_co_init(VCTOCP(vcp), SMBL_VC, objtype);
vcp->vc_co.co_free = smb_vc_free;
vcp->vc_co.co_gone = smb_vc_gone;
cv_init(&vcp->vc_statechg, objtype, CV_DRIVER, NULL);
rw_init(&vcp->iod_rqlock, objtype, RW_DRIVER, NULL);
cv_init(&vcp->iod_idle, objtype, CV_DRIVER, NULL);
cv_init(&vcp->iod_muxwait, objtype, CV_DRIVER, NULL);
/* Expanded TAILQ_HEAD_INITIALIZER */
vcp->iod_rqlist.tqh_last = &vcp->iod_rqlist.tqh_first;
/* A brand new VC should connect. */
vcp->vc_state = SMBIOD_ST_RECONNECT;
/*
* These identify the connection.
*/
vcp->vc_zoneid = getzoneid();
bcopy(ossn, &vcp->vc_ssn, sizeof (*ossn));
/* This fills in vcp->vc_tdata */
vcp->vc_tdesc = &smb_tran_nbtcp_desc;
if ((error = SMB_TRAN_CREATE(vcp, cr)) != 0)
goto errout;
/* Success! */
smb_co_addchild(&smb_vclist, VCTOCP(vcp));
*vcpp = vcp;
return (0);
errout:
/*
* This will destroy the new vc.
* See: smb_vc_free
*/
smb_vc_rele(vcp);
return (error);
}
/*
* Find or create a VC identified by the info in ossn
* and return it with a "hold", but not locked.
*/
/*ARGSUSED*/
int
smb_vc_findcreate(smbioc_ossn_t *ossn, smb_cred_t *scred, smb_vc_t **vcpp)
{
struct smb_connobj *co;
struct smb_vc *vcp;
smbioc_ssn_ident_t *vc_id;
int error;
zoneid_t zoneid = getzoneid();
*vcpp = vcp = NULL;
SMB_CO_LOCK(&smb_vclist);
/* var, head, next_field */
SLIST_FOREACH(co, &smb_vclist.co_children, co_next) {
vcp = CPTOVC(co);
/*
* Some things we can check without
* holding the lock (those that are
* set at creation and never change).
*/
/* VCs in other zones are invisibile. */
if (vcp->vc_zoneid != zoneid)
continue;
/* Also segregate by Unix owner. */
if (vcp->vc_owner != ossn->ssn_owner)
continue;
/*
* Compare identifying info:
* server address, user, domain
* names are case-insensitive
*/
vc_id = &vcp->vc_ssn.ssn_id;
if (bcmp(&vc_id->id_srvaddr,
&ossn->ssn_id.id_srvaddr,
sizeof (vc_id->id_srvaddr)))
continue;
if (u8_strcmp(vc_id->id_user, ossn->ssn_id.id_user, 0,
U8_STRCMP_CI_LOWER, U8_UNICODE_LATEST, &error))
continue;
if (u8_strcmp(vc_id->id_domain, ossn->ssn_id.id_domain, 0,
U8_STRCMP_CI_LOWER, U8_UNICODE_LATEST, &error))
continue;
/*
* We have a match, but still have to check
* the _GONE flag, and do that with a lock.
* No new references when _GONE is set.
*
* Also clear SMBVOPT_CREATE which the caller
* may check to find out if we did create.
*/
SMB_VC_LOCK(vcp);
if ((vcp->vc_flags & SMBV_GONE) == 0) {
ossn->ssn_vopt &= ~SMBVOPT_CREATE;
/*
* Return it held, unlocked.
* In-line smb_vc_hold here.
*/
co->co_usecount++;
SMB_VC_UNLOCK(vcp);
*vcpp = vcp;
error = 0;
goto out;
}
SMB_VC_UNLOCK(vcp);
/* keep looking. */
}
vcp = NULL;
/* Note: smb_vclist is still locked. */
if (ossn->ssn_vopt & SMBVOPT_CREATE) {
/*
* Create a new VC. It starts out with
* hold count = 1, so don't incr. here.
*/
error = smb_vc_create(ossn, scred, &vcp);
if (error == 0)
*vcpp = vcp;
} else
error = ENOENT;
out:
SMB_CO_UNLOCK(&smb_vclist);
return (error);
}
/*
* Helper functions that operate on VCs
*/
/*
* Get a pointer to the IP address suitable for passing to Trusted
* Extensions find_tpc() routine. Used by smbfs_mount_label_policy().
* Compare this code to nfs_mount_label_policy() if problems arise.
*/
void *
smb_vc_getipaddr(struct smb_vc *vcp, int *ipvers)
{
smbioc_ssn_ident_t *id = &vcp->vc_ssn.ssn_id;
void *ret;
switch (id->id_srvaddr.sa.sa_family) {
case AF_INET:
*ipvers = IPV4_VERSION;
ret = &id->id_srvaddr.sin.sin_addr;
break;
case AF_INET6:
*ipvers = IPV6_VERSION;
ret = &id->id_srvaddr.sin6.sin6_addr;
break;
default:
SMBSDEBUG("invalid address family %d\n",
id->id_srvaddr.sa.sa_family);
*ipvers = 0;
ret = NULL;
break;
}
return (ret);
}
void
smb_vc_walkshares(struct smb_vc *vcp,
walk_share_func_t func)
{
smb_connobj_t *co;
smb_share_t *ssp;
/*
* Walk the share list calling func(ssp, arg)
*/
SMB_VC_LOCK(vcp);
SLIST_FOREACH(co, &(VCTOCP(vcp)->co_children), co_next) {
ssp = CPTOSS(co);
SMB_SS_LOCK(ssp);
func(ssp);
SMB_SS_UNLOCK(ssp);
}
SMB_VC_UNLOCK(vcp);
}
/*
* Share implementation
*/
void
smb_share_hold(struct smb_share *ssp)
{
smb_co_hold(SSTOCP(ssp));
}
void
smb_share_rele(struct smb_share *ssp)
{
smb_co_rele(SSTOCP(ssp));
}
void
smb_share_kill(struct smb_share *ssp)
{
smb_co_kill(SSTOCP(ssp));
}
/*
* Normally called via smb_share_rele()
* after co_usecount drops to zero.
* Also called via: smb_share_kill()
*/
static void
smb_share_gone(struct smb_connobj *cp)
{
struct smb_cred scred;
struct smb_share *ssp = CPTOSS(cp);
smb_vc_t *vcp = SSTOVC(ssp);
smb_credinit(&scred, NULL);
smb_iod_shutdown_share(ssp);
if (vcp->vc_flags & SMBV_SMB2)
(void) smb2_smb_treedisconnect(ssp, &scred);
else
(void) smb_smb_treedisconnect(ssp, &scred);
smb_credrele(&scred);
}
/*
* Normally called via smb_share_rele()
* after co_usecount drops to zero.
*/
static void
smb_share_free(struct smb_connobj *cp)
{
struct smb_share *ssp = CPTOSS(cp);
cv_destroy(&ssp->ss_conn_done);
smb_co_done(SSTOCP(ssp));
kmem_free(ssp, sizeof (*ssp));
}
/*
* Allocate share structure and attach it to the given VC
* Connection expected to be locked on entry. Share will be returned
* in locked state.
*/
/*ARGSUSED*/
int
smb_share_create(smbioc_tcon_t *tcon, struct smb_vc *vcp,
struct smb_share **sspp, struct smb_cred *scred)
{
static char objtype[] = "smb_ss";
struct smb_share *ssp;
ASSERT(MUTEX_HELD(&vcp->vc_lock));
ssp = kmem_zalloc(sizeof (struct smb_share), KM_SLEEP);
smb_co_init(SSTOCP(ssp), SMBL_SHARE, objtype);
ssp->ss_co.co_free = smb_share_free;
ssp->ss_co.co_gone = smb_share_gone;
cv_init(&ssp->ss_conn_done, objtype, CV_DRIVER, NULL);
ssp->ss_tid = SMB_TID_UNKNOWN;
ssp->ss2_tree_id = SMB2_TID_UNKNOWN;
bcopy(&tcon->tc_sh, &ssp->ss_ioc,
sizeof (smbioc_oshare_t));
smb_co_addchild(VCTOCP(vcp), SSTOCP(ssp));
*sspp = ssp;
return (0);
}
/*
* Find or create a share under the given VC
* and return it with a "hold", but not locked.
*/
int
smb_share_findcreate(smbioc_tcon_t *tcon, struct smb_vc *vcp,
struct smb_share **sspp, struct smb_cred *scred)
{
struct smb_connobj *co;
struct smb_share *ssp = NULL;
int error = 0;
*sspp = NULL;
SMB_VC_LOCK(vcp);
/* var, head, next_field */
SLIST_FOREACH(co, &(VCTOCP(vcp)->co_children), co_next) {
ssp = CPTOSS(co);
/* Share name */
if (u8_strcmp(ssp->ss_name, tcon->tc_sh.sh_name, 0,
U8_STRCMP_CI_LOWER, U8_UNICODE_LATEST, &error))
continue;
/*
* We have a match, but still have to check
* the _GONE flag, and do that with a lock.
* No new references when _GONE is set.
*
* Also clear SMBSOPT_CREATE which the caller
* may check to find out if we did create.
*/
SMB_SS_LOCK(ssp);
if ((ssp->ss_flags & SMBS_GONE) == 0) {
tcon->tc_opt &= ~SMBSOPT_CREATE;
/*
* Return it held, unlocked.
* In-line smb_share_hold here.
*/
co->co_usecount++;
SMB_SS_UNLOCK(ssp);
*sspp = ssp;
error = 0;
goto out;
}
SMB_SS_UNLOCK(ssp);
/* keep looking. */
}
ssp = NULL;
/* Note: vcp (list of shares) is still locked. */
if (tcon->tc_opt & SMBSOPT_CREATE) {
/*
* Create a new share. It starts out with
* hold count = 1, so don't incr. here.
*/
error = smb_share_create(tcon, vcp, &ssp, scred);
if (error == 0)
*sspp = ssp;
} else
error = ENOENT;
out:
SMB_VC_UNLOCK(vcp);
return (error);
}
/*
* Helper functions that operate on shares
*/
/*
* Mark this share as invalid, so consumers will know
* their file handles have become invalid.
*
* Most share consumers store a copy of ss_vcgenid when
* opening a file handle and compare that with what's in
* the share before using a file handle. If the genid
* doesn't match, the file handle has become "stale"
* due to disconnect. Therefore, zap ss_vcgenid here.
*/
void
smb_share_invalidate(struct smb_share *ssp)
{
ASSERT(MUTEX_HELD(&ssp->ss_lock));
ssp->ss_flags &= ~SMBS_CONNECTED;
ssp->ss_tid = SMB_TID_UNKNOWN;
ssp->ss_vcgenid = 0;
}
/*
* Connect (or reconnect) a share object.
*
* Called by smb_usr_get_tree() for new connections,
* and called by smb_rq_enqueue() for reconnect.
*/
int
smb_share_tcon(smb_share_t *ssp, smb_cred_t *scred)
{
smb_vc_t *vcp = SSTOVC(ssp);
clock_t tmo;
int error;
SMB_SS_LOCK(ssp);
if (ssp->ss_flags & SMBS_CONNECTED) {
SMBIODEBUG("alread connected?");
error = 0;
goto out;
}
/*
* Wait for completion of any state changes
* that might be underway.
*/
while (ssp->ss_flags & SMBS_RECONNECTING) {
ssp->ss_conn_waiters++;
tmo = cv_wait_sig(&ssp->ss_conn_done, &ssp->ss_lock);
ssp->ss_conn_waiters--;
if (tmo == 0) {
/* Interrupt! */
error = EINTR;
goto out;
}
}
/* Did someone else do it for us? */
if (ssp->ss_flags & SMBS_CONNECTED) {
error = 0;
goto out;
}
/*
* OK, we'll do the work.
*/
ssp->ss_flags |= SMBS_RECONNECTING;
/*
* Drop the lock while doing the TCON.
* On success, sets ss_tid, ss_vcgenid,
* and ss_flags |= SMBS_CONNECTED;
*/
SMB_SS_UNLOCK(ssp);
if (vcp->vc_flags & SMBV_SMB2)
error = smb2_smb_treeconnect(ssp, scred);
else
error = smb_smb_treeconnect(ssp, scred);
SMB_SS_LOCK(ssp);
ssp->ss_flags &= ~SMBS_RECONNECTING;
/* They can all go ahead! */
if (ssp->ss_conn_waiters)
cv_broadcast(&ssp->ss_conn_done);
out:
SMB_SS_UNLOCK(ssp);
return (error);
}
/*
* File handle level functions
*/
void
smb_fh_hold(struct smb_fh *fhp)
{
smb_co_hold(FHTOCP(fhp));
}
void
smb_fh_rele(struct smb_fh *fhp)
{
smb_co_rele(FHTOCP(fhp));
}
void
smb_fh_close(struct smb_fh *fhp)
{
smb_co_kill(FHTOCP(fhp));
}
/*
* Normally called via smb_fh_rele()
* after co_usecount drops to zero.
* Also called via: smb_fh_kill()
*/
static void
smb_fh_gone(struct smb_connobj *cp)
{
struct smb_cred scred;
struct smb_fh *fhp = CPTOFH(cp);
smb_share_t *ssp = FHTOSS(fhp);
int err;
if ((fhp->fh_flags & SMBFH_VALID) == 0)
return;
/*
* We have no durable handles (yet) so if there has been a
* reconnect, don't bother to close this handle.
*/
if (fhp->fh_vcgenid != ssp->ss_vcgenid)
return;
smb_credinit(&scred, NULL);
err = smb_smb_close(ssp, fhp, &scred);
smb_credrele(&scred);
if (err) {
SMBSDEBUG("close err=%d\n", err);
}
}
/*
* Normally called via smb_fh_rele()
* after co_usecount drops to zero.
*/
static void
smb_fh_free(struct smb_connobj *cp)
{
struct smb_fh *fhp = CPTOFH(cp);
smb_co_done(FHTOCP(fhp));
kmem_free(fhp, sizeof (*fhp));
}
/*
* Allocate fh structure and attach it to the given share.
* Share expected to be locked on entry.
*/
/*ARGSUSED*/
int
smb_fh_create(smb_share_t *ssp, struct smb_fh **fhpp)
{
static char objtype[] = "smb_fh";
struct smb_fh *fhp;
fhp = kmem_zalloc(sizeof (struct smb_fh), KM_SLEEP);
smb_co_init(FHTOCP(fhp), SMBL_FH, objtype);
fhp->fh_co.co_free = smb_fh_free;
fhp->fh_co.co_gone = smb_fh_gone;
SMB_SS_LOCK(ssp);
if ((ssp->ss_flags & SMBS_GONE) != 0) {
SMB_SS_UNLOCK(ssp);
smb_fh_free(FHTOCP(fhp));
return (ENOTCONN);
}
smb_co_addchild(SSTOCP(ssp), FHTOCP(fhp));
*fhpp = fhp;
SMB_SS_UNLOCK(ssp);
return (0);
}
void
smb_fh_opened(struct smb_fh *fhp)
{
smb_share_t *ssp = FHTOSS(fhp);
SMB_FH_LOCK(fhp);
fhp->fh_vcgenid = ssp->ss_vcgenid;
fhp->fh_flags |= SMBFH_VALID;
SMB_FH_UNLOCK(fhp);
}
/*
* Solaris zones support
*/
/*ARGSUSED*/
void
lingering_vc(struct smb_vc *vc)
{
/* good place for a breakpoint */
DEBUG_ENTER("lingering VC");
}
/*
* On zone shutdown, kill any IOD threads still running in this zone.
*/
/* ARGSUSED */
void
nsmb_zone_shutdown(zoneid_t zoneid, void *data)
{
struct smb_connobj *co;
struct smb_vc *vcp;
SMB_CO_LOCK(&smb_vclist);
SLIST_FOREACH(co, &smb_vclist.co_children, co_next) {
vcp = CPTOVC(co);
if (vcp->vc_zoneid != zoneid)
continue;
/*
* This will close the connection, and
* cause the IOD thread to terminate.
*/
smb_vc_kill(vcp);
}
SMB_CO_UNLOCK(&smb_vclist);
}
/*
* On zone destroy, kill any IOD threads and free all resources they used.
*/
/* ARGSUSED */
void
nsmb_zone_destroy(zoneid_t zoneid, void *data)
{
struct smb_connobj *co;
struct smb_vc *vcp;
/*
* We will repeat what should have already happened
* in zone_shutdown to make things go away.
*
* There should have been an smb_vc_rele call
* by now for all VCs in the zone. If not,
* there's probably more we needed to do in
* the shutdown call.
*/
SMB_CO_LOCK(&smb_vclist);
if (smb_vclist.co_usecount > 1) {
SMBERROR("%d connections still active\n",
smb_vclist.co_usecount - 1);
}
/* var, head, next_field */
SLIST_FOREACH(co, &smb_vclist.co_children, co_next) {
vcp = CPTOVC(co);
if (vcp->vc_zoneid != zoneid)
continue;
/* Debugging */
lingering_vc(vcp);
}
SMB_CO_UNLOCK(&smb_vclist);
}
|