xref: /titanic_41/usr/src/cmd/svc/configd/object.c (revision 0cfdb6036e046270988a17ac442e4d717d426a44)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * This file only contains the transaction commit logic.
31  */
32 
33 #include <assert.h>
34 #include <alloca.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <strings.h>
39 #include <sys/sysmacros.h>
40 #include "configd.h"
41 
42 #define	INVALID_OBJ_ID ((uint32_t)-1)
43 #define	INVALID_TYPE ((uint32_t)-1)
44 
45 struct tx_cmd {
46 	const struct rep_protocol_transaction_cmd *tx_cmd;
47 	const char	*tx_prop;
48 	uint32_t	*tx_values;
49 	uint32_t	tx_nvalues;
50 	uint32_t	tx_orig_value_id;
51 	char		tx_found;
52 	char		tx_processed;
53 	char		tx_bad;
54 };
55 
56 static int
57 tx_cmd_compare(const void *key, const void *elem_arg)
58 {
59 	const struct tx_cmd *elem = elem_arg;
60 
61 	return (strcmp((const char *)key, elem->tx_prop));
62 }
63 
64 struct tx_commit_data {
65 	uint32_t	txc_pg_id;
66 	uint32_t	txc_gen;
67 	uint32_t	txc_oldgen;
68 	short		txc_backend;
69 	backend_tx_t	*txc_tx;
70 	backend_query_t	*txc_inserts;
71 	size_t		txc_count;
72 	rep_protocol_responseid_t txc_result;
73 	struct tx_cmd	txc_cmds[1];		/* actually txc_count */
74 };
75 #define	TX_COMMIT_DATA_SIZE(count) \
76 	offsetof(struct tx_commit_data, txc_cmds[count])
77 
78 /*ARGSUSED*/
79 static int
80 tx_check_genid(void *data_arg, int columns, char **vals, char **names)
81 {
82 	tx_commit_data_t *data = data_arg;
83 	assert(columns == 1);
84 	if (atoi(vals[0]) != data->txc_oldgen)
85 		data->txc_result = REP_PROTOCOL_FAIL_NOT_LATEST;
86 	else
87 		data->txc_result = REP_PROTOCOL_SUCCESS;
88 	return (BACKEND_CALLBACK_CONTINUE);
89 }
90 
91 /*
92  * tx_process_property() is called once for each property in current
93  * property group generation.  Its purpose is threefold:
94  *
95  *	1. copy properties not mentioned in the transaction over unchanged.
96  *	2. mark DELETEd properties as seen (they will be left out of the new
97  *	   generation).
98  *	3. consistancy-check NEW, CLEAR, and REPLACE commands.
99  *
100  * Any consistancy problems set tx_bad, and seen properties are marked
101  * tx_found.  These is used later, in tx_process_cmds().
102  */
103 /*ARGSUSED*/
104 static int
105 tx_process_property(void *data_arg, int columns, char **vals, char **names)
106 {
107 	tx_commit_data_t *data = data_arg;
108 	struct tx_cmd *elem;
109 
110 	const char *prop_name = vals[0];
111 	const char *prop_type = vals[1];
112 	const char *lnk_val_id = vals[2];
113 
114 	char *endptr;
115 
116 	assert(columns == 3);
117 
118 	elem = bsearch(prop_name, data->txc_cmds, data->txc_count,
119 	    sizeof (*data->txc_cmds), tx_cmd_compare);
120 
121 	if (elem == NULL) {
122 		backend_query_add(data->txc_inserts,
123 		    "INSERT INTO prop_lnk_tbl"
124 		    "    (lnk_pg_id, lnk_gen_id, lnk_prop_name, lnk_prop_type,"
125 		    "    lnk_val_id) "
126 		    "VALUES ( %d, %d, '%q', '%q', %Q );",
127 		    data->txc_pg_id, data->txc_gen, prop_name, prop_type,
128 		    lnk_val_id);
129 	} else {
130 		assert(!elem->tx_found);
131 		elem->tx_found = 1;
132 
133 		if (lnk_val_id != NULL) {
134 			errno = 0;
135 			elem->tx_orig_value_id =
136 			    strtoul(lnk_val_id, &endptr, 10);
137 			if (elem->tx_orig_value_id == 0 || *endptr != 0 ||
138 			    errno != 0) {
139 				return (BACKEND_CALLBACK_ABORT);
140 			}
141 		} else {
142 			elem->tx_orig_value_id = 0;
143 		}
144 
145 		switch (elem->tx_cmd->rptc_action) {
146 		case REP_PROTOCOL_TX_ENTRY_NEW:
147 			elem->tx_bad = 1;
148 			data->txc_result = REP_PROTOCOL_FAIL_EXISTS;
149 			break;
150 		case REP_PROTOCOL_TX_ENTRY_CLEAR:
151 			if (REP_PROTOCOL_BASE_TYPE(elem->tx_cmd->rptc_type) !=
152 			    prop_type[0] &&
153 			    REP_PROTOCOL_SUBTYPE(elem->tx_cmd->rptc_type) !=
154 			    prop_type[1]) {
155 				elem->tx_bad = 1;
156 				data->txc_result =
157 				    REP_PROTOCOL_FAIL_TYPE_MISMATCH;
158 			}
159 			break;
160 		case REP_PROTOCOL_TX_ENTRY_REPLACE:
161 			break;
162 		case REP_PROTOCOL_TX_ENTRY_DELETE:
163 			elem->tx_processed = 1;
164 			break;
165 		default:
166 			assert(0);
167 			break;
168 		}
169 	}
170 	return (BACKEND_CALLBACK_CONTINUE);
171 }
172 
173 /*
174  * tx_process_cmds() finishes the job tx_process_property() started:
175  *
176  *	1. if tx_process_property() marked a command as bad, we skip it.
177  *	2. if a DELETE, REPLACE, or CLEAR operated on a non-existant property,
178  *	    we mark it as bad.
179  *	3. we complete the work of NEW, REPLACE, and CLEAR, by inserting the
180  *	    appropriate values into the database.
181  *	4. we delete all replaced data, if it is no longer referenced.
182  *
183  * Finally, we check all of the commands, and fail if anything was marked bad.
184  */
185 static int
186 tx_process_cmds(tx_commit_data_t *data)
187 {
188 	int idx;
189 	int r;
190 	int count = data->txc_count;
191 	struct tx_cmd *elem;
192 	uint32_t val_id = 0;
193 	uint8_t type[3];
194 
195 	backend_query_t *q;
196 	int do_delete;
197 	uint32_t nvalues;
198 
199 	/*
200 	 * For persistent pgs, we use backend_fail_if_seen to abort the
201 	 * deletion if there is a snapshot using our current state.
202 	 *
203 	 * All of the deletions in this function are safe, since
204 	 * rc_tx_commit() guarantees that all the data is in-cache.
205 	 */
206 	q = backend_query_alloc();
207 
208 	if (data->txc_backend != BACKEND_TYPE_NONPERSIST) {
209 		backend_query_add(q,
210 		    "SELECT 1 FROM snaplevel_lnk_tbl "
211 		    "    WHERE (snaplvl_pg_id = %d AND snaplvl_gen_id = %d); ",
212 		    data->txc_pg_id, data->txc_oldgen);
213 	}
214 	backend_query_add(q,
215 	    "DELETE FROM prop_lnk_tbl"
216 	    "    WHERE (lnk_pg_id = %d AND lnk_gen_id = %d)",
217 	    data->txc_pg_id, data->txc_oldgen);
218 	r = backend_tx_run(data->txc_tx, q, backend_fail_if_seen, NULL);
219 	backend_query_free(q);
220 
221 	if (r == REP_PROTOCOL_SUCCESS)
222 		do_delete = 1;
223 	else if (r == REP_PROTOCOL_DONE)
224 		do_delete = 0;		/* old gen_id is in use */
225 	else
226 		return (r);
227 
228 	for (idx = 0; idx < count; idx++) {
229 		elem = &data->txc_cmds[idx];
230 
231 		if (elem->tx_bad)
232 			continue;
233 
234 		switch (elem->tx_cmd->rptc_action) {
235 		case REP_PROTOCOL_TX_ENTRY_DELETE:
236 		case REP_PROTOCOL_TX_ENTRY_REPLACE:
237 		case REP_PROTOCOL_TX_ENTRY_CLEAR:
238 			if (!elem->tx_found) {
239 				elem->tx_bad = 1;
240 				continue;
241 			}
242 			break;
243 		case REP_PROTOCOL_TX_ENTRY_NEW:
244 			break;
245 		default:
246 			assert(0);
247 			break;
248 		}
249 
250 		if (do_delete &&
251 		    elem->tx_cmd->rptc_action != REP_PROTOCOL_TX_ENTRY_NEW &&
252 		    elem->tx_orig_value_id != 0) {
253 			/*
254 			 * delete the old values, if they are not in use
255 			 */
256 			q = backend_query_alloc();
257 			backend_query_add(q,
258 			    "SELECT 1 FROM prop_lnk_tbl "
259 			    "    WHERE (lnk_val_id = %d); "
260 			    "DELETE FROM value_tbl"
261 			    "    WHERE (value_id = %d)",
262 			    elem->tx_orig_value_id, elem->tx_orig_value_id);
263 			r = backend_tx_run(data->txc_tx, q,
264 			    backend_fail_if_seen, NULL);
265 			backend_query_free(q);
266 			if (r != REP_PROTOCOL_SUCCESS && r != REP_PROTOCOL_DONE)
267 				return (r);
268 		}
269 
270 		if (elem->tx_cmd->rptc_action == REP_PROTOCOL_TX_ENTRY_DELETE)
271 			continue;		/* no further work to do */
272 
273 		type[0] = REP_PROTOCOL_BASE_TYPE(elem->tx_cmd->rptc_type);
274 		type[1] = REP_PROTOCOL_SUBTYPE(elem->tx_cmd->rptc_type);
275 		type[2] = 0;
276 
277 		if (elem->tx_nvalues == 0) {
278 			r = backend_tx_run_update(data->txc_tx,
279 			    "INSERT INTO prop_lnk_tbl"
280 			    "    (lnk_pg_id, lnk_gen_id, "
281 			    "    lnk_prop_name, lnk_prop_type, lnk_val_id) "
282 			    "VALUES ( %d, %d, '%q', '%q', NULL );",
283 			    data->txc_pg_id, data->txc_gen, elem->tx_prop,
284 			    type);
285 		} else {
286 			uint32_t *v;
287 			const char *str;
288 
289 			val_id = backend_new_id(data->txc_tx, BACKEND_ID_VALUE);
290 			if (val_id == 0)
291 				return (REP_PROTOCOL_FAIL_NO_RESOURCES);
292 			r = backend_tx_run_update(data->txc_tx,
293 			    "INSERT INTO prop_lnk_tbl "
294 			    "    (lnk_pg_id, lnk_gen_id, "
295 			    "    lnk_prop_name, lnk_prop_type, lnk_val_id) "
296 			    "VALUES ( %d, %d, '%q', '%q', %d );",
297 			    data->txc_pg_id, data->txc_gen, elem->tx_prop,
298 			    type, val_id);
299 
300 			v = elem->tx_values;
301 
302 			nvalues = elem->tx_nvalues;
303 			while (r == REP_PROTOCOL_SUCCESS &&
304 			    nvalues--) {
305 				str = (const char *)&v[1];
306 
307 				r = backend_tx_run_update(data->txc_tx,
308 				    "INSERT INTO value_tbl "
309 				    " (value_id, value_type, value_value) "
310 				    "VALUES (%d, '%c', '%q');\n",
311 				    val_id, elem->tx_cmd->rptc_type, str);
312 
313 				/*LINTED alignment*/
314 				v = (uint32_t *)((caddr_t)str + TX_SIZE(*v));
315 			}
316 		}
317 		if (r != REP_PROTOCOL_SUCCESS)
318 			return (REP_PROTOCOL_FAIL_UNKNOWN);
319 		elem->tx_processed = 1;
320 	}
321 
322 	for (idx = 0; idx < count; idx++) {
323 		elem = &data->txc_cmds[idx];
324 
325 		if (elem->tx_bad)
326 			return (REP_PROTOCOL_FAIL_BAD_TX);
327 	}
328 	return (REP_PROTOCOL_SUCCESS);
329 }
330 
331 static boolean_t
332 check_string(uintptr_t loc, uint32_t len, uint32_t sz)
333 {
334 	const char *ptr = (const char *)loc;
335 
336 	if (len == 0 || len > sz || ptr[len - 1] != 0 || strlen(ptr) != len - 1)
337 		return (0);
338 	return (1);
339 }
340 
341 static int
342 tx_check_and_setup(tx_commit_data_t *data, const void *cmds_arg,
343     uint32_t count)
344 {
345 	const struct rep_protocol_transaction_cmd *cmds;
346 	struct tx_cmd *cur;
347 	struct tx_cmd *prev = NULL;
348 
349 	uintptr_t loc;
350 	uint32_t sz, len;
351 	int idx;
352 
353 	loc = (uintptr_t)cmds_arg;
354 
355 	for (idx = 0; idx < count; idx++) {
356 		cur = &data->txc_cmds[idx];
357 
358 		cmds = (struct rep_protocol_transaction_cmd *)loc;
359 		cur->tx_cmd = cmds;
360 
361 		sz = cmds->rptc_size;
362 
363 		loc += REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE;
364 		sz -= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE;
365 
366 		len = cmds->rptc_name_len;
367 		if (len <= 1 || !check_string(loc, len, sz)) {
368 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
369 		}
370 		cur->tx_prop = (const char *)loc;
371 
372 		len = TX_SIZE(len);
373 		loc += len;
374 		sz -= len;
375 
376 		cur->tx_nvalues = 0;
377 		cur->tx_values = (uint32_t *)loc;
378 
379 		while (sz > 0) {
380 			if (sz < sizeof (uint32_t))
381 				return (REP_PROTOCOL_FAIL_BAD_REQUEST);
382 
383 			cur->tx_nvalues++;
384 
385 			len = *(uint32_t *)loc;
386 			loc += sizeof (uint32_t);
387 			sz -= sizeof (uint32_t);
388 
389 			if (!check_string(loc, len, sz))
390 				return (REP_PROTOCOL_FAIL_BAD_REQUEST);
391 
392 			/*
393 			 * XXX here, we should be checking that the values
394 			 * match the purported type
395 			 */
396 
397 			len = TX_SIZE(len);
398 
399 			if (len > sz)
400 				return (REP_PROTOCOL_FAIL_BAD_REQUEST);
401 
402 			loc += len;
403 			sz -= len;
404 		}
405 
406 		if (prev != NULL && strcmp(prev->tx_prop, cur->tx_prop) >= 0)
407 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
408 
409 		prev = cur;
410 	}
411 	return (REP_PROTOCOL_SUCCESS);
412 }
413 
414 /*
415  * Free the memory associated with a tx_commit_data structure.
416  */
417 void
418 tx_commit_data_free(tx_commit_data_t *tx_data)
419 {
420 	uu_free(tx_data);
421 }
422 
423 /*
424  * Parse the data of a REP_PROTOCOL_PROPERTYGRP_TX_COMMIT message into a
425  * more useful form.  The data in the message will be represented by a
426  * tx_commit_data_t structure which is allocated by this function.  The
427  * address of the allocated structure is returned to *tx_data and must be
428  * freed by calling tx_commit_data_free().
429  *
430  * Parameters:
431  *	cmds_arg	Address of the commands in the
432  *			REP_PROTOCOL_PROPERTYGRP_TX_COMMIT message.
433  *
434  *	cmds_sz		Number of message bytes at cmds_arg.
435  *
436  *	tx_data		Points to the place to receive the address of the
437  *			allocated memory.
438  *
439  * Fails with
440  *	_BAD_REQUEST
441  *	_NO_RESOURCES
442  */
443 int
444 tx_commit_data_new(const void *cmds_arg, size_t cmds_sz,
445     tx_commit_data_t **tx_data)
446 {
447 	const struct rep_protocol_transaction_cmd *cmds;
448 	tx_commit_data_t *data;
449 	uintptr_t loc;
450 	uint32_t count;
451 	uint32_t sz;
452 	int ret;
453 
454 	/*
455 	 * First, verify that the reported sizes make sense, and count
456 	 * the number of commands.
457 	 */
458 	count = 0;
459 	loc = (uintptr_t)cmds_arg;
460 
461 	while (cmds_sz > 0) {
462 		cmds = (struct rep_protocol_transaction_cmd *)loc;
463 
464 		if (cmds_sz <= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE)
465 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
466 
467 		sz = cmds->rptc_size;
468 		if (sz <= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE)
469 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
470 
471 		sz = TX_SIZE(sz);
472 		if (sz > cmds_sz)
473 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
474 
475 		loc += sz;
476 		cmds_sz -= sz;
477 		count++;
478 	}
479 
480 	data = uu_zalloc(TX_COMMIT_DATA_SIZE(count));
481 	if (data == NULL)
482 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
483 
484 	/*
485 	 * verify that everything looks okay, and set up our command
486 	 * datastructures.
487 	 */
488 	data->txc_count = count;
489 	ret = tx_check_and_setup(data, cmds_arg, count);
490 	if (ret == REP_PROTOCOL_SUCCESS) {
491 		*tx_data = data;
492 	} else {
493 		*tx_data = NULL;
494 		uu_free(data);
495 	}
496 	return (ret);
497 }
498 
499 /*
500  * The following are a set of accessor functions to retrieve data from a
501  * tx_commit_data_t that has been allocated by tx_commit_data_new().
502  */
503 
504 /*
505  * Return the action of the transaction command whose command number is
506  * cmd_no.  The action is placed at *action.
507  *
508  * Returns:
509  *	_FAIL_BAD_REQUEST	cmd_no is out of range.
510  */
511 int
512 tx_cmd_action(tx_commit_data_t *tx_data, size_t cmd_no,
513     enum rep_protocol_transaction_action *action)
514 {
515 	struct tx_cmd *cur;
516 
517 	assert(cmd_no < tx_data->txc_count);
518 	if (cmd_no >= tx_data->txc_count)
519 		return (REP_PROTOCOL_FAIL_BAD_REQUEST);
520 
521 	cur = &tx_data->txc_cmds[cmd_no];
522 	*action = cur->tx_cmd->rptc_action;
523 	return (REP_PROTOCOL_SUCCESS);
524 }
525 
526 /*
527  * Return the number of transaction commands held in tx_data.
528  */
529 size_t
530 tx_cmd_count(tx_commit_data_t *tx_data)
531 {
532 	return (tx_data->txc_count);
533 }
534 
535 /*
536  * Return the number of property values that are associated with the
537  * transaction command whose number is cmd_no.  The number of values is
538  * returned to *nvalues.
539  *
540  * Returns:
541  *	_FAIL_BAD_REQUEST	cmd_no is out of range.
542  */
543 int
544 tx_cmd_nvalues(tx_commit_data_t *tx_data, size_t cmd_no, uint32_t *nvalues)
545 {
546 	struct tx_cmd *cur;
547 
548 	assert(cmd_no < tx_data->txc_count);
549 	if (cmd_no >= tx_data->txc_count)
550 		return (REP_PROTOCOL_FAIL_BAD_REQUEST);
551 
552 	cur = &tx_data->txc_cmds[cmd_no];
553 	*nvalues = cur->tx_nvalues;
554 	return (REP_PROTOCOL_SUCCESS);
555 }
556 
557 /*
558  * Return a pointer to the property name of the command whose number is
559  * cmd_no.  The property name pointer is returned to *pname.
560  *
561  * Returns:
562  *	_FAIL_BAD_REQUEST	cmd_no is out of range.
563  */
564 int
565 tx_cmd_prop(tx_commit_data_t *tx_data, size_t cmd_no, const char **pname)
566 {
567 	struct tx_cmd *cur;
568 
569 	assert(cmd_no < tx_data->txc_count);
570 	if (cmd_no >= tx_data->txc_count)
571 		return (REP_PROTOCOL_FAIL_BAD_REQUEST);
572 
573 	cur = &tx_data->txc_cmds[cmd_no];
574 	*pname = cur->tx_prop;
575 	return (REP_PROTOCOL_SUCCESS);
576 }
577 
578 /*
579  * Return the property type of the property whose command number is
580  * cmd_no.  The property type is returned to *ptype.
581  *
582  * Returns:
583  *	_FAIL_BAD_REQUEST	cmd_no is out of range.
584  */
585 int
586 tx_cmd_prop_type(tx_commit_data_t *tx_data, size_t cmd_no, uint32_t *ptype)
587 {
588 	struct tx_cmd *cur;
589 
590 	assert(cmd_no < tx_data->txc_count);
591 	if (cmd_no >= tx_data->txc_count)
592 		return (REP_PROTOCOL_FAIL_BAD_REQUEST);
593 
594 	cur = &tx_data->txc_cmds[cmd_no];
595 	*ptype = cur->tx_cmd->rptc_type;
596 	return (REP_PROTOCOL_SUCCESS);
597 }
598 
599 /*
600  * This function is used to retrieve a property value from the transaction
601  * data.  val_no specifies which value is to be retrieved from the
602  * transaction command whose number is cmd_no.  A pointer to the specified
603  * value is placed in *val.
604  *
605  * Returns:
606  *	_FAIL_BAD_REQUEST	cmd_no or val_no is out of range.
607  */
608 int
609 tx_cmd_value(tx_commit_data_t *tx_data, size_t cmd_no, uint32_t val_no,
610     const char **val)
611 {
612 	const char *bp;
613 	struct tx_cmd *cur;
614 	uint32_t i;
615 	uint32_t value_len;
616 
617 	assert(cmd_no < tx_data->txc_count);
618 	if (cmd_no >= tx_data->txc_count)
619 		return (REP_PROTOCOL_FAIL_BAD_REQUEST);
620 
621 	cur = &tx_data->txc_cmds[cmd_no];
622 	assert(val_no < cur->tx_nvalues);
623 	if (val_no >= cur->tx_nvalues)
624 		return (REP_PROTOCOL_FAIL_BAD_REQUEST);
625 
626 	/* Find the correct value */
627 	bp = (char *)cur->tx_values;
628 	for (i = 0; i < val_no; i++) {
629 		/* LINTED alignment */
630 		value_len = *(uint32_t *)bp;
631 		bp += sizeof (uint32_t) + TX_SIZE(value_len);
632 	}
633 
634 	/* Bypass the count & return pointer to value. */
635 	bp += sizeof (uint32_t);
636 	*val = bp;
637 	return (REP_PROTOCOL_SUCCESS);
638 }
639 
640 int
641 object_tx_commit(rc_node_lookup_t *lp, tx_commit_data_t *data, uint32_t *gen)
642 {
643 	uint32_t new_gen;
644 	int ret;
645 	rep_protocol_responseid_t r;
646 	backend_tx_t *tx;
647 	backend_query_t *q;
648 	int backend = lp->rl_backend;
649 
650 	ret = backend_tx_begin(backend, &tx);
651 	if (ret != REP_PROTOCOL_SUCCESS)
652 		return (ret);
653 
654 	/* Make sure the pg is up-to-date. */
655 	data->txc_oldgen = *gen;
656 	data->txc_backend = backend;
657 	data->txc_result = REP_PROTOCOL_FAIL_NOT_FOUND;
658 
659 	q = backend_query_alloc();
660 	backend_query_add(q, "SELECT pg_gen_id FROM pg_tbl WHERE (pg_id = %d);",
661 	    lp->rl_main_id);
662 	r = backend_tx_run(tx, q, tx_check_genid, data);
663 	backend_query_free(q);
664 
665 	if (r != REP_PROTOCOL_SUCCESS ||
666 	    (r = data->txc_result) != REP_PROTOCOL_SUCCESS) {
667 		backend_tx_rollback(tx);
668 		goto end;
669 	}
670 
671 	/* If the transaction is empty, cut out early. */
672 	if (data->txc_count == 0) {
673 		backend_tx_rollback(tx);
674 		r = REP_PROTOCOL_DONE;
675 		goto end;
676 	}
677 
678 	new_gen = backend_new_id(tx, BACKEND_ID_GENERATION);
679 	if (new_gen == 0) {
680 		backend_tx_rollback(tx);
681 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
682 	}
683 
684 	data->txc_pg_id = lp->rl_main_id;
685 	data->txc_gen = new_gen;
686 	data->txc_tx = tx;
687 
688 	r = backend_tx_run_update(tx,
689 	    "UPDATE pg_tbl SET pg_gen_id = %d "
690 	    "    WHERE (pg_id = %d AND pg_gen_id = %d);",
691 	    new_gen, lp->rl_main_id, *gen);
692 
693 	if (r != REP_PROTOCOL_SUCCESS) {
694 		backend_tx_rollback(tx);
695 		goto end;
696 	}
697 
698 	q = backend_query_alloc();
699 
700 	backend_query_add(q,
701 	    "SELECT lnk_prop_name, lnk_prop_type, lnk_val_id "
702 	    "FROM prop_lnk_tbl "
703 	    "WHERE (lnk_pg_id = %d AND lnk_gen_id = %d)",
704 	    lp->rl_main_id, *gen);
705 
706 	data->txc_inserts = backend_query_alloc();
707 	r = backend_tx_run(tx, q, tx_process_property, data);
708 	backend_query_free(q);
709 
710 	if (r == REP_PROTOCOL_DONE)
711 		r = REP_PROTOCOL_FAIL_UNKNOWN;		/* corruption */
712 
713 	if (r != REP_PROTOCOL_SUCCESS ||
714 	    (r = data->txc_result) != REP_PROTOCOL_SUCCESS) {
715 		backend_query_free(data->txc_inserts);
716 		backend_tx_rollback(tx);
717 		goto end;
718 	}
719 
720 	r = backend_tx_run(tx, data->txc_inserts, NULL, NULL);
721 	backend_query_free(data->txc_inserts);
722 
723 	if (r != REP_PROTOCOL_SUCCESS) {
724 		backend_tx_rollback(tx);
725 		goto end;
726 	}
727 
728 	r = tx_process_cmds(data);
729 	if (r != REP_PROTOCOL_SUCCESS) {
730 		backend_tx_rollback(tx);
731 		goto end;
732 	}
733 	r = backend_tx_commit(tx);
734 
735 	if (r == REP_PROTOCOL_SUCCESS)
736 		*gen = new_gen;
737 end:
738 	return (r);
739 }
740