xref: /titanic_54/usr/src/cmd/svc/configd/object.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * This file only contains the transaction commit logic.
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <assert.h>
34*7c478bd9Sstevel@tonic-gate #include <alloca.h>
35*7c478bd9Sstevel@tonic-gate #include <errno.h>
36*7c478bd9Sstevel@tonic-gate #include <stdio.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <strings.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
40*7c478bd9Sstevel@tonic-gate #include "configd.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #define	INVALID_OBJ_ID ((uint32_t)-1)
43*7c478bd9Sstevel@tonic-gate #define	INVALID_TYPE ((uint32_t)-1)
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate struct tx_cmd {
46*7c478bd9Sstevel@tonic-gate 	const struct rep_protocol_transaction_cmd *tx_cmd;
47*7c478bd9Sstevel@tonic-gate 	const char	*tx_prop;
48*7c478bd9Sstevel@tonic-gate 	uint32_t	*tx_values;
49*7c478bd9Sstevel@tonic-gate 	uint32_t	tx_nvalues;
50*7c478bd9Sstevel@tonic-gate 	uint32_t	tx_orig_value_id;
51*7c478bd9Sstevel@tonic-gate 	char		tx_found;
52*7c478bd9Sstevel@tonic-gate 	char		tx_processed;
53*7c478bd9Sstevel@tonic-gate 	char		tx_bad;
54*7c478bd9Sstevel@tonic-gate };
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static int
57*7c478bd9Sstevel@tonic-gate tx_cmd_compare(const void *key, const void *elem_arg)
58*7c478bd9Sstevel@tonic-gate {
59*7c478bd9Sstevel@tonic-gate 	const struct tx_cmd *elem = elem_arg;
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	return (strcmp((const char *)key, elem->tx_prop));
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate struct tx_commit_data {
65*7c478bd9Sstevel@tonic-gate 	uint32_t	txc_pg_id;
66*7c478bd9Sstevel@tonic-gate 	uint32_t	txc_gen;
67*7c478bd9Sstevel@tonic-gate 	uint32_t	txc_oldgen;
68*7c478bd9Sstevel@tonic-gate 	short		txc_backend;
69*7c478bd9Sstevel@tonic-gate 	backend_tx_t	*txc_tx;
70*7c478bd9Sstevel@tonic-gate 	backend_query_t	*txc_inserts;
71*7c478bd9Sstevel@tonic-gate 	size_t		txc_count;
72*7c478bd9Sstevel@tonic-gate 	rep_protocol_responseid_t txc_result;
73*7c478bd9Sstevel@tonic-gate 	struct tx_cmd	txc_cmds[1];		/* actually txc_count */
74*7c478bd9Sstevel@tonic-gate };
75*7c478bd9Sstevel@tonic-gate #define	TX_COMMIT_DATA_SIZE(count) \
76*7c478bd9Sstevel@tonic-gate 	offsetof(struct tx_commit_data, txc_cmds[count])
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
79*7c478bd9Sstevel@tonic-gate static int
80*7c478bd9Sstevel@tonic-gate tx_check_genid(void *data_arg, int columns, char **vals, char **names)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate 	struct tx_commit_data *data = data_arg;
83*7c478bd9Sstevel@tonic-gate 	assert(columns == 1);
84*7c478bd9Sstevel@tonic-gate 	if (atoi(vals[0]) != data->txc_oldgen)
85*7c478bd9Sstevel@tonic-gate 		data->txc_result = REP_PROTOCOL_FAIL_NOT_LATEST;
86*7c478bd9Sstevel@tonic-gate 	else
87*7c478bd9Sstevel@tonic-gate 		data->txc_result = REP_PROTOCOL_SUCCESS;
88*7c478bd9Sstevel@tonic-gate 	return (BACKEND_CALLBACK_CONTINUE);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate /*
92*7c478bd9Sstevel@tonic-gate  * tx_process_property() is called once for each property in current
93*7c478bd9Sstevel@tonic-gate  * property group generation.  Its purpose is threefold:
94*7c478bd9Sstevel@tonic-gate  *
95*7c478bd9Sstevel@tonic-gate  *	1. copy properties not mentioned in the transaction over unchanged.
96*7c478bd9Sstevel@tonic-gate  *	2. mark DELETEd properties as seen (they will be left out of the new
97*7c478bd9Sstevel@tonic-gate  *	   generation).
98*7c478bd9Sstevel@tonic-gate  *	3. consistancy-check NEW, CLEAR, and REPLACE commands.
99*7c478bd9Sstevel@tonic-gate  *
100*7c478bd9Sstevel@tonic-gate  * Any consistancy problems set tx_bad, and seen properties are marked
101*7c478bd9Sstevel@tonic-gate  * tx_found.  These is used later, in tx_process_cmds().
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
104*7c478bd9Sstevel@tonic-gate static int
105*7c478bd9Sstevel@tonic-gate tx_process_property(void *data_arg, int columns, char **vals, char **names)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	struct tx_commit_data *data = data_arg;
108*7c478bd9Sstevel@tonic-gate 	struct tx_cmd *elem;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	const char *prop_name = vals[0];
111*7c478bd9Sstevel@tonic-gate 	const char *prop_type = vals[1];
112*7c478bd9Sstevel@tonic-gate 	const char *lnk_val_id = vals[2];
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	char *endptr;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	assert(columns == 3);
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	elem = bsearch(prop_name, data->txc_cmds, data->txc_count,
119*7c478bd9Sstevel@tonic-gate 	    sizeof (*data->txc_cmds), tx_cmd_compare);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if (elem == NULL) {
122*7c478bd9Sstevel@tonic-gate 		backend_query_add(data->txc_inserts,
123*7c478bd9Sstevel@tonic-gate 		    "INSERT INTO prop_lnk_tbl"
124*7c478bd9Sstevel@tonic-gate 		    "    (lnk_pg_id, lnk_gen_id, lnk_prop_name, lnk_prop_type,"
125*7c478bd9Sstevel@tonic-gate 		    "    lnk_val_id) "
126*7c478bd9Sstevel@tonic-gate 		    "VALUES ( %d, %d, '%q', '%q', %Q );",
127*7c478bd9Sstevel@tonic-gate 		    data->txc_pg_id, data->txc_gen, prop_name, prop_type,
128*7c478bd9Sstevel@tonic-gate 		    lnk_val_id);
129*7c478bd9Sstevel@tonic-gate 	} else {
130*7c478bd9Sstevel@tonic-gate 		assert(!elem->tx_found);
131*7c478bd9Sstevel@tonic-gate 		elem->tx_found = 1;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 		if (lnk_val_id != NULL) {
134*7c478bd9Sstevel@tonic-gate 			errno = 0;
135*7c478bd9Sstevel@tonic-gate 			elem->tx_orig_value_id =
136*7c478bd9Sstevel@tonic-gate 			    strtoul(lnk_val_id, &endptr, 10);
137*7c478bd9Sstevel@tonic-gate 			if (elem->tx_orig_value_id == 0 || *endptr != 0 ||
138*7c478bd9Sstevel@tonic-gate 			    errno != 0) {
139*7c478bd9Sstevel@tonic-gate 				return (BACKEND_CALLBACK_ABORT);
140*7c478bd9Sstevel@tonic-gate 			}
141*7c478bd9Sstevel@tonic-gate 		} else {
142*7c478bd9Sstevel@tonic-gate 			elem->tx_orig_value_id = 0;
143*7c478bd9Sstevel@tonic-gate 		}
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 		switch (elem->tx_cmd->rptc_action) {
146*7c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_TX_ENTRY_NEW:
147*7c478bd9Sstevel@tonic-gate 			elem->tx_bad = 1;
148*7c478bd9Sstevel@tonic-gate 			data->txc_result = REP_PROTOCOL_FAIL_EXISTS;
149*7c478bd9Sstevel@tonic-gate 			break;
150*7c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_TX_ENTRY_CLEAR:
151*7c478bd9Sstevel@tonic-gate 			if (REP_PROTOCOL_BASE_TYPE(elem->tx_cmd->rptc_type) !=
152*7c478bd9Sstevel@tonic-gate 			    prop_type[0] &&
153*7c478bd9Sstevel@tonic-gate 			    REP_PROTOCOL_SUBTYPE(elem->tx_cmd->rptc_type) !=
154*7c478bd9Sstevel@tonic-gate 			    prop_type[1]) {
155*7c478bd9Sstevel@tonic-gate 				elem->tx_bad = 1;
156*7c478bd9Sstevel@tonic-gate 				data->txc_result =
157*7c478bd9Sstevel@tonic-gate 				    REP_PROTOCOL_FAIL_TYPE_MISMATCH;
158*7c478bd9Sstevel@tonic-gate 			}
159*7c478bd9Sstevel@tonic-gate 			break;
160*7c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_TX_ENTRY_REPLACE:
161*7c478bd9Sstevel@tonic-gate 			break;
162*7c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_TX_ENTRY_DELETE:
163*7c478bd9Sstevel@tonic-gate 			elem->tx_processed = 1;
164*7c478bd9Sstevel@tonic-gate 			break;
165*7c478bd9Sstevel@tonic-gate 		default:
166*7c478bd9Sstevel@tonic-gate 			assert(0);
167*7c478bd9Sstevel@tonic-gate 			break;
168*7c478bd9Sstevel@tonic-gate 		}
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate 	return (BACKEND_CALLBACK_CONTINUE);
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * tx_process_cmds() finishes the job tx_process_property() started:
175*7c478bd9Sstevel@tonic-gate  *
176*7c478bd9Sstevel@tonic-gate  *	1. if tx_process_property() marked a command as bad, we skip it.
177*7c478bd9Sstevel@tonic-gate  *	2. if a DELETE, REPLACE, or CLEAR operated on a non-existant property,
178*7c478bd9Sstevel@tonic-gate  *	    we mark it as bad.
179*7c478bd9Sstevel@tonic-gate  *	3. we complete the work of NEW, REPLACE, and CLEAR, by inserting the
180*7c478bd9Sstevel@tonic-gate  *	    appropriate values into the database.
181*7c478bd9Sstevel@tonic-gate  *	4. we delete all replaced data, if it is no longer referenced.
182*7c478bd9Sstevel@tonic-gate  *
183*7c478bd9Sstevel@tonic-gate  * Finally, we check all of the commands, and fail if anything was marked bad.
184*7c478bd9Sstevel@tonic-gate  */
185*7c478bd9Sstevel@tonic-gate static int
186*7c478bd9Sstevel@tonic-gate tx_process_cmds(struct tx_commit_data *data)
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	int idx;
189*7c478bd9Sstevel@tonic-gate 	int r;
190*7c478bd9Sstevel@tonic-gate 	int count = data->txc_count;
191*7c478bd9Sstevel@tonic-gate 	struct tx_cmd *elem;
192*7c478bd9Sstevel@tonic-gate 	uint32_t val_id = 0;
193*7c478bd9Sstevel@tonic-gate 	uint8_t type[3];
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	backend_query_t *q;
196*7c478bd9Sstevel@tonic-gate 	int do_delete;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	/*
199*7c478bd9Sstevel@tonic-gate 	 * For persistent pgs, we use backend_fail_if_seen to abort the
200*7c478bd9Sstevel@tonic-gate 	 * deletion if there is a snapshot using our current state.
201*7c478bd9Sstevel@tonic-gate 	 *
202*7c478bd9Sstevel@tonic-gate 	 * All of the deletions in this function are safe, since
203*7c478bd9Sstevel@tonic-gate 	 * rc_tx_commit() guarantees that all the data is in-cache.
204*7c478bd9Sstevel@tonic-gate 	 */
205*7c478bd9Sstevel@tonic-gate 	q = backend_query_alloc();
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	if (data->txc_backend != BACKEND_TYPE_NONPERSIST) {
208*7c478bd9Sstevel@tonic-gate 		backend_query_add(q,
209*7c478bd9Sstevel@tonic-gate 		    "SELECT 1 FROM snaplevel_lnk_tbl "
210*7c478bd9Sstevel@tonic-gate 		    "    WHERE (snaplvl_pg_id = %d AND snaplvl_gen_id = %d); ",
211*7c478bd9Sstevel@tonic-gate 		    data->txc_pg_id, data->txc_oldgen);
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 	backend_query_add(q,
214*7c478bd9Sstevel@tonic-gate 	    "DELETE FROM prop_lnk_tbl"
215*7c478bd9Sstevel@tonic-gate 	    "    WHERE (lnk_pg_id = %d AND lnk_gen_id = %d)",
216*7c478bd9Sstevel@tonic-gate 	    data->txc_pg_id, data->txc_oldgen);
217*7c478bd9Sstevel@tonic-gate 	r = backend_tx_run(data->txc_tx, q, backend_fail_if_seen, NULL);
218*7c478bd9Sstevel@tonic-gate 	backend_query_free(q);
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	if (r == REP_PROTOCOL_SUCCESS)
221*7c478bd9Sstevel@tonic-gate 		do_delete = 1;
222*7c478bd9Sstevel@tonic-gate 	else if (r == REP_PROTOCOL_DONE)
223*7c478bd9Sstevel@tonic-gate 		do_delete = 0;		/* old gen_id is in use */
224*7c478bd9Sstevel@tonic-gate 	else
225*7c478bd9Sstevel@tonic-gate 		return (r);
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < count; idx++) {
228*7c478bd9Sstevel@tonic-gate 		elem = &data->txc_cmds[idx];
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 		if (elem->tx_bad)
231*7c478bd9Sstevel@tonic-gate 			continue;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 		switch (elem->tx_cmd->rptc_action) {
234*7c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_TX_ENTRY_DELETE:
235*7c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_TX_ENTRY_REPLACE:
236*7c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_TX_ENTRY_CLEAR:
237*7c478bd9Sstevel@tonic-gate 			if (!elem->tx_found) {
238*7c478bd9Sstevel@tonic-gate 				elem->tx_bad = 1;
239*7c478bd9Sstevel@tonic-gate 				continue;
240*7c478bd9Sstevel@tonic-gate 			}
241*7c478bd9Sstevel@tonic-gate 			break;
242*7c478bd9Sstevel@tonic-gate 		case REP_PROTOCOL_TX_ENTRY_NEW:
243*7c478bd9Sstevel@tonic-gate 			break;
244*7c478bd9Sstevel@tonic-gate 		default:
245*7c478bd9Sstevel@tonic-gate 			assert(0);
246*7c478bd9Sstevel@tonic-gate 			break;
247*7c478bd9Sstevel@tonic-gate 		}
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 		if (do_delete &&
250*7c478bd9Sstevel@tonic-gate 		    elem->tx_cmd->rptc_action != REP_PROTOCOL_TX_ENTRY_NEW &&
251*7c478bd9Sstevel@tonic-gate 		    elem->tx_orig_value_id != 0) {
252*7c478bd9Sstevel@tonic-gate 			/*
253*7c478bd9Sstevel@tonic-gate 			 * delete the old values, if they are not in use
254*7c478bd9Sstevel@tonic-gate 			 */
255*7c478bd9Sstevel@tonic-gate 			q = backend_query_alloc();
256*7c478bd9Sstevel@tonic-gate 			backend_query_add(q,
257*7c478bd9Sstevel@tonic-gate 			    "SELECT 1 FROM prop_lnk_tbl "
258*7c478bd9Sstevel@tonic-gate 			    "    WHERE (lnk_val_id = %d); "
259*7c478bd9Sstevel@tonic-gate 			    "DELETE FROM value_tbl"
260*7c478bd9Sstevel@tonic-gate 			    "    WHERE (value_id = %d)",
261*7c478bd9Sstevel@tonic-gate 			    elem->tx_orig_value_id, elem->tx_orig_value_id);
262*7c478bd9Sstevel@tonic-gate 			r = backend_tx_run(data->txc_tx, q,
263*7c478bd9Sstevel@tonic-gate 			    backend_fail_if_seen, NULL);
264*7c478bd9Sstevel@tonic-gate 			backend_query_free(q);
265*7c478bd9Sstevel@tonic-gate 			if (r != REP_PROTOCOL_SUCCESS && r != REP_PROTOCOL_DONE)
266*7c478bd9Sstevel@tonic-gate 				return (r);
267*7c478bd9Sstevel@tonic-gate 		}
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 		if (elem->tx_cmd->rptc_action == REP_PROTOCOL_TX_ENTRY_DELETE)
270*7c478bd9Sstevel@tonic-gate 			continue;		/* no further work to do */
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 		type[0] = REP_PROTOCOL_BASE_TYPE(elem->tx_cmd->rptc_type);
273*7c478bd9Sstevel@tonic-gate 		type[1] = REP_PROTOCOL_SUBTYPE(elem->tx_cmd->rptc_type);
274*7c478bd9Sstevel@tonic-gate 		type[2] = 0;
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate 		if (elem->tx_nvalues == 0) {
277*7c478bd9Sstevel@tonic-gate 			r = backend_tx_run_update(data->txc_tx,
278*7c478bd9Sstevel@tonic-gate 			    "INSERT INTO prop_lnk_tbl"
279*7c478bd9Sstevel@tonic-gate 			    "    (lnk_pg_id, lnk_gen_id, "
280*7c478bd9Sstevel@tonic-gate 			    "    lnk_prop_name, lnk_prop_type, lnk_val_id) "
281*7c478bd9Sstevel@tonic-gate 			    "VALUES ( %d, %d, '%q', '%q', NULL );",
282*7c478bd9Sstevel@tonic-gate 			    data->txc_pg_id, data->txc_gen, elem->tx_prop,
283*7c478bd9Sstevel@tonic-gate 			    type);
284*7c478bd9Sstevel@tonic-gate 		} else {
285*7c478bd9Sstevel@tonic-gate 			uint32_t *v;
286*7c478bd9Sstevel@tonic-gate 			const char *str;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 			val_id = backend_new_id(data->txc_tx, BACKEND_ID_VALUE);
289*7c478bd9Sstevel@tonic-gate 			if (val_id == 0)
290*7c478bd9Sstevel@tonic-gate 				return (REP_PROTOCOL_FAIL_NO_RESOURCES);
291*7c478bd9Sstevel@tonic-gate 			r = backend_tx_run_update(data->txc_tx,
292*7c478bd9Sstevel@tonic-gate 			    "INSERT INTO prop_lnk_tbl "
293*7c478bd9Sstevel@tonic-gate 			    "    (lnk_pg_id, lnk_gen_id, "
294*7c478bd9Sstevel@tonic-gate 			    "    lnk_prop_name, lnk_prop_type, lnk_val_id) "
295*7c478bd9Sstevel@tonic-gate 			    "VALUES ( %d, %d, '%q', '%q', %d );",
296*7c478bd9Sstevel@tonic-gate 			    data->txc_pg_id, data->txc_gen, elem->tx_prop,
297*7c478bd9Sstevel@tonic-gate 			    type, val_id);
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 			v = elem->tx_values;
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 			while (r == REP_PROTOCOL_SUCCESS &&
302*7c478bd9Sstevel@tonic-gate 			    elem->tx_nvalues--) {
303*7c478bd9Sstevel@tonic-gate 				str = (const char *)&v[1];
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 				r = backend_tx_run_update(data->txc_tx,
306*7c478bd9Sstevel@tonic-gate 				    "INSERT INTO value_tbl "
307*7c478bd9Sstevel@tonic-gate 				    " (value_id, value_type, value_value) "
308*7c478bd9Sstevel@tonic-gate 				    "VALUES (%d, '%c', '%q');\n",
309*7c478bd9Sstevel@tonic-gate 				    val_id, elem->tx_cmd->rptc_type, str);
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 				/*LINTED alignment*/
312*7c478bd9Sstevel@tonic-gate 				v = (uint32_t *)((caddr_t)str + TX_SIZE(*v));
313*7c478bd9Sstevel@tonic-gate 			}
314*7c478bd9Sstevel@tonic-gate 		}
315*7c478bd9Sstevel@tonic-gate 		if (r != REP_PROTOCOL_SUCCESS)
316*7c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_UNKNOWN);
317*7c478bd9Sstevel@tonic-gate 		elem->tx_processed = 1;
318*7c478bd9Sstevel@tonic-gate 	}
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < count; idx++) {
321*7c478bd9Sstevel@tonic-gate 		elem = &data->txc_cmds[idx];
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 		if (elem->tx_bad)
324*7c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_BAD_TX);
325*7c478bd9Sstevel@tonic-gate 	}
326*7c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
327*7c478bd9Sstevel@tonic-gate }
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate static boolean_t
330*7c478bd9Sstevel@tonic-gate check_string(uintptr_t loc, uint32_t len, uint32_t sz)
331*7c478bd9Sstevel@tonic-gate {
332*7c478bd9Sstevel@tonic-gate 	const char *ptr = (const char *)loc;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 	if (len == 0 || len > sz || ptr[len - 1] != 0 || strlen(ptr) != len - 1)
335*7c478bd9Sstevel@tonic-gate 		return (0);
336*7c478bd9Sstevel@tonic-gate 	return (1);
337*7c478bd9Sstevel@tonic-gate }
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate static int
340*7c478bd9Sstevel@tonic-gate tx_check_and_setup(struct tx_commit_data *data, const void *cmds_arg,
341*7c478bd9Sstevel@tonic-gate     uint32_t count)
342*7c478bd9Sstevel@tonic-gate {
343*7c478bd9Sstevel@tonic-gate 	const struct rep_protocol_transaction_cmd *cmds;
344*7c478bd9Sstevel@tonic-gate 	struct tx_cmd *cur;
345*7c478bd9Sstevel@tonic-gate 	struct tx_cmd *prev = NULL;
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	uintptr_t loc;
348*7c478bd9Sstevel@tonic-gate 	uint32_t sz, len;
349*7c478bd9Sstevel@tonic-gate 	int idx;
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 	loc = (uintptr_t)cmds_arg;
352*7c478bd9Sstevel@tonic-gate 
353*7c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < count; idx++) {
354*7c478bd9Sstevel@tonic-gate 		cur = &data->txc_cmds[idx];
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 		cmds = (struct rep_protocol_transaction_cmd *)loc;
357*7c478bd9Sstevel@tonic-gate 		cur->tx_cmd = cmds;
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 		sz = cmds->rptc_size;
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 		loc += REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE;
362*7c478bd9Sstevel@tonic-gate 		sz -= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE;
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 		len = cmds->rptc_name_len;
365*7c478bd9Sstevel@tonic-gate 		if (len <= 1 || !check_string(loc, len, sz)) {
366*7c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
367*7c478bd9Sstevel@tonic-gate 		}
368*7c478bd9Sstevel@tonic-gate 		cur->tx_prop = (const char *)loc;
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 		len = TX_SIZE(len);
371*7c478bd9Sstevel@tonic-gate 		loc += len;
372*7c478bd9Sstevel@tonic-gate 		sz -= len;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 		cur->tx_nvalues = 0;
375*7c478bd9Sstevel@tonic-gate 		cur->tx_values = (uint32_t *)loc;
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 		while (sz > 0) {
378*7c478bd9Sstevel@tonic-gate 			if (sz < sizeof (uint32_t))
379*7c478bd9Sstevel@tonic-gate 				return (REP_PROTOCOL_FAIL_BAD_REQUEST);
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 			cur->tx_nvalues++;
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 			len = *(uint32_t *)loc;
384*7c478bd9Sstevel@tonic-gate 			loc += sizeof (uint32_t);
385*7c478bd9Sstevel@tonic-gate 			sz -= sizeof (uint32_t);
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate 			if (!check_string(loc, len, sz))
388*7c478bd9Sstevel@tonic-gate 				return (REP_PROTOCOL_FAIL_BAD_REQUEST);
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 			/*
391*7c478bd9Sstevel@tonic-gate 			 * XXX here, we should be checking that the values
392*7c478bd9Sstevel@tonic-gate 			 * match the purported type
393*7c478bd9Sstevel@tonic-gate 			 */
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 			len = TX_SIZE(len);
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 			if (len > sz)
398*7c478bd9Sstevel@tonic-gate 				return (REP_PROTOCOL_FAIL_BAD_REQUEST);
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 			loc += len;
401*7c478bd9Sstevel@tonic-gate 			sz -= len;
402*7c478bd9Sstevel@tonic-gate 		}
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 		if (prev != NULL && strcmp(prev->tx_prop, cur->tx_prop) >= 0)
405*7c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 		prev = cur;
408*7c478bd9Sstevel@tonic-gate 	}
409*7c478bd9Sstevel@tonic-gate 	return (REP_PROTOCOL_SUCCESS);
410*7c478bd9Sstevel@tonic-gate }
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate int
413*7c478bd9Sstevel@tonic-gate object_tx_commit(rc_node_lookup_t *lp, const void *cmds_arg, size_t cmds_sz,
414*7c478bd9Sstevel@tonic-gate     uint32_t *gen)
415*7c478bd9Sstevel@tonic-gate {
416*7c478bd9Sstevel@tonic-gate 	const struct rep_protocol_transaction_cmd *cmds;
417*7c478bd9Sstevel@tonic-gate 	uintptr_t loc;
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	struct tx_commit_data *data;
420*7c478bd9Sstevel@tonic-gate 	uint32_t count, sz;
421*7c478bd9Sstevel@tonic-gate 	uint32_t new_gen;
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate 	int ret;
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	rep_protocol_responseid_t r;
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	backend_tx_t *tx;
428*7c478bd9Sstevel@tonic-gate 	backend_query_t *q;
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 	int backend = lp->rl_backend;
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 	/*
433*7c478bd9Sstevel@tonic-gate 	 * First, verify that the reported sizes make sense, and count
434*7c478bd9Sstevel@tonic-gate 	 * the number of commands.
435*7c478bd9Sstevel@tonic-gate 	 */
436*7c478bd9Sstevel@tonic-gate 	count = 0;
437*7c478bd9Sstevel@tonic-gate 	loc = (uintptr_t)cmds_arg;
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate 	while (cmds_sz > 0) {
440*7c478bd9Sstevel@tonic-gate 		cmds = (struct rep_protocol_transaction_cmd *)loc;
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 		if (cmds_sz <= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE)
443*7c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 		sz = cmds->rptc_size;
446*7c478bd9Sstevel@tonic-gate 		if (sz <= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE)
447*7c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
448*7c478bd9Sstevel@tonic-gate 
449*7c478bd9Sstevel@tonic-gate 		sz = TX_SIZE(sz);
450*7c478bd9Sstevel@tonic-gate 		if (sz > cmds_sz)
451*7c478bd9Sstevel@tonic-gate 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 		loc += sz;
454*7c478bd9Sstevel@tonic-gate 		cmds_sz -= sz;
455*7c478bd9Sstevel@tonic-gate 		count++;
456*7c478bd9Sstevel@tonic-gate 	}
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	data = alloca(TX_COMMIT_DATA_SIZE(count));
459*7c478bd9Sstevel@tonic-gate 	(void) memset(data, 0, TX_COMMIT_DATA_SIZE(count));
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 	/*
462*7c478bd9Sstevel@tonic-gate 	 * verify that everything looks okay, and set up our command
463*7c478bd9Sstevel@tonic-gate 	 * datastructures.
464*7c478bd9Sstevel@tonic-gate 	 */
465*7c478bd9Sstevel@tonic-gate 	ret = tx_check_and_setup(data, cmds_arg, count);
466*7c478bd9Sstevel@tonic-gate 	if (ret != REP_PROTOCOL_SUCCESS)
467*7c478bd9Sstevel@tonic-gate 		return (ret);
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 	ret = backend_tx_begin(backend, &tx);
470*7c478bd9Sstevel@tonic-gate 	if (ret != REP_PROTOCOL_SUCCESS)
471*7c478bd9Sstevel@tonic-gate 		return (ret);
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 	/* Make sure the pg is up-to-date. */
474*7c478bd9Sstevel@tonic-gate 	data->txc_oldgen = *gen;
475*7c478bd9Sstevel@tonic-gate 	data->txc_backend = backend;
476*7c478bd9Sstevel@tonic-gate 	data->txc_result = REP_PROTOCOL_FAIL_NOT_FOUND;
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	q = backend_query_alloc();
479*7c478bd9Sstevel@tonic-gate 	backend_query_add(q, "SELECT pg_gen_id FROM pg_tbl WHERE (pg_id = %d);",
480*7c478bd9Sstevel@tonic-gate 	    lp->rl_main_id);
481*7c478bd9Sstevel@tonic-gate 	r = backend_tx_run(tx, q, tx_check_genid, data);
482*7c478bd9Sstevel@tonic-gate 	backend_query_free(q);
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS ||
485*7c478bd9Sstevel@tonic-gate 	    (r = data->txc_result) != REP_PROTOCOL_SUCCESS) {
486*7c478bd9Sstevel@tonic-gate 		backend_tx_rollback(tx);
487*7c478bd9Sstevel@tonic-gate 		goto end;
488*7c478bd9Sstevel@tonic-gate 	}
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 	/* If the transaction is empty, cut out early. */
491*7c478bd9Sstevel@tonic-gate 	if (count == 0) {
492*7c478bd9Sstevel@tonic-gate 		backend_tx_rollback(tx);
493*7c478bd9Sstevel@tonic-gate 		r = REP_PROTOCOL_DONE;
494*7c478bd9Sstevel@tonic-gate 		goto end;
495*7c478bd9Sstevel@tonic-gate 	}
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate 	new_gen = backend_new_id(tx, BACKEND_ID_GENERATION);
498*7c478bd9Sstevel@tonic-gate 	if (new_gen == 0) {
499*7c478bd9Sstevel@tonic-gate 		backend_tx_rollback(tx);
500*7c478bd9Sstevel@tonic-gate 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
501*7c478bd9Sstevel@tonic-gate 	}
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate 	data->txc_pg_id = lp->rl_main_id;
504*7c478bd9Sstevel@tonic-gate 	data->txc_gen = new_gen;
505*7c478bd9Sstevel@tonic-gate 	data->txc_tx = tx;
506*7c478bd9Sstevel@tonic-gate 	data->txc_count = count;
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate 	r = backend_tx_run_update(tx,
509*7c478bd9Sstevel@tonic-gate 	    "UPDATE pg_tbl SET pg_gen_id = %d "
510*7c478bd9Sstevel@tonic-gate 	    "    WHERE (pg_id = %d AND pg_gen_id = %d);",
511*7c478bd9Sstevel@tonic-gate 	    new_gen, lp->rl_main_id, *gen);
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS) {
514*7c478bd9Sstevel@tonic-gate 		backend_tx_rollback(tx);
515*7c478bd9Sstevel@tonic-gate 		goto end;
516*7c478bd9Sstevel@tonic-gate 	}
517*7c478bd9Sstevel@tonic-gate 
518*7c478bd9Sstevel@tonic-gate 	q = backend_query_alloc();
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate 	backend_query_add(q,
521*7c478bd9Sstevel@tonic-gate 	    "SELECT lnk_prop_name, lnk_prop_type, lnk_val_id "
522*7c478bd9Sstevel@tonic-gate 	    "FROM prop_lnk_tbl "
523*7c478bd9Sstevel@tonic-gate 	    "WHERE (lnk_pg_id = %d AND lnk_gen_id = %d)",
524*7c478bd9Sstevel@tonic-gate 	    lp->rl_main_id, *gen);
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 	data->txc_inserts = backend_query_alloc();
527*7c478bd9Sstevel@tonic-gate 	r = backend_tx_run(tx, q, tx_process_property, data);
528*7c478bd9Sstevel@tonic-gate 	backend_query_free(q);
529*7c478bd9Sstevel@tonic-gate 
530*7c478bd9Sstevel@tonic-gate 	if (r == REP_PROTOCOL_DONE)
531*7c478bd9Sstevel@tonic-gate 		r = REP_PROTOCOL_FAIL_UNKNOWN;		/* corruption */
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS ||
534*7c478bd9Sstevel@tonic-gate 	    (r = data->txc_result) != REP_PROTOCOL_SUCCESS) {
535*7c478bd9Sstevel@tonic-gate 		backend_query_free(data->txc_inserts);
536*7c478bd9Sstevel@tonic-gate 		backend_tx_rollback(tx);
537*7c478bd9Sstevel@tonic-gate 		goto end;
538*7c478bd9Sstevel@tonic-gate 	}
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate 	r = backend_tx_run(tx, data->txc_inserts, NULL, NULL);
541*7c478bd9Sstevel@tonic-gate 	backend_query_free(data->txc_inserts);
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS) {
544*7c478bd9Sstevel@tonic-gate 		backend_tx_rollback(tx);
545*7c478bd9Sstevel@tonic-gate 		goto end;
546*7c478bd9Sstevel@tonic-gate 	}
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate 	r = tx_process_cmds(data);
549*7c478bd9Sstevel@tonic-gate 	if (r != REP_PROTOCOL_SUCCESS) {
550*7c478bd9Sstevel@tonic-gate 		backend_tx_rollback(tx);
551*7c478bd9Sstevel@tonic-gate 		goto end;
552*7c478bd9Sstevel@tonic-gate 	}
553*7c478bd9Sstevel@tonic-gate 	r = backend_tx_commit(tx);
554*7c478bd9Sstevel@tonic-gate 
555*7c478bd9Sstevel@tonic-gate 	if (r == REP_PROTOCOL_SUCCESS)
556*7c478bd9Sstevel@tonic-gate 		*gen = new_gen;
557*7c478bd9Sstevel@tonic-gate end:
558*7c478bd9Sstevel@tonic-gate 	return (r);
559*7c478bd9Sstevel@tonic-gate }
560