xref: /titanic_41/usr/src/uts/common/fs/smbsrv/smb_server.c (revision eb0cc229f19c437a6b538d3ac0d0443268290b7e)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * General Structures Layout
30  * -------------------------
31  *
32  * This is a simplified diagram showing the relationship between most of the
33  * main structures.
34  *
35  * +-------------------+
36  * |     SMB_SERVER    |
37  * +-------------------+
38  *          |
39  *          |
40  *          v
41  * +-------------------+       +-------------------+      +-------------------+
42  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
43  * +-------------------+       +-------------------+      +-------------------+
44  *          |
45  *          |
46  *          v
47  * +-------------------+       +-------------------+      +-------------------+
48  * |       USER        |<----->|       USER        |......|       USER        |
49  * +-------------------+       +-------------------+      +-------------------+
50  *          |
51  *          |
52  *          v
53  * +-------------------+       +-------------------+      +-------------------+
54  * |       TREE        |<----->|       TREE        |......|       TREE        |
55  * +-------------------+       +-------------------+      +-------------------+
56  *      |         |
57  *      |         |
58  *      |         v
59  *      |     +-------+       +-------+      +-------+
60  *      |     | OFILE |<----->| OFILE |......| OFILE |
61  *      |     +-------+       +-------+      +-------+
62  *      |
63  *      |
64  *      v
65  *  +-------+       +------+      +------+
66  *  | ODIR  |<----->| ODIR |......| ODIR |
67  *  +-------+       +------+      +------+
68  *
69  *
70  * Module Interface Overview
71  * -------------------------
72  *
73  *
74  *	    +===================================+
75  *	    |		 smbd daemon		|
76  *	    +===================================+
77  *	      |		     |		      ^
78  *	      |		     |		      |
79  * User	      |		     |		      |
80  * -----------|--------------|----------------|--------------------------------
81  * Kernel     |		     |		      |
82  *            |		     |		      |
83  *	      |		     |		      |
84  *  +=========|==============|================|=================+
85  *  |	      v		     v		      |			|
86  *  | +-----------+ +--------------------+ +------------------+ |
87  *  | |     IO    | | Kernel Door Server | | User Door Servers|	|
88  *  | | Interface | |     Interface      | |   Interface      | |
89  *  | +-----------+ +--------------------+ +------------------+ |
90  *  |		|	     |		      ^		^	|
91  *  |		v	     v		      |		|	|    +=========+
92  *  |	     +-----------------------------------+	|	|    |	       |
93  *  |	     + SMB Server Management (this file) |<------------------|	 ZFS   |
94  *  |	     +-----------------------------------+	|	|    |	       |
95  *  |							|	|    |  Module |
96  *  |	     +-----------------------------------+	|	|    |	       |
97  *  |	     +     SMB Server Internal Layers    |------+	|    +=========+
98  *  |	     +-----------------------------------+		|
99  *  |								|
100  *  |								|
101  *  +===========================================================+
102  *
103  *
104  * Server State Machine
105  * --------------------
106  *                                  |
107  *                                  | T0
108  *                                  |
109  *                                  v
110  *                    +-----------------------------+
111  *		      |   SMB_SERVER_STATE_CREATED  |
112  *		      +-----------------------------+
113  *				    |
114  *				    | T1
115  *				    |
116  *				    v
117  *		      +-----------------------------+
118  *		      | SMB_SERVER_STATE_CONFIGURED |
119  *		      +-----------------------------+
120  *				    |
121  *				    | T2
122  *				    |
123  *				    v
124  *		      +-----------------------------+
125  *		      |  SMB_SERVER_STATE_RUNNING   |
126  *		      +-----------------------------+
127  *				    |
128  *				    | T3
129  *				    |
130  *				    v
131  *		      +-----------------------------+
132  *		      |  SMB_SERVER_STATE_DELETING  |
133  *                    +-----------------------------+
134  *				    |
135  *				    |
136  *				    |
137  *				    v
138  *
139  * States
140  * ------
141  *
142  * SMB_SERVER_STATE_CREATED
143  *
144  *    This is the state of the server just after creation.
145  *
146  * SMB_SERVER_STATE_CONFIGURED
147  *
148  *    The server has been configured.
149  *
150  * SMB_SERVER_STATE_RUNNING
151  *
152  *    The server has been started. While in this state the threads listening on
153  *    the sockets car be started. The smbd daemon does so through an Ioctl:
154  *
155  *	smb_drv_ioctl(SMB_IOC_NBT_LISTEN) --> smb_server_nbt_listen()
156  *	smb_drv_ioctl(SMB_IOC_TCP_LISTEN) --> smb_server_nbt_listen()
157  *
158  *    When a client establishes a connection the thread listening leaves
159  *    temporarily the kernel. While in user space it creates a thread for the
160  *    new session. It then returns to kernel with the result of the thread
161  *    creation. If the creation failed the new session context is destroyed
162  *    before returning listening.
163  *
164  *    The new created thread enters the kernel though an Ioctl:
165  *
166  *	smb_drv_ioctl(SMB_IOC_NBT_RECEIVE) --> smb_server_nbt_receive()
167  *	smb_drv_ioctl(SMB_IOC_TCP_RECEIVE) --> smb_server_tcp_receive()
168  *
169  * SMB_SERVER_STATE_STOPPING
170  *
171  *    The threads listening on the NBT and TCP sockets are being terminated.
172  *
173  *
174  * Transitions
175  * -----------
176  *
177  * Transition T0
178  *
179  *    The daemon smbd triggers its creation by opening the smbsrv device. If
180  *    the zone where the daemon lives doesn't have an smb server yet it is
181  *    created.
182  *
183  *		smb_drv_open() --> smb_server_create()
184  *
185  * Transition T1
186  *
187  *    This transition occurs in smb_server_configure(). It is triggered by the
188  *    daemon through an Ioctl.
189  *
190  *	smb_drv_ioctl(SMB_IOC_CONFIG) --> smb_server_configure()
191  *
192  * Transition T2
193  *
194  *    This transition occurs in smb_server_start(). It is triggered by the
195  *    daemon through an Ioctl.
196  *
197  *	smb_drv_ioctl(SMB_IOC_START) --> smb_server_start()
198  *
199  * Transition T3
200  *
201  *    This transition occurs in smb_server_delete(). It is triggered by the
202  *    daemon when closing the smbsrv device
203  *
204  *		smb_drv_close() --> smb_server_delete()
205  *
206  * Comments
207  * --------
208  *
209  * This files assumes that there will one SMB server per zone. For now the
210  * smb server works only in global zone. There's nothing in this file preventing
211  * an smb server from being created in a non global zone. That limitation is
212  * enforced in user space.
213  */
214 
215 #include <sys/strsubr.h>
216 #include <sys/cmn_err.h>
217 #include <sys/priv.h>
218 #include <sys/socketvar.h>
219 #include <sys/zone.h>
220 #include <smbsrv/smb_kproto.h>
221 #include <smbsrv/netbios.h>
222 #include <smbsrv/smb_incl.h>
223 #include <smbsrv/cifs.h>
224 #include <smbsrv/smb_fsops.h>
225 #include <smbsrv/lmshare.h>
226 #include <smbsrv/smb_door_svc.h>
227 #include <smbsrv/smb_kstat.h>
228 
229 extern void smb_dispatch_kstat_init(void);
230 extern void smb_dispatch_kstat_fini(void);
231 extern void smb_reply_notify_change_request(smb_request_t *);
232 
233 static int smb_server_kstat_init(smb_server_t *);
234 static void smb_server_kstat_fini(smb_server_t *);
235 static int smb_server_kstat_update_info(kstat_t *, int);
236 static void smb_server_timers(smb_thread_t *, void *);
237 static int smb_server_listen(smb_server_t *, smb_listener_daemon_t *,
238     in_port_t, int);
239 static int smb_server_lookup(smb_server_t **);
240 static void smb_server_release(smb_server_t *);
241 static int smb_server_ulist_geti(smb_session_list_t *, int, smb_dr_user_ctx_t *,
242     int);
243 static void smb_server_store_cfg(smb_server_t *, smb_kmod_cfg_t *);
244 static void smb_server_stop(smb_server_t *);
245 static int smb_server_fsop_start(smb_server_t *);
246 static void smb_server_fsop_stop(smb_server_t *);
247 
248 static smb_llist_t	smb_servers;
249 
250 /*
251  * *****************************************************************************
252  * **************** Functions called from the device interface *****************
253  * *****************************************************************************
254  *
255  * These functions determine the relevant smb server to which the call apply.
256  */
257 
258 /*
259  * smb_server_svc_init
260  *
261  * This function must called from smb_drv_attach().
262  */
263 int
264 smb_server_svc_init(void)
265 {
266 	int	rc = 0;
267 
268 	while (rc == 0) {
269 		if (rc = smb_vop_init())
270 			continue;
271 		if (rc = smb_node_init())
272 			break;
273 		if (rc = smb_fem_init())
274 			break;
275 		if (rc = smb_notify_init())
276 			break;
277 		if (rc = smb_kdoor_srv_start())
278 			break;
279 		smb_llist_constructor(&smb_servers, sizeof (smb_server_t),
280 		    offsetof(smb_server_t, sv_lnd));
281 		return (0);
282 	}
283 	smb_notify_fini();
284 	smb_fem_fini();
285 	smb_node_fini();
286 	smb_vop_fini();
287 	return (rc);
288 }
289 
290 /*
291  * smb_server_svc_fini
292  *
293  * This function must called from smb_drv_detach(). It will fail if servers
294  * still exist.
295  */
296 int
297 smb_server_svc_fini(void)
298 {
299 	int	rc = EBUSY;
300 
301 	if (smb_llist_get_count(&smb_servers) == 0) {
302 		smb_kdoor_srv_stop();
303 		smb_notify_fini();
304 		smb_fem_fini();
305 		smb_node_fini();
306 		smb_vop_fini();
307 		smb_llist_destructor(&smb_servers);
308 		rc = 0;
309 	}
310 	return (rc);
311 }
312 
313 /*
314  * smb_server_create
315  *
316  * This function will fail if there's already a server associated with the
317  * caller's zone.
318  */
319 int
320 smb_server_create(void)
321 {
322 	zoneid_t	zid;
323 	smb_server_t	*sv;
324 
325 	zid = getzoneid();
326 
327 	smb_llist_enter(&smb_servers, RW_WRITER);
328 	sv = smb_llist_head(&smb_servers);
329 	while (sv) {
330 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
331 		if (sv->sv_zid == zid) {
332 			smb_llist_exit(&smb_servers);
333 			return (EEXIST);
334 		}
335 		sv = smb_llist_next(&smb_servers, sv);
336 	}
337 
338 	sv = kmem_zalloc(sizeof (smb_server_t), KM_NOSLEEP);
339 	if (sv == NULL) {
340 		smb_llist_exit(&smb_servers);
341 		return (ENOMEM);
342 	}
343 
344 	smb_llist_constructor(&sv->sv_vfs_list, sizeof (smb_vfs_t),
345 	    offsetof(smb_vfs_t, sv_lnd));
346 
347 	smb_session_list_constructor(&sv->sv_nbt_daemon.ld_session_list);
348 	smb_session_list_constructor(&sv->sv_tcp_daemon.ld_session_list);
349 
350 	sv->si_cache_vfs = kmem_cache_create("smb_vfs_cache",
351 	    sizeof (smb_vfs_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
352 	sv->si_cache_request = kmem_cache_create("smb_request_cache",
353 	    sizeof (smb_request_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
354 	sv->si_cache_session = kmem_cache_create("smb_session_cache",
355 	    sizeof (smb_session_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
356 	sv->si_cache_user = kmem_cache_create("smb_user_cache",
357 	    sizeof (smb_user_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
358 	sv->si_cache_tree = kmem_cache_create("smb_tree_cache",
359 	    sizeof (smb_tree_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
360 	sv->si_cache_ofile = kmem_cache_create("smb_ofile_cache",
361 	    sizeof (smb_ofile_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
362 	sv->si_cache_odir = kmem_cache_create("smb_odir_cache",
363 	    sizeof (smb_odir_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
364 	sv->si_cache_node = kmem_cache_create("smb_node_cache",
365 	    sizeof (smb_node_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
366 
367 	smb_thread_init(&sv->si_thread_timers,
368 	    "smb_timers", smb_server_timers, sv,
369 	    NULL, NULL);
370 
371 	sv->sv_pid = curproc->p_pid;
372 
373 	smb_winpipe_init();
374 	(void) smb_server_kstat_init(sv);
375 
376 	mutex_init(&sv->sv_mutex, NULL, MUTEX_DEFAULT, NULL);
377 	cv_init(&sv->sv_cv, NULL, CV_DEFAULT, NULL);
378 	sv->sv_state = SMB_SERVER_STATE_CREATED;
379 	sv->sv_magic = SMB_SERVER_MAGIC;
380 	sv->sv_zid = zid;
381 
382 	smb_llist_insert_tail(&smb_servers, sv);
383 	smb_llist_exit(&smb_servers);
384 	return (0);
385 }
386 
387 /*
388  * smb_server_delete
389  *
390  * This function will delete the server passed in. It will make sure that all
391  * activity associated that server has ceased before destroying it.
392  */
393 int
394 smb_server_delete(void)
395 {
396 	smb_server_t	*sv;
397 	int		rc;
398 
399 	rc = smb_server_lookup(&sv);
400 	if (rc != 0)
401 		return (rc);
402 
403 	mutex_enter(&sv->sv_mutex);
404 	switch (sv->sv_state) {
405 	case SMB_SERVER_STATE_RUNNING:
406 	{
407 		boolean_t	nbt = B_FALSE;
408 		boolean_t	tcp = B_FALSE;
409 
410 		if (sv->sv_nbt_daemon.ld_kth) {
411 			tsignal(sv->sv_nbt_daemon.ld_kth, SIGINT);
412 			nbt = B_TRUE;
413 		}
414 		if (sv->sv_tcp_daemon.ld_kth) {
415 			tsignal(sv->sv_tcp_daemon.ld_kth, SIGINT);
416 			tcp = B_TRUE;
417 		}
418 		sv->sv_state = SMB_SERVER_STATE_DELETING;
419 		mutex_exit(&sv->sv_mutex);
420 		if (nbt)
421 			thread_join(sv->sv_nbt_daemon.ld_ktdid);
422 		if (tcp)
423 			thread_join(sv->sv_tcp_daemon.ld_ktdid);
424 		mutex_enter(&sv->sv_mutex);
425 		break;
426 	}
427 	case SMB_SERVER_STATE_CONFIGURED:
428 	case SMB_SERVER_STATE_CREATED:
429 		sv->sv_state = SMB_SERVER_STATE_DELETING;
430 		break;
431 	default:
432 		ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
433 		mutex_exit(&sv->sv_mutex);
434 		smb_server_release(sv);
435 		return (ENOTTY);
436 	}
437 
438 	ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
439 
440 	sv->sv_refcnt--;
441 	while (sv->sv_refcnt)
442 		cv_wait(&sv->sv_cv, &sv->sv_mutex);
443 
444 	mutex_exit(&sv->sv_mutex);
445 
446 	smb_llist_enter(&smb_servers, RW_WRITER);
447 	smb_llist_remove(&smb_servers, sv);
448 	smb_llist_exit(&smb_servers);
449 
450 	smb_server_stop(sv);
451 	rw_destroy(&sv->sv_cfg_lock);
452 	smb_winpipe_fini();
453 	smb_server_kstat_fini(sv);
454 	smb_llist_destructor(&sv->sv_vfs_list);
455 	kmem_cache_destroy(sv->si_cache_vfs);
456 	kmem_cache_destroy(sv->si_cache_request);
457 	kmem_cache_destroy(sv->si_cache_session);
458 	kmem_cache_destroy(sv->si_cache_user);
459 	kmem_cache_destroy(sv->si_cache_tree);
460 	kmem_cache_destroy(sv->si_cache_ofile);
461 	kmem_cache_destroy(sv->si_cache_odir);
462 	kmem_cache_destroy(sv->si_cache_node);
463 
464 	taskq_destroy(sv->sv_thread_pool);
465 	smb_thread_destroy(&sv->si_thread_timers);
466 	mutex_destroy(&sv->sv_mutex);
467 	cv_destroy(&sv->sv_cv);
468 	sv->sv_magic = 0;
469 	kmem_free(sv, sizeof (smb_server_t));
470 
471 	return (0);
472 }
473 
474 /*
475  * smb_server_configure
476  */
477 int
478 smb_server_configure(smb_kmod_cfg_t *cfg)
479 {
480 	int		rc = 0;
481 	smb_server_t	*sv;
482 
483 	rc = smb_server_lookup(&sv);
484 	if (rc)
485 		return (rc);
486 
487 	mutex_enter(&sv->sv_mutex);
488 	switch (sv->sv_state) {
489 	case SMB_SERVER_STATE_CREATED:
490 		smb_server_store_cfg(sv, cfg);
491 		sv->sv_state = SMB_SERVER_STATE_CONFIGURED;
492 		break;
493 
494 	case SMB_SERVER_STATE_CONFIGURED:
495 		smb_server_store_cfg(sv, cfg);
496 		break;
497 
498 	case SMB_SERVER_STATE_RUNNING:
499 		rw_enter(&sv->sv_cfg_lock, RW_WRITER);
500 		smb_server_store_cfg(sv, cfg);
501 		rw_exit(&sv->sv_cfg_lock);
502 		break;
503 
504 	default:
505 		ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
506 		rc = EFAULT;
507 		break;
508 	}
509 	mutex_exit(&sv->sv_mutex);
510 
511 	smb_server_release(sv);
512 
513 	return (rc);
514 }
515 
516 /*
517  * smb_server_start
518  */
519 int
520 smb_server_start(struct smb_io_start *io_start)
521 {
522 	int		rc = 0;
523 	smb_server_t	*sv;
524 
525 	rc = smb_server_lookup(&sv);
526 	if (rc)
527 		return (rc);
528 
529 	mutex_enter(&sv->sv_mutex);
530 	switch (sv->sv_state) {
531 	case SMB_SERVER_STATE_CONFIGURED:
532 
533 		sv->sv_thread_pool = taskq_create("smb_workers",
534 		    sv->sv_cfg.skc_maxworkers, SMB_WORKER_PRIORITY,
535 		    sv->sv_cfg.skc_maxworkers, INT_MAX,
536 		    TASKQ_DYNAMIC|TASKQ_PREPOPULATE);
537 
538 		sv->sv_session = smb_session_create(NULL, 0, sv);
539 		if (sv->sv_session == NULL) {
540 			rc = ENOMEM;
541 			break;
542 		}
543 
544 		if (rc = smb_server_fsop_start(sv))
545 			break;
546 		ASSERT(sv->sv_lmshrd == NULL);
547 		sv->sv_lmshrd = lmshrd_kclient_init(io_start->lmshrd);
548 		if (sv->sv_lmshrd == NULL)
549 			break;
550 		if (rc = smb_kdoor_clnt_start(io_start->udoor))
551 			break;
552 		if (rc = smb_kdoor_srv_set_dwncall())
553 			break;
554 		if (rc = smb_thread_start(&sv->si_thread_timers))
555 			break;
556 		/*
557 		 * XXX We give up the NET_MAC_AWARE privilege because it keeps
558 		 * us from re-opening the connection when there are leftover TCP
559 		 * connections in TCPS_TIME_WAIT state.  There seem to be some
560 		 * security ramifications around reestablishing a connection
561 		 * while possessing the NET_MAC_AWARE privilege.
562 		 *
563 		 * This approach may cause problems when we try to support
564 		 * zones.  An alternative would be to retry the connection setup
565 		 * for a fixed period of time until the stale connections clear
566 		 * up but that implies we would be offline for a couple minutes
567 		 * every time the service is restarted with active connections.
568 		 */
569 		rc = setpflags(NET_MAC_AWARE, 0, CRED());
570 		if (rc) {
571 			cmn_err(CE_WARN,
572 			    "Cannot remove NET_MAC_AWARE privilege");
573 			break;
574 		}
575 		if (rc = smb_winpipe_open(io_start->winpipe)) {
576 			cmn_err(CE_WARN, "Cannot open winpipe door");
577 			break;
578 		}
579 		sv->sv_state = SMB_SERVER_STATE_RUNNING;
580 		mutex_exit(&sv->sv_mutex);
581 		smb_server_release(sv);
582 		return (0);
583 	default:
584 		ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
585 		    (sv->sv_state == SMB_SERVER_STATE_RUNNING) ||
586 		    (sv->sv_state == SMB_SERVER_STATE_DELETING));
587 		mutex_exit(&sv->sv_mutex);
588 		smb_server_release(sv);
589 		return (ENOTTY);
590 	}
591 
592 	smb_server_stop(sv);
593 	mutex_exit(&sv->sv_mutex);
594 	smb_server_release(sv);
595 	return (rc);
596 }
597 
598 /*
599  * smb_server_nbt_listen: SMB-over-NetBIOS service
600  *
601  * Traditional SMB service over NetBIOS (port 139), which requires
602  * that a NetBIOS session be established.
603  */
604 int
605 smb_server_nbt_listen(int error)
606 {
607 	smb_server_t	*sv;
608 	int		rc;
609 
610 	rc = smb_server_lookup(&sv);
611 	if (rc)
612 		return (rc);
613 
614 	mutex_enter(&sv->sv_mutex);
615 	switch (sv->sv_state) {
616 	case SMB_SERVER_STATE_RUNNING:
617 		if ((sv->sv_nbt_daemon.ld_kth != NULL) &&
618 		    (sv->sv_nbt_daemon.ld_kth != curthread)) {
619 			mutex_exit(&sv->sv_mutex);
620 			return (EACCES);
621 		} else {
622 			sv->sv_nbt_daemon.ld_kth = curthread;
623 			sv->sv_nbt_daemon.ld_ktdid = curthread->t_did;
624 		}
625 		break;
626 	default:
627 		ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
628 		    (sv->sv_state == SMB_SERVER_STATE_CONFIGURED) ||
629 		    (sv->sv_state == SMB_SERVER_STATE_DELETING));
630 		mutex_exit(&sv->sv_mutex);
631 		smb_server_release(sv);
632 		return (EFAULT);
633 	}
634 	mutex_exit(&sv->sv_mutex);
635 
636 	rc = smb_server_listen(sv, &sv->sv_nbt_daemon, SSN_SRVC_TCP_PORT,
637 	    error);
638 
639 	if (rc) {
640 		mutex_enter(&sv->sv_mutex);
641 		sv->sv_nbt_daemon.ld_kth = NULL;
642 		mutex_exit(&sv->sv_mutex);
643 	}
644 
645 	smb_server_release(sv);
646 
647 	return (rc);
648 }
649 
650 int
651 smb_server_tcp_listen(int error)
652 {
653 	smb_server_t	*sv;
654 	int		rc;
655 
656 	rc = smb_server_lookup(&sv);
657 	if (rc)
658 		return (rc);
659 
660 	mutex_enter(&sv->sv_mutex);
661 	switch (sv->sv_state) {
662 	case SMB_SERVER_STATE_RUNNING:
663 		if ((sv->sv_tcp_daemon.ld_kth) &&
664 		    (sv->sv_tcp_daemon.ld_kth != curthread)) {
665 			mutex_exit(&sv->sv_mutex);
666 			return (EACCES);
667 		} else {
668 			sv->sv_tcp_daemon.ld_kth = curthread;
669 			sv->sv_tcp_daemon.ld_ktdid = curthread->t_did;
670 		}
671 		break;
672 	default:
673 		ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
674 		    (sv->sv_state == SMB_SERVER_STATE_CONFIGURED) ||
675 		    (sv->sv_state == SMB_SERVER_STATE_DELETING));
676 		mutex_exit(&sv->sv_mutex);
677 		return (EFAULT);
678 	}
679 	mutex_exit(&sv->sv_mutex);
680 
681 	rc = smb_server_listen(sv, &sv->sv_tcp_daemon, SMB_SRVC_TCP_PORT,
682 	    error);
683 
684 	if (rc) {
685 		mutex_enter(&sv->sv_mutex);
686 		sv->sv_tcp_daemon.ld_kth = NULL;
687 		mutex_exit(&sv->sv_mutex);
688 	}
689 
690 	smb_server_release(sv);
691 
692 	return (rc);
693 }
694 
695 /*
696  * smb_server_nbt_receive
697  */
698 int
699 smb_server_nbt_receive(void)
700 {
701 	int		rc;
702 	smb_server_t	*sv;
703 
704 	rc = smb_server_lookup(&sv);
705 	if (rc)
706 		return (rc);
707 
708 	rc = smb_session_daemon(&sv->sv_nbt_daemon.ld_session_list);
709 
710 	smb_server_release(sv);
711 
712 	return (rc);
713 }
714 
715 /*
716  * smb_server_tcp_receive
717  */
718 int
719 smb_server_tcp_receive(void)
720 {
721 	int		rc;
722 	smb_server_t	*sv;
723 
724 	rc = smb_server_lookup(&sv);
725 	if (rc)
726 		return (rc);
727 
728 	rc = smb_session_daemon(&sv->sv_tcp_daemon.ld_session_list);
729 
730 	smb_server_release(sv);
731 
732 	return (rc);
733 }
734 
735 int
736 smb_server_set_gmtoff(uint32_t goff)
737 {
738 	int		rc;
739 	smb_server_t	*sv;
740 
741 
742 	rc = smb_server_lookup(&sv);
743 	if (rc)
744 		return (rc);
745 
746 	sv->si_gmtoff = goff;
747 
748 	smb_server_release(sv);
749 
750 	return (rc);
751 }
752 
753 /*
754  * *****************************************************************************
755  * ****************** Functions called from the door interface *****************
756  * *****************************************************************************
757  *
758  * These functions determine the relevant smb server to which the call apply.
759  */
760 
761 uint32_t
762 smb_server_get_user_count(void)
763 {
764 	smb_server_t	*sv;
765 	uint32_t	counter = 0;
766 
767 	if (smb_server_lookup(&sv))
768 		return (0);
769 
770 	counter = (uint32_t)sv->sv_open_users;
771 
772 	smb_server_release(sv);
773 
774 	return (counter);
775 }
776 
777 uint32_t
778 smb_server_get_session_count(void)
779 {
780 	smb_server_t	*sv;
781 	uint32_t	counter = 0;
782 
783 	if (smb_server_lookup(&sv))
784 		return (0);
785 
786 	rw_enter(&sv->sv_nbt_daemon.ld_session_list.se_lock, RW_READER);
787 	counter = sv->sv_nbt_daemon.ld_session_list.se_act.count;
788 	rw_exit(&sv->sv_nbt_daemon.ld_session_list.se_lock);
789 	rw_enter(&sv->sv_tcp_daemon.ld_session_list.se_lock, RW_READER);
790 	counter += sv->sv_tcp_daemon.ld_session_list.se_act.count;
791 	rw_exit(&sv->sv_tcp_daemon.ld_session_list.se_lock);
792 
793 	smb_server_release(sv);
794 
795 	return (counter);
796 }
797 
798 /*
799  * smb_session_disconnect_share
800  *
801  * Disconnects the specified share. This function should be called after the
802  * share passed in has been made unavailable by the "share manager".
803  */
804 void
805 smb_server_disconnect_share(char *sharename)
806 {
807 	smb_server_t	*sv;
808 
809 	if (smb_server_lookup(&sv))
810 		return;
811 
812 	smb_session_disconnect_share(&sv->sv_nbt_daemon.ld_session_list,
813 	    sharename);
814 	smb_session_disconnect_share(&sv->sv_tcp_daemon.ld_session_list,
815 	    sharename);
816 
817 	smb_server_release(sv);
818 }
819 
820 void
821 smb_server_disconnect_volume(fs_desc_t *fsd)
822 {
823 	smb_server_t	*sv;
824 
825 	if (smb_server_lookup(&sv))
826 		return;
827 
828 	smb_session_disconnect_volume(&sv->sv_nbt_daemon.ld_session_list, fsd);
829 	smb_session_disconnect_volume(&sv->sv_tcp_daemon.ld_session_list, fsd);
830 
831 	smb_server_release(sv);
832 }
833 
834 int
835 smb_server_dr_ulist_get(int offset, smb_dr_ulist_t *dr_ulist, int max_cnt)
836 {
837 	smb_server_t	*sv;
838 
839 	if (!dr_ulist)
840 		return (-1);
841 
842 	if (smb_server_lookup(&sv))
843 		return (-1);
844 
845 	dr_ulist->dul_cnt =
846 	    smb_server_ulist_geti(&sv->sv_nbt_daemon.ld_session_list,
847 	    offset, dr_ulist->dul_users, max_cnt);
848 	dr_ulist->dul_cnt +=
849 	    smb_server_ulist_geti(&sv->sv_tcp_daemon.ld_session_list,
850 	    offset - dr_ulist->dul_cnt, &dr_ulist->dul_users[dr_ulist->dul_cnt],
851 	    max_cnt);
852 
853 	return (dr_ulist->dul_cnt);
854 }
855 
856 /*
857  * smb_server_share_export()
858  *
859  * This function handles kernel processing at share enable time.
860  *
861  * At share-enable time (LMSHRD_ADD), the file system corresponding to
862  * the share is checked for characteristics that are required for SMB
863  * sharing.  If this check passes, then a hold is taken on the root vnode
864  * of the file system (or a reference count on the corresponding smb_vfs_t
865  * is bumped), preventing an unmount.  (See smb_vfs_hold()).
866  */
867 
868 int
869 smb_server_share_export(char *path)
870 {
871 	smb_server_t	*sv;
872 	int		error;
873 	smb_node_t	*fnode = NULL;
874 	smb_node_t	*dnode;
875 	smb_attr_t	ret_attr;
876 	char		last_comp[MAXNAMELEN];
877 	smb_request_t	*sr;
878 
879 	if (smb_server_lookup(&sv))
880 		return (EINVAL);
881 
882 	sr = smb_request_alloc(sv->sv_session, 0);
883 	if (sr == NULL) {
884 		smb_server_release(sv);
885 		return (ENOMEM);
886 	}
887 
888 	sr->user_cr = kcred;
889 
890 	error = smb_pathname_reduce(sr, kcred, path, NULL, NULL, &dnode,
891 	    last_comp);
892 
893 	if (error) {
894 		smb_request_free(sr);
895 		smb_server_release(sv);
896 		return (error);
897 	}
898 
899 	error = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS, NULL, dnode,
900 	    last_comp, &fnode, &ret_attr, NULL, NULL);
901 
902 	smb_node_release(dnode);
903 
904 	if (error) {
905 		smb_request_free(sr);
906 		smb_server_release(sv);
907 		return (error);
908 	}
909 
910 	ASSERT(fnode->vp && fnode->vp->v_vfsp);
911 
912 #ifdef SMB_ENFORCE_NODEV
913 	if (vfs_optionisset(fnode->vp->v_vfsp, MNTOPT_NODEVICES, NULL) == 0)
914 		return (EINVAL);
915 #endif /* SMB_ENFORCE_NODEV */
916 
917 	if (!smb_vfs_hold(sv, fnode->vp->v_vfsp)) {
918 		smb_node_release(fnode);
919 		smb_request_free(sr);
920 		smb_server_release(sv);
921 		return (ENOMEM);
922 	}
923 
924 	/*
925 	 * The refcount on the smb_vfs has been incremented.
926 	 * If it wasn't already, a hold has also been taken
927 	 * on the root vnode of the file system.
928 	 */
929 
930 	smb_node_release(fnode);
931 	smb_request_free(sr);
932 	smb_server_release(sv);
933 	return (0);
934 }
935 
936 
937 
938 /*
939  * smb_server_share_unexport()
940  *
941  * This function handles kernel processing at share disable time.
942  *
943  * At share-disable time (LMSHRD_DELETE), the reference count on the
944  * corresponding smb_vfs_t is decremented.  If this is the last share
945  * on the file system, the hold on the root vnode of the file system
946  * will be released.  (See smb_vfs_rele().)
947  */
948 
949 int
950 smb_server_share_unexport(char *path, char *sharename)
951 {
952 	smb_server_t	*sv;
953 	int		error;
954 	smb_node_t	*fnode = NULL;
955 	smb_node_t	*dnode;
956 	smb_attr_t	ret_attr;
957 	char		last_comp[MAXNAMELEN];
958 	smb_request_t	*sr;
959 
960 	if (smb_server_lookup(&sv))
961 		return (EINVAL);
962 
963 	sr = smb_request_alloc(sv->sv_session, 0);
964 	if (sr == NULL) {
965 		smb_server_release(sv);
966 		return (ENOMEM);
967 	}
968 	sr->user_cr = kcred;
969 
970 	error = smb_pathname_reduce(sr, kcred, path, NULL, NULL, &dnode,
971 	    last_comp);
972 
973 	if (error) {
974 		smb_request_free(sr);
975 		smb_server_release(sv);
976 		return (error);
977 	}
978 
979 	error = smb_fsop_lookup(sr, kcred, SMB_FOLLOW_LINKS, NULL, dnode,
980 	    last_comp, &fnode, &ret_attr, NULL, NULL);
981 
982 	smb_node_release(dnode);
983 
984 	if (error) {
985 		smb_request_free(sr);
986 		smb_server_release(sv);
987 		return (error);
988 	}
989 
990 	ASSERT(fnode->vp && fnode->vp->v_vfsp);
991 
992 	smb_session_disconnect_share(&sv->sv_nbt_daemon.ld_session_list,
993 	    sharename);
994 	smb_session_disconnect_share(&sv->sv_tcp_daemon.ld_session_list,
995 	    sharename);
996 	smb_vfs_rele(sv, fnode->vp->v_vfsp);
997 	smb_node_release(fnode);
998 	smb_request_free(sr);
999 	smb_server_release(sv);
1000 	return (0);
1001 }
1002 
1003 /*
1004  * This is a special interface that will be utilized by ZFS to cause a share to
1005  * be added/removed.
1006  *
1007  * arg is either a lmshare_info_t or share_name from userspace.
1008  * It will need to be copied into the kernel.   It is lmshare_info_t
1009  * for add operations and share_name for delete operations.
1010  */
1011 int
1012 smb_server_share(void *arg, boolean_t add_share)
1013 {
1014 	smb_server_t	*sv;
1015 	int		rc;
1016 
1017 	rc = smb_server_lookup(&sv);
1018 	if (rc == 0) {
1019 		mutex_enter(&sv->sv_mutex);
1020 		if (sv->sv_state == SMB_SERVER_STATE_RUNNING) {
1021 			mutex_exit(&sv->sv_mutex);
1022 			rc = lmshrd_share_upcall(sv->sv_lmshrd, arg, add_share);
1023 		} else {
1024 			mutex_exit(&sv->sv_mutex);
1025 			rc = EPERM;
1026 		}
1027 		smb_server_release(sv);
1028 	}
1029 	return (rc);
1030 }
1031 
1032 /*
1033  * *****************************************************************************
1034  * **************** Functions called from the internal layers ******************
1035  * *****************************************************************************
1036  *
1037  * These functions are provided the relevant smb server by the caller.
1038  */
1039 
1040 void
1041 smb_server_reconnection_check(smb_server_t *sv, smb_session_t *session)
1042 {
1043 	ASSERT(sv == session->s_server);
1044 
1045 	smb_session_reconnection_check(&sv->sv_nbt_daemon.ld_session_list,
1046 	    session);
1047 	smb_session_reconnection_check(&sv->sv_tcp_daemon.ld_session_list,
1048 	    session);
1049 }
1050 
1051 void
1052 smb_server_get_cfg(smb_server_t *sv, smb_kmod_cfg_t *cfg)
1053 {
1054 	rw_enter(&sv->sv_cfg_lock, RW_READER);
1055 	bcopy(&sv->sv_cfg, cfg, sizeof (*cfg));
1056 	rw_exit(&sv->sv_cfg_lock);
1057 }
1058 
1059 /*
1060  * *****************************************************************************
1061  * *************************** Static Functions ********************************
1062  * *****************************************************************************
1063  */
1064 
1065 static void
1066 smb_server_timers(smb_thread_t *thread, void *arg)
1067 {
1068 	smb_server_t	*sv = (smb_server_t *)arg;
1069 
1070 	ASSERT(sv != NULL);
1071 
1072 	while (smb_thread_continue_timedwait(thread, 1 /* Seconds */)) {
1073 		smb_session_timers(&sv->sv_nbt_daemon.ld_session_list);
1074 		smb_session_timers(&sv->sv_tcp_daemon.ld_session_list);
1075 	}
1076 }
1077 
1078 /*
1079  * smb_server_kstat_init
1080  */
1081 static int
1082 smb_server_kstat_init(smb_server_t *sv)
1083 {
1084 	(void) snprintf(sv->sv_ksp_name, sizeof (sv->sv_ksp_name), "%s%d",
1085 	    SMBSRV_KSTAT_NAME, sv->sv_zid);
1086 
1087 	sv->sv_ksp = kstat_create(SMBSRV_KSTAT_MODULE, 0, sv->sv_ksp_name,
1088 	    SMBSRV_KSTAT_CLASS, KSTAT_TYPE_NAMED,
1089 	    sizeof (sv->sv_ks_data) / sizeof (kstat_named_t),
1090 	    KSTAT_FLAG_VIRTUAL);
1091 
1092 	if (sv->sv_ksp) {
1093 		(void) strlcpy(sv->sv_ks_data.state.name, "state",
1094 		    sizeof (sv->sv_ks_data.state.name));
1095 		sv->sv_ks_data.state.data_type = KSTAT_DATA_UINT32;
1096 		(void) strlcpy(sv->sv_ks_data.open_files.name, "open_files",
1097 		    sizeof (sv->sv_ks_data.open_files.name));
1098 		sv->sv_ks_data.open_files.data_type = KSTAT_DATA_UINT32;
1099 		(void) strlcpy(sv->sv_ks_data.open_trees.name, "connections",
1100 		    sizeof (sv->sv_ks_data.open_trees.name));
1101 		sv->sv_ks_data.open_trees.data_type = KSTAT_DATA_UINT32;
1102 		(void) strlcpy(sv->sv_ks_data.open_users.name, "sessions",
1103 		    sizeof (sv->sv_ks_data.open_users.name));
1104 		sv->sv_ks_data.open_users.data_type = KSTAT_DATA_UINT32;
1105 
1106 		mutex_init(&sv->sv_ksp_mutex, NULL, MUTEX_DEFAULT, NULL);
1107 		sv->sv_ksp->ks_lock = &sv->sv_ksp_mutex;
1108 		sv->sv_ksp->ks_data = (void *)&sv->sv_ks_data;
1109 		sv->sv_ksp->ks_update = smb_server_kstat_update_info;
1110 		kstat_install(sv->sv_ksp);
1111 	}
1112 
1113 	/* create and initialize smb kstats - smb_dispatch stats */
1114 	smb_dispatch_kstat_init();
1115 
1116 	return (0);
1117 }
1118 
1119 /*
1120  * smb_server_kstat_fini
1121  */
1122 static void
1123 smb_server_kstat_fini(smb_server_t *sv)
1124 {
1125 	if (sv->sv_ksp) {
1126 		kstat_delete(sv->sv_ksp);
1127 		mutex_destroy(&sv->sv_ksp_mutex);
1128 		sv->sv_ksp = NULL;
1129 	}
1130 	smb_dispatch_kstat_fini();
1131 }
1132 
1133 /* ARGSUSED */
1134 static int
1135 smb_server_kstat_update_info(kstat_t *ksp, int rw)
1136 {
1137 	smb_server_t	*sv;
1138 
1139 	if (rw == KSTAT_WRITE) {
1140 		return (EACCES);
1141 	} else {
1142 		ASSERT(MUTEX_HELD(ksp->ks_lock));
1143 
1144 		_NOTE(LINTED("pointer cast may result in improper alignment"))
1145 		sv = (smb_server_t *)((uint8_t *)(ksp->ks_data) -
1146 		    offsetof(smb_server_t, sv_ks_data));
1147 
1148 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1149 
1150 		sv->sv_ks_data.state.value.ui32 = sv->sv_state;
1151 		sv->sv_ks_data.open_files.value.ui32 = sv->sv_open_files;
1152 		sv->sv_ks_data.open_trees.value.ui32 = sv->sv_open_trees;
1153 		sv->sv_ks_data.open_users.value.ui32 = sv->sv_open_users;
1154 	}
1155 	return (0);
1156 }
1157 
1158 /*
1159  * smb_server_stop
1160  *
1161  * The mutex of the server must have been entered before calling this function.
1162  */
1163 static void
1164 smb_server_stop(smb_server_t *sv)
1165 {
1166 	ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1167 
1168 	smb_winpipe_close();
1169 	smb_thread_stop(&sv->si_thread_timers);
1170 	smb_kdoor_clnt_stop();
1171 	lmshrd_kclient_fini(sv->sv_lmshrd);
1172 	smb_server_fsop_stop(sv);
1173 	if (sv->sv_session) {
1174 		smb_session_delete(sv->sv_session);
1175 		sv->sv_session = NULL;
1176 	}
1177 }
1178 
1179 static int
1180 smb_server_listen(
1181     smb_server_t		*sv,
1182     smb_listener_daemon_t	*ld,
1183     in_port_t			port,
1184     int				pthread_create_error)
1185 {
1186 	int			rc;
1187 	struct sonode		*s_so;
1188 	uint32_t		on = 1;
1189 	smb_session_t		*session;
1190 
1191 	if (pthread_create_error) {
1192 		/*
1193 		 * Delete the last session created. The user space thread
1194 		 * creation failed.
1195 		 */
1196 		smb_session_list_delete_tail(&ld->ld_session_list);
1197 	}
1198 
1199 	if (ld->ld_so == NULL) {
1200 		/* First time listener */
1201 		ld->ld_sin.sin_family = AF_INET;
1202 		ld->ld_sin.sin_port = htons(port);
1203 		ld->ld_sin.sin_addr.s_addr = htonl(INADDR_ANY);
1204 		ld->ld_so = smb_socreate(AF_INET, SOCK_STREAM, 0);
1205 
1206 		if (ld->ld_so) {
1207 
1208 			(void) sosetsockopt(ld->ld_so, SOL_SOCKET,
1209 			    SO_REUSEADDR, (const void *)&on, sizeof (on));
1210 
1211 			rc = sobind(ld->ld_so, (struct sockaddr *)&ld->ld_sin,
1212 			    sizeof (ld->ld_sin), 0, 0);
1213 
1214 			if (rc == 0) {
1215 				rc =  solisten(ld->ld_so, 20);
1216 				if (rc < 0) {
1217 					cmn_err(CE_WARN,
1218 					    "Port %d: listen failed", port);
1219 					smb_soshutdown(ld->ld_so);
1220 					smb_sodestroy(ld->ld_so);
1221 					ld->ld_so = NULL;
1222 					return (rc);
1223 				}
1224 			} else {
1225 				cmn_err(CE_WARN,
1226 				    "Port %d: bind failed", port);
1227 				smb_soshutdown(ld->ld_so);
1228 				smb_sodestroy(ld->ld_so);
1229 				ld->ld_so = NULL;
1230 				return (rc);
1231 			}
1232 		} else {
1233 			cmn_err(CE_WARN,
1234 			    "Port %d: socket create failed", port);
1235 			return (ENOMEM);
1236 		}
1237 	}
1238 
1239 	DTRACE_PROBE1(so__wait__accept, struct sonode *, ld->ld_so);
1240 
1241 	for (;;) {
1242 		rc = soaccept(ld->ld_so, 0, &s_so);
1243 		if (rc == 0) {
1244 			uint32_t	txbuf_size = 128*1024;
1245 			uint32_t	on = 1;
1246 
1247 			DTRACE_PROBE1(so__accept, struct sonode *, s_so);
1248 
1249 			(void) sosetsockopt(s_so, IPPROTO_TCP, TCP_NODELAY,
1250 			    (const void *)&on, sizeof (on));
1251 			(void) sosetsockopt(s_so, SOL_SOCKET, SO_KEEPALIVE,
1252 			    (const void *)&on, sizeof (on));
1253 			(void) sosetsockopt(s_so, SOL_SOCKET, SO_SNDBUF,
1254 			    (const void *)&txbuf_size, sizeof (txbuf_size));
1255 			/*
1256 			 * Create a session for this connection.
1257 			 */
1258 			session = smb_session_create(s_so, port, sv);
1259 			if (session) {
1260 				smb_session_list_append(&ld->ld_session_list,
1261 				    session);
1262 				break;
1263 			} else {
1264 				smb_soshutdown(s_so);
1265 				smb_sodestroy(s_so);
1266 			}
1267 			continue;
1268 		}
1269 		smb_session_list_signal(&ld->ld_session_list);
1270 		smb_soshutdown(ld->ld_so);
1271 		smb_sodestroy(ld->ld_so);
1272 		ld->ld_so = NULL;
1273 		break;
1274 	}
1275 
1276 	return (rc);
1277 }
1278 
1279 /*
1280  * smb_server_lookup
1281  *
1282  * This function tries to find the server associated with the zone of the
1283  * caller.
1284  */
1285 static int
1286 smb_server_lookup(smb_server_t **psv)
1287 {
1288 	zoneid_t	zid;
1289 	smb_server_t	*sv;
1290 
1291 	zid = getzoneid();
1292 
1293 	smb_llist_enter(&smb_servers, RW_READER);
1294 	sv = smb_llist_head(&smb_servers);
1295 	while (sv) {
1296 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1297 		if (sv->sv_zid == zid) {
1298 			mutex_enter(&sv->sv_mutex);
1299 			if (sv->sv_state != SMB_SERVER_STATE_DELETING) {
1300 				sv->sv_refcnt++;
1301 				mutex_exit(&sv->sv_mutex);
1302 				smb_llist_exit(&smb_servers);
1303 				*psv = sv;
1304 				return (0);
1305 			}
1306 			mutex_exit(&sv->sv_mutex);
1307 			break;
1308 		}
1309 		sv = smb_llist_next(&smb_servers, sv);
1310 	}
1311 	smb_llist_exit(&smb_servers);
1312 	return (EPERM);
1313 }
1314 
1315 /*
1316  * smb_server_release
1317  *
1318  * This function decrements the reference count of the server and signals its
1319  * condition variable if the state of the server is SMB_SERVER_STATE_DELETING.
1320  */
1321 static void
1322 smb_server_release(smb_server_t *sv)
1323 {
1324 	ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
1325 
1326 	mutex_enter(&sv->sv_mutex);
1327 	ASSERT(sv->sv_refcnt);
1328 	sv->sv_refcnt--;
1329 	if ((sv->sv_refcnt == 0) && (sv->sv_state == SMB_SERVER_STATE_DELETING))
1330 		cv_signal(&sv->sv_cv);
1331 	mutex_exit(&sv->sv_mutex);
1332 }
1333 
1334 static int
1335 smb_server_ulist_geti(
1336     smb_session_list_t	*se,
1337     int			offset,
1338     smb_dr_user_ctx_t	*uinfo,
1339     int			max_cnt)
1340 {
1341 	smb_session_t	*sn = NULL;
1342 	smb_user_t	*user;
1343 	smb_llist_t	*ulist;
1344 	int		cnt = 0, skip = 0;
1345 
1346 	rw_enter(&se->se_lock, RW_READER);
1347 	sn = list_head(&se->se_act.lst);
1348 	while (sn && (cnt < max_cnt)) {
1349 		ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
1350 		ulist = &sn->s_user_list;
1351 		smb_llist_enter(ulist, RW_READER);
1352 		user = smb_llist_head(ulist);
1353 		while (user && (cnt < max_cnt)) {
1354 			ASSERT(user->u_magic == SMB_USER_MAGIC);
1355 			mutex_enter(&user->u_mutex);
1356 			if (user->u_state == SMB_USER_STATE_LOGGED_IN) {
1357 				if (skip++ < offset) {
1358 					mutex_exit(&user->u_mutex);
1359 					user = smb_llist_next(ulist, user);
1360 					continue;
1361 				}
1362 
1363 				if (smb_dr_user_create(uinfo, sn->s_kid,
1364 				    user->u_uid, user->u_domain, user->u_name,
1365 				    sn->workstation, sn->ipaddr, sn->native_os,
1366 				    user->u_logon_time, user->u_flags) != 0) {
1367 					mutex_exit(&user->u_mutex);
1368 					user = smb_llist_next(ulist, user);
1369 					continue;
1370 				}
1371 				uinfo++;
1372 				cnt++;
1373 			}
1374 			mutex_exit(&user->u_mutex);
1375 			user = smb_llist_next(ulist, user);
1376 		}
1377 		smb_llist_exit(ulist);
1378 	}
1379 	rw_exit(&se->se_lock);
1380 	return (cnt);
1381 }
1382 
1383 static void
1384 smb_server_store_cfg(smb_server_t *sv, smb_kmod_cfg_t *cfg)
1385 {
1386 	if (cfg->skc_maxconnections == 0)
1387 		cfg->skc_maxconnections = 0xFFFFFFFF;
1388 
1389 	/*
1390 	 * XXX should not override configuration.
1391 	 * For now, this disables server side
1392 	 * signing regardless of configuration.
1393 	 */
1394 	cfg->skc_signing_enable = 0;
1395 	cfg->skc_signing_required = 0;
1396 	cfg->skc_signing_check = 0;
1397 
1398 	smb_session_correct_keep_alive_values(
1399 	    &sv->sv_nbt_daemon.ld_session_list, cfg->skc_keepalive);
1400 	smb_session_correct_keep_alive_values(
1401 	    &sv->sv_tcp_daemon.ld_session_list, cfg->skc_keepalive);
1402 
1403 	/*
1404 	 * XXX The following code was pulled from smb_oplock_init.
1405 	 * It should be combined with with the config process if
1406 	 * this info will be stored with the configuration or with
1407 	 * the smb_fsop_start function if the data will be stored
1408 	 * in the root of the fs.
1409 	 */
1410 
1411 	/*
1412 	 * XXX oplock enable flag.
1413 	 * Should be stored in extended attribute in root of fs
1414 	 * or a ZFS user-defined property.
1415 	 */
1416 	if (cfg->skc_oplock_enable == 0) {
1417 		cmn_err(CE_NOTE, "SmbOplocks: disabled");
1418 	}
1419 
1420 	bcopy(cfg, &sv->sv_cfg, sizeof (sv->sv_cfg));
1421 }
1422 
1423 static int
1424 smb_server_fsop_start(smb_server_t *sv)
1425 {
1426 	int	error;
1427 
1428 	error = smb_node_root_init(rootdir, sv, &sv->si_root_smb_node);
1429 	if (error != 0)
1430 		sv->si_root_smb_node = NULL;
1431 
1432 	return (error);
1433 }
1434 
1435 static void
1436 smb_server_fsop_stop(smb_server_t *sv)
1437 {
1438 	if (sv->si_root_smb_node != NULL) {
1439 		smb_vfs_rele_all(sv);
1440 		smb_node_release(sv->si_root_smb_node);
1441 		sv->si_root_smb_node = NULL;
1442 	}
1443 }
1444