10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55777Stw21770 * Common Development and Distribution License (the "License").
65777Stw21770 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
215777Stw21770
220Sstevel@tonic-gate /*
235777Stw21770 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * This file only contains the transaction commit logic.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <assert.h>
340Sstevel@tonic-gate #include <alloca.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <strings.h>
390Sstevel@tonic-gate #include <sys/sysmacros.h>
400Sstevel@tonic-gate #include "configd.h"
410Sstevel@tonic-gate
420Sstevel@tonic-gate #define INVALID_OBJ_ID ((uint32_t)-1)
430Sstevel@tonic-gate #define INVALID_TYPE ((uint32_t)-1)
440Sstevel@tonic-gate
450Sstevel@tonic-gate struct tx_cmd {
460Sstevel@tonic-gate const struct rep_protocol_transaction_cmd *tx_cmd;
470Sstevel@tonic-gate const char *tx_prop;
480Sstevel@tonic-gate uint32_t *tx_values;
490Sstevel@tonic-gate uint32_t tx_nvalues;
500Sstevel@tonic-gate uint32_t tx_orig_value_id;
510Sstevel@tonic-gate char tx_found;
520Sstevel@tonic-gate char tx_processed;
530Sstevel@tonic-gate char tx_bad;
540Sstevel@tonic-gate };
550Sstevel@tonic-gate
560Sstevel@tonic-gate static int
tx_cmd_compare(const void * key,const void * elem_arg)570Sstevel@tonic-gate tx_cmd_compare(const void *key, const void *elem_arg)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate const struct tx_cmd *elem = elem_arg;
600Sstevel@tonic-gate
610Sstevel@tonic-gate return (strcmp((const char *)key, elem->tx_prop));
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate struct tx_commit_data {
650Sstevel@tonic-gate uint32_t txc_pg_id;
660Sstevel@tonic-gate uint32_t txc_gen;
670Sstevel@tonic-gate uint32_t txc_oldgen;
680Sstevel@tonic-gate short txc_backend;
690Sstevel@tonic-gate backend_tx_t *txc_tx;
700Sstevel@tonic-gate backend_query_t *txc_inserts;
710Sstevel@tonic-gate size_t txc_count;
720Sstevel@tonic-gate rep_protocol_responseid_t txc_result;
730Sstevel@tonic-gate struct tx_cmd txc_cmds[1]; /* actually txc_count */
740Sstevel@tonic-gate };
750Sstevel@tonic-gate #define TX_COMMIT_DATA_SIZE(count) \
760Sstevel@tonic-gate offsetof(struct tx_commit_data, txc_cmds[count])
770Sstevel@tonic-gate
780Sstevel@tonic-gate /*ARGSUSED*/
790Sstevel@tonic-gate static int
tx_check_genid(void * data_arg,int columns,char ** vals,char ** names)800Sstevel@tonic-gate tx_check_genid(void *data_arg, int columns, char **vals, char **names)
810Sstevel@tonic-gate {
825777Stw21770 tx_commit_data_t *data = data_arg;
830Sstevel@tonic-gate assert(columns == 1);
840Sstevel@tonic-gate if (atoi(vals[0]) != data->txc_oldgen)
850Sstevel@tonic-gate data->txc_result = REP_PROTOCOL_FAIL_NOT_LATEST;
860Sstevel@tonic-gate else
870Sstevel@tonic-gate data->txc_result = REP_PROTOCOL_SUCCESS;
880Sstevel@tonic-gate return (BACKEND_CALLBACK_CONTINUE);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * tx_process_property() is called once for each property in current
930Sstevel@tonic-gate * property group generation. Its purpose is threefold:
940Sstevel@tonic-gate *
950Sstevel@tonic-gate * 1. copy properties not mentioned in the transaction over unchanged.
960Sstevel@tonic-gate * 2. mark DELETEd properties as seen (they will be left out of the new
970Sstevel@tonic-gate * generation).
980Sstevel@tonic-gate * 3. consistancy-check NEW, CLEAR, and REPLACE commands.
990Sstevel@tonic-gate *
1000Sstevel@tonic-gate * Any consistancy problems set tx_bad, and seen properties are marked
1010Sstevel@tonic-gate * tx_found. These is used later, in tx_process_cmds().
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate /*ARGSUSED*/
1040Sstevel@tonic-gate static int
tx_process_property(void * data_arg,int columns,char ** vals,char ** names)1050Sstevel@tonic-gate tx_process_property(void *data_arg, int columns, char **vals, char **names)
1060Sstevel@tonic-gate {
1075777Stw21770 tx_commit_data_t *data = data_arg;
1080Sstevel@tonic-gate struct tx_cmd *elem;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate const char *prop_name = vals[0];
1110Sstevel@tonic-gate const char *prop_type = vals[1];
1120Sstevel@tonic-gate const char *lnk_val_id = vals[2];
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate char *endptr;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate assert(columns == 3);
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate elem = bsearch(prop_name, data->txc_cmds, data->txc_count,
1190Sstevel@tonic-gate sizeof (*data->txc_cmds), tx_cmd_compare);
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate if (elem == NULL) {
1220Sstevel@tonic-gate backend_query_add(data->txc_inserts,
1230Sstevel@tonic-gate "INSERT INTO prop_lnk_tbl"
1240Sstevel@tonic-gate " (lnk_pg_id, lnk_gen_id, lnk_prop_name, lnk_prop_type,"
1250Sstevel@tonic-gate " lnk_val_id) "
1260Sstevel@tonic-gate "VALUES ( %d, %d, '%q', '%q', %Q );",
1270Sstevel@tonic-gate data->txc_pg_id, data->txc_gen, prop_name, prop_type,
1280Sstevel@tonic-gate lnk_val_id);
1290Sstevel@tonic-gate } else {
1300Sstevel@tonic-gate assert(!elem->tx_found);
1310Sstevel@tonic-gate elem->tx_found = 1;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate if (lnk_val_id != NULL) {
1340Sstevel@tonic-gate errno = 0;
1350Sstevel@tonic-gate elem->tx_orig_value_id =
1360Sstevel@tonic-gate strtoul(lnk_val_id, &endptr, 10);
1370Sstevel@tonic-gate if (elem->tx_orig_value_id == 0 || *endptr != 0 ||
1380Sstevel@tonic-gate errno != 0) {
1390Sstevel@tonic-gate return (BACKEND_CALLBACK_ABORT);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate } else {
1420Sstevel@tonic-gate elem->tx_orig_value_id = 0;
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate switch (elem->tx_cmd->rptc_action) {
1460Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_NEW:
1470Sstevel@tonic-gate elem->tx_bad = 1;
1480Sstevel@tonic-gate data->txc_result = REP_PROTOCOL_FAIL_EXISTS;
1490Sstevel@tonic-gate break;
1500Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_CLEAR:
1510Sstevel@tonic-gate if (REP_PROTOCOL_BASE_TYPE(elem->tx_cmd->rptc_type) !=
1520Sstevel@tonic-gate prop_type[0] &&
1530Sstevel@tonic-gate REP_PROTOCOL_SUBTYPE(elem->tx_cmd->rptc_type) !=
1540Sstevel@tonic-gate prop_type[1]) {
1550Sstevel@tonic-gate elem->tx_bad = 1;
1560Sstevel@tonic-gate data->txc_result =
1570Sstevel@tonic-gate REP_PROTOCOL_FAIL_TYPE_MISMATCH;
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate break;
1600Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_REPLACE:
1610Sstevel@tonic-gate break;
1620Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_DELETE:
1630Sstevel@tonic-gate elem->tx_processed = 1;
1640Sstevel@tonic-gate break;
1650Sstevel@tonic-gate default:
1660Sstevel@tonic-gate assert(0);
1670Sstevel@tonic-gate break;
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate return (BACKEND_CALLBACK_CONTINUE);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate * tx_process_cmds() finishes the job tx_process_property() started:
1750Sstevel@tonic-gate *
1760Sstevel@tonic-gate * 1. if tx_process_property() marked a command as bad, we skip it.
1770Sstevel@tonic-gate * 2. if a DELETE, REPLACE, or CLEAR operated on a non-existant property,
1780Sstevel@tonic-gate * we mark it as bad.
1790Sstevel@tonic-gate * 3. we complete the work of NEW, REPLACE, and CLEAR, by inserting the
1800Sstevel@tonic-gate * appropriate values into the database.
1810Sstevel@tonic-gate * 4. we delete all replaced data, if it is no longer referenced.
1820Sstevel@tonic-gate *
1830Sstevel@tonic-gate * Finally, we check all of the commands, and fail if anything was marked bad.
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate static int
tx_process_cmds(tx_commit_data_t * data)1865777Stw21770 tx_process_cmds(tx_commit_data_t *data)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate int idx;
1890Sstevel@tonic-gate int r;
1900Sstevel@tonic-gate int count = data->txc_count;
1910Sstevel@tonic-gate struct tx_cmd *elem;
1920Sstevel@tonic-gate uint32_t val_id = 0;
1930Sstevel@tonic-gate uint8_t type[3];
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate backend_query_t *q;
1960Sstevel@tonic-gate int do_delete;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate * For persistent pgs, we use backend_fail_if_seen to abort the
2000Sstevel@tonic-gate * deletion if there is a snapshot using our current state.
2010Sstevel@tonic-gate *
2020Sstevel@tonic-gate * All of the deletions in this function are safe, since
2030Sstevel@tonic-gate * rc_tx_commit() guarantees that all the data is in-cache.
2040Sstevel@tonic-gate */
2050Sstevel@tonic-gate q = backend_query_alloc();
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate if (data->txc_backend != BACKEND_TYPE_NONPERSIST) {
2080Sstevel@tonic-gate backend_query_add(q,
2090Sstevel@tonic-gate "SELECT 1 FROM snaplevel_lnk_tbl "
2100Sstevel@tonic-gate " WHERE (snaplvl_pg_id = %d AND snaplvl_gen_id = %d); ",
2110Sstevel@tonic-gate data->txc_pg_id, data->txc_oldgen);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate backend_query_add(q,
2140Sstevel@tonic-gate "DELETE FROM prop_lnk_tbl"
2150Sstevel@tonic-gate " WHERE (lnk_pg_id = %d AND lnk_gen_id = %d)",
2160Sstevel@tonic-gate data->txc_pg_id, data->txc_oldgen);
2170Sstevel@tonic-gate r = backend_tx_run(data->txc_tx, q, backend_fail_if_seen, NULL);
2180Sstevel@tonic-gate backend_query_free(q);
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate if (r == REP_PROTOCOL_SUCCESS)
2210Sstevel@tonic-gate do_delete = 1;
2220Sstevel@tonic-gate else if (r == REP_PROTOCOL_DONE)
2230Sstevel@tonic-gate do_delete = 0; /* old gen_id is in use */
2240Sstevel@tonic-gate else
2250Sstevel@tonic-gate return (r);
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate for (idx = 0; idx < count; idx++) {
2280Sstevel@tonic-gate elem = &data->txc_cmds[idx];
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate if (elem->tx_bad)
2310Sstevel@tonic-gate continue;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate switch (elem->tx_cmd->rptc_action) {
2340Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_DELETE:
2350Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_REPLACE:
2360Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_CLEAR:
2370Sstevel@tonic-gate if (!elem->tx_found) {
2380Sstevel@tonic-gate elem->tx_bad = 1;
2390Sstevel@tonic-gate continue;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate break;
2420Sstevel@tonic-gate case REP_PROTOCOL_TX_ENTRY_NEW:
2430Sstevel@tonic-gate break;
2440Sstevel@tonic-gate default:
2450Sstevel@tonic-gate assert(0);
2460Sstevel@tonic-gate break;
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate if (do_delete &&
2500Sstevel@tonic-gate elem->tx_cmd->rptc_action != REP_PROTOCOL_TX_ENTRY_NEW &&
2510Sstevel@tonic-gate elem->tx_orig_value_id != 0) {
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate * delete the old values, if they are not in use
2540Sstevel@tonic-gate */
2550Sstevel@tonic-gate q = backend_query_alloc();
2560Sstevel@tonic-gate backend_query_add(q,
2570Sstevel@tonic-gate "SELECT 1 FROM prop_lnk_tbl "
2580Sstevel@tonic-gate " WHERE (lnk_val_id = %d); "
2590Sstevel@tonic-gate "DELETE FROM value_tbl"
2600Sstevel@tonic-gate " WHERE (value_id = %d)",
2610Sstevel@tonic-gate elem->tx_orig_value_id, elem->tx_orig_value_id);
2620Sstevel@tonic-gate r = backend_tx_run(data->txc_tx, q,
2630Sstevel@tonic-gate backend_fail_if_seen, NULL);
2640Sstevel@tonic-gate backend_query_free(q);
2650Sstevel@tonic-gate if (r != REP_PROTOCOL_SUCCESS && r != REP_PROTOCOL_DONE)
2660Sstevel@tonic-gate return (r);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate if (elem->tx_cmd->rptc_action == REP_PROTOCOL_TX_ENTRY_DELETE)
2700Sstevel@tonic-gate continue; /* no further work to do */
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate type[0] = REP_PROTOCOL_BASE_TYPE(elem->tx_cmd->rptc_type);
2730Sstevel@tonic-gate type[1] = REP_PROTOCOL_SUBTYPE(elem->tx_cmd->rptc_type);
2740Sstevel@tonic-gate type[2] = 0;
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate if (elem->tx_nvalues == 0) {
2770Sstevel@tonic-gate r = backend_tx_run_update(data->txc_tx,
2780Sstevel@tonic-gate "INSERT INTO prop_lnk_tbl"
2790Sstevel@tonic-gate " (lnk_pg_id, lnk_gen_id, "
2800Sstevel@tonic-gate " lnk_prop_name, lnk_prop_type, lnk_val_id) "
2810Sstevel@tonic-gate "VALUES ( %d, %d, '%q', '%q', NULL );",
2820Sstevel@tonic-gate data->txc_pg_id, data->txc_gen, elem->tx_prop,
2830Sstevel@tonic-gate type);
2840Sstevel@tonic-gate } else {
285*7128Samaguire uint32_t *v, i = 0;
2860Sstevel@tonic-gate const char *str;
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate val_id = backend_new_id(data->txc_tx, BACKEND_ID_VALUE);
2890Sstevel@tonic-gate if (val_id == 0)
2900Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_NO_RESOURCES);
2910Sstevel@tonic-gate r = backend_tx_run_update(data->txc_tx,
2920Sstevel@tonic-gate "INSERT INTO prop_lnk_tbl "
2930Sstevel@tonic-gate " (lnk_pg_id, lnk_gen_id, "
2940Sstevel@tonic-gate " lnk_prop_name, lnk_prop_type, lnk_val_id) "
2950Sstevel@tonic-gate "VALUES ( %d, %d, '%q', '%q', %d );",
2960Sstevel@tonic-gate data->txc_pg_id, data->txc_gen, elem->tx_prop,
2970Sstevel@tonic-gate type, val_id);
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate v = elem->tx_values;
3000Sstevel@tonic-gate
301*7128Samaguire for (i = 0; i < elem->tx_nvalues; i++) {
3020Sstevel@tonic-gate str = (const char *)&v[1];
3030Sstevel@tonic-gate
304*7128Samaguire /*
305*7128Samaguire * Update values in backend, imposing
306*7128Samaguire * ordering via the value_order column.
307*7128Samaguire * This ordering is then used in subseqent
308*7128Samaguire * value retrieval operations. We can
309*7128Samaguire * safely assume that the repository schema
310*7128Samaguire * has been upgraded (and hence has the
311*7128Samaguire * value_order column in value_tbl), since
312*7128Samaguire * it is upgraded as soon as the repository
313*7128Samaguire * is writable.
314*7128Samaguire */
3150Sstevel@tonic-gate r = backend_tx_run_update(data->txc_tx,
316*7128Samaguire "INSERT INTO value_tbl (value_id, "
317*7128Samaguire "value_type, value_value, "
318*7128Samaguire "value_order) VALUES (%d, '%c', "
319*7128Samaguire "'%q', '%d');\n",
320*7128Samaguire val_id, elem->tx_cmd->rptc_type,
321*7128Samaguire str, i);
322*7128Samaguire if (r != REP_PROTOCOL_SUCCESS)
323*7128Samaguire break;
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate /*LINTED alignment*/
3260Sstevel@tonic-gate v = (uint32_t *)((caddr_t)str + TX_SIZE(*v));
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate if (r != REP_PROTOCOL_SUCCESS)
3300Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_UNKNOWN);
3310Sstevel@tonic-gate elem->tx_processed = 1;
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate for (idx = 0; idx < count; idx++) {
3350Sstevel@tonic-gate elem = &data->txc_cmds[idx];
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate if (elem->tx_bad)
3380Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_TX);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate static boolean_t
check_string(uintptr_t loc,uint32_t len,uint32_t sz)3440Sstevel@tonic-gate check_string(uintptr_t loc, uint32_t len, uint32_t sz)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate const char *ptr = (const char *)loc;
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate if (len == 0 || len > sz || ptr[len - 1] != 0 || strlen(ptr) != len - 1)
3490Sstevel@tonic-gate return (0);
3500Sstevel@tonic-gate return (1);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate static int
tx_check_and_setup(tx_commit_data_t * data,const void * cmds_arg,uint32_t count)3545777Stw21770 tx_check_and_setup(tx_commit_data_t *data, const void *cmds_arg,
3550Sstevel@tonic-gate uint32_t count)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate const struct rep_protocol_transaction_cmd *cmds;
3580Sstevel@tonic-gate struct tx_cmd *cur;
3590Sstevel@tonic-gate struct tx_cmd *prev = NULL;
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate uintptr_t loc;
3620Sstevel@tonic-gate uint32_t sz, len;
3630Sstevel@tonic-gate int idx;
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate loc = (uintptr_t)cmds_arg;
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate for (idx = 0; idx < count; idx++) {
3680Sstevel@tonic-gate cur = &data->txc_cmds[idx];
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate cmds = (struct rep_protocol_transaction_cmd *)loc;
3710Sstevel@tonic-gate cur->tx_cmd = cmds;
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate sz = cmds->rptc_size;
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate loc += REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE;
3760Sstevel@tonic-gate sz -= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE;
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate len = cmds->rptc_name_len;
3790Sstevel@tonic-gate if (len <= 1 || !check_string(loc, len, sz)) {
3800Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate cur->tx_prop = (const char *)loc;
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate len = TX_SIZE(len);
3850Sstevel@tonic-gate loc += len;
3860Sstevel@tonic-gate sz -= len;
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate cur->tx_nvalues = 0;
3890Sstevel@tonic-gate cur->tx_values = (uint32_t *)loc;
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate while (sz > 0) {
3920Sstevel@tonic-gate if (sz < sizeof (uint32_t))
3930Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST);
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate cur->tx_nvalues++;
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate len = *(uint32_t *)loc;
3980Sstevel@tonic-gate loc += sizeof (uint32_t);
3990Sstevel@tonic-gate sz -= sizeof (uint32_t);
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate if (!check_string(loc, len, sz))
4020Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST);
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate * XXX here, we should be checking that the values
4060Sstevel@tonic-gate * match the purported type
4070Sstevel@tonic-gate */
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate len = TX_SIZE(len);
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate if (len > sz)
4120Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST);
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate loc += len;
4150Sstevel@tonic-gate sz -= len;
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate if (prev != NULL && strcmp(prev->tx_prop, cur->tx_prop) >= 0)
4190Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST);
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate prev = cur;
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate return (REP_PROTOCOL_SUCCESS);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate
4265777Stw21770 /*
4275777Stw21770 * Free the memory associated with a tx_commit_data structure.
4285777Stw21770 */
4295777Stw21770 void
tx_commit_data_free(tx_commit_data_t * tx_data)4305777Stw21770 tx_commit_data_free(tx_commit_data_t *tx_data)
4315777Stw21770 {
4325777Stw21770 uu_free(tx_data);
4335777Stw21770 }
4345777Stw21770
4355777Stw21770 /*
4365777Stw21770 * Parse the data of a REP_PROTOCOL_PROPERTYGRP_TX_COMMIT message into a
4375777Stw21770 * more useful form. The data in the message will be represented by a
4385777Stw21770 * tx_commit_data_t structure which is allocated by this function. The
4395777Stw21770 * address of the allocated structure is returned to *tx_data and must be
4405777Stw21770 * freed by calling tx_commit_data_free().
4415777Stw21770 *
4425777Stw21770 * Parameters:
4435777Stw21770 * cmds_arg Address of the commands in the
4445777Stw21770 * REP_PROTOCOL_PROPERTYGRP_TX_COMMIT message.
4455777Stw21770 *
4465777Stw21770 * cmds_sz Number of message bytes at cmds_arg.
4475777Stw21770 *
4485777Stw21770 * tx_data Points to the place to receive the address of the
4495777Stw21770 * allocated memory.
4505777Stw21770 *
4515777Stw21770 * Fails with
4525777Stw21770 * _BAD_REQUEST
4535777Stw21770 * _NO_RESOURCES
4545777Stw21770 */
4550Sstevel@tonic-gate int
tx_commit_data_new(const void * cmds_arg,size_t cmds_sz,tx_commit_data_t ** tx_data)4565777Stw21770 tx_commit_data_new(const void *cmds_arg, size_t cmds_sz,
4575777Stw21770 tx_commit_data_t **tx_data)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate const struct rep_protocol_transaction_cmd *cmds;
4605777Stw21770 tx_commit_data_t *data;
4610Sstevel@tonic-gate uintptr_t loc;
4625777Stw21770 uint32_t count;
4635777Stw21770 uint32_t sz;
4640Sstevel@tonic-gate int ret;
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate * First, verify that the reported sizes make sense, and count
4680Sstevel@tonic-gate * the number of commands.
4690Sstevel@tonic-gate */
4700Sstevel@tonic-gate count = 0;
4710Sstevel@tonic-gate loc = (uintptr_t)cmds_arg;
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate while (cmds_sz > 0) {
4740Sstevel@tonic-gate cmds = (struct rep_protocol_transaction_cmd *)loc;
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate if (cmds_sz <= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE)
4770Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST);
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate sz = cmds->rptc_size;
4800Sstevel@tonic-gate if (sz <= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE)
4810Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST);
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate sz = TX_SIZE(sz);
4840Sstevel@tonic-gate if (sz > cmds_sz)
4850Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_BAD_REQUEST);
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate loc += sz;
4880Sstevel@tonic-gate cmds_sz -= sz;
4890Sstevel@tonic-gate count++;
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate
4925777Stw21770 data = uu_zalloc(TX_COMMIT_DATA_SIZE(count));
4935777Stw21770 if (data == NULL)
4945777Stw21770 return (REP_PROTOCOL_FAIL_NO_RESOURCES);
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate /*
4970Sstevel@tonic-gate * verify that everything looks okay, and set up our command
4980Sstevel@tonic-gate * datastructures.
4990Sstevel@tonic-gate */
5005777Stw21770 data->txc_count = count;
5010Sstevel@tonic-gate ret = tx_check_and_setup(data, cmds_arg, count);
5025777Stw21770 if (ret == REP_PROTOCOL_SUCCESS) {
5035777Stw21770 *tx_data = data;
5045777Stw21770 } else {
5055777Stw21770 *tx_data = NULL;
5065777Stw21770 uu_free(data);
5075777Stw21770 }
5085777Stw21770 return (ret);
5095777Stw21770 }
5105777Stw21770
5115777Stw21770 /*
5125777Stw21770 * The following are a set of accessor functions to retrieve data from a
5135777Stw21770 * tx_commit_data_t that has been allocated by tx_commit_data_new().
5145777Stw21770 */
5155777Stw21770
5165777Stw21770 /*
5175777Stw21770 * Return the action of the transaction command whose command number is
5185777Stw21770 * cmd_no. The action is placed at *action.
5195777Stw21770 *
5205777Stw21770 * Returns:
5215777Stw21770 * _FAIL_BAD_REQUEST cmd_no is out of range.
5225777Stw21770 */
5235777Stw21770 int
tx_cmd_action(tx_commit_data_t * tx_data,size_t cmd_no,enum rep_protocol_transaction_action * action)5245777Stw21770 tx_cmd_action(tx_commit_data_t *tx_data, size_t cmd_no,
5255777Stw21770 enum rep_protocol_transaction_action *action)
5265777Stw21770 {
5275777Stw21770 struct tx_cmd *cur;
5285777Stw21770
5295777Stw21770 assert(cmd_no < tx_data->txc_count);
5305777Stw21770 if (cmd_no >= tx_data->txc_count)
5315777Stw21770 return (REP_PROTOCOL_FAIL_BAD_REQUEST);
5325777Stw21770
5335777Stw21770 cur = &tx_data->txc_cmds[cmd_no];
5345777Stw21770 *action = cur->tx_cmd->rptc_action;
5355777Stw21770 return (REP_PROTOCOL_SUCCESS);
5365777Stw21770 }
5375777Stw21770
5385777Stw21770 /*
5395777Stw21770 * Return the number of transaction commands held in tx_data.
5405777Stw21770 */
5415777Stw21770 size_t
tx_cmd_count(tx_commit_data_t * tx_data)5425777Stw21770 tx_cmd_count(tx_commit_data_t *tx_data)
5435777Stw21770 {
5445777Stw21770 return (tx_data->txc_count);
5455777Stw21770 }
5465777Stw21770
5475777Stw21770 /*
5485777Stw21770 * Return the number of property values that are associated with the
5495777Stw21770 * transaction command whose number is cmd_no. The number of values is
5505777Stw21770 * returned to *nvalues.
5515777Stw21770 *
5525777Stw21770 * Returns:
5535777Stw21770 * _FAIL_BAD_REQUEST cmd_no is out of range.
5545777Stw21770 */
5555777Stw21770 int
tx_cmd_nvalues(tx_commit_data_t * tx_data,size_t cmd_no,uint32_t * nvalues)5565777Stw21770 tx_cmd_nvalues(tx_commit_data_t *tx_data, size_t cmd_no, uint32_t *nvalues)
5575777Stw21770 {
5585777Stw21770 struct tx_cmd *cur;
5595777Stw21770
5605777Stw21770 assert(cmd_no < tx_data->txc_count);
5615777Stw21770 if (cmd_no >= tx_data->txc_count)
5625777Stw21770 return (REP_PROTOCOL_FAIL_BAD_REQUEST);
5635777Stw21770
5645777Stw21770 cur = &tx_data->txc_cmds[cmd_no];
5655777Stw21770 *nvalues = cur->tx_nvalues;
5665777Stw21770 return (REP_PROTOCOL_SUCCESS);
5675777Stw21770 }
5685777Stw21770
5695777Stw21770 /*
5705777Stw21770 * Return a pointer to the property name of the command whose number is
5715777Stw21770 * cmd_no. The property name pointer is returned to *pname.
5725777Stw21770 *
5735777Stw21770 * Returns:
5745777Stw21770 * _FAIL_BAD_REQUEST cmd_no is out of range.
5755777Stw21770 */
5765777Stw21770 int
tx_cmd_prop(tx_commit_data_t * tx_data,size_t cmd_no,const char ** pname)5775777Stw21770 tx_cmd_prop(tx_commit_data_t *tx_data, size_t cmd_no, const char **pname)
5785777Stw21770 {
5795777Stw21770 struct tx_cmd *cur;
5805777Stw21770
5815777Stw21770 assert(cmd_no < tx_data->txc_count);
5825777Stw21770 if (cmd_no >= tx_data->txc_count)
5835777Stw21770 return (REP_PROTOCOL_FAIL_BAD_REQUEST);
5845777Stw21770
5855777Stw21770 cur = &tx_data->txc_cmds[cmd_no];
5865777Stw21770 *pname = cur->tx_prop;
5875777Stw21770 return (REP_PROTOCOL_SUCCESS);
5885777Stw21770 }
5895777Stw21770
5905777Stw21770 /*
5915777Stw21770 * Return the property type of the property whose command number is
5925777Stw21770 * cmd_no. The property type is returned to *ptype.
5935777Stw21770 *
5945777Stw21770 * Returns:
5955777Stw21770 * _FAIL_BAD_REQUEST cmd_no is out of range.
5965777Stw21770 */
5975777Stw21770 int
tx_cmd_prop_type(tx_commit_data_t * tx_data,size_t cmd_no,uint32_t * ptype)5985777Stw21770 tx_cmd_prop_type(tx_commit_data_t *tx_data, size_t cmd_no, uint32_t *ptype)
5995777Stw21770 {
6005777Stw21770 struct tx_cmd *cur;
6015777Stw21770
6025777Stw21770 assert(cmd_no < tx_data->txc_count);
6035777Stw21770 if (cmd_no >= tx_data->txc_count)
6045777Stw21770 return (REP_PROTOCOL_FAIL_BAD_REQUEST);
6055777Stw21770
6065777Stw21770 cur = &tx_data->txc_cmds[cmd_no];
6075777Stw21770 *ptype = cur->tx_cmd->rptc_type;
6085777Stw21770 return (REP_PROTOCOL_SUCCESS);
6095777Stw21770 }
6105777Stw21770
6115777Stw21770 /*
6125777Stw21770 * This function is used to retrieve a property value from the transaction
6135777Stw21770 * data. val_no specifies which value is to be retrieved from the
6145777Stw21770 * transaction command whose number is cmd_no. A pointer to the specified
6155777Stw21770 * value is placed in *val.
6165777Stw21770 *
6175777Stw21770 * Returns:
6185777Stw21770 * _FAIL_BAD_REQUEST cmd_no or val_no is out of range.
6195777Stw21770 */
6205777Stw21770 int
tx_cmd_value(tx_commit_data_t * tx_data,size_t cmd_no,uint32_t val_no,const char ** val)6215777Stw21770 tx_cmd_value(tx_commit_data_t *tx_data, size_t cmd_no, uint32_t val_no,
6225777Stw21770 const char **val)
6235777Stw21770 {
6245777Stw21770 const char *bp;
6255777Stw21770 struct tx_cmd *cur;
6265777Stw21770 uint32_t i;
6275777Stw21770 uint32_t value_len;
6285777Stw21770
6295777Stw21770 assert(cmd_no < tx_data->txc_count);
6305777Stw21770 if (cmd_no >= tx_data->txc_count)
6315777Stw21770 return (REP_PROTOCOL_FAIL_BAD_REQUEST);
6325777Stw21770
6335777Stw21770 cur = &tx_data->txc_cmds[cmd_no];
6345777Stw21770 assert(val_no < cur->tx_nvalues);
6355777Stw21770 if (val_no >= cur->tx_nvalues)
6365777Stw21770 return (REP_PROTOCOL_FAIL_BAD_REQUEST);
6375777Stw21770
6385777Stw21770 /* Find the correct value */
6395777Stw21770 bp = (char *)cur->tx_values;
6405777Stw21770 for (i = 0; i < val_no; i++) {
6415777Stw21770 /* LINTED alignment */
6425777Stw21770 value_len = *(uint32_t *)bp;
6435777Stw21770 bp += sizeof (uint32_t) + TX_SIZE(value_len);
6445777Stw21770 }
6455777Stw21770
6465777Stw21770 /* Bypass the count & return pointer to value. */
6475777Stw21770 bp += sizeof (uint32_t);
6485777Stw21770 *val = bp;
6495777Stw21770 return (REP_PROTOCOL_SUCCESS);
6505777Stw21770 }
6515777Stw21770
6525777Stw21770 int
object_tx_commit(rc_node_lookup_t * lp,tx_commit_data_t * data,uint32_t * gen)6535777Stw21770 object_tx_commit(rc_node_lookup_t *lp, tx_commit_data_t *data, uint32_t *gen)
6545777Stw21770 {
6555777Stw21770 uint32_t new_gen;
6565777Stw21770 int ret;
6575777Stw21770 rep_protocol_responseid_t r;
6585777Stw21770 backend_tx_t *tx;
6595777Stw21770 backend_query_t *q;
6605777Stw21770 int backend = lp->rl_backend;
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate ret = backend_tx_begin(backend, &tx);
6630Sstevel@tonic-gate if (ret != REP_PROTOCOL_SUCCESS)
6640Sstevel@tonic-gate return (ret);
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate /* Make sure the pg is up-to-date. */
6670Sstevel@tonic-gate data->txc_oldgen = *gen;
6680Sstevel@tonic-gate data->txc_backend = backend;
6690Sstevel@tonic-gate data->txc_result = REP_PROTOCOL_FAIL_NOT_FOUND;
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate q = backend_query_alloc();
6720Sstevel@tonic-gate backend_query_add(q, "SELECT pg_gen_id FROM pg_tbl WHERE (pg_id = %d);",
6730Sstevel@tonic-gate lp->rl_main_id);
6740Sstevel@tonic-gate r = backend_tx_run(tx, q, tx_check_genid, data);
6750Sstevel@tonic-gate backend_query_free(q);
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate if (r != REP_PROTOCOL_SUCCESS ||
6780Sstevel@tonic-gate (r = data->txc_result) != REP_PROTOCOL_SUCCESS) {
6790Sstevel@tonic-gate backend_tx_rollback(tx);
6800Sstevel@tonic-gate goto end;
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate
6830Sstevel@tonic-gate /* If the transaction is empty, cut out early. */
6845777Stw21770 if (data->txc_count == 0) {
6850Sstevel@tonic-gate backend_tx_rollback(tx);
6860Sstevel@tonic-gate r = REP_PROTOCOL_DONE;
6870Sstevel@tonic-gate goto end;
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate new_gen = backend_new_id(tx, BACKEND_ID_GENERATION);
6910Sstevel@tonic-gate if (new_gen == 0) {
6920Sstevel@tonic-gate backend_tx_rollback(tx);
6930Sstevel@tonic-gate return (REP_PROTOCOL_FAIL_NO_RESOURCES);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate data->txc_pg_id = lp->rl_main_id;
6970Sstevel@tonic-gate data->txc_gen = new_gen;
6980Sstevel@tonic-gate data->txc_tx = tx;
6990Sstevel@tonic-gate
7000Sstevel@tonic-gate r = backend_tx_run_update(tx,
7010Sstevel@tonic-gate "UPDATE pg_tbl SET pg_gen_id = %d "
7020Sstevel@tonic-gate " WHERE (pg_id = %d AND pg_gen_id = %d);",
7030Sstevel@tonic-gate new_gen, lp->rl_main_id, *gen);
7040Sstevel@tonic-gate
7050Sstevel@tonic-gate if (r != REP_PROTOCOL_SUCCESS) {
7060Sstevel@tonic-gate backend_tx_rollback(tx);
7070Sstevel@tonic-gate goto end;
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate q = backend_query_alloc();
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate backend_query_add(q,
7130Sstevel@tonic-gate "SELECT lnk_prop_name, lnk_prop_type, lnk_val_id "
7140Sstevel@tonic-gate "FROM prop_lnk_tbl "
7150Sstevel@tonic-gate "WHERE (lnk_pg_id = %d AND lnk_gen_id = %d)",
7160Sstevel@tonic-gate lp->rl_main_id, *gen);
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate data->txc_inserts = backend_query_alloc();
7190Sstevel@tonic-gate r = backend_tx_run(tx, q, tx_process_property, data);
7200Sstevel@tonic-gate backend_query_free(q);
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate if (r == REP_PROTOCOL_DONE)
7230Sstevel@tonic-gate r = REP_PROTOCOL_FAIL_UNKNOWN; /* corruption */
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate if (r != REP_PROTOCOL_SUCCESS ||
7260Sstevel@tonic-gate (r = data->txc_result) != REP_PROTOCOL_SUCCESS) {
7270Sstevel@tonic-gate backend_query_free(data->txc_inserts);
7280Sstevel@tonic-gate backend_tx_rollback(tx);
7290Sstevel@tonic-gate goto end;
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate r = backend_tx_run(tx, data->txc_inserts, NULL, NULL);
7330Sstevel@tonic-gate backend_query_free(data->txc_inserts);
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate if (r != REP_PROTOCOL_SUCCESS) {
7360Sstevel@tonic-gate backend_tx_rollback(tx);
7370Sstevel@tonic-gate goto end;
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate
7400Sstevel@tonic-gate r = tx_process_cmds(data);
7410Sstevel@tonic-gate if (r != REP_PROTOCOL_SUCCESS) {
7420Sstevel@tonic-gate backend_tx_rollback(tx);
7430Sstevel@tonic-gate goto end;
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate r = backend_tx_commit(tx);
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate if (r == REP_PROTOCOL_SUCCESS)
7480Sstevel@tonic-gate *gen = new_gen;
7490Sstevel@tonic-gate end:
7500Sstevel@tonic-gate return (r);
7510Sstevel@tonic-gate }
752