xref: /titanic_51/usr/src/uts/common/fs/smbsrv/smb_server.c (revision e085f1530a2b52c62d916408e99d66a557855d42)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
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  *		      +-----------------------------+
125  *				    |
126  *				    | T3
127  *				    |
128  *				    v
129  *		      +-----------------------------+
130  *		      |  SMB_SERVER_STATE_DELETING  |
131  *                    +-----------------------------+
132  *				    |
133  *				    |
134  *				    |
135  *				    v
136  *
137  * States
138  * ------
139  *
140  * SMB_SERVER_STATE_CREATED
141  *
142  *    This is the state of the server just after creation.
143  *
144  * SMB_SERVER_STATE_CONFIGURED
145  *
146  *    The server has been configured.
147  *
148  * SMB_SERVER_STATE_RUNNING
149  *
150  *    The server has been started. While in this state the threads listening on
151  *    the sockets car be started. The smbd daemon does so through an Ioctl:
152  *
153  *	smb_drv_ioctl(SMB_IOC_NBT_LISTEN) --> smb_server_nbt_listen()
154  *	smb_drv_ioctl(SMB_IOC_TCP_LISTEN) --> smb_server_nbt_listen()
155  *
156  *    When a client establishes a connection the thread listening leaves
157  *    temporarily the kernel. While in user space it creates a thread for the
158  *    new session. It then returns to kernel with the result of the thread
159  *    creation. If the creation failed the new session context is destroyed
160  *    before returning listening.
161  *
162  *    The new created thread enters the kernel though an Ioctl:
163  *
164  *	smb_drv_ioctl(SMB_IOC_NBT_RECEIVE) --> smb_server_nbt_receive()
165  *	smb_drv_ioctl(SMB_IOC_TCP_RECEIVE) --> smb_server_tcp_receive()
166  *
167  * SMB_SERVER_STATE_STOPPING
168  *
169  *    The threads listening on the NBT and TCP sockets are being terminated.
170  *
171  *
172  * Transitions
173  * -----------
174  *
175  * Transition T0
176  *
177  *    The daemon smbd triggers its creation by opening the smbsrv device. If
178  *    the zone where the daemon lives doesn't have an smb server yet it is
179  *    created.
180  *
181  *		smb_drv_open() --> smb_server_create()
182  *
183  * Transition T1
184  *
185  *    This transition occurs in smb_server_configure(). It is triggered by the
186  *    daemon through an Ioctl.
187  *
188  *	smb_drv_ioctl(SMB_IOC_CONFIG) --> smb_server_configure()
189  *
190  * Transition T2
191  *
192  *    This transition occurs in smb_server_start(). It is triggered by the
193  *    daemon through an Ioctl.
194  *
195  *	smb_drv_ioctl(SMB_IOC_START) --> smb_server_start()
196  *
197  * Transition T3
198  *
199  *    This transition occurs in smb_server_delete(). It is triggered by the
200  *    daemon when closing the smbsrv device
201  *
202  *		smb_drv_close() --> smb_server_delete()
203  *
204  * Comments
205  * --------
206  *
207  * This files assumes that there will one SMB server per zone. For now the
208  * smb server works only in global zone. There's nothing in this file preventing
209  * an smb server from being created in a non global zone. That limitation is
210  * enforced in user space.
211  */
212 
213 #include <sys/strsubr.h>
214 #include <sys/cmn_err.h>
215 #include <sys/priv.h>
216 #include <sys/socketvar.h>
217 #include <sys/zone.h>
218 #include <smbsrv/smb_kproto.h>
219 #include <smbsrv/netbios.h>
220 #include <smbsrv/smb_incl.h>
221 #include <smbsrv/cifs.h>
222 #include <smbsrv/smb_fsops.h>
223 #include <smbsrv/smb_share.h>
224 #include <smbsrv/smb_door_svc.h>
225 #include <smbsrv/smb_kstat.h>
226 
227 extern void smb_dispatch_kstat_init(void);
228 extern void smb_dispatch_kstat_fini(void);
229 extern void smb_reply_notify_change_request(smb_request_t *);
230 
231 static int smb_server_kstat_init(smb_server_t *);
232 static void smb_server_kstat_fini(smb_server_t *);
233 static int smb_server_kstat_update_info(kstat_t *, int);
234 static void smb_server_timers(smb_thread_t *, void *);
235 static int smb_server_listen(smb_server_t *, smb_listener_daemon_t *,
236     in_port_t, int, int);
237 static int smb_server_lookup(smb_server_t **);
238 static void smb_server_release(smb_server_t *);
239 static int smb_server_ulist_geti(smb_session_list_t *, uint32_t,
240     uint8_t *, uint32_t, uint_t *);
241 static void smb_server_store_cfg(smb_server_t *, smb_ioc_cfg_t *);
242 static void smb_server_stop(smb_server_t *);
243 static int smb_server_fsop_start(smb_server_t *);
244 static void smb_server_fsop_stop(smb_server_t *);
245 
246 static void smb_server_disconnect_share(char *, smb_server_t *);
247 static void smb_server_thread_unexport(smb_thread_t *, void *);
248 
249 static smb_llist_t	smb_servers;
250 
251 /*
252  * *****************************************************************************
253  * **************** Functions called from the device interface *****************
254  * *****************************************************************************
255  *
256  * These functions determine the relevant smb server to which the call apply.
257  */
258 
259 /*
260  * smb_server_svc_init
261  *
262  * This function must be called from smb_drv_attach().
263  */
264 int
265 smb_server_svc_init(void)
266 {
267 	int	rc = 0;
268 
269 	while (rc == 0) {
270 		if (rc = smb_mbc_init())
271 			continue;
272 		if (rc = smb_vop_init())
273 			continue;
274 		if (rc = smb_node_init())
275 			continue;
276 		if (rc = smb_fem_init())
277 			continue;
278 		if (rc = smb_user_init())
279 			continue;
280 		if (rc = smb_notify_init())
281 			continue;
282 		if (rc = smb_net_init())
283 			continue;
284 		smb_llist_constructor(&smb_servers, sizeof (smb_server_t),
285 		    offsetof(smb_server_t, sv_lnd));
286 		return (0);
287 	}
288 	smb_net_fini();
289 	smb_notify_fini();
290 	smb_user_fini();
291 	smb_fem_fini();
292 	smb_node_fini();
293 	smb_vop_fini();
294 	smb_mbc_fini();
295 	return (rc);
296 }
297 
298 /*
299  * smb_server_svc_fini
300  *
301  * This function must called from smb_drv_detach(). It will fail if servers
302  * still exist.
303  */
304 int
305 smb_server_svc_fini(void)
306 {
307 	int	rc = EBUSY;
308 
309 	if (smb_llist_get_count(&smb_servers) == 0) {
310 		smb_net_fini();
311 		smb_notify_fini();
312 		smb_user_fini();
313 		smb_fem_fini();
314 		smb_node_fini();
315 		smb_vop_fini();
316 		smb_mbc_fini();
317 		smb_llist_destructor(&smb_servers);
318 		rc = 0;
319 	}
320 	return (rc);
321 }
322 
323 /*
324  * smb_server_create
325  *
326  * This function will fail if there's already a server associated with the
327  * caller's zone.
328  */
329 int
330 smb_server_create(void)
331 {
332 	zoneid_t	zid;
333 	smb_server_t	*sv;
334 
335 	zid = getzoneid();
336 
337 	smb_llist_enter(&smb_servers, RW_WRITER);
338 	sv = smb_llist_head(&smb_servers);
339 	while (sv) {
340 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
341 		if (sv->sv_zid == zid) {
342 			smb_llist_exit(&smb_servers);
343 			return (EPERM);
344 		}
345 		sv = smb_llist_next(&smb_servers, sv);
346 	}
347 
348 	sv = kmem_zalloc(sizeof (smb_server_t), KM_NOSLEEP);
349 	if (sv == NULL) {
350 		smb_llist_exit(&smb_servers);
351 		return (ENOMEM);
352 	}
353 
354 	smb_llist_constructor(&sv->sv_vfs_list, sizeof (smb_vfs_t),
355 	    offsetof(smb_vfs_t, sv_lnd));
356 
357 	smb_slist_constructor(&sv->sv_unexport_list, sizeof (smb_unexport_t),
358 	    offsetof(smb_unexport_t, ux_lnd));
359 
360 	smb_session_list_constructor(&sv->sv_nbt_daemon.ld_session_list);
361 	smb_session_list_constructor(&sv->sv_tcp_daemon.ld_session_list);
362 
363 	sv->si_cache_unexport = kmem_cache_create("smb_unexport_cache",
364 	    sizeof (smb_unexport_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
365 	sv->si_cache_vfs = kmem_cache_create("smb_vfs_cache",
366 	    sizeof (smb_vfs_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
367 	sv->si_cache_request = kmem_cache_create("smb_request_cache",
368 	    sizeof (smb_request_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
369 	sv->si_cache_session = kmem_cache_create("smb_session_cache",
370 	    sizeof (smb_session_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
371 	sv->si_cache_user = kmem_cache_create("smb_user_cache",
372 	    sizeof (smb_user_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
373 	sv->si_cache_tree = kmem_cache_create("smb_tree_cache",
374 	    sizeof (smb_tree_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
375 	sv->si_cache_ofile = kmem_cache_create("smb_ofile_cache",
376 	    sizeof (smb_ofile_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
377 	sv->si_cache_odir = kmem_cache_create("smb_odir_cache",
378 	    sizeof (smb_odir_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
379 
380 	smb_thread_init(&sv->si_thread_timers,
381 	    "smb_timers", smb_server_timers, sv,
382 	    NULL, NULL);
383 
384 	smb_thread_init(&sv->si_thread_unexport, "smb_thread_unexport",
385 	    smb_server_thread_unexport, sv, NULL, NULL);
386 
387 	sv->sv_pid = curproc->p_pid;
388 
389 	smb_kdoor_clnt_init();
390 	smb_opipe_door_init();
391 	(void) smb_server_kstat_init(sv);
392 
393 	mutex_init(&sv->sv_mutex, NULL, MUTEX_DEFAULT, NULL);
394 	cv_init(&sv->sv_cv, NULL, CV_DEFAULT, NULL);
395 	sv->sv_state = SMB_SERVER_STATE_CREATED;
396 	sv->sv_magic = SMB_SERVER_MAGIC;
397 	sv->sv_zid = zid;
398 
399 	smb_llist_insert_tail(&smb_servers, sv);
400 	smb_llist_exit(&smb_servers);
401 	return (0);
402 }
403 
404 /*
405  * smb_server_delete
406  *
407  * This function will delete the server passed in. It will make sure that all
408  * activity associated that server has ceased before destroying it.
409  */
410 int
411 smb_server_delete(void)
412 {
413 	smb_server_t	*sv;
414 	smb_unexport_t	*ux;
415 	int		rc;
416 
417 	rc = smb_server_lookup(&sv);
418 	if (rc != 0)
419 		return (rc);
420 
421 	mutex_enter(&sv->sv_mutex);
422 	switch (sv->sv_state) {
423 	case SMB_SERVER_STATE_RUNNING:
424 	{
425 		boolean_t	nbt = B_FALSE;
426 		boolean_t	tcp = B_FALSE;
427 
428 		if (sv->sv_nbt_daemon.ld_kth) {
429 			tsignal(sv->sv_nbt_daemon.ld_kth, SIGINT);
430 			nbt = B_TRUE;
431 		}
432 		if (sv->sv_tcp_daemon.ld_kth) {
433 			tsignal(sv->sv_tcp_daemon.ld_kth, SIGINT);
434 			tcp = B_TRUE;
435 		}
436 		sv->sv_state = SMB_SERVER_STATE_DELETING;
437 		mutex_exit(&sv->sv_mutex);
438 		if (nbt)
439 			thread_join(sv->sv_nbt_daemon.ld_ktdid);
440 		if (tcp)
441 			thread_join(sv->sv_tcp_daemon.ld_ktdid);
442 		mutex_enter(&sv->sv_mutex);
443 		break;
444 	}
445 	case SMB_SERVER_STATE_CONFIGURED:
446 	case SMB_SERVER_STATE_CREATED:
447 		sv->sv_state = SMB_SERVER_STATE_DELETING;
448 		break;
449 	default:
450 		ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
451 		mutex_exit(&sv->sv_mutex);
452 		smb_server_release(sv);
453 		return (ENOTTY);
454 	}
455 
456 	ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
457 
458 	sv->sv_refcnt--;
459 	while (sv->sv_refcnt)
460 		cv_wait(&sv->sv_cv, &sv->sv_mutex);
461 
462 	mutex_exit(&sv->sv_mutex);
463 
464 	smb_llist_enter(&smb_servers, RW_WRITER);
465 	smb_llist_remove(&smb_servers, sv);
466 	smb_llist_exit(&smb_servers);
467 
468 	smb_server_stop(sv);
469 	rw_destroy(&sv->sv_cfg_lock);
470 	smb_opipe_door_fini();
471 	smb_kdoor_clnt_fini();
472 	smb_server_kstat_fini(sv);
473 	smb_llist_destructor(&sv->sv_vfs_list);
474 
475 	while ((ux = list_head(&sv->sv_unexport_list.sl_list)) != NULL) {
476 		smb_slist_remove(&sv->sv_unexport_list, ux);
477 		kmem_cache_free(sv->si_cache_unexport, ux);
478 	}
479 	smb_slist_destructor(&sv->sv_unexport_list);
480 
481 	kmem_cache_destroy(sv->si_cache_unexport);
482 	kmem_cache_destroy(sv->si_cache_vfs);
483 	kmem_cache_destroy(sv->si_cache_request);
484 	kmem_cache_destroy(sv->si_cache_session);
485 	kmem_cache_destroy(sv->si_cache_user);
486 	kmem_cache_destroy(sv->si_cache_tree);
487 	kmem_cache_destroy(sv->si_cache_ofile);
488 	kmem_cache_destroy(sv->si_cache_odir);
489 
490 	smb_thread_destroy(&sv->si_thread_timers);
491 	smb_thread_destroy(&sv->si_thread_unexport);
492 	mutex_destroy(&sv->sv_mutex);
493 	cv_destroy(&sv->sv_cv);
494 	sv->sv_magic = 0;
495 	kmem_free(sv, sizeof (smb_server_t));
496 
497 	return (0);
498 }
499 
500 /*
501  * smb_server_configure
502  */
503 int
504 smb_server_configure(smb_ioc_cfg_t *ioc)
505 {
506 	int		rc = 0;
507 	smb_server_t	*sv;
508 
509 	rc = smb_server_lookup(&sv);
510 	if (rc)
511 		return (rc);
512 
513 	mutex_enter(&sv->sv_mutex);
514 	switch (sv->sv_state) {
515 	case SMB_SERVER_STATE_CREATED:
516 		smb_server_store_cfg(sv, ioc);
517 		sv->sv_state = SMB_SERVER_STATE_CONFIGURED;
518 		break;
519 
520 	case SMB_SERVER_STATE_CONFIGURED:
521 		smb_server_store_cfg(sv, ioc);
522 		break;
523 
524 	case SMB_SERVER_STATE_RUNNING:
525 		rw_enter(&sv->sv_cfg_lock, RW_WRITER);
526 		smb_server_store_cfg(sv, ioc);
527 		rw_exit(&sv->sv_cfg_lock);
528 		break;
529 
530 	default:
531 		ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
532 		rc = EFAULT;
533 		break;
534 	}
535 	mutex_exit(&sv->sv_mutex);
536 
537 	smb_server_release(sv);
538 
539 	return (rc);
540 }
541 
542 /*
543  * smb_server_start
544  */
545 int
546 smb_server_start(smb_ioc_start_t *ioc)
547 {
548 	int		rc = 0;
549 	smb_server_t	*sv;
550 
551 	rc = smb_server_lookup(&sv);
552 	if (rc)
553 		return (rc);
554 
555 	mutex_enter(&sv->sv_mutex);
556 	switch (sv->sv_state) {
557 	case SMB_SERVER_STATE_CONFIGURED:
558 
559 		sv->sv_thread_pool = taskq_create("smb_workers",
560 		    sv->sv_cfg.skc_maxworkers, SMB_WORKER_PRIORITY,
561 		    sv->sv_cfg.skc_maxworkers, INT_MAX,
562 		    TASKQ_DYNAMIC|TASKQ_PREPOPULATE);
563 
564 		sv->sv_session = smb_session_create(NULL, 0, sv, 0);
565 
566 		if (sv->sv_thread_pool == NULL || sv->sv_session == NULL) {
567 			rc = ENOMEM;
568 			break;
569 		}
570 
571 		if (rc = smb_server_fsop_start(sv))
572 			break;
573 		ASSERT(sv->sv_lmshrd == NULL);
574 		sv->sv_lmshrd = smb_kshare_init(ioc->lmshrd);
575 		if (sv->sv_lmshrd == NULL)
576 			break;
577 		if (rc = smb_kdoor_clnt_open(ioc->udoor))
578 			break;
579 		if (rc = smb_thread_start(&sv->si_thread_timers))
580 			break;
581 		if (rc = smb_thread_start(&sv->si_thread_unexport))
582 			break;
583 		if (rc = smb_opipe_door_open(ioc->opipe)) {
584 			cmn_err(CE_WARN, "Cannot open opipe door");
585 			break;
586 		}
587 
588 		(void) oem_language_set("english");
589 
590 		sv->sv_state = SMB_SERVER_STATE_RUNNING;
591 		mutex_exit(&sv->sv_mutex);
592 		smb_server_release(sv);
593 		return (0);
594 	default:
595 		ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
596 		    (sv->sv_state == SMB_SERVER_STATE_RUNNING) ||
597 		    (sv->sv_state == SMB_SERVER_STATE_DELETING));
598 		mutex_exit(&sv->sv_mutex);
599 		smb_server_release(sv);
600 		return (ENOTTY);
601 	}
602 
603 	smb_server_stop(sv);
604 	mutex_exit(&sv->sv_mutex);
605 	smb_server_release(sv);
606 	return (rc);
607 }
608 
609 /*
610  * smb_server_nbt_listen: SMB-over-NetBIOS service
611  *
612  * Traditional SMB service over NetBIOS (port 139), which requires
613  * that a NetBIOS session be established.
614  */
615 int
616 smb_server_nbt_listen(smb_ioc_listen_t *ioc)
617 {
618 	smb_server_t	*sv;
619 	int		rc;
620 
621 	rc = smb_server_lookup(&sv);
622 	if (rc)
623 		return (rc);
624 
625 	mutex_enter(&sv->sv_mutex);
626 	switch (sv->sv_state) {
627 	case SMB_SERVER_STATE_RUNNING:
628 		if ((sv->sv_nbt_daemon.ld_kth != NULL) &&
629 		    (sv->sv_nbt_daemon.ld_kth != curthread)) {
630 			mutex_exit(&sv->sv_mutex);
631 			smb_server_release(sv);
632 			return (EACCES);
633 		} else {
634 			sv->sv_nbt_daemon.ld_kth = curthread;
635 			sv->sv_nbt_daemon.ld_ktdid = curthread->t_did;
636 		}
637 		break;
638 	default:
639 		ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
640 		    (sv->sv_state == SMB_SERVER_STATE_CONFIGURED) ||
641 		    (sv->sv_state == SMB_SERVER_STATE_DELETING));
642 		mutex_exit(&sv->sv_mutex);
643 		smb_server_release(sv);
644 		return (EFAULT);
645 	}
646 	mutex_exit(&sv->sv_mutex);
647 
648 	/*
649 	 * netbios must be ipv4
650 	 */
651 	rc = smb_server_listen(sv, &sv->sv_nbt_daemon, SSN_SRVC_TCP_PORT,
652 	    AF_INET, ioc->error);
653 
654 	if (rc) {
655 		mutex_enter(&sv->sv_mutex);
656 		sv->sv_nbt_daemon.ld_kth = NULL;
657 		mutex_exit(&sv->sv_mutex);
658 	}
659 
660 	smb_server_release(sv);
661 	return (rc);
662 }
663 
664 int
665 smb_server_tcp_listen(smb_ioc_listen_t *ioc)
666 {
667 	smb_server_t	*sv;
668 	int		rc;
669 
670 	rc = smb_server_lookup(&sv);
671 	if (rc)
672 		return (rc);
673 
674 	mutex_enter(&sv->sv_mutex);
675 	switch (sv->sv_state) {
676 	case SMB_SERVER_STATE_RUNNING:
677 		if ((sv->sv_tcp_daemon.ld_kth) &&
678 		    (sv->sv_tcp_daemon.ld_kth != curthread)) {
679 			mutex_exit(&sv->sv_mutex);
680 			smb_server_release(sv);
681 			return (EACCES);
682 		} else {
683 			sv->sv_tcp_daemon.ld_kth = curthread;
684 			sv->sv_tcp_daemon.ld_ktdid = curthread->t_did;
685 		}
686 		break;
687 	default:
688 		ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
689 		    (sv->sv_state == SMB_SERVER_STATE_CONFIGURED) ||
690 		    (sv->sv_state == SMB_SERVER_STATE_DELETING));
691 		mutex_exit(&sv->sv_mutex);
692 		smb_server_release(sv);
693 		return (EFAULT);
694 	}
695 	mutex_exit(&sv->sv_mutex);
696 
697 	if (sv->sv_cfg.skc_ipv6_enable)
698 		rc = smb_server_listen(sv, &sv->sv_tcp_daemon,
699 		    SMB_SRVC_TCP_PORT, AF_INET6, ioc->error);
700 	else
701 		rc = smb_server_listen(sv, &sv->sv_tcp_daemon,
702 		    SMB_SRVC_TCP_PORT, AF_INET, ioc->error);
703 	if (rc) {
704 		mutex_enter(&sv->sv_mutex);
705 		sv->sv_tcp_daemon.ld_kth = NULL;
706 		mutex_exit(&sv->sv_mutex);
707 	}
708 
709 	smb_server_release(sv);
710 	return (rc);
711 }
712 
713 /*
714  * smb_server_nbt_receive
715  */
716 int
717 smb_server_nbt_receive(void)
718 {
719 	int		rc;
720 	smb_server_t	*sv;
721 
722 	if ((rc = smb_server_lookup(&sv)) == 0) {
723 		rc = smb_session_daemon(&sv->sv_nbt_daemon.ld_session_list);
724 		smb_server_release(sv);
725 	}
726 
727 	return (rc);
728 }
729 
730 /*
731  * smb_server_tcp_receive
732  */
733 int
734 smb_server_tcp_receive(void)
735 {
736 	int		rc;
737 	smb_server_t	*sv;
738 
739 	if ((rc = smb_server_lookup(&sv)) == 0) {
740 		rc = smb_session_daemon(&sv->sv_tcp_daemon.ld_session_list);
741 		smb_server_release(sv);
742 	}
743 
744 	return (rc);
745 }
746 
747 int
748 smb_server_set_gmtoff(smb_ioc_gmt_t *ioc)
749 {
750 	int		rc;
751 	smb_server_t	*sv;
752 
753 	if ((rc = smb_server_lookup(&sv)) == 0) {
754 		sv->si_gmtoff = ioc->offset;
755 		smb_server_release(sv);
756 	}
757 
758 	return (rc);
759 }
760 
761 int
762 smb_server_user_number(smb_ioc_usernum_t *ioc)
763 {
764 	smb_server_t	*sv;
765 	int		rc;
766 
767 	if ((rc = smb_server_lookup(&sv)) == 0) {
768 		ioc->num = sv->sv_open_users;
769 		smb_server_release(sv);
770 	}
771 	return (rc);
772 }
773 
774 int
775 smb_server_user_list(smb_ioc_ulist_t *ioc)
776 {
777 	smb_server_t *sv;
778 	uint8_t *data;
779 	uint32_t data_len;
780 	uint_t bytes_encoded;
781 	int rc;
782 
783 	if ((rc = smb_server_lookup(&sv)) == 0) {
784 
785 		bytes_encoded = 0;
786 		data = ioc->data;
787 		data_len = ioc->data_len;
788 		ioc->num =
789 		    smb_server_ulist_geti(&sv->sv_nbt_daemon.ld_session_list,
790 		    ioc->cookie, data, data_len, &bytes_encoded);
791 
792 		data += bytes_encoded;
793 		data_len -= bytes_encoded;
794 		ioc->data_len = bytes_encoded;
795 		ioc->cookie += ioc->num;
796 
797 		ioc->num +=
798 		    smb_server_ulist_geti(&sv->sv_tcp_daemon.ld_session_list,
799 		    ioc->cookie, data, data_len, &bytes_encoded);
800 
801 		ioc->data_len += bytes_encoded;
802 
803 		smb_server_release(sv);
804 	}
805 	return (rc);
806 }
807 
808 /*
809  * *****************************************************************************
810  * ****************** Functions called from the door interface *****************
811  * *****************************************************************************
812  *
813  * These functions determine the relevant smb server to which the call apply.
814  */
815 
816 uint32_t
817 smb_server_get_user_count(void)
818 {
819 	smb_server_t	*sv;
820 	uint32_t	counter = 0;
821 
822 	if (smb_server_lookup(&sv) == 0) {
823 		counter = (uint32_t)sv->sv_open_users;
824 		smb_server_release(sv);
825 	}
826 
827 	return (counter);
828 }
829 
830 uint32_t
831 smb_server_get_session_count(void)
832 {
833 	smb_server_t	*sv;
834 	uint32_t	counter = 0;
835 
836 	if (smb_server_lookup(&sv))
837 		return (0);
838 
839 	rw_enter(&sv->sv_nbt_daemon.ld_session_list.se_lock, RW_READER);
840 	counter = sv->sv_nbt_daemon.ld_session_list.se_act.count;
841 	rw_exit(&sv->sv_nbt_daemon.ld_session_list.se_lock);
842 	rw_enter(&sv->sv_tcp_daemon.ld_session_list.se_lock, RW_READER);
843 	counter += sv->sv_tcp_daemon.ld_session_list.se_act.count;
844 	rw_exit(&sv->sv_tcp_daemon.ld_session_list.se_lock);
845 
846 	smb_server_release(sv);
847 
848 	return (counter);
849 }
850 
851 /*
852  * smb_server_disconnect_share
853  *
854  * Disconnects the specified share. This function should be called after the
855  * share passed in has been made unavailable by the "share manager".
856  */
857 static void
858 smb_server_disconnect_share(char *sharename, smb_server_t *sv)
859 {
860 	smb_session_disconnect_share(&sv->sv_nbt_daemon.ld_session_list,
861 	    sharename);
862 	smb_session_disconnect_share(&sv->sv_tcp_daemon.ld_session_list,
863 	    sharename);
864 }
865 
866 /*
867  * smb_server_share_export()
868  *
869  * This function handles kernel processing at share enable time.
870  *
871  * At share-enable time (LMSHRD_ADD), the file system corresponding to
872  * the share is checked for characteristics that are required for SMB
873  * sharing.  If this check passes, then a hold is taken on the root vnode
874  * of the file system (or a reference count on the corresponding smb_vfs_t
875  * is bumped), preventing an unmount.  (See smb_vfs_hold()).
876  */
877 
878 int
879 smb_server_share_export(smb_ioc_share_t *ioc)
880 {
881 	smb_server_t	*sv;
882 	int		error = 0;
883 	smb_node_t	*fnode = NULL;
884 	smb_node_t	*dnode;
885 	char		last_comp[MAXNAMELEN];
886 	smb_request_t	*sr;
887 
888 	if (smb_server_lookup(&sv))
889 		return (EINVAL);
890 
891 	mutex_enter(&sv->sv_mutex);
892 	switch (sv->sv_state) {
893 	case SMB_SERVER_STATE_RUNNING:
894 		break;
895 	default:
896 		mutex_exit(&sv->sv_mutex);
897 		return (ENOTACTIVE);
898 	}
899 	mutex_exit(&sv->sv_mutex);
900 
901 	sr = smb_request_alloc(sv->sv_session, 0);
902 	if (sr == NULL) {
903 		smb_server_release(sv);
904 		return (ENOMEM);
905 	}
906 
907 	sr->user_cr = kcred;
908 
909 	error = smb_pathname_reduce(sr, kcred, ioc->path,
910 	    NULL, NULL, &dnode, last_comp);
911 
912 	if (error) {
913 		smb_request_free(sr);
914 		smb_server_release(sv);
915 		return (error);
916 	}
917 
918 	error = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS,
919 	    sv->si_root_smb_node, dnode, last_comp, &fnode);
920 
921 	smb_node_release(dnode);
922 
923 	if (error) {
924 		smb_request_free(sr);
925 		smb_server_release(sv);
926 		return (error);
927 	}
928 
929 	ASSERT(fnode->vp && fnode->vp->v_vfsp);
930 
931 #ifdef SMB_ENFORCE_NODEV
932 	if (vfs_optionisset(fnode->vp->v_vfsp, MNTOPT_NODEVICES, NULL) == 0) {
933 		smb_node_release(fnode);
934 		smb_request_free(sr);
935 		smb_server_release(sv);
936 		return (EINVAL);
937 	}
938 #endif /* SMB_ENFORCE_NODEV */
939 
940 	if (!smb_vfs_hold(sv, fnode->vp->v_vfsp))
941 		error = ENOMEM;
942 
943 	/*
944 	 * The refcount on the smb_vfs has been incremented.
945 	 * If it wasn't already, a hold has also been taken
946 	 * on the root vnode of the file system.
947 	 */
948 
949 	smb_node_release(fnode);
950 	smb_request_free(sr);
951 	smb_server_release(sv);
952 	return (error);
953 }
954 
955 /*
956  * smb_server_share_unexport()
957  *
958  * This function is invoked when a share is disabled to disconnect trees
959  * and close files.  Cleaning up may involve VOP and/or VFS calls, which
960  * may conflict/deadlock with stuck threads if something is amiss with the
961  * file system.  Queueing the request for asynchronous processing allows the
962  * call to return immediately so that, if the unshare is being done in the
963  * context of a forced unmount, the forced unmount will always be able to
964  * proceed (unblocking stuck I/O and eventually allowing all blocked unshare
965  * processes to complete).
966  *
967  * The path lookup to find the root vnode of the VFS in question and the
968  * release of this vnode are done synchronously prior to any associated
969  * unmount.  Doing these asynchronous to an associated unmount could run
970  * the risk of a spurious EBUSY for a standard unmount or an EIO during
971  * the path lookup due to a forced unmount finishing first.
972  */
973 
974 int
975 smb_server_share_unexport(smb_ioc_share_t *ioc)
976 {
977 	smb_server_t	*sv;
978 	smb_request_t	*sr;
979 	smb_unexport_t	*ux;
980 	smb_node_t	*fnode = NULL;
981 	smb_node_t	*dnode;
982 	char		last_comp[MAXNAMELEN];
983 	int		rc;
984 
985 	if ((rc = smb_server_lookup(&sv)))
986 		return (rc);
987 
988 	mutex_enter(&sv->sv_mutex);
989 	switch (sv->sv_state) {
990 	case SMB_SERVER_STATE_RUNNING:
991 		break;
992 	default:
993 		mutex_exit(&sv->sv_mutex);
994 		return (ENOTACTIVE);
995 	}
996 	mutex_exit(&sv->sv_mutex);
997 
998 	sr = smb_request_alloc(sv->sv_session, 0);
999 
1000 	if (sr == NULL) {
1001 		smb_server_release(sv);
1002 		return (ENOMEM);
1003 	}
1004 
1005 	sr->user_cr = kcred;
1006 
1007 	rc = smb_pathname_reduce(sr, kcred, ioc->path, NULL, NULL,
1008 	    &dnode, last_comp);
1009 
1010 	if (rc) {
1011 		smb_request_free(sr);
1012 		smb_server_release(sv);
1013 		return (rc);
1014 	}
1015 
1016 	rc = smb_fsop_lookup(sr, kcred, SMB_FOLLOW_LINKS, sv->si_root_smb_node,
1017 	    dnode, last_comp, &fnode);
1018 
1019 	smb_node_release(dnode);
1020 	smb_request_free(sr);
1021 
1022 	if (rc) {
1023 		smb_server_release(sv);
1024 		return (rc);
1025 	}
1026 
1027 	ASSERT(fnode->vp && fnode->vp->v_vfsp);
1028 
1029 	smb_vfs_rele(sv, fnode->vp->v_vfsp);
1030 
1031 	smb_node_release(fnode);
1032 
1033 	ux = kmem_cache_alloc(sv->si_cache_unexport, KM_SLEEP);
1034 
1035 	(void) strlcpy(ux->ux_sharename, ioc->name, MAXNAMELEN);
1036 
1037 	smb_slist_insert_tail(&sv->sv_unexport_list, ux);
1038 	smb_thread_signal(&sv->si_thread_unexport);
1039 
1040 	smb_server_release(sv);
1041 	return (0);
1042 }
1043 
1044 /*
1045  * smb_server_thread_unexport
1046  *
1047  * This function processes the unexport event list and disconnects shares
1048  * asynchronously.  The function executes as a zone-specific thread.
1049  *
1050  * The server arg passed in is safe to use without a reference count, because
1051  * the server cannot be deleted until smb_thread_stop()/destroy() return,
1052  * which is also when the thread exits.
1053  */
1054 
1055 static void
1056 smb_server_thread_unexport(smb_thread_t *thread, void *arg)
1057 {
1058 	smb_server_t *sv = (smb_server_t *)arg;
1059 	smb_unexport_t	*ux;
1060 
1061 	while (smb_thread_continue(thread)) {
1062 		while ((ux = list_head(&sv->sv_unexport_list.sl_list))
1063 		    != NULL) {
1064 			smb_slist_remove(&sv->sv_unexport_list, ux);
1065 			smb_server_disconnect_share(ux->ux_sharename, sv);
1066 			kmem_cache_free(sv->si_cache_unexport, ux);
1067 		}
1068 	}
1069 }
1070 
1071 /*
1072  * This is a special interface that will be utilized by ZFS to cause a share to
1073  * be added/removed.
1074  *
1075  * arg is either a lmshare_info_t or share_name from userspace.
1076  * It will need to be copied into the kernel.   It is lmshare_info_t
1077  * for add operations and share_name for delete operations.
1078  */
1079 int
1080 smb_server_share(void *arg, boolean_t add_share)
1081 {
1082 	smb_server_t	*sv;
1083 	int		rc;
1084 
1085 	rc = smb_server_lookup(&sv);
1086 	if (rc == 0) {
1087 		mutex_enter(&sv->sv_mutex);
1088 		switch (sv->sv_state) {
1089 		case SMB_SERVER_STATE_RUNNING:
1090 			mutex_exit(&sv->sv_mutex);
1091 			(void) smb_kshare_upcall(sv->sv_lmshrd, arg, add_share);
1092 			break;
1093 		default:
1094 			mutex_exit(&sv->sv_mutex);
1095 			break;
1096 		}
1097 		smb_server_release(sv);
1098 	}
1099 	return (0);
1100 }
1101 
1102 /*
1103  * *****************************************************************************
1104  * **************** Functions called from the internal layers ******************
1105  * *****************************************************************************
1106  *
1107  * These functions are provided the relevant smb server by the caller.
1108  */
1109 
1110 void
1111 smb_server_reconnection_check(smb_server_t *sv, smb_session_t *session)
1112 {
1113 	ASSERT(sv == session->s_server);
1114 
1115 	smb_session_reconnection_check(&sv->sv_nbt_daemon.ld_session_list,
1116 	    session);
1117 	smb_session_reconnection_check(&sv->sv_tcp_daemon.ld_session_list,
1118 	    session);
1119 }
1120 
1121 void
1122 smb_server_get_cfg(smb_server_t *sv, smb_kmod_cfg_t *cfg)
1123 {
1124 	rw_enter(&sv->sv_cfg_lock, RW_READER);
1125 	bcopy(&sv->sv_cfg, cfg, sizeof (*cfg));
1126 	rw_exit(&sv->sv_cfg_lock);
1127 }
1128 
1129 /*
1130  * *****************************************************************************
1131  * *************************** Static Functions ********************************
1132  * *****************************************************************************
1133  */
1134 
1135 static void
1136 smb_server_timers(smb_thread_t *thread, void *arg)
1137 {
1138 	smb_server_t	*sv = (smb_server_t *)arg;
1139 
1140 	ASSERT(sv != NULL);
1141 
1142 	while (smb_thread_continue_timedwait(thread, 1 /* Seconds */)) {
1143 		smb_session_timers(&sv->sv_nbt_daemon.ld_session_list);
1144 		smb_session_timers(&sv->sv_tcp_daemon.ld_session_list);
1145 	}
1146 }
1147 
1148 /*
1149  * smb_server_kstat_init
1150  */
1151 static int
1152 smb_server_kstat_init(smb_server_t *sv)
1153 {
1154 	(void) snprintf(sv->sv_ksp_name, sizeof (sv->sv_ksp_name), "%s%d",
1155 	    SMBSRV_KSTAT_NAME, sv->sv_zid);
1156 
1157 	sv->sv_ksp = kstat_create(SMBSRV_KSTAT_MODULE, 0, sv->sv_ksp_name,
1158 	    SMBSRV_KSTAT_CLASS, KSTAT_TYPE_NAMED,
1159 	    sizeof (sv->sv_ks_data) / sizeof (kstat_named_t),
1160 	    KSTAT_FLAG_VIRTUAL);
1161 
1162 	if (sv->sv_ksp) {
1163 		(void) strlcpy(sv->sv_ks_data.open_files.name, "open_files",
1164 		    sizeof (sv->sv_ks_data.open_files.name));
1165 		sv->sv_ks_data.open_files.data_type = KSTAT_DATA_UINT32;
1166 		(void) strlcpy(sv->sv_ks_data.open_trees.name, "connections",
1167 		    sizeof (sv->sv_ks_data.open_trees.name));
1168 		sv->sv_ks_data.open_trees.data_type = KSTAT_DATA_UINT32;
1169 		(void) strlcpy(sv->sv_ks_data.open_users.name, "sessions",
1170 		    sizeof (sv->sv_ks_data.open_users.name));
1171 		sv->sv_ks_data.open_users.data_type = KSTAT_DATA_UINT32;
1172 
1173 		mutex_init(&sv->sv_ksp_mutex, NULL, MUTEX_DEFAULT, NULL);
1174 		sv->sv_ksp->ks_lock = &sv->sv_ksp_mutex;
1175 		sv->sv_ksp->ks_data = (void *)&sv->sv_ks_data;
1176 		sv->sv_ksp->ks_update = smb_server_kstat_update_info;
1177 		kstat_install(sv->sv_ksp);
1178 	}
1179 
1180 	/* create and initialize smb kstats - smb_dispatch stats */
1181 	smb_dispatch_kstat_init();
1182 
1183 	return (0);
1184 }
1185 
1186 /*
1187  * smb_server_kstat_fini
1188  */
1189 static void
1190 smb_server_kstat_fini(smb_server_t *sv)
1191 {
1192 	if (sv->sv_ksp) {
1193 		kstat_delete(sv->sv_ksp);
1194 		mutex_destroy(&sv->sv_ksp_mutex);
1195 		sv->sv_ksp = NULL;
1196 	}
1197 	smb_dispatch_kstat_fini();
1198 }
1199 
1200 /* ARGSUSED */
1201 static int
1202 smb_server_kstat_update_info(kstat_t *ksp, int rw)
1203 {
1204 	smb_server_t	*sv;
1205 
1206 	if (rw == KSTAT_WRITE) {
1207 		return (EACCES);
1208 	} else {
1209 		ASSERT(MUTEX_HELD(ksp->ks_lock));
1210 
1211 		_NOTE(LINTED("pointer cast may result in improper alignment"))
1212 		sv = (smb_server_t *)((uint8_t *)(ksp->ks_data) -
1213 		    offsetof(smb_server_t, sv_ks_data));
1214 
1215 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1216 
1217 		sv->sv_ks_data.open_files.value.ui32 = sv->sv_open_files;
1218 		sv->sv_ks_data.open_trees.value.ui32 = sv->sv_open_trees;
1219 		sv->sv_ks_data.open_users.value.ui32 = sv->sv_open_users;
1220 	}
1221 	return (0);
1222 }
1223 
1224 /*
1225  * smb_server_stop
1226  *
1227  * The mutex of the server must have been entered before calling this function.
1228  */
1229 static void
1230 smb_server_stop(smb_server_t *sv)
1231 {
1232 	ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1233 
1234 	smb_opipe_door_close();
1235 	smb_thread_stop(&sv->si_thread_timers);
1236 	smb_thread_stop(&sv->si_thread_unexport);
1237 	smb_kdoor_clnt_close();
1238 	smb_kshare_fini(sv->sv_lmshrd);
1239 	sv->sv_lmshrd = NULL;
1240 	smb_server_fsop_stop(sv);
1241 
1242 	if (sv->sv_session) {
1243 		smb_session_delete(sv->sv_session);
1244 		sv->sv_session = NULL;
1245 	}
1246 
1247 	if (sv->sv_thread_pool) {
1248 		taskq_destroy(sv->sv_thread_pool);
1249 		sv->sv_thread_pool = NULL;
1250 	}
1251 }
1252 
1253 static int
1254 smb_server_listen(
1255     smb_server_t		*sv,
1256     smb_listener_daemon_t	*ld,
1257     in_port_t			port,
1258     int				family,
1259     int				pthread_create_error)
1260 {
1261 	int			rc;
1262 	ksocket_t		s_so;
1263 	const uint32_t		on = 1;
1264 	const uint32_t		off = 0;
1265 	smb_session_t		*session;
1266 
1267 	if (pthread_create_error) {
1268 		/*
1269 		 * Delete the last session created. The user space thread
1270 		 * creation failed.
1271 		 */
1272 		smb_session_list_delete_tail(&ld->ld_session_list);
1273 	}
1274 
1275 	if (ld->ld_so == NULL) {
1276 		/* First time listener */
1277 		if (family == AF_INET) {
1278 			ld->ld_sin.sin_family = (uint32_t)family;
1279 			ld->ld_sin.sin_port = htons(port);
1280 			ld->ld_sin.sin_addr.s_addr = htonl(INADDR_ANY);
1281 		} else {
1282 			ld->ld_sin6.sin6_family = (uint32_t)family;
1283 			ld->ld_sin6.sin6_port = htons(port);
1284 			(void) memset(&ld->ld_sin6.sin6_addr.s6_addr, 0,
1285 			    sizeof (ld->ld_sin6.sin6_addr.s6_addr));
1286 		}
1287 		ld->ld_so = smb_socreate(family, SOCK_STREAM, 0);
1288 		if (ld->ld_so) {
1289 
1290 			(void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET,
1291 			    SO_MAC_EXEMPT, &off, sizeof (off), CRED());
1292 			(void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET,
1293 			    SO_REUSEADDR, &on, sizeof (on), CRED());
1294 
1295 			if (family == AF_INET) {
1296 				rc = ksocket_bind(ld->ld_so,
1297 				    (struct sockaddr *)&ld->ld_sin,
1298 				    sizeof (ld->ld_sin), CRED());
1299 			} else {
1300 				rc = ksocket_bind(ld->ld_so,
1301 				    (struct sockaddr *)&ld->ld_sin6,
1302 				    sizeof (ld->ld_sin6), CRED());
1303 			}
1304 			if (rc == 0) {
1305 				rc =  ksocket_listen(ld->ld_so, 20, CRED());
1306 				if (rc < 0) {
1307 					cmn_err(CE_WARN,
1308 					    "Port %d: listen failed", port);
1309 					smb_soshutdown(ld->ld_so);
1310 					smb_sodestroy(ld->ld_so);
1311 					ld->ld_so = NULL;
1312 					return (rc);
1313 				}
1314 			} else {
1315 				cmn_err(CE_WARN,
1316 				    "Port %d: bind failed", port);
1317 				smb_soshutdown(ld->ld_so);
1318 				smb_sodestroy(ld->ld_so);
1319 				ld->ld_so = NULL;
1320 				return (rc);
1321 			}
1322 		} else {
1323 			cmn_err(CE_WARN,
1324 			    "Port %d: socket create failed", port);
1325 			return (ENOMEM);
1326 		}
1327 	}
1328 
1329 	DTRACE_PROBE1(so__wait__accept, struct sonode *, ld->ld_so);
1330 
1331 	for (;;) {
1332 		rc = ksocket_accept(ld->ld_so, NULL, NULL, &s_so, CRED());
1333 		if (rc == 0) {
1334 			uint32_t	txbuf_size = 128*1024;
1335 			uint32_t	on = 1;
1336 
1337 			DTRACE_PROBE1(so__accept, struct sonode *, s_so);
1338 
1339 			(void) ksocket_setsockopt(s_so, IPPROTO_TCP,
1340 			    TCP_NODELAY, &on, sizeof (on), CRED());
1341 			(void) ksocket_setsockopt(s_so, SOL_SOCKET,
1342 			    SO_KEEPALIVE, &on, sizeof (on), CRED());
1343 			(void) ksocket_setsockopt(s_so, SOL_SOCKET, SO_SNDBUF,
1344 			    (const void *)&txbuf_size, sizeof (txbuf_size),
1345 			    CRED());
1346 			/*
1347 			 * Create a session for this connection.
1348 			 */
1349 			session = smb_session_create(s_so, port, sv, family);
1350 			if (session) {
1351 				smb_session_list_append(&ld->ld_session_list,
1352 				    session);
1353 				break;
1354 			} else {
1355 				smb_soshutdown(s_so);
1356 				smb_sodestroy(s_so);
1357 			}
1358 			continue;
1359 		}
1360 		smb_session_list_signal(&ld->ld_session_list);
1361 		smb_soshutdown(ld->ld_so);
1362 		smb_sodestroy(ld->ld_so);
1363 		ld->ld_so = NULL;
1364 		break;
1365 	}
1366 
1367 	return (rc);
1368 }
1369 
1370 /*
1371  * smb_server_lookup
1372  *
1373  * This function tries to find the server associated with the zone of the
1374  * caller.
1375  */
1376 static int
1377 smb_server_lookup(smb_server_t **psv)
1378 {
1379 	zoneid_t	zid;
1380 	smb_server_t	*sv;
1381 
1382 	zid = getzoneid();
1383 
1384 	smb_llist_enter(&smb_servers, RW_READER);
1385 	sv = smb_llist_head(&smb_servers);
1386 	while (sv) {
1387 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1388 		if (sv->sv_zid == zid) {
1389 			mutex_enter(&sv->sv_mutex);
1390 			if (sv->sv_state != SMB_SERVER_STATE_DELETING) {
1391 				sv->sv_refcnt++;
1392 				mutex_exit(&sv->sv_mutex);
1393 				smb_llist_exit(&smb_servers);
1394 				*psv = sv;
1395 				return (0);
1396 			}
1397 			mutex_exit(&sv->sv_mutex);
1398 			break;
1399 		}
1400 		sv = smb_llist_next(&smb_servers, sv);
1401 	}
1402 	smb_llist_exit(&smb_servers);
1403 	return (EPERM);
1404 }
1405 
1406 /*
1407  * smb_server_release
1408  *
1409  * This function decrements the reference count of the server and signals its
1410  * condition variable if the state of the server is SMB_SERVER_STATE_DELETING.
1411  */
1412 static void
1413 smb_server_release(smb_server_t *sv)
1414 {
1415 	ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1416 
1417 	mutex_enter(&sv->sv_mutex);
1418 	ASSERT(sv->sv_refcnt);
1419 	sv->sv_refcnt--;
1420 	if ((sv->sv_refcnt == 0) && (sv->sv_state == SMB_SERVER_STATE_DELETING))
1421 		cv_signal(&sv->sv_cv);
1422 	mutex_exit(&sv->sv_mutex);
1423 }
1424 
1425 static int
1426 smb_server_ulist_geti(smb_session_list_t *se, uint32_t cookie,
1427     uint8_t *buf, uint32_t buf_len, uint_t *pbe)
1428 {
1429 	smb_session_t *sn = NULL;
1430 	smb_user_t *user;
1431 	smb_llist_t *ulist;
1432 	smb_opipe_context_t ctx;
1433 	uint_t bytes_encoded;
1434 	int rc = 0;
1435 	int cnt = 0;
1436 
1437 	rw_enter(&se->se_lock, RW_READER);
1438 	sn = list_head(&se->se_act.lst);
1439 	while ((sn != NULL) && (rc == 0)) {
1440 		ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
1441 		ulist = &sn->s_user_list;
1442 		smb_llist_enter(ulist, RW_READER);
1443 		user = smb_llist_head(ulist);
1444 		while ((user != NULL) && (rc == 0)) {
1445 			ASSERT(user->u_magic == SMB_USER_MAGIC);
1446 			mutex_enter(&user->u_mutex);
1447 			if ((user->u_state == SMB_USER_STATE_LOGGED_IN) &&
1448 			    (cookie == 0)) {
1449 				smb_user_context_init(user, &ctx);
1450 				rc = smb_opipe_context_encode(&ctx, buf,
1451 				    buf_len, &bytes_encoded);
1452 				smb_user_context_fini(&ctx);
1453 				if (rc == 0) {
1454 					*pbe += bytes_encoded;
1455 					buf += bytes_encoded;
1456 					buf_len -= bytes_encoded;
1457 					cnt++;
1458 				}
1459 			}
1460 			mutex_exit(&user->u_mutex);
1461 			user = smb_llist_next(ulist, user);
1462 			if (cookie > 0)
1463 				cookie--;
1464 		}
1465 		smb_llist_exit(ulist);
1466 		sn = list_next(&se->se_act.lst, sn);
1467 	}
1468 	rw_exit(&se->se_lock);
1469 	return (cnt);
1470 }
1471 
1472 static void
1473 smb_server_store_cfg(smb_server_t *sv, smb_ioc_cfg_t *ioc)
1474 {
1475 	if (ioc->maxconnections == 0)
1476 		ioc->maxconnections = 0xFFFFFFFF;
1477 
1478 	smb_session_correct_keep_alive_values(
1479 	    &sv->sv_nbt_daemon.ld_session_list, ioc->keepalive);
1480 	smb_session_correct_keep_alive_values(
1481 	    &sv->sv_tcp_daemon.ld_session_list, ioc->keepalive);
1482 
1483 	sv->sv_cfg.skc_maxworkers = ioc->maxworkers;
1484 	sv->sv_cfg.skc_maxconnections = ioc->maxconnections;
1485 	sv->sv_cfg.skc_keepalive = ioc->keepalive;
1486 	sv->sv_cfg.skc_restrict_anon = ioc->restrict_anon;
1487 	sv->sv_cfg.skc_signing_enable = ioc->signing_enable;
1488 	sv->sv_cfg.skc_signing_required = ioc->signing_required;
1489 	sv->sv_cfg.skc_oplock_enable = ioc->oplock_enable;
1490 	sv->sv_cfg.skc_sync_enable = ioc->sync_enable;
1491 	sv->sv_cfg.skc_secmode = ioc->secmode;
1492 	sv->sv_cfg.skc_ipv6_enable = ioc->ipv6_enable;
1493 	(void) strlcpy(sv->sv_cfg.skc_nbdomain, ioc->nbdomain,
1494 	    sizeof (sv->sv_cfg.skc_nbdomain));
1495 	(void) strlcpy(sv->sv_cfg.skc_fqdn, ioc->fqdn,
1496 	    sizeof (sv->sv_cfg.skc_fqdn));
1497 	(void) strlcpy(sv->sv_cfg.skc_hostname, ioc->hostname,
1498 	    sizeof (sv->sv_cfg.skc_hostname));
1499 	(void) strlcpy(sv->sv_cfg.skc_system_comment, ioc->system_comment,
1500 	    sizeof (sv->sv_cfg.skc_system_comment));
1501 }
1502 
1503 static int
1504 smb_server_fsop_start(smb_server_t *sv)
1505 {
1506 	int	error;
1507 
1508 	error = smb_node_root_init(rootdir, sv, &sv->si_root_smb_node);
1509 	if (error != 0)
1510 		sv->si_root_smb_node = NULL;
1511 
1512 	return (error);
1513 }
1514 
1515 static void
1516 smb_server_fsop_stop(smb_server_t *sv)
1517 {
1518 	if (sv->si_root_smb_node != NULL) {
1519 		smb_vfs_rele_all(sv);
1520 		smb_node_release(sv->si_root_smb_node);
1521 		sv->si_root_smb_node = NULL;
1522 	}
1523 }
1524