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