1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright (c) 2017 by Delphix. All rights reserved. 24 * Copyright 2019 Nexenta by DDN, Inc. All rights reserved. 25 * Copyright 2021-2023 RackTop Systems, Inc. 26 */ 27 28 /* 29 * General Structures Layout 30 * ------------------------- 31 * 32 * This is a simplified diagram showing the relationship between most of the 33 * main structures. 34 * 35 * +-------------------+ 36 * | SMB_SERVER | 37 * +-------------------+ 38 * | 39 * | 40 * v 41 * +-------------------+ +-------------------+ +-------------------+ 42 * | SESSION |<----->| SESSION |......| SESSION | 43 * +-------------------+ +-------------------+ +-------------------+ 44 * | 45 * | 46 * v 47 * +-------------------+ +-------------------+ +-------------------+ 48 * | USER |<----->| USER |......| USER | 49 * +-------------------+ +-------------------+ +-------------------+ 50 * | 51 * | 52 * v 53 * +-------------------+ +-------------------+ +-------------------+ 54 * | TREE |<----->| TREE |......| TREE | 55 * +-------------------+ +-------------------+ +-------------------+ 56 * | | 57 * | | 58 * | v 59 * | +-------+ +-------+ +-------+ 60 * | | OFILE |<----->| OFILE |......| OFILE | 61 * | +-------+ +-------+ +-------+ 62 * | 63 * | 64 * v 65 * +-------+ +------+ +------+ 66 * | ODIR |<----->| ODIR |......| ODIR | 67 * +-------+ +------+ +------+ 68 * 69 * 70 * Module Interface Overview 71 * ------------------------- 72 * 73 * 74 * +===================================+ 75 * | smbd daemon | 76 * +===================================+ 77 * | | ^ 78 * | | | 79 * User | | | 80 * -----------|--------------|----------------|-------------------------------- 81 * Kernel | | | 82 * | | | 83 * | | | 84 * +=========|==============|================|=================+ 85 * | v v | | 86 * | +-----------+ +--------------------+ +------------------+ | 87 * | | IO | | Kernel Door Server | | User Door Servers| | 88 * | | Interface | | Interface | | Interface | | 89 * | +-----------+ +--------------------+ +------------------+ | 90 * | | | ^ ^ | 91 * | v v | | | +=========+ 92 * | +-----------------------------------+ | | | | 93 * | + SMB Server Management (this file) |<------------------| ZFS | 94 * | +-----------------------------------+ | | | | 95 * | | | | Module | 96 * | +-----------------------------------+ | | | | 97 * | + SMB Server Internal Layers |------+ | +=========+ 98 * | +-----------------------------------+ | 99 * | | 100 * | | 101 * +===========================================================+ 102 * 103 * 104 * Server State Machine 105 * -------------------- 106 * | 107 * | T0 108 * | 109 * v 110 * +-----------------------------+ 111 * | SMB_SERVER_STATE_CREATED | 112 * +-----------------------------+ 113 * | 114 * | T1 115 * | 116 * v 117 * +-----------------------------+ 118 * | SMB_SERVER_STATE_CONFIGURED | 119 * +-----------------------------+ 120 * | 121 * | T2 122 * | 123 * v 124 * +-----------------------------+ 125 * | SMB_SERVER_STATE_RUNNING / | 126 * | SMB_SERVER_STATE_STOPPING | 127 * +-----------------------------+ 128 * | 129 * | T3 130 * | 131 * v 132 * +-----------------------------+ 133 * | SMB_SERVER_STATE_DELETING | 134 * +-----------------------------+ 135 * | 136 * | 137 * | 138 * v 139 * 140 * States 141 * ------ 142 * 143 * SMB_SERVER_STATE_CREATED 144 * 145 * This is the state of the server just after creation. 146 * 147 * SMB_SERVER_STATE_CONFIGURED 148 * 149 * The server has been configured. 150 * 151 * SMB_SERVER_STATE_RUNNING 152 * 153 * The server has been started. While in this state the threads listening on 154 * the sockets are started. 155 * 156 * When a client establishes a connection the thread listening dispatches 157 * a task with the new session as an argument. If the dispatch fails the new 158 * session context is destroyed. 159 * 160 * SMB_SERVER_STATE_STOPPING 161 * 162 * The threads listening on the NBT and TCP sockets are being terminated. 163 * 164 * 165 * Transitions 166 * ----------- 167 * 168 * Transition T0 169 * 170 * The daemon smbd triggers its creation by opening the smbsrv device. If 171 * the zone where the daemon lives doesn't have an smb server yet it is 172 * created. 173 * 174 * smb_drv_open() --> smb_server_create() 175 * 176 * Transition T1 177 * 178 * This transition occurs in smb_server_configure(). It is triggered by the 179 * daemon through an Ioctl. 180 * 181 * smb_drv_ioctl(SMB_IOC_CONFIG) --> smb_server_configure() 182 * 183 * Transition T2 184 * 185 * This transition occurs in smb_server_start(). It is triggered by the 186 * daemon through an Ioctl. 187 * 188 * smb_drv_ioctl(SMB_IOC_START) --> smb_server_start() 189 * 190 * Transition T3 191 * 192 * This transition occurs in smb_server_delete(). It is triggered by the 193 * daemon when closing the smbsrv device 194 * 195 * smb_drv_close() --> smb_server_delete() 196 * 197 * Comments 198 * -------- 199 * 200 * This files assumes that there will one SMB server per zone. For now the 201 * smb server works only in global zone. There's nothing in this file preventing 202 * an smb server from being created in a non global zone. That limitation is 203 * enforced in user space. 204 */ 205 206 #include <sys/cmn_err.h> 207 #include <sys/priv.h> 208 #include <sys/zone.h> 209 #include <sys/sysmacros.h> 210 #include <sys/callb.h> 211 #include <sys/class.h> 212 #include <sys/disp.h> 213 #include <netinet/in.h> 214 #include <netinet/in_systm.h> 215 #include <netinet/ip.h> 216 #include <netinet/ip_icmp.h> 217 #include <netinet/ip_var.h> 218 #include <netinet/tcp.h> 219 #include <smbsrv/smb2_kproto.h> 220 #include <smbsrv/string.h> 221 #include <smbsrv/netbios.h> 222 #include <smbsrv/smb_fsops.h> 223 #include <smbsrv/smb_share.h> 224 #include <smbsrv/smb_door.h> 225 #include <smbsrv/smb_kstat.h> 226 227 static void smb_server_kstat_init(smb_server_t *); 228 static void smb_server_kstat_fini(smb_server_t *); 229 static void smb_server_timers(smb_thread_t *, void *); 230 static void smb_server_store_cfg(smb_server_t *, smb_ioc_cfg_t *); 231 static void smb_server_shutdown(smb_server_t *); 232 static int smb_server_fsop_start(smb_server_t *); 233 static void smb_server_fsop_stop(smb_server_t *); 234 static void smb_event_cancel(smb_server_t *, uint32_t); 235 static uint32_t smb_event_alloc_txid(void); 236 237 static void smb_server_disconnect_share(smb_server_t *, const char *); 238 static void smb_server_enum_users(smb_server_t *, smb_svcenum_t *); 239 static void smb_server_enum_trees(smb_server_t *, smb_svcenum_t *); 240 static int smb_server_session_disconnect(smb_server_t *, const char *, 241 const char *); 242 static int smb_server_fclose(smb_server_t *, uint32_t); 243 static int smb_server_kstat_update(kstat_t *, int); 244 static int smb_server_legacy_kstat_update(kstat_t *, int); 245 static void smb_server_listener_init(smb_server_t *, smb_listener_daemon_t *, 246 char *, in_port_t, int); 247 static void smb_server_listener_destroy(smb_listener_daemon_t *); 248 static int smb_server_listener_start(smb_listener_daemon_t *); 249 static void smb_server_listener_stop(smb_listener_daemon_t *); 250 static void smb_server_listener(smb_thread_t *, void *); 251 static void smb_server_receiver(void *); 252 static void smb_server_create_session(smb_listener_daemon_t *, ksocket_t); 253 static void smb_server_destroy_session(smb_session_t *); 254 static uint16_t smb_spool_get_fid(smb_server_t *); 255 static boolean_t smb_spool_lookup_doc_byfid(smb_server_t *, uint16_t, 256 smb_kspooldoc_t *); 257 258 #ifdef _KERNEL 259 int smb_create_process = 1; 260 static void smb_server_delproc(smb_server_t *); 261 static int smb_server_newproc(smb_server_t *); 262 static void smb_server_proc_main(void *); 263 #endif 264 265 /* 266 * How many "buckets" should our hash tables use? On a "real" server, 267 * make them much larger than the number of CPUs we're likely to have. 268 * On "fksmbd" make it smaller so dtrace logs are shorter. 269 * These must be powers of two. 270 */ 271 #ifdef _KERNEL 272 #define DEFAULT_HASH_NBUCKETS 256 /* real server */ 273 #else 274 #define DEFAULT_HASH_NBUCKETS 16 /* for "fksmbd" */ 275 #endif 276 uint32_t SMB_OFILE_HASH_NBUCKETS = DEFAULT_HASH_NBUCKETS; 277 uint32_t SMB_LEASE_HASH_NBUCKETS = DEFAULT_HASH_NBUCKETS; 278 279 int smb_event_debug = 0; 280 281 static smb_llist_t smb_servers; 282 283 /* for smb_server_destroy_session() */ 284 static smb_llist_t smb_server_session_zombies; 285 286 kmem_cache_t *smb_cache_request; 287 kmem_cache_t *smb_cache_session; 288 kmem_cache_t *smb_cache_user; 289 kmem_cache_t *smb_cache_tree; 290 kmem_cache_t *smb_cache_ofile; 291 kmem_cache_t *smb_cache_odir; 292 kmem_cache_t *smb_cache_opipe; 293 kmem_cache_t *smb_cache_event; 294 kmem_cache_t *smb_cache_lock; 295 296 /* 297 * ***************************************************************************** 298 * **************** Functions called from the device interface ***************** 299 * ***************************************************************************** 300 * 301 * These functions typically have to determine the relevant smb server 302 * to which the call applies. 303 */ 304 305 /* 306 * How many zones have an SMB server active? 307 */ 308 int 309 smb_server_get_count(void) 310 { 311 return (smb_llist_get_count(&smb_servers)); 312 } 313 314 /* 315 * smb_server_g_init 316 * 317 * This function must be called from smb_drv_attach(). 318 */ 319 int 320 smb_server_g_init(void) 321 { 322 int rc; 323 324 if ((rc = smb_vop_init()) != 0) 325 goto errout; 326 if ((rc = smb_fem_init()) != 0) 327 goto errout; 328 329 smb_kshare_g_init(); 330 smb_codepage_init(); 331 smb_mbc_init(); /* smb_mbc_cache */ 332 smb_node_init(); /* smb_node_cache, lists */ 333 smb2_lease_init(); 334 335 smb_cache_request = kmem_cache_create("smb_request_cache", 336 sizeof (smb_request_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 337 smb_cache_session = kmem_cache_create("smb_session_cache", 338 sizeof (smb_session_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 339 smb_cache_user = kmem_cache_create("smb_user_cache", 340 sizeof (smb_user_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 341 smb_cache_tree = kmem_cache_create("smb_tree_cache", 342 sizeof (smb_tree_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 343 smb_cache_ofile = kmem_cache_create("smb_ofile_cache", 344 sizeof (smb_ofile_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 345 smb_cache_odir = kmem_cache_create("smb_odir_cache", 346 sizeof (smb_odir_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 347 smb_cache_opipe = kmem_cache_create("smb_opipe_cache", 348 sizeof (smb_opipe_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 349 smb_cache_event = kmem_cache_create("smb_event_cache", 350 sizeof (smb_event_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 351 smb_cache_lock = kmem_cache_create("smb_lock_cache", 352 sizeof (smb_lock_t), 8, NULL, NULL, NULL, NULL, NULL, 0); 353 354 smb_llist_init(); 355 smb_llist_constructor(&smb_servers, sizeof (smb_server_t), 356 offsetof(smb_server_t, sv_lnd)); 357 358 smb_llist_constructor(&smb_server_session_zombies, 359 sizeof (smb_session_t), offsetof(smb_session_t, s_lnd)); 360 361 return (0); 362 363 errout: 364 smb_fem_fini(); 365 smb_vop_fini(); 366 return (rc); 367 } 368 369 /* 370 * smb_server_g_fini 371 * 372 * This function must called from smb_drv_detach(). It will fail if servers 373 * still exist. 374 */ 375 void 376 smb_server_g_fini(void) 377 { 378 379 ASSERT(smb_llist_get_count(&smb_servers) == 0); 380 381 smb_llist_fini(); 382 383 kmem_cache_destroy(smb_cache_request); 384 kmem_cache_destroy(smb_cache_session); 385 kmem_cache_destroy(smb_cache_user); 386 kmem_cache_destroy(smb_cache_tree); 387 kmem_cache_destroy(smb_cache_ofile); 388 kmem_cache_destroy(smb_cache_odir); 389 kmem_cache_destroy(smb_cache_opipe); 390 kmem_cache_destroy(smb_cache_event); 391 kmem_cache_destroy(smb_cache_lock); 392 393 smb2_lease_fini(); 394 smb_node_fini(); 395 smb_mbc_fini(); 396 smb_codepage_fini(); 397 smb_kshare_g_fini(); 398 399 smb_fem_fini(); 400 smb_vop_fini(); 401 402 smb_llist_destructor(&smb_servers); 403 } 404 405 /* 406 * smb_server_create 407 * 408 * Called by driver open 409 * 410 * This function will fail if there's already a server associated with the 411 * caller's zone. 412 * 413 * This object is one-to-one with zones, so we could instead 414 * create/destroy this via zone_key_create callbacks. 415 * See smb_server_delete() for destruction. 416 */ 417 int 418 smb_server_create(void) 419 { 420 zoneid_t zid; 421 smb_server_t *sv; 422 423 zid = getzoneid(); 424 425 smb_llist_enter(&smb_servers, RW_WRITER); 426 sv = smb_llist_head(&smb_servers); 427 while (sv) { 428 SMB_SERVER_VALID(sv); 429 if (sv->sv_zid == zid) { 430 smb_llist_exit(&smb_servers); 431 return (EPERM); 432 } 433 sv = smb_llist_next(&smb_servers, sv); 434 } 435 436 sv = kmem_zalloc(sizeof (smb_server_t), KM_SLEEP); 437 438 sv->sv_magic = SMB_SERVER_MAGIC; 439 sv->sv_state = SMB_SERVER_STATE_CREATED; 440 sv->sv_zid = zid; 441 sv->sv_pid = ddi_get_pid(); 442 sv->sv_proc_state = SMB_THREAD_STATE_EXITED; 443 444 mutex_init(&sv->sv_mutex, NULL, MUTEX_DEFAULT, NULL); 445 cv_init(&sv->sv_cv, NULL, CV_DEFAULT, NULL); 446 cv_init(&sv->sp_info.sp_cv, NULL, CV_DEFAULT, NULL); 447 448 sv->sv_persistid_ht = smb_hash_create(sizeof (smb_ofile_t), 449 offsetof(smb_ofile_t, f_dh_lnd), SMB_OFILE_HASH_NBUCKETS); 450 451 sv->sv_lease_ht = smb_hash_create(sizeof (smb_lease_t), 452 offsetof(smb_lease_t, ls_lnd), SMB_LEASE_HASH_NBUCKETS); 453 454 smb_llist_constructor(&sv->sv_session_list, sizeof (smb_session_t), 455 offsetof(smb_session_t, s_lnd)); 456 457 smb_llist_constructor(&sv->sv_event_list, sizeof (smb_event_t), 458 offsetof(smb_event_t, se_lnd)); 459 460 smb_llist_constructor(&sv->sp_info.sp_list, sizeof (smb_kspooldoc_t), 461 offsetof(smb_kspooldoc_t, sd_lnd)); 462 463 smb_llist_constructor(&sv->sp_info.sp_fidlist, 464 sizeof (smb_spoolfid_t), offsetof(smb_spoolfid_t, sf_lnd)); 465 466 sv->sv_disp_stats1 = kmem_zalloc(SMB_COM_NUM * 467 sizeof (smb_disp_stats_t), KM_SLEEP); 468 469 sv->sv_disp_stats2 = kmem_zalloc(SMB2__NCMDS * 470 sizeof (smb_disp_stats_t), KM_SLEEP); 471 472 smb_thread_init(&sv->si_thread_timers, "smb_timers", 473 smb_server_timers, sv, smbsrv_timer_pri, sv); 474 475 smb_srqueue_init(&sv->sv_srqueue); 476 477 smb_kdoor_init(sv); 478 smb_kshare_init(sv); 479 smb_server_kstat_init(sv); 480 481 smb_threshold_init(&sv->sv_ssetup_ct, SMB_SSETUP_CMD, 482 smb_ssetup_threshold, smb_ssetup_timeout); 483 smb_threshold_init(&sv->sv_tcon_ct, SMB_TCON_CMD, 484 smb_tcon_threshold, smb_tcon_timeout); 485 smb_threshold_init(&sv->sv_opipe_ct, SMB_OPIPE_CMD, 486 smb_opipe_threshold, smb_opipe_timeout); 487 488 smb_llist_insert_tail(&smb_servers, sv); 489 smb_llist_exit(&smb_servers); 490 491 return (0); 492 } 493 494 /* 495 * smb_server_delete 496 * 497 * Called by driver close 498 * 499 * This function will delete the server passed in. It will make sure that all 500 * activity associated that server has ceased before destroying it. 501 */ 502 int 503 smb_server_delete(smb_server_t *sv) 504 { 505 506 mutex_enter(&sv->sv_mutex); 507 switch (sv->sv_state) { 508 case SMB_SERVER_STATE_RUNNING: 509 sv->sv_state = SMB_SERVER_STATE_STOPPING; 510 mutex_exit(&sv->sv_mutex); 511 smb_server_shutdown(sv); 512 mutex_enter(&sv->sv_mutex); 513 cv_broadcast(&sv->sp_info.sp_cv); 514 sv->sv_state = SMB_SERVER_STATE_DELETING; 515 break; 516 case SMB_SERVER_STATE_STOPPING: 517 sv->sv_state = SMB_SERVER_STATE_DELETING; 518 break; 519 case SMB_SERVER_STATE_CONFIGURED: 520 case SMB_SERVER_STATE_CREATED: 521 sv->sv_state = SMB_SERVER_STATE_DELETING; 522 break; 523 default: 524 SMB_SERVER_STATE_VALID(sv->sv_state); 525 mutex_exit(&sv->sv_mutex); 526 smb_server_release(sv); 527 return (ENOTTY); 528 } 529 530 ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING); 531 532 sv->sv_refcnt--; 533 while (sv->sv_refcnt) 534 cv_wait(&sv->sv_cv, &sv->sv_mutex); 535 536 mutex_exit(&sv->sv_mutex); 537 538 smb_llist_enter(&smb_servers, RW_WRITER); 539 smb_llist_remove(&smb_servers, sv); 540 smb_llist_exit(&smb_servers); 541 542 smb_threshold_fini(&sv->sv_ssetup_ct); 543 smb_threshold_fini(&sv->sv_tcon_ct); 544 smb_threshold_fini(&sv->sv_opipe_ct); 545 546 smb_server_listener_destroy(&sv->sv_nbt_daemon); 547 smb_server_listener_destroy(&sv->sv_tcp_daemon); 548 rw_destroy(&sv->sv_cfg_lock); 549 smb_server_kstat_fini(sv); 550 smb_kshare_fini(sv); 551 smb_kdoor_fini(sv); 552 smb_llist_destructor(&sv->sv_event_list); 553 smb_llist_destructor(&sv->sv_session_list); 554 555 kmem_free(sv->sv_disp_stats1, 556 SMB_COM_NUM * sizeof (smb_disp_stats_t)); 557 558 kmem_free(sv->sv_disp_stats2, 559 SMB2__NCMDS * sizeof (smb_disp_stats_t)); 560 561 smb_srqueue_destroy(&sv->sv_srqueue); 562 smb_thread_destroy(&sv->si_thread_timers); 563 564 mutex_destroy(&sv->sv_mutex); 565 smb_hash_destroy(sv->sv_lease_ht); 566 smb_hash_destroy(sv->sv_persistid_ht); 567 cv_destroy(&sv->sv_cv); 568 sv->sv_magic = 0; 569 kmem_free(sv, sizeof (smb_server_t)); 570 571 return (0); 572 } 573 574 /* 575 * smb_server_configure 576 * 577 * Called via SMB_IOC_CONFIG, for smbd startup or refresh. 578 */ 579 int 580 smb_server_configure(smb_ioc_cfg_t *ioc) 581 { 582 int rc = 0; 583 smb_server_t *sv; 584 585 /* 586 * Reality check negotiation token length vs. #define'd maximum. 587 */ 588 if (ioc->negtok_len > SMB_PI_MAX_NEGTOK) 589 return (EINVAL); 590 591 rc = smb_server_lookup(&sv); 592 if (rc) 593 return (rc); 594 595 mutex_enter(&sv->sv_mutex); 596 switch (sv->sv_state) { 597 case SMB_SERVER_STATE_CREATED: 598 smb_server_store_cfg(sv, ioc); 599 sv->sv_state = SMB_SERVER_STATE_CONFIGURED; 600 break; 601 602 case SMB_SERVER_STATE_CONFIGURED: 603 smb_server_store_cfg(sv, ioc); 604 break; 605 606 case SMB_SERVER_STATE_RUNNING: 607 case SMB_SERVER_STATE_STOPPING: 608 rw_enter(&sv->sv_cfg_lock, RW_WRITER); 609 smb_server_store_cfg(sv, ioc); 610 rw_exit(&sv->sv_cfg_lock); 611 break; 612 613 default: 614 SMB_SERVER_STATE_VALID(sv->sv_state); 615 rc = EFAULT; 616 break; 617 } 618 mutex_exit(&sv->sv_mutex); 619 620 smb_server_release(sv); 621 622 return (rc); 623 } 624 625 /* 626 * smb_server_start 627 * 628 * Called via SMB_IOC_START during smbd startup. 629 * Bring up the activities requried for SMB service. 630 */ 631 int 632 smb_server_start(smb_ioc_start_t *ioc) 633 { 634 int rc = 0; 635 int family; 636 smb_server_t *sv; 637 cred_t *ucr; 638 struct proc *tqproc; 639 640 rc = smb_server_lookup(&sv); 641 if (rc) 642 return (rc); 643 644 mutex_enter(&sv->sv_mutex); 645 switch (sv->sv_state) { 646 case SMB_SERVER_STATE_CONFIGURED: 647 648 #ifdef _KERNEL 649 if (smb_create_process) { 650 rc = smb_server_newproc(sv); 651 if (rc != 0) 652 break; 653 } 654 #endif /* _KERNEL */ 655 656 if ((rc = smb_server_fsop_start(sv)) != 0) 657 break; 658 659 /* 660 * Note: smb_kshare_start needs sv_session. 661 */ 662 sv->sv_session = smb_session_create(NULL, 0, sv, 0); 663 if (sv->sv_session == NULL) { 664 rc = ENOMEM; 665 break; 666 } 667 668 /* 669 * Create a logon on the server session, 670 * used when importing CA shares. 671 */ 672 sv->sv_rootuser = smb_user_new(sv->sv_session); 673 ucr = smb_kcred_create(); 674 rc = smb_user_logon(sv->sv_rootuser, ucr, "", "root", 675 SMB_USER_FLAG_ADMIN, 0, 0); 676 crfree(ucr); 677 ucr = NULL; 678 if (rc != 0) { 679 cmn_err(CE_NOTE, "smb_server_start: " 680 "failed to create root user"); 681 break; 682 } 683 684 if ((rc = smb_kshare_start(sv)) != 0) 685 break; 686 687 /* 688 * Create our taskq's (thread pools) 689 * 690 * NB: the proc passed here has to be a "system" one. 691 * Normally that's p0, or the NGZ eqivalent. 692 * 693 * The notify pool is sized at a quarter the number of 694 * worker threads (instead of another config item). 695 */ 696 tqproc = (sv->sv_proc_p != NULL) ? 697 sv->sv_proc_p : curzone->zone_zsched; 698 699 sv->sv_notify_pool = taskq_create_proc("smb_notify", 700 sv->sv_cfg.skc_maxworkers / 4, smbsrv_notify_pri, 701 sv->sv_cfg.skc_maxworkers / 4, INT_MAX, 702 tqproc, TASKQ_DYNAMIC|TASKQ_THREADS_LWP); 703 704 sv->sv_worker_pool = taskq_create_proc("smb_workers", 705 sv->sv_cfg.skc_maxworkers, smbsrv_worker_pri, 706 sv->sv_cfg.skc_maxworkers, INT_MAX, 707 tqproc, TASKQ_DYNAMIC|TASKQ_THREADS_LWP); 708 709 sv->sv_receiver_pool = taskq_create_proc("smb_receivers", 710 sv->sv_cfg.skc_maxconnections, smbsrv_receive_pri, 711 sv->sv_cfg.skc_maxconnections, INT_MAX, 712 tqproc, TASKQ_DYNAMIC|TASKQ_THREADS_LWP); 713 714 if (sv->sv_notify_pool == NULL || 715 sv->sv_worker_pool == NULL || 716 sv->sv_receiver_pool == NULL) { 717 rc = ENOMEM; 718 break; 719 } 720 721 #ifdef _KERNEL 722 ASSERT(sv->sv_lmshrd == NULL); 723 sv->sv_lmshrd = smb_kshare_door_init(ioc->lmshrd); 724 if (sv->sv_lmshrd == NULL) 725 break; 726 if ((rc = smb_kdoor_open(sv, ioc->udoor)) != 0) { 727 cmn_err(CE_WARN, "Cannot open smbd door"); 728 break; 729 } 730 #else /* _KERNEL */ 731 /* Fake kernel does not use the kshare_door */ 732 fksmb_kdoor_open(sv, ioc->udoor_func); 733 #endif /* _KERNEL */ 734 735 if ((rc = smb_thread_start(&sv->si_thread_timers)) != 0) 736 break; 737 738 family = AF_INET; 739 smb_server_listener_init(sv, &sv->sv_nbt_daemon, 740 "smb_nbt_listener", IPPORT_NETBIOS_SSN, family); 741 if (sv->sv_cfg.skc_ipv6_enable) 742 family = AF_INET6; 743 smb_server_listener_init(sv, &sv->sv_tcp_daemon, 744 "smb_tcp_listener", IPPORT_SMB, family); 745 rc = smb_server_listener_start(&sv->sv_tcp_daemon); 746 if (rc != 0) 747 break; 748 if (sv->sv_cfg.skc_netbios_enable) 749 (void) smb_server_listener_start(&sv->sv_nbt_daemon); 750 751 sv->sv_state = SMB_SERVER_STATE_RUNNING; 752 sv->sv_start_time = gethrtime(); 753 mutex_exit(&sv->sv_mutex); 754 smb_server_release(sv); 755 smb_export_start(sv); 756 return (0); 757 default: 758 SMB_SERVER_STATE_VALID(sv->sv_state); 759 mutex_exit(&sv->sv_mutex); 760 smb_server_release(sv); 761 return (ENOTTY); 762 } 763 764 mutex_exit(&sv->sv_mutex); 765 smb_server_shutdown(sv); 766 smb_server_release(sv); 767 return (rc); 768 } 769 770 /* 771 * An smbd is shutting down. 772 */ 773 int 774 smb_server_stop(void) 775 { 776 smb_server_t *sv; 777 int rc; 778 779 if ((rc = smb_server_lookup(&sv)) != 0) 780 return (rc); 781 782 mutex_enter(&sv->sv_mutex); 783 switch (sv->sv_state) { 784 case SMB_SERVER_STATE_RUNNING: 785 sv->sv_state = SMB_SERVER_STATE_STOPPING; 786 mutex_exit(&sv->sv_mutex); 787 smb_server_shutdown(sv); 788 mutex_enter(&sv->sv_mutex); 789 cv_broadcast(&sv->sp_info.sp_cv); 790 break; 791 default: 792 SMB_SERVER_STATE_VALID(sv->sv_state); 793 break; 794 } 795 mutex_exit(&sv->sv_mutex); 796 797 smb_server_release(sv); 798 return (0); 799 } 800 801 boolean_t 802 smb_server_is_stopping(smb_server_t *sv) 803 { 804 boolean_t status; 805 806 SMB_SERVER_VALID(sv); 807 808 mutex_enter(&sv->sv_mutex); 809 810 switch (sv->sv_state) { 811 case SMB_SERVER_STATE_STOPPING: 812 case SMB_SERVER_STATE_DELETING: 813 status = B_TRUE; 814 break; 815 default: 816 status = B_FALSE; 817 break; 818 } 819 820 mutex_exit(&sv->sv_mutex); 821 return (status); 822 } 823 824 void 825 smb_server_cancel_event(smb_server_t *sv, uint32_t txid) 826 { 827 smb_event_cancel(sv, txid); 828 } 829 830 int 831 smb_server_notify_event(smb_ioc_event_t *ioc) 832 { 833 smb_server_t *sv; 834 int rc; 835 836 if ((rc = smb_server_lookup(&sv)) == 0) { 837 smb_event_notify(sv, ioc->txid); 838 smb_server_release(sv); 839 } 840 841 return (rc); 842 } 843 844 /* 845 * smb_server_spooldoc 846 * 847 * Waits for print file close broadcast. 848 * Gets the head of the fid list, 849 * then searches the spooldoc list and returns 850 * this info via the ioctl to user land. 851 * 852 * rc - 0 success 853 */ 854 855 int 856 smb_server_spooldoc(smb_ioc_spooldoc_t *ioc) 857 { 858 smb_server_t *sv; 859 int rc; 860 smb_kspooldoc_t *spdoc; 861 uint16_t fid; 862 863 if ((rc = smb_server_lookup(&sv)) != 0) 864 return (rc); 865 866 if (sv->sv_cfg.skc_print_enable == 0) { 867 rc = ENOTTY; 868 goto out; 869 } 870 871 mutex_enter(&sv->sv_mutex); 872 for (;;) { 873 if (sv->sv_state != SMB_SERVER_STATE_RUNNING) { 874 rc = ECANCELED; 875 break; 876 } 877 if ((fid = smb_spool_get_fid(sv)) != 0) { 878 rc = 0; 879 break; 880 } 881 if (cv_wait_sig(&sv->sp_info.sp_cv, &sv->sv_mutex) == 0) { 882 rc = EINTR; 883 break; 884 } 885 } 886 mutex_exit(&sv->sv_mutex); 887 if (rc != 0) 888 goto out; 889 890 spdoc = kmem_zalloc(sizeof (*spdoc), KM_SLEEP); 891 if (smb_spool_lookup_doc_byfid(sv, fid, spdoc)) { 892 ioc->spool_num = spdoc->sd_spool_num; 893 ioc->ipaddr = spdoc->sd_ipaddr; 894 (void) strlcpy(ioc->path, spdoc->sd_path, 895 MAXPATHLEN); 896 (void) strlcpy(ioc->username, 897 spdoc->sd_username, MAXNAMELEN); 898 } else { 899 /* Did not find that print job. */ 900 rc = EAGAIN; 901 } 902 kmem_free(spdoc, sizeof (*spdoc)); 903 904 out: 905 smb_server_release(sv); 906 return (rc); 907 } 908 909 int 910 smb_server_set_gmtoff(smb_ioc_gmt_t *ioc) 911 { 912 int rc; 913 smb_server_t *sv; 914 915 if ((rc = smb_server_lookup(&sv)) == 0) { 916 sv->si_gmtoff = ioc->offset; 917 smb_server_release(sv); 918 } 919 920 return (rc); 921 } 922 923 int 924 smb_server_numopen(smb_ioc_opennum_t *ioc) 925 { 926 smb_server_t *sv; 927 int rc; 928 929 if ((rc = smb_server_lookup(&sv)) == 0) { 930 ioc->open_users = sv->sv_users; 931 ioc->open_trees = sv->sv_trees; 932 ioc->open_files = sv->sv_files + sv->sv_pipes; 933 smb_server_release(sv); 934 } 935 return (rc); 936 } 937 938 /* 939 * Enumerate objects within the server. The svcenum provides the 940 * enumeration context, i.e. what the caller want to get back. 941 */ 942 int 943 smb_server_enum(smb_ioc_svcenum_t *ioc) 944 { 945 smb_svcenum_t *svcenum = &ioc->svcenum; 946 smb_server_t *sv; 947 int rc; 948 949 /* 950 * Reality check that the buffer-length insize the enum doesn't 951 * overrun the ioctl's total length. 952 */ 953 if (svcenum->se_buflen + sizeof (*ioc) > ioc->hdr.len) 954 return (EINVAL); 955 956 if ((rc = smb_server_lookup(&sv)) != 0) 957 return (rc); 958 959 svcenum->se_bavail = svcenum->se_buflen; 960 svcenum->se_bused = 0; 961 svcenum->se_nitems = 0; 962 963 switch (svcenum->se_type) { 964 case SMB_SVCENUM_TYPE_USER: 965 smb_server_enum_users(sv, svcenum); 966 break; 967 case SMB_SVCENUM_TYPE_TREE: 968 case SMB_SVCENUM_TYPE_FILE: 969 smb_server_enum_trees(sv, svcenum); 970 break; 971 default: 972 rc = EINVAL; 973 } 974 975 smb_server_release(sv); 976 return (rc); 977 } 978 979 /* 980 * Look for sessions to disconnect by client and user name. 981 */ 982 int 983 smb_server_session_close(smb_ioc_session_t *ioc) 984 { 985 smb_server_t *sv; 986 int cnt; 987 int rc; 988 989 if ((rc = smb_server_lookup(&sv)) != 0) 990 return (rc); 991 992 cnt = smb_server_session_disconnect(sv, ioc->client, ioc->username); 993 994 smb_server_release(sv); 995 996 if (cnt == 0) 997 return (ENOENT); 998 return (0); 999 } 1000 1001 /* 1002 * Close a file by uniqid. 1003 */ 1004 int 1005 smb_server_file_close(smb_ioc_fileid_t *ioc) 1006 { 1007 uint32_t uniqid = ioc->uniqid; 1008 smb_server_t *sv; 1009 int rc; 1010 1011 if ((rc = smb_server_lookup(&sv)) != 0) 1012 return (rc); 1013 1014 rc = smb_server_fclose(sv, uniqid); 1015 1016 smb_server_release(sv); 1017 return (rc); 1018 } 1019 1020 /* 1021 * These functions determine the relevant smb server to which the call apply. 1022 */ 1023 1024 uint32_t 1025 smb_server_get_session_count(smb_server_t *sv) 1026 { 1027 uint32_t counter = 0; 1028 1029 counter = smb_llist_get_count(&sv->sv_session_list); 1030 1031 return (counter); 1032 } 1033 1034 /* 1035 * Gets the smb_node of the specified share path. 1036 * Node is returned held (caller must rele.) 1037 */ 1038 int 1039 smb_server_share_lookup(smb_server_t *sv, const char *shr_path, 1040 smb_node_t **nodepp) 1041 { 1042 smb_request_t *sr; 1043 smb_node_t *fnode = NULL; 1044 smb_node_t *dnode = NULL; 1045 char last_comp[MAXNAMELEN]; 1046 int rc = 0; 1047 1048 ASSERT(shr_path); 1049 1050 mutex_enter(&sv->sv_mutex); 1051 switch (sv->sv_state) { 1052 case SMB_SERVER_STATE_RUNNING: 1053 break; 1054 default: 1055 mutex_exit(&sv->sv_mutex); 1056 return (ENOTACTIVE); 1057 } 1058 mutex_exit(&sv->sv_mutex); 1059 1060 if ((sr = smb_request_alloc(sv->sv_session, 0)) == NULL) { 1061 return (ENOTCONN); 1062 } 1063 sr->user_cr = zone_kcred(); 1064 1065 rc = smb_pathname_reduce(sr, sr->user_cr, shr_path, 1066 NULL, NULL, &dnode, last_comp); 1067 1068 if (rc == 0) { 1069 rc = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS, 1070 sv->si_root_smb_node, dnode, last_comp, &fnode); 1071 smb_node_release(dnode); 1072 } 1073 1074 smb_request_free(sr); 1075 1076 if (rc != 0) 1077 return (rc); 1078 1079 ASSERT(fnode->vp && fnode->vp->v_vfsp); 1080 1081 *nodepp = fnode; 1082 1083 return (0); 1084 } 1085 1086 #ifdef _KERNEL 1087 /* 1088 * This is a special interface that will be utilized by ZFS to cause a share to 1089 * be added/removed. 1090 * 1091 * arg is either a lmshare_info_t or share_name from userspace. 1092 * It will need to be copied into the kernel. It is lmshare_info_t 1093 * for add operations and share_name for delete operations. 1094 */ 1095 int 1096 smb_server_share(void *arg, boolean_t add_share) 1097 { 1098 smb_server_t *sv; 1099 int rc; 1100 1101 if ((rc = smb_server_lookup(&sv)) == 0) { 1102 mutex_enter(&sv->sv_mutex); 1103 switch (sv->sv_state) { 1104 case SMB_SERVER_STATE_RUNNING: 1105 mutex_exit(&sv->sv_mutex); 1106 (void) smb_kshare_upcall(sv->sv_lmshrd, arg, add_share); 1107 break; 1108 default: 1109 mutex_exit(&sv->sv_mutex); 1110 break; 1111 } 1112 smb_server_release(sv); 1113 } 1114 1115 return (rc); 1116 } 1117 #endif /* _KERNEL */ 1118 1119 int 1120 smb_server_unshare(const char *sharename) 1121 { 1122 smb_server_t *sv; 1123 int rc; 1124 1125 if ((rc = smb_server_lookup(&sv))) 1126 return (rc); 1127 1128 mutex_enter(&sv->sv_mutex); 1129 switch (sv->sv_state) { 1130 case SMB_SERVER_STATE_RUNNING: 1131 case SMB_SERVER_STATE_STOPPING: 1132 break; 1133 default: 1134 mutex_exit(&sv->sv_mutex); 1135 smb_server_release(sv); 1136 return (ENOTACTIVE); 1137 } 1138 mutex_exit(&sv->sv_mutex); 1139 1140 smb_server_disconnect_share(sv, sharename); 1141 1142 smb_server_release(sv); 1143 return (0); 1144 } 1145 1146 /* 1147 * Disconnect the specified share. 1148 * Typically called when a share has been removed. 1149 */ 1150 static void 1151 smb_server_disconnect_share(smb_server_t *sv, const char *sharename) 1152 { 1153 smb_llist_t *ll; 1154 smb_session_t *session; 1155 1156 ll = &sv->sv_session_list; 1157 smb_llist_enter(ll, RW_READER); 1158 1159 session = smb_llist_head(ll); 1160 while (session) { 1161 SMB_SESSION_VALID(session); 1162 smb_rwx_rwenter(&session->s_lock, RW_READER); 1163 switch (session->s_state) { 1164 case SMB_SESSION_STATE_NEGOTIATED: 1165 smb_rwx_rwexit(&session->s_lock); 1166 smb_session_disconnect_share(session, sharename); 1167 break; 1168 default: 1169 smb_rwx_rwexit(&session->s_lock); 1170 break; 1171 } 1172 session = smb_llist_next(ll, session); 1173 } 1174 1175 smb_llist_exit(ll); 1176 } 1177 1178 #ifdef _KERNEL 1179 1180 /* 1181 * Create a process to own SMB server threads (like zfs spa.c) 1182 * so we can see the CPU usage etc. with "prstat -L". 1183 * The new process MUST be in the same zone as the caller. 1184 */ 1185 static int 1186 smb_server_newproc(smb_server_t *sv) 1187 { 1188 int rc; 1189 1190 /* 1191 * Todo: Fix newproc() for zones. 1192 * At present, it always creates in p0. 1193 * For now, only do this for the global zone. 1194 */ 1195 if (getzoneid() != GLOBAL_ZONEID) 1196 return (0); 1197 1198 mutex_enter(&sv->sv_proc_lock); 1199 if (sv->sv_proc_p != NULL) { 1200 /* restart? re-use proc */ 1201 rc = 0; 1202 goto out; 1203 } 1204 1205 sv->sv_proc_state = SMB_THREAD_STATE_STARTING; 1206 rc = newproc(smb_server_proc_main, (caddr_t)sv, 1207 syscid, smbsrv_base_pri, NULL, 0); 1208 if (rc != 0) { 1209 cmn_err(CE_WARN, "newproc failed, rc=%d", rc); 1210 goto out; 1211 } 1212 1213 /* Rendez-vous with new proc thread. */ 1214 while (sv->sv_proc_state == SMB_THREAD_STATE_STARTING) { 1215 cv_wait(&sv->sv_proc_cv, &sv->sv_proc_lock); 1216 1217 } 1218 if (sv->sv_proc_state != SMB_THREAD_STATE_RUNNING) { 1219 rc = ESRCH; 1220 goto out; 1221 } 1222 ASSERT(sv->sv_proc_p != NULL); 1223 1224 out: 1225 mutex_exit(&sv->sv_proc_lock); 1226 return (rc); 1227 } 1228 1229 /* 1230 * Main thread for the process we create to own SMB server threads. 1231 */ 1232 static void 1233 smb_server_proc_main(void *arg) 1234 { 1235 callb_cpr_t cprinfo; 1236 smb_server_t *sv = arg; 1237 user_t *pu = PTOU(curproc); 1238 zoneid_t zid = getzoneid(); 1239 1240 ASSERT(curproc != &p0); 1241 ASSERT(zid == sv->sv_zid); 1242 1243 (void) strlcpy(pu->u_comm, "smbsrv", sizeof (pu->u_comm)); 1244 (void) snprintf(pu->u_psargs, sizeof (pu->u_psargs), 1245 "smbsrv %d", (int)zid); 1246 1247 CALLB_CPR_INIT(&cprinfo, &sv->sv_proc_lock, callb_generic_cpr, 1248 pu->u_psargs); 1249 1250 mutex_enter(&sv->sv_proc_lock); 1251 ASSERT(sv->sv_proc_state == SMB_THREAD_STATE_STARTING); 1252 1253 sv->sv_proc_p = curproc; 1254 sv->sv_proc_did = curthread->t_did; 1255 1256 sv->sv_proc_state = SMB_THREAD_STATE_RUNNING; 1257 cv_broadcast(&sv->sv_proc_cv); 1258 1259 CALLB_CPR_SAFE_BEGIN(&cprinfo); 1260 while (sv->sv_proc_state == SMB_THREAD_STATE_RUNNING) 1261 cv_wait(&sv->sv_proc_cv, &sv->sv_proc_lock); 1262 CALLB_CPR_SAFE_END(&cprinfo, &sv->sv_proc_lock); 1263 1264 ASSERT(sv->sv_proc_state == SMB_THREAD_STATE_EXITING); 1265 sv->sv_proc_state = SMB_THREAD_STATE_EXITED; 1266 sv->sv_proc_p = NULL; 1267 cv_broadcast(&sv->sv_proc_cv); 1268 CALLB_CPR_EXIT(&cprinfo); /* mutex_exit sv_proc_lock */ 1269 1270 /* Note: lwp_exit() expects p_lock entered. */ 1271 mutex_enter(&curproc->p_lock); 1272 lwp_exit(); 1273 } 1274 1275 /* 1276 * Delete the server proc (if any) 1277 */ 1278 static void 1279 smb_server_delproc(smb_server_t *sv) 1280 { 1281 1282 mutex_enter(&sv->sv_proc_lock); 1283 1284 if (sv->sv_proc_state != SMB_THREAD_STATE_RUNNING) 1285 goto out; 1286 ASSERT(sv->sv_proc_p != NULL); 1287 1288 sv->sv_proc_state = SMB_THREAD_STATE_EXITING; 1289 cv_broadcast(&sv->sv_proc_cv); 1290 1291 /* Rendez-vous with proc thread. */ 1292 while (sv->sv_proc_state == SMB_THREAD_STATE_EXITING) { 1293 cv_wait(&sv->sv_proc_cv, &sv->sv_proc_lock); 1294 1295 } 1296 if (sv->sv_proc_state != SMB_THREAD_STATE_EXITED) { 1297 cmn_err(CE_WARN, "smb_server_delproc, state=%d", 1298 sv->sv_proc_state); 1299 goto out; 1300 } 1301 if (sv->sv_proc_did != 0) { 1302 thread_join(sv->sv_proc_did); 1303 sv->sv_proc_did = 0; 1304 } 1305 1306 out: 1307 mutex_exit(&sv->sv_proc_lock); 1308 } 1309 1310 #endif /* _KERNEL */ 1311 1312 /* 1313 * ***************************************************************************** 1314 * **************** Functions called from the internal layers ****************** 1315 * ***************************************************************************** 1316 * 1317 * These functions are provided the relevant smb server by the caller. 1318 */ 1319 1320 void 1321 smb_server_get_cfg(smb_server_t *sv, smb_kmod_cfg_t *cfg) 1322 { 1323 rw_enter(&sv->sv_cfg_lock, RW_READER); 1324 bcopy(&sv->sv_cfg, cfg, sizeof (*cfg)); 1325 rw_exit(&sv->sv_cfg_lock); 1326 } 1327 1328 /* 1329 * 1330 */ 1331 void 1332 smb_server_inc_nbt_sess(smb_server_t *sv) 1333 { 1334 SMB_SERVER_VALID(sv); 1335 atomic_inc_32(&sv->sv_nbt_sess); 1336 } 1337 1338 void 1339 smb_server_dec_nbt_sess(smb_server_t *sv) 1340 { 1341 SMB_SERVER_VALID(sv); 1342 atomic_dec_32(&sv->sv_nbt_sess); 1343 } 1344 1345 void 1346 smb_server_inc_tcp_sess(smb_server_t *sv) 1347 { 1348 SMB_SERVER_VALID(sv); 1349 atomic_inc_32(&sv->sv_tcp_sess); 1350 } 1351 1352 void 1353 smb_server_dec_tcp_sess(smb_server_t *sv) 1354 { 1355 SMB_SERVER_VALID(sv); 1356 atomic_dec_32(&sv->sv_tcp_sess); 1357 } 1358 1359 void 1360 smb_server_inc_users(smb_server_t *sv) 1361 { 1362 SMB_SERVER_VALID(sv); 1363 atomic_inc_32(&sv->sv_users); 1364 } 1365 1366 void 1367 smb_server_dec_users(smb_server_t *sv) 1368 { 1369 SMB_SERVER_VALID(sv); 1370 atomic_dec_32(&sv->sv_users); 1371 } 1372 1373 void 1374 smb_server_inc_trees(smb_server_t *sv) 1375 { 1376 SMB_SERVER_VALID(sv); 1377 atomic_inc_32(&sv->sv_trees); 1378 } 1379 1380 void 1381 smb_server_dec_trees(smb_server_t *sv) 1382 { 1383 SMB_SERVER_VALID(sv); 1384 atomic_dec_32(&sv->sv_trees); 1385 } 1386 1387 void 1388 smb_server_inc_files(smb_server_t *sv) 1389 { 1390 SMB_SERVER_VALID(sv); 1391 atomic_inc_32(&sv->sv_files); 1392 } 1393 1394 void 1395 smb_server_dec_files(smb_server_t *sv) 1396 { 1397 SMB_SERVER_VALID(sv); 1398 atomic_dec_32(&sv->sv_files); 1399 } 1400 1401 void 1402 smb_server_inc_pipes(smb_server_t *sv) 1403 { 1404 SMB_SERVER_VALID(sv); 1405 atomic_inc_32(&sv->sv_pipes); 1406 } 1407 1408 void 1409 smb_server_dec_pipes(smb_server_t *sv) 1410 { 1411 SMB_SERVER_VALID(sv); 1412 atomic_dec_32(&sv->sv_pipes); 1413 } 1414 1415 void 1416 smb_server_add_rxb(smb_server_t *sv, int64_t value) 1417 { 1418 SMB_SERVER_VALID(sv); 1419 atomic_add_64(&sv->sv_rxb, value); 1420 } 1421 1422 void 1423 smb_server_add_txb(smb_server_t *sv, int64_t value) 1424 { 1425 SMB_SERVER_VALID(sv); 1426 atomic_add_64(&sv->sv_txb, value); 1427 } 1428 1429 void 1430 smb_server_inc_req(smb_server_t *sv) 1431 { 1432 SMB_SERVER_VALID(sv); 1433 atomic_inc_64(&sv->sv_nreq); 1434 } 1435 1436 /* 1437 * ***************************************************************************** 1438 * *************************** Static Functions ******************************** 1439 * ***************************************************************************** 1440 */ 1441 1442 static void 1443 smb_server_timers(smb_thread_t *thread, void *arg) 1444 { 1445 smb_server_t *sv = (smb_server_t *)arg; 1446 1447 ASSERT(sv != NULL); 1448 1449 /* 1450 * This kills old inactive sessions and expired durable 1451 * handles. The session code expects one call per minute. 1452 */ 1453 while (smb_thread_continue_timedwait(thread, 60 /* Seconds */)) { 1454 if (sv->sv_cfg.skc_keepalive != 0) 1455 smb_session_timers(sv); 1456 smb2_durable_timers(sv); 1457 } 1458 } 1459 1460 /* 1461 * smb_server_kstat_init 1462 */ 1463 static void 1464 smb_server_kstat_init(smb_server_t *sv) 1465 { 1466 1467 sv->sv_ksp = kstat_create_zone(SMBSRV_KSTAT_MODULE, 0, 1468 SMBSRV_KSTAT_STATISTICS, SMBSRV_KSTAT_CLASS, KSTAT_TYPE_RAW, 1469 sizeof (smbsrv_kstats_t), 0, sv->sv_zid); 1470 1471 if (sv->sv_ksp != NULL) { 1472 sv->sv_ksp->ks_update = smb_server_kstat_update; 1473 sv->sv_ksp->ks_private = sv; 1474 ((smbsrv_kstats_t *)sv->sv_ksp->ks_data)->ks_start_time = 1475 sv->sv_start_time; 1476 smb_dispatch_stats_init(sv); 1477 smb2_dispatch_stats_init(sv); 1478 kstat_install(sv->sv_ksp); 1479 } else { 1480 cmn_err(CE_WARN, "SMB Server: Statistics unavailable"); 1481 } 1482 1483 sv->sv_legacy_ksp = kstat_create_zone(SMBSRV_KSTAT_MODULE, 0, 1484 SMBSRV_KSTAT_NAME, SMBSRV_KSTAT_CLASS, KSTAT_TYPE_NAMED, 1485 sizeof (smb_server_legacy_kstat_t) / sizeof (kstat_named_t), 1486 0, sv->sv_zid); 1487 1488 if (sv->sv_legacy_ksp != NULL) { 1489 smb_server_legacy_kstat_t *ksd; 1490 1491 ksd = sv->sv_legacy_ksp->ks_data; 1492 1493 (void) strlcpy(ksd->ls_files.name, "open_files", 1494 sizeof (ksd->ls_files.name)); 1495 ksd->ls_files.data_type = KSTAT_DATA_UINT32; 1496 1497 (void) strlcpy(ksd->ls_trees.name, "connections", 1498 sizeof (ksd->ls_trees.name)); 1499 ksd->ls_trees.data_type = KSTAT_DATA_UINT32; 1500 1501 (void) strlcpy(ksd->ls_users.name, "connections", 1502 sizeof (ksd->ls_users.name)); 1503 ksd->ls_users.data_type = KSTAT_DATA_UINT32; 1504 1505 mutex_init(&sv->sv_legacy_ksmtx, NULL, MUTEX_DEFAULT, NULL); 1506 sv->sv_legacy_ksp->ks_lock = &sv->sv_legacy_ksmtx; 1507 sv->sv_legacy_ksp->ks_update = smb_server_legacy_kstat_update; 1508 kstat_install(sv->sv_legacy_ksp); 1509 } 1510 } 1511 1512 /* 1513 * smb_server_kstat_fini 1514 */ 1515 static void 1516 smb_server_kstat_fini(smb_server_t *sv) 1517 { 1518 if (sv->sv_legacy_ksp != NULL) { 1519 kstat_delete(sv->sv_legacy_ksp); 1520 mutex_destroy(&sv->sv_legacy_ksmtx); 1521 sv->sv_legacy_ksp = NULL; 1522 } 1523 1524 if (sv->sv_ksp != NULL) { 1525 kstat_delete(sv->sv_ksp); 1526 sv->sv_ksp = NULL; 1527 smb_dispatch_stats_fini(sv); 1528 smb2_dispatch_stats_fini(sv); 1529 } 1530 } 1531 1532 /* 1533 * Verify the defines in smb_kstat.h used by ks_reqs1 ks_reqs2 1534 */ 1535 CTASSERT(SMBSRV_KS_NREQS1 == SMB_COM_NUM); 1536 CTASSERT(SMBSRV_KS_NREQS2 == SMB2__NCMDS); 1537 1538 /* 1539 * smb_server_kstat_update 1540 */ 1541 static int 1542 smb_server_kstat_update(kstat_t *ksp, int rw) 1543 { 1544 smb_server_t *sv; 1545 smbsrv_kstats_t *ksd; 1546 1547 if (rw == KSTAT_READ) { 1548 sv = ksp->ks_private; 1549 SMB_SERVER_VALID(sv); 1550 ksd = (smbsrv_kstats_t *)ksp->ks_data; 1551 /* 1552 * Counters 1553 */ 1554 ksd->ks_nbt_sess = sv->sv_nbt_sess; 1555 ksd->ks_tcp_sess = sv->sv_tcp_sess; 1556 ksd->ks_users = sv->sv_users; 1557 ksd->ks_trees = sv->sv_trees; 1558 ksd->ks_files = sv->sv_files; 1559 ksd->ks_pipes = sv->sv_pipes; 1560 /* 1561 * Throughput 1562 */ 1563 ksd->ks_txb = sv->sv_txb; 1564 ksd->ks_rxb = sv->sv_rxb; 1565 ksd->ks_nreq = sv->sv_nreq; 1566 /* 1567 * Busyness 1568 */ 1569 ksd->ks_maxreqs = sv->sv_cfg.skc_maxworkers; 1570 smb_srqueue_update(&sv->sv_srqueue, 1571 &ksd->ks_utilization); 1572 /* 1573 * Latency & Throughput of the requests 1574 */ 1575 smb_dispatch_stats_update(sv, ksd->ks_reqs1, 0, SMB_COM_NUM); 1576 smb2_dispatch_stats_update(sv, ksd->ks_reqs2, 0, SMB2__NCMDS); 1577 return (0); 1578 } 1579 if (rw == KSTAT_WRITE) 1580 return (EACCES); 1581 1582 return (EIO); 1583 } 1584 1585 static int 1586 smb_server_legacy_kstat_update(kstat_t *ksp, int rw) 1587 { 1588 smb_server_t *sv; 1589 smb_server_legacy_kstat_t *ksd; 1590 int rc; 1591 1592 switch (rw) { 1593 case KSTAT_WRITE: 1594 rc = EACCES; 1595 break; 1596 case KSTAT_READ: 1597 if (!smb_server_lookup(&sv)) { 1598 ASSERT(MUTEX_HELD(ksp->ks_lock)); 1599 ASSERT(sv->sv_legacy_ksp == ksp); 1600 ksd = (smb_server_legacy_kstat_t *)ksp->ks_data; 1601 ksd->ls_files.value.ui32 = sv->sv_files + sv->sv_pipes; 1602 ksd->ls_trees.value.ui32 = sv->sv_trees; 1603 ksd->ls_users.value.ui32 = sv->sv_users; 1604 smb_server_release(sv); 1605 rc = 0; 1606 break; 1607 } 1608 /* FALLTHROUGH */ 1609 default: 1610 rc = EIO; 1611 break; 1612 } 1613 return (rc); 1614 1615 } 1616 1617 int smb_server_shutdown_wait1 = 15; /* seconds */ 1618 1619 /* 1620 * smb_server_shutdown 1621 */ 1622 static void 1623 smb_server_shutdown(smb_server_t *sv) 1624 { 1625 smb_llist_t *sl = &sv->sv_session_list; 1626 smb_session_t *session; 1627 clock_t time0, time1, time2; 1628 1629 SMB_SERVER_VALID(sv); 1630 1631 /* 1632 * Stop the listeners first, so we can't get any more 1633 * new sessions while we're trying to shut down. 1634 */ 1635 smb_server_listener_stop(&sv->sv_nbt_daemon); 1636 smb_server_listener_stop(&sv->sv_tcp_daemon); 1637 1638 /* 1639 * Disconnect all of the sessions. This causes all the 1640 * smb_server_receiver threads to see a disconnect and 1641 * begin tear-down (in parallel) in smb_session_cancel. 1642 */ 1643 smb_llist_enter(sl, RW_READER); 1644 session = smb_llist_head(sl); 1645 while (session != NULL) { 1646 smb_session_disconnect(session); 1647 session = smb_llist_next(sl, session); 1648 } 1649 smb_llist_exit(sl); 1650 1651 /* 1652 * Wake up any threads we might have blocked. 1653 * Must precede kdoor_close etc. because those will 1654 * wait for such threads to get out. 1655 */ 1656 smb_event_cancel(sv, 0); 1657 smb_threshold_wake_all(&sv->sv_ssetup_ct); 1658 smb_threshold_wake_all(&sv->sv_tcon_ct); 1659 smb_threshold_wake_all(&sv->sv_opipe_ct); 1660 1661 /* 1662 * Wait for the session list to empty. 1663 * (cv_signal in smb_server_destroy_session) 1664 * 1665 * We must wait for all the SMB session readers to finish, or 1666 * we could proceed here while there might be worker threads 1667 * running in any of those sessions. See smb_session_logoff 1668 * for timeouts applied to session tear-down. If this takes 1669 * longer than expected, make some noise, and fire a dtrace 1670 * probe one might use to investigate. 1671 */ 1672 time0 = ddi_get_lbolt(); 1673 time1 = SEC_TO_TICK(smb_server_shutdown_wait1) + time0; 1674 mutex_enter(&sv->sv_mutex); 1675 while (sv->sv_session_list.ll_count != 0) { 1676 if (cv_timedwait(&sv->sv_cv, &sv->sv_mutex, time1) < 0) { 1677 cmn_err(CE_NOTE, "!shutdown waited %d seconds" 1678 " with %d sessions still remaining", 1679 smb_server_shutdown_wait1, 1680 sv->sv_session_list.ll_count); 1681 DTRACE_PROBE1(max__wait, smb_server_t *, sv); 1682 break; 1683 } 1684 } 1685 while (sv->sv_session_list.ll_count != 0) { 1686 cv_wait(&sv->sv_cv, &sv->sv_mutex); 1687 } 1688 mutex_exit(&sv->sv_mutex); 1689 1690 time2 = ddi_get_lbolt(); 1691 if (time2 > time1) { 1692 cmn_err(CE_NOTE, "!shutdown waited %d seconds" 1693 " for all sessions to finish", 1694 (int)TICK_TO_SEC(time2 - time0)); 1695 } 1696 1697 smb_kdoor_close(sv); 1698 #ifdef _KERNEL 1699 smb_kshare_door_fini(sv->sv_lmshrd); 1700 #endif /* _KERNEL */ 1701 sv->sv_lmshrd = NULL; 1702 1703 smb_export_stop(sv); 1704 smb_kshare_stop(sv); 1705 smb_thread_stop(&sv->si_thread_timers); 1706 1707 /* 1708 * Both kshare and the oplock break sub-systems may have 1709 * taskq jobs on the spcial "server" session, until we've 1710 * closed all ofiles and stopped the kshare exporter. 1711 * Now it's safe to destroy the server session, but first 1712 * wait for any requests on it to finish. Note that for 1713 * normal sessions, this happens in smb_session_cancel, 1714 * but that's not called for the server session. 1715 */ 1716 if (sv->sv_rootuser != NULL) { 1717 smb_user_logoff(sv->sv_rootuser); 1718 smb_user_release(sv->sv_rootuser); 1719 sv->sv_rootuser = NULL; 1720 } 1721 if (sv->sv_session != NULL) { 1722 smb_session_cancel_requests(sv->sv_session, NULL, NULL); 1723 smb_slist_wait_for_empty(&sv->sv_session->s_req_list); 1724 1725 /* Just in case import left users and trees */ 1726 smb_session_logoff(sv->sv_session); 1727 1728 smb_session_delete(sv->sv_session); 1729 sv->sv_session = NULL; 1730 } 1731 1732 if (sv->sv_receiver_pool != NULL) { 1733 taskq_destroy(sv->sv_receiver_pool); 1734 sv->sv_receiver_pool = NULL; 1735 } 1736 1737 if (sv->sv_worker_pool != NULL) { 1738 taskq_destroy(sv->sv_worker_pool); 1739 sv->sv_worker_pool = NULL; 1740 } 1741 1742 if (sv->sv_notify_pool != NULL) { 1743 taskq_destroy(sv->sv_notify_pool); 1744 sv->sv_notify_pool = NULL; 1745 } 1746 1747 /* 1748 * Clean out any durable handles. After this we should 1749 * have no ofiles remaining (and no more oplock breaks). 1750 */ 1751 smb2_dh_shutdown(sv); 1752 1753 smb_server_fsop_stop(sv); 1754 1755 #ifdef _KERNEL 1756 if (sv->sv_proc_p != NULL) { 1757 smb_server_delproc(sv); 1758 } 1759 #endif 1760 } 1761 1762 /* 1763 * smb_server_listener_init 1764 * 1765 * Initializes listener contexts. 1766 */ 1767 static void 1768 smb_server_listener_init( 1769 smb_server_t *sv, 1770 smb_listener_daemon_t *ld, 1771 char *name, 1772 in_port_t port, 1773 int family) 1774 { 1775 ASSERT(ld->ld_magic != SMB_LISTENER_MAGIC); 1776 1777 bzero(ld, sizeof (*ld)); 1778 1779 ld->ld_sv = sv; 1780 ld->ld_family = family; 1781 ld->ld_port = port; 1782 1783 if (family == AF_INET) { 1784 ld->ld_sin.sin_family = (uint32_t)family; 1785 ld->ld_sin.sin_port = htons(port); 1786 ld->ld_sin.sin_addr.s_addr = htonl(INADDR_ANY); 1787 } else { 1788 ld->ld_sin6.sin6_family = (uint32_t)family; 1789 ld->ld_sin6.sin6_port = htons(port); 1790 (void) memset(&ld->ld_sin6.sin6_addr.s6_addr, 0, 1791 sizeof (ld->ld_sin6.sin6_addr.s6_addr)); 1792 } 1793 1794 smb_thread_init(&ld->ld_thread, name, smb_server_listener, ld, 1795 smbsrv_listen_pri, sv); 1796 ld->ld_magic = SMB_LISTENER_MAGIC; 1797 } 1798 1799 /* 1800 * smb_server_listener_destroy 1801 * 1802 * Destroyes listener contexts. 1803 */ 1804 static void 1805 smb_server_listener_destroy(smb_listener_daemon_t *ld) 1806 { 1807 /* 1808 * Note that if startup fails early, we can legitimately 1809 * get here with an all-zeros object. 1810 */ 1811 if (ld->ld_magic == 0) 1812 return; 1813 1814 SMB_LISTENER_VALID(ld); 1815 ASSERT(ld->ld_so == NULL); 1816 smb_thread_destroy(&ld->ld_thread); 1817 ld->ld_magic = 0; 1818 } 1819 1820 /* 1821 * smb_server_listener_start 1822 * 1823 * Starts the listener associated with the context passed in. 1824 * 1825 * Return: 0 Success 1826 * not 0 Failure 1827 */ 1828 static int 1829 smb_server_listener_start(smb_listener_daemon_t *ld) 1830 { 1831 int rc; 1832 uint32_t on; 1833 uint32_t off; 1834 1835 SMB_LISTENER_VALID(ld); 1836 1837 if (ld->ld_so != NULL) 1838 return (EINVAL); 1839 1840 ld->ld_so = smb_socreate(ld->ld_family, SOCK_STREAM, 0); 1841 if (ld->ld_so == NULL) { 1842 cmn_err(CE_WARN, "port %d: socket create failed", ld->ld_port); 1843 return (ENOMEM); 1844 } 1845 1846 off = 0; 1847 (void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET, 1848 SO_MAC_EXEMPT, &off, sizeof (off), CRED()); 1849 1850 on = 1; 1851 (void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET, 1852 SO_REUSEADDR, &on, sizeof (on), CRED()); 1853 1854 if (ld->ld_family == AF_INET) { 1855 rc = ksocket_bind(ld->ld_so, 1856 (struct sockaddr *)&ld->ld_sin, 1857 sizeof (ld->ld_sin), CRED()); 1858 } else { 1859 rc = ksocket_bind(ld->ld_so, 1860 (struct sockaddr *)&ld->ld_sin6, 1861 sizeof (ld->ld_sin6), CRED()); 1862 } 1863 1864 if (rc != 0) { 1865 cmn_err(CE_WARN, "port %d: bind failed", ld->ld_port); 1866 return (rc); 1867 } 1868 1869 rc = ksocket_listen(ld->ld_so, 20, CRED()); 1870 if (rc < 0) { 1871 cmn_err(CE_WARN, "port %d: listen failed", ld->ld_port); 1872 return (rc); 1873 } 1874 1875 ksocket_hold(ld->ld_so); 1876 rc = smb_thread_start(&ld->ld_thread); 1877 if (rc != 0) { 1878 ksocket_rele(ld->ld_so); 1879 cmn_err(CE_WARN, "port %d: listener failed to start", 1880 ld->ld_port); 1881 return (rc); 1882 } 1883 return (0); 1884 } 1885 1886 /* 1887 * smb_server_listener_stop 1888 * 1889 * Stops the listener associated with the context passed in. 1890 */ 1891 static void 1892 smb_server_listener_stop(smb_listener_daemon_t *ld) 1893 { 1894 SMB_LISTENER_VALID(ld); 1895 1896 if (ld->ld_so != NULL) { 1897 smb_soshutdown(ld->ld_so); 1898 smb_sodestroy(ld->ld_so); 1899 smb_thread_stop(&ld->ld_thread); 1900 ld->ld_so = NULL; 1901 } 1902 } 1903 1904 /* 1905 * smb_server_listener 1906 * 1907 * Entry point of the listeners. 1908 */ 1909 static void 1910 smb_server_listener(smb_thread_t *thread, void *arg) 1911 { 1912 _NOTE(ARGUNUSED(thread)) 1913 smb_listener_daemon_t *ld; 1914 ksocket_t s_so; 1915 int on; 1916 int txbuf_size; 1917 1918 ld = (smb_listener_daemon_t *)arg; 1919 1920 SMB_LISTENER_VALID(ld); 1921 1922 DTRACE_PROBE1(so__wait__accept, struct sonode *, ld->ld_so); 1923 1924 while (smb_thread_continue_nowait(&ld->ld_thread) && 1925 ld->ld_sv->sv_state != SMB_SERVER_STATE_STOPPING) { 1926 int ret = ksocket_accept(ld->ld_so, NULL, NULL, &s_so, CRED()); 1927 1928 switch (ret) { 1929 case 0: 1930 break; 1931 case ECONNABORTED: 1932 continue; 1933 1934 case EINTR: 1935 case EBADF: 1936 case ENOTSOCK: 1937 /* These are normal during shutdown. Silence. */ 1938 if (ld->ld_sv->sv_state == SMB_SERVER_STATE_STOPPING) 1939 goto out; 1940 /* FALLTHROUGH */ 1941 default: 1942 cmn_err(CE_WARN, 1943 "smb_server_listener: ksocket_accept failed (%d)", 1944 ret); 1945 /* avoid a tight CPU-burn loop here */ 1946 delay(MSEC_TO_TICK(10)); 1947 continue; 1948 } 1949 1950 DTRACE_PROBE1(so__accept, struct sonode *, s_so); 1951 1952 on = 1; 1953 (void) ksocket_setsockopt(s_so, IPPROTO_TCP, TCP_NODELAY, 1954 &on, sizeof (on), CRED()); 1955 1956 on = 1; 1957 (void) ksocket_setsockopt(s_so, SOL_SOCKET, SO_KEEPALIVE, 1958 &on, sizeof (on), CRED()); 1959 1960 txbuf_size = 128*1024; 1961 (void) ksocket_setsockopt(s_so, SOL_SOCKET, SO_SNDBUF, 1962 (const void *)&txbuf_size, sizeof (txbuf_size), CRED()); 1963 1964 /* 1965 * Create a session for this connection. 1966 */ 1967 smb_server_create_session(ld, s_so); 1968 } 1969 out: 1970 ksocket_rele(ld->ld_so); 1971 } 1972 1973 /* 1974 * smb_server_receiver 1975 * 1976 * Entry point of the receiver threads. 1977 * Also does cleanup when socket disconnected. 1978 */ 1979 static void 1980 smb_server_receiver(void *arg) 1981 { 1982 smb_session_t *session; 1983 1984 session = (smb_session_t *)arg; 1985 1986 /* We stay in here until socket disconnect. */ 1987 smb_session_receiver(session); 1988 1989 smb_server_destroy_session(session); 1990 } 1991 1992 /* 1993 * smb_server_lookup 1994 * 1995 * This function finds the server associated with the zone of the 1996 * caller. Note: requires a fix in the dynamic taskq code: 1997 * 1501 taskq_create_proc ... TQ_DYNAMIC puts tasks in p0 1998 */ 1999 int 2000 smb_server_lookup(smb_server_t **psv) 2001 { 2002 zoneid_t zid; 2003 smb_server_t *sv; 2004 2005 zid = getzoneid(); 2006 2007 smb_llist_enter(&smb_servers, RW_READER); 2008 sv = smb_llist_head(&smb_servers); 2009 while (sv) { 2010 SMB_SERVER_VALID(sv); 2011 if (sv->sv_zid == zid) { 2012 mutex_enter(&sv->sv_mutex); 2013 if (sv->sv_state != SMB_SERVER_STATE_DELETING) { 2014 sv->sv_refcnt++; 2015 mutex_exit(&sv->sv_mutex); 2016 smb_llist_exit(&smb_servers); 2017 *psv = sv; 2018 return (0); 2019 } 2020 mutex_exit(&sv->sv_mutex); 2021 break; 2022 } 2023 sv = smb_llist_next(&smb_servers, sv); 2024 } 2025 smb_llist_exit(&smb_servers); 2026 return (EPERM); 2027 } 2028 2029 /* 2030 * smb_server_release 2031 * 2032 * This function decrements the reference count of the server and signals its 2033 * condition variable if the state of the server is SMB_SERVER_STATE_DELETING. 2034 */ 2035 void 2036 smb_server_release(smb_server_t *sv) 2037 { 2038 SMB_SERVER_VALID(sv); 2039 2040 mutex_enter(&sv->sv_mutex); 2041 ASSERT(sv->sv_refcnt); 2042 sv->sv_refcnt--; 2043 if ((sv->sv_refcnt == 0) && (sv->sv_state == SMB_SERVER_STATE_DELETING)) 2044 cv_signal(&sv->sv_cv); 2045 mutex_exit(&sv->sv_mutex); 2046 } 2047 2048 /* 2049 * smb_server_lookup_user 2050 * 2051 * Find an smb_user by its user_id 2052 * Optional ssn_id (if non-zero) restricts search to a specific session. 2053 * 2054 * Returns smb_user_t * held. Caller must smb_user_rele(user) 2055 * Returns NULL if not found. 2056 */ 2057 smb_user_t * 2058 smb_server_lookup_user(smb_server_t *sv, uint64_t ssn_id, uint64_t user_id) 2059 { 2060 smb_llist_t *slist = &sv->sv_session_list; 2061 smb_session_t *sn; 2062 smb_user_t *user = NULL; 2063 2064 smb_llist_enter(slist, RW_READER); 2065 2066 for (sn = smb_llist_head(slist); 2067 sn != NULL && user == NULL; 2068 sn = smb_llist_next(slist, sn)) { 2069 SMB_SESSION_VALID(sn); 2070 2071 if (ssn_id != 0 && ssn_id != sn->s_kid) 2072 continue; 2073 if (sn->s_state != SMB_SESSION_STATE_NEGOTIATED) 2074 continue; 2075 2076 user = smb_session_lookup_ssnid(sn, user_id); 2077 } 2078 smb_llist_exit(slist); 2079 2080 return (user); 2081 } 2082 2083 /* 2084 * Enumerate the users associated with a session list. 2085 */ 2086 static void 2087 smb_server_enum_users(smb_server_t *sv, smb_svcenum_t *svcenum) 2088 { 2089 smb_llist_t *ll = &sv->sv_session_list; 2090 smb_session_t *sn; 2091 smb_llist_t *ulist; 2092 smb_user_t *user; 2093 int rc = 0; 2094 2095 smb_llist_enter(ll, RW_READER); 2096 sn = smb_llist_head(ll); 2097 2098 while (sn != NULL) { 2099 SMB_SESSION_VALID(sn); 2100 ulist = &sn->s_user_list; 2101 smb_llist_enter(ulist, RW_READER); 2102 user = smb_llist_head(ulist); 2103 2104 while (user != NULL) { 2105 if (smb_user_hold(user)) { 2106 rc = smb_user_enum(user, svcenum); 2107 smb_user_release(user); 2108 if (rc != 0) 2109 break; 2110 } 2111 2112 user = smb_llist_next(ulist, user); 2113 } 2114 2115 smb_llist_exit(ulist); 2116 2117 if (rc != 0) 2118 break; 2119 2120 sn = smb_llist_next(ll, sn); 2121 } 2122 2123 smb_llist_exit(ll); 2124 } 2125 2126 /* 2127 * Enumerate the trees/files associated with a session list. 2128 */ 2129 static void 2130 smb_server_enum_trees(smb_server_t *sv, smb_svcenum_t *svcenum) 2131 { 2132 smb_llist_t *ll = &sv->sv_session_list; 2133 smb_session_t *sn; 2134 smb_llist_t *tlist; 2135 smb_tree_t *tree; 2136 int rc = 0; 2137 2138 smb_llist_enter(ll, RW_READER); 2139 sn = smb_llist_head(ll); 2140 2141 while (sn != NULL) { 2142 SMB_SESSION_VALID(sn); 2143 tlist = &sn->s_tree_list; 2144 smb_llist_enter(tlist, RW_READER); 2145 tree = smb_llist_head(tlist); 2146 2147 while (tree != NULL) { 2148 if (smb_tree_hold(tree)) { 2149 rc = smb_tree_enum(tree, svcenum); 2150 smb_tree_release(tree); 2151 if (rc != 0) 2152 break; 2153 } 2154 2155 tree = smb_llist_next(tlist, tree); 2156 } 2157 2158 smb_llist_exit(tlist); 2159 2160 if (rc != 0) 2161 break; 2162 2163 sn = smb_llist_next(ll, sn); 2164 } 2165 2166 smb_llist_exit(ll); 2167 } 2168 2169 /* 2170 * Disconnect sessions associated with the specified client and username. 2171 * Empty strings are treated as wildcards. 2172 */ 2173 static int 2174 smb_server_session_disconnect(smb_server_t *sv, 2175 const char *client, const char *name) 2176 { 2177 smb_llist_t *ll = &sv->sv_session_list; 2178 smb_session_t *sn; 2179 smb_llist_t *ulist; 2180 smb_user_t *user; 2181 int count = 0; 2182 2183 smb_llist_enter(ll, RW_READER); 2184 2185 for (sn = smb_llist_head(ll); 2186 sn != NULL; 2187 sn = smb_llist_next(ll, sn)) { 2188 SMB_SESSION_VALID(sn); 2189 2190 if (*client != '\0' && !smb_session_isclient(sn, client)) 2191 continue; 2192 2193 ulist = &sn->s_user_list; 2194 smb_llist_enter(ulist, RW_READER); 2195 2196 for (user = smb_llist_head(ulist); 2197 user != NULL; 2198 user = smb_llist_next(ulist, user)) { 2199 2200 if (smb_user_hold(user)) { 2201 2202 if (*name == '\0' || 2203 smb_user_namecmp(user, name)) { 2204 smb_user_logoff(user); 2205 count++; 2206 } 2207 2208 smb_user_release(user); 2209 } 2210 } 2211 2212 smb_llist_exit(ulist); 2213 } 2214 2215 smb_llist_exit(ll); 2216 return (count); 2217 } 2218 2219 /* 2220 * Close a file by its unique id. 2221 */ 2222 static int 2223 smb_server_fclose(smb_server_t *sv, uint32_t uniqid) 2224 { 2225 smb_llist_t *ll; 2226 smb_session_t *sn; 2227 smb_llist_t *tlist; 2228 smb_tree_t *tree; 2229 int rc = ENOENT; 2230 2231 ll = &sv->sv_session_list; 2232 smb_llist_enter(ll, RW_READER); 2233 sn = smb_llist_head(ll); 2234 2235 while ((sn != NULL) && (rc == ENOENT)) { 2236 SMB_SESSION_VALID(sn); 2237 tlist = &sn->s_tree_list; 2238 smb_llist_enter(tlist, RW_READER); 2239 tree = smb_llist_head(tlist); 2240 2241 while ((tree != NULL) && (rc == ENOENT)) { 2242 if (smb_tree_hold(tree)) { 2243 rc = smb_tree_fclose(tree, uniqid); 2244 smb_tree_release(tree); 2245 } 2246 2247 tree = smb_llist_next(tlist, tree); 2248 } 2249 2250 smb_llist_exit(tlist); 2251 sn = smb_llist_next(ll, sn); 2252 } 2253 2254 smb_llist_exit(ll); 2255 return (rc); 2256 } 2257 2258 /* 2259 * This is used by SMB2 session setup to logoff a previous session, 2260 * so it can force a logoff that we haven't noticed yet. 2261 * This is not called frequently, so we just walk the list of 2262 * connections searching for the user. 2263 * 2264 * Note that this must wait for any durable handles (ofiles) 2265 * owned by this user to become "orphaned", so that a reconnect 2266 * that may immediately follow can find and use such ofiles. 2267 */ 2268 void 2269 smb_server_logoff_ssnid(smb_request_t *sr, uint64_t ssnid) 2270 { 2271 smb_server_t *sv = sr->sr_server; 2272 smb_llist_t *sess_list; 2273 smb_session_t *sess; 2274 smb_user_t *user = NULL; 2275 boolean_t do_logoff = B_FALSE; 2276 2277 SMB_SERVER_VALID(sv); 2278 2279 if (sv->sv_state != SMB_SERVER_STATE_RUNNING) 2280 return; 2281 2282 sess_list = &sv->sv_session_list; 2283 smb_llist_enter(sess_list, RW_READER); 2284 2285 for (sess = smb_llist_head(sess_list); 2286 sess != NULL; 2287 sess = smb_llist_next(sess_list, sess)) { 2288 2289 SMB_SESSION_VALID(sess); 2290 2291 if (sess->dialect < SMB_VERS_2_BASE) 2292 continue; 2293 2294 switch (sess->s_state) { 2295 case SMB_SESSION_STATE_NEGOTIATED: 2296 case SMB_SESSION_STATE_TERMINATED: 2297 case SMB_SESSION_STATE_DISCONNECTED: 2298 break; 2299 default: 2300 continue; 2301 } 2302 2303 /* 2304 * Normal situation is to find a LOGGED_ON user. 2305 */ 2306 user = smb_session_lookup_uid_st(sess, ssnid, 0, 2307 SMB_USER_STATE_LOGGED_ON); 2308 if (user != NULL) { 2309 if (smb_is_same_user(user->u_cred, sr->user_cr)) { 2310 do_logoff = B_TRUE; 2311 break; 2312 } 2313 smb_user_release(user); 2314 user = NULL; 2315 } 2316 2317 /* 2318 * If we raced with disconnect, may find LOGGING_OFF, 2319 * in which case we want to just wait for it. 2320 */ 2321 user = smb_session_lookup_uid_st(sess, ssnid, 0, 2322 SMB_USER_STATE_LOGGING_OFF); 2323 if (user != NULL) { 2324 if (smb_is_same_user(user->u_cred, sr->user_cr)) 2325 break; 2326 smb_user_release(user); 2327 user = NULL; 2328 } 2329 } 2330 2331 smb_llist_exit(sess_list); 2332 2333 /* 2334 * Note that smb_user_logoff() can block for a while if 2335 * smb_session_disconnect_owned_trees, smb_ofile_close_all 2336 * ends up blocked on locks held by running requests. 2337 * Do that while not holding the session list rwlock. 2338 */ 2339 if (user != NULL) { 2340 if (do_logoff) { 2341 /* Treat this as if we lost the connection */ 2342 user->preserve_opens = SMB2_DH_PRESERVE_SOME; 2343 smb_user_logoff(user); 2344 } 2345 2346 /* 2347 * Wait for durable handles to be orphaned. 2348 * Note: not holding the sess list rwlock. 2349 */ 2350 smb_user_wait_trees(user); 2351 2352 /* 2353 * Could be doing the last release on a user below, 2354 * which can leave work on the delete queues for 2355 * s_user_list or s_tree_list so flush those. 2356 * Must hold the session list after the user release 2357 * so that the session can't go away while we flush. 2358 */ 2359 smb_llist_enter(sess_list, RW_READER); 2360 2361 sess = user->u_session; 2362 smb_user_release(user); 2363 2364 smb_llist_flush(&sess->s_tree_list); 2365 smb_llist_flush(&sess->s_user_list); 2366 2367 smb_llist_exit(sess_list); 2368 } 2369 } 2370 2371 /* See also: libsmb smb_kmod_setcfg */ 2372 static void 2373 smb_server_store_cfg(smb_server_t *sv, smb_ioc_cfg_t *ioc) 2374 { 2375 if (ioc->maxconnections == 0) 2376 ioc->maxconnections = 0xFFFFFFFF; 2377 2378 if (ioc->encrypt == SMB_CONFIG_REQUIRED && 2379 ioc->max_protocol < SMB_VERS_3_0) { 2380 cmn_err(CE_WARN, "Server set to require encryption; " 2381 "forcing max_protocol to 3.0"); 2382 ioc->max_protocol = SMB_VERS_3_0; 2383 } 2384 sv->sv_cfg.skc_maxworkers = ioc->maxworkers; 2385 sv->sv_cfg.skc_maxconnections = ioc->maxconnections; 2386 sv->sv_cfg.skc_keepalive = ioc->keepalive; 2387 sv->sv_cfg.skc_restrict_anon = ioc->restrict_anon; 2388 sv->sv_cfg.skc_signing_enable = ioc->signing_enable; 2389 sv->sv_cfg.skc_signing_required = ioc->signing_required; 2390 sv->sv_cfg.skc_oplock_enable = ioc->oplock_enable; 2391 sv->sv_cfg.skc_sync_enable = ioc->sync_enable; 2392 sv->sv_cfg.skc_secmode = ioc->secmode; 2393 sv->sv_cfg.skc_netbios_enable = ioc->netbios_enable; 2394 sv->sv_cfg.skc_ipv6_enable = ioc->ipv6_enable; 2395 sv->sv_cfg.skc_print_enable = ioc->print_enable; 2396 sv->sv_cfg.skc_traverse_mounts = ioc->traverse_mounts; 2397 sv->sv_cfg.skc_short_names = ioc->short_names; 2398 sv->sv_cfg.skc_max_protocol = ioc->max_protocol; 2399 sv->sv_cfg.skc_min_protocol = ioc->min_protocol; 2400 sv->sv_cfg.skc_encrypt = ioc->encrypt; 2401 sv->sv_cfg.skc_encrypt_ciphers = ioc->encrypt_ciphers; 2402 sv->sv_cfg.skc_execflags = ioc->exec_flags; 2403 sv->sv_cfg.skc_negtok_len = ioc->negtok_len; 2404 sv->sv_cfg.skc_max_opens = ioc->max_opens; 2405 sv->sv_cfg.skc_version = ioc->version; 2406 sv->sv_cfg.skc_initial_credits = ioc->initial_credits; 2407 sv->sv_cfg.skc_maximum_credits = ioc->maximum_credits; 2408 2409 (void) memcpy(sv->sv_cfg.skc_machine_uuid, ioc->machine_uuid, 2410 sizeof (uuid_t)); 2411 (void) memcpy(sv->sv_cfg.skc_negtok, ioc->negtok, 2412 sizeof (sv->sv_cfg.skc_negtok)); 2413 (void) memcpy(sv->sv_cfg.skc_native_os, ioc->native_os, 2414 sizeof (sv->sv_cfg.skc_native_os)); 2415 (void) memcpy(sv->sv_cfg.skc_native_lm, ioc->native_lm, 2416 sizeof (sv->sv_cfg.skc_native_lm)); 2417 2418 (void) strlcpy(sv->sv_cfg.skc_nbdomain, ioc->nbdomain, 2419 sizeof (sv->sv_cfg.skc_nbdomain)); 2420 (void) strlcpy(sv->sv_cfg.skc_fqdn, ioc->fqdn, 2421 sizeof (sv->sv_cfg.skc_fqdn)); 2422 (void) strlcpy(sv->sv_cfg.skc_hostname, ioc->hostname, 2423 sizeof (sv->sv_cfg.skc_hostname)); 2424 (void) strlcpy(sv->sv_cfg.skc_system_comment, ioc->system_comment, 2425 sizeof (sv->sv_cfg.skc_system_comment)); 2426 } 2427 2428 static int 2429 smb_server_fsop_start(smb_server_t *sv) 2430 { 2431 int error; 2432 2433 error = smb_node_root_init(sv, &sv->si_root_smb_node); 2434 if (error != 0) 2435 sv->si_root_smb_node = NULL; 2436 2437 return (error); 2438 } 2439 2440 static void 2441 smb_server_fsop_stop(smb_server_t *sv) 2442 { 2443 if (sv->si_root_smb_node != NULL) { 2444 smb_node_release(sv->si_root_smb_node); 2445 sv->si_root_smb_node = NULL; 2446 } 2447 } 2448 2449 smb_event_t * 2450 smb_event_create(smb_server_t *sv, int timeout) 2451 { 2452 smb_event_t *event; 2453 2454 if (smb_server_is_stopping(sv)) 2455 return (NULL); 2456 2457 event = kmem_cache_alloc(smb_cache_event, KM_SLEEP); 2458 2459 bzero(event, sizeof (smb_event_t)); 2460 mutex_init(&event->se_mutex, NULL, MUTEX_DEFAULT, NULL); 2461 cv_init(&event->se_cv, NULL, CV_DEFAULT, NULL); 2462 event->se_magic = SMB_EVENT_MAGIC; 2463 event->se_txid = smb_event_alloc_txid(); 2464 event->se_server = sv; 2465 event->se_timeout = timeout; 2466 2467 smb_llist_enter(&sv->sv_event_list, RW_WRITER); 2468 smb_llist_insert_tail(&sv->sv_event_list, event); 2469 smb_llist_exit(&sv->sv_event_list); 2470 2471 return (event); 2472 } 2473 2474 void 2475 smb_event_destroy(smb_event_t *event) 2476 { 2477 smb_server_t *sv; 2478 2479 if (event == NULL) 2480 return; 2481 2482 SMB_EVENT_VALID(event); 2483 ASSERT(event->se_waittime == 0); 2484 sv = event->se_server; 2485 SMB_SERVER_VALID(sv); 2486 2487 smb_llist_enter(&sv->sv_event_list, RW_WRITER); 2488 smb_llist_remove(&sv->sv_event_list, event); 2489 smb_llist_exit(&sv->sv_event_list); 2490 2491 event->se_magic = (uint32_t)~SMB_EVENT_MAGIC; 2492 cv_destroy(&event->se_cv); 2493 mutex_destroy(&event->se_mutex); 2494 2495 kmem_cache_free(smb_cache_event, event); 2496 } 2497 2498 /* 2499 * Get the txid for the specified event. 2500 */ 2501 uint32_t 2502 smb_event_txid(smb_event_t *event) 2503 { 2504 if (event != NULL) { 2505 SMB_EVENT_VALID(event); 2506 return (event->se_txid); 2507 } 2508 2509 cmn_err(CE_NOTE, "smb_event_txid failed"); 2510 return ((uint32_t)-1); 2511 } 2512 2513 /* 2514 * Wait for event notification. 2515 */ 2516 int 2517 smb_event_wait(smb_event_t *event) 2518 { 2519 int seconds = 1; 2520 int ticks; 2521 int err; 2522 2523 if (event == NULL) 2524 return (EINVAL); 2525 2526 SMB_EVENT_VALID(event); 2527 2528 mutex_enter(&event->se_mutex); 2529 event->se_waittime = 1; 2530 event->se_errno = 0; 2531 2532 while (!(event->se_notified)) { 2533 if (smb_event_debug && ((event->se_waittime % 30) == 0)) 2534 cmn_err(CE_NOTE, "smb_event_wait[%d] (%d sec)", 2535 event->se_txid, event->se_waittime); 2536 2537 if (event->se_errno != 0) 2538 break; 2539 2540 if (event->se_waittime > event->se_timeout) { 2541 event->se_errno = ETIME; 2542 break; 2543 } 2544 2545 ticks = SEC_TO_TICK(seconds); 2546 (void) cv_reltimedwait(&event->se_cv, 2547 &event->se_mutex, (clock_t)ticks, TR_CLOCK_TICK); 2548 ++event->se_waittime; 2549 } 2550 2551 err = event->se_errno; 2552 event->se_waittime = 0; 2553 event->se_notified = B_FALSE; 2554 cv_signal(&event->se_cv); 2555 mutex_exit(&event->se_mutex); 2556 return (err); 2557 } 2558 2559 /* 2560 * If txid is non-zero, cancel the specified event. 2561 * Otherwise, cancel all events. 2562 */ 2563 static void 2564 smb_event_cancel(smb_server_t *sv, uint32_t txid) 2565 { 2566 smb_event_t *event; 2567 smb_llist_t *event_list; 2568 2569 SMB_SERVER_VALID(sv); 2570 2571 event_list = &sv->sv_event_list; 2572 smb_llist_enter(event_list, RW_WRITER); 2573 2574 event = smb_llist_head(event_list); 2575 while (event) { 2576 SMB_EVENT_VALID(event); 2577 2578 if (txid == 0 || event->se_txid == txid) { 2579 mutex_enter(&event->se_mutex); 2580 event->se_errno = ECANCELED; 2581 event->se_notified = B_TRUE; 2582 cv_signal(&event->se_cv); 2583 mutex_exit(&event->se_mutex); 2584 2585 if (txid != 0) 2586 break; 2587 } 2588 2589 event = smb_llist_next(event_list, event); 2590 } 2591 2592 smb_llist_exit(event_list); 2593 } 2594 2595 /* 2596 * If txid is non-zero, notify the specified event. 2597 * Otherwise, notify all events. 2598 */ 2599 void 2600 smb_event_notify(smb_server_t *sv, uint32_t txid) 2601 { 2602 smb_event_t *event; 2603 smb_llist_t *event_list; 2604 2605 SMB_SERVER_VALID(sv); 2606 2607 event_list = &sv->sv_event_list; 2608 smb_llist_enter(event_list, RW_READER); 2609 2610 event = smb_llist_head(event_list); 2611 while (event) { 2612 SMB_EVENT_VALID(event); 2613 2614 if (txid == 0 || event->se_txid == txid) { 2615 mutex_enter(&event->se_mutex); 2616 event->se_notified = B_TRUE; 2617 cv_signal(&event->se_cv); 2618 mutex_exit(&event->se_mutex); 2619 2620 if (txid != 0) 2621 break; 2622 } 2623 2624 event = smb_llist_next(event_list, event); 2625 } 2626 2627 smb_llist_exit(event_list); 2628 } 2629 2630 /* 2631 * Allocate a new transaction id (txid). 2632 * 2633 * 0 or -1 are not assigned because they are used to detect invalid 2634 * conditions or to indicate all open id's. 2635 */ 2636 static uint32_t 2637 smb_event_alloc_txid(void) 2638 { 2639 static kmutex_t txmutex; 2640 static uint32_t txid; 2641 uint32_t txid_ret; 2642 2643 mutex_enter(&txmutex); 2644 2645 if (txid == 0) 2646 txid = ddi_get_lbolt() << 11; 2647 2648 do { 2649 ++txid; 2650 } while (txid == 0 || txid == (uint32_t)-1); 2651 2652 txid_ret = txid; 2653 mutex_exit(&txmutex); 2654 2655 return (txid_ret); 2656 } 2657 2658 /* 2659 * Called by the ioctl to find the corresponding 2660 * spooldoc node. removes node on success 2661 * 2662 * Return values 2663 * rc 2664 * B_FALSE - not found 2665 * B_TRUE - found 2666 * 2667 */ 2668 2669 static boolean_t 2670 smb_spool_lookup_doc_byfid(smb_server_t *sv, uint16_t fid, 2671 smb_kspooldoc_t *spdoc) 2672 { 2673 smb_kspooldoc_t *sp; 2674 smb_llist_t *splist; 2675 2676 splist = &sv->sp_info.sp_list; 2677 smb_llist_enter(splist, RW_WRITER); 2678 sp = smb_llist_head(splist); 2679 while (sp != NULL) { 2680 /* 2681 * check for a matching fid 2682 */ 2683 if (sp->sd_fid == fid) { 2684 *spdoc = *sp; 2685 smb_llist_remove(splist, sp); 2686 smb_llist_exit(splist); 2687 kmem_free(sp, sizeof (smb_kspooldoc_t)); 2688 return (B_TRUE); 2689 } 2690 sp = smb_llist_next(splist, sp); 2691 } 2692 cmn_err(CE_WARN, "smb_spool_lookup_user_byfid: no fid:%d", fid); 2693 smb_llist_exit(splist); 2694 return (B_FALSE); 2695 } 2696 2697 /* 2698 * Adds the spool fid to a linked list to be used 2699 * as a search key in the spooldoc queue 2700 * 2701 * Return values 2702 * rc non-zero error 2703 * rc zero success 2704 * 2705 */ 2706 2707 void 2708 smb_spool_add_fid(smb_server_t *sv, uint16_t fid) 2709 { 2710 smb_llist_t *fidlist; 2711 smb_spoolfid_t *sf; 2712 2713 if (sv->sv_cfg.skc_print_enable == 0) 2714 return; 2715 2716 sf = kmem_zalloc(sizeof (smb_spoolfid_t), KM_SLEEP); 2717 fidlist = &sv->sp_info.sp_fidlist; 2718 smb_llist_enter(fidlist, RW_WRITER); 2719 sf->sf_fid = fid; 2720 smb_llist_insert_tail(fidlist, sf); 2721 smb_llist_exit(fidlist); 2722 cv_broadcast(&sv->sp_info.sp_cv); 2723 } 2724 2725 /* 2726 * Called by the ioctl to get and remove the head of the fid list 2727 * 2728 * Return values 2729 * int fd 2730 * greater than 0 success 2731 * 0 - error 2732 * 2733 */ 2734 2735 static uint16_t 2736 smb_spool_get_fid(smb_server_t *sv) 2737 { 2738 smb_spoolfid_t *spfid; 2739 smb_llist_t *splist; 2740 uint16_t fid; 2741 2742 splist = &sv->sp_info.sp_fidlist; 2743 smb_llist_enter(splist, RW_WRITER); 2744 spfid = smb_llist_head(splist); 2745 if (spfid != NULL) { 2746 fid = spfid->sf_fid; 2747 smb_llist_remove(&sv->sp_info.sp_fidlist, spfid); 2748 kmem_free(spfid, sizeof (smb_spoolfid_t)); 2749 } else { 2750 fid = 0; 2751 } 2752 smb_llist_exit(splist); 2753 return (fid); 2754 } 2755 2756 /* 2757 * Adds the spooldoc to the tail of the spooldoc list 2758 * 2759 * Return values 2760 * rc non-zero error 2761 * rc zero success 2762 */ 2763 int 2764 smb_spool_add_doc(smb_tree_t *tree, smb_kspooldoc_t *sp) 2765 { 2766 smb_llist_t *splist; 2767 smb_server_t *sv = tree->t_server; 2768 int rc = 0; 2769 2770 splist = &sv->sp_info.sp_list; 2771 smb_llist_enter(splist, RW_WRITER); 2772 sp->sd_spool_num = atomic_inc_32_nv(&sv->sp_info.sp_cnt); 2773 smb_llist_insert_tail(splist, sp); 2774 smb_llist_exit(splist); 2775 2776 return (rc); 2777 } 2778 2779 /* 2780 * smb_server_create_session 2781 */ 2782 static void 2783 smb_server_create_session(smb_listener_daemon_t *ld, ksocket_t s_so) 2784 { 2785 smb_server_t *sv = ld->ld_sv; 2786 smb_session_t *session; 2787 smb_llist_t *sl; 2788 taskqid_t tqid; 2789 clock_t now; 2790 2791 session = smb_session_create(s_so, ld->ld_port, sv, 2792 ld->ld_family); 2793 2794 if (session == NULL) { 2795 /* This should be rare (create sleeps) */ 2796 smb_soshutdown(s_so); 2797 smb_sodestroy(s_so); 2798 cmn_err(CE_WARN, "SMB Session: alloc failed"); 2799 return; 2800 } 2801 2802 sl = &sv->sv_session_list; 2803 smb_llist_enter(sl, RW_WRITER); 2804 if (smb_llist_get_count(sl) >= sv->sv_cfg.skc_maxconnections) { 2805 /* 2806 * New session not in sv_session_list, so we can just 2807 * delete it directly. 2808 */ 2809 smb_llist_exit(sl); 2810 DTRACE_PROBE1(maxconn, smb_session_t *, session); 2811 smb_soshutdown(session->sock); 2812 smb_session_delete(session); 2813 goto logmaxconn; 2814 } 2815 smb_llist_insert_tail(sl, session); 2816 smb_llist_exit(sl); 2817 2818 /* 2819 * These taskq entries must run independently of one another, 2820 * so TQ_NOQUEUE. TQ_SLEEP (==0) just for clarity. 2821 */ 2822 tqid = taskq_dispatch(sv->sv_receiver_pool, 2823 smb_server_receiver, session, TQ_NOQUEUE | TQ_SLEEP); 2824 if (tqid != TASKQID_INVALID) { 2825 /* Success */ 2826 return; 2827 } 2828 2829 /* 2830 * Have: tqid == TASKQID_INVALID 2831 * We never entered smb_server_receiver() 2832 * so need to do its return cleanup 2833 */ 2834 DTRACE_PROBE1(maxconn, smb_session_t *, session); 2835 smb_session_disconnect(session); 2836 smb_session_logoff(session); 2837 smb_server_destroy_session(session); 2838 2839 logmaxconn: 2840 /* 2841 * If we hit max_connections, log something so an admin 2842 * can find out why new connections are failing, but 2843 * log this no more than once a minute. 2844 */ 2845 now = ddi_get_lbolt(); 2846 if (now > ld->ld_quiet) { 2847 ld->ld_quiet = now + SEC_TO_TICK(60); 2848 cmn_err(CE_WARN, "SMB can't create session: " 2849 "Would exceed max_connections."); 2850 } 2851 } 2852 2853 static void 2854 smb_server_destroy_session(smb_session_t *session) 2855 { 2856 smb_server_t *sv; 2857 smb_llist_t *ll; 2858 uint32_t count; 2859 2860 ASSERT(session->s_server != NULL); 2861 sv = session->s_server; 2862 ll = &sv->sv_session_list; 2863 2864 smb_llist_flush(&session->s_tree_list); 2865 smb_llist_flush(&session->s_user_list); 2866 2867 smb_llist_enter(ll, RW_WRITER); 2868 smb_llist_remove(ll, session); 2869 count = ll->ll_count; 2870 smb_llist_exit(ll); 2871 2872 /* 2873 * Normally, the session should have state SHUTDOWN here. 2874 * If the session has any ofiles remaining, eg. due to 2875 * forgotten ofile references or something, the state 2876 * will be _DISCONNECTED or _TERMINATED. Keep such 2877 * sessions in the list of zombies (for debugging). 2878 */ 2879 if (session->s_state == SMB_SESSION_STATE_SHUTDOWN) { 2880 smb_session_delete(session); 2881 } else { 2882 cmn_err(CE_NOTE, "!Leaked session: 0x%p", (void *)session); 2883 DTRACE_PROBE1(new__zombie, smb_session_t *, session); 2884 smb_llist_enter(&smb_server_session_zombies, RW_WRITER); 2885 smb_llist_insert_head(&smb_server_session_zombies, session); 2886 smb_llist_exit(&smb_server_session_zombies); 2887 } 2888 2889 if (count == 0) { 2890 /* See smb_server_shutdown */ 2891 cv_signal(&sv->sv_cv); 2892 } 2893 } 2894