summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.lib/dsvclockd/datastore.c
blob: e0d0def37552b5cb4a8ee290ce7414887a84cac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <stropts.h>
#include <synch.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <dhcp_svc_private.h>
#include <sys/time.h>
#include <dhcpmsg.h>

#include "dsvclockd.h"
#include "datastore.h"

static uint32_t		ds_hash(const char *);

/*
 * Create a datastore named `ds_name' and a door which will service requests
 * for this datastore.  When the door is called, callback `ds_callback'.
 * Returns the created datastore.
 */
dsvcd_datastore_t *
ds_create(const char *ds_name, dsvcd_svc_t *ds_callback)
{
	char			door_path[MAXPATHLEN];
	dsvcd_datastore_t	*ds = NULL;
	int			fd;
	unsigned int		i;
	door_info_t		info;

	dhcpmsg(MSG_VERBOSE, "managing locks for datastore `%s'", ds_name);

	ds = malloc(sizeof (dsvcd_datastore_t));
	if (ds == NULL) {
		dhcpmsg(MSG_ERR, "cannot manage locks for datastore `%s'",
		    ds_name);
		return (NULL);
	}

	ds->ds_name = strdup(ds_name);
	if (ds->ds_name == NULL) {
		dhcpmsg(MSG_ERR, "cannot manage locks for datastore `%s'",
		    ds_name);
		free(ds);
		return (NULL);
	}

	ds->ds_doorfd = door_create((void (*)())ds_callback, ds,
	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
	if (ds->ds_doorfd == -1) {
		dhcpmsg(MSG_ERR, "cannot create door for datastore `%s'",
		    ds_name);
		free(ds->ds_name);
		free(ds);
		return (NULL);
	}

	for (i = 0; i < DSVCD_DS_HASH_SIZE; i++) {
		ds->ds_hash[i].cl_head = NULL;
		(void) mutex_init(&ds->ds_hash[i].cl_lock, USYNC_THREAD, 0);
	}

	/*
	 * Create the door name in the filesystem.  First, check to see if
	 * a door already exists at the specified pathname.  If it does,
	 * and the server process (no doubt another copy of us) is already
	 * running, then fail.  Otherwise, unlink the old door and fattach
	 * a new one.
	 */
	(void) snprintf(door_path, sizeof (door_path), DSVCD_DOOR_FMT, ds_name);

	fd = open(door_path, O_RDWR);
	if (fd != -1) {
		if (door_info(fd, &info) == 0 && info.di_target != -1) {
			dhcpmsg(MSG_ERROR, "%s is in use by process %lu",
			    door_path, info.di_target);
			(void) close(fd);
			(void) close(ds->ds_doorfd);
			free(ds->ds_name);
			free(ds);
			return (NULL);
		}
		(void) close(fd);
		(void) unlink(door_path);
	}

	fd = open(door_path, O_CREAT|O_EXCL|O_RDWR, 0644);
	if (fd == -1) {
		dhcpmsg(MSG_ERR, "cannot create door rendezvous for datastore "
		    "`%s'", ds_name);
		(void) close(ds->ds_doorfd);
		free(ds->ds_name);
		free(ds);
		return (NULL);
	}
	(void) close(fd);

	/*
	 * Attach the door onto the name
	 */
	if (fattach(ds->ds_doorfd, door_path) == -1) {
		dhcpmsg(MSG_ERR, "cannot fattach door rendezvous for datastore "
		    "`%s'", ds_name);
		(void) close(ds->ds_doorfd);
		free(ds->ds_name);
		free(ds);
		return (NULL);
	}

	return (ds);
}

/*
 * Destroy a datastore `ds' and its associated containers, and remove
 * its door from the filesystem.
 */
void
ds_destroy(dsvcd_datastore_t *ds)
{
	unsigned int		i;
	char			door_path[MAXPATHLEN];
	dsvcd_container_t	*cn, *cn_next;

	dhcpmsg(MSG_VERBOSE, "stopping lock management for datastore `%s'",
	    ds->ds_name);

	/*
	 * Detach and revoke access to the door.  The detach makes it so
	 * new callers who open the door will fail; the revoke makes it
	 * so that callers that already have a door descriptor will fail.
	 * We do this prior to calling cn_destroy() to make it easier for
	 * the container lockcount to drain.
	 */
	(void) snprintf(door_path, MAXPATHLEN, DSVCD_DOOR_FMT, ds->ds_name);
	(void) fdetach(door_path);
	(void) unlink(door_path);
	(void) door_revoke(ds->ds_doorfd);
	(void) close(ds->ds_doorfd);

	/*
	 * Destroy all the underlying containers.  We're single-threaded at
	 * this point, so don't worry about locks.
	 */
	for (i = 0; i < DSVCD_DS_HASH_SIZE; i++) {
		for (cn = ds->ds_hash[i].cl_head; cn != NULL; cn = cn_next) {
			cn_next = cn->cn_next;
			cn_destroy(cn);
		}
		(void) mutex_destroy(&ds->ds_hash[i].cl_lock);
	}

	free(ds->ds_name);
	free(ds);
}

/*
 * Get a container with id `cn_id' from datastore `ds'; create the
 * container if it does not exist.  If `crosshost' is set and the container
 * does not yet exist, then the container will synchronize across hosts.  .
 * If the container cannot be found or created, NULL is returned.  When the
 * calling thread is done with the container, ds_release_container() must
 * be called.
 */
dsvcd_container_t *
ds_get_container(dsvcd_datastore_t *ds, const char *cn_id, boolean_t crosshost)
{
	dsvcd_container_list_t	*cn_list;
	dsvcd_container_t	*cn;
	uint32_t		idhash = ds_hash(cn_id);

	cn_list = &ds->ds_hash[idhash % DSVCD_DS_HASH_SIZE];
	(void) mutex_lock(&cn_list->cl_lock);

	for (cn = cn_list->cl_head; cn != NULL; cn = cn->cn_next) {
		if (idhash == cn->cn_idhash && strcmp(cn_id, cn->cn_id) == 0)
			break;
	}

	if (cn == NULL) {
		cn = cn_create(cn_id, crosshost);
		if (cn != NULL) {
			if (cn_list->cl_head != NULL)
				cn_list->cl_head->cn_prev = cn;

			cn->cn_next	 = cn_list->cl_head;
			cn->cn_prev	 = NULL;
			cn_list->cl_head = cn;
			cn->cn_idhash	 = idhash;
			cn->cn_nout	 = 0;
			cn->cn_lastrel	 = 0;
		}
	}

	if (cn != NULL)
		cn->cn_nout++;

	(void) mutex_unlock(&cn_list->cl_lock);
	return (cn);
}

/*
 * Release a container `cn' belonging to datastore `ds'.  Once a container
 * has been released, it can no longer be used by the releasing thread.
 * Used to track the number of active instances of a container.
 */
void
ds_release_container(dsvcd_datastore_t *ds, dsvcd_container_t *cn)
{
	dsvcd_container_list_t	*cn_list;
	uint32_t		idhash = ds_hash(cn->cn_id);

	cn_list = &ds->ds_hash[idhash % DSVCD_DS_HASH_SIZE];

	(void) mutex_lock(&cn_list->cl_lock);

	cn->cn_nout--;
	cn->cn_lastrel = time(NULL);

	(void) mutex_unlock(&cn_list->cl_lock);
}

/*
 * Destroy any containers in datastore `ds' that have not been accessed in
 * the last `idle' seconds.  Return the number of destroyed (reaped)
 * containers.
 */
unsigned int
ds_reap_containers(dsvcd_datastore_t *ds, unsigned int idle)
{
	dsvcd_container_list_t	*cn_list;
	dsvcd_container_t	*cn, *cn_next;
	unsigned int		i, nreaped = 0;

	for (i = 0; i < DSVCD_DS_HASH_SIZE; i++) {
		cn_list = &ds->ds_hash[i];

		(void) mutex_lock(&cn_list->cl_lock);
		for (cn = cn_list->cl_head; cn != NULL; cn = cn_next) {
			cn_next = cn->cn_next;

			/*
			 * Since a container is not checked out across a
			 * lock operation, we must check if the lock is
			 * held as well as the number of instances checked
			 * out.
			 */
			if (cn->cn_nout != 0 ||
			    cn_locktype(cn) != DSVCD_NOLOCK ||
			    cn->cn_lastrel + idle >= time(NULL))
				continue;

			if (cn == cn_list->cl_head)
				cn_list->cl_head = cn->cn_next;
			else
				cn->cn_prev->cn_next = cn->cn_next;

			if (cn->cn_next != NULL)
				cn->cn_next->cn_prev = cn->cn_prev;

			cn_destroy(cn);
			nreaped++;
		}
		(void) mutex_unlock(&cn_list->cl_lock);
	}

	return (nreaped);
}

/*
 * Hash a container identified by `cn_id' into a 32-bit unsigned integer
 * suitable for use as a key in a hash table.
 */
static uint32_t
ds_hash(const char *cn_id)
{
	uint32_t	result = 0;
	unsigned int	i;

	for (i = 0; cn_id[i] != '\0'; i++)
		result += cn_id[i] << i;

	return (result);
}