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