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