usr/src/cmd/rad/rad_container.c
author David Powell <david.e.powell@oracle.com>
Tue, 23 Nov 2010 15:54:20 -0800
changeset 604 20d9acfeb7fb
parent 575 e5ca78dfe938
child 650 265ab927a268
permissions -rw-r--r--
17421 name key order preservation 17422 rad dynamic namespace support 17423 rad kstat module

/*
 * 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 (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include <stddef.h>
#include <time.h>
#include <sys/types.h>
#include <atomic.h>

#include <rad/adr.h>
#include "rad_object.h"
#include "rad_error.h"
#include "rad_modapi.h"
#include "rad_event.h"
#include "rad_modapi_xport.h"
#include "rad_proto.h"
#include "rad_connection.h"
#include "rad_container.h"
#include "rad.h"

#include "api_errors.h"
#include "api_container.h"

void
cont_create(rad_container_t *c)
{
	rad_mutex_init(&c->c_lock);
	list_create(&c->c_instances, sizeof (rad_instance_t),
	    offsetof(rad_instance_t, i_cnode));
	list_create(&c->c_dynamic, sizeof (rad_dynamic_t),
	    offsetof(rad_dynamic_t, rd_node));
	c->c_ninstances = 0;
	c->c_id = 0;
	c->c_contobj = NULL;
}

conerr_t
cont_list(rad_container_t *c, adr_name_t *pattern, data_t **names)
{
	rad_mutex_enter(&c->c_lock);
	data_t *result = data_new_array(&t_array_string, c->c_ninstances);
	if (result == NULL)
		goto out;

	int n = 0;
	for (rad_instance_t *i = list_head(&c->c_instances); i != NULL;
	    i = list_next(&c->c_instances, i)) {
		if (!i->i_visible || !adr_name_match(i->i_name, pattern))
			continue;
		array_set(result, n++, data_new_string(i->i_namestr, lt_copy));
	}

	for (rad_dynamic_t *dyn = list_head(&c->c_dynamic); dyn;
	    dyn = list_next(&c->c_dynamic, dyn)) {
		data_t *n = NULL;
		if (dyn->rd_list == NULL)
			continue;

		if (dyn->rd_list(pattern, &n, dyn->rd_arg) == ce_ok) {
			result = array_addall(result, n);
		} else {
			data_free(result);
			result = NULL;
		}
		if (result == NULL)
			break;
	}

	*names = result;
out:
	rad_mutex_exit(&c->c_lock);
	return (result == NULL ? ce_nomem : ce_ok);
}


static rad_instance_t *
cont_lookup_int(rad_container_t *c, adr_name_t *name)
{
	for (rad_instance_t *inst = list_head(&c->c_instances); inst != NULL;
	    inst = list_next(&c->c_instances, inst))
		if (adr_name_cmp(inst->i_name, name) == 0)
			return (inst);
	return (NULL);
}

static conerr_t
cont_insert_unlocked(rad_container_t *c, rad_instance_t *inst, long long id);

conerr_t
cont_lookup(rad_container_t *c, const char *name, rad_instance_t **inst,
    data_t **error)
{
	adr_name_t *adrn = adr_name_fromstr(name);
	if (adrn == NULL)
		return (error_notfound(error, &e__target_object, name));

	rad_mutex_enter(&c->c_lock);
retry:
	*inst = cont_lookup_int(c, adrn);
	if (*inst == NULL)
		for (rad_dynamic_t *dyn = list_head(&c->c_dynamic); dyn;
		    dyn = list_next(&c->c_dynamic, dyn))
			if (adr_name_match(adrn, dyn->rd_pattern) &&
			    dyn->rd_lookup != NULL &&
			    dyn->rd_lookup(&adrn, inst, dyn->rd_arg) == ce_ok) {
				if (*inst == NULL)
					goto retry;
				cont_insert_unlocked(c, *inst, INST_ID_PICK);
				/*
				 * We defer to dynamic list for objects
				 * instantiated by dynamic lookup.
				 */
				(*inst)->i_visible = B_FALSE;
				break;
			}
	instance_hold(*inst);
	rad_mutex_exit(&c->c_lock);
	adr_name_rele(adrn);
	if (*inst == NULL)
		return (error_notfound(error, &e__target_object, name));

	return (ce_ok);
}

static rad_instance_t *
cont_lookup_id_int(rad_container_t *c, long long id)
{
	for (rad_instance_t *inst = list_head(&c->c_instances); inst != NULL;
	    inst = list_next(&c->c_instances, inst))
		if (inst->i_id == id)
			return (inst);
	return (NULL);
}

conerr_t
cont_lookup_id(rad_container_t *c, long long id, rad_instance_t **inst,
    data_t **error)
{
	rad_mutex_enter(&c->c_lock);
	*inst = cont_lookup_id_int(c, id);
	instance_hold(*inst);
	rad_mutex_exit(&c->c_lock);
	if (*inst == NULL)
		return (error_notfound(error, &e__target_object, NULL));

	return (ce_ok);
}

static void
cont_sendevent(rad_container_t *c, data_t *type, rad_instance_t *inst)
{
	if (c->c_contobj == NULL)
		return;

	data_t *event = data_new_struct(&t__update);
	struct_set(event, "type", type);
	if (inst != NULL)
		struct_set(event, "name", instance_getname(inst));

	if (data_verify(event, NULL, B_FALSE)) {
		rad_log(RL_DEBUG, "Sending container event");
		instance_notify(c->c_contobj, "updates", 0, event);
	} else {
		rad_log(RL_WARN, "Dropping container event");
		data_free(event);
	}
}

static volatile uint64_t instance_lastid = 0;

static conerr_t
cont_insert_unlocked(rad_container_t *c, rad_instance_t *inst, long long id)
{
	rad_instance_t **newarray;

	/* Should be RL_DEBUG, but RL_WARN for development is a good thing */
	if (rad_isproxy)
		rad_log(RL_NOTE, "adding instance to proxy: %s",
		    inst->i_namestr);

	if (cont_lookup_int(c, inst->i_name) != NULL ||
	    (id != INST_ID_PICK && cont_lookup_id_int(c, id) != NULL))
		return (ce_exists);

	c->c_ninstances++;
	list_insert_tail(&c->c_instances, inst);
	if (id == INST_ID_PICK) {
		inst->i_id = (rad_isproxy ? INST_ID_PROXY : 0) |
		    (long long)atomic_inc_64_nv(&instance_lastid);
	} else {
		assert((id & INST_ID_PROXY) ^
		    (rad_isproxy ? 0 : INST_ID_PROXY));
		inst->i_id = id;
	}
	instance_hold(inst);

	cont_sendevent(c, &e__nschange_add, inst);

	return (ce_ok);
}

conerr_t
cont_insert(rad_container_t *c, rad_instance_t *inst, long long id)
{
	conerr_t result;

	rad_mutex_enter(&c->c_lock);
	result = cont_insert_unlocked(c, inst, id);
	rad_mutex_exit(&c->c_lock);

	return (result);
}

conerr_t
cont_insert_singleton_id(rad_container_t *c, adr_name_t *name,
    rad_object_t *obj, long long id)
{
	rad_instance_t *inst = instance_create(name, obj, NULL, NULL);
	if (inst == NULL)
		return (ce_nomem);

	conerr_t err = cont_insert(c, inst, id);
	instance_rele(inst);
	return (err);
}

conerr_t
cont_insert_singleton(rad_container_t *c, adr_name_t *name, rad_object_t *obj)
{
	return (cont_insert_singleton_id(c, name, obj, INST_ID_PICK));
}

conerr_t
cont_remove(rad_container_t *c, rad_instance_t *inst)
{
	rad_mutex_enter(&c->c_lock);
	list_remove(&c->c_instances, inst);
	cont_sendevent(c, &e__nschange_remove, inst);
	rad_mutex_exit(&c->c_lock);
	instance_rele(inst);
	return (ce_ok);
}

/*
 * Insert singleton "container object" instance.
 * Provides things like notifications for addition/removal of instances.
 */
boolean_t
cont_insert_contobj(rad_container_t *c)
{
	adr_name_t *objname = adr_name_vcreate("org.opensolaris.os.rad", 1,
	    "type", "container");
	rad_instance_t *inst =
	    instance_create(objname, &api_container_svr, NULL, NULL);
	if (inst == NULL)
		return (B_FALSE);

	rad_mutex_enter(&c->c_lock);
	if (c->c_contobj != NULL)
		rad_log(RL_FATAL,
		    "Attempt to insert second container object detected");

	conerr_t err = cont_insert_unlocked(c, inst, INST_ID_PICK);
	if (err == ce_ok)
		c->c_contobj = inst;
	else
		instance_rele(inst);
	rad_mutex_exit(&c->c_lock);

	return (err == ce_ok);
}

conerr_t
cont_register_dynamic(rad_container_t *c, adr_name_t *pattern,
    rad_dyn_list_t listf, rad_dyn_lookup_t lookupf, void *arg)
{
	rad_dynamic_t *dyn = malloc(sizeof (rad_dynamic_t));
	if (dyn == NULL)
		return (ce_nomem);
	dyn->rd_pattern = pattern;
	dyn->rd_list = listf;
	dyn->rd_lookup = lookupf;
	dyn->rd_arg = arg;

	rad_mutex_enter(&c->c_lock);
	list_insert_tail(&c->c_dynamic, dyn);
	rad_mutex_exit(&c->c_lock);

	return (ce_ok);
}