xref: /onnv-gate/usr/src/uts/common/fs/smbsrv/smb_server.c (revision 6496:76a38b87ee13)
16139Sjb150015 /*
26139Sjb150015  * CDDL HEADER START
36139Sjb150015  *
46139Sjb150015  * The contents of this file are subject to the terms of the
56139Sjb150015  * Common Development and Distribution License (the "License").
66139Sjb150015  * You may not use this file except in compliance with the License.
76139Sjb150015  *
86139Sjb150015  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96139Sjb150015  * or http://www.opensolaris.org/os/licensing.
106139Sjb150015  * See the License for the specific language governing permissions
116139Sjb150015  * and limitations under the License.
126139Sjb150015  *
136139Sjb150015  * When distributing Covered Code, include this CDDL HEADER in each
146139Sjb150015  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156139Sjb150015  * If applicable, add the following below this CDDL HEADER, with the
166139Sjb150015  * fields enclosed by brackets "[]" replaced with your own identifying
176139Sjb150015  * information: Portions Copyright [yyyy] [name of copyright owner]
186139Sjb150015  *
196139Sjb150015  * CDDL HEADER END
206139Sjb150015  */
216139Sjb150015 /*
226139Sjb150015  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
236139Sjb150015  * Use is subject to license terms.
246139Sjb150015  */
256139Sjb150015 
266139Sjb150015 #pragma ident	"%Z%%M%	%I%	%E% SMI"
276139Sjb150015 
286139Sjb150015 /*
296139Sjb150015  * General Structures Layout
306139Sjb150015  * -------------------------
316139Sjb150015  *
326139Sjb150015  * This is a simplified diagram showing the relationship between most of the
336139Sjb150015  * main structures.
346139Sjb150015  *
356139Sjb150015  * +-------------------+
366139Sjb150015  * |     SMB_SERVER    |
376139Sjb150015  * +-------------------+
386139Sjb150015  *          |
396139Sjb150015  *          |
406139Sjb150015  *          v
416139Sjb150015  * +-------------------+       +-------------------+      +-------------------+
426139Sjb150015  * |     SESSION       |<----->|     SESSION       |......|      SESSION      |
436139Sjb150015  * +-------------------+       +-------------------+      +-------------------+
446139Sjb150015  *          |
456139Sjb150015  *          |
466139Sjb150015  *          v
476139Sjb150015  * +-------------------+       +-------------------+      +-------------------+
486139Sjb150015  * |       USER        |<----->|       USER        |......|       USER        |
496139Sjb150015  * +-------------------+       +-------------------+      +-------------------+
506139Sjb150015  *          |
516139Sjb150015  *          |
526139Sjb150015  *          v
536139Sjb150015  * +-------------------+       +-------------------+      +-------------------+
546139Sjb150015  * |       TREE        |<----->|       TREE        |......|       TREE        |
556139Sjb150015  * +-------------------+       +-------------------+      +-------------------+
566139Sjb150015  *      |         |
576139Sjb150015  *      |         |
586139Sjb150015  *      |         v
596139Sjb150015  *      |     +-------+       +-------+      +-------+
606139Sjb150015  *      |     | OFILE |<----->| OFILE |......| OFILE |
616139Sjb150015  *      |     +-------+       +-------+      +-------+
626139Sjb150015  *      |
636139Sjb150015  *      |
646139Sjb150015  *      v
656139Sjb150015  *  +-------+       +------+      +------+
666139Sjb150015  *  | ODIR  |<----->| ODIR |......| ODIR |
676139Sjb150015  *  +-------+       +------+      +------+
686139Sjb150015  *
696139Sjb150015  *
706139Sjb150015  * Module Interface Overview
716139Sjb150015  * -------------------------
726139Sjb150015  *
736139Sjb150015  *
746139Sjb150015  *	    +===================================+
756139Sjb150015  *	    |		 smbd daemon		|
766139Sjb150015  *	    +===================================+
776139Sjb150015  *	      |		     |		      ^
786139Sjb150015  *	      |		     |		      |
796139Sjb150015  * User	      |		     |		      |
806139Sjb150015  * -----------|--------------|----------------|--------------------------------
816139Sjb150015  * Kernel     |		     |		      |
826139Sjb150015  *            |		     |		      |
836139Sjb150015  *	      |		     |		      |
846139Sjb150015  *  +=========|==============|================|=================+
856139Sjb150015  *  |	      v		     v		      |			|
866139Sjb150015  *  | +-----------+ +--------------------+ +------------------+ |
876139Sjb150015  *  | |     IO    | | Kernel Door Server | | User Door Servers|	|
886139Sjb150015  *  | | Interface | |     Interface      | |   Interface      | |
896139Sjb150015  *  | +-----------+ +--------------------+ +------------------+ |
906139Sjb150015  *  |		|	     |		      ^		^	|
916139Sjb150015  *  |		v	     v		      |		|	|    +=========+
926139Sjb150015  *  |	     +-----------------------------------+	|	|    |	       |
936139Sjb150015  *  |	     + SMB Server Management (this file) |<------------------|	 ZFS   |
946139Sjb150015  *  |	     +-----------------------------------+	|	|    |	       |
956139Sjb150015  *  |							|	|    |  Module |
966139Sjb150015  *  |	     +-----------------------------------+	|	|    |	       |
976139Sjb150015  *  |	     +     SMB Server Internal Layers    |------+	|    +=========+
986139Sjb150015  *  |	     +-----------------------------------+		|
996139Sjb150015  *  |								|
1006139Sjb150015  *  |								|
1016139Sjb150015  *  +===========================================================+
1026139Sjb150015  *
1036139Sjb150015  *
1046139Sjb150015  * Server State Machine
1056139Sjb150015  * --------------------
1066139Sjb150015  *                                  |
1076139Sjb150015  *                                  | T0
1086139Sjb150015  *                                  |
1096139Sjb150015  *                                  v
1106139Sjb150015  *                    +-----------------------------+
1116139Sjb150015  *		      |   SMB_SERVER_STATE_CREATED  |
1126139Sjb150015  *		      +-----------------------------+
1136139Sjb150015  *				    |
1146139Sjb150015  *				    | T1
1156139Sjb150015  *				    |
1166139Sjb150015  *				    v
1176139Sjb150015  *		      +-----------------------------+
1186139Sjb150015  *		      | SMB_SERVER_STATE_CONFIGURED |
1196139Sjb150015  *		      +-----------------------------+
1206139Sjb150015  *				    |
1216139Sjb150015  *				    | T2
1226139Sjb150015  *				    |
1236139Sjb150015  *				    v
1246139Sjb150015  *		      +-----------------------------+
1256139Sjb150015  *		      |  SMB_SERVER_STATE_RUNNING   |
1266139Sjb150015  *		      +-----------------------------+
1276139Sjb150015  *				    |
1286139Sjb150015  *				    | T3
1296139Sjb150015  *				    |
1306139Sjb150015  *				    v
1316139Sjb150015  *		      +-----------------------------+
1326139Sjb150015  *		      |  SMB_SERVER_STATE_DELETING  |
1336139Sjb150015  *                    +-----------------------------+
1346139Sjb150015  *				    |
1356139Sjb150015  *				    |
1366139Sjb150015  *				    |
1376139Sjb150015  *				    v
1386139Sjb150015  *
1396139Sjb150015  * States
1406139Sjb150015  * ------
1416139Sjb150015  *
1426139Sjb150015  * SMB_SERVER_STATE_CREATED
1436139Sjb150015  *
1446139Sjb150015  *    This is the state of the server just after creation.
1456139Sjb150015  *
1466139Sjb150015  * SMB_SERVER_STATE_CONFIGURED
1476139Sjb150015  *
1486139Sjb150015  *    The server has been configured.
1496139Sjb150015  *
1506139Sjb150015  * SMB_SERVER_STATE_RUNNING
1516139Sjb150015  *
1526139Sjb150015  *    The server has been started. While in this state the threads listening on
1536139Sjb150015  *    the sockets car be started. The smbd daemon does so through an Ioctl:
1546139Sjb150015  *
1556139Sjb150015  *	smb_drv_ioctl(SMB_IOC_NBT_LISTEN) --> smb_server_nbt_listen()
1566139Sjb150015  *	smb_drv_ioctl(SMB_IOC_TCP_LISTEN) --> smb_server_nbt_listen()
1576139Sjb150015  *
1586139Sjb150015  *    When a client establishes a connection the thread listening leaves
1596139Sjb150015  *    temporarily the kernel. While in user space it creates a thread for the
1606139Sjb150015  *    new session. It then returns to kernel with the result of the thread
1616139Sjb150015  *    creation. If the creation failed the new session context is destroyed
1626139Sjb150015  *    before returning listening.
1636139Sjb150015  *
1646139Sjb150015  *    The new created thread enters the kernel though an Ioctl:
1656139Sjb150015  *
1666139Sjb150015  *	smb_drv_ioctl(SMB_IOC_NBT_RECEIVE) --> smb_server_nbt_receive()
1676139Sjb150015  *	smb_drv_ioctl(SMB_IOC_TCP_RECEIVE) --> smb_server_tcp_receive()
1686139Sjb150015  *
1696139Sjb150015  * SMB_SERVER_STATE_STOPPING
1706139Sjb150015  *
1716139Sjb150015  *    The threads listening on the NBT and TCP sockets are being terminated.
1726139Sjb150015  *
1736139Sjb150015  *
1746139Sjb150015  * Transitions
1756139Sjb150015  * -----------
1766139Sjb150015  *
1776139Sjb150015  * Transition T0
1786139Sjb150015  *
1796139Sjb150015  *    The daemon smbd triggers its creation by opening the smbsrv device. If
1806139Sjb150015  *    the zone where the daemon lives doesn't have an smb server yet it is
1816139Sjb150015  *    created.
1826139Sjb150015  *
1836139Sjb150015  *		smb_drv_open() --> smb_server_create()
1846139Sjb150015  *
1856139Sjb150015  * Transition T1
1866139Sjb150015  *
1876139Sjb150015  *    This transition occurs in smb_server_configure(). It is triggered by the
1886139Sjb150015  *    daemon through an Ioctl.
1896139Sjb150015  *
1906139Sjb150015  *	smb_drv_ioctl(SMB_IOC_CONFIG) --> smb_server_configure()
1916139Sjb150015  *
1926139Sjb150015  * Transition T2
1936139Sjb150015  *
1946139Sjb150015  *    This transition occurs in smb_server_start(). It is triggered by the
1956139Sjb150015  *    daemon through an Ioctl.
1966139Sjb150015  *
1976139Sjb150015  *	smb_drv_ioctl(SMB_IOC_START) --> smb_server_start()
1986139Sjb150015  *
1996139Sjb150015  * Transition T3
2006139Sjb150015  *
2016139Sjb150015  *    This transition occurs in smb_server_delete(). It is triggered by the
2026139Sjb150015  *    daemon when closing the smbsrv device
2036139Sjb150015  *
2046139Sjb150015  *		smb_drv_close() --> smb_server_delete()
2056139Sjb150015  *
2066139Sjb150015  * Comments
2076139Sjb150015  * --------
2086139Sjb150015  *
2096139Sjb150015  * This files assumes that there will one SMB server per zone. For now the
2106139Sjb150015  * smb server works only in global zone. There's nothing in this file preventing
2116139Sjb150015  * an smb server from being created in a non global zone. That limitation is
2126139Sjb150015  * enforced in user space.
2136139Sjb150015  */
2146139Sjb150015 
2156139Sjb150015 #include <sys/strsubr.h>
2166139Sjb150015 #include <sys/cmn_err.h>
2176139Sjb150015 #include <sys/priv.h>
2186139Sjb150015 #include <sys/socketvar.h>
2196139Sjb150015 #include <sys/zone.h>
2206139Sjb150015 #include <smbsrv/smb_kproto.h>
2216139Sjb150015 #include <smbsrv/netbios.h>
2226139Sjb150015 #include <smbsrv/smb_incl.h>
2236139Sjb150015 #include <smbsrv/cifs.h>
2246139Sjb150015 #include <smbsrv/smb_fsops.h>
2256139Sjb150015 #include <smbsrv/lmshare.h>
2266139Sjb150015 #include <smbsrv/smb_door_svc.h>
2276432Sas200622 #include <smbsrv/smb_kstat.h>
2286139Sjb150015 
2296139Sjb150015 extern void smb_dispatch_kstat_init(void);
2306139Sjb150015 extern void smb_dispatch_kstat_fini(void);
2316139Sjb150015 extern void smb_reply_notify_change_request(smb_request_t *);
2326139Sjb150015 
2336139Sjb150015 static int smb_server_kstat_init(smb_server_t *);
2346139Sjb150015 static void smb_server_kstat_fini(smb_server_t *);
2356139Sjb150015 static int smb_server_kstat_update_info(kstat_t *, int);
2366139Sjb150015 static void smb_server_timers(smb_thread_t *, void *);
2376139Sjb150015 static int smb_server_listen(smb_server_t *, smb_listener_daemon_t *,
2386139Sjb150015     in_port_t, int);
2396139Sjb150015 static int smb_server_lookup(smb_server_t **);
2406139Sjb150015 static void smb_server_release(smb_server_t *);
2416139Sjb150015 static int smb_server_ulist_geti(smb_session_list_t *, int, smb_dr_user_ctx_t *,
2426139Sjb150015     int);
2436139Sjb150015 static void smb_server_store_cfg(smb_server_t *, smb_kmod_cfg_t *);
2446139Sjb150015 static void smb_server_stop(smb_server_t *);
2456139Sjb150015 static int smb_server_fsop_start(smb_server_t *);
2466139Sjb150015 static void smb_server_fsop_stop(smb_server_t *);
2476139Sjb150015 
2486139Sjb150015 static smb_llist_t	smb_servers;
2496139Sjb150015 
2506139Sjb150015 /*
2516139Sjb150015  * *****************************************************************************
2526139Sjb150015  * **************** Functions called from the device interface *****************
2536139Sjb150015  * *****************************************************************************
2546139Sjb150015  *
2556139Sjb150015  * These functions determine the relevant smb server to which the call apply.
2566139Sjb150015  */
2576139Sjb150015 
2586139Sjb150015 /*
2596139Sjb150015  * smb_server_svc_init
2606139Sjb150015  *
2616139Sjb150015  * This function must called from smb_drv_attach().
2626139Sjb150015  */
2636139Sjb150015 int
2646139Sjb150015 smb_server_svc_init(void)
2656139Sjb150015 {
2666139Sjb150015 	int	rc = 0;
2676139Sjb150015 
2686139Sjb150015 	while (rc == 0) {
2696139Sjb150015 		if (rc = smb_vop_init())
2706139Sjb150015 			continue;
2716139Sjb150015 		if (rc = smb_node_init())
272*6496Sjb150015 			continue;
2736139Sjb150015 		if (rc = smb_fem_init())
274*6496Sjb150015 			continue;
2756139Sjb150015 		if (rc = smb_notify_init())
276*6496Sjb150015 			continue;
277*6496Sjb150015 		if (rc = smb_net_init())
278*6496Sjb150015 			continue;
2796139Sjb150015 		if (rc = smb_kdoor_srv_start())
280*6496Sjb150015 			continue;
2816139Sjb150015 		smb_llist_constructor(&smb_servers, sizeof (smb_server_t),
2826139Sjb150015 		    offsetof(smb_server_t, sv_lnd));
2836139Sjb150015 		return (0);
2846139Sjb150015 	}
285*6496Sjb150015 	smb_net_fini();
2866139Sjb150015 	smb_notify_fini();
2876139Sjb150015 	smb_fem_fini();
2886139Sjb150015 	smb_node_fini();
2896139Sjb150015 	smb_vop_fini();
2906139Sjb150015 	return (rc);
2916139Sjb150015 }
2926139Sjb150015 
2936139Sjb150015 /*
2946139Sjb150015  * smb_server_svc_fini
2956139Sjb150015  *
2966139Sjb150015  * This function must called from smb_drv_detach(). It will fail if servers
2976139Sjb150015  * still exist.
2986139Sjb150015  */
2996139Sjb150015 int
3006139Sjb150015 smb_server_svc_fini(void)
3016139Sjb150015 {
3026139Sjb150015 	int	rc = EBUSY;
3036139Sjb150015 
3046139Sjb150015 	if (smb_llist_get_count(&smb_servers) == 0) {
3056139Sjb150015 		smb_kdoor_srv_stop();
306*6496Sjb150015 		smb_net_fini();
3076139Sjb150015 		smb_notify_fini();
3086139Sjb150015 		smb_fem_fini();
3096139Sjb150015 		smb_node_fini();
3106139Sjb150015 		smb_vop_fini();
3116139Sjb150015 		smb_llist_destructor(&smb_servers);
3126139Sjb150015 		rc = 0;
3136139Sjb150015 	}
3146139Sjb150015 	return (rc);
3156139Sjb150015 }
3166139Sjb150015 
3176139Sjb150015 /*
3186139Sjb150015  * smb_server_create
3196139Sjb150015  *
3206139Sjb150015  * This function will fail if there's already a server associated with the
3216139Sjb150015  * caller's zone.
3226139Sjb150015  */
3236139Sjb150015 int
3246139Sjb150015 smb_server_create(void)
3256139Sjb150015 {
3266139Sjb150015 	zoneid_t	zid;
3276139Sjb150015 	smb_server_t	*sv;
3286139Sjb150015 
3296139Sjb150015 	zid = getzoneid();
3306139Sjb150015 
3316139Sjb150015 	smb_llist_enter(&smb_servers, RW_WRITER);
3326139Sjb150015 	sv = smb_llist_head(&smb_servers);
3336139Sjb150015 	while (sv) {
3346139Sjb150015 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
3356139Sjb150015 		if (sv->sv_zid == zid) {
3366139Sjb150015 			smb_llist_exit(&smb_servers);
3376139Sjb150015 			return (EEXIST);
3386139Sjb150015 		}
3396139Sjb150015 		sv = smb_llist_next(&smb_servers, sv);
3406139Sjb150015 	}
3416139Sjb150015 
3426139Sjb150015 	sv = kmem_zalloc(sizeof (smb_server_t), KM_NOSLEEP);
3436139Sjb150015 	if (sv == NULL) {
3446139Sjb150015 		smb_llist_exit(&smb_servers);
3456139Sjb150015 		return (ENOMEM);
3466139Sjb150015 	}
3476139Sjb150015 
3486139Sjb150015 	smb_llist_constructor(&sv->sv_vfs_list, sizeof (smb_vfs_t),
3496139Sjb150015 	    offsetof(smb_vfs_t, sv_lnd));
3506139Sjb150015 
3516139Sjb150015 	smb_session_list_constructor(&sv->sv_nbt_daemon.ld_session_list);
3526139Sjb150015 	smb_session_list_constructor(&sv->sv_tcp_daemon.ld_session_list);
3536139Sjb150015 
3546139Sjb150015 	sv->si_cache_vfs = kmem_cache_create("smb_vfs_cache",
3556139Sjb150015 	    sizeof (smb_vfs_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
3566139Sjb150015 	sv->si_cache_request = kmem_cache_create("smb_request_cache",
3576139Sjb150015 	    sizeof (smb_request_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
3586139Sjb150015 	sv->si_cache_session = kmem_cache_create("smb_session_cache",
3596139Sjb150015 	    sizeof (smb_session_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
3606139Sjb150015 	sv->si_cache_user = kmem_cache_create("smb_user_cache",
3616139Sjb150015 	    sizeof (smb_user_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
3626139Sjb150015 	sv->si_cache_tree = kmem_cache_create("smb_tree_cache",
3636139Sjb150015 	    sizeof (smb_tree_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
3646139Sjb150015 	sv->si_cache_ofile = kmem_cache_create("smb_ofile_cache",
3656139Sjb150015 	    sizeof (smb_ofile_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
3666139Sjb150015 	sv->si_cache_odir = kmem_cache_create("smb_odir_cache",
3676139Sjb150015 	    sizeof (smb_odir_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
3686139Sjb150015 	sv->si_cache_node = kmem_cache_create("smb_node_cache",
3696139Sjb150015 	    sizeof (smb_node_t), 8, NULL, NULL, NULL, NULL, NULL, 0);
3706139Sjb150015 
3716139Sjb150015 	smb_thread_init(&sv->si_thread_timers,
3726139Sjb150015 	    "smb_timers", smb_server_timers, sv,
3736139Sjb150015 	    NULL, NULL);
3746139Sjb150015 
3756432Sas200622 	sv->sv_pid = curproc->p_pid;
3766139Sjb150015 
3776139Sjb150015 	smb_winpipe_init();
3786139Sjb150015 	(void) smb_server_kstat_init(sv);
3796139Sjb150015 
3806139Sjb150015 	mutex_init(&sv->sv_mutex, NULL, MUTEX_DEFAULT, NULL);
3816139Sjb150015 	cv_init(&sv->sv_cv, NULL, CV_DEFAULT, NULL);
3826139Sjb150015 	sv->sv_state = SMB_SERVER_STATE_CREATED;
3836139Sjb150015 	sv->sv_magic = SMB_SERVER_MAGIC;
3846139Sjb150015 	sv->sv_zid = zid;
3856139Sjb150015 
3866139Sjb150015 	smb_llist_insert_tail(&smb_servers, sv);
3876139Sjb150015 	smb_llist_exit(&smb_servers);
3886139Sjb150015 	return (0);
3896139Sjb150015 }
3906139Sjb150015 
3916139Sjb150015 /*
3926139Sjb150015  * smb_server_delete
3936139Sjb150015  *
3946139Sjb150015  * This function will delete the server passed in. It will make sure that all
3956139Sjb150015  * activity associated that server has ceased before destroying it.
3966139Sjb150015  */
3976139Sjb150015 int
3986139Sjb150015 smb_server_delete(void)
3996139Sjb150015 {
4006139Sjb150015 	smb_server_t	*sv;
4016139Sjb150015 	int		rc;
4026139Sjb150015 
4036139Sjb150015 	rc = smb_server_lookup(&sv);
4046139Sjb150015 	if (rc != 0)
4056139Sjb150015 		return (rc);
4066139Sjb150015 
4076139Sjb150015 	mutex_enter(&sv->sv_mutex);
4086139Sjb150015 	switch (sv->sv_state) {
4096139Sjb150015 	case SMB_SERVER_STATE_RUNNING:
4106139Sjb150015 	{
4116139Sjb150015 		boolean_t	nbt = B_FALSE;
4126139Sjb150015 		boolean_t	tcp = B_FALSE;
4136139Sjb150015 
4146139Sjb150015 		if (sv->sv_nbt_daemon.ld_kth) {
4156139Sjb150015 			tsignal(sv->sv_nbt_daemon.ld_kth, SIGINT);
4166139Sjb150015 			nbt = B_TRUE;
4176139Sjb150015 		}
4186139Sjb150015 		if (sv->sv_tcp_daemon.ld_kth) {
4196139Sjb150015 			tsignal(sv->sv_tcp_daemon.ld_kth, SIGINT);
4206139Sjb150015 			tcp = B_TRUE;
4216139Sjb150015 		}
4226139Sjb150015 		sv->sv_state = SMB_SERVER_STATE_DELETING;
4236139Sjb150015 		mutex_exit(&sv->sv_mutex);
4246139Sjb150015 		if (nbt)
4256139Sjb150015 			thread_join(sv->sv_nbt_daemon.ld_ktdid);
4266139Sjb150015 		if (tcp)
4276139Sjb150015 			thread_join(sv->sv_tcp_daemon.ld_ktdid);
4286139Sjb150015 		mutex_enter(&sv->sv_mutex);
4296139Sjb150015 		break;
4306139Sjb150015 	}
4316139Sjb150015 	case SMB_SERVER_STATE_CONFIGURED:
4326139Sjb150015 	case SMB_SERVER_STATE_CREATED:
4336139Sjb150015 		sv->sv_state = SMB_SERVER_STATE_DELETING;
4346139Sjb150015 		break;
4356139Sjb150015 	default:
4366139Sjb150015 		ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
4376139Sjb150015 		mutex_exit(&sv->sv_mutex);
4386139Sjb150015 		smb_server_release(sv);
4396139Sjb150015 		return (ENOTTY);
4406139Sjb150015 	}
4416139Sjb150015 
4426139Sjb150015 	ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
4436139Sjb150015 
4446139Sjb150015 	sv->sv_refcnt--;
4456139Sjb150015 	while (sv->sv_refcnt)
4466139Sjb150015 		cv_wait(&sv->sv_cv, &sv->sv_mutex);
4476139Sjb150015 
4486139Sjb150015 	mutex_exit(&sv->sv_mutex);
4496139Sjb150015 
4506139Sjb150015 	smb_llist_enter(&smb_servers, RW_WRITER);
4516139Sjb150015 	smb_llist_remove(&smb_servers, sv);
4526139Sjb150015 	smb_llist_exit(&smb_servers);
4536139Sjb150015 
4546139Sjb150015 	smb_server_stop(sv);
4556139Sjb150015 	rw_destroy(&sv->sv_cfg_lock);
4566139Sjb150015 	smb_winpipe_fini();
4576139Sjb150015 	smb_server_kstat_fini(sv);
4586139Sjb150015 	smb_llist_destructor(&sv->sv_vfs_list);
4596139Sjb150015 	kmem_cache_destroy(sv->si_cache_vfs);
4606139Sjb150015 	kmem_cache_destroy(sv->si_cache_request);
4616139Sjb150015 	kmem_cache_destroy(sv->si_cache_session);
4626139Sjb150015 	kmem_cache_destroy(sv->si_cache_user);
4636139Sjb150015 	kmem_cache_destroy(sv->si_cache_tree);
4646139Sjb150015 	kmem_cache_destroy(sv->si_cache_ofile);
4656139Sjb150015 	kmem_cache_destroy(sv->si_cache_odir);
4666139Sjb150015 	kmem_cache_destroy(sv->si_cache_node);
4676139Sjb150015 
4686139Sjb150015 	taskq_destroy(sv->sv_thread_pool);
4696139Sjb150015 	smb_thread_destroy(&sv->si_thread_timers);
4706139Sjb150015 	mutex_destroy(&sv->sv_mutex);
4716139Sjb150015 	cv_destroy(&sv->sv_cv);
4726139Sjb150015 	sv->sv_magic = 0;
4736139Sjb150015 	kmem_free(sv, sizeof (smb_server_t));
4746139Sjb150015 
4756139Sjb150015 	return (0);
4766139Sjb150015 }
4776139Sjb150015 
4786139Sjb150015 /*
4796139Sjb150015  * smb_server_configure
4806139Sjb150015  */
4816139Sjb150015 int
4826139Sjb150015 smb_server_configure(smb_kmod_cfg_t *cfg)
4836139Sjb150015 {
4846139Sjb150015 	int		rc = 0;
4856139Sjb150015 	smb_server_t	*sv;
4866139Sjb150015 
4876139Sjb150015 	rc = smb_server_lookup(&sv);
4886139Sjb150015 	if (rc)
4896139Sjb150015 		return (rc);
4906139Sjb150015 
4916139Sjb150015 	mutex_enter(&sv->sv_mutex);
4926139Sjb150015 	switch (sv->sv_state) {
4936139Sjb150015 	case SMB_SERVER_STATE_CREATED:
4946139Sjb150015 		smb_server_store_cfg(sv, cfg);
4956139Sjb150015 		sv->sv_state = SMB_SERVER_STATE_CONFIGURED;
4966139Sjb150015 		break;
4976139Sjb150015 
4986139Sjb150015 	case SMB_SERVER_STATE_CONFIGURED:
4996139Sjb150015 		smb_server_store_cfg(sv, cfg);
5006139Sjb150015 		break;
5016139Sjb150015 
5026139Sjb150015 	case SMB_SERVER_STATE_RUNNING:
5036139Sjb150015 		rw_enter(&sv->sv_cfg_lock, RW_WRITER);
5046139Sjb150015 		smb_server_store_cfg(sv, cfg);
5056139Sjb150015 		rw_exit(&sv->sv_cfg_lock);
5066139Sjb150015 		break;
5076139Sjb150015 
5086139Sjb150015 	default:
5096139Sjb150015 		ASSERT(sv->sv_state == SMB_SERVER_STATE_DELETING);
5106139Sjb150015 		rc = EFAULT;
5116139Sjb150015 		break;
5126139Sjb150015 	}
5136139Sjb150015 	mutex_exit(&sv->sv_mutex);
5146139Sjb150015 
5156139Sjb150015 	smb_server_release(sv);
5166139Sjb150015 
5176139Sjb150015 	return (rc);
5186139Sjb150015 }
5196139Sjb150015 
5206139Sjb150015 /*
5216139Sjb150015  * smb_server_start
5226139Sjb150015  */
5236139Sjb150015 int
5246139Sjb150015 smb_server_start(struct smb_io_start *io_start)
5256139Sjb150015 {
5266139Sjb150015 	int		rc = 0;
5276139Sjb150015 	smb_server_t	*sv;
5286139Sjb150015 
5296139Sjb150015 	rc = smb_server_lookup(&sv);
5306139Sjb150015 	if (rc)
5316139Sjb150015 		return (rc);
5326139Sjb150015 
5336139Sjb150015 	mutex_enter(&sv->sv_mutex);
5346139Sjb150015 	switch (sv->sv_state) {
5356139Sjb150015 	case SMB_SERVER_STATE_CONFIGURED:
5366139Sjb150015 
5376139Sjb150015 		sv->sv_thread_pool = taskq_create("smb_workers",
5386139Sjb150015 		    sv->sv_cfg.skc_maxworkers, SMB_WORKER_PRIORITY,
5396139Sjb150015 		    sv->sv_cfg.skc_maxworkers, INT_MAX,
5406139Sjb150015 		    TASKQ_DYNAMIC|TASKQ_PREPOPULATE);
5416139Sjb150015 
5426139Sjb150015 		sv->sv_session = smb_session_create(NULL, 0, sv);
5436139Sjb150015 		if (sv->sv_session == NULL) {
5446139Sjb150015 			rc = ENOMEM;
5456139Sjb150015 			break;
5466139Sjb150015 		}
5476139Sjb150015 
5486139Sjb150015 		if (rc = smb_server_fsop_start(sv))
5496139Sjb150015 			break;
5506139Sjb150015 		ASSERT(sv->sv_lmshrd == NULL);
5516139Sjb150015 		sv->sv_lmshrd = lmshrd_kclient_init(io_start->lmshrd);
5526139Sjb150015 		if (sv->sv_lmshrd == NULL)
5536139Sjb150015 			break;
5546139Sjb150015 		if (rc = smb_kdoor_clnt_start(io_start->udoor))
5556139Sjb150015 			break;
5566139Sjb150015 		if (rc = smb_kdoor_srv_set_dwncall())
5576139Sjb150015 			break;
5586139Sjb150015 		if (rc = smb_thread_start(&sv->si_thread_timers))
5596139Sjb150015 			break;
5606139Sjb150015 		/*
5616139Sjb150015 		 * XXX We give up the NET_MAC_AWARE privilege because it keeps
5626139Sjb150015 		 * us from re-opening the connection when there are leftover TCP
5636139Sjb150015 		 * connections in TCPS_TIME_WAIT state.  There seem to be some
5646139Sjb150015 		 * security ramifications around reestablishing a connection
5656139Sjb150015 		 * while possessing the NET_MAC_AWARE privilege.
5666139Sjb150015 		 *
5676139Sjb150015 		 * This approach may cause problems when we try to support
5686139Sjb150015 		 * zones.  An alternative would be to retry the connection setup
5696139Sjb150015 		 * for a fixed period of time until the stale connections clear
5706139Sjb150015 		 * up but that implies we would be offline for a couple minutes
5716139Sjb150015 		 * every time the service is restarted with active connections.
5726139Sjb150015 		 */
5736139Sjb150015 		rc = setpflags(NET_MAC_AWARE, 0, CRED());
5746139Sjb150015 		if (rc) {
5756139Sjb150015 			cmn_err(CE_WARN,
5766139Sjb150015 			    "Cannot remove NET_MAC_AWARE privilege");
5776139Sjb150015 			break;
5786139Sjb150015 		}
5796139Sjb150015 		if (rc = smb_winpipe_open(io_start->winpipe)) {
5806139Sjb150015 			cmn_err(CE_WARN, "Cannot open winpipe door");
5816139Sjb150015 			break;
5826139Sjb150015 		}
5836139Sjb150015 		sv->sv_state = SMB_SERVER_STATE_RUNNING;
5846139Sjb150015 		mutex_exit(&sv->sv_mutex);
5856139Sjb150015 		smb_server_release(sv);
5866139Sjb150015 		return (0);
5876139Sjb150015 	default:
5886139Sjb150015 		ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
5896139Sjb150015 		    (sv->sv_state == SMB_SERVER_STATE_RUNNING) ||
5906139Sjb150015 		    (sv->sv_state == SMB_SERVER_STATE_DELETING));
5916139Sjb150015 		mutex_exit(&sv->sv_mutex);
5926139Sjb150015 		smb_server_release(sv);
5936139Sjb150015 		return (ENOTTY);
5946139Sjb150015 	}
5956139Sjb150015 
5966139Sjb150015 	smb_server_stop(sv);
5976139Sjb150015 	mutex_exit(&sv->sv_mutex);
5986139Sjb150015 	smb_server_release(sv);
5996139Sjb150015 	return (rc);
6006139Sjb150015 }
6016139Sjb150015 
6026139Sjb150015 /*
6036139Sjb150015  * smb_server_nbt_listen: SMB-over-NetBIOS service
6046139Sjb150015  *
6056139Sjb150015  * Traditional SMB service over NetBIOS (port 139), which requires
6066139Sjb150015  * that a NetBIOS session be established.
6076139Sjb150015  */
6086139Sjb150015 int
6096139Sjb150015 smb_server_nbt_listen(int error)
6106139Sjb150015 {
6116139Sjb150015 	smb_server_t	*sv;
6126139Sjb150015 	int		rc;
6136139Sjb150015 
6146139Sjb150015 	rc = smb_server_lookup(&sv);
6156139Sjb150015 	if (rc)
6166139Sjb150015 		return (rc);
6176139Sjb150015 
6186139Sjb150015 	mutex_enter(&sv->sv_mutex);
6196139Sjb150015 	switch (sv->sv_state) {
6206139Sjb150015 	case SMB_SERVER_STATE_RUNNING:
6216139Sjb150015 		if ((sv->sv_nbt_daemon.ld_kth != NULL) &&
6226139Sjb150015 		    (sv->sv_nbt_daemon.ld_kth != curthread)) {
6236139Sjb150015 			mutex_exit(&sv->sv_mutex);
6246139Sjb150015 			return (EACCES);
6256139Sjb150015 		} else {
6266139Sjb150015 			sv->sv_nbt_daemon.ld_kth = curthread;
6276139Sjb150015 			sv->sv_nbt_daemon.ld_ktdid = curthread->t_did;
6286139Sjb150015 		}
6296139Sjb150015 		break;
6306139Sjb150015 	default:
6316139Sjb150015 		ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
6326139Sjb150015 		    (sv->sv_state == SMB_SERVER_STATE_CONFIGURED) ||
6336139Sjb150015 		    (sv->sv_state == SMB_SERVER_STATE_DELETING));
6346139Sjb150015 		mutex_exit(&sv->sv_mutex);
6356139Sjb150015 		smb_server_release(sv);
6366139Sjb150015 		return (EFAULT);
6376139Sjb150015 	}
6386139Sjb150015 	mutex_exit(&sv->sv_mutex);
6396139Sjb150015 
6406139Sjb150015 	rc = smb_server_listen(sv, &sv->sv_nbt_daemon, SSN_SRVC_TCP_PORT,
6416139Sjb150015 	    error);
6426139Sjb150015 
6436139Sjb150015 	if (rc) {
6446139Sjb150015 		mutex_enter(&sv->sv_mutex);
6456139Sjb150015 		sv->sv_nbt_daemon.ld_kth = NULL;
6466139Sjb150015 		mutex_exit(&sv->sv_mutex);
6476139Sjb150015 	}
6486139Sjb150015 
6496139Sjb150015 	smb_server_release(sv);
6506139Sjb150015 
6516139Sjb150015 	return (rc);
6526139Sjb150015 }
6536139Sjb150015 
6546139Sjb150015 int
6556139Sjb150015 smb_server_tcp_listen(int error)
6566139Sjb150015 {
6576139Sjb150015 	smb_server_t	*sv;
6586139Sjb150015 	int		rc;
6596139Sjb150015 
6606139Sjb150015 	rc = smb_server_lookup(&sv);
6616139Sjb150015 	if (rc)
6626139Sjb150015 		return (rc);
6636139Sjb150015 
6646139Sjb150015 	mutex_enter(&sv->sv_mutex);
6656139Sjb150015 	switch (sv->sv_state) {
6666139Sjb150015 	case SMB_SERVER_STATE_RUNNING:
6676139Sjb150015 		if ((sv->sv_tcp_daemon.ld_kth) &&
6686139Sjb150015 		    (sv->sv_tcp_daemon.ld_kth != curthread)) {
6696139Sjb150015 			mutex_exit(&sv->sv_mutex);
6706139Sjb150015 			return (EACCES);
6716139Sjb150015 		} else {
6726139Sjb150015 			sv->sv_tcp_daemon.ld_kth = curthread;
6736139Sjb150015 			sv->sv_tcp_daemon.ld_ktdid = curthread->t_did;
6746139Sjb150015 		}
6756139Sjb150015 		break;
6766139Sjb150015 	default:
6776139Sjb150015 		ASSERT((sv->sv_state == SMB_SERVER_STATE_CREATED) ||
6786139Sjb150015 		    (sv->sv_state == SMB_SERVER_STATE_CONFIGURED) ||
6796139Sjb150015 		    (sv->sv_state == SMB_SERVER_STATE_DELETING));
6806139Sjb150015 		mutex_exit(&sv->sv_mutex);
6816139Sjb150015 		return (EFAULT);
6826139Sjb150015 	}
6836139Sjb150015 	mutex_exit(&sv->sv_mutex);
6846139Sjb150015 
6856139Sjb150015 	rc = smb_server_listen(sv, &sv->sv_tcp_daemon, SMB_SRVC_TCP_PORT,
6866139Sjb150015 	    error);
6876139Sjb150015 
6886139Sjb150015 	if (rc) {
6896139Sjb150015 		mutex_enter(&sv->sv_mutex);
6906139Sjb150015 		sv->sv_tcp_daemon.ld_kth = NULL;
6916139Sjb150015 		mutex_exit(&sv->sv_mutex);
6926139Sjb150015 	}
6936139Sjb150015 
6946139Sjb150015 	smb_server_release(sv);
6956139Sjb150015 
6966139Sjb150015 	return (rc);
6976139Sjb150015 }
6986139Sjb150015 
6996139Sjb150015 /*
7006139Sjb150015  * smb_server_nbt_receive
7016139Sjb150015  */
7026139Sjb150015 int
7036139Sjb150015 smb_server_nbt_receive(void)
7046139Sjb150015 {
7056139Sjb150015 	int		rc;
7066139Sjb150015 	smb_server_t	*sv;
7076139Sjb150015 
7086139Sjb150015 	rc = smb_server_lookup(&sv);
7096139Sjb150015 	if (rc)
7106139Sjb150015 		return (rc);
7116139Sjb150015 
7126139Sjb150015 	rc = smb_session_daemon(&sv->sv_nbt_daemon.ld_session_list);
7136139Sjb150015 
7146139Sjb150015 	smb_server_release(sv);
7156139Sjb150015 
7166139Sjb150015 	return (rc);
7176139Sjb150015 }
7186139Sjb150015 
7196139Sjb150015 /*
7206139Sjb150015  * smb_server_tcp_receive
7216139Sjb150015  */
7226139Sjb150015 int
7236139Sjb150015 smb_server_tcp_receive(void)
7246139Sjb150015 {
7256139Sjb150015 	int		rc;
7266139Sjb150015 	smb_server_t	*sv;
7276139Sjb150015 
7286139Sjb150015 	rc = smb_server_lookup(&sv);
7296139Sjb150015 	if (rc)
7306139Sjb150015 		return (rc);
7316139Sjb150015 
7326139Sjb150015 	rc = smb_session_daemon(&sv->sv_tcp_daemon.ld_session_list);
7336139Sjb150015 
7346139Sjb150015 	smb_server_release(sv);
7356139Sjb150015 
7366139Sjb150015 	return (rc);
7376139Sjb150015 }
7386139Sjb150015 
7396139Sjb150015 int
7406139Sjb150015 smb_server_set_gmtoff(uint32_t goff)
7416139Sjb150015 {
7426139Sjb150015 	int		rc;
7436139Sjb150015 	smb_server_t	*sv;
7446139Sjb150015 
7456139Sjb150015 
7466139Sjb150015 	rc = smb_server_lookup(&sv);
7476139Sjb150015 	if (rc)
7486139Sjb150015 		return (rc);
7496139Sjb150015 
7506139Sjb150015 	sv->si_gmtoff = goff;
7516139Sjb150015 
7526139Sjb150015 	smb_server_release(sv);
7536139Sjb150015 
7546139Sjb150015 	return (rc);
7556139Sjb150015 }
7566139Sjb150015 
7576139Sjb150015 /*
7586139Sjb150015  * *****************************************************************************
7596139Sjb150015  * ****************** Functions called from the door interface *****************
7606139Sjb150015  * *****************************************************************************
7616139Sjb150015  *
7626139Sjb150015  * These functions determine the relevant smb server to which the call apply.
7636139Sjb150015  */
7646139Sjb150015 
7656139Sjb150015 uint32_t
7666139Sjb150015 smb_server_get_user_count(void)
7676139Sjb150015 {
7686139Sjb150015 	smb_server_t	*sv;
7696139Sjb150015 	uint32_t	counter = 0;
7706139Sjb150015 
7716139Sjb150015 	if (smb_server_lookup(&sv))
7726139Sjb150015 		return (0);
7736139Sjb150015 
7746139Sjb150015 	counter = (uint32_t)sv->sv_open_users;
7756139Sjb150015 
7766139Sjb150015 	smb_server_release(sv);
7776139Sjb150015 
7786139Sjb150015 	return (counter);
7796139Sjb150015 }
7806139Sjb150015 
7816139Sjb150015 uint32_t
7826139Sjb150015 smb_server_get_session_count(void)
7836139Sjb150015 {
7846139Sjb150015 	smb_server_t	*sv;
7856139Sjb150015 	uint32_t	counter = 0;
7866139Sjb150015 
7876139Sjb150015 	if (smb_server_lookup(&sv))
7886139Sjb150015 		return (0);
7896139Sjb150015 
7906139Sjb150015 	rw_enter(&sv->sv_nbt_daemon.ld_session_list.se_lock, RW_READER);
7916139Sjb150015 	counter = sv->sv_nbt_daemon.ld_session_list.se_act.count;
7926139Sjb150015 	rw_exit(&sv->sv_nbt_daemon.ld_session_list.se_lock);
7936139Sjb150015 	rw_enter(&sv->sv_tcp_daemon.ld_session_list.se_lock, RW_READER);
7946139Sjb150015 	counter += sv->sv_tcp_daemon.ld_session_list.se_act.count;
7956139Sjb150015 	rw_exit(&sv->sv_tcp_daemon.ld_session_list.se_lock);
7966139Sjb150015 
7976139Sjb150015 	smb_server_release(sv);
7986139Sjb150015 
7996139Sjb150015 	return (counter);
8006139Sjb150015 }
8016139Sjb150015 
8026139Sjb150015 /*
8036139Sjb150015  * smb_session_disconnect_share
8046139Sjb150015  *
8056139Sjb150015  * Disconnects the specified share. This function should be called after the
8066139Sjb150015  * share passed in has been made unavailable by the "share manager".
8076139Sjb150015  */
8086139Sjb150015 void
8096139Sjb150015 smb_server_disconnect_share(char *sharename)
8106139Sjb150015 {
8116139Sjb150015 	smb_server_t	*sv;
8126139Sjb150015 
8136139Sjb150015 	if (smb_server_lookup(&sv))
8146139Sjb150015 		return;
8156139Sjb150015 
8166139Sjb150015 	smb_session_disconnect_share(&sv->sv_nbt_daemon.ld_session_list,
8176139Sjb150015 	    sharename);
8186139Sjb150015 	smb_session_disconnect_share(&sv->sv_tcp_daemon.ld_session_list,
8196139Sjb150015 	    sharename);
8206139Sjb150015 
8216139Sjb150015 	smb_server_release(sv);
8226139Sjb150015 }
8236139Sjb150015 
8246139Sjb150015 void
8256139Sjb150015 smb_server_disconnect_volume(fs_desc_t *fsd)
8266139Sjb150015 {
8276139Sjb150015 	smb_server_t	*sv;
8286139Sjb150015 
8296139Sjb150015 	if (smb_server_lookup(&sv))
8306139Sjb150015 		return;
8316139Sjb150015 
8326139Sjb150015 	smb_session_disconnect_volume(&sv->sv_nbt_daemon.ld_session_list, fsd);
8336139Sjb150015 	smb_session_disconnect_volume(&sv->sv_tcp_daemon.ld_session_list, fsd);
8346139Sjb150015 
8356139Sjb150015 	smb_server_release(sv);
8366139Sjb150015 }
8376139Sjb150015 
8386139Sjb150015 int
8396139Sjb150015 smb_server_dr_ulist_get(int offset, smb_dr_ulist_t *dr_ulist, int max_cnt)
8406139Sjb150015 {
8416139Sjb150015 	smb_server_t	*sv;
8426139Sjb150015 
8436139Sjb150015 	if (!dr_ulist)
8446139Sjb150015 		return (-1);
8456139Sjb150015 
8466139Sjb150015 	if (smb_server_lookup(&sv))
8476139Sjb150015 		return (-1);
8486139Sjb150015 
8496139Sjb150015 	dr_ulist->dul_cnt =
8506139Sjb150015 	    smb_server_ulist_geti(&sv->sv_nbt_daemon.ld_session_list,
8516139Sjb150015 	    offset, dr_ulist->dul_users, max_cnt);
8526139Sjb150015 	dr_ulist->dul_cnt +=
8536139Sjb150015 	    smb_server_ulist_geti(&sv->sv_tcp_daemon.ld_session_list,
8546139Sjb150015 	    offset - dr_ulist->dul_cnt, &dr_ulist->dul_users[dr_ulist->dul_cnt],
8556139Sjb150015 	    max_cnt);
8566139Sjb150015 
8576139Sjb150015 	return (dr_ulist->dul_cnt);
8586139Sjb150015 }
8596139Sjb150015 
8606139Sjb150015 /*
8616139Sjb150015  * smb_server_share_export()
8626139Sjb150015  *
8636139Sjb150015  * This function handles kernel processing at share enable time.
8646139Sjb150015  *
8656139Sjb150015  * At share-enable time (LMSHRD_ADD), the file system corresponding to
8666139Sjb150015  * the share is checked for characteristics that are required for SMB
8676139Sjb150015  * sharing.  If this check passes, then a hold is taken on the root vnode
8686139Sjb150015  * of the file system (or a reference count on the corresponding smb_vfs_t
8696139Sjb150015  * is bumped), preventing an unmount.  (See smb_vfs_hold()).
8706139Sjb150015  */
8716139Sjb150015 
8726139Sjb150015 int
8736139Sjb150015 smb_server_share_export(char *path)
8746139Sjb150015 {
8756139Sjb150015 	smb_server_t	*sv;
8766139Sjb150015 	int		error;
8776139Sjb150015 	smb_node_t	*fnode = NULL;
8786139Sjb150015 	smb_node_t	*dnode;
8796139Sjb150015 	smb_attr_t	ret_attr;
8806139Sjb150015 	char		last_comp[MAXNAMELEN];
8816139Sjb150015 	smb_request_t	*sr;
8826139Sjb150015 
8836139Sjb150015 	if (smb_server_lookup(&sv))
8846139Sjb150015 		return (EINVAL);
8856139Sjb150015 
8866139Sjb150015 	sr = smb_request_alloc(sv->sv_session, 0);
8876139Sjb150015 	if (sr == NULL) {
8886139Sjb150015 		smb_server_release(sv);
8896139Sjb150015 		return (ENOMEM);
8906139Sjb150015 	}
8916139Sjb150015 
8926139Sjb150015 	sr->user_cr = kcred;
8936139Sjb150015 
8946139Sjb150015 	error = smb_pathname_reduce(sr, kcred, path, NULL, NULL, &dnode,
8956139Sjb150015 	    last_comp);
8966139Sjb150015 
8976139Sjb150015 	if (error) {
8986139Sjb150015 		smb_request_free(sr);
8996139Sjb150015 		smb_server_release(sv);
9006139Sjb150015 		return (error);
9016139Sjb150015 	}
9026139Sjb150015 
9036139Sjb150015 	error = smb_fsop_lookup(sr, sr->user_cr, SMB_FOLLOW_LINKS, NULL, dnode,
9046139Sjb150015 	    last_comp, &fnode, &ret_attr, NULL, NULL);
9056139Sjb150015 
9066139Sjb150015 	smb_node_release(dnode);
9076139Sjb150015 
9086139Sjb150015 	if (error) {
9096139Sjb150015 		smb_request_free(sr);
9106139Sjb150015 		smb_server_release(sv);
9116139Sjb150015 		return (error);
9126139Sjb150015 	}
9136139Sjb150015 
9146139Sjb150015 	ASSERT(fnode->vp && fnode->vp->v_vfsp);
9156139Sjb150015 
9166139Sjb150015 #ifdef SMB_ENFORCE_NODEV
9176139Sjb150015 	if (vfs_optionisset(fnode->vp->v_vfsp, MNTOPT_NODEVICES, NULL) == 0)
9186139Sjb150015 		return (EINVAL);
9196139Sjb150015 #endif /* SMB_ENFORCE_NODEV */
9206139Sjb150015 
9216139Sjb150015 	if (!smb_vfs_hold(sv, fnode->vp->v_vfsp)) {
9226139Sjb150015 		smb_node_release(fnode);
9236139Sjb150015 		smb_request_free(sr);
9246139Sjb150015 		smb_server_release(sv);
9256139Sjb150015 		return (ENOMEM);
9266139Sjb150015 	}
9276139Sjb150015 
9286139Sjb150015 	/*
9296139Sjb150015 	 * The refcount on the smb_vfs has been incremented.
9306139Sjb150015 	 * If it wasn't already, a hold has also been taken
9316139Sjb150015 	 * on the root vnode of the file system.
9326139Sjb150015 	 */
9336139Sjb150015 
9346139Sjb150015 	smb_node_release(fnode);
9356139Sjb150015 	smb_request_free(sr);
9366139Sjb150015 	smb_server_release(sv);
9376139Sjb150015 	return (0);
9386139Sjb150015 }
9396139Sjb150015 
9406139Sjb150015 
9416139Sjb150015 
9426139Sjb150015 /*
9436139Sjb150015  * smb_server_share_unexport()
9446139Sjb150015  *
9456139Sjb150015  * This function handles kernel processing at share disable time.
9466139Sjb150015  *
9476139Sjb150015  * At share-disable time (LMSHRD_DELETE), the reference count on the
9486139Sjb150015  * corresponding smb_vfs_t is decremented.  If this is the last share
9496139Sjb150015  * on the file system, the hold on the root vnode of the file system
9506139Sjb150015  * will be released.  (See smb_vfs_rele().)
9516139Sjb150015  */
9526139Sjb150015 
9536139Sjb150015 int
9546139Sjb150015 smb_server_share_unexport(char *path, char *sharename)
9556139Sjb150015 {
9566139Sjb150015 	smb_server_t	*sv;
9576139Sjb150015 	int		error;
9586139Sjb150015 	smb_node_t	*fnode = NULL;
9596139Sjb150015 	smb_node_t	*dnode;
9606139Sjb150015 	smb_attr_t	ret_attr;
9616139Sjb150015 	char		last_comp[MAXNAMELEN];
9626139Sjb150015 	smb_request_t	*sr;
9636139Sjb150015 
9646139Sjb150015 	if (smb_server_lookup(&sv))
9656139Sjb150015 		return (EINVAL);
9666139Sjb150015 
9676139Sjb150015 	sr = smb_request_alloc(sv->sv_session, 0);
9686139Sjb150015 	if (sr == NULL) {
9696139Sjb150015 		smb_server_release(sv);
9706139Sjb150015 		return (ENOMEM);
9716139Sjb150015 	}
9726139Sjb150015 	sr->user_cr = kcred;
9736139Sjb150015 
9746139Sjb150015 	error = smb_pathname_reduce(sr, kcred, path, NULL, NULL, &dnode,
9756139Sjb150015 	    last_comp);
9766139Sjb150015 
9776139Sjb150015 	if (error) {
9786139Sjb150015 		smb_request_free(sr);
9796139Sjb150015 		smb_server_release(sv);
9806139Sjb150015 		return (error);
9816139Sjb150015 	}
9826139Sjb150015 
9836139Sjb150015 	error = smb_fsop_lookup(sr, kcred, SMB_FOLLOW_LINKS, NULL, dnode,
9846139Sjb150015 	    last_comp, &fnode, &ret_attr, NULL, NULL);
9856139Sjb150015 
9866139Sjb150015 	smb_node_release(dnode);
9876139Sjb150015 
9886139Sjb150015 	if (error) {
9896139Sjb150015 		smb_request_free(sr);
9906139Sjb150015 		smb_server_release(sv);
9916139Sjb150015 		return (error);
9926139Sjb150015 	}
9936139Sjb150015 
9946139Sjb150015 	ASSERT(fnode->vp && fnode->vp->v_vfsp);
9956139Sjb150015 
9966139Sjb150015 	smb_session_disconnect_share(&sv->sv_nbt_daemon.ld_session_list,
9976139Sjb150015 	    sharename);
9986139Sjb150015 	smb_session_disconnect_share(&sv->sv_tcp_daemon.ld_session_list,
9996139Sjb150015 	    sharename);
10006139Sjb150015 	smb_vfs_rele(sv, fnode->vp->v_vfsp);
10016139Sjb150015 	smb_node_release(fnode);
10026139Sjb150015 	smb_request_free(sr);
10036139Sjb150015 	smb_server_release(sv);
10046139Sjb150015 	return (0);
10056139Sjb150015 }
10066139Sjb150015 
10076139Sjb150015 /*
10086139Sjb150015  * This is a special interface that will be utilized by ZFS to cause a share to
10096139Sjb150015  * be added/removed.
10106139Sjb150015  *
10116139Sjb150015  * arg is either a lmshare_info_t or share_name from userspace.
10126139Sjb150015  * It will need to be copied into the kernel.   It is lmshare_info_t
10136139Sjb150015  * for add operations and share_name for delete operations.
10146139Sjb150015  */
10156139Sjb150015 int
10166139Sjb150015 smb_server_share(void *arg, boolean_t add_share)
10176139Sjb150015 {
10186139Sjb150015 	smb_server_t	*sv;
10196139Sjb150015 	int		rc;
10206139Sjb150015 
10216139Sjb150015 	rc = smb_server_lookup(&sv);
10226139Sjb150015 	if (rc == 0) {
10236139Sjb150015 		mutex_enter(&sv->sv_mutex);
10246139Sjb150015 		if (sv->sv_state == SMB_SERVER_STATE_RUNNING) {
10256139Sjb150015 			mutex_exit(&sv->sv_mutex);
10266139Sjb150015 			rc = lmshrd_share_upcall(sv->sv_lmshrd, arg, add_share);
10276139Sjb150015 		} else {
10286139Sjb150015 			mutex_exit(&sv->sv_mutex);
10296139Sjb150015 			rc = EPERM;
10306139Sjb150015 		}
10316139Sjb150015 		smb_server_release(sv);
10326139Sjb150015 	}
10336139Sjb150015 	return (rc);
10346139Sjb150015 }
10356139Sjb150015 
10366139Sjb150015 /*
10376139Sjb150015  * *****************************************************************************
10386139Sjb150015  * **************** Functions called from the internal layers ******************
10396139Sjb150015  * *****************************************************************************
10406139Sjb150015  *
10416139Sjb150015  * These functions are provided the relevant smb server by the caller.
10426139Sjb150015  */
10436139Sjb150015 
10446139Sjb150015 void
10456139Sjb150015 smb_server_reconnection_check(smb_server_t *sv, smb_session_t *session)
10466139Sjb150015 {
10476139Sjb150015 	ASSERT(sv == session->s_server);
10486139Sjb150015 
10496139Sjb150015 	smb_session_reconnection_check(&sv->sv_nbt_daemon.ld_session_list,
10506139Sjb150015 	    session);
10516139Sjb150015 	smb_session_reconnection_check(&sv->sv_tcp_daemon.ld_session_list,
10526139Sjb150015 	    session);
10536139Sjb150015 }
10546139Sjb150015 
10556139Sjb150015 void
10566139Sjb150015 smb_server_get_cfg(smb_server_t *sv, smb_kmod_cfg_t *cfg)
10576139Sjb150015 {
10586139Sjb150015 	rw_enter(&sv->sv_cfg_lock, RW_READER);
10596139Sjb150015 	bcopy(&sv->sv_cfg, cfg, sizeof (*cfg));
10606139Sjb150015 	rw_exit(&sv->sv_cfg_lock);
10616139Sjb150015 }
10626139Sjb150015 
10636139Sjb150015 /*
10646139Sjb150015  * *****************************************************************************
10656139Sjb150015  * *************************** Static Functions ********************************
10666139Sjb150015  * *****************************************************************************
10676139Sjb150015  */
10686139Sjb150015 
10696139Sjb150015 static void
10706139Sjb150015 smb_server_timers(smb_thread_t *thread, void *arg)
10716139Sjb150015 {
10726139Sjb150015 	smb_server_t	*sv = (smb_server_t *)arg;
10736139Sjb150015 
10746139Sjb150015 	ASSERT(sv != NULL);
10756139Sjb150015 
10766139Sjb150015 	while (smb_thread_continue_timedwait(thread, 1 /* Seconds */)) {
10776139Sjb150015 		smb_session_timers(&sv->sv_nbt_daemon.ld_session_list);
10786139Sjb150015 		smb_session_timers(&sv->sv_tcp_daemon.ld_session_list);
10796139Sjb150015 	}
10806139Sjb150015 }
10816139Sjb150015 
10826139Sjb150015 /*
10836139Sjb150015  * smb_server_kstat_init
10846139Sjb150015  */
10856139Sjb150015 static int
10866139Sjb150015 smb_server_kstat_init(smb_server_t *sv)
10876139Sjb150015 {
10886432Sas200622 	(void) snprintf(sv->sv_ksp_name, sizeof (sv->sv_ksp_name), "%s%d",
10896432Sas200622 	    SMBSRV_KSTAT_NAME, sv->sv_zid);
10906139Sjb150015 
10916432Sas200622 	sv->sv_ksp = kstat_create(SMBSRV_KSTAT_MODULE, 0, sv->sv_ksp_name,
10926432Sas200622 	    SMBSRV_KSTAT_CLASS, KSTAT_TYPE_NAMED,
10936432Sas200622 	    sizeof (sv->sv_ks_data) / sizeof (kstat_named_t),
10946139Sjb150015 	    KSTAT_FLAG_VIRTUAL);
10956139Sjb150015 
10966139Sjb150015 	if (sv->sv_ksp) {
10976139Sjb150015 		(void) strlcpy(sv->sv_ks_data.state.name, "state",
10986139Sjb150015 		    sizeof (sv->sv_ks_data.state.name));
10996139Sjb150015 		sv->sv_ks_data.state.data_type = KSTAT_DATA_UINT32;
11006139Sjb150015 		(void) strlcpy(sv->sv_ks_data.open_files.name, "open_files",
11016139Sjb150015 		    sizeof (sv->sv_ks_data.open_files.name));
11026139Sjb150015 		sv->sv_ks_data.open_files.data_type = KSTAT_DATA_UINT32;
11036139Sjb150015 		(void) strlcpy(sv->sv_ks_data.open_trees.name, "connections",
11046139Sjb150015 		    sizeof (sv->sv_ks_data.open_trees.name));
11056139Sjb150015 		sv->sv_ks_data.open_trees.data_type = KSTAT_DATA_UINT32;
11066139Sjb150015 		(void) strlcpy(sv->sv_ks_data.open_users.name, "sessions",
11076139Sjb150015 		    sizeof (sv->sv_ks_data.open_users.name));
11086139Sjb150015 		sv->sv_ks_data.open_users.data_type = KSTAT_DATA_UINT32;
11096139Sjb150015 
11106139Sjb150015 		mutex_init(&sv->sv_ksp_mutex, NULL, MUTEX_DEFAULT, NULL);
11116139Sjb150015 		sv->sv_ksp->ks_lock = &sv->sv_ksp_mutex;
11126139Sjb150015 		sv->sv_ksp->ks_data = (void *)&sv->sv_ks_data;
11136139Sjb150015 		sv->sv_ksp->ks_update = smb_server_kstat_update_info;
11146139Sjb150015 		kstat_install(sv->sv_ksp);
11156139Sjb150015 	}
11166139Sjb150015 
11176139Sjb150015 	/* create and initialize smb kstats - smb_dispatch stats */
11186139Sjb150015 	smb_dispatch_kstat_init();
11196139Sjb150015 
11206139Sjb150015 	return (0);
11216139Sjb150015 }
11226139Sjb150015 
11236139Sjb150015 /*
11246139Sjb150015  * smb_server_kstat_fini
11256139Sjb150015  */
11266139Sjb150015 static void
11276139Sjb150015 smb_server_kstat_fini(smb_server_t *sv)
11286139Sjb150015 {
11296139Sjb150015 	if (sv->sv_ksp) {
11306139Sjb150015 		kstat_delete(sv->sv_ksp);
11316139Sjb150015 		mutex_destroy(&sv->sv_ksp_mutex);
11326139Sjb150015 		sv->sv_ksp = NULL;
11336139Sjb150015 	}
11346139Sjb150015 	smb_dispatch_kstat_fini();
11356139Sjb150015 }
11366139Sjb150015 
11376139Sjb150015 /* ARGSUSED */
11386139Sjb150015 static int
11396139Sjb150015 smb_server_kstat_update_info(kstat_t *ksp, int rw)
11406139Sjb150015 {
11416139Sjb150015 	smb_server_t	*sv;
11426139Sjb150015 
11436139Sjb150015 	if (rw == KSTAT_WRITE) {
11446139Sjb150015 		return (EACCES);
11456139Sjb150015 	} else {
11466139Sjb150015 		ASSERT(MUTEX_HELD(ksp->ks_lock));
11476139Sjb150015 
11486139Sjb150015 		_NOTE(LINTED("pointer cast may result in improper alignment"))
11496139Sjb150015 		sv = (smb_server_t *)((uint8_t *)(ksp->ks_data) -
11506139Sjb150015 		    offsetof(smb_server_t, sv_ks_data));
11516139Sjb150015 
11526139Sjb150015 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
11536139Sjb150015 
11546139Sjb150015 		sv->sv_ks_data.state.value.ui32 = sv->sv_state;
11556139Sjb150015 		sv->sv_ks_data.open_files.value.ui32 = sv->sv_open_files;
11566139Sjb150015 		sv->sv_ks_data.open_trees.value.ui32 = sv->sv_open_trees;
11576139Sjb150015 		sv->sv_ks_data.open_users.value.ui32 = sv->sv_open_users;
11586139Sjb150015 	}
11596139Sjb150015 	return (0);
11606139Sjb150015 }
11616139Sjb150015 
11626139Sjb150015 /*
11636139Sjb150015  * smb_server_stop
11646139Sjb150015  *
11656139Sjb150015  * The mutex of the server must have been entered before calling this function.
11666139Sjb150015  */
11676139Sjb150015 static void
11686139Sjb150015 smb_server_stop(smb_server_t *sv)
11696139Sjb150015 {
11706139Sjb150015 	ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
11716139Sjb150015 
11726139Sjb150015 	smb_winpipe_close();
11736139Sjb150015 	smb_thread_stop(&sv->si_thread_timers);
11746139Sjb150015 	smb_kdoor_clnt_stop();
11756139Sjb150015 	lmshrd_kclient_fini(sv->sv_lmshrd);
11766139Sjb150015 	smb_server_fsop_stop(sv);
11776139Sjb150015 	if (sv->sv_session) {
11786139Sjb150015 		smb_session_delete(sv->sv_session);
11796139Sjb150015 		sv->sv_session = NULL;
11806139Sjb150015 	}
11816139Sjb150015 }
11826139Sjb150015 
11836139Sjb150015 static int
11846139Sjb150015 smb_server_listen(
11856139Sjb150015     smb_server_t		*sv,
11866139Sjb150015     smb_listener_daemon_t	*ld,
11876139Sjb150015     in_port_t			port,
11886139Sjb150015     int				pthread_create_error)
11896139Sjb150015 {
11906139Sjb150015 	int			rc;
11916139Sjb150015 	struct sonode		*s_so;
11926139Sjb150015 	uint32_t		on = 1;
11936139Sjb150015 	smb_session_t		*session;
11946139Sjb150015 
11956139Sjb150015 	if (pthread_create_error) {
11966139Sjb150015 		/*
11976139Sjb150015 		 * Delete the last session created. The user space thread
11986139Sjb150015 		 * creation failed.
11996139Sjb150015 		 */
12006139Sjb150015 		smb_session_list_delete_tail(&ld->ld_session_list);
12016139Sjb150015 	}
12026139Sjb150015 
12036139Sjb150015 	if (ld->ld_so == NULL) {
12046139Sjb150015 		/* First time listener */
12056139Sjb150015 		ld->ld_sin.sin_family = AF_INET;
12066139Sjb150015 		ld->ld_sin.sin_port = htons(port);
12076139Sjb150015 		ld->ld_sin.sin_addr.s_addr = htonl(INADDR_ANY);
12086139Sjb150015 		ld->ld_so = smb_socreate(AF_INET, SOCK_STREAM, 0);
12096139Sjb150015 
12106139Sjb150015 		if (ld->ld_so) {
12116139Sjb150015 
12126139Sjb150015 			(void) sosetsockopt(ld->ld_so, SOL_SOCKET,
12136139Sjb150015 			    SO_REUSEADDR, (const void *)&on, sizeof (on));
12146139Sjb150015 
12156139Sjb150015 			rc = sobind(ld->ld_so, (struct sockaddr *)&ld->ld_sin,
12166139Sjb150015 			    sizeof (ld->ld_sin), 0, 0);
12176139Sjb150015 
12186139Sjb150015 			if (rc == 0) {
12196139Sjb150015 				rc =  solisten(ld->ld_so, 20);
12206139Sjb150015 				if (rc < 0) {
12216139Sjb150015 					cmn_err(CE_WARN,
12226139Sjb150015 					    "Port %d: listen failed", port);
12236139Sjb150015 					smb_soshutdown(ld->ld_so);
12246139Sjb150015 					smb_sodestroy(ld->ld_so);
12256139Sjb150015 					ld->ld_so = NULL;
12266139Sjb150015 					return (rc);
12276139Sjb150015 				}
12286139Sjb150015 			} else {
12296139Sjb150015 				cmn_err(CE_WARN,
12306139Sjb150015 				    "Port %d: bind failed", port);
12316139Sjb150015 				smb_soshutdown(ld->ld_so);
12326139Sjb150015 				smb_sodestroy(ld->ld_so);
12336139Sjb150015 				ld->ld_so = NULL;
12346139Sjb150015 				return (rc);
12356139Sjb150015 			}
12366139Sjb150015 		} else {
12376139Sjb150015 			cmn_err(CE_WARN,
12386139Sjb150015 			    "Port %d: socket create failed", port);
12396139Sjb150015 			return (ENOMEM);
12406139Sjb150015 		}
12416139Sjb150015 	}
12426139Sjb150015 
12436139Sjb150015 	DTRACE_PROBE1(so__wait__accept, struct sonode *, ld->ld_so);
12446139Sjb150015 
12456139Sjb150015 	for (;;) {
12466139Sjb150015 		rc = soaccept(ld->ld_so, 0, &s_so);
12476139Sjb150015 		if (rc == 0) {
12486139Sjb150015 			uint32_t	txbuf_size = 128*1024;
12496139Sjb150015 			uint32_t	on = 1;
12506139Sjb150015 
12516139Sjb150015 			DTRACE_PROBE1(so__accept, struct sonode *, s_so);
12526139Sjb150015 
12536139Sjb150015 			(void) sosetsockopt(s_so, IPPROTO_TCP, TCP_NODELAY,
12546139Sjb150015 			    (const void *)&on, sizeof (on));
12556139Sjb150015 			(void) sosetsockopt(s_so, SOL_SOCKET, SO_KEEPALIVE,
12566139Sjb150015 			    (const void *)&on, sizeof (on));
12576139Sjb150015 			(void) sosetsockopt(s_so, SOL_SOCKET, SO_SNDBUF,
12586139Sjb150015 			    (const void *)&txbuf_size, sizeof (txbuf_size));
12596139Sjb150015 			/*
12606139Sjb150015 			 * Create a session for this connection.
12616139Sjb150015 			 */
12626139Sjb150015 			session = smb_session_create(s_so, port, sv);
12636139Sjb150015 			if (session) {
12646139Sjb150015 				smb_session_list_append(&ld->ld_session_list,
12656139Sjb150015 				    session);
12666139Sjb150015 				break;
12676139Sjb150015 			} else {
12686139Sjb150015 				smb_soshutdown(s_so);
12696139Sjb150015 				smb_sodestroy(s_so);
12706139Sjb150015 			}
12716139Sjb150015 			continue;
12726139Sjb150015 		}
12736139Sjb150015 		smb_session_list_signal(&ld->ld_session_list);
12746139Sjb150015 		smb_soshutdown(ld->ld_so);
12756139Sjb150015 		smb_sodestroy(ld->ld_so);
12766139Sjb150015 		ld->ld_so = NULL;
12776139Sjb150015 		break;
12786139Sjb150015 	}
12796139Sjb150015 
12806139Sjb150015 	return (rc);
12816139Sjb150015 }
12826139Sjb150015 
12836139Sjb150015 /*
12846139Sjb150015  * smb_server_lookup
12856139Sjb150015  *
12866139Sjb150015  * This function tries to find the server associated with the zone of the
12876139Sjb150015  * caller.
12886139Sjb150015  */
12896139Sjb150015 static int
12906139Sjb150015 smb_server_lookup(smb_server_t **psv)
12916139Sjb150015 {
12926139Sjb150015 	zoneid_t	zid;
12936139Sjb150015 	smb_server_t	*sv;
12946139Sjb150015 
12956139Sjb150015 	zid = getzoneid();
12966139Sjb150015 
12976139Sjb150015 	smb_llist_enter(&smb_servers, RW_READER);
12986139Sjb150015 	sv = smb_llist_head(&smb_servers);
12996139Sjb150015 	while (sv) {
13006139Sjb150015 		ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
13016139Sjb150015 		if (sv->sv_zid == zid) {
13026139Sjb150015 			mutex_enter(&sv->sv_mutex);
13036139Sjb150015 			if (sv->sv_state != SMB_SERVER_STATE_DELETING) {
13046139Sjb150015 				sv->sv_refcnt++;
13056139Sjb150015 				mutex_exit(&sv->sv_mutex);
13066139Sjb150015 				smb_llist_exit(&smb_servers);
13076139Sjb150015 				*psv = sv;
13086139Sjb150015 				return (0);
13096139Sjb150015 			}
13106139Sjb150015 			mutex_exit(&sv->sv_mutex);
13116139Sjb150015 			break;
13126139Sjb150015 		}
13136139Sjb150015 		sv = smb_llist_next(&smb_servers, sv);
13146139Sjb150015 	}
13156139Sjb150015 	smb_llist_exit(&smb_servers);
13166139Sjb150015 	return (EPERM);
13176139Sjb150015 }
13186139Sjb150015 
13196139Sjb150015 /*
13206139Sjb150015  * smb_server_release
13216139Sjb150015  *
13226139Sjb150015  * This function decrements the reference count of the server and signals its
13236139Sjb150015  * condition variable if the state of the server is SMB_SERVER_STATE_DELETING.
13246139Sjb150015  */
13256139Sjb150015 static void
13266139Sjb150015 smb_server_release(smb_server_t *sv)
13276139Sjb150015 {
13286139Sjb150015 	ASSERT(sv->sv_magic == SMB_SERVER_MAGIC);
13296139Sjb150015 
13306139Sjb150015 	mutex_enter(&sv->sv_mutex);
13316139Sjb150015 	ASSERT(sv->sv_refcnt);
13326139Sjb150015 	sv->sv_refcnt--;
13336139Sjb150015 	if ((sv->sv_refcnt == 0) && (sv->sv_state == SMB_SERVER_STATE_DELETING))
13346139Sjb150015 		cv_signal(&sv->sv_cv);
13356139Sjb150015 	mutex_exit(&sv->sv_mutex);
13366139Sjb150015 }
13376139Sjb150015 
13386139Sjb150015 static int
13396139Sjb150015 smb_server_ulist_geti(
13406139Sjb150015     smb_session_list_t	*se,
13416139Sjb150015     int			offset,
13426139Sjb150015     smb_dr_user_ctx_t	*uinfo,
13436139Sjb150015     int			max_cnt)
13446139Sjb150015 {
13456139Sjb150015 	smb_session_t	*sn = NULL;
13466139Sjb150015 	smb_user_t	*user;
13476139Sjb150015 	smb_llist_t	*ulist;
13486139Sjb150015 	int		cnt = 0, skip = 0;
13496139Sjb150015 
13506139Sjb150015 	rw_enter(&se->se_lock, RW_READER);
13516139Sjb150015 	sn = list_head(&se->se_act.lst);
13526139Sjb150015 	while (sn && (cnt < max_cnt)) {
13536139Sjb150015 		ASSERT(sn->s_magic == SMB_SESSION_MAGIC);
13546139Sjb150015 		ulist = &sn->s_user_list;
13556139Sjb150015 		smb_llist_enter(ulist, RW_READER);
13566139Sjb150015 		user = smb_llist_head(ulist);
13576139Sjb150015 		while (user && (cnt < max_cnt)) {
13586139Sjb150015 			ASSERT(user->u_magic == SMB_USER_MAGIC);
13596139Sjb150015 			mutex_enter(&user->u_mutex);
13606139Sjb150015 			if (user->u_state == SMB_USER_STATE_LOGGED_IN) {
13616139Sjb150015 				if (skip++ < offset) {
13626139Sjb150015 					mutex_exit(&user->u_mutex);
13636139Sjb150015 					user = smb_llist_next(ulist, user);
13646139Sjb150015 					continue;
13656139Sjb150015 				}
13666139Sjb150015 
13676139Sjb150015 				if (smb_dr_user_create(uinfo, sn->s_kid,
13686139Sjb150015 				    user->u_uid, user->u_domain, user->u_name,
13696139Sjb150015 				    sn->workstation, sn->ipaddr, sn->native_os,
13706139Sjb150015 				    user->u_logon_time, user->u_flags) != 0) {
13716139Sjb150015 					mutex_exit(&user->u_mutex);
13726139Sjb150015 					user = smb_llist_next(ulist, user);
13736139Sjb150015 					continue;
13746139Sjb150015 				}
13756139Sjb150015 				uinfo++;
13766139Sjb150015 				cnt++;
13776139Sjb150015 			}
13786139Sjb150015 			mutex_exit(&user->u_mutex);
13796139Sjb150015 			user = smb_llist_next(ulist, user);
13806139Sjb150015 		}
13816139Sjb150015 		smb_llist_exit(ulist);
13826139Sjb150015 	}
13836139Sjb150015 	rw_exit(&se->se_lock);
13846139Sjb150015 	return (cnt);
13856139Sjb150015 }
13866139Sjb150015 
13876139Sjb150015 static void
13886139Sjb150015 smb_server_store_cfg(smb_server_t *sv, smb_kmod_cfg_t *cfg)
13896139Sjb150015 {
13906139Sjb150015 	if (cfg->skc_maxconnections == 0)
13916139Sjb150015 		cfg->skc_maxconnections = 0xFFFFFFFF;
13926139Sjb150015 
13936139Sjb150015 	/*
13946139Sjb150015 	 * XXX should not override configuration.
13956139Sjb150015 	 * For now, this disables server side
13966139Sjb150015 	 * signing regardless of configuration.
13976139Sjb150015 	 */
13986139Sjb150015 	cfg->skc_signing_enable = 0;
13996139Sjb150015 	cfg->skc_signing_required = 0;
14006139Sjb150015 	cfg->skc_signing_check = 0;
14016139Sjb150015 
14026139Sjb150015 	smb_session_correct_keep_alive_values(
14036139Sjb150015 	    &sv->sv_nbt_daemon.ld_session_list, cfg->skc_keepalive);
14046139Sjb150015 	smb_session_correct_keep_alive_values(
14056139Sjb150015 	    &sv->sv_tcp_daemon.ld_session_list, cfg->skc_keepalive);
14066139Sjb150015 
14076139Sjb150015 	/*
14086139Sjb150015 	 * XXX The following code was pulled from smb_oplock_init.
14096139Sjb150015 	 * It should be combined with with the config process if
14106139Sjb150015 	 * this info will be stored with the configuration or with
14116139Sjb150015 	 * the smb_fsop_start function if the data will be stored
14126139Sjb150015 	 * in the root of the fs.
14136139Sjb150015 	 */
14146139Sjb150015 
14156139Sjb150015 	/*
14166139Sjb150015 	 * XXX oplock enable flag.
14176139Sjb150015 	 * Should be stored in extended attribute in root of fs
14186139Sjb150015 	 * or a ZFS user-defined property.
14196139Sjb150015 	 */
14206139Sjb150015 	if (cfg->skc_oplock_enable == 0) {
14216139Sjb150015 		cmn_err(CE_NOTE, "SmbOplocks: disabled");
14226139Sjb150015 	}
14236139Sjb150015 
14246139Sjb150015 	bcopy(cfg, &sv->sv_cfg, sizeof (sv->sv_cfg));
14256139Sjb150015 }
14266139Sjb150015 
14276139Sjb150015 static int
14286139Sjb150015 smb_server_fsop_start(smb_server_t *sv)
14296139Sjb150015 {
14306139Sjb150015 	int	error;
14316139Sjb150015 
14326139Sjb150015 	error = smb_node_root_init(rootdir, sv, &sv->si_root_smb_node);
14336139Sjb150015 	if (error != 0)
14346139Sjb150015 		sv->si_root_smb_node = NULL;
14356139Sjb150015 
14366139Sjb150015 	return (error);
14376139Sjb150015 }
14386139Sjb150015 
14396139Sjb150015 static void
14406139Sjb150015 smb_server_fsop_stop(smb_server_t *sv)
14416139Sjb150015 {
14426139Sjb150015 	if (sv->si_root_smb_node != NULL) {
14436139Sjb150015 		smb_vfs_rele_all(sv);
14446139Sjb150015 		smb_node_release(sv->si_root_smb_node);
14456139Sjb150015 		sv->si_root_smb_node = NULL;
14466139Sjb150015 	}
14476139Sjb150015 }
1448