]>
Commit | Line | Data |
---|---|---|
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 |
37 | VLOG_DEFINE_THIS_MODULE(transaction); |
38 | ||
f85f8ebb BP |
39 | struct 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. */ | |
46 | struct 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 | */ |
76 | struct 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 | 94 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
c5f341ab | 95 | delete_garbage_row(struct ovsdb_txn *txn, struct ovsdb_txn_row *r); |
3e010b7a | 96 | static void ovsdb_txn_row_prefree(struct ovsdb_txn_row *); |
cab50449 | 97 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
3e010b7a BP |
98 | for_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. */ | |
104 | static unsigned int serial; | |
105 | ||
f85f8ebb BP |
106 | struct ovsdb_txn * |
107 | ovsdb_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 | ||
116 | static void | |
3e010b7a | 117 | ovsdb_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 | 124 | static struct ovsdb_error * |
3e010b7a BP |
125 | ovsdb_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'. */ | |
149 | static size_t | |
150 | ovsdb_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'. */ | |
159 | static struct hmap_node * | |
160 | ovsdb_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'. */ | |
167 | static struct ovsdb_row * | |
168 | ovsdb_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 |
174 | void |
175 | ovsdb_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 |
181 | static struct ovsdb_txn_row * |
182 | find_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 |
200 | static struct ovsdb_txn_row * |
201 | find_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 | 214 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
97f7803b BP |
215 | ovsdb_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 | ||
228 | table = base->u.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 | 254 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
c7d85e0d BP |
255 | ovsdb_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 | 270 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
c7d85e0d | 271 | update_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 | 299 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
c7d85e0d | 300 | check_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 | 313 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
c5f341ab BP |
314 | delete_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 | ||
325 | table = base->u.uuid.refTable; | |
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 | 355 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
c5f341ab BP |
356 | delete_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 | 390 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
c5f341ab BP |
391 | collect_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 | 399 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
0d0f05b9 BP |
400 | update_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 | 412 | static struct ovsdb_error * |
3e010b7a BP |
413 | ovsdb_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 |
445 | static struct ovsdb_error * |
446 | ovsdb_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 | 485 | static void |
aa1fc801 | 486 | add_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 | 514 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
7360012b BP |
515 | assess_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 | ||
552 | row = ovsdb_table_get_row(column->type.key.u.uuid.refTable, | |
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 | ||
570 | row = ovsdb_table_get_row(column->type.value.u.uuid.refTable, | |
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 | 620 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
17d18afb BP |
621 | determine_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 | 654 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
87ab878c BP |
655 | check_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 |
675 | static struct ovsdb_row * |
676 | ovsdb_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 | ||
694 | static void | |
695 | duplicate_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 | 723 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
6910a6e6 BP |
724 | duplicate_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 | 762 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
6910a6e6 BP |
763 | check_index_uniqueness(struct ovsdb_txn *txn OVS_UNUSED, |
764 | struct ovsdb_txn_row *txn_row) | |
765 | { | |
766 | struct ovsdb_txn_table *txn_table = txn_row->table->txn_table; | |
767 | struct ovsdb_table *table = txn_row->table; | |
768 | struct ovsdb_row *row = txn_row->new; | |
769 | size_t i; | |
770 | ||
771 | if (!row) { | |
772 | return NULL; | |
773 | } | |
774 | ||
775 | for (i = 0; i < table->schema->n_indexes; i++) { | |
776 | const struct ovsdb_column_set *index = &table->schema->indexes[i]; | |
777 | struct ovsdb_row *irow; | |
778 | uint32_t hash; | |
779 | ||
780 | hash = ovsdb_row_hash_columns(row, index, 0); | |
781 | irow = ovsdb_index_search(&txn_table->txn_indexes[i], row, i, hash); | |
782 | if (irow) { | |
783 | return duplicate_index_row(index, irow, row); | |
784 | } | |
785 | ||
786 | irow = ovsdb_index_search(&table->indexes[i], row, i, hash); | |
787 | if (irow && !irow->txn_row) { | |
788 | return duplicate_index_row(index, irow, row); | |
789 | } | |
790 | ||
791 | hmap_insert(&txn_table->txn_indexes[i], | |
792 | ovsdb_row_get_index_node(row, i), hash); | |
793 | } | |
794 | ||
795 | return NULL; | |
796 | } | |
797 | ||
eac0dc83 BP |
798 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
799 | update_version(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *txn_row) | |
800 | { | |
801 | struct ovsdb_table *table = txn_row->table; | |
802 | size_t n_columns = shash_count(&table->schema->columns); | |
803 | ||
804 | if (txn_row->old && txn_row->new | |
805 | && !bitmap_is_all_zeros(txn_row->changed, n_columns)) { | |
806 | bitmap_set1(txn_row->changed, OVSDB_COL_VERSION); | |
807 | uuid_generate(ovsdb_row_get_version_rw(txn_row->new)); | |
808 | } | |
809 | ||
810 | return NULL; | |
811 | } | |
812 | ||
53178986 BP |
813 | static bool |
814 | ovsdb_txn_is_empty(const struct ovsdb_txn *txn) | |
815 | { | |
816 | return ovs_list_is_empty(&txn->txn_tables); | |
817 | } | |
818 | ||
1b1d2e6d BP |
819 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
820 | ovsdb_txn_precommit(struct ovsdb_txn *txn) | |
f85f8ebb | 821 | { |
bd06962a | 822 | struct ovsdb_error *error; |
f85f8ebb | 823 | |
17d18afb BP |
824 | /* Figure out what actually changed, and abort early if the transaction |
825 | * was really a no-op. */ | |
826 | error = for_each_txn_row(txn, determine_changes); | |
827 | if (error) { | |
1e8c7cae | 828 | ovsdb_txn_abort(txn); |
1dd5b71d | 829 | return OVSDB_WRAP_BUG("can't happen", error); |
17d18afb | 830 | } |
1b1d2e6d | 831 | if (ovs_list_is_empty(&txn->txn_tables)) { |
17d18afb BP |
832 | return NULL; |
833 | } | |
834 | ||
c5f341ab BP |
835 | /* Update reference counts and check referential integrity. */ |
836 | error = update_ref_counts(txn); | |
87ab878c | 837 | if (error) { |
87ab878c BP |
838 | return error; |
839 | } | |
840 | ||
c5f341ab BP |
841 | /* Delete unreferenced, non-root rows. */ |
842 | error = for_each_txn_row(txn, collect_garbage); | |
843 | if (error) { | |
c5f341ab BP |
844 | return OVSDB_WRAP_BUG("can't happen", error); |
845 | } | |
846 | ||
847 | /* Check maximum rows table constraints. */ | |
848 | error = check_max_rows(txn); | |
0d0f05b9 | 849 | if (error) { |
0d0f05b9 BP |
850 | return error; |
851 | } | |
852 | ||
6910a6e6 | 853 | /* Check reference counts and remove bad references for "weak" referential |
7360012b BP |
854 | * integrity. */ |
855 | error = for_each_txn_row(txn, assess_weak_refs); | |
856 | if (error) { | |
7360012b BP |
857 | return error; |
858 | } | |
859 | ||
6910a6e6 BP |
860 | /* Verify that the indexes will still be unique post-transaction. */ |
861 | error = for_each_txn_row(txn, check_index_uniqueness); | |
862 | if (error) { | |
6910a6e6 BP |
863 | return error; |
864 | } | |
865 | ||
eac0dc83 BP |
866 | /* Update _version for rows that changed. */ |
867 | error = for_each_txn_row(txn, update_version); | |
868 | if (error) { | |
869 | return OVSDB_WRAP_BUG("can't happen", error); | |
870 | } | |
871 | ||
1b1d2e6d | 872 | return error; |
53178986 BP |
873 | } |
874 | ||
1b1d2e6d BP |
875 | /* Finalize commit. */ |
876 | void | |
877 | ovsdb_txn_complete(struct ovsdb_txn *txn) | |
53178986 | 878 | { |
1b1d2e6d BP |
879 | if (!ovsdb_txn_is_empty(txn)) { |
880 | txn->db->run_triggers = true; | |
881 | ovsdb_monitors_commit(txn->db, txn); | |
882 | ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_update_weak_refs)); | |
883 | ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit)); | |
884 | } | |
885 | ovsdb_txn_free(txn); | |
886 | } | |
887 | ||
888 | /* Applies 'txn' to the internal representation of the database. This is for | |
889 | * transactions that don't need to be written to storage; probably, they came | |
890 | * from storage. These transactions shouldn't ordinarily fail because storage | |
891 | * should contain only consistent transactions. (One exception is for database | |
892 | * conversion in ovsdb_convert().) */ | |
893 | struct ovsdb_error * OVS_WARN_UNUSED_RESULT | |
894 | ovsdb_txn_replay_commit(struct ovsdb_txn *txn) | |
895 | { | |
896 | struct ovsdb_error *error = ovsdb_txn_precommit(txn); | |
897 | if (error) { | |
898 | ovsdb_txn_abort(txn); | |
899 | } else { | |
900 | ovsdb_txn_complete(txn); | |
901 | } | |
902 | return error; | |
903 | } | |
904 | ||
905 | /* If 'error' is nonnull, the transaction is complete, with the given error as | |
906 | * the result. | |
907 | * | |
908 | * Otherwise, if 'write' is nonnull, then the transaction is waiting for | |
909 | * 'write' to complete. | |
910 | * | |
911 | * Otherwise, if 'commit_index' is nonzero, then the transaction is waiting for | |
912 | * 'commit_index' to be applied to the storage. | |
913 | * | |
914 | * Otherwise, the transaction is complete and successful. */ | |
915 | struct ovsdb_txn_progress { | |
916 | struct ovsdb_error *error; | |
917 | struct ovsdb_write *write; | |
918 | uint64_t commit_index; | |
919 | ||
920 | struct ovsdb_storage *storage; | |
921 | }; | |
922 | ||
923 | struct ovsdb_txn_progress * | |
924 | ovsdb_txn_propose_schema_change(struct ovsdb *db, | |
925 | const struct json *schema, | |
926 | const struct json *data) | |
927 | { | |
928 | struct ovsdb_txn_progress *progress = xzalloc(sizeof *progress); | |
929 | progress->storage = db->storage; | |
930 | ||
931 | struct uuid next; | |
932 | struct ovsdb_write *write = ovsdb_storage_write_schema_change( | |
933 | db->storage, schema, data, &db->prereq, &next); | |
934 | if (!ovsdb_write_is_complete(write)) { | |
935 | progress->write = write; | |
936 | } else { | |
937 | progress->error = ovsdb_error_clone(ovsdb_write_get_error(write)); | |
938 | ovsdb_write_destroy(write); | |
939 | } | |
940 | return progress; | |
941 | } | |
942 | ||
943 | struct ovsdb_txn_progress * | |
944 | ovsdb_txn_propose_commit(struct ovsdb_txn *txn, bool durable) | |
945 | { | |
946 | struct ovsdb_txn_progress *progress = xzalloc(sizeof *progress); | |
947 | progress->storage = txn->db->storage; | |
948 | progress->error = ovsdb_txn_precommit(txn); | |
949 | if (progress->error) { | |
950 | return progress; | |
951 | } | |
952 | ||
953 | /* Turn the commit into the format used for the storage logs.. */ | |
954 | struct json *txn_json = ovsdb_file_txn_to_json(txn); | |
955 | if (!txn_json) { | |
956 | /* Nothing to do, so success. */ | |
957 | return progress; | |
958 | } | |
959 | txn_json = ovsdb_file_txn_annotate(txn_json, ovsdb_txn_get_comment(txn)); | |
960 | ||
961 | struct uuid next; | |
962 | struct ovsdb_write *write = ovsdb_storage_write( | |
963 | txn->db->storage, txn_json, &txn->db->prereq, &next, durable); | |
964 | json_destroy(txn_json); | |
965 | if (!ovsdb_write_is_complete(write)) { | |
966 | progress->write = write; | |
967 | } else { | |
968 | progress->error = ovsdb_error_clone(ovsdb_write_get_error(write)); | |
969 | ovsdb_write_destroy(write); | |
970 | } | |
971 | return progress; | |
972 | } | |
973 | ||
974 | /* Proposes 'txn' for commitment and then waits for the commit to succeed or | |
975 | * fail. Returns null if successful, otherwise the error. | |
976 | * | |
977 | * **In addition**, this function also completes or aborts the transaction if | |
978 | * the transaction succeeded or failed, respectively. */ | |
979 | struct ovsdb_error * OVS_WARN_UNUSED_RESULT | |
980 | ovsdb_txn_propose_commit_block(struct ovsdb_txn *txn, bool durable) | |
981 | { | |
982 | struct ovsdb_txn_progress *p = ovsdb_txn_propose_commit(txn, durable); | |
983 | for (;;) { | |
984 | ovsdb_storage_run(p->storage); | |
985 | if (ovsdb_txn_progress_is_complete(p)) { | |
986 | struct ovsdb_error *error | |
987 | = ovsdb_error_clone(ovsdb_txn_progress_get_error(p)); | |
988 | ovsdb_txn_progress_destroy(p); | |
989 | ||
990 | if (error) { | |
991 | ovsdb_txn_abort(txn); | |
992 | } else { | |
993 | ovsdb_txn_complete(txn); | |
994 | } | |
995 | ||
f85f8ebb BP |
996 | return error; |
997 | } | |
1b1d2e6d BP |
998 | ovsdb_storage_wait(p->storage); |
999 | poll_block(); | |
f85f8ebb | 1000 | } |
1b1d2e6d | 1001 | } |
53178986 | 1002 | |
1b1d2e6d BP |
1003 | static void |
1004 | ovsdb_txn_progress_run(struct ovsdb_txn_progress *p) | |
1005 | { | |
1006 | if (p->error) { | |
1007 | return; | |
1008 | } | |
f85f8ebb | 1009 | |
1b1d2e6d BP |
1010 | if (p->write) { |
1011 | if (!ovsdb_write_is_complete(p->write)) { | |
1012 | return; | |
1013 | } | |
1014 | p->error = ovsdb_error_clone(ovsdb_write_get_error(p->write)); | |
1015 | p->commit_index = ovsdb_write_get_commit_index(p->write); | |
1016 | ovsdb_write_destroy(p->write); | |
1017 | p->write = NULL; | |
3e010b7a | 1018 | |
1b1d2e6d BP |
1019 | if (p->error) { |
1020 | return; | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | if (p->commit_index) { | |
1025 | if (ovsdb_storage_get_applied_index(p->storage) >= p->commit_index) { | |
1026 | p->commit_index = 0; | |
1027 | } | |
1028 | } | |
f85f8ebb BP |
1029 | } |
1030 | ||
1b1d2e6d BP |
1031 | static bool |
1032 | ovsdb_txn_progress_is_complete__(const struct ovsdb_txn_progress *p) | |
bc487aef | 1033 | { |
1b1d2e6d BP |
1034 | return p->error || (!p->write && !p->commit_index); |
1035 | } | |
1036 | ||
1037 | bool | |
1038 | ovsdb_txn_progress_is_complete(const struct ovsdb_txn_progress *p) | |
1039 | { | |
1040 | ovsdb_txn_progress_run(CONST_CAST(struct ovsdb_txn_progress *, p)); | |
1041 | return ovsdb_txn_progress_is_complete__(p); | |
1042 | } | |
1043 | ||
1044 | const struct ovsdb_error * | |
1045 | ovsdb_txn_progress_get_error(const struct ovsdb_txn_progress *p) | |
1046 | { | |
1047 | ovs_assert(ovsdb_txn_progress_is_complete__(p)); | |
1048 | return p->error; | |
1049 | } | |
1050 | ||
1051 | void | |
1052 | ovsdb_txn_progress_destroy(struct ovsdb_txn_progress *p) | |
1053 | { | |
1054 | if (p) { | |
1055 | ovsdb_error_destroy(p->error); | |
1056 | ovsdb_write_destroy(p->write); | |
1057 | free(p); | |
53178986 | 1058 | } |
bc487aef AZ |
1059 | } |
1060 | ||
bd06962a BP |
1061 | void |
1062 | ovsdb_txn_for_each_change(const struct ovsdb_txn *txn, | |
1063 | ovsdb_txn_row_cb_func *cb, void *aux) | |
f85f8ebb | 1064 | { |
bd06962a BP |
1065 | struct ovsdb_txn_table *t; |
1066 | struct ovsdb_txn_row *r; | |
f85f8ebb | 1067 | |
4e8e4213 BP |
1068 | LIST_FOR_EACH (t, node, &txn->txn_tables) { |
1069 | HMAP_FOR_EACH (r, hmap_node, &t->txn_rows) { | |
a8c37dc5 | 1070 | if ((r->old || r->new) && !cb(r->old, r->new, r->changed, aux)) { |
bd06962a BP |
1071 | break; |
1072 | } | |
f85f8ebb | 1073 | } |
bd06962a | 1074 | } |
f85f8ebb BP |
1075 | } |
1076 | ||
1077 | static struct ovsdb_txn_table * | |
71c93bd4 | 1078 | ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table) |
f85f8ebb | 1079 | { |
71c93bd4 BP |
1080 | if (!table->txn_table) { |
1081 | struct ovsdb_txn_table *txn_table; | |
6910a6e6 | 1082 | size_t i; |
f85f8ebb | 1083 | |
71c93bd4 | 1084 | table->txn_table = txn_table = xmalloc(sizeof *table->txn_table); |
f85f8ebb BP |
1085 | txn_table->table = table; |
1086 | hmap_init(&txn_table->txn_rows); | |
3e010b7a | 1087 | txn_table->serial = serial - 1; |
6910a6e6 BP |
1088 | txn_table->txn_indexes = xmalloc(table->schema->n_indexes |
1089 | * sizeof *txn_table->txn_indexes); | |
1090 | for (i = 0; i < table->schema->n_indexes; i++) { | |
1091 | hmap_init(&txn_table->txn_indexes[i]); | |
1092 | } | |
417e7e66 | 1093 | ovs_list_push_back(&txn->txn_tables, &txn_table->node); |
f85f8ebb | 1094 | } |
71c93bd4 | 1095 | return table->txn_table; |
f85f8ebb BP |
1096 | } |
1097 | ||
1098 | static struct ovsdb_txn_row * | |
71c93bd4 | 1099 | ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table, |
b405dcfb | 1100 | const struct ovsdb_row *old_, struct ovsdb_row *new) |
f85f8ebb | 1101 | { |
a8c37dc5 | 1102 | const struct ovsdb_row *row = old_ ? old_ : new; |
ebc56baa | 1103 | struct ovsdb_row *old = CONST_CAST(struct ovsdb_row *, old_); |
17d18afb | 1104 | size_t n_columns = shash_count(&table->schema->columns); |
71c93bd4 | 1105 | struct ovsdb_txn_table *txn_table; |
f85f8ebb BP |
1106 | struct ovsdb_txn_row *txn_row; |
1107 | ||
17d18afb BP |
1108 | txn_row = xzalloc(offsetof(struct ovsdb_txn_row, changed) |
1109 | + bitmap_n_bytes(n_columns)); | |
a8c37dc5 BP |
1110 | txn_row->uuid = *ovsdb_row_get_uuid(row); |
1111 | txn_row->table = row->table; | |
1112 | txn_row->old = old; | |
f85f8ebb | 1113 | txn_row->new = new; |
c7d85e0d | 1114 | txn_row->n_refs = old ? old->n_refs : 0; |
3e010b7a | 1115 | txn_row->serial = serial - 1; |
71c93bd4 | 1116 | |
b405dcfb BP |
1117 | if (old) { |
1118 | old->txn_row = txn_row; | |
1119 | } | |
1120 | if (new) { | |
1121 | new->txn_row = txn_row; | |
1122 | } | |
1123 | ||
71c93bd4 BP |
1124 | txn_table = ovsdb_txn_create_txn_table(txn, table); |
1125 | hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node, | |
1126 | ovsdb_row_hash(old ? old : new)); | |
f85f8ebb BP |
1127 | |
1128 | return txn_row; | |
1129 | } | |
1130 | ||
1131 | struct ovsdb_row * | |
1132 | ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_) | |
1133 | { | |
ebc56baa | 1134 | struct ovsdb_row *ro_row = CONST_CAST(struct ovsdb_row *, ro_row_); |
f85f8ebb BP |
1135 | |
1136 | if (ro_row->txn_row) { | |
cb22974d | 1137 | ovs_assert(ro_row == ro_row->txn_row->new); |
f85f8ebb BP |
1138 | return ro_row; |
1139 | } else { | |
1140 | struct ovsdb_table *table = ro_row->table; | |
f85f8ebb BP |
1141 | struct ovsdb_row *rw_row; |
1142 | ||
f85f8ebb | 1143 | rw_row = ovsdb_row_clone(ro_row); |
0d0f05b9 | 1144 | rw_row->n_refs = ro_row->n_refs; |
b405dcfb | 1145 | ovsdb_txn_row_create(txn, table, ro_row, rw_row); |
f85f8ebb BP |
1146 | hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node); |
1147 | ||
1148 | return rw_row; | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | void | |
1153 | ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row) | |
1154 | { | |
1155 | uint32_t hash = ovsdb_row_hash(row); | |
1156 | struct ovsdb_table *table = row->table; | |
f85f8ebb BP |
1157 | |
1158 | uuid_generate(ovsdb_row_get_version_rw(row)); | |
1159 | ||
b405dcfb | 1160 | ovsdb_txn_row_create(txn, table, NULL, row); |
f85f8ebb BP |
1161 | hmap_insert(&table->rows, &row->hmap_node, hash); |
1162 | } | |
1163 | ||
1164 | /* 'row' must be assumed destroyed upon return; the caller must not reference | |
1165 | * it again. */ | |
1166 | void | |
1167 | ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_) | |
1168 | { | |
ebc56baa | 1169 | struct ovsdb_row *row = CONST_CAST(struct ovsdb_row *, row_); |
f85f8ebb BP |
1170 | struct ovsdb_table *table = row->table; |
1171 | struct ovsdb_txn_row *txn_row = row->txn_row; | |
f85f8ebb BP |
1172 | |
1173 | hmap_remove(&table->rows, &row->hmap_node); | |
1174 | ||
1175 | if (!txn_row) { | |
b405dcfb | 1176 | ovsdb_txn_row_create(txn, table, row, NULL); |
f85f8ebb | 1177 | } else { |
cb22974d | 1178 | ovs_assert(txn_row->new == row); |
f85f8ebb BP |
1179 | if (txn_row->old) { |
1180 | txn_row->new = NULL; | |
1181 | } else { | |
71c93bd4 | 1182 | hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node); |
e084f690 | 1183 | free(txn_row); |
f85f8ebb BP |
1184 | } |
1185 | ovsdb_row_destroy(row); | |
1186 | } | |
1187 | } | |
d171b584 BP |
1188 | |
1189 | void | |
1190 | ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s) | |
1191 | { | |
1192 | if (txn->comment.length) { | |
1193 | ds_put_char(&txn->comment, '\n'); | |
1194 | } | |
1195 | ds_put_cstr(&txn->comment, s); | |
1196 | } | |
1197 | ||
1198 | const char * | |
1199 | ovsdb_txn_get_comment(const struct ovsdb_txn *txn) | |
1200 | { | |
1201 | return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL; | |
1202 | } | |
3e010b7a BP |
1203 | \f |
1204 | static void | |
1205 | ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row) | |
1206 | { | |
a8c37dc5 | 1207 | struct ovsdb_txn_table *txn_table = txn_row->table->txn_table; |
3e010b7a BP |
1208 | |
1209 | txn_table->n_processed--; | |
1210 | hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node); | |
1211 | ||
1212 | if (txn_row->old) { | |
1213 | txn_row->old->txn_row = NULL; | |
1214 | } | |
1215 | if (txn_row->new) { | |
1216 | txn_row->new->txn_row = NULL; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | static void | |
1221 | ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table) | |
1222 | { | |
6910a6e6 BP |
1223 | size_t i; |
1224 | ||
cb22974d | 1225 | ovs_assert(hmap_is_empty(&txn_table->txn_rows)); |
6910a6e6 BP |
1226 | |
1227 | for (i = 0; i < txn_table->table->schema->n_indexes; i++) { | |
1228 | hmap_destroy(&txn_table->txn_indexes[i]); | |
1229 | } | |
714110de | 1230 | free(txn_table->txn_indexes); |
6910a6e6 | 1231 | |
3e010b7a BP |
1232 | txn_table->table->txn_table = NULL; |
1233 | hmap_destroy(&txn_table->txn_rows); | |
417e7e66 | 1234 | ovs_list_remove(&txn_table->node); |
3e010b7a BP |
1235 | free(txn_table); |
1236 | } | |
1237 | ||
1238 | /* Calls 'cb' for every txn_row within 'txn'. If 'cb' returns nonnull, this | |
1239 | * aborts the iteration and for_each_txn_row() passes the error up. Otherwise, | |
1240 | * returns a null pointer after iteration is complete. | |
1241 | * | |
1242 | * 'cb' may insert new txn_rows and new txn_tables into 'txn'. It may delete | |
1243 | * the txn_row that it is passed in, or txn_rows in txn_tables other than the | |
1244 | * one passed to 'cb'. It may *not* delete txn_rows other than the one passed | |
1245 | * in within the same txn_table. It may *not* delete any txn_tables. As long | |
1246 | * as these rules are followed, 'cb' will be called exactly once for each | |
1247 | * txn_row in 'txn', even those added by 'cb'. | |
a8c37dc5 BP |
1248 | * |
1249 | * (Even though 'cb' is not allowed to delete some txn_rows, it can still | |
1250 | * delete any actual row by clearing a txn_row's 'new' member.) | |
3e010b7a | 1251 | */ |
cab50449 | 1252 | static struct ovsdb_error * OVS_WARN_UNUSED_RESULT |
3e010b7a BP |
1253 | for_each_txn_row(struct ovsdb_txn *txn, |
1254 | struct ovsdb_error *(*cb)(struct ovsdb_txn *, | |
1255 | struct ovsdb_txn_row *)) | |
1256 | { | |
1257 | bool any_work; | |
1258 | ||
1259 | serial++; | |
1260 | ||
1261 | do { | |
1262 | struct ovsdb_txn_table *t, *next_txn_table; | |
1263 | ||
1264 | any_work = false; | |
4e8e4213 | 1265 | LIST_FOR_EACH_SAFE (t, next_txn_table, node, &txn->txn_tables) { |
3e010b7a BP |
1266 | if (t->serial != serial) { |
1267 | t->serial = serial; | |
1268 | t->n_processed = 0; | |
1269 | } | |
1270 | ||
1271 | while (t->n_processed < hmap_count(&t->txn_rows)) { | |
1272 | struct ovsdb_txn_row *r, *next_txn_row; | |
1273 | ||
4e8e4213 | 1274 | HMAP_FOR_EACH_SAFE (r, next_txn_row, hmap_node, &t->txn_rows) { |
3e010b7a BP |
1275 | if (r->serial != serial) { |
1276 | struct ovsdb_error *error; | |
1277 | ||
1278 | r->serial = serial; | |
1279 | t->n_processed++; | |
1280 | any_work = true; | |
1281 | ||
1282 | error = cb(txn, r); | |
1283 | if (error) { | |
1284 | return error; | |
1285 | } | |
1286 | } | |
1287 | } | |
1288 | } | |
1289 | if (hmap_is_empty(&t->txn_rows)) { | |
1290 | /* Table is empty. Drop it. */ | |
1291 | ovsdb_txn_table_destroy(t); | |
1292 | } | |
1293 | } | |
1294 | } while (any_work); | |
1295 | ||
1296 | return NULL; | |
1297 | } |