]> git.proxmox.com Git - mirror_ovs.git/blame - ovsdb/transaction.c
stream: Allow timeout configuration for open_block.
[mirror_ovs.git] / ovsdb / transaction.c
CommitLineData
120fb2ca 1/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017 Nicira, Inc.
f85f8ebb
BP
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <config.h>
17
18#include "transaction.h"
19
17d18afb 20#include "bitmap.h"
3e8a2ad1 21#include "openvswitch/dynamic-string.h"
120fb2ca 22#include "file.h"
f85f8ebb 23#include "hash.h"
009bf21f 24#include "monitor.h"
ee89ea7b
TW
25#include "openvswitch/hmap.h"
26#include "openvswitch/json.h"
b19bab5b 27#include "openvswitch/list.h"
1b1d2e6d
BP
28#include "openvswitch/poll-loop.h"
29#include "openvswitch/vlog.h"
f85f8ebb
BP
30#include "ovsdb-error.h"
31#include "ovsdb.h"
32#include "row.h"
1b1d2e6d 33#include "storage.h"
f85f8ebb
BP
34#include "table.h"
35#include "uuid.h"
36
1b1d2e6d
BP
37VLOG_DEFINE_THIS_MODULE(transaction);
38
f85f8ebb
BP
39struct ovsdb_txn {
40 struct ovsdb *db;
ca6ba700 41 struct ovs_list txn_tables; /* Contains "struct ovsdb_txn_table"s. */
d171b584 42 struct ds comment;
f85f8ebb
BP
43};
44
45/* A table modified by a transaction. */
46struct ovsdb_txn_table {
ca6ba700 47 struct ovs_list node; /* Element in ovsdb_txn's txn_tables list. */
f85f8ebb
BP
48 struct ovsdb_table *table;
49 struct hmap txn_rows; /* Contains "struct ovsdb_txn_row"s. */
3e010b7a 50
6910a6e6
BP
51 /* This has the same form as the 'indexes' member of struct ovsdb_table,
52 * but it is only used or updated at transaction commit time, from
53 * check_index_uniqueness(). */
54 struct hmap *txn_indexes;
55
3e010b7a
BP
56 /* Used by for_each_txn_row(). */
57 unsigned int serial; /* Serial number of in-progress iteration. */
58 unsigned int n_processed; /* Number of rows processed. */
f85f8ebb
BP
59};
60
61/* A row modified by the transaction:
62 *
63 * - A row added by a transaction will have null 'old' and non-null 'new'.
64 *
65 * - A row deleted by a transaction will have non-null 'old' and null
66 * 'new'.
67 *
68 * - A row modified by a transaction will have non-null 'old' and 'new'.
69 *
a8c37dc5
BP
70 * - 'old' and 'new' both null indicates that a row was added then deleted
71 * within a single transaction. Most of the time we instead delete the
72 * ovsdb_txn_row entirely, but inside a for_each_txn_row() callback
73 * there are restrictions that sometimes mean we have to leave the
74 * ovsdb_txn_row in place.
f85f8ebb
BP
75 */
76struct ovsdb_txn_row {
77 struct hmap_node hmap_node; /* In ovsdb_txn_table's txn_rows hmap. */
78 struct ovsdb_row *old; /* The old row. */
79 struct ovsdb_row *new; /* The new row. */
c7d85e0d 80 size_t n_refs; /* Number of remaining references. */
3e010b7a 81
a8c37dc5
BP
82 /* These members are the same as the corresponding members of 'old' or
83 * 'new'. They are present here for convenience and because occasionally
84 * there can be an ovsdb_txn_row where both 'old' and 'new' are NULL. */
85 struct uuid uuid;
86 struct ovsdb_table *table;
87
3e010b7a
BP
88 /* Used by for_each_txn_row(). */
89 unsigned int serial; /* Serial number of in-progress commit. */
17d18afb
BP
90
91 unsigned long changed[]; /* Bits set to 1 for columns that changed. */
f85f8ebb
BP
92};
93
cab50449 94static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
c5f341ab 95delete_garbage_row(struct ovsdb_txn *txn, struct ovsdb_txn_row *r);
3e010b7a 96static void ovsdb_txn_row_prefree(struct ovsdb_txn_row *);
cab50449 97static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
3e010b7a
BP
98for_each_txn_row(struct ovsdb_txn *txn,
99 struct ovsdb_error *(*)(struct ovsdb_txn *,
100 struct ovsdb_txn_row *));
101
102/* Used by for_each_txn_row() to track tables and rows that have been
103 * processed. */
104static unsigned int serial;
105
f85f8ebb
BP
106struct ovsdb_txn *
107ovsdb_txn_create(struct ovsdb *db)
108{
109 struct ovsdb_txn *txn = xmalloc(sizeof *txn);
110 txn->db = db;
417e7e66 111 ovs_list_init(&txn->txn_tables);
d171b584 112 ds_init(&txn->comment);
f85f8ebb
BP
113 return txn;
114}
115
116static void
3e010b7a 117ovsdb_txn_free(struct ovsdb_txn *txn)
f85f8ebb 118{
417e7e66 119 ovs_assert(ovs_list_is_empty(&txn->txn_tables));
d171b584 120 ds_destroy(&txn->comment);
f85f8ebb
BP
121 free(txn);
122}
123
17d18afb 124static struct ovsdb_error *
3e010b7a
BP
125ovsdb_txn_row_abort(struct ovsdb_txn *txn OVS_UNUSED,
126 struct ovsdb_txn_row *txn_row)
f85f8ebb
BP
127{
128 struct ovsdb_row *old = txn_row->old;
129 struct ovsdb_row *new = txn_row->new;
130
3e010b7a 131 ovsdb_txn_row_prefree(txn_row);
f85f8ebb 132 if (!old) {
a8c37dc5
BP
133 if (new) {
134 hmap_remove(&new->table->rows, &new->hmap_node);
135 }
f85f8ebb
BP
136 } else if (!new) {
137 hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
138 } else {
139 hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
140 }
141 ovsdb_row_destroy(new);
3e010b7a
BP
142 free(txn_row);
143
144 return NULL;
f85f8ebb
BP
145}
146
6910a6e6
BP
147/* Returns the offset in bytes from the start of an ovsdb_row for 'table' to
148 * the hmap_node for the index numbered 'i'. */
149static size_t
150ovsdb_row_index_offset__(const struct ovsdb_table *table, size_t i)
151{
152 size_t n_fields = shash_count(&table->schema->columns);
153 return (offsetof(struct ovsdb_row, fields)
154 + n_fields * sizeof(struct ovsdb_datum)
155 + i * sizeof(struct hmap_node));
156}
157
158/* Returns the hmap_node in 'row' for the index numbered 'i'. */
159static struct hmap_node *
160ovsdb_row_get_index_node(struct ovsdb_row *row, size_t i)
161{
162 return (void *) ((char *) row + ovsdb_row_index_offset__(row->table, i));
163}
164
165/* Returns the ovsdb_row given 'index_node', which is a pointer to that row's
166 * hmap_node for the index numbered 'i' within 'table'. */
167static struct ovsdb_row *
168ovsdb_row_from_index_node(struct hmap_node *index_node,
169 const struct ovsdb_table *table, size_t i)
170{
171 return (void *) ((char *) index_node - ovsdb_row_index_offset__(table, i));
172}
173
f85f8ebb
BP
174void
175ovsdb_txn_abort(struct ovsdb_txn *txn)
176{
3e010b7a
BP
177 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_abort));
178 ovsdb_txn_free(txn);
f85f8ebb
BP
179}
180
0d0f05b9
BP
181static struct ovsdb_txn_row *
182find_txn_row(const struct ovsdb_table *table, const struct uuid *uuid)
183{
184 struct ovsdb_txn_row *txn_row;
185
186 if (!table->txn_table) {
187 return NULL;
188 }
189
4e8e4213 190 HMAP_FOR_EACH_WITH_HASH (txn_row, hmap_node,
0d0f05b9 191 uuid_hash(uuid), &table->txn_table->txn_rows) {
a8c37dc5 192 if (uuid_equals(uuid, &txn_row->uuid)) {
0d0f05b9
BP
193 return txn_row;
194 }
195 }
196
197 return NULL;
198}
199
c5f341ab
BP
200static struct ovsdb_txn_row *
201find_or_make_txn_row(struct ovsdb_txn *txn, const struct ovsdb_table *table,
202 const struct uuid *uuid)
203{
204 struct ovsdb_txn_row *txn_row = find_txn_row(table, uuid);
205 if (!txn_row) {
206 const struct ovsdb_row *row = ovsdb_table_get_row(table, uuid);
207 if (row) {
208 txn_row = ovsdb_txn_row_modify(txn, row)->txn_row;
209 }
210 }
211 return txn_row;
212}
213
cab50449 214static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
97f7803b
BP
215ovsdb_txn_adjust_atom_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
216 const struct ovsdb_column *c,
c7d85e0d
BP
217 const struct ovsdb_base_type *base,
218 const union ovsdb_atom *atoms, unsigned int n,
219 int delta)
0d0f05b9 220{
c7d85e0d 221 const struct ovsdb_table *table;
0d0f05b9
BP
222 unsigned int i;
223
7360012b 224 if (!ovsdb_base_type_is_strong_ref(base)) {
c7d85e0d
BP
225 return NULL;
226 }
227
fa37affa 228 table = base->uuid.refTable;
0d0f05b9
BP
229 for (i = 0; i < n; i++) {
230 const struct uuid *uuid = &atoms[i].uuid;
c5f341ab
BP
231 struct ovsdb_txn_row *txn_row;
232
7f90cb0e
BP
233 if (uuid_equals(uuid, ovsdb_row_get_uuid(r))) {
234 /* Self-references don't count. */
235 continue;
236 }
c5f341ab
BP
237
238 txn_row = find_or_make_txn_row(txn, table, uuid);
c7d85e0d 239 if (!txn_row) {
c5f341ab
BP
240 return ovsdb_error("referential integrity violation",
241 "Table %s column %s row "UUID_FMT" "
242 "references nonexistent row "UUID_FMT" in "
243 "table %s.",
244 r->table->schema->name, c->name,
245 UUID_ARGS(ovsdb_row_get_uuid(r)),
246 UUID_ARGS(uuid), table->schema->name);
0d0f05b9 247 }
c7d85e0d 248 txn_row->n_refs += delta;
0d0f05b9 249 }
c7d85e0d
BP
250
251 return NULL;
0d0f05b9
BP
252}
253
cab50449 254static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
c7d85e0d
BP
255ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
256 const struct ovsdb_column *column, int delta)
0d0f05b9
BP
257{
258 const struct ovsdb_datum *field = &r->fields[column->index];
c7d85e0d 259 struct ovsdb_error *error;
0d0f05b9 260
97f7803b 261 error = ovsdb_txn_adjust_atom_refs(txn, r, column, &column->type.key,
c7d85e0d
BP
262 field->keys, field->n, delta);
263 if (!error) {
97f7803b 264 error = ovsdb_txn_adjust_atom_refs(txn, r, column, &column->type.value,
c7d85e0d 265 field->values, field->n, delta);
0d0f05b9 266 }
c7d85e0d 267 return error;
0d0f05b9
BP
268}
269
cab50449 270static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
c7d85e0d 271update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
0d0f05b9 272{
a8c37dc5 273 struct ovsdb_table *table = r->table;
c7d85e0d
BP
274 struct shash_node *node;
275
276 SHASH_FOR_EACH (node, &table->schema->columns) {
277 const struct ovsdb_column *column = node->data;
278 struct ovsdb_error *error;
279
4f94601a
RBE
280 if (bitmap_is_set(r->changed, column->index)) {
281 if (r->old) {
282 error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
283 if (error) {
284 return OVSDB_WRAP_BUG("error decreasing refcount", error);
285 }
c7d85e0d 286 }
4f94601a
RBE
287 if (r->new) {
288 error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
289 if (error) {
290 return error;
291 }
0d0f05b9
BP
292 }
293 }
294 }
0d0f05b9 295
c7d85e0d 296 return NULL;
0d0f05b9
BP
297}
298
cab50449 299static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
c7d85e0d 300check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
0d0f05b9 301{
c7d85e0d
BP
302 if (r->new || !r->n_refs) {
303 return NULL;
304 } else {
305 return ovsdb_error("referential integrity violation",
306 "cannot delete %s row "UUID_FMT" because "
34582733 307 "of %"PRIuSIZE" remaining reference(s)",
a8c37dc5 308 r->table->schema->name, UUID_ARGS(&r->uuid),
c7d85e0d 309 r->n_refs);
0d0f05b9 310 }
0d0f05b9
BP
311}
312
cab50449 313static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
c5f341ab
BP
314delete_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *row,
315 const struct ovsdb_base_type *base,
316 const union ovsdb_atom *atoms, unsigned int n)
317{
318 const struct ovsdb_table *table;
319 unsigned int i;
320
321 if (!ovsdb_base_type_is_strong_ref(base)) {
322 return NULL;
323 }
324
fa37affa 325 table = base->uuid.refTable;
c5f341ab
BP
326 for (i = 0; i < n; i++) {
327 const struct uuid *uuid = &atoms[i].uuid;
328 struct ovsdb_txn_row *txn_row;
329
330 if (uuid_equals(uuid, ovsdb_row_get_uuid(row))) {
331 /* Self-references don't count. */
332 continue;
333 }
334
335 txn_row = find_or_make_txn_row(txn, table, uuid);
336 if (!txn_row) {
337 return OVSDB_BUG("strong ref target missing");
338 } else if (!txn_row->n_refs) {
339 return OVSDB_BUG("strong ref target has zero n_refs");
340 } else if (!txn_row->new) {
341 return OVSDB_BUG("deleted strong ref target");
342 }
343
344 if (--txn_row->n_refs == 0) {
345 struct ovsdb_error *error = delete_garbage_row(txn, txn_row);
346 if (error) {
347 return error;
348 }
349 }
350 }
351
352 return NULL;
353}
354
cab50449 355static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
c5f341ab
BP
356delete_garbage_row(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
357{
358 struct shash_node *node;
359 struct ovsdb_row *row;
360
361 if (txn_row->table->schema->is_root) {
362 return NULL;
363 }
364
365 row = txn_row->new;
366 txn_row->new = NULL;
367 hmap_remove(&txn_row->table->rows, &row->hmap_node);
368 SHASH_FOR_EACH (node, &txn_row->table->schema->columns) {
369 const struct ovsdb_column *column = node->data;
370 const struct ovsdb_datum *field = &row->fields[column->index];
371 struct ovsdb_error *error;
372
373 error = delete_row_refs(txn, row,
374 &column->type.key, field->keys, field->n);
375 if (error) {
376 return error;
377 }
378
379 error = delete_row_refs(txn, row,
380 &column->type.value, field->values, field->n);
381 if (error) {
382 return error;
383 }
384 }
385 ovsdb_row_destroy(row);
386
387 return NULL;
388}
389
cab50449 390static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
c5f341ab
BP
391collect_garbage(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
392{
393 if (txn_row->new && !txn_row->n_refs) {
394 return delete_garbage_row(txn, txn_row);
395 }
396 return NULL;
397}
398
cab50449 399static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
0d0f05b9
BP
400update_ref_counts(struct ovsdb_txn *txn)
401{
402 struct ovsdb_error *error;
0d0f05b9 403
c7d85e0d 404 error = for_each_txn_row(txn, update_row_ref_count);
0d0f05b9
BP
405 if (error) {
406 return error;
407 }
408
c7d85e0d 409 return for_each_txn_row(txn, check_ref_count);
0d0f05b9
BP
410}
411
17d18afb 412static struct ovsdb_error *
3e010b7a
BP
413ovsdb_txn_row_commit(struct ovsdb_txn *txn OVS_UNUSED,
414 struct ovsdb_txn_row *txn_row)
415{
6910a6e6
BP
416 size_t n_indexes = txn_row->table->schema->n_indexes;
417
418 if (txn_row->old) {
419 size_t i;
420
421 for (i = 0; i < n_indexes; i++) {
422 struct hmap_node *node = ovsdb_row_get_index_node(txn_row->old, i);
423 hmap_remove(&txn_row->table->indexes[i], node);
424 }
425 }
426 if (txn_row->new) {
427 size_t i;
428
429 for (i = 0; i < n_indexes; i++) {
430 struct hmap_node *node = ovsdb_row_get_index_node(txn_row->new, i);
431 hmap_insert(&txn_row->table->indexes[i], node, node->hash);
432 }
433 }
434
3e010b7a 435 ovsdb_txn_row_prefree(txn_row);
c7d85e0d
BP
436 if (txn_row->new) {
437 txn_row->new->n_refs = txn_row->n_refs;
438 }
3e010b7a
BP
439 ovsdb_row_destroy(txn_row->old);
440 free(txn_row);
441
442 return NULL;
443}
444
aa1fc801
RBE
445static struct ovsdb_error *
446ovsdb_txn_update_weak_refs(struct ovsdb_txn *txn OVS_UNUSED,
447 struct ovsdb_txn_row *txn_row)
448{
449 struct ovsdb_weak_ref *weak, *next;
450
451 /* Remove the weak references originating in the old version of the row. */
452 if (txn_row->old) {
453 LIST_FOR_EACH_SAFE (weak, next, src_node, &txn_row->old->src_refs) {
454 ovs_list_remove(&weak->src_node);
455 ovs_list_remove(&weak->dst_node);
456 free(weak);
457 }
458 }
459
460 /* Although the originating rows have the responsibility of updating the
461 * weak references in the dst, it is possible that some source rows aren't
462 * part of the transaction. In that situation this row needs to move the
463 * list of incoming weak references from the old row into the new one.
464 */
465 if (txn_row->old && txn_row->new) {
466 /* Move the incoming weak references from old to new. */
467 ovs_list_push_back_all(&txn_row->new->dst_refs,
468 &txn_row->old->dst_refs);
469 }
470
471 /* Insert the weak references originating in the new version of the row. */
472 struct ovsdb_row *dst_row;
473 if (txn_row->new) {
474 LIST_FOR_EACH (weak, src_node, &txn_row->new->src_refs) {
475 /* dst_row MUST exist. */
476 dst_row = CONST_CAST(struct ovsdb_row *,
477 ovsdb_table_get_row(weak->dst_table, &weak->dst));
478 ovs_list_insert(&dst_row->dst_refs, &weak->dst_node);
479 }
480 }
481
482 return NULL;
483}
484
7360012b 485static void
aa1fc801 486add_weak_ref(const struct ovsdb_row *src_, const struct ovsdb_row *dst_)
7360012b 487{
ebc56baa
BP
488 struct ovsdb_row *src = CONST_CAST(struct ovsdb_row *, src_);
489 struct ovsdb_row *dst = CONST_CAST(struct ovsdb_row *, dst_);
7360012b
BP
490 struct ovsdb_weak_ref *weak;
491
492 if (src == dst) {
493 return;
494 }
495
417e7e66 496 if (!ovs_list_is_empty(&dst->dst_refs)) {
7360012b 497 /* Omit duplicates. */
417e7e66 498 weak = CONTAINER_OF(ovs_list_back(&dst->dst_refs),
7360012b
BP
499 struct ovsdb_weak_ref, dst_node);
500 if (weak->src == src) {
501 return;
502 }
503 }
504
505 weak = xmalloc(sizeof *weak);
506 weak->src = src;
aa1fc801
RBE
507 weak->dst_table = dst->table;
508 weak->dst = *ovsdb_row_get_uuid(dst);
509 /* The dst_refs list is updated at commit time. */
510 ovs_list_init(&weak->dst_node);
417e7e66 511 ovs_list_push_back(&src->src_refs, &weak->src_node);
7360012b
BP
512}
513
cab50449 514static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
7360012b
BP
515assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
516{
517 struct ovsdb_table *table;
518 struct shash_node *node;
519
aa1fc801 520 if (txn_row->old && !txn_row->new) {
7360012b
BP
521 /* Mark rows that have weak references to 'txn_row' as modified, so
522 * that their weak references will get reassessed. */
523 struct ovsdb_weak_ref *weak, *next;
524
4e8e4213 525 LIST_FOR_EACH_SAFE (weak, next, dst_node, &txn_row->old->dst_refs) {
7360012b
BP
526 if (!weak->src->txn_row) {
527 ovsdb_txn_row_modify(txn, weak->src);
528 }
529 }
530 }
531
532 if (!txn_row->new) {
533 /* We don't have to do anything about references that originate at
534 * 'txn_row', because ovsdb_row_destroy() will remove those weak
535 * references. */
536 return NULL;
537 }
538
a8c37dc5 539 table = txn_row->table;
7360012b
BP
540 SHASH_FOR_EACH (node, &table->schema->columns) {
541 const struct ovsdb_column *column = node->data;
542 struct ovsdb_datum *datum = &txn_row->new->fields[column->index];
543 unsigned int orig_n, i;
544 bool zero = false;
545
546 orig_n = datum->n;
547
548 if (ovsdb_base_type_is_weak_ref(&column->type.key)) {
549 for (i = 0; i < datum->n; ) {
550 const struct ovsdb_row *row;
551
fa37affa 552 row = ovsdb_table_get_row(column->type.key.uuid.refTable,
7360012b
BP
553 &datum->keys[i].uuid);
554 if (row) {
aa1fc801 555 add_weak_ref(txn_row->new, row);
7360012b
BP
556 i++;
557 } else {
558 if (uuid_is_zero(&datum->keys[i].uuid)) {
559 zero = true;
560 }
561 ovsdb_datum_remove_unsafe(datum, i, &column->type);
562 }
563 }
564 }
565
566 if (ovsdb_base_type_is_weak_ref(&column->type.value)) {
567 for (i = 0; i < datum->n; ) {
568 const struct ovsdb_row *row;
569
fa37affa 570 row = ovsdb_table_get_row(column->type.value.uuid.refTable,
7360012b
BP
571 &datum->values[i].uuid);
572 if (row) {
aa1fc801 573 add_weak_ref(txn_row->new, row);
7360012b
BP
574 i++;
575 } else {
576 if (uuid_is_zero(&datum->values[i].uuid)) {
577 zero = true;
578 }
579 ovsdb_datum_remove_unsafe(datum, i, &column->type);
580 }
581 }
582 }
583
584 if (datum->n != orig_n) {
585 bitmap_set1(txn_row->changed, column->index);
586 ovsdb_datum_sort_assert(datum, column->type.key.type);
587 if (datum->n < column->type.n_min) {
588 const struct uuid *row_uuid = ovsdb_row_get_uuid(txn_row->new);
589 if (zero && !txn_row->old) {
590 return ovsdb_error(
591 "constraint violation",
592 "Weak reference column \"%s\" in \"%s\" row "UUID_FMT
593 " (inserted within this transaction) contained "
594 "all-zeros UUID (probably as the default value for "
595 "this column) but deleting this value caused a "
596 "constraint volation because this column is not "
597 "allowed to be empty.", column->name,
598 table->schema->name, UUID_ARGS(row_uuid));
599 } else {
600 return ovsdb_error(
601 "constraint violation",
602 "Deletion of %u weak reference(s) to deleted (or "
603 "never-existing) rows from column \"%s\" in \"%s\" "
604 "row "UUID_FMT" %scaused this column to become empty, "
605 "but constraints on this column disallow an "
606 "empty column.",
607 orig_n - datum->n, column->name, table->schema->name,
608 UUID_ARGS(row_uuid),
609 (txn_row->old
610 ? ""
611 : "(inserted within this transaction) "));
612 }
613 }
614 }
615 }
616
617 return NULL;
618}
619
cab50449 620static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
17d18afb
BP
621determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
622{
a8c37dc5 623 struct ovsdb_table *table = txn_row->table;
17d18afb 624
17d18afb
BP
625 if (txn_row->old && txn_row->new) {
626 struct shash_node *node;
627 bool changed = false;
628
629 SHASH_FOR_EACH (node, &table->schema->columns) {
630 const struct ovsdb_column *column = node->data;
631 const struct ovsdb_type *type = &column->type;
632 unsigned int idx = column->index;
633
634 if (!ovsdb_datum_equals(&txn_row->old->fields[idx],
635 &txn_row->new->fields[idx],
636 type)) {
637 bitmap_set1(txn_row->changed, idx);
638 changed = true;
639 }
640 }
641
642 if (!changed) {
643 /* Nothing actually changed in this row, so drop it. */
644 ovsdb_txn_row_abort(txn, txn_row);
645 }
646 } else {
647 bitmap_set_multiple(txn_row->changed, 0,
648 shash_count(&table->schema->columns), 1);
649 }
650
651 return NULL;
652}
653
cab50449 654static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
87ab878c
BP
655check_max_rows(struct ovsdb_txn *txn)
656{
657 struct ovsdb_txn_table *t;
658
4e8e4213 659 LIST_FOR_EACH (t, node, &txn->txn_tables) {
87ab878c
BP
660 size_t n_rows = hmap_count(&t->table->rows);
661 unsigned int max_rows = t->table->schema->max_rows;
662
663 if (n_rows > max_rows) {
664 return ovsdb_error("constraint violation",
665 "transaction causes \"%s\" table to contain "
34582733 666 "%"PRIuSIZE" rows, greater than the schema-defined "
87ab878c
BP
667 "limit of %u row(s)",
668 t->table->schema->name, n_rows, max_rows);
669 }
670 }
671
672 return NULL;
673}
674
6910a6e6
BP
675static struct ovsdb_row *
676ovsdb_index_search(struct hmap *index, struct ovsdb_row *row, size_t i,
677 uint32_t hash)
678{
679 const struct ovsdb_table *table = row->table;
680 const struct ovsdb_column_set *columns = &table->schema->indexes[i];
681 struct hmap_node *node;
682
683 for (node = hmap_first_with_hash(index, hash); node;
684 node = hmap_next_with_hash(node)) {
685 struct ovsdb_row *irow = ovsdb_row_from_index_node(node, table, i);
686 if (ovsdb_row_equal_columns(row, irow, columns)) {
687 return irow;
688 }
689 }
690
691 return NULL;
692}
693
694static void
695duplicate_index_row__(const struct ovsdb_column_set *index,
696 const struct ovsdb_row *row,
697 const char *title,
698 struct ds *out)
699{
700 size_t n_columns = shash_count(&row->table->schema->columns);
701
702 ds_put_format(out, "%s row, with UUID "UUID_FMT", ",
703 title, UUID_ARGS(ovsdb_row_get_uuid(row)));
704 if (!row->txn_row
b71273f6 705 || bitmap_scan(row->txn_row->changed, 1, 0, n_columns) == n_columns) {
6910a6e6
BP
706 ds_put_cstr(out, "existed in the database before this "
707 "transaction and was not modified by the transaction.");
708 } else if (!row->txn_row->old) {
709 ds_put_cstr(out, "was inserted by this transaction.");
710 } else if (ovsdb_row_equal_columns(row->txn_row->old,
711 row->txn_row->new, index)) {
712 ds_put_cstr(out, "existed in the database before this "
713 "transaction, which modified some of the row's columns "
714 "but not any columns in this index.");
715 } else {
716 ds_put_cstr(out, "had the following index values before the "
717 "transaction: ");
718 ovsdb_row_columns_to_string(row->txn_row->old, index, out);
719 ds_put_char(out, '.');
720 }
721}
722
cab50449 723static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
6910a6e6
BP
724duplicate_index_row(const struct ovsdb_column_set *index,
725 const struct ovsdb_row *a,
726 const struct ovsdb_row *b)
727{
728 struct ovsdb_column_set all_columns;
729 struct ovsdb_error *error;
730 char *index_s;
731 struct ds s;
732
733 /* Put 'a' and 'b' in a predictable order to make error messages
734 * reproducible for testing. */
735 ovsdb_column_set_init(&all_columns);
736 ovsdb_column_set_add_all(&all_columns, a->table);
737 if (ovsdb_row_compare_columns_3way(a, b, &all_columns) < 0) {
738 const struct ovsdb_row *tmp = a;
739 a = b;
740 b = tmp;
741 }
742 ovsdb_column_set_destroy(&all_columns);
743
744 index_s = ovsdb_column_set_to_string(index);
745
746 ds_init(&s);
747 ds_put_format(&s, "Transaction causes multiple rows in \"%s\" table to "
748 "have identical values (", a->table->schema->name);
749 ovsdb_row_columns_to_string(a, index, &s);
750 ds_put_format(&s, ") for index on %s. ", index_s);
751 duplicate_index_row__(index, a, "First", &s);
752 ds_put_cstr(&s, " ");
753 duplicate_index_row__(index, b, "Second", &s);
754
755 free(index_s);
756
757 error = ovsdb_error("constraint violation", "%s", ds_cstr(&s));
758 ds_destroy(&s);
759 return error;
760}
761
cab50449 762static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
6910a6e6
BP
763check_index_uniqueness(struct ovsdb_txn *txn OVS_UNUSED,
764 struct ovsdb_txn_row *txn_row)
765{
3b3bad07 766 /* Skip rows that are being deleted since they can't violate uniqueness. */
6910a6e6 767 struct ovsdb_row *row = txn_row->new;
6910a6e6
BP
768 if (!row) {
769 return NULL;
770 }
771
3b3bad07
BP
772 struct ovsdb_txn_table *txn_table = txn_row->table->txn_table;
773 struct ovsdb_table *table = txn_row->table;
774 for (size_t i = 0; i < table->schema->n_indexes; i++) {
6910a6e6 775 const struct ovsdb_column_set *index = &table->schema->indexes[i];
3b3bad07
BP
776 uint32_t hash = ovsdb_row_hash_columns(row, index, 0);
777
778 /* Check whether the row has a match in the temporary hash table that
779 * we're building. If we add two rows with the same index data, then
780 * there's a duplicate within the rows added or modified in this
781 * transaction.*/
782 struct ovsdb_row *irow
783 = ovsdb_index_search(&txn_table->txn_indexes[i], row, i, hash);
6910a6e6
BP
784 if (irow) {
785 return duplicate_index_row(index, irow, row);
786 }
787
3b3bad07
BP
788 /* Also check whether the row has a match in the table's real index
789 * (which won't be updated until transaction commit is certain). If
790 * there's a match, and it's for a row that wasn't pulled into the
791 * transaction, then it's a duplicate. (If it is for a row that is
792 * part of the transaction, then the first check has already handled
793 * it.) */
6910a6e6
BP
794 irow = ovsdb_index_search(&table->indexes[i], row, i, hash);
795 if (irow && !irow->txn_row) {
796 return duplicate_index_row(index, irow, row);
797 }
798
3b3bad07 799 /* Add row to temporary hash table. */
6910a6e6
BP
800 hmap_insert(&txn_table->txn_indexes[i],
801 ovsdb_row_get_index_node(row, i), hash);
802 }
803
804 return NULL;
805}
806
eac0dc83
BP
807static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
808update_version(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *txn_row)
809{
810 struct ovsdb_table *table = txn_row->table;
811 size_t n_columns = shash_count(&table->schema->columns);
812
813 if (txn_row->old && txn_row->new
814 && !bitmap_is_all_zeros(txn_row->changed, n_columns)) {
815 bitmap_set1(txn_row->changed, OVSDB_COL_VERSION);
816 uuid_generate(ovsdb_row_get_version_rw(txn_row->new));
817 }
818
819 return NULL;
820}
821
53178986
BP
822static bool
823ovsdb_txn_is_empty(const struct ovsdb_txn *txn)
824{
825 return ovs_list_is_empty(&txn->txn_tables);
826}
827
1b1d2e6d
BP
828static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
829ovsdb_txn_precommit(struct ovsdb_txn *txn)
f85f8ebb 830{
bd06962a 831 struct ovsdb_error *error;
f85f8ebb 832
17d18afb
BP
833 /* Figure out what actually changed, and abort early if the transaction
834 * was really a no-op. */
835 error = for_each_txn_row(txn, determine_changes);
836 if (error) {
1e8c7cae 837 ovsdb_txn_abort(txn);
1dd5b71d 838 return OVSDB_WRAP_BUG("can't happen", error);
17d18afb 839 }
1b1d2e6d 840 if (ovs_list_is_empty(&txn->txn_tables)) {
17d18afb
BP
841 return NULL;
842 }
843
c5f341ab
BP
844 /* Update reference counts and check referential integrity. */
845 error = update_ref_counts(txn);
87ab878c 846 if (error) {
87ab878c
BP
847 return error;
848 }
849
c5f341ab
BP
850 /* Delete unreferenced, non-root rows. */
851 error = for_each_txn_row(txn, collect_garbage);
852 if (error) {
c5f341ab
BP
853 return OVSDB_WRAP_BUG("can't happen", error);
854 }
855
856 /* Check maximum rows table constraints. */
857 error = check_max_rows(txn);
0d0f05b9 858 if (error) {
0d0f05b9
BP
859 return error;
860 }
861
6910a6e6 862 /* Check reference counts and remove bad references for "weak" referential
7360012b
BP
863 * integrity. */
864 error = for_each_txn_row(txn, assess_weak_refs);
865 if (error) {
7360012b
BP
866 return error;
867 }
868
6910a6e6
BP
869 /* Verify that the indexes will still be unique post-transaction. */
870 error = for_each_txn_row(txn, check_index_uniqueness);
871 if (error) {
6910a6e6
BP
872 return error;
873 }
874
eac0dc83
BP
875 /* Update _version for rows that changed. */
876 error = for_each_txn_row(txn, update_version);
877 if (error) {
878 return OVSDB_WRAP_BUG("can't happen", error);
879 }
880
1b1d2e6d 881 return error;
53178986
BP
882}
883
1b1d2e6d
BP
884/* Finalize commit. */
885void
886ovsdb_txn_complete(struct ovsdb_txn *txn)
53178986 887{
1b1d2e6d
BP
888 if (!ovsdb_txn_is_empty(txn)) {
889 txn->db->run_triggers = true;
890 ovsdb_monitors_commit(txn->db, txn);
891 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_update_weak_refs));
892 ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
893 }
894 ovsdb_txn_free(txn);
895}
896
897/* Applies 'txn' to the internal representation of the database. This is for
898 * transactions that don't need to be written to storage; probably, they came
899 * from storage. These transactions shouldn't ordinarily fail because storage
900 * should contain only consistent transactions. (One exception is for database
901 * conversion in ovsdb_convert().) */
902struct ovsdb_error * OVS_WARN_UNUSED_RESULT
903ovsdb_txn_replay_commit(struct ovsdb_txn *txn)
904{
905 struct ovsdb_error *error = ovsdb_txn_precommit(txn);
906 if (error) {
907 ovsdb_txn_abort(txn);
908 } else {
909 ovsdb_txn_complete(txn);
910 }
911 return error;
912}
913
914/* If 'error' is nonnull, the transaction is complete, with the given error as
915 * the result.
916 *
917 * Otherwise, if 'write' is nonnull, then the transaction is waiting for
918 * 'write' to complete.
919 *
920 * Otherwise, if 'commit_index' is nonzero, then the transaction is waiting for
921 * 'commit_index' to be applied to the storage.
922 *
923 * Otherwise, the transaction is complete and successful. */
924struct ovsdb_txn_progress {
925 struct ovsdb_error *error;
926 struct ovsdb_write *write;
927 uint64_t commit_index;
928
929 struct ovsdb_storage *storage;
930};
931
932struct ovsdb_txn_progress *
933ovsdb_txn_propose_schema_change(struct ovsdb *db,
934 const struct json *schema,
935 const struct json *data)
936{
937 struct ovsdb_txn_progress *progress = xzalloc(sizeof *progress);
938 progress->storage = db->storage;
939
940 struct uuid next;
941 struct ovsdb_write *write = ovsdb_storage_write_schema_change(
942 db->storage, schema, data, &db->prereq, &next);
943 if (!ovsdb_write_is_complete(write)) {
944 progress->write = write;
945 } else {
946 progress->error = ovsdb_error_clone(ovsdb_write_get_error(write));
947 ovsdb_write_destroy(write);
948 }
949 return progress;
950}
951
952struct ovsdb_txn_progress *
953ovsdb_txn_propose_commit(struct ovsdb_txn *txn, bool durable)
954{
955 struct ovsdb_txn_progress *progress = xzalloc(sizeof *progress);
956 progress->storage = txn->db->storage;
957 progress->error = ovsdb_txn_precommit(txn);
958 if (progress->error) {
959 return progress;
960 }
961
962 /* Turn the commit into the format used for the storage logs.. */
963 struct json *txn_json = ovsdb_file_txn_to_json(txn);
964 if (!txn_json) {
965 /* Nothing to do, so success. */
966 return progress;
967 }
968 txn_json = ovsdb_file_txn_annotate(txn_json, ovsdb_txn_get_comment(txn));
969
970 struct uuid next;
971 struct ovsdb_write *write = ovsdb_storage_write(
972 txn->db->storage, txn_json, &txn->db->prereq, &next, durable);
973 json_destroy(txn_json);
974 if (!ovsdb_write_is_complete(write)) {
975 progress->write = write;
976 } else {
977 progress->error = ovsdb_error_clone(ovsdb_write_get_error(write));
978 ovsdb_write_destroy(write);
979 }
980 return progress;
981}
982
983/* Proposes 'txn' for commitment and then waits for the commit to succeed or
984 * fail. Returns null if successful, otherwise the error.
985 *
986 * **In addition**, this function also completes or aborts the transaction if
987 * the transaction succeeded or failed, respectively. */
988struct ovsdb_error * OVS_WARN_UNUSED_RESULT
989ovsdb_txn_propose_commit_block(struct ovsdb_txn *txn, bool durable)
990{
991 struct ovsdb_txn_progress *p = ovsdb_txn_propose_commit(txn, durable);
992 for (;;) {
993 ovsdb_storage_run(p->storage);
994 if (ovsdb_txn_progress_is_complete(p)) {
995 struct ovsdb_error *error
996 = ovsdb_error_clone(ovsdb_txn_progress_get_error(p));
997 ovsdb_txn_progress_destroy(p);
998
999 if (error) {
1000 ovsdb_txn_abort(txn);
1001 } else {
1002 ovsdb_txn_complete(txn);
1003 }
1004
f85f8ebb
BP
1005 return error;
1006 }
1b1d2e6d
BP
1007 ovsdb_storage_wait(p->storage);
1008 poll_block();
f85f8ebb 1009 }
1b1d2e6d 1010}
53178986 1011
1b1d2e6d
BP
1012static void
1013ovsdb_txn_progress_run(struct ovsdb_txn_progress *p)
1014{
1015 if (p->error) {
1016 return;
1017 }
f85f8ebb 1018
1b1d2e6d
BP
1019 if (p->write) {
1020 if (!ovsdb_write_is_complete(p->write)) {
1021 return;
1022 }
1023 p->error = ovsdb_error_clone(ovsdb_write_get_error(p->write));
1024 p->commit_index = ovsdb_write_get_commit_index(p->write);
1025 ovsdb_write_destroy(p->write);
1026 p->write = NULL;
3e010b7a 1027
1b1d2e6d
BP
1028 if (p->error) {
1029 return;
1030 }
1031 }
1032
1033 if (p->commit_index) {
1034 if (ovsdb_storage_get_applied_index(p->storage) >= p->commit_index) {
1035 p->commit_index = 0;
1036 }
1037 }
f85f8ebb
BP
1038}
1039
1b1d2e6d
BP
1040static bool
1041ovsdb_txn_progress_is_complete__(const struct ovsdb_txn_progress *p)
bc487aef 1042{
1b1d2e6d
BP
1043 return p->error || (!p->write && !p->commit_index);
1044}
1045
1046bool
1047ovsdb_txn_progress_is_complete(const struct ovsdb_txn_progress *p)
1048{
1049 ovsdb_txn_progress_run(CONST_CAST(struct ovsdb_txn_progress *, p));
1050 return ovsdb_txn_progress_is_complete__(p);
1051}
1052
1053const struct ovsdb_error *
1054ovsdb_txn_progress_get_error(const struct ovsdb_txn_progress *p)
1055{
1056 ovs_assert(ovsdb_txn_progress_is_complete__(p));
1057 return p->error;
1058}
1059
1060void
1061ovsdb_txn_progress_destroy(struct ovsdb_txn_progress *p)
1062{
1063 if (p) {
1064 ovsdb_error_destroy(p->error);
1065 ovsdb_write_destroy(p->write);
1066 free(p);
53178986 1067 }
bc487aef
AZ
1068}
1069
bd06962a
BP
1070void
1071ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
1072 ovsdb_txn_row_cb_func *cb, void *aux)
f85f8ebb 1073{
bd06962a
BP
1074 struct ovsdb_txn_table *t;
1075 struct ovsdb_txn_row *r;
f85f8ebb 1076
4e8e4213
BP
1077 LIST_FOR_EACH (t, node, &txn->txn_tables) {
1078 HMAP_FOR_EACH (r, hmap_node, &t->txn_rows) {
a8c37dc5 1079 if ((r->old || r->new) && !cb(r->old, r->new, r->changed, aux)) {
bd06962a
BP
1080 break;
1081 }
f85f8ebb 1082 }
bd06962a 1083 }
f85f8ebb
BP
1084}
1085
1086static struct ovsdb_txn_table *
71c93bd4 1087ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
f85f8ebb 1088{
71c93bd4
BP
1089 if (!table->txn_table) {
1090 struct ovsdb_txn_table *txn_table;
6910a6e6 1091 size_t i;
f85f8ebb 1092
71c93bd4 1093 table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
f85f8ebb
BP
1094 txn_table->table = table;
1095 hmap_init(&txn_table->txn_rows);
3e010b7a 1096 txn_table->serial = serial - 1;
6910a6e6
BP
1097 txn_table->txn_indexes = xmalloc(table->schema->n_indexes
1098 * sizeof *txn_table->txn_indexes);
1099 for (i = 0; i < table->schema->n_indexes; i++) {
1100 hmap_init(&txn_table->txn_indexes[i]);
1101 }
417e7e66 1102 ovs_list_push_back(&txn->txn_tables, &txn_table->node);
f85f8ebb 1103 }
71c93bd4 1104 return table->txn_table;
f85f8ebb
BP
1105}
1106
1107static struct ovsdb_txn_row *
71c93bd4 1108ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
b405dcfb 1109 const struct ovsdb_row *old_, struct ovsdb_row *new)
f85f8ebb 1110{
a8c37dc5 1111 const struct ovsdb_row *row = old_ ? old_ : new;
ebc56baa 1112 struct ovsdb_row *old = CONST_CAST(struct ovsdb_row *, old_);
17d18afb 1113 size_t n_columns = shash_count(&table->schema->columns);
71c93bd4 1114 struct ovsdb_txn_table *txn_table;
f85f8ebb
BP
1115 struct ovsdb_txn_row *txn_row;
1116
17d18afb
BP
1117 txn_row = xzalloc(offsetof(struct ovsdb_txn_row, changed)
1118 + bitmap_n_bytes(n_columns));
a8c37dc5
BP
1119 txn_row->uuid = *ovsdb_row_get_uuid(row);
1120 txn_row->table = row->table;
1121 txn_row->old = old;
f85f8ebb 1122 txn_row->new = new;
c7d85e0d 1123 txn_row->n_refs = old ? old->n_refs : 0;
3e010b7a 1124 txn_row->serial = serial - 1;
71c93bd4 1125
b405dcfb
BP
1126 if (old) {
1127 old->txn_row = txn_row;
1128 }
1129 if (new) {
1130 new->txn_row = txn_row;
1131 }
1132
71c93bd4
BP
1133 txn_table = ovsdb_txn_create_txn_table(txn, table);
1134 hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
1135 ovsdb_row_hash(old ? old : new));
f85f8ebb
BP
1136
1137 return txn_row;
1138}
1139
1140struct ovsdb_row *
1141ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
1142{
ebc56baa 1143 struct ovsdb_row *ro_row = CONST_CAST(struct ovsdb_row *, ro_row_);
f85f8ebb
BP
1144
1145 if (ro_row->txn_row) {
cb22974d 1146 ovs_assert(ro_row == ro_row->txn_row->new);
f85f8ebb
BP
1147 return ro_row;
1148 } else {
1149 struct ovsdb_table *table = ro_row->table;
f85f8ebb
BP
1150 struct ovsdb_row *rw_row;
1151
f85f8ebb 1152 rw_row = ovsdb_row_clone(ro_row);
0d0f05b9 1153 rw_row->n_refs = ro_row->n_refs;
b405dcfb 1154 ovsdb_txn_row_create(txn, table, ro_row, rw_row);
f85f8ebb
BP
1155 hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
1156
1157 return rw_row;
1158 }
1159}
1160
1161void
1162ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
1163{
1164 uint32_t hash = ovsdb_row_hash(row);
1165 struct ovsdb_table *table = row->table;
f85f8ebb
BP
1166
1167 uuid_generate(ovsdb_row_get_version_rw(row));
1168
b405dcfb 1169 ovsdb_txn_row_create(txn, table, NULL, row);
f85f8ebb
BP
1170 hmap_insert(&table->rows, &row->hmap_node, hash);
1171}
1172
1173/* 'row' must be assumed destroyed upon return; the caller must not reference
1174 * it again. */
1175void
1176ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
1177{
ebc56baa 1178 struct ovsdb_row *row = CONST_CAST(struct ovsdb_row *, row_);
f85f8ebb
BP
1179 struct ovsdb_table *table = row->table;
1180 struct ovsdb_txn_row *txn_row = row->txn_row;
f85f8ebb
BP
1181
1182 hmap_remove(&table->rows, &row->hmap_node);
1183
1184 if (!txn_row) {
b405dcfb 1185 ovsdb_txn_row_create(txn, table, row, NULL);
f85f8ebb 1186 } else {
cb22974d 1187 ovs_assert(txn_row->new == row);
f85f8ebb
BP
1188 if (txn_row->old) {
1189 txn_row->new = NULL;
1190 } else {
71c93bd4 1191 hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
e084f690 1192 free(txn_row);
f85f8ebb
BP
1193 }
1194 ovsdb_row_destroy(row);
1195 }
1196}
d171b584
BP
1197
1198void
1199ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
1200{
1201 if (txn->comment.length) {
1202 ds_put_char(&txn->comment, '\n');
1203 }
1204 ds_put_cstr(&txn->comment, s);
1205}
1206
1207const char *
1208ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
1209{
1210 return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
1211}
3e010b7a
BP
1212\f
1213static void
1214ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
1215{
a8c37dc5 1216 struct ovsdb_txn_table *txn_table = txn_row->table->txn_table;
3e010b7a
BP
1217
1218 txn_table->n_processed--;
1219 hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
1220
1221 if (txn_row->old) {
1222 txn_row->old->txn_row = NULL;
1223 }
1224 if (txn_row->new) {
1225 txn_row->new->txn_row = NULL;
1226 }
1227}
1228
1229static void
1230ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
1231{
6910a6e6
BP
1232 size_t i;
1233
cb22974d 1234 ovs_assert(hmap_is_empty(&txn_table->txn_rows));
6910a6e6
BP
1235
1236 for (i = 0; i < txn_table->table->schema->n_indexes; i++) {
1237 hmap_destroy(&txn_table->txn_indexes[i]);
1238 }
714110de 1239 free(txn_table->txn_indexes);
6910a6e6 1240
3e010b7a
BP
1241 txn_table->table->txn_table = NULL;
1242 hmap_destroy(&txn_table->txn_rows);
417e7e66 1243 ovs_list_remove(&txn_table->node);
3e010b7a
BP
1244 free(txn_table);
1245}
1246
1247/* Calls 'cb' for every txn_row within 'txn'. If 'cb' returns nonnull, this
1248 * aborts the iteration and for_each_txn_row() passes the error up. Otherwise,
1249 * returns a null pointer after iteration is complete.
1250 *
1251 * 'cb' may insert new txn_rows and new txn_tables into 'txn'. It may delete
1252 * the txn_row that it is passed in, or txn_rows in txn_tables other than the
1253 * one passed to 'cb'. It may *not* delete txn_rows other than the one passed
1254 * in within the same txn_table. It may *not* delete any txn_tables. As long
1255 * as these rules are followed, 'cb' will be called exactly once for each
1256 * txn_row in 'txn', even those added by 'cb'.
a8c37dc5
BP
1257 *
1258 * (Even though 'cb' is not allowed to delete some txn_rows, it can still
1259 * delete any actual row by clearing a txn_row's 'new' member.)
3e010b7a 1260 */
cab50449 1261static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
3e010b7a
BP
1262for_each_txn_row(struct ovsdb_txn *txn,
1263 struct ovsdb_error *(*cb)(struct ovsdb_txn *,
1264 struct ovsdb_txn_row *))
1265{
1266 bool any_work;
1267
1268 serial++;
1269
1270 do {
1271 struct ovsdb_txn_table *t, *next_txn_table;
1272
1273 any_work = false;
4e8e4213 1274 LIST_FOR_EACH_SAFE (t, next_txn_table, node, &txn->txn_tables) {
3e010b7a
BP
1275 if (t->serial != serial) {
1276 t->serial = serial;
1277 t->n_processed = 0;
1278 }
1279
1280 while (t->n_processed < hmap_count(&t->txn_rows)) {
1281 struct ovsdb_txn_row *r, *next_txn_row;
1282
4e8e4213 1283 HMAP_FOR_EACH_SAFE (r, next_txn_row, hmap_node, &t->txn_rows) {
3e010b7a
BP
1284 if (r->serial != serial) {
1285 struct ovsdb_error *error;
1286
1287 r->serial = serial;
1288 t->n_processed++;
1289 any_work = true;
1290
1291 error = cb(txn, r);
1292 if (error) {
1293 return error;
1294 }
1295 }
1296 }
1297 }
1298 if (hmap_is_empty(&t->txn_rows)) {
1299 /* Table is empty. Drop it. */
1300 ovsdb_txn_table_destroy(t);
1301 }
1302 }
1303 } while (any_work);
1304
1305 return NULL;
1306}