/*
* 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
* 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 2008 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 char *tx_prop;
char tx_found;
char tx_processed;
char tx_bad;
};
static int
{
}
struct tx_commit_data {
short txc_backend;
};
/*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, i = 0;
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 );",
for (i = 0; i < elem->tx_nvalues; i++) {
str = (const char *)&v[1];
/*
* Update values in backend, imposing
* ordering via the value_order column.
* This ordering is then used in subseqent
* value retrieval operations. We can
* safely assume that the repository schema
* has been upgraded (and hence has the
* value_order column in value_tbl), since
* it is upgraded as soon as the repository
* is writable.
*/
"INSERT INTO value_tbl (value_id, "
"value_type, value_value, "
"value_order) VALUES (%d, '%c', "
"'%q', '%d');\n",
str, i);
if (r != REP_PROTOCOL_SUCCESS)
break;
/*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
{
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);
}
/*
* Free the memory associated with a tx_commit_data structure.
*/
void
{
}
/*
* Parse the data of a REP_PROTOCOL_PROPERTYGRP_TX_COMMIT message into a
* more useful form. The data in the message will be represented by a
* tx_commit_data_t structure which is allocated by this function. The
* address of the allocated structure is returned to *tx_data and must be
* freed by calling tx_commit_data_free().
*
* Parameters:
* cmds_arg Address of the commands in the
* REP_PROTOCOL_PROPERTYGRP_TX_COMMIT message.
*
* cmds_sz Number of message bytes at cmds_arg.
*
* tx_data Points to the place to receive the address of the
* allocated memory.
*
* Fails with
* _BAD_REQUEST
* _NO_RESOURCES
*/
int
{
int ret;
/*
* 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++;
}
return (REP_PROTOCOL_FAIL_NO_RESOURCES);
/*
* verify that everything looks okay, and set up our command
* datastructures.
*/
if (ret == REP_PROTOCOL_SUCCESS) {
} else {
}
return (ret);
}
/*
* The following are a set of accessor functions to retrieve data from a
* tx_commit_data_t that has been allocated by tx_commit_data_new().
*/
/*
* Return the action of the transaction command whose command number is
* cmd_no. The action is placed at *action.
*
* Returns:
* _FAIL_BAD_REQUEST cmd_no is out of range.
*/
int
{
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
return (REP_PROTOCOL_SUCCESS);
}
/*
* Return the number of transaction commands held in tx_data.
*/
{
}
/*
* Return the number of property values that are associated with the
* transaction command whose number is cmd_no. The number of values is
* returned to *nvalues.
*
* Returns:
* _FAIL_BAD_REQUEST cmd_no is out of range.
*/
int
{
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
return (REP_PROTOCOL_SUCCESS);
}
/*
* Return a pointer to the property name of the command whose number is
* cmd_no. The property name pointer is returned to *pname.
*
* Returns:
* _FAIL_BAD_REQUEST cmd_no is out of range.
*/
int
{
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
return (REP_PROTOCOL_SUCCESS);
}
/*
* Return the property type of the property whose command number is
* cmd_no. The property type is returned to *ptype.
*
* Returns:
* _FAIL_BAD_REQUEST cmd_no is out of range.
*/
int
{
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
return (REP_PROTOCOL_SUCCESS);
}
/*
* This function is used to retrieve a property value from the transaction
* data. val_no specifies which value is to be retrieved from the
* transaction command whose number is cmd_no. A pointer to the specified
* value is placed in *val.
*
* Returns:
* _FAIL_BAD_REQUEST cmd_no or val_no is out of range.
*/
int
const char **val)
{
const char *bp;
uint32_t i;
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
return (REP_PROTOCOL_FAIL_BAD_REQUEST);
/* Find the correct value */
for (i = 0; i < val_no; i++) {
/* LINTED alignment */
}
/* Bypass the count & return pointer to value. */
return (REP_PROTOCOL_SUCCESS);
}
int
{
int ret;
backend_query_t *q;
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. */
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);
}