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