xref: /illumos-gate/usr/src/cmd/svc/configd/object.c (revision 3db86aab)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 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 	struct tx_commit_data *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 	struct tx_commit_data *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(struct tx_commit_data *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 
198 	/*
199 	 * For persistent pgs, we use backend_fail_if_seen to abort the
200 	 * deletion if there is a snapshot using our current state.
201 	 *
202 	 * All of the deletions in this function are safe, since
203 	 * rc_tx_commit() guarantees that all the data is in-cache.
204 	 */
205 	q = backend_query_alloc();
206 
207 	if (data->txc_backend != BACKEND_TYPE_NONPERSIST) {
208 		backend_query_add(q,
209 		    "SELECT 1 FROM snaplevel_lnk_tbl "
210 		    "    WHERE (snaplvl_pg_id = %d AND snaplvl_gen_id = %d); ",
211 		    data->txc_pg_id, data->txc_oldgen);
212 	}
213 	backend_query_add(q,
214 	    "DELETE FROM prop_lnk_tbl"
215 	    "    WHERE (lnk_pg_id = %d AND lnk_gen_id = %d)",
216 	    data->txc_pg_id, data->txc_oldgen);
217 	r = backend_tx_run(data->txc_tx, q, backend_fail_if_seen, NULL);
218 	backend_query_free(q);
219 
220 	if (r == REP_PROTOCOL_SUCCESS)
221 		do_delete = 1;
222 	else if (r == REP_PROTOCOL_DONE)
223 		do_delete = 0;		/* old gen_id is in use */
224 	else
225 		return (r);
226 
227 	for (idx = 0; idx < count; idx++) {
228 		elem = &data->txc_cmds[idx];
229 
230 		if (elem->tx_bad)
231 			continue;
232 
233 		switch (elem->tx_cmd->rptc_action) {
234 		case REP_PROTOCOL_TX_ENTRY_DELETE:
235 		case REP_PROTOCOL_TX_ENTRY_REPLACE:
236 		case REP_PROTOCOL_TX_ENTRY_CLEAR:
237 			if (!elem->tx_found) {
238 				elem->tx_bad = 1;
239 				continue;
240 			}
241 			break;
242 		case REP_PROTOCOL_TX_ENTRY_NEW:
243 			break;
244 		default:
245 			assert(0);
246 			break;
247 		}
248 
249 		if (do_delete &&
250 		    elem->tx_cmd->rptc_action != REP_PROTOCOL_TX_ENTRY_NEW &&
251 		    elem->tx_orig_value_id != 0) {
252 			/*
253 			 * delete the old values, if they are not in use
254 			 */
255 			q = backend_query_alloc();
256 			backend_query_add(q,
257 			    "SELECT 1 FROM prop_lnk_tbl "
258 			    "    WHERE (lnk_val_id = %d); "
259 			    "DELETE FROM value_tbl"
260 			    "    WHERE (value_id = %d)",
261 			    elem->tx_orig_value_id, elem->tx_orig_value_id);
262 			r = backend_tx_run(data->txc_tx, q,
263 			    backend_fail_if_seen, NULL);
264 			backend_query_free(q);
265 			if (r != REP_PROTOCOL_SUCCESS && r != REP_PROTOCOL_DONE)
266 				return (r);
267 		}
268 
269 		if (elem->tx_cmd->rptc_action == REP_PROTOCOL_TX_ENTRY_DELETE)
270 			continue;		/* no further work to do */
271 
272 		type[0] = REP_PROTOCOL_BASE_TYPE(elem->tx_cmd->rptc_type);
273 		type[1] = REP_PROTOCOL_SUBTYPE(elem->tx_cmd->rptc_type);
274 		type[2] = 0;
275 
276 		if (elem->tx_nvalues == 0) {
277 			r = backend_tx_run_update(data->txc_tx,
278 			    "INSERT INTO prop_lnk_tbl"
279 			    "    (lnk_pg_id, lnk_gen_id, "
280 			    "    lnk_prop_name, lnk_prop_type, lnk_val_id) "
281 			    "VALUES ( %d, %d, '%q', '%q', NULL );",
282 			    data->txc_pg_id, data->txc_gen, elem->tx_prop,
283 			    type);
284 		} else {
285 			uint32_t *v;
286 			const char *str;
287 
288 			val_id = backend_new_id(data->txc_tx, BACKEND_ID_VALUE);
289 			if (val_id == 0)
290 				return (REP_PROTOCOL_FAIL_NO_RESOURCES);
291 			r = backend_tx_run_update(data->txc_tx,
292 			    "INSERT INTO prop_lnk_tbl "
293 			    "    (lnk_pg_id, lnk_gen_id, "
294 			    "    lnk_prop_name, lnk_prop_type, lnk_val_id) "
295 			    "VALUES ( %d, %d, '%q', '%q', %d );",
296 			    data->txc_pg_id, data->txc_gen, elem->tx_prop,
297 			    type, val_id);
298 
299 			v = elem->tx_values;
300 
301 			while (r == REP_PROTOCOL_SUCCESS &&
302 			    elem->tx_nvalues--) {
303 				str = (const char *)&v[1];
304 
305 				r = backend_tx_run_update(data->txc_tx,
306 				    "INSERT INTO value_tbl "
307 				    " (value_id, value_type, value_value) "
308 				    "VALUES (%d, '%c', '%q');\n",
309 				    val_id, elem->tx_cmd->rptc_type, str);
310 
311 				/*LINTED alignment*/
312 				v = (uint32_t *)((caddr_t)str + TX_SIZE(*v));
313 			}
314 		}
315 		if (r != REP_PROTOCOL_SUCCESS)
316 			return (REP_PROTOCOL_FAIL_UNKNOWN);
317 		elem->tx_processed = 1;
318 	}
319 
320 	for (idx = 0; idx < count; idx++) {
321 		elem = &data->txc_cmds[idx];
322 
323 		if (elem->tx_bad)
324 			return (REP_PROTOCOL_FAIL_BAD_TX);
325 	}
326 	return (REP_PROTOCOL_SUCCESS);
327 }
328 
329 static boolean_t
330 check_string(uintptr_t loc, uint32_t len, uint32_t sz)
331 {
332 	const char *ptr = (const char *)loc;
333 
334 	if (len == 0 || len > sz || ptr[len - 1] != 0 || strlen(ptr) != len - 1)
335 		return (0);
336 	return (1);
337 }
338 
339 static int
340 tx_check_and_setup(struct tx_commit_data *data, const void *cmds_arg,
341     uint32_t count)
342 {
343 	const struct rep_protocol_transaction_cmd *cmds;
344 	struct tx_cmd *cur;
345 	struct tx_cmd *prev = NULL;
346 
347 	uintptr_t loc;
348 	uint32_t sz, len;
349 	int idx;
350 
351 	loc = (uintptr_t)cmds_arg;
352 
353 	for (idx = 0; idx < count; idx++) {
354 		cur = &data->txc_cmds[idx];
355 
356 		cmds = (struct rep_protocol_transaction_cmd *)loc;
357 		cur->tx_cmd = cmds;
358 
359 		sz = cmds->rptc_size;
360 
361 		loc += REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE;
362 		sz -= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE;
363 
364 		len = cmds->rptc_name_len;
365 		if (len <= 1 || !check_string(loc, len, sz)) {
366 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
367 		}
368 		cur->tx_prop = (const char *)loc;
369 
370 		len = TX_SIZE(len);
371 		loc += len;
372 		sz -= len;
373 
374 		cur->tx_nvalues = 0;
375 		cur->tx_values = (uint32_t *)loc;
376 
377 		while (sz > 0) {
378 			if (sz < sizeof (uint32_t))
379 				return (REP_PROTOCOL_FAIL_BAD_REQUEST);
380 
381 			cur->tx_nvalues++;
382 
383 			len = *(uint32_t *)loc;
384 			loc += sizeof (uint32_t);
385 			sz -= sizeof (uint32_t);
386 
387 			if (!check_string(loc, len, sz))
388 				return (REP_PROTOCOL_FAIL_BAD_REQUEST);
389 
390 			/*
391 			 * XXX here, we should be checking that the values
392 			 * match the purported type
393 			 */
394 
395 			len = TX_SIZE(len);
396 
397 			if (len > sz)
398 				return (REP_PROTOCOL_FAIL_BAD_REQUEST);
399 
400 			loc += len;
401 			sz -= len;
402 		}
403 
404 		if (prev != NULL && strcmp(prev->tx_prop, cur->tx_prop) >= 0)
405 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
406 
407 		prev = cur;
408 	}
409 	return (REP_PROTOCOL_SUCCESS);
410 }
411 
412 int
413 object_tx_commit(rc_node_lookup_t *lp, const void *cmds_arg, size_t cmds_sz,
414     uint32_t *gen)
415 {
416 	const struct rep_protocol_transaction_cmd *cmds;
417 	uintptr_t loc;
418 
419 	struct tx_commit_data *data;
420 	uint32_t count, sz;
421 	uint32_t new_gen;
422 
423 	int ret;
424 
425 	rep_protocol_responseid_t r;
426 
427 	backend_tx_t *tx;
428 	backend_query_t *q;
429 
430 	int backend = lp->rl_backend;
431 
432 	/*
433 	 * First, verify that the reported sizes make sense, and count
434 	 * the number of commands.
435 	 */
436 	count = 0;
437 	loc = (uintptr_t)cmds_arg;
438 
439 	while (cmds_sz > 0) {
440 		cmds = (struct rep_protocol_transaction_cmd *)loc;
441 
442 		if (cmds_sz <= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE)
443 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
444 
445 		sz = cmds->rptc_size;
446 		if (sz <= REP_PROTOCOL_TRANSACTION_CMD_MIN_SIZE)
447 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
448 
449 		sz = TX_SIZE(sz);
450 		if (sz > cmds_sz)
451 			return (REP_PROTOCOL_FAIL_BAD_REQUEST);
452 
453 		loc += sz;
454 		cmds_sz -= sz;
455 		count++;
456 	}
457 
458 	data = alloca(TX_COMMIT_DATA_SIZE(count));
459 	(void) memset(data, 0, TX_COMMIT_DATA_SIZE(count));
460 
461 	/*
462 	 * verify that everything looks okay, and set up our command
463 	 * datastructures.
464 	 */
465 	ret = tx_check_and_setup(data, cmds_arg, count);
466 	if (ret != REP_PROTOCOL_SUCCESS)
467 		return (ret);
468 
469 	ret = backend_tx_begin(backend, &tx);
470 	if (ret != REP_PROTOCOL_SUCCESS)
471 		return (ret);
472 
473 	/* Make sure the pg is up-to-date. */
474 	data->txc_oldgen = *gen;
475 	data->txc_backend = backend;
476 	data->txc_result = REP_PROTOCOL_FAIL_NOT_FOUND;
477 
478 	q = backend_query_alloc();
479 	backend_query_add(q, "SELECT pg_gen_id FROM pg_tbl WHERE (pg_id = %d);",
480 	    lp->rl_main_id);
481 	r = backend_tx_run(tx, q, tx_check_genid, data);
482 	backend_query_free(q);
483 
484 	if (r != REP_PROTOCOL_SUCCESS ||
485 	    (r = data->txc_result) != REP_PROTOCOL_SUCCESS) {
486 		backend_tx_rollback(tx);
487 		goto end;
488 	}
489 
490 	/* If the transaction is empty, cut out early. */
491 	if (count == 0) {
492 		backend_tx_rollback(tx);
493 		r = REP_PROTOCOL_DONE;
494 		goto end;
495 	}
496 
497 	new_gen = backend_new_id(tx, BACKEND_ID_GENERATION);
498 	if (new_gen == 0) {
499 		backend_tx_rollback(tx);
500 		return (REP_PROTOCOL_FAIL_NO_RESOURCES);
501 	}
502 
503 	data->txc_pg_id = lp->rl_main_id;
504 	data->txc_gen = new_gen;
505 	data->txc_tx = tx;
506 	data->txc_count = count;
507 
508 	r = backend_tx_run_update(tx,
509 	    "UPDATE pg_tbl SET pg_gen_id = %d "
510 	    "    WHERE (pg_id = %d AND pg_gen_id = %d);",
511 	    new_gen, lp->rl_main_id, *gen);
512 
513 	if (r != REP_PROTOCOL_SUCCESS) {
514 		backend_tx_rollback(tx);
515 		goto end;
516 	}
517 
518 	q = backend_query_alloc();
519 
520 	backend_query_add(q,
521 	    "SELECT lnk_prop_name, lnk_prop_type, lnk_val_id "
522 	    "FROM prop_lnk_tbl "
523 	    "WHERE (lnk_pg_id = %d AND lnk_gen_id = %d)",
524 	    lp->rl_main_id, *gen);
525 
526 	data->txc_inserts = backend_query_alloc();
527 	r = backend_tx_run(tx, q, tx_process_property, data);
528 	backend_query_free(q);
529 
530 	if (r == REP_PROTOCOL_DONE)
531 		r = REP_PROTOCOL_FAIL_UNKNOWN;		/* corruption */
532 
533 	if (r != REP_PROTOCOL_SUCCESS ||
534 	    (r = data->txc_result) != REP_PROTOCOL_SUCCESS) {
535 		backend_query_free(data->txc_inserts);
536 		backend_tx_rollback(tx);
537 		goto end;
538 	}
539 
540 	r = backend_tx_run(tx, data->txc_inserts, NULL, NULL);
541 	backend_query_free(data->txc_inserts);
542 
543 	if (r != REP_PROTOCOL_SUCCESS) {
544 		backend_tx_rollback(tx);
545 		goto end;
546 	}
547 
548 	r = tx_process_cmds(data);
549 	if (r != REP_PROTOCOL_SUCCESS) {
550 		backend_tx_rollback(tx);
551 		goto end;
552 	}
553 	r = backend_tx_commit(tx);
554 
555 	if (r == REP_PROTOCOL_SUCCESS)
556 		*gen = new_gen;
557 end:
558 	return (r);
559 }
560