xref: /titanic_41/usr/src/uts/common/fs/smbsrv/smb_server.c (revision ba7866cd2cbdf574f47d4e38a1301b90744dd677)
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  */
24 
25 /*
26  * General Structures Layout
27  * -------------------------
28  *
29  * This is a simplified diagram showing the relationship between most of the
30  * main structures.
31  *
32  * +-------------------+
33  * |     SMB_SERVER    |
34  * +-------------------+
35  *          |
36  *          |
37  *          v
38  * +-------------------+       +-------------------+      +-------------------+
39  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
40  * +-------------------+       +-------------------+      +-------------------+
41  *          |
42  *          |
43  *          v
44  * +-------------------+       +-------------------+      +-------------------+
45  * |       USER        |<----->|       USER        |......|       USER        |
46  * +-------------------+       +-------------------+      +-------------------+
47  *          |
48  *          |
49  *          v
50  * +-------------------+       +-------------------+      +-------------------+
51  * |       TREE        |<----->|       TREE        |......|       TREE        |
52  * +-------------------+       +-------------------+      +-------------------+
53  *      |         |
54  *      |         |
55  *      |         v
56  *      |     +-------+       +-------+      +-------+
57  *      |     | OFILE |<----->| OFILE |......| OFILE |
58  *      |     +-------+       +-------+      +-------+
59  *      |
60  *      |
61  *      v
62  *  +-------+       +------+      +------+
63  *  | ODIR  |<----->| ODIR |......| ODIR |
64  *  +-------+       +------+      +------+
65  *
66  *
67  * Module Interface Overview
68  * -------------------------
69  *
70  *
71  *	    +===================================+
72  *	    |		 smbd daemon		|
73  *	    +===================================+
74  *	      |		     |		      ^
75  *	      |		     |		      |
76  * User	      |		     |		      |
77  * -----------|--------------|----------------|--------------------------------
78  * Kernel     |		     |		      |
79  *            |		     |		      |
80  *	      |		     |		      |
81  *  +=========|==============|================|=================+
82  *  |	      v		     v		      |			|
83  *  | +-----------+ +--------------------+ +------------------+ |
84  *  | |     IO    | | Kernel Door Server | | User Door Servers|	|
85  *  | | Interface | |     Interface      | |   Interface      | |
86  *  | +-----------+ +--------------------+ +------------------+ |
87  *  |		|	     |		      ^		^	|
88  *  |		v	     v		      |		|	|    +=========+
89  *  |	     +-----------------------------------+	|	|    |	       |
90  *  |	     + SMB Server Management (this file) |<------------------|	 ZFS   |
91  *  |	     +-----------------------------------+	|	|    |	       |
92  *  |							|	|    |  Module |
93  *  |	     +-----------------------------------+	|	|    |	       |
94  *  |	     +     SMB Server Internal Layers    |------+	|    +=========+
95  *  |	     +-----------------------------------+		|
96  *  |								|
97  *  |								|
98  *  +===========================================================+
99  *
100  *
101  * Server State Machine
102  * --------------------
103  *                                  |
104  *                                  | T0
105  *                                  |
106  *                                  v
107  *                    +-----------------------------+
108  *		      |   SMB_SERVER_STATE_CREATED  |
109  *		      +-----------------------------+
110  *				    |
111  *				    | T1
112  *				    |
113  *				    v
114  *		      +-----------------------------+
115  *		      | SMB_SERVER_STATE_CONFIGURED |
116  *		      +-----------------------------+
117  *				    |
118  *				    | T2
119  *				    |
120  *				    v
121  *		      +-----------------------------+
122  *		      |  SMB_SERVER_STATE_RUNNING / |
123  *		      |  SMB_SERVER_STATE_STOPPING  |
124  *		      +-----------------------------+
125  *				    |
126  *				    | T3
127  *				    |
128  *				    v
129  *		      +-----------------------------+
130  *		      |  SMB_SERVER_STATE_DELETING  |
131  *                    +-----------------------------+
132  *				    |
133  *				    |
134  *				    |
135  *				    v
136  *
137  * States
138  * ------
139  *
140  * SMB_SERVER_STATE_CREATED
141  *
142  *    This is the state of the server just after creation.
143  *
144  * SMB_SERVER_STATE_CONFIGURED
145  *
146  *    The server has been configured.
147  *
148  * SMB_SERVER_STATE_RUNNING
149  *
150  *    The server has been started. While in this state the threads listening on
151  *    the sockets car be started. The smbd daemon does so through an Ioctl:
152  *
153  *	smb_drv_ioctl(SMB_IOC_NBT_LISTEN) --> smb_server_nbt_listen()
154  *	smb_drv_ioctl(SMB_IOC_TCP_LISTEN) --> smb_server_nbt_listen()
155  *
156  *    When a client establishes a connection the thread listening leaves
157  *    temporarily the kernel. While in user space it creates a thread for the
158  *    new session. It then returns to kernel with the result of the thread
159  *    creation. If the creation failed the new session context is destroyed
160  *    before returning listening.
161  *
162  *    The new created thread enters the kernel though an Ioctl:
163  *
164  *	smb_drv_ioctl(SMB_IOC_NBT_RECEIVE) --> smb_server_nbt_receive()
165  *	smb_drv_ioctl(SMB_IOC_TCP_RECEIVE) --> smb_server_tcp_receive()
166  *
167  * SMB_SERVER_STATE_STOPPING
168  *
169  *    The threads listening on the NBT and TCP sockets are being terminated.
170  *
171  *
172  * Transitions
173  * -----------
174  *
175  * Transition T0
176  *
177  *    The daemon smbd triggers its creation by opening the smbsrv device. If
178  *    the zone where the daemon lives doesn't have an smb server yet it is
179  *    created.
180  *
181  *		smb_drv_open() --> smb_server_create()
182  *
183  * Transition T1
184  *
185  *    This transition occurs in smb_server_configure(). It is triggered by the
186  *    daemon through an Ioctl.
187  *
188  *	smb_drv_ioctl(SMB_IOC_CONFIG) --> smb_server_configure()
189  *
190  * Transition T2
191  *
192  *    This transition occurs in smb_server_start(). It is triggered by the
193  *    daemon through an Ioctl.
194  *
195  *	smb_drv_ioctl(SMB_IOC_START) --> smb_server_start()
196  *
197  * Transition T3
198  *
199  *    This transition occurs in smb_server_delete(). It is triggered by the
200  *    daemon when closing the smbsrv device
201  *
202  *		smb_drv_close() --> smb_server_delete()
203  *
204  * Comments
205  * --------
206  *
207  * This files assumes that there will one SMB server per zone. For now the
208  * smb server works only in global zone. There's nothing in this file preventing
209  * an smb server from being created in a non global zone. That limitation is
210  * enforced in user space.
211  */
212 
213 #include <sys/strsubr.h>
214 #include <sys/cmn_err.h>
215 #include <sys/priv.h>
216 #include <sys/socketvar.h>
217 #include <sys/zone.h>
218 #include <netinet/in.h>
219 #include <netinet/in_systm.h>
220 #include <netinet/ip.h>
221 #include <netinet/ip_icmp.h>
222 #include <netinet/ip_var.h>
223 #include <netinet/tcp.h>
224 #include <smbsrv/smb_kproto.h>
225 #include <smbsrv/string.h>
226 #include <smbsrv/netbios.h>
227 #include <smbsrv/smb_fsops.h>
228 #include <smbsrv/smb_share.h>
229 #include <smbsrv/smb_door.h>
230 #include <smbsrv/smb_kstat.h>
231 
232 #define	SMB_EVENT_TIMEOUT		45	/* seconds */
233 
234 extern void smb_reply_notify_change_request(smb_request_t *);
235 
236 static void smb_server_kstat_init(smb_server_t *);
237 static void smb_server_kstat_fini(smb_server_t *);
238 static void smb_server_timers(smb_thread_t *, void *);
239 static int smb_server_listen(smb_server_t *, smb_listener_daemon_t *,
240     in_port_t, int, int);
241 static void smb_server_listen_fini(smb_listener_daemon_t *);
242 static kt_did_t smb_server_listener_tid(smb_listener_daemon_t *);
243 static int smb_server_lookup(smb_server_t **);
244 static void smb_server_release(smb_server_t *);
245 static void smb_server_store_cfg(smb_server_t *, smb_ioc_cfg_t *);
246 static void smb_server_shutdown(smb_server_t *);
247 static int smb_server_fsop_start(smb_server_t *);
248 static void smb_server_fsop_stop(smb_server_t *);
249 static void smb_server_signal_listeners(smb_server_t *);
250 static void smb_event_cancel(smb_server_t *, uint32_t);
251 static void smb_event_notify(smb_server_t *, uint32_t);
252 static uint32_t smb_event_alloc_txid(void);
253 
254 static void smb_server_disconnect_share(smb_session_list_t *, const char *);
255 static void smb_server_enum_private(smb_session_list_t *, smb_svcenum_t *);
256 static int smb_server_sesion_disconnect(smb_session_list_t *, const char *,
257     const char *);
258 static int smb_server_fclose(smb_session_list_t *, uint32_t);
259 static int smb_server_kstat_update(kstat_t *, int);
260 
261 int smb_event_debug = 0;
262 
263 static smb_llist_t	smb_servers;
264 
265 /*
266  * *****************************************************************************
267  * **************** Functions called from the device interface *****************
268  * *****************************************************************************
269  *
270  * These functions typically have to determine the relevant smb server
271  * to which the call applies.
272  */
273 
274 /*
275  * smb_server_svc_init
276  *
277  * This function must be called from smb_drv_attach().
278  */
279 int
280 smb_server_svc_init(void)
281 {
282 	int	rc = 0;
283 
284 	while (rc == 0) {
285 		if (rc = smb_mbc_init())
286 			continue;
287 		if (rc = smb_vop_init())
288 			continue;
289 		if (rc = smb_node_init())
290 			continue;
291 		if (rc = smb_fem_init())
292 			continue;
293 		if (rc = smb_notify_init())
294 			continue;
295 		if (rc = smb_net_init())
296 			continue;
297 		smb_llist_init();
298 		smb_llist_constructor(&smb_servers, sizeof (smb_server_t),
299 		    offsetof(smb_server_t, sv_lnd));
300 		return (0);
301 	}
302 
303 	smb_llist_fini();
304 	smb_net_fini();
305 	smb_notify_fini();
306 	smb_fem_fini();
307 	smb_node_fini();
308 	smb_vop_fini();
309 	smb_mbc_fini();
310 	return (rc);
311 }
312 
313 /*
314  * smb_server_svc_fini
315  *
316  * This function must called from smb_drv_detach(). It will fail if servers
317  * still exist.
318  */
319 int
320 smb_server_svc_fini(void)
321 {
322 	int	rc = EBUSY;
323 
324 	if (smb_llist_get_count(&smb_servers) == 0) {
325 		smb_llist_fini();
326 		smb_net_fini();
327 		smb_notify_fini();
328 		smb_fem_fini();
329 		smb_node_fini();
330 		smb_vop_fini();
331 		smb_mbc_fini();
332 		smb_llist_destructor(&smb_servers);
333 		rc = 0;
334 	}
335 	return (rc);
336 }
337 
338 /*
339  * smb_server_create
340  *
341  * This function will fail if there's already a server associated with the
342  * caller's zone.
343  */
344 int
345 smb_server_create(void)
346 {
347 	zoneid_t	zid;
348 	smb_server_t	*sv;
349 
350 	zid = getzoneid();
351 
352 	smb_llist_enter(&smb_servers, RW_WRITER);
353 	sv = smb_llist_head(&smb_servers);
354 	while (sv) {
355 		SMB_SERVER_VALID(sv);
356 		if (sv->sv_zid == zid) {
357 			smb_llist_exit(&smb_servers);
358 			return (EPERM);
359 		}
360 		sv = smb_llist_next(&smb_servers, sv);
361 	}
362 
363 	sv = kmem_zalloc(sizeof (smb_server_t), KM_NOSLEEP);
364 	if (sv == NULL) {
365 		smb_llist_exit(&smb_servers);
366 		return (ENOMEM);
367 	}
368 
369 	smb_llist_constructor(&sv->sv_opipe_list, sizeof (smb_opipe_t),
370 	    offsetof(smb_opipe_t, p_lnd));
371 
372 	smb_llist_constructor(&sv->sv_event_list, sizeof (smb_event_t),
373 	    offsetof(smb_event_t, se_lnd));
374 
375 	smb_session_list_constructor(&sv->sv_nbt_daemon.ld_session_list);
376 	smb_session_list_constructor(&sv->sv_tcp_daemon.ld_session_list);
377 
378 	sv->si_cache_request = kmem_cache_create("smb_request_cache",
379 	    sizeof (smb_request_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
380 	sv->si_cache_session = kmem_cache_create("smb_session_cache",
381 	    sizeof (smb_session_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
382 	sv->si_cache_user = kmem_cache_create("smb_user_cache",
383 	    sizeof (smb_user_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
384 	sv->si_cache_tree = kmem_cache_create("smb_tree_cache",
385 	    sizeof (smb_tree_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
386 	sv->si_cache_ofile = kmem_cache_create("smb_ofile_cache",
387 	    sizeof (smb_ofile_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
388 	sv->si_cache_odir = kmem_cache_create("smb_odir_cache",
389 	    sizeof (smb_odir_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
390 	sv->si_cache_opipe = kmem_cache_create("smb_opipe_cache",
391 	    sizeof (smb_opipe_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
392 	sv->si_cache_event = kmem_cache_create("smb_event_cache",
393 	    sizeof (smb_event_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
394 
395 	smb_thread_init(&sv->si_thread_timers,
396 	    "smb_timers", smb_server_timers, sv,
397 	    NULL, NULL);
398 
399 	sv->sv_pid = curproc->p_pid;
400 	smb_srqueue_init(&sv->sv_srqueue);
401 
402 	smb_kdoor_init();
403 	smb_opipe_door_init();
404 	smb_server_kstat_init(sv);
405 
406 	mutex_init(&sv->sv_mutex, NULL, MUTEX_DEFAULT, NULL);
407 	cv_init(&sv->sv_cv, NULL, CV_DEFAULT, NULL);
408 	sv->sv_state = SMB_SERVER_STATE_CREATED;
409 	sv->sv_magic = SMB_SERVER_MAGIC;
410 	sv->sv_zid = zid;
411 
412 	smb_llist_insert_tail(&smb_servers, sv);
413 	smb_llist_exit(&smb_servers);
414 	return (0);
415 }
416 
417 /*
418  * smb_server_delete
419  *
420  * This function will delete the server passed in. It will make sure that all
421  * activity associated that server has ceased before destroying it.
422  */
423 int
424 smb_server_delete(void)
425 {
426 	smb_server_t	*sv;
427 	kt_did_t	nbt_tid;
428 	kt_did_t	tcp_tid;
429 	int		rc;
430 
431 	rc = smb_server_lookup(&sv);
432 	if (rc != 0)
433 		return (rc);
434 
435 	mutex_enter(&sv->sv_mutex);
436 	switch (sv->sv_state) {
437 	case SMB_SERVER_STATE_RUNNING:
438 	case SMB_SERVER_STATE_STOPPING:
439 		sv->sv_state = SMB_SERVER_STATE_STOPPING;
440 		smb_server_signal_listeners(sv);
441 		nbt_tid = smb_server_listener_tid(&sv->sv_nbt_daemon);
442 		tcp_tid = smb_server_listener_tid(&sv->sv_tcp_daemon);
443 
444 		sv->sv_state = SMB_SERVER_STATE_DELETING;
445 		mutex_exit(&sv->sv_mutex);
446 
447 		if (nbt_tid != 0)
448 			thread_join(nbt_tid);
449 		if (tcp_tid != 0)
450 			thread_join(tcp_tid);
451 
452 		smb_server_listen_fini(&sv->sv_nbt_daemon);
453 		smb_server_listen_fini(&sv->sv_tcp_daemon);
454 		mutex_enter(&sv->sv_mutex);
455 		break;
456 	case SMB_SERVER_STATE_CONFIGURED:
457 	case SMB_SERVER_STATE_CREATED:
458 		sv->sv_state = SMB_SERVER_STATE_DELETING;
459 		break;
460 	default:
461 		SMB_SERVER_STATE_VALID(sv->sv_state);
462 		mutex_exit(&sv->sv_mutex);
463 		smb_server_release(sv);
464 		return (ENOTTY);
465 	}
466 
467 	ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
468 
469 	sv->sv_refcnt--;
470 	while (sv->sv_refcnt)
471 		cv_wait(&sv->sv_cv, &sv->sv_mutex);
472 
473 	mutex_exit(&sv->sv_mutex);
474 
475 	smb_llist_enter(&smb_servers, RW_WRITER);
476 	smb_llist_remove(&smb_servers, sv);
477 	smb_llist_exit(&smb_servers);
478 
479 	smb_server_shutdown(sv);
480 	rw_destroy(&sv->sv_cfg_lock);
481 	smb_opipe_door_fini();
482 	smb_kdoor_fini();
483 	smb_server_kstat_fini(sv);
484 	smb_llist_destructor(&sv->sv_opipe_list);
485 	smb_llist_destructor(&sv->sv_event_list);
486 
487 	kmem_cache_destroy(sv->si_cache_request);
488 	kmem_cache_destroy(sv->si_cache_session);
489 	kmem_cache_destroy(sv->si_cache_user);
490 	kmem_cache_destroy(sv->si_cache_tree);
491 	kmem_cache_destroy(sv->si_cache_ofile);
492 	kmem_cache_destroy(sv->si_cache_odir);
493 	kmem_cache_destroy(sv->si_cache_opipe);
494 	kmem_cache_destroy(sv->si_cache_event);
495 
496 	smb_srqueue_destroy(&sv->sv_srqueue);
497 
498 	smb_thread_destroy(&sv->si_thread_timers);
499 	mutex_destroy(&sv->sv_mutex);
500 	cv_destroy(&sv->sv_cv);
501 	sv->sv_magic = 0;
502 	kmem_free(sv, sizeof (smb_server_t));
503 
504 	return (0);
505 }
506 
507 /*
508  * smb_server_configure
509  */
510 int
511 smb_server_configure(smb_ioc_cfg_t *ioc)
512 {
513 	int		rc = 0;
514 	smb_server_t	*sv;
515 
516 	rc = smb_server_lookup(&sv);
517 	if (rc)
518 		return (rc);
519 
520 	mutex_enter(&sv->sv_mutex);
521 	switch (sv->sv_state) {
522 	case SMB_SERVER_STATE_CREATED:
523 		smb_server_store_cfg(sv, ioc);
524 		sv->sv_state = SMB_SERVER_STATE_CONFIGURED;
525 		break;
526 
527 	case SMB_SERVER_STATE_CONFIGURED:
528 		smb_server_store_cfg(sv, ioc);
529 		break;
530 
531 	case SMB_SERVER_STATE_RUNNING:
532 	case SMB_SERVER_STATE_STOPPING:
533 		rw_enter(&sv->sv_cfg_lock, RW_WRITER);
534 		smb_server_store_cfg(sv, ioc);
535 		rw_exit(&sv->sv_cfg_lock);
536 		break;
537 
538 	default:
539 		SMB_SERVER_STATE_VALID(sv->sv_state);
540 		rc = EFAULT;
541 		break;
542 	}
543 	mutex_exit(&sv->sv_mutex);
544 
545 	smb_server_release(sv);
546 
547 	return (rc);
548 }
549 
550 /*
551  * smb_server_start
552  */
553 int
554 smb_server_start(smb_ioc_start_t *ioc)
555 {
556 	int		rc = 0;
557 	smb_server_t	*sv;
558 
559 	rc = smb_server_lookup(&sv);
560 	if (rc)
561 		return (rc);
562 
563 	mutex_enter(&sv->sv_mutex);
564 	switch (sv->sv_state) {
565 	case SMB_SERVER_STATE_CONFIGURED:
566 		smb_codepage_init();
567 
568 		sv->sv_thread_pool = taskq_create("smb_workers",
569 		    sv->sv_cfg.skc_maxworkers, SMB_WORKER_PRIORITY,
570 		    sv->sv_cfg.skc_maxworkers, INT_MAX,
571 		    TASKQ_DYNAMIC|TASKQ_PREPOPULATE);
572 
573 		sv->sv_session = smb_session_create(NULL, 0, sv, 0);
574 
575 		if (sv->sv_thread_pool == NULL || sv->sv_session == NULL) {
576 			rc = ENOMEM;
577 			break;
578 		}
579 
580 		if (rc = smb_server_fsop_start(sv))
581 			break;
582 		ASSERT(sv->sv_lmshrd == NULL);
583 		sv->sv_lmshrd = smb_kshare_door_init(ioc->lmshrd);
584 		if (sv->sv_lmshrd == NULL)
585 			break;
586 		if (rc = smb_kdoor_open(ioc->udoor)) {
587 			cmn_err(CE_WARN, "Cannot open smbd door");
588 			break;
589 		}
590 		if (rc = smb_opipe_door_open(ioc->opipe)) {
591 			cmn_err(CE_WARN, "Cannot open opipe door");
592 			break;
593 		}
594 		if (rc = smb_thread_start(&sv->si_thread_timers))
595 			break;
596 		sv->sv_state = SMB_SERVER_STATE_RUNNING;
597 		sv->sv_start_time = gethrtime();
598 		mutex_exit(&sv->sv_mutex);
599 		smb_server_release(sv);
600 
601 		smb_export_start();
602 		return (0);
603 	default:
604 		SMB_SERVER_STATE_VALID(sv->sv_state);
605 		mutex_exit(&sv->sv_mutex);
606 		smb_server_release(sv);
607 		return (ENOTTY);
608 	}
609 
610 	smb_server_shutdown(sv);
611 	mutex_exit(&sv->sv_mutex);
612 	smb_server_release(sv);
613 	return (rc);
614 }
615 
616 /*
617  * An smbd is shutting down.
618  */
619 int
620 smb_server_stop(void)
621 {
622 	smb_server_t	*sv;
623 	int		rc;
624 
625 	if ((rc = smb_server_lookup(&sv)) != 0)
626 		return (rc);
627 
628 	mutex_enter(&sv->sv_mutex);
629 	switch (sv->sv_state) {
630 	case SMB_SERVER_STATE_RUNNING:
631 		sv->sv_state = SMB_SERVER_STATE_STOPPING;
632 		smb_server_signal_listeners(sv);
633 		break;
634 	default:
635 		SMB_SERVER_STATE_VALID(sv->sv_state);
636 		break;
637 	}
638 	mutex_exit(&sv->sv_mutex);
639 
640 	smb_server_release(sv);
641 	return (0);
642 }
643 
644 boolean_t
645 smb_server_is_stopping(void)
646 {
647 	smb_server_t    *sv;
648 	boolean_t	status;
649 
650 	if (smb_server_lookup(&sv) != 0)
651 		return (B_TRUE);
652 
653 	SMB_SERVER_VALID(sv);
654 
655 	mutex_enter(&sv->sv_mutex);
656 
657 	switch (sv->sv_state) {
658 	case SMB_SERVER_STATE_STOPPING:
659 	case SMB_SERVER_STATE_DELETING:
660 		status = B_TRUE;
661 		break;
662 	default:
663 		status = B_FALSE;
664 		break;
665 	}
666 
667 	mutex_exit(&sv->sv_mutex);
668 	smb_server_release(sv);
669 	return (status);
670 }
671 
672 int
673 smb_server_cancel_event(uint32_t txid)
674 {
675 	smb_server_t	*sv;
676 	int		rc;
677 
678 	if ((rc = smb_server_lookup(&sv)) == 0) {
679 		smb_event_cancel(sv, txid);
680 		smb_server_release(sv);
681 	}
682 
683 	return (rc);
684 }
685 
686 int
687 smb_server_notify_event(smb_ioc_event_t *ioc)
688 {
689 	smb_server_t	*sv;
690 	int		rc;
691 
692 	if ((rc = smb_server_lookup(&sv)) == 0) {
693 		smb_event_notify(sv, ioc->txid);
694 		smb_server_release(sv);
695 	}
696 
697 	return (rc);
698 }
699 
700 /*
701  * SMB-over-NetBIOS (port 139)
702  *
703  * Traditional SMB service over NetBIOS, which requires that a NetBIOS
704  * session be established.
705  */
706 int
707 smb_server_nbt_listen(smb_ioc_listen_t *ioc)
708 {
709 	smb_server_t	*sv;
710 	int		rc;
711 
712 	rc = smb_server_lookup(&sv);
713 	if (rc)
714 		return (rc);
715 
716 	mutex_enter(&sv->sv_mutex);
717 	switch (sv->sv_state) {
718 	case SMB_SERVER_STATE_RUNNING:
719 		if ((sv->sv_nbt_daemon.ld_kth != NULL) &&
720 		    (sv->sv_nbt_daemon.ld_kth != curthread)) {
721 			mutex_exit(&sv->sv_mutex);
722 			smb_server_release(sv);
723 			return (EACCES);
724 		} else {
725 			sv->sv_nbt_daemon.ld_kth = curthread;
726 			sv->sv_nbt_daemon.ld_ktdid = curthread->t_did;
727 		}
728 		break;
729 	case SMB_SERVER_STATE_STOPPING:
730 		mutex_exit(&sv->sv_mutex);
731 		smb_server_release(sv);
732 		return (ECANCELED);
733 	default:
734 		SMB_SERVER_STATE_VALID(sv->sv_state);
735 		mutex_exit(&sv->sv_mutex);
736 		smb_server_release(sv);
737 		return (EFAULT);
738 	}
739 	mutex_exit(&sv->sv_mutex);
740 
741 	/*
742 	 * netbios must be ipv4
743 	 */
744 	rc = smb_server_listen(sv, &sv->sv_nbt_daemon, IPPORT_NETBIOS_SSN,
745 	    AF_INET, ioc->error);
746 
747 	mutex_enter(&sv->sv_mutex);
748 	sv->sv_nbt_daemon.ld_kth = NULL;
749 	mutex_exit(&sv->sv_mutex);
750 
751 	smb_server_release(sv);
752 	return (rc);
753 }
754 
755 /*
756  *  SMB-over-TCP (port 445)
757  */
758 int
759 smb_server_tcp_listen(smb_ioc_listen_t *ioc)
760 {
761 	smb_server_t	*sv;
762 	int		rc;
763 
764 	rc = smb_server_lookup(&sv);
765 	if (rc)
766 		return (rc);
767 
768 	mutex_enter(&sv->sv_mutex);
769 	switch (sv->sv_state) {
770 	case SMB_SERVER_STATE_RUNNING:
771 		if ((sv->sv_tcp_daemon.ld_kth != NULL) &&
772 		    (sv->sv_tcp_daemon.ld_kth != curthread)) {
773 			mutex_exit(&sv->sv_mutex);
774 			smb_server_release(sv);
775 			return (EACCES);
776 		} else {
777 			sv->sv_tcp_daemon.ld_kth = curthread;
778 			sv->sv_tcp_daemon.ld_ktdid = curthread->t_did;
779 		}
780 		break;
781 	case SMB_SERVER_STATE_STOPPING:
782 		mutex_exit(&sv->sv_mutex);
783 		smb_server_release(sv);
784 		return (ECANCELED);
785 	default:
786 		SMB_SERVER_STATE_VALID(sv->sv_state);
787 		mutex_exit(&sv->sv_mutex);
788 		smb_server_release(sv);
789 		return (EFAULT);
790 	}
791 	mutex_exit(&sv->sv_mutex);
792 
793 	if (sv->sv_cfg.skc_ipv6_enable)
794 		rc = smb_server_listen(sv, &sv->sv_tcp_daemon,
795 		    IPPORT_SMB, AF_INET6, ioc->error);
796 	else
797 		rc = smb_server_listen(sv, &sv->sv_tcp_daemon,
798 		    IPPORT_SMB, AF_INET, ioc->error);
799 
800 	mutex_enter(&sv->sv_mutex);
801 	sv->sv_tcp_daemon.ld_kth = NULL;
802 	mutex_exit(&sv->sv_mutex);
803 
804 	smb_server_release(sv);
805 	return (rc);
806 }
807 
808 /*
809  * smb_server_nbt_receive
810  */
811 int
812 smb_server_nbt_receive(void)
813 {
814 	int		rc;
815 	smb_server_t	*sv;
816 
817 	if ((rc = smb_server_lookup(&sv)) == 0) {
818 		rc = smb_session_daemon(&sv->sv_nbt_daemon.ld_session_list);
819 		smb_server_release(sv);
820 	}
821 
822 	return (rc);
823 }
824 
825 /*
826  * smb_server_tcp_receive
827  */
828 int
829 smb_server_tcp_receive(void)
830 {
831 	int		rc;
832 	smb_server_t	*sv;
833 
834 	if ((rc = smb_server_lookup(&sv)) == 0) {
835 		rc = smb_session_daemon(&sv->sv_tcp_daemon.ld_session_list);
836 		smb_server_release(sv);
837 	}
838 
839 	return (rc);
840 }
841 
842 int
843 smb_server_set_gmtoff(smb_ioc_gmt_t *ioc)
844 {
845 	int		rc;
846 	smb_server_t	*sv;
847 
848 	if ((rc = smb_server_lookup(&sv)) == 0) {
849 		sv->si_gmtoff = ioc->offset;
850 		smb_server_release(sv);
851 	}
852 
853 	return (rc);
854 }
855 
856 int
857 smb_server_numopen(smb_ioc_opennum_t *ioc)
858 {
859 	smb_server_t	*sv;
860 	int		rc;
861 
862 	if ((rc = smb_server_lookup(&sv)) == 0) {
863 		ioc->open_users = sv->sv_users;
864 		ioc->open_trees = sv->sv_trees;
865 		ioc->open_files = sv->sv_files + sv->sv_pipes;
866 		smb_server_release(sv);
867 	}
868 	return (rc);
869 }
870 
871 /*
872  * Enumerate objects within the server.  The svcenum provides the
873  * enumeration context, i.e. what the caller want to get back.
874  */
875 int
876 smb_server_enum(smb_ioc_svcenum_t *ioc)
877 {
878 	smb_svcenum_t		*svcenum = &ioc->svcenum;
879 	smb_server_t		*sv;
880 	smb_session_list_t	*se;
881 	int			rc;
882 
883 	switch (svcenum->se_type) {
884 	case SMB_SVCENUM_TYPE_USER:
885 	case SMB_SVCENUM_TYPE_TREE:
886 	case SMB_SVCENUM_TYPE_FILE:
887 		break;
888 	default:
889 		return (EINVAL);
890 	}
891 
892 	if ((rc = smb_server_lookup(&sv)) != 0)
893 		return (rc);
894 
895 	svcenum->se_bavail = svcenum->se_buflen;
896 	svcenum->se_bused = 0;
897 	svcenum->se_nitems = 0;
898 
899 	se = &sv->sv_nbt_daemon.ld_session_list;
900 	smb_server_enum_private(se, svcenum);
901 
902 	se = &sv->sv_tcp_daemon.ld_session_list;
903 	smb_server_enum_private(se, svcenum);
904 
905 	smb_server_release(sv);
906 	return (0);
907 }
908 
909 /*
910  * Look for sessions to disconnect by client and user name.
911  */
912 int
913 smb_server_session_close(smb_ioc_session_t *ioc)
914 {
915 	smb_session_list_t	*se;
916 	smb_server_t		*sv;
917 	int			nbt_cnt;
918 	int			tcp_cnt;
919 	int			rc;
920 
921 	if ((rc = smb_server_lookup(&sv)) != 0)
922 		return (rc);
923 
924 	se = &sv->sv_nbt_daemon.ld_session_list;
925 	nbt_cnt = smb_server_sesion_disconnect(se, ioc->client, ioc->username);
926 
927 	se = &sv->sv_tcp_daemon.ld_session_list;
928 	tcp_cnt = smb_server_sesion_disconnect(se, ioc->client, ioc->username);
929 
930 	smb_server_release(sv);
931 
932 	if ((nbt_cnt == 0) && (tcp_cnt == 0))
933 		return (ENOENT);
934 	return (0);
935 }
936 
937 /*
938  * Close a file by uniqid.
939  */
940 int
941 smb_server_file_close(smb_ioc_fileid_t *ioc)
942 {
943 	uint32_t		uniqid = ioc->uniqid;
944 	smb_session_list_t	*se;
945 	smb_server_t		*sv;
946 	int			rc;
947 
948 	if ((rc = smb_server_lookup(&sv)) != 0)
949 		return (rc);
950 
951 	se = &sv->sv_nbt_daemon.ld_session_list;
952 	rc = smb_server_fclose(se, uniqid);
953 
954 	if (rc == ENOENT) {
955 		se = &sv->sv_tcp_daemon.ld_session_list;
956 		rc = smb_server_fclose(se, uniqid);
957 	}
958 
959 	smb_server_release(sv);
960 	return (rc);
961 }
962 
963 /*
964  * These functions determine the relevant smb server to which the call apply.
965  */
966 
967 uint32_t
968 smb_server_get_session_count(void)
969 {
970 	smb_server_t	*sv;
971 	uint32_t	counter = 0;
972 
973 	if (smb_server_lookup(&sv))
974 		return (0);
975 
976 	rw_enter(&sv->sv_nbt_daemon.ld_session_list.se_lock, RW_READER);
977 	counter = sv->sv_nbt_daemon.ld_session_list.se_act.count;
978 	rw_exit(&sv->sv_nbt_daemon.ld_session_list.se_lock);
979 	rw_enter(&sv->sv_tcp_daemon.ld_session_list.se_lock, RW_READER);
980 	counter += sv->sv_tcp_daemon.ld_session_list.se_act.count;
981 	rw_exit(&sv->sv_tcp_daemon.ld_session_list.se_lock);
982 
983 	smb_server_release(sv);
984 
985 	return (counter);
986 }
987 
988 /*
989  * Gets the vnode of the specified share path.
990  *
991  * A hold on the returned vnode pointer is taken so the caller
992  * must call VN_RELE.
993  */
994 int
995 smb_server_sharevp(const char *shr_path, vnode_t **vp)
996 {
997 	smb_server_t	*sv;
998 	smb_request_t	*sr;
999 	smb_node_t	*fnode = NULL;
1000 	smb_node_t	*dnode;
1001 	char		last_comp[MAXNAMELEN];
1002 	int		rc = 0;
1003 
1004 	ASSERT(shr_path);
1005 
1006 	if ((rc = smb_server_lookup(&sv)))
1007 		return (rc);
1008 
1009 	mutex_enter(&sv->sv_mutex);
1010 	switch (sv->sv_state) {
1011 	case SMB_SERVER_STATE_RUNNING:
1012 		break;
1013 	default:
1014 		mutex_exit(&sv->sv_mutex);
1015 		smb_server_release(sv);
1016 		return (ENOTACTIVE);
1017 	}
1018 	mutex_exit(&sv->sv_mutex);
1019 
1020 	if ((sr = smb_request_alloc(sv->sv_session, 0)) == NULL) {
1021 		smb_server_release(sv);
1022 		return (ENOMEM);
1023 	}
1024 	sr->user_cr = kcred;
1025 
1026 	rc = smb_pathname_reduce(sr, sr->user_cr, shr_path,
1027 	    NULL, NULL, &dnode, last_comp);
1028 
1029 	if (rc == 0) {
1030 		rc = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS,
1031 		    sv->si_root_smb_node, dnode, last_comp, &fnode);
1032 		smb_node_release(dnode);
1033 	}
1034 
1035 	smb_request_free(sr);
1036 	smb_server_release(sv);
1037 
1038 	if (rc != 0)
1039 		return (rc);
1040 
1041 	ASSERT(fnode->vp && fnode->vp->v_vfsp);
1042 
1043 	VN_HOLD(fnode->vp);
1044 	*vp = fnode->vp;
1045 
1046 	smb_node_release(fnode);
1047 
1048 	return (0);
1049 }
1050 
1051 
1052 /*
1053  * This is a special interface that will be utilized by ZFS to cause a share to
1054  * be added/removed.
1055  *
1056  * arg is either a lmshare_info_t or share_name from userspace.
1057  * It will need to be copied into the kernel.   It is lmshare_info_t
1058  * for add operations and share_name for delete operations.
1059  */
1060 int
1061 smb_server_share(void *arg, boolean_t add_share)
1062 {
1063 	smb_server_t	*sv;
1064 	int		rc;
1065 
1066 	if ((rc = smb_server_lookup(&sv)) == 0) {
1067 		mutex_enter(&sv->sv_mutex);
1068 		switch (sv->sv_state) {
1069 		case SMB_SERVER_STATE_RUNNING:
1070 			mutex_exit(&sv->sv_mutex);
1071 			(void) smb_kshare_upcall(sv->sv_lmshrd, arg, add_share);
1072 			break;
1073 		default:
1074 			mutex_exit(&sv->sv_mutex);
1075 			break;
1076 		}
1077 		smb_server_release(sv);
1078 	}
1079 
1080 	return (rc);
1081 }
1082 
1083 int
1084 smb_server_unshare(const char *sharename)
1085 {
1086 	smb_server_t		*sv;
1087 	smb_session_list_t	*slist;
1088 	int			rc;
1089 
1090 	if ((rc = smb_server_lookup(&sv)))
1091 		return (rc);
1092 
1093 	mutex_enter(&sv->sv_mutex);
1094 	switch (sv->sv_state) {
1095 	case SMB_SERVER_STATE_RUNNING:
1096 	case SMB_SERVER_STATE_STOPPING:
1097 		break;
1098 	default:
1099 		mutex_exit(&sv->sv_mutex);
1100 		smb_server_release(sv);
1101 		return (ENOTACTIVE);
1102 	}
1103 	mutex_exit(&sv->sv_mutex);
1104 
1105 	slist = &sv->sv_nbt_daemon.ld_session_list;
1106 	smb_server_disconnect_share(slist, sharename);
1107 
1108 	slist = &sv->sv_tcp_daemon.ld_session_list;
1109 	smb_server_disconnect_share(slist, sharename);
1110 
1111 	smb_server_release(sv);
1112 	return (0);
1113 }
1114 
1115 /*
1116  * Disconnect the specified share.
1117  * Typically called when a share has been removed.
1118  */
1119 static void
1120 smb_server_disconnect_share(smb_session_list_t *slist, const char *sharename)
1121 {
1122 	smb_session_t		*session;
1123 
1124 	rw_enter(&slist->se_lock, RW_READER);
1125 
1126 	session = list_head(&slist->se_act.lst);
1127 	while (session) {
1128 		ASSERT(session->s_magic == SMB_SESSION_MAGIC);
1129 		smb_rwx_rwenter(&session->s_lock, RW_READER);
1130 		switch (session->s_state) {
1131 		case SMB_SESSION_STATE_NEGOTIATED:
1132 		case SMB_SESSION_STATE_OPLOCK_BREAKING:
1133 		case SMB_SESSION_STATE_WRITE_RAW_ACTIVE:
1134 			smb_session_disconnect_share(session, sharename);
1135 			break;
1136 		default:
1137 			break;
1138 		}
1139 		smb_rwx_rwexit(&session->s_lock);
1140 		session = list_next(&slist->se_act.lst, session);
1141 	}
1142 
1143 	rw_exit(&slist->se_lock);
1144 }
1145 
1146 /*
1147  * *****************************************************************************
1148  * **************** Functions called from the internal layers ******************
1149  * *****************************************************************************
1150  *
1151  * These functions are provided the relevant smb server by the caller.
1152  */
1153 
1154 void
1155 smb_server_reconnection_check(smb_server_t *sv, smb_session_t *session)
1156 {
1157 	ASSERT(sv == session->s_server);
1158 
1159 	smb_session_reconnection_check(&sv->sv_nbt_daemon.ld_session_list,
1160 	    session);
1161 	smb_session_reconnection_check(&sv->sv_tcp_daemon.ld_session_list,
1162 	    session);
1163 }
1164 
1165 void
1166 smb_server_get_cfg(smb_server_t *sv, smb_kmod_cfg_t *cfg)
1167 {
1168 	rw_enter(&sv->sv_cfg_lock, RW_READER);
1169 	bcopy(&sv->sv_cfg, cfg, sizeof (*cfg));
1170 	rw_exit(&sv->sv_cfg_lock);
1171 }
1172 
1173 /*
1174  *
1175  */
1176 void
1177 smb_server_inc_nbt_sess(smb_server_t *sv)
1178 {
1179 	SMB_SERVER_VALID(sv);
1180 	atomic_inc_32(&sv->sv_nbt_sess);
1181 }
1182 
1183 void
1184 smb_server_dec_nbt_sess(smb_server_t *sv)
1185 {
1186 	SMB_SERVER_VALID(sv);
1187 	atomic_dec_32(&sv->sv_nbt_sess);
1188 }
1189 
1190 void
1191 smb_server_inc_tcp_sess(smb_server_t *sv)
1192 {
1193 	SMB_SERVER_VALID(sv);
1194 	atomic_inc_32(&sv->sv_tcp_sess);
1195 }
1196 
1197 void
1198 smb_server_dec_tcp_sess(smb_server_t *sv)
1199 {
1200 	SMB_SERVER_VALID(sv);
1201 	atomic_dec_32(&sv->sv_tcp_sess);
1202 }
1203 
1204 void
1205 smb_server_inc_users(smb_server_t *sv)
1206 {
1207 	SMB_SERVER_VALID(sv);
1208 	atomic_inc_32(&sv->sv_users);
1209 }
1210 
1211 void
1212 smb_server_dec_users(smb_server_t *sv)
1213 {
1214 	SMB_SERVER_VALID(sv);
1215 	atomic_dec_32(&sv->sv_users);
1216 }
1217 
1218 void
1219 smb_server_inc_trees(smb_server_t *sv)
1220 {
1221 	SMB_SERVER_VALID(sv);
1222 	atomic_inc_32(&sv->sv_trees);
1223 }
1224 
1225 void
1226 smb_server_dec_trees(smb_server_t *sv)
1227 {
1228 	SMB_SERVER_VALID(sv);
1229 	atomic_dec_32(&sv->sv_trees);
1230 }
1231 
1232 void
1233 smb_server_inc_files(smb_server_t *sv)
1234 {
1235 	SMB_SERVER_VALID(sv);
1236 	atomic_inc_32(&sv->sv_files);
1237 }
1238 
1239 void
1240 smb_server_dec_files(smb_server_t *sv)
1241 {
1242 	SMB_SERVER_VALID(sv);
1243 	atomic_dec_32(&sv->sv_files);
1244 }
1245 
1246 void
1247 smb_server_inc_pipes(smb_server_t *sv)
1248 {
1249 	SMB_SERVER_VALID(sv);
1250 	atomic_inc_32(&sv->sv_pipes);
1251 }
1252 
1253 void
1254 smb_server_dec_pipes(smb_server_t *sv)
1255 {
1256 	SMB_SERVER_VALID(sv);
1257 	atomic_dec_32(&sv->sv_pipes);
1258 }
1259 
1260 void
1261 smb_server_add_rxb(smb_server_t *sv, int64_t value)
1262 {
1263 	SMB_SERVER_VALID(sv);
1264 	atomic_add_64(&sv->sv_rxb, value);
1265 }
1266 
1267 void
1268 smb_server_add_txb(smb_server_t *sv, int64_t value)
1269 {
1270 	SMB_SERVER_VALID(sv);
1271 	atomic_add_64(&sv->sv_txb, value);
1272 }
1273 
1274 void
1275 smb_server_inc_req(smb_server_t *sv)
1276 {
1277 	SMB_SERVER_VALID(sv);
1278 	atomic_inc_64(&sv->sv_nreq);
1279 }
1280 
1281 /*
1282  * *****************************************************************************
1283  * *************************** Static Functions ********************************
1284  * *****************************************************************************
1285  */
1286 
1287 static void
1288 smb_server_timers(smb_thread_t *thread, void *arg)
1289 {
1290 	smb_server_t	*sv = (smb_server_t *)arg;
1291 
1292 	ASSERT(sv != NULL);
1293 
1294 	while (smb_thread_continue_timedwait(thread, 1 /* Seconds */)) {
1295 		smb_session_timers(&sv->sv_nbt_daemon.ld_session_list);
1296 		smb_session_timers(&sv->sv_tcp_daemon.ld_session_list);
1297 	}
1298 }
1299 
1300 /*
1301  * smb_server_kstat_init
1302  */
1303 static void
1304 smb_server_kstat_init(smb_server_t *sv)
1305 {
1306 	sv->sv_ksp = kstat_create_zone(SMBSRV_KSTAT_MODULE, sv->sv_zid,
1307 	    SMBSRV_KSTAT_STATISTICS, SMBSRV_KSTAT_CLASS, KSTAT_TYPE_RAW,
1308 	    sizeof (smbsrv_kstats_t), 0, sv->sv_zid);
1309 
1310 	if (sv->sv_ksp != NULL) {
1311 		sv->sv_ksp->ks_update = smb_server_kstat_update;
1312 		sv->sv_ksp->ks_private = sv;
1313 		((smbsrv_kstats_t *)sv->sv_ksp->ks_data)->ks_start_time =
1314 		    sv->sv_start_time;
1315 		smb_dispatch_stats_init(
1316 		    ((smbsrv_kstats_t *)sv->sv_ksp->ks_data)->ks_reqs);
1317 		kstat_install(sv->sv_ksp);
1318 	} else {
1319 		cmn_err(CE_WARN, "SMB Server: Statistics unavailable");
1320 	}
1321 }
1322 
1323 /*
1324  * smb_server_kstat_fini
1325  */
1326 static void
1327 smb_server_kstat_fini(smb_server_t *sv)
1328 {
1329 	if (sv->sv_ksp != NULL) {
1330 		kstat_delete(sv->sv_ksp);
1331 		sv->sv_ksp = NULL;
1332 		smb_dispatch_stats_fini();
1333 	}
1334 }
1335 
1336 /*
1337  * smb_server_kstat_update
1338  */
1339 static int
1340 smb_server_kstat_update(kstat_t *ksp, int rw)
1341 {
1342 	smb_server_t	*sv;
1343 	smbsrv_kstats_t	*ksd;
1344 
1345 	if (rw == KSTAT_READ) {
1346 		sv = ksp->ks_private;
1347 		SMB_SERVER_VALID(sv);
1348 		ksd = (smbsrv_kstats_t *)ksp->ks_data;
1349 		/*
1350 		 * Counters
1351 		 */
1352 		ksd->ks_nbt_sess = sv->sv_nbt_sess;
1353 		ksd->ks_tcp_sess = sv->sv_tcp_sess;
1354 		ksd->ks_users = sv->sv_users;
1355 		ksd->ks_trees = sv->sv_trees;
1356 		ksd->ks_files = sv->sv_files;
1357 		ksd->ks_pipes = sv->sv_pipes;
1358 		/*
1359 		 * Throughput
1360 		 */
1361 		ksd->ks_txb = sv->sv_txb;
1362 		ksd->ks_rxb = sv->sv_rxb;
1363 		ksd->ks_nreq = sv->sv_nreq;
1364 		/*
1365 		 * Busyness
1366 		 */
1367 		ksd->ks_maxreqs = sv->sv_cfg.skc_maxworkers;
1368 		smb_srqueue_update(&sv->sv_srqueue,
1369 		    &ksd->ks_utilization);
1370 		/*
1371 		 * Latency & Throughput of the requests
1372 		 */
1373 		smb_dispatch_stats_update(ksd->ks_reqs, 0, SMB_COM_NUM);
1374 		return (0);
1375 	}
1376 	if (rw == KSTAT_WRITE)
1377 		return (EACCES);
1378 
1379 	return (EIO);
1380 }
1381 
1382 /*
1383  * The mutex of the server must have been entered before calling this function.
1384  */
1385 static void
1386 smb_server_shutdown(smb_server_t *sv)
1387 {
1388 	SMB_SERVER_VALID(sv);
1389 
1390 	smb_opipe_door_close();
1391 	smb_thread_stop(&sv->si_thread_timers);
1392 	smb_kdoor_close();
1393 	smb_kshare_door_fini(sv->sv_lmshrd);
1394 	sv->sv_lmshrd = NULL;
1395 	smb_export_stop();
1396 	smb_server_fsop_stop(sv);
1397 
1398 	if (sv->sv_session) {
1399 		smb_session_delete(sv->sv_session);
1400 		sv->sv_session = NULL;
1401 	}
1402 
1403 	if (sv->sv_thread_pool) {
1404 		taskq_destroy(sv->sv_thread_pool);
1405 		sv->sv_thread_pool = NULL;
1406 	}
1407 }
1408 
1409 static int
1410 smb_server_listen(
1411     smb_server_t		*sv,
1412     smb_listener_daemon_t	*ld,
1413     in_port_t			port,
1414     int				family,
1415     int				pthread_create_error)
1416 {
1417 	int			rc = 0;
1418 	ksocket_t		s_so;
1419 	uint32_t		on;
1420 	uint32_t		off;
1421 	uint32_t		txbuf_size;
1422 	smb_session_t		*session;
1423 
1424 	if (pthread_create_error) {
1425 		/*
1426 		 * Delete the last session created. The user space thread
1427 		 * creation failed.
1428 		 */
1429 		smb_session_list_delete_tail(&ld->ld_session_list);
1430 	}
1431 
1432 	if (ld->ld_so == NULL) {
1433 		/* First time listener */
1434 		if (family == AF_INET) {
1435 			ld->ld_sin.sin_family = (uint32_t)family;
1436 			ld->ld_sin.sin_port = htons(port);
1437 			ld->ld_sin.sin_addr.s_addr = htonl(INADDR_ANY);
1438 		} else {
1439 			ld->ld_sin6.sin6_family = (uint32_t)family;
1440 			ld->ld_sin6.sin6_port = htons(port);
1441 			(void) memset(&ld->ld_sin6.sin6_addr.s6_addr, 0,
1442 			    sizeof (ld->ld_sin6.sin6_addr.s6_addr));
1443 		}
1444 
1445 		ld->ld_so = smb_socreate(family, SOCK_STREAM, 0);
1446 		if (ld->ld_so == NULL) {
1447 			cmn_err(CE_WARN, "port %d: socket create failed", port);
1448 			return (ENOMEM);
1449 		}
1450 
1451 		off = 0;
1452 		(void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET,
1453 		    SO_MAC_EXEMPT, &off, sizeof (off), CRED());
1454 
1455 		on = 1;
1456 		(void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET,
1457 		    SO_REUSEADDR, &on, sizeof (on), CRED());
1458 
1459 		if (family == AF_INET) {
1460 			rc = ksocket_bind(ld->ld_so,
1461 			    (struct sockaddr *)&ld->ld_sin,
1462 			    sizeof (ld->ld_sin), CRED());
1463 		} else {
1464 			rc = ksocket_bind(ld->ld_so,
1465 			    (struct sockaddr *)&ld->ld_sin6,
1466 			    sizeof (ld->ld_sin6), CRED());
1467 		}
1468 
1469 		if (rc != 0) {
1470 			cmn_err(CE_WARN, "port %d: bind failed (%d)", port, rc);
1471 			smb_server_listen_fini(ld);
1472 			return (rc);
1473 		}
1474 
1475 		rc =  ksocket_listen(ld->ld_so, 20, CRED());
1476 		if (rc < 0) {
1477 			cmn_err(CE_WARN, "port %d: listen failed", port);
1478 			smb_server_listen_fini(ld);
1479 			return (rc);
1480 		}
1481 	}
1482 
1483 	DTRACE_PROBE1(so__wait__accept, struct sonode *, ld->ld_so);
1484 
1485 	for (;;) {
1486 		if (smb_server_is_stopping()) {
1487 			rc = ECANCELED;
1488 			break;
1489 		}
1490 
1491 		rc = ksocket_accept(ld->ld_so, NULL, NULL, &s_so, CRED());
1492 		if (rc != 0)
1493 			break;
1494 
1495 		if (smb_server_is_stopping()) {
1496 			smb_soshutdown(s_so);
1497 			smb_sodestroy(s_so);
1498 			rc = ECANCELED;
1499 			break;
1500 		}
1501 
1502 		DTRACE_PROBE1(so__accept, struct sonode *, s_so);
1503 
1504 		on = 1;
1505 		(void) ksocket_setsockopt(s_so, IPPROTO_TCP, TCP_NODELAY,
1506 		    &on, sizeof (on), CRED());
1507 
1508 		on = 1;
1509 		(void) ksocket_setsockopt(s_so, SOL_SOCKET, SO_KEEPALIVE,
1510 		    &on, sizeof (on), CRED());
1511 
1512 		txbuf_size = 128*1024;
1513 		(void) ksocket_setsockopt(s_so, SOL_SOCKET, SO_SNDBUF,
1514 		    (const void *)&txbuf_size, sizeof (txbuf_size), CRED());
1515 
1516 		/*
1517 		 * Create a session for this connection.
1518 		 */
1519 		session = smb_session_create(s_so, port, sv, family);
1520 		if (session) {
1521 			smb_session_list_append(&ld->ld_session_list, session);
1522 			rc = 0;
1523 			break;
1524 		} else {
1525 			smb_soshutdown(s_so);
1526 			smb_sodestroy(s_so);
1527 		}
1528 	}
1529 
1530 	if (rc != 0)
1531 		smb_server_listen_fini(ld);
1532 
1533 	return (rc);
1534 }
1535 
1536 static void
1537 smb_server_listen_fini(smb_listener_daemon_t *ld)
1538 {
1539 	if (ld->ld_so != NULL) {
1540 		smb_session_list_signal(&ld->ld_session_list);
1541 		smb_soshutdown(ld->ld_so);
1542 		smb_sodestroy(ld->ld_so);
1543 		ld->ld_so = NULL;
1544 	}
1545 }
1546 
1547 static kt_did_t
1548 smb_server_listener_tid(smb_listener_daemon_t *ld)
1549 {
1550 	kt_did_t	tid;
1551 
1552 	if (ld->ld_ktdid != 0) {
1553 		tid = ld->ld_ktdid;
1554 		ld->ld_ktdid = 0;
1555 	}
1556 
1557 	return (tid);
1558 }
1559 
1560 /*
1561  * smb_server_lookup
1562  *
1563  * This function tries to find the server associated with the zone of the
1564  * caller.
1565  */
1566 static int
1567 smb_server_lookup(smb_server_t **psv)
1568 {
1569 	zoneid_t	zid;
1570 	smb_server_t	*sv;
1571 
1572 	zid = getzoneid();
1573 
1574 	smb_llist_enter(&smb_servers, RW_READER);
1575 	sv = smb_llist_head(&smb_servers);
1576 	while (sv) {
1577 		SMB_SERVER_VALID(sv);
1578 		if (sv->sv_zid == zid) {
1579 			mutex_enter(&sv->sv_mutex);
1580 			if (sv->sv_state != SMB_SERVER_STATE_DELETING) {
1581 				sv->sv_refcnt++;
1582 				mutex_exit(&sv->sv_mutex);
1583 				smb_llist_exit(&smb_servers);
1584 				*psv = sv;
1585 				return (0);
1586 			}
1587 			mutex_exit(&sv->sv_mutex);
1588 			break;
1589 		}
1590 		sv = smb_llist_next(&smb_servers, sv);
1591 	}
1592 	smb_llist_exit(&smb_servers);
1593 	return (EPERM);
1594 }
1595 
1596 /*
1597  * smb_server_release
1598  *
1599  * This function decrements the reference count of the server and signals its
1600  * condition variable if the state of the server is SMB_SERVER_STATE_DELETING.
1601  */
1602 static void
1603 smb_server_release(smb_server_t *sv)
1604 {
1605 	SMB_SERVER_VALID(sv);
1606 
1607 	mutex_enter(&sv->sv_mutex);
1608 	ASSERT(sv->sv_refcnt);
1609 	sv->sv_refcnt--;
1610 	if ((sv->sv_refcnt == 0) && (sv->sv_state == SMB_SERVER_STATE_DELETING))
1611 		cv_signal(&sv->sv_cv);
1612 	mutex_exit(&sv->sv_mutex);
1613 }
1614 
1615 /*
1616  * Enumerate the users associated with a session list.
1617  */
1618 static void
1619 smb_server_enum_private(smb_session_list_t *se, smb_svcenum_t *svcenum)
1620 {
1621 	smb_session_t	*sn;
1622 	smb_llist_t	*ulist;
1623 	smb_user_t	*user;
1624 	int		rc = 0;
1625 
1626 	rw_enter(&se->se_lock, RW_READER);
1627 	sn = list_head(&se->se_act.lst);
1628 
1629 	while (sn != NULL) {
1630 		ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
1631 		ulist = &sn->s_user_list;
1632 		smb_llist_enter(ulist, RW_READER);
1633 		user = smb_llist_head(ulist);
1634 
1635 		while (user != NULL) {
1636 			if (smb_user_hold(user)) {
1637 				rc = smb_user_enum(user, svcenum);
1638 				smb_user_release(user);
1639 			}
1640 
1641 			user = smb_llist_next(ulist, user);
1642 		}
1643 
1644 		smb_llist_exit(ulist);
1645 
1646 		if (rc != 0)
1647 			break;
1648 
1649 		sn = list_next(&se->se_act.lst, sn);
1650 	}
1651 
1652 	rw_exit(&se->se_lock);
1653 }
1654 
1655 /*
1656  * Disconnect sessions associated with the specified client and username.
1657  * Empty strings are treated as wildcards.
1658  */
1659 static int
1660 smb_server_sesion_disconnect(smb_session_list_t *se,
1661     const char *client, const char *name)
1662 {
1663 	smb_session_t	*sn;
1664 	smb_llist_t	*ulist;
1665 	smb_user_t	*user;
1666 	boolean_t	match;
1667 	int		count = 0;
1668 
1669 	rw_enter(&se->se_lock, RW_READER);
1670 	sn = list_head(&se->se_act.lst);
1671 
1672 	while (sn != NULL) {
1673 		ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
1674 
1675 		if ((*client != '\0') && (!smb_session_isclient(sn, client))) {
1676 			sn = list_next(&se->se_act.lst, sn);
1677 			continue;
1678 		}
1679 
1680 		ulist = &sn->s_user_list;
1681 		smb_llist_enter(ulist, RW_READER);
1682 		user = smb_llist_head(ulist);
1683 
1684 		while (user != NULL) {
1685 			if (smb_user_hold(user)) {
1686 				match = (*name == '\0');
1687 				if (!match)
1688 					match = smb_user_namecmp(user, name);
1689 
1690 				if (match) {
1691 					smb_llist_exit(ulist);
1692 					smb_user_logoff(user);
1693 					++count;
1694 					smb_user_release(user);
1695 					smb_llist_enter(ulist, RW_READER);
1696 					user = smb_llist_head(ulist);
1697 					continue;
1698 				}
1699 
1700 				smb_user_release(user);
1701 			}
1702 
1703 			user = smb_llist_next(ulist, user);
1704 		}
1705 
1706 		smb_llist_exit(ulist);
1707 		sn = list_next(&se->se_act.lst, sn);
1708 	}
1709 
1710 	rw_exit(&se->se_lock);
1711 	return (count);
1712 }
1713 
1714 /*
1715  * Close a file by its unique id.
1716  */
1717 static int
1718 smb_server_fclose(smb_session_list_t *se, uint32_t uniqid)
1719 {
1720 	smb_session_t	*sn;
1721 	smb_llist_t	*ulist;
1722 	smb_user_t	*user;
1723 	int		rc = ENOENT;
1724 
1725 	rw_enter(&se->se_lock, RW_READER);
1726 	sn = list_head(&se->se_act.lst);
1727 
1728 	while ((sn != NULL) && (rc == ENOENT)) {
1729 		ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
1730 		ulist = &sn->s_user_list;
1731 		smb_llist_enter(ulist, RW_READER);
1732 		user = smb_llist_head(ulist);
1733 
1734 		while ((user != NULL) && (rc == ENOENT)) {
1735 			if (smb_user_hold(user)) {
1736 				rc = smb_user_fclose(user, uniqid);
1737 				smb_user_release(user);
1738 			}
1739 
1740 			user = smb_llist_next(ulist, user);
1741 		}
1742 
1743 		smb_llist_exit(ulist);
1744 		sn = list_next(&se->se_act.lst, sn);
1745 	}
1746 
1747 	rw_exit(&se->se_lock);
1748 	return (rc);
1749 }
1750 
1751 static void
1752 smb_server_store_cfg(smb_server_t *sv, smb_ioc_cfg_t *ioc)
1753 {
1754 	if (ioc->maxconnections == 0)
1755 		ioc->maxconnections = 0xFFFFFFFF;
1756 
1757 	smb_session_correct_keep_alive_values(
1758 	    &sv->sv_nbt_daemon.ld_session_list, ioc->keepalive);
1759 	smb_session_correct_keep_alive_values(
1760 	    &sv->sv_tcp_daemon.ld_session_list, ioc->keepalive);
1761 
1762 	sv->sv_cfg.skc_maxworkers = ioc->maxworkers;
1763 	sv->sv_cfg.skc_maxconnections = ioc->maxconnections;
1764 	sv->sv_cfg.skc_keepalive = ioc->keepalive;
1765 	sv->sv_cfg.skc_restrict_anon = ioc->restrict_anon;
1766 	sv->sv_cfg.skc_signing_enable = ioc->signing_enable;
1767 	sv->sv_cfg.skc_signing_required = ioc->signing_required;
1768 	sv->sv_cfg.skc_oplock_enable = ioc->oplock_enable;
1769 	sv->sv_cfg.skc_sync_enable = ioc->sync_enable;
1770 	sv->sv_cfg.skc_secmode = ioc->secmode;
1771 	sv->sv_cfg.skc_ipv6_enable = ioc->ipv6_enable;
1772 	sv->sv_cfg.skc_execflags = ioc->exec_flags;
1773 	sv->sv_cfg.skc_version = ioc->version;
1774 	(void) strlcpy(sv->sv_cfg.skc_nbdomain, ioc->nbdomain,
1775 	    sizeof (sv->sv_cfg.skc_nbdomain));
1776 	(void) strlcpy(sv->sv_cfg.skc_fqdn, ioc->fqdn,
1777 	    sizeof (sv->sv_cfg.skc_fqdn));
1778 	(void) strlcpy(sv->sv_cfg.skc_hostname, ioc->hostname,
1779 	    sizeof (sv->sv_cfg.skc_hostname));
1780 	(void) strlcpy(sv->sv_cfg.skc_system_comment, ioc->system_comment,
1781 	    sizeof (sv->sv_cfg.skc_system_comment));
1782 }
1783 
1784 static int
1785 smb_server_fsop_start(smb_server_t *sv)
1786 {
1787 	int	error;
1788 
1789 	error = smb_node_root_init(rootdir, sv, &sv->si_root_smb_node);
1790 	if (error != 0)
1791 		sv->si_root_smb_node = NULL;
1792 
1793 	return (error);
1794 }
1795 
1796 static void
1797 smb_server_fsop_stop(smb_server_t *sv)
1798 {
1799 	if (sv->si_root_smb_node != NULL) {
1800 		smb_node_release(sv->si_root_smb_node);
1801 		sv->si_root_smb_node = NULL;
1802 	}
1803 }
1804 
1805 static void
1806 smb_server_signal_listeners(smb_server_t *sv)
1807 {
1808 	SMB_SERVER_VALID(sv);
1809 	ASSERT(sv->sv_state == SMB_SERVER_STATE_STOPPING);
1810 	ASSERT(MUTEX_HELD(&sv->sv_mutex));
1811 
1812 	smb_event_cancel(sv, 0);
1813 
1814 	if (sv->sv_nbt_daemon.ld_kth != NULL) {
1815 		tsignal(sv->sv_nbt_daemon.ld_kth, SIGINT);
1816 		sv->sv_nbt_daemon.ld_kth = NULL;
1817 	}
1818 
1819 	if (sv->sv_tcp_daemon.ld_kth != NULL) {
1820 		tsignal(sv->sv_tcp_daemon.ld_kth, SIGINT);
1821 		sv->sv_tcp_daemon.ld_kth = NULL;
1822 	}
1823 }
1824 
1825 smb_event_t *
1826 smb_event_create(void)
1827 {
1828 	smb_server_t	*sv;
1829 	smb_event_t	*event;
1830 
1831 	if (smb_server_is_stopping())
1832 		return (NULL);
1833 
1834 	if (smb_server_lookup(&sv) != 0) {
1835 		cmn_err(CE_NOTE, "smb_event_create failed");
1836 		return (NULL);
1837 	}
1838 
1839 	event = kmem_cache_alloc(sv->si_cache_event, KM_SLEEP);
1840 
1841 	bzero(event, sizeof (smb_event_t));
1842 	mutex_init(&event->se_mutex, NULL, MUTEX_DEFAULT, NULL);
1843 	cv_init(&event->se_cv, NULL, CV_DEFAULT, NULL);
1844 	event->se_magic = SMB_EVENT_MAGIC;
1845 	event->se_txid = smb_event_alloc_txid();
1846 	event->se_server = sv;
1847 
1848 	smb_llist_enter(&sv->sv_event_list, RW_WRITER);
1849 	smb_llist_insert_tail(&sv->sv_event_list, event);
1850 	smb_llist_exit(&sv->sv_event_list);
1851 
1852 	smb_server_release(sv);
1853 	return (event);
1854 }
1855 
1856 void
1857 smb_event_destroy(smb_event_t *event)
1858 {
1859 	smb_server_t	*sv;
1860 
1861 	if (event == NULL)
1862 		return;
1863 
1864 	SMB_EVENT_VALID(event);
1865 	ASSERT(event->se_waittime == 0);
1866 
1867 	if (smb_server_lookup(&sv) != 0)
1868 		return;
1869 
1870 	smb_llist_enter(&sv->sv_event_list, RW_WRITER);
1871 	smb_llist_remove(&sv->sv_event_list, event);
1872 	smb_llist_exit(&sv->sv_event_list);
1873 
1874 	event->se_magic = (uint32_t)~SMB_EVENT_MAGIC;
1875 	cv_destroy(&event->se_cv);
1876 	mutex_destroy(&event->se_mutex);
1877 
1878 	kmem_cache_free(sv->si_cache_event, event);
1879 	smb_server_release(sv);
1880 }
1881 
1882 /*
1883  * Get the txid for the specified event.
1884  */
1885 uint32_t
1886 smb_event_txid(smb_event_t *event)
1887 {
1888 	if (event != NULL) {
1889 		SMB_EVENT_VALID(event);
1890 		return (event->se_txid);
1891 	}
1892 
1893 	cmn_err(CE_NOTE, "smb_event_txid failed");
1894 	return ((uint32_t)-1);
1895 }
1896 
1897 /*
1898  * Wait for event notification.
1899  */
1900 int
1901 smb_event_wait(smb_event_t *event)
1902 {
1903 	int	seconds = 1;
1904 	int	ticks;
1905 
1906 	if (event == NULL)
1907 		return (EINVAL);
1908 
1909 	SMB_EVENT_VALID(event);
1910 
1911 	mutex_enter(&event->se_mutex);
1912 	event->se_waittime = 1;
1913 	event->se_errno = 0;
1914 
1915 	while (!(event->se_notified)) {
1916 		if (smb_event_debug && ((event->se_waittime % 30) == 0))
1917 			cmn_err(CE_NOTE, "smb_event_wait[%d] (%d sec)",
1918 			    event->se_txid, event->se_waittime);
1919 
1920 		if (event->se_errno != 0)
1921 			break;
1922 
1923 		if (event->se_waittime > SMB_EVENT_TIMEOUT) {
1924 			event->se_errno = ETIME;
1925 			break;
1926 		}
1927 
1928 		ticks = SEC_TO_TICK(seconds);
1929 		(void) cv_reltimedwait(&event->se_cv,
1930 		    &event->se_mutex, (clock_t)ticks, TR_CLOCK_TICK);
1931 		++event->se_waittime;
1932 	}
1933 
1934 	event->se_waittime = 0;
1935 	event->se_notified = B_FALSE;
1936 	cv_signal(&event->se_cv);
1937 	mutex_exit(&event->se_mutex);
1938 	return (event->se_errno);
1939 }
1940 
1941 /*
1942  * If txid is non-zero, cancel the specified event.
1943  * Otherwise, cancel all events.
1944  */
1945 static void
1946 smb_event_cancel(smb_server_t *sv, uint32_t txid)
1947 {
1948 	smb_event_t	*event;
1949 	smb_llist_t	*event_list;
1950 
1951 	SMB_SERVER_VALID(sv);
1952 
1953 	event_list = &sv->sv_event_list;
1954 	smb_llist_enter(event_list, RW_WRITER);
1955 
1956 	event = smb_llist_head(event_list);
1957 	while (event) {
1958 		SMB_EVENT_VALID(event);
1959 
1960 		if (txid == 0 || event->se_txid == txid) {
1961 			mutex_enter(&event->se_mutex);
1962 			event->se_errno = ECANCELED;
1963 			event->se_notified = B_TRUE;
1964 			cv_signal(&event->se_cv);
1965 			mutex_exit(&event->se_mutex);
1966 
1967 			if (txid != 0)
1968 				break;
1969 		}
1970 
1971 		event = smb_llist_next(event_list, event);
1972 	}
1973 
1974 	smb_llist_exit(event_list);
1975 }
1976 
1977 /*
1978  * If txid is non-zero, notify the specified event.
1979  * Otherwise, notify all events.
1980  */
1981 static void
1982 smb_event_notify(smb_server_t *sv, uint32_t txid)
1983 {
1984 	smb_event_t	*event;
1985 	smb_llist_t	*event_list;
1986 
1987 	SMB_SERVER_VALID(sv);
1988 
1989 	event_list = &sv->sv_event_list;
1990 	smb_llist_enter(event_list, RW_READER);
1991 
1992 	event = smb_llist_head(event_list);
1993 	while (event) {
1994 		SMB_EVENT_VALID(event);
1995 
1996 		if (txid == 0 || event->se_txid == txid) {
1997 			mutex_enter(&event->se_mutex);
1998 			event->se_notified = B_TRUE;
1999 			cv_signal(&event->se_cv);
2000 			mutex_exit(&event->se_mutex);
2001 
2002 			if (txid != 0)
2003 				break;
2004 		}
2005 
2006 		event = smb_llist_next(event_list, event);
2007 	}
2008 
2009 	smb_llist_exit(event_list);
2010 }
2011 
2012 /*
2013  * Allocate a new transaction id (txid).
2014  *
2015  * 0 or -1 are not assigned because they are used to detect invalid
2016  * conditions or to indicate all open id's.
2017  */
2018 static uint32_t
2019 smb_event_alloc_txid(void)
2020 {
2021 	static kmutex_t	txmutex;
2022 	static uint32_t	txid;
2023 	uint32_t	txid_ret;
2024 
2025 	mutex_enter(&txmutex);
2026 
2027 	if (txid == 0)
2028 		txid = ddi_get_lbolt() << 11;
2029 
2030 	do {
2031 		++txid;
2032 	} while (txid == 0 || txid == (uint32_t)-1);
2033 
2034 	txid_ret = txid;
2035 	mutex_exit(&txmutex);
2036 
2037 	return (txid_ret);
2038 }
2039