xref: /illumos-gate/usr/src/lib/pkcs11/libpkcs11/common/metaSessionManager.c (revision 8cae6764ee663bd6a0b140294645a7c2fd8399b8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d3a28a55Sdinak  * Common Development and Distribution License (the "License").
6d3a28a55Sdinak  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*8cae6764SAnthony Scarpino  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdlib.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include "metaGlobal.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * The list and the list lock are global for two external uses:
327c478bd9Sstevel@tonic-gate  * 1) C_CloseAllSessions need to close the head (repeatedly,
337c478bd9Sstevel@tonic-gate  *    until no more sessions exist).
347c478bd9Sstevel@tonic-gate  * 2) meta_object_find_by_handle needs to walk all sessions,
357c478bd9Sstevel@tonic-gate  *    searching each session object list for matching objects.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate pthread_rwlock_t meta_sessionlist_lock;
387c478bd9Sstevel@tonic-gate meta_session_t *meta_sessionlist_head;
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * The following 2 variables are used for tracking the number of
427c478bd9Sstevel@tonic-gate  * sessions and number of rw sessios that are currently open
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * They are being manipulated in the metaSession.c file, and being
457c478bd9Sstevel@tonic-gate  * referenced in the metaSlotToken.c file
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate CK_ULONG num_meta_sessions;
487c478bd9Sstevel@tonic-gate CK_ULONG num_rw_meta_sessions;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static pthread_rwlock_t meta_sessionclose_lock;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * meta_sessionManager_initialize
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * Called from meta_Initialize.  Initializes all the variables used
597c478bd9Sstevel@tonic-gate  * by the session manager.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate CK_RV
meta_sessionManager_initialize()627c478bd9Sstevel@tonic-gate meta_sessionManager_initialize()
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (pthread_rwlock_init(&meta_sessionlist_lock, NULL) != 0) {
667c478bd9Sstevel@tonic-gate 		return (CKR_FUNCTION_FAILED);
677c478bd9Sstevel@tonic-gate 	}
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	if (pthread_rwlock_init(&meta_sessionclose_lock, NULL) != 0) {
707c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_destroy(&meta_sessionlist_lock);
717c478bd9Sstevel@tonic-gate 		return (CKR_FUNCTION_FAILED);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	meta_sessionlist_head = NULL;
757c478bd9Sstevel@tonic-gate 	num_meta_sessions = 0;
767c478bd9Sstevel@tonic-gate 	num_rw_meta_sessions = 0;
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	return (CKR_OK);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * meta_sessionManager_finalize
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  * Close all sessions, and destroy all the locks
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate void
meta_sessionManager_finalize()877c478bd9Sstevel@tonic-gate meta_sessionManager_finalize()
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	/*
907c478bd9Sstevel@tonic-gate 	 * Close any remaining metasessions, can just simply call
917c478bd9Sstevel@tonic-gate 	 * meta_CloseAllSessions.  The METASLOT_SLOTID argument is
927c478bd9Sstevel@tonic-gate 	 * not used, but need to be passed in.
937c478bd9Sstevel@tonic-gate 	 */
947c478bd9Sstevel@tonic-gate 	(void) meta_CloseAllSessions(METASLOT_SLOTID);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_destroy(&meta_sessionclose_lock);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_destroy(&meta_sessionlist_lock);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * meta_handle2session
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  * Convert a CK_SESSION_HANDLE to the corresponding metasession. If
105f243d98aSkrishna  * successful, a write-lock on the session will be held to indicate
1067c478bd9Sstevel@tonic-gate  * that it's in use. Call REFRELEASE() when finished.
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate CK_RV
meta_handle2session(CK_SESSION_HANDLE hSession,meta_session_t ** session)1107c478bd9Sstevel@tonic-gate meta_handle2session(CK_SESSION_HANDLE hSession, meta_session_t **session)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	meta_session_t *tmp_session = (meta_session_t *)(hSession);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/* Check for bad args (eg CK_INVALID_HANDLE, which is 0/NULL). */
115f243d98aSkrishna 	if (tmp_session == NULL ||
116f243d98aSkrishna 	    tmp_session->magic_marker != METASLOT_SESSION_MAGIC) {
1177c478bd9Sstevel@tonic-gate 		return (CKR_SESSION_HANDLE_INVALID);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * sessions can only be used by a single thread at a time.
1227c478bd9Sstevel@tonic-gate 	 * So, we need to get a write-lock.
1237c478bd9Sstevel@tonic-gate 	 */
1247c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_wrlock(&tmp_session->session_lock);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/* Make sure this session is not in the process of being deleted */
1277c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&tmp_session->isClosingSession_lock);
1287c478bd9Sstevel@tonic-gate 	if (tmp_session->isClosingSession) {
1297c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(
1307c478bd9Sstevel@tonic-gate 		    &tmp_session->isClosingSession_lock);
1317c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&tmp_session->session_lock);
1327c478bd9Sstevel@tonic-gate 		return (CKR_SESSION_HANDLE_INVALID);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&tmp_session->isClosingSession_lock);
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	*session = tmp_session;
1377c478bd9Sstevel@tonic-gate 	return (CKR_OK);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * meta_session_alloc
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate CK_RV
meta_session_alloc(meta_session_t ** session)1457c478bd9Sstevel@tonic-gate meta_session_alloc(meta_session_t **session)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	meta_session_t *new_session;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/* Allocate memory for the session. */
1507c478bd9Sstevel@tonic-gate 	new_session = calloc(1, sizeof (meta_session_t));
1517c478bd9Sstevel@tonic-gate 	if (new_session == NULL)
1527c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	(new_session->mech_support_info).supporting_slots
1557c478bd9Sstevel@tonic-gate 	    = malloc(meta_slotManager_get_slotcount() * sizeof (mechinfo_t *));
1567c478bd9Sstevel@tonic-gate 	if ((new_session->mech_support_info).supporting_slots == NULL) {
1577c478bd9Sstevel@tonic-gate 		free(new_session);
1587c478bd9Sstevel@tonic-gate 		return (CKR_HOST_MEMORY);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	(new_session->mech_support_info).num_supporting_slots = 0;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	new_session->magic_marker = METASLOT_SESSION_MAGIC;
1637c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_init(&new_session->session_lock, NULL);
1647c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&new_session->isClosingSession_lock, NULL);
1657c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_init(&new_session->object_list_lock, NULL);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	*session = new_session;
1687c478bd9Sstevel@tonic-gate 	return (CKR_OK);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * meta_session_activate
1747c478bd9Sstevel@tonic-gate  *
1757c478bd9Sstevel@tonic-gate  * Create and add a session to the list of active meta sessions.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate CK_RV
meta_session_activate(meta_session_t * session)1787c478bd9Sstevel@tonic-gate meta_session_activate(meta_session_t *session)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/* Add session to the list of sessions. */
1837c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_wrlock(&meta_sessionlist_lock);
1847c478bd9Sstevel@tonic-gate 	INSERT_INTO_LIST(meta_sessionlist_head, session);
1857c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	return (rv);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * meta_session_deactivate
1927c478bd9Sstevel@tonic-gate  *
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate CK_RV
meta_session_deactivate(meta_session_t * session,boolean_t have_sessionlist_lock)1967c478bd9Sstevel@tonic-gate meta_session_deactivate(meta_session_t *session,
1977c478bd9Sstevel@tonic-gate     boolean_t have_sessionlist_lock)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	boolean_t isLastSession = B_FALSE;
2007c478bd9Sstevel@tonic-gate 	meta_object_t *object;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	/* Safely resolve attempts of concurrent-close */
2037c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session->isClosingSession_lock);
2047c478bd9Sstevel@tonic-gate 	if (session->isClosingSession) {
2057c478bd9Sstevel@tonic-gate 		/* Lost a delete race. */
2067c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&session->isClosingSession_lock);
2077c478bd9Sstevel@tonic-gate 		REFRELEASE(session);
2087c478bd9Sstevel@tonic-gate 		return (CKR_SESSION_HANDLE_INVALID);
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	session->isClosingSession = B_TRUE;
211f243d98aSkrishna 	session->magic_marker = METASLOT_SESSION_BADMAGIC;
2127c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session->isClosingSession_lock);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * Remove session from the session list. Once removed, it will not
2167c478bd9Sstevel@tonic-gate 	 * be possible for another thread to begin using the session.
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_wrlock(&meta_sessionclose_lock);
2197c478bd9Sstevel@tonic-gate 	if (!have_sessionlist_lock) {
2207c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_wrlock(&meta_sessionlist_lock);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	REMOVE_FROM_LIST(meta_sessionlist_head, session);
2247c478bd9Sstevel@tonic-gate 	if (meta_sessionlist_head == NULL) {
2257c478bd9Sstevel@tonic-gate 		isLastSession = B_TRUE;
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 	if (!have_sessionlist_lock) {
2287c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&meta_sessionlist_lock);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&meta_sessionclose_lock);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&session->session_lock);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/* Cleanup any in-progress operations. */
235d3a28a55Sdinak 	if (session->op1.type != 0) {
2367c478bd9Sstevel@tonic-gate 		meta_operation_cleanup(session, session->op1.type, FALSE);
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (session->op1.session != NULL) {
2407c478bd9Sstevel@tonic-gate 		meta_release_slot_session(session->op1.session);
2417c478bd9Sstevel@tonic-gate 		session->op1.session = NULL;
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* Remove all the session metaobjects created in this session. */
2457c478bd9Sstevel@tonic-gate 	/* Basically, emulate C_DestroyObject, including safety h2s */
2467c478bd9Sstevel@tonic-gate 	while ((object = session->object_list_head) != NULL) {
2477c478bd9Sstevel@tonic-gate 		CK_RV rv;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 		rv = meta_handle2object((CK_OBJECT_HANDLE)object, &object);
2507c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
2517c478bd9Sstevel@tonic-gate 			/* Can only happen if someone else just closed it. */
2527c478bd9Sstevel@tonic-gate 			continue;
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		rv = meta_object_deactivate(object, B_FALSE, B_TRUE);
2567c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
2577c478bd9Sstevel@tonic-gate 			continue;
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 
260*8cae6764SAnthony Scarpino 		rv = meta_object_dealloc(NULL, object, B_FALSE);
2617c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
2627c478bd9Sstevel@tonic-gate 			continue;
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if ((isLastSession) && (metaslot_logged_in())) {
2687c478bd9Sstevel@tonic-gate 		slot_session_t *slotsessp;
2697c478bd9Sstevel@tonic-gate 		CK_RV rv;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		rv = meta_get_slot_session(get_keystore_slotnum(), &slotsessp,
2727c478bd9Sstevel@tonic-gate 		    session->session_flags);
2737c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
2747c478bd9Sstevel@tonic-gate 			return (rv);
2757c478bd9Sstevel@tonic-gate 		rv = FUNCLIST(slotsessp->fw_st_id)->C_Logout(
2767c478bd9Sstevel@tonic-gate 		    slotsessp->hSession);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		meta_release_slot_session(slotsessp);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		/* if C_Logout fails, just ignore the error */
2817c478bd9Sstevel@tonic-gate 		metaslot_set_logged_in_flag(B_FALSE);
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK)
2847c478bd9Sstevel@tonic-gate 			return (rv);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		/* need to deactivate all the PRIVATE token objects */
2877c478bd9Sstevel@tonic-gate 		rv = meta_token_object_deactivate(PRIVATE_TOKEN);
2887c478bd9Sstevel@tonic-gate 		if (rv != CKR_OK) {
2897c478bd9Sstevel@tonic-gate 			return (rv);
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	return (CKR_OK);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * meta_session_dealloc
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  * Release the resources held by a metasession. If the session has been
3017c478bd9Sstevel@tonic-gate  * activated, it must be deactivated first.
3027c478bd9Sstevel@tonic-gate  */
3037c478bd9Sstevel@tonic-gate void
meta_session_dealloc(meta_session_t * session)3047c478bd9Sstevel@tonic-gate meta_session_dealloc(meta_session_t *session)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	if ((session->find_objs_info).matched_objs) {
3077c478bd9Sstevel@tonic-gate 		free((session->find_objs_info).matched_objs);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	free((session->mech_support_info).supporting_slots);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	/*
3137c478bd9Sstevel@tonic-gate 	 * If there were active operations, cleanup the slot session so that
3147c478bd9Sstevel@tonic-gate 	 * it can be reused (otherwise provider might complain that an
3157c478bd9Sstevel@tonic-gate 	 * operation is active).
3167c478bd9Sstevel@tonic-gate 	 */
317d3a28a55Sdinak 	if (session->op1.type != 0)
3187c478bd9Sstevel@tonic-gate 		meta_operation_cleanup(session, session->op1.type, FALSE);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/* Final object cleanup. */
3217c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_destroy(&session->session_lock);
3227c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&session->isClosingSession_lock);
3237c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_destroy(&session->object_list_lock);
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	meta_session_delay_free(session);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * This function adds the to-be-freed meta session to a linked list.
3307c478bd9Sstevel@tonic-gate  * When the number of sessions queued in the linked list reaches the
3317c478bd9Sstevel@tonic-gate  * maximum threshold MAX_SESSION_TO_BE_FREED, it will free the first
3327c478bd9Sstevel@tonic-gate  * session (FIFO) in the list.
3337c478bd9Sstevel@tonic-gate  */
3347c478bd9Sstevel@tonic-gate void
meta_session_delay_free(meta_session_t * sp)3357c478bd9Sstevel@tonic-gate meta_session_delay_free(meta_session_t *sp)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	meta_session_t *tmp;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&ses_delay_freed.ses_to_be_free_mutex);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* Add the newly deleted session at the end of the list */
3427c478bd9Sstevel@tonic-gate 	sp->next = NULL;
3437c478bd9Sstevel@tonic-gate 	if (ses_delay_freed.first == NULL) {
3447c478bd9Sstevel@tonic-gate 		ses_delay_freed.last = sp;
3457c478bd9Sstevel@tonic-gate 		ses_delay_freed.first = sp;
3467c478bd9Sstevel@tonic-gate 	} else {
3477c478bd9Sstevel@tonic-gate 		ses_delay_freed.last->next = sp;
3487c478bd9Sstevel@tonic-gate 		ses_delay_freed.last = sp;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	if (++ses_delay_freed.count >= MAX_SESSION_TO_BE_FREED) {
3527c478bd9Sstevel@tonic-gate 		/*
3537c478bd9Sstevel@tonic-gate 		 * Free the first session in the list only if
3547c478bd9Sstevel@tonic-gate 		 * the total count reaches maximum threshold.
3557c478bd9Sstevel@tonic-gate 		 */
3567c478bd9Sstevel@tonic-gate 		ses_delay_freed.count--;
3577c478bd9Sstevel@tonic-gate 		tmp = ses_delay_freed.first->next;
3587c478bd9Sstevel@tonic-gate 		free(ses_delay_freed.first);
3597c478bd9Sstevel@tonic-gate 		ses_delay_freed.first = tmp;
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&ses_delay_freed.ses_to_be_free_mutex);
3627c478bd9Sstevel@tonic-gate }
363