xref: /titanic_41/usr/src/uts/common/fs/smbsrv/smb_server.c (revision 29bd28862cfb8abbd3a0f0a4b17e08bbc3652836)
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 	smb_attr_t	ret_attr;
886 	char		last_comp[MAXNAMELEN];
887 	smb_request_t	*sr;
888 
889 	if (smb_server_lookup(&sv))
890 		return (EINVAL);
891 
892 	mutex_enter(&sv->sv_mutex);
893 	switch (sv->sv_state) {
894 	case SMB_SERVER_STATE_RUNNING:
895 		break;
896 	default:
897 		mutex_exit(&sv->sv_mutex);
898 		return (ENOTACTIVE);
899 	}
900 	mutex_exit(&sv->sv_mutex);
901 
902 	sr = smb_request_alloc(sv->sv_session, 0);
903 	if (sr == NULL) {
904 		smb_server_release(sv);
905 		return (ENOMEM);
906 	}
907 
908 	sr->user_cr = kcred;
909 
910 	error = smb_pathname_reduce(sr, kcred, ioc->path,
911 	    NULL, NULL, &dnode, last_comp);
912 
913 	if (error) {
914 		smb_request_free(sr);
915 		smb_server_release(sv);
916 		return (error);
917 	}
918 
919 	error = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS,
920 	    sv->si_root_smb_node, dnode, last_comp, &fnode, &ret_attr);
921 
922 	smb_node_release(dnode);
923 
924 	if (error) {
925 		smb_request_free(sr);
926 		smb_server_release(sv);
927 		return (error);
928 	}
929 
930 	ASSERT(fnode->vp && fnode->vp->v_vfsp);
931 
932 #ifdef SMB_ENFORCE_NODEV
933 	if (vfs_optionisset(fnode->vp->v_vfsp, MNTOPT_NODEVICES, NULL) == 0) {
934 		smb_node_release(fnode);
935 		smb_request_free(sr);
936 		smb_server_release(sv);
937 		return (EINVAL);
938 	}
939 #endif /* SMB_ENFORCE_NODEV */
940 
941 	if (!smb_vfs_hold(sv, fnode->vp->v_vfsp))
942 		error = ENOMEM;
943 
944 	/*
945 	 * The refcount on the smb_vfs has been incremented.
946 	 * If it wasn't already, a hold has also been taken
947 	 * on the root vnode of the file system.
948 	 */
949 
950 	smb_node_release(fnode);
951 	smb_request_free(sr);
952 	smb_server_release(sv);
953 	return (error);
954 }
955 
956 /*
957  * smb_server_share_unexport()
958  *
959  * This function is invoked when a share is disabled to disconnect trees
960  * and close files.  Cleaning up may involve VOP and/or VFS calls, which
961  * may conflict/deadlock with stuck threads if something is amiss with the
962  * file system.  Queueing the request for asynchronous processing allows the
963  * call to return immediately so that, if the unshare is being done in the
964  * context of a forced unmount, the forced unmount will always be able to
965  * proceed (unblocking stuck I/O and eventually allowing all blocked unshare
966  * processes to complete).
967  *
968  * The path lookup to find the root vnode of the VFS in question and the
969  * release of this vnode are done synchronously prior to any associated
970  * unmount.  Doing these asynchronous to an associated unmount could run
971  * the risk of a spurious EBUSY for a standard unmount or an EIO during
972  * the path lookup due to a forced unmount finishing first.
973  */
974 
975 int
976 smb_server_share_unexport(smb_ioc_share_t *ioc)
977 {
978 	smb_server_t	*sv;
979 	smb_request_t	*sr;
980 	smb_unexport_t	*ux;
981 	smb_node_t	*fnode = NULL;
982 	smb_node_t	*dnode;
983 	smb_attr_t	ret_attr;
984 	char		last_comp[MAXNAMELEN];
985 	int		rc;
986 
987 	if ((rc = smb_server_lookup(&sv)))
988 		return (rc);
989 
990 	mutex_enter(&sv->sv_mutex);
991 	switch (sv->sv_state) {
992 	case SMB_SERVER_STATE_RUNNING:
993 		break;
994 	default:
995 		mutex_exit(&sv->sv_mutex);
996 		return (ENOTACTIVE);
997 	}
998 	mutex_exit(&sv->sv_mutex);
999 
1000 	sr = smb_request_alloc(sv->sv_session, 0);
1001 
1002 	if (sr == NULL) {
1003 		smb_server_release(sv);
1004 		return (ENOMEM);
1005 	}
1006 
1007 	sr->user_cr = kcred;
1008 
1009 	rc = smb_pathname_reduce(sr, kcred, ioc->path, NULL, NULL,
1010 	    &dnode, last_comp);
1011 
1012 	if (rc) {
1013 		smb_request_free(sr);
1014 		smb_server_release(sv);
1015 		return (rc);
1016 	}
1017 
1018 	rc = smb_fsop_lookup(sr, kcred, SMB_FOLLOW_LINKS, sv->si_root_smb_node,
1019 	    dnode, last_comp, &fnode, &ret_attr);
1020 
1021 	smb_node_release(dnode);
1022 	smb_request_free(sr);
1023 
1024 	if (rc) {
1025 		smb_server_release(sv);
1026 		return (rc);
1027 	}
1028 
1029 	ASSERT(fnode->vp && fnode->vp->v_vfsp);
1030 
1031 	smb_vfs_rele(sv, fnode->vp->v_vfsp);
1032 
1033 	smb_node_release(fnode);
1034 
1035 	ux = kmem_cache_alloc(sv->si_cache_unexport, KM_SLEEP);
1036 
1037 	(void) strlcpy(ux->ux_sharename, ioc->name, MAXNAMELEN);
1038 
1039 	smb_slist_insert_tail(&sv->sv_unexport_list, ux);
1040 	smb_thread_signal(&sv->si_thread_unexport);
1041 
1042 	smb_server_release(sv);
1043 	return (0);
1044 }
1045 
1046 /*
1047  * smb_server_thread_unexport
1048  *
1049  * This function processes the unexport event list and disconnects shares
1050  * asynchronously.  The function executes as a zone-specific thread.
1051  *
1052  * The server arg passed in is safe to use without a reference count, because
1053  * the server cannot be deleted until smb_thread_stop()/destroy() return,
1054  * which is also when the thread exits.
1055  */
1056 
1057 static void
1058 smb_server_thread_unexport(smb_thread_t *thread, void *arg)
1059 {
1060 	smb_server_t *sv = (smb_server_t *)arg;
1061 	smb_unexport_t	*ux;
1062 
1063 	while (smb_thread_continue(thread)) {
1064 		while ((ux = list_head(&sv->sv_unexport_list.sl_list))
1065 		    != NULL) {
1066 			smb_slist_remove(&sv->sv_unexport_list, ux);
1067 			smb_server_disconnect_share(ux->ux_sharename, sv);
1068 			kmem_cache_free(sv->si_cache_unexport, ux);
1069 		}
1070 	}
1071 }
1072 
1073 /*
1074  * This is a special interface that will be utilized by ZFS to cause a share to
1075  * be added/removed.
1076  *
1077  * arg is either a lmshare_info_t or share_name from userspace.
1078  * It will need to be copied into the kernel.   It is lmshare_info_t
1079  * for add operations and share_name for delete operations.
1080  */
1081 int
1082 smb_server_share(void *arg, boolean_t add_share)
1083 {
1084 	smb_server_t	*sv;
1085 	int		rc;
1086 
1087 	rc = smb_server_lookup(&sv);
1088 	if (rc == 0) {
1089 		mutex_enter(&sv->sv_mutex);
1090 		switch (sv->sv_state) {
1091 		case SMB_SERVER_STATE_RUNNING:
1092 			mutex_exit(&sv->sv_mutex);
1093 			(void) smb_kshare_upcall(sv->sv_lmshrd, arg, add_share);
1094 			break;
1095 		default:
1096 			mutex_exit(&sv->sv_mutex);
1097 			break;
1098 		}
1099 		smb_server_release(sv);
1100 	}
1101 	return (0);
1102 }
1103 
1104 /*
1105  * *****************************************************************************
1106  * **************** Functions called from the internal layers ******************
1107  * *****************************************************************************
1108  *
1109  * These functions are provided the relevant smb server by the caller.
1110  */
1111 
1112 void
1113 smb_server_reconnection_check(smb_server_t *sv, smb_session_t *session)
1114 {
1115 	ASSERT(sv == session->s_server);
1116 
1117 	smb_session_reconnection_check(&sv->sv_nbt_daemon.ld_session_list,
1118 	    session);
1119 	smb_session_reconnection_check(&sv->sv_tcp_daemon.ld_session_list,
1120 	    session);
1121 }
1122 
1123 void
1124 smb_server_get_cfg(smb_server_t *sv, smb_kmod_cfg_t *cfg)
1125 {
1126 	rw_enter(&sv->sv_cfg_lock, RW_READER);
1127 	bcopy(&sv->sv_cfg, cfg, sizeof (*cfg));
1128 	rw_exit(&sv->sv_cfg_lock);
1129 }
1130 
1131 /*
1132  * *****************************************************************************
1133  * *************************** Static Functions ********************************
1134  * *****************************************************************************
1135  */
1136 
1137 static void
1138 smb_server_timers(smb_thread_t *thread, void *arg)
1139 {
1140 	smb_server_t	*sv = (smb_server_t *)arg;
1141 
1142 	ASSERT(sv != NULL);
1143 
1144 	while (smb_thread_continue_timedwait(thread, 1 /* Seconds */)) {
1145 		smb_session_timers(&sv->sv_nbt_daemon.ld_session_list);
1146 		smb_session_timers(&sv->sv_tcp_daemon.ld_session_list);
1147 	}
1148 }
1149 
1150 /*
1151  * smb_server_kstat_init
1152  */
1153 static int
1154 smb_server_kstat_init(smb_server_t *sv)
1155 {
1156 	(void) snprintf(sv->sv_ksp_name, sizeof (sv->sv_ksp_name), "%s%d",
1157 	    SMBSRV_KSTAT_NAME, sv->sv_zid);
1158 
1159 	sv->sv_ksp = kstat_create(SMBSRV_KSTAT_MODULE, 0, sv->sv_ksp_name,
1160 	    SMBSRV_KSTAT_CLASS, KSTAT_TYPE_NAMED,
1161 	    sizeof (sv->sv_ks_data) / sizeof (kstat_named_t),
1162 	    KSTAT_FLAG_VIRTUAL);
1163 
1164 	if (sv->sv_ksp) {
1165 		(void) strlcpy(sv->sv_ks_data.open_files.name, "open_files",
1166 		    sizeof (sv->sv_ks_data.open_files.name));
1167 		sv->sv_ks_data.open_files.data_type = KSTAT_DATA_UINT32;
1168 		(void) strlcpy(sv->sv_ks_data.open_trees.name, "connections",
1169 		    sizeof (sv->sv_ks_data.open_trees.name));
1170 		sv->sv_ks_data.open_trees.data_type = KSTAT_DATA_UINT32;
1171 		(void) strlcpy(sv->sv_ks_data.open_users.name, "sessions",
1172 		    sizeof (sv->sv_ks_data.open_users.name));
1173 		sv->sv_ks_data.open_users.data_type = KSTAT_DATA_UINT32;
1174 
1175 		mutex_init(&sv->sv_ksp_mutex, NULL, MUTEX_DEFAULT, NULL);
1176 		sv->sv_ksp->ks_lock = &sv->sv_ksp_mutex;
1177 		sv->sv_ksp->ks_data = (void *)&sv->sv_ks_data;
1178 		sv->sv_ksp->ks_update = smb_server_kstat_update_info;
1179 		kstat_install(sv->sv_ksp);
1180 	}
1181 
1182 	/* create and initialize smb kstats - smb_dispatch stats */
1183 	smb_dispatch_kstat_init();
1184 
1185 	return (0);
1186 }
1187 
1188 /*
1189  * smb_server_kstat_fini
1190  */
1191 static void
1192 smb_server_kstat_fini(smb_server_t *sv)
1193 {
1194 	if (sv->sv_ksp) {
1195 		kstat_delete(sv->sv_ksp);
1196 		mutex_destroy(&sv->sv_ksp_mutex);
1197 		sv->sv_ksp = NULL;
1198 	}
1199 	smb_dispatch_kstat_fini();
1200 }
1201 
1202 /* ARGSUSED */
1203 static int
1204 smb_server_kstat_update_info(kstat_t *ksp, int rw)
1205 {
1206 	smb_server_t	*sv;
1207 
1208 	if (rw == KSTAT_WRITE) {
1209 		return (EACCES);
1210 	} else {
1211 		ASSERT(MUTEX_HELD(ksp->ks_lock));
1212 
1213 		_NOTE(LINTED("pointer cast may result in improper alignment"))
1214 		sv = (smb_server_t *)((uint8_t *)(ksp->ks_data) -
1215 		    offsetof(smb_server_t, sv_ks_data));
1216 
1217 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1218 
1219 		sv->sv_ks_data.open_files.value.ui32 = sv->sv_open_files;
1220 		sv->sv_ks_data.open_trees.value.ui32 = sv->sv_open_trees;
1221 		sv->sv_ks_data.open_users.value.ui32 = sv->sv_open_users;
1222 	}
1223 	return (0);
1224 }
1225 
1226 /*
1227  * smb_server_stop
1228  *
1229  * The mutex of the server must have been entered before calling this function.
1230  */
1231 static void
1232 smb_server_stop(smb_server_t *sv)
1233 {
1234 	ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1235 
1236 	smb_opipe_door_close();
1237 	smb_thread_stop(&sv->si_thread_timers);
1238 	smb_thread_stop(&sv->si_thread_unexport);
1239 	smb_kdoor_clnt_close();
1240 	smb_kshare_fini(sv->sv_lmshrd);
1241 	sv->sv_lmshrd = NULL;
1242 	smb_server_fsop_stop(sv);
1243 
1244 	if (sv->sv_session) {
1245 		smb_session_delete(sv->sv_session);
1246 		sv->sv_session = NULL;
1247 	}
1248 
1249 	if (sv->sv_thread_pool) {
1250 		taskq_destroy(sv->sv_thread_pool);
1251 		sv->sv_thread_pool = NULL;
1252 	}
1253 }
1254 
1255 static int
1256 smb_server_listen(
1257     smb_server_t		*sv,
1258     smb_listener_daemon_t	*ld,
1259     in_port_t			port,
1260     int				family,
1261     int				pthread_create_error)
1262 {
1263 	int			rc;
1264 	ksocket_t		s_so;
1265 	const uint32_t		on = 1;
1266 	const uint32_t		off = 0;
1267 	smb_session_t		*session;
1268 
1269 	if (pthread_create_error) {
1270 		/*
1271 		 * Delete the last session created. The user space thread
1272 		 * creation failed.
1273 		 */
1274 		smb_session_list_delete_tail(&ld->ld_session_list);
1275 	}
1276 
1277 	if (ld->ld_so == NULL) {
1278 		/* First time listener */
1279 		if (family == AF_INET) {
1280 			ld->ld_sin.sin_family = (uint32_t)family;
1281 			ld->ld_sin.sin_port = htons(port);
1282 			ld->ld_sin.sin_addr.s_addr = htonl(INADDR_ANY);
1283 		} else {
1284 			ld->ld_sin6.sin6_family = (uint32_t)family;
1285 			ld->ld_sin6.sin6_port = htons(port);
1286 			(void) memset(&ld->ld_sin6.sin6_addr.s6_addr, 0,
1287 			    sizeof (ld->ld_sin6.sin6_addr.s6_addr));
1288 		}
1289 		ld->ld_so = smb_socreate(family, SOCK_STREAM, 0);
1290 		if (ld->ld_so) {
1291 
1292 			(void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET,
1293 			    SO_MAC_EXEMPT, &off, sizeof (off), CRED());
1294 			(void) ksocket_setsockopt(ld->ld_so, SOL_SOCKET,
1295 			    SO_REUSEADDR, &on, sizeof (on), CRED());
1296 
1297 			if (family == AF_INET) {
1298 				rc = ksocket_bind(ld->ld_so,
1299 				    (struct sockaddr *)&ld->ld_sin,
1300 				    sizeof (ld->ld_sin), CRED());
1301 			} else {
1302 				rc = ksocket_bind(ld->ld_so,
1303 				    (struct sockaddr *)&ld->ld_sin6,
1304 				    sizeof (ld->ld_sin6), CRED());
1305 			}
1306 			if (rc == 0) {
1307 				rc =  ksocket_listen(ld->ld_so, 20, CRED());
1308 				if (rc < 0) {
1309 					cmn_err(CE_WARN,
1310 					    "Port %d: listen failed", port);
1311 					smb_soshutdown(ld->ld_so);
1312 					smb_sodestroy(ld->ld_so);
1313 					ld->ld_so = NULL;
1314 					return (rc);
1315 				}
1316 			} else {
1317 				cmn_err(CE_WARN,
1318 				    "Port %d: bind failed", port);
1319 				smb_soshutdown(ld->ld_so);
1320 				smb_sodestroy(ld->ld_so);
1321 				ld->ld_so = NULL;
1322 				return (rc);
1323 			}
1324 		} else {
1325 			cmn_err(CE_WARN,
1326 			    "Port %d: socket create failed", port);
1327 			return (ENOMEM);
1328 		}
1329 	}
1330 
1331 	DTRACE_PROBE1(so__wait__accept, struct sonode *, ld->ld_so);
1332 
1333 	for (;;) {
1334 		rc = ksocket_accept(ld->ld_so, NULL, NULL, &s_so, CRED());
1335 		if (rc == 0) {
1336 			uint32_t	txbuf_size = 128*1024;
1337 			uint32_t	on = 1;
1338 
1339 			DTRACE_PROBE1(so__accept, struct sonode *, s_so);
1340 
1341 			(void) ksocket_setsockopt(s_so, IPPROTO_TCP,
1342 			    TCP_NODELAY, &on, sizeof (on), CRED());
1343 			(void) ksocket_setsockopt(s_so, SOL_SOCKET,
1344 			    SO_KEEPALIVE, &on, sizeof (on), CRED());
1345 			(void) ksocket_setsockopt(s_so, SOL_SOCKET, SO_SNDBUF,
1346 			    (const void *)&txbuf_size, sizeof (txbuf_size),
1347 			    CRED());
1348 			/*
1349 			 * Create a session for this connection.
1350 			 */
1351 			session = smb_session_create(s_so, port, sv, family);
1352 			if (session) {
1353 				smb_session_list_append(&ld->ld_session_list,
1354 				    session);
1355 				break;
1356 			} else {
1357 				smb_soshutdown(s_so);
1358 				smb_sodestroy(s_so);
1359 			}
1360 			continue;
1361 		}
1362 		smb_session_list_signal(&ld->ld_session_list);
1363 		smb_soshutdown(ld->ld_so);
1364 		smb_sodestroy(ld->ld_so);
1365 		ld->ld_so = NULL;
1366 		break;
1367 	}
1368 
1369 	return (rc);
1370 }
1371 
1372 /*
1373  * smb_server_lookup
1374  *
1375  * This function tries to find the server associated with the zone of the
1376  * caller.
1377  */
1378 static int
1379 smb_server_lookup(smb_server_t **psv)
1380 {
1381 	zoneid_t	zid;
1382 	smb_server_t	*sv;
1383 
1384 	zid = getzoneid();
1385 
1386 	smb_llist_enter(&smb_servers, RW_READER);
1387 	sv = smb_llist_head(&smb_servers);
1388 	while (sv) {
1389 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1390 		if (sv->sv_zid == zid) {
1391 			mutex_enter(&sv->sv_mutex);
1392 			if (sv->sv_state != SMB_SERVER_STATE_DELETING) {
1393 				sv->sv_refcnt++;
1394 				mutex_exit(&sv->sv_mutex);
1395 				smb_llist_exit(&smb_servers);
1396 				*psv = sv;
1397 				return (0);
1398 			}
1399 			mutex_exit(&sv->sv_mutex);
1400 			break;
1401 		}
1402 		sv = smb_llist_next(&smb_servers, sv);
1403 	}
1404 	smb_llist_exit(&smb_servers);
1405 	return (EPERM);
1406 }
1407 
1408 /*
1409  * smb_server_release
1410  *
1411  * This function decrements the reference count of the server and signals its
1412  * condition variable if the state of the server is SMB_SERVER_STATE_DELETING.
1413  */
1414 static void
1415 smb_server_release(smb_server_t *sv)
1416 {
1417 	ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1418 
1419 	mutex_enter(&sv->sv_mutex);
1420 	ASSERT(sv->sv_refcnt);
1421 	sv->sv_refcnt--;
1422 	if ((sv->sv_refcnt == 0) && (sv->sv_state == SMB_SERVER_STATE_DELETING))
1423 		cv_signal(&sv->sv_cv);
1424 	mutex_exit(&sv->sv_mutex);
1425 }
1426 
1427 static int
1428 smb_server_ulist_geti(smb_session_list_t *se, uint32_t cookie,
1429     uint8_t *buf, uint32_t buf_len, uint_t *pbe)
1430 {
1431 	smb_session_t *sn = NULL;
1432 	smb_user_t *user;
1433 	smb_llist_t *ulist;
1434 	smb_opipe_context_t ctx;
1435 	uint_t bytes_encoded;
1436 	int rc = 0;
1437 	int cnt = 0;
1438 
1439 	rw_enter(&se->se_lock, RW_READER);
1440 	sn = list_head(&se->se_act.lst);
1441 	while ((sn != NULL) && (rc == 0)) {
1442 		ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
1443 		ulist = &sn->s_user_list;
1444 		smb_llist_enter(ulist, RW_READER);
1445 		user = smb_llist_head(ulist);
1446 		while ((user != NULL) && (rc == 0)) {
1447 			ASSERT(user->u_magic == SMB_USER_MAGIC);
1448 			mutex_enter(&user->u_mutex);
1449 			if ((user->u_state == SMB_USER_STATE_LOGGED_IN) &&
1450 			    (cookie == 0)) {
1451 				smb_user_context_init(user, &ctx);
1452 				rc = smb_opipe_context_encode(&ctx, buf,
1453 				    buf_len, &bytes_encoded);
1454 				smb_user_context_fini(&ctx);
1455 				if (rc == 0) {
1456 					*pbe += bytes_encoded;
1457 					buf += bytes_encoded;
1458 					buf_len -= bytes_encoded;
1459 					cnt++;
1460 				}
1461 			}
1462 			mutex_exit(&user->u_mutex);
1463 			user = smb_llist_next(ulist, user);
1464 			if (cookie > 0)
1465 				cookie--;
1466 		}
1467 		smb_llist_exit(ulist);
1468 		sn = list_next(&se->se_act.lst, sn);
1469 	}
1470 	rw_exit(&se->se_lock);
1471 	return (cnt);
1472 }
1473 
1474 static void
1475 smb_server_store_cfg(smb_server_t *sv, smb_ioc_cfg_t *ioc)
1476 {
1477 	if (ioc->maxconnections == 0)
1478 		ioc->maxconnections = 0xFFFFFFFF;
1479 
1480 	smb_session_correct_keep_alive_values(
1481 	    &sv->sv_nbt_daemon.ld_session_list, ioc->keepalive);
1482 	smb_session_correct_keep_alive_values(
1483 	    &sv->sv_tcp_daemon.ld_session_list, ioc->keepalive);
1484 
1485 	sv->sv_cfg.skc_maxworkers = ioc->maxworkers;
1486 	sv->sv_cfg.skc_maxconnections = ioc->maxconnections;
1487 	sv->sv_cfg.skc_keepalive = ioc->keepalive;
1488 	sv->sv_cfg.skc_restrict_anon = ioc->restrict_anon;
1489 	sv->sv_cfg.skc_signing_enable = ioc->signing_enable;
1490 	sv->sv_cfg.skc_signing_required = ioc->signing_required;
1491 	sv->sv_cfg.skc_oplock_enable = ioc->oplock_enable;
1492 	sv->sv_cfg.skc_sync_enable = ioc->sync_enable;
1493 	sv->sv_cfg.skc_secmode = ioc->secmode;
1494 	sv->sv_cfg.skc_ipv6_enable = ioc->ipv6_enable;
1495 	(void) strlcpy(sv->sv_cfg.skc_nbdomain, ioc->nbdomain,
1496 	    sizeof (sv->sv_cfg.skc_nbdomain));
1497 	(void) strlcpy(sv->sv_cfg.skc_fqdn, ioc->fqdn,
1498 	    sizeof (sv->sv_cfg.skc_fqdn));
1499 	(void) strlcpy(sv->sv_cfg.skc_hostname, ioc->hostname,
1500 	    sizeof (sv->sv_cfg.skc_hostname));
1501 	(void) strlcpy(sv->sv_cfg.skc_system_comment, ioc->system_comment,
1502 	    sizeof (sv->sv_cfg.skc_system_comment));
1503 }
1504 
1505 static int
1506 smb_server_fsop_start(smb_server_t *sv)
1507 {
1508 	int	error;
1509 
1510 	error = smb_node_root_init(rootdir, sv, &sv->si_root_smb_node);
1511 	if (error != 0)
1512 		sv->si_root_smb_node = NULL;
1513 
1514 	return (error);
1515 }
1516 
1517 static void
1518 smb_server_fsop_stop(smb_server_t *sv)
1519 {
1520 	if (sv->si_root_smb_node != NULL) {
1521 		smb_vfs_rele_all(sv);
1522 		smb_node_release(sv->si_root_smb_node);
1523 		sv->si_root_smb_node = NULL;
1524 	}
1525 }
1526