object.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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
* 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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file only contains the transaction commit logic.
*/
#include <assert.h>
#include <alloca.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/sysmacros.h>
#include "configd.h"
struct tx_cmd {
const struct rep_protocol_transaction_cmd *tx_cmd;
const char *tx_prop;
char tx_found;
char tx_processed;
char tx_bad;
};
static int
{
}
struct tx_commit_data {
short txc_backend;
};
#define TX_COMMIT_DATA_SIZE(count) \
/*ARGSUSED*/
static int
{
else
return (BACKEND_CALLBACK_CONTINUE);
}
/*
* tx_process_property() is called once for each property in current
* property group generation. Its purpose is threefold:
*
* 1. copy properties not mentioned in the transaction over unchanged.
* 2. mark DELETEd properties as seen (they will be left out of the new
* generation).
* 3. consistancy-check NEW, CLEAR, and REPLACE commands.
*
* Any consistancy problems set tx_bad, and seen properties are marked
* tx_found. These is used later, in tx_process_cmds().
*/
/*ARGSUSED*/
static int
{
char *endptr;
"INSERT INTO prop_lnk_tbl"
" (lnk_pg_id, lnk_gen_id, lnk_prop_name, lnk_prop_type,"
" lnk_val_id) "
"VALUES ( %d, %d, '%q', '%q', %Q );",
} else {
if (lnk_val_id != NULL) {
errno = 0;
errno != 0) {
return (BACKEND_CALLBACK_ABORT);
}
} else {
elem->tx_orig_value_id = 0;
}
break;
prop_type[0] &&
prop_type[1]) {
data->txc_result =
}
break;
break;
break;
default:
assert(0);
break;
}
}
return (BACKEND_CALLBACK_CONTINUE);
}
/*
* tx_process_cmds() finishes the job tx_process_property() started:
*
* 1. if tx_process_property() marked a command as bad, we skip it.
* 2. if a DELETE, REPLACE, or CLEAR operated on a non-existant property,
* we mark it as bad.
* 3. we complete the work of NEW, REPLACE, and CLEAR, by inserting the
* appropriate values into the database.
* 4. we delete all replaced data, if it is no longer referenced.
*
* Finally, we check all of the commands, and fail if anything was marked bad.
*/
static int
{
int idx;
int r;
backend_query_t *q;
int do_delete;
/*
* For persistent pgs, we use backend_fail_if_seen to abort the
* deletion if there is a snapshot using our current state.
*
* All of the deletions in this function are safe, since
* rc_tx_commit() guarantees that all the data is in-cache.
*/
q = backend_query_alloc();
"SELECT 1 FROM snaplevel_lnk_tbl "
" WHERE (snaplvl_pg_id = %d AND snaplvl_gen_id = %d); ",
}
"DELETE FROM prop_lnk_tbl"
" WHERE (lnk_pg_id = %d AND lnk_gen_id = %d)",
if (r == REP_PROTOCOL_SUCCESS)
do_delete = 1;
else if (r == REP_PROTOCOL_DONE)
do_delete = 0; /* old gen_id is in use */
else
return (r);
continue;
continue;
}
break;
break;
default:
assert(0);
break;
}
if (do_delete &&
elem->tx_orig_value_id != 0) {
/*
* delete the old values, if they are not in use
*/
q = backend_query_alloc();
"SELECT 1 FROM prop_lnk_tbl "
" WHERE (lnk_val_id = %d); "
"DELETE FROM value_tbl"
" WHERE (value_id = %d)",
if (r != REP_PROTOCOL_SUCCESS && r != REP_PROTOCOL_DONE)
return (r);
}
continue; /* no further work to do */
type[2] = 0;
if (elem->tx_nvalues == 0) {
"INSERT INTO prop_lnk_tbl"
" (lnk_pg_id, lnk_gen_id, "
" lnk_prop_name, lnk_prop_type, lnk_val_id) "
"VALUES ( %d, %d, '%q', '%q', NULL );",
type);
} else {
uint32_t *v;
const char *str;
if (val_id == 0)
return (REP_PROTOCOL_FAIL_NO_RESOURCES);
"INSERT INTO prop_lnk_tbl "
" (lnk_pg_id, lnk_gen_id, "
" lnk_prop_name, lnk_prop_type, lnk_val_id) "
"VALUES ( %d, %d, '%q', '%q', %d );",
while (r == REP_PROTOCOL_SUCCESS &&
elem->tx_nvalues--) {
str = (const char *)&v[1];
"INSERT INTO value_tbl "
" (value_id, value_type, value_value) "
"VALUES (%d, '%c', '%q');\n",
/*LINTED alignment*/
}
}
if (r != REP_PROTOCOL_SUCCESS)
return (REP_PROTOCOL_FAIL_UNKNOWN);
}
return (REP_PROTOCOL_FAIL_BAD_TX);
}
return (REP_PROTOCOL_SUCCESS);
}
static boolean_t
{
return (0);
return (1);
}
static int
{
const struct rep_protocol_transaction_cmd *cmds;
int idx;
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
}
cur->tx_nvalues = 0;
while (sz > 0) {
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
cur->tx_nvalues++;
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
/*
* XXX here, we should be checking that the values
* match the purported type
*/
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
}
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
}
return (REP_PROTOCOL_SUCCESS);
}
int
{
const struct rep_protocol_transaction_cmd *cmds;
struct tx_commit_data *data;
int ret;
backend_query_t *q;
/*
* First, verify that the reported sizes make sense, and count
* the number of commands.
*/
count = 0;
while (cmds_sz > 0) {
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
count++;
}
/*
* verify that everything looks okay, and set up our command
* datastructures.
*/
if (ret != REP_PROTOCOL_SUCCESS)
return (ret);
if (ret != REP_PROTOCOL_SUCCESS)
return (ret);
/* Make sure the pg is up-to-date. */
q = backend_query_alloc();
backend_query_add(q, "SELECT pg_gen_id FROM pg_tbl WHERE (pg_id = %d);",
lp->rl_main_id);
if (r != REP_PROTOCOL_SUCCESS ||
goto end;
}
/* If the transaction is empty, cut out early. */
if (count == 0) {
r = REP_PROTOCOL_DONE;
goto end;
}
if (new_gen == 0) {
return (REP_PROTOCOL_FAIL_NO_RESOURCES);
}
r = backend_tx_run_update(tx,
"UPDATE pg_tbl SET pg_gen_id = %d "
" WHERE (pg_id = %d AND pg_gen_id = %d);",
if (r != REP_PROTOCOL_SUCCESS) {
goto end;
}
q = backend_query_alloc();
"SELECT lnk_prop_name, lnk_prop_type, lnk_val_id "
"FROM prop_lnk_tbl "
"WHERE (lnk_pg_id = %d AND lnk_gen_id = %d)",
if (r == REP_PROTOCOL_DONE)
r = REP_PROTOCOL_FAIL_UNKNOWN; /* corruption */
if (r != REP_PROTOCOL_SUCCESS ||
goto end;
}
if (r != REP_PROTOCOL_SUCCESS) {
goto end;
}
r = tx_process_cmds(data);
if (r != REP_PROTOCOL_SUCCESS) {
goto end;
}
r = backend_tx_commit(tx);
if (r == REP_PROTOCOL_SUCCESS)
end:
return (r);
}