ovsdb: Allow constraining the number of rows in a table.
[cascardo/ovs.git] / ovsdb / transaction.c
1 /* Copyright (c) 2009, 2010 Nicira Networks
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
20 #include <assert.h>
21
22 #include "bitmap.h"
23 #include "dynamic-string.h"
24 #include "hash.h"
25 #include "hmap.h"
26 #include "json.h"
27 #include "list.h"
28 #include "ovsdb-error.h"
29 #include "ovsdb.h"
30 #include "row.h"
31 #include "table.h"
32 #include "uuid.h"
33
34 struct ovsdb_txn {
35     struct ovsdb *db;
36     struct list txn_tables;     /* Contains "struct ovsdb_txn_table"s. */
37     struct ds comment;
38 };
39
40 /* A table modified by a transaction. */
41 struct ovsdb_txn_table {
42     struct list node;           /* Element in ovsdb_txn's txn_tables list. */
43     struct ovsdb_table *table;
44     struct hmap txn_rows;       /* Contains "struct ovsdb_txn_row"s. */
45
46     /* Used by for_each_txn_row(). */
47     unsigned int serial;        /* Serial number of in-progress iteration. */
48     unsigned int n_processed;   /* Number of rows processed. */
49 };
50
51 /* A row modified by the transaction:
52  *
53  *      - A row added by a transaction will have null 'old' and non-null 'new'.
54  *
55  *      - A row deleted by a transaction will have non-null 'old' and null
56  *        'new'.
57  *
58  *      - A row modified by a transaction will have non-null 'old' and 'new'.
59  *
60  *      - 'old' and 'new' both null is invalid.  It would indicate that a row
61  *        was added then deleted within a single transaction, but we instead
62  *        handle that case by deleting the txn_row entirely.
63  */
64 struct ovsdb_txn_row {
65     struct hmap_node hmap_node; /* In ovsdb_txn_table's txn_rows hmap. */
66     struct ovsdb_row *old;      /* The old row. */
67     struct ovsdb_row *new;      /* The new row. */
68     size_t n_refs;              /* Number of remaining references. */
69
70     /* Used by for_each_txn_row(). */
71     unsigned int serial;        /* Serial number of in-progress commit. */
72
73     unsigned long changed[];    /* Bits set to 1 for columns that changed. */
74 };
75
76 static void ovsdb_txn_row_prefree(struct ovsdb_txn_row *);
77 static struct ovsdb_error * WARN_UNUSED_RESULT
78 for_each_txn_row(struct ovsdb_txn *txn,
79                       struct ovsdb_error *(*)(struct ovsdb_txn *,
80                                               struct ovsdb_txn_row *));
81
82 /* Used by for_each_txn_row() to track tables and rows that have been
83  * processed.  */
84 static unsigned int serial;
85
86 struct ovsdb_txn *
87 ovsdb_txn_create(struct ovsdb *db)
88 {
89     struct ovsdb_txn *txn = xmalloc(sizeof *txn);
90     txn->db = db;
91     list_init(&txn->txn_tables);
92     ds_init(&txn->comment);
93     return txn;
94 }
95
96 static void
97 ovsdb_txn_free(struct ovsdb_txn *txn)
98 {
99     assert(list_is_empty(&txn->txn_tables));
100     ds_destroy(&txn->comment);
101     free(txn);
102 }
103
104 static struct ovsdb_error *
105 ovsdb_txn_row_abort(struct ovsdb_txn *txn OVS_UNUSED,
106                     struct ovsdb_txn_row *txn_row)
107 {
108     struct ovsdb_row *old = txn_row->old;
109     struct ovsdb_row *new = txn_row->new;
110
111     ovsdb_txn_row_prefree(txn_row);
112     if (!old) {
113         hmap_remove(&new->table->rows, &new->hmap_node);
114     } else if (!new) {
115         hmap_insert(&old->table->rows, &old->hmap_node, ovsdb_row_hash(old));
116     } else {
117         hmap_replace(&new->table->rows, &new->hmap_node, &old->hmap_node);
118     }
119     ovsdb_row_destroy(new);
120     free(txn_row);
121
122     return NULL;
123 }
124
125 void
126 ovsdb_txn_abort(struct ovsdb_txn *txn)
127 {
128     ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_abort));
129     ovsdb_txn_free(txn);
130 }
131
132 static struct ovsdb_txn_row *
133 find_txn_row(const struct ovsdb_table *table, const struct uuid *uuid)
134 {
135     struct ovsdb_txn_row *txn_row;
136
137     if (!table->txn_table) {
138         return NULL;
139     }
140
141     HMAP_FOR_EACH_WITH_HASH (txn_row, struct ovsdb_txn_row, hmap_node,
142                              uuid_hash(uuid), &table->txn_table->txn_rows) {
143         const struct ovsdb_row *row;
144
145         row = txn_row->old ? txn_row->old : txn_row->new;
146         if (uuid_equals(uuid, ovsdb_row_get_uuid(row))) {
147             return txn_row;
148         }
149     }
150
151     return NULL;
152 }
153
154 static struct ovsdb_error * WARN_UNUSED_RESULT
155 ovsdb_txn_adjust_atom_refs(struct ovsdb_txn *txn,
156                            const struct ovsdb_base_type *base,
157                            const union ovsdb_atom *atoms, unsigned int n,
158                            int delta)
159 {
160     const struct ovsdb_table *table;
161     unsigned int i;
162
163     if (!ovsdb_base_type_is_strong_ref(base)) {
164         return NULL;
165     }
166
167     table = base->u.uuid.refTable;
168     for (i = 0; i < n; i++) {
169         const struct uuid *uuid = &atoms[i].uuid;
170         struct ovsdb_txn_row *txn_row = find_txn_row(table, uuid);
171         if (!txn_row) {
172             const struct ovsdb_row *row = ovsdb_table_get_row(table, uuid);
173             if (row) {
174                 txn_row = ovsdb_txn_row_modify(txn, row)->txn_row;
175             } else {
176                 return ovsdb_error("referential integrity violation",
177                                    "reference to nonexistent row "
178                                    UUID_FMT, UUID_ARGS(uuid));
179             }
180         }
181         txn_row->n_refs += delta;
182     }
183
184     return NULL;
185 }
186
187 static struct ovsdb_error * WARN_UNUSED_RESULT
188 ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
189                           const struct ovsdb_column *column, int delta)
190 {
191     const struct ovsdb_datum *field = &r->fields[column->index];
192     struct ovsdb_error *error;
193
194     error = ovsdb_txn_adjust_atom_refs(txn, &column->type.key,
195                                        field->keys, field->n, delta);
196     if (!error) {
197         error = ovsdb_txn_adjust_atom_refs(txn, &column->type.value,
198                                            field->values, field->n, delta);
199     }
200     return error;
201 }
202
203 static struct ovsdb_error * WARN_UNUSED_RESULT
204 update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
205 {
206     struct ovsdb_table *table = r->old ? r->old->table : r->new->table;
207     struct shash_node *node;
208
209     SHASH_FOR_EACH (node, &table->schema->columns) {
210         const struct ovsdb_column *column = node->data;
211         struct ovsdb_error *error;
212
213         if (r->old) {
214             error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
215             if (error) {
216                 ovsdb_error_destroy(error);
217                 return OVSDB_BUG("error decreasing refcount");
218             }
219         }
220         if (r->new) {
221             error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
222             if (error) {
223                 return error;
224             }
225         }
226     }
227
228     return NULL;
229 }
230
231 static struct ovsdb_error * WARN_UNUSED_RESULT
232 check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
233 {
234     if (r->new || !r->n_refs) {
235         return NULL;
236     } else {
237         return ovsdb_error("referential integrity violation",
238                            "cannot delete %s row "UUID_FMT" because "
239                            "of %zu remaining reference(s)",
240                            r->old->table->schema->name,
241                            UUID_ARGS(ovsdb_row_get_uuid(r->old)),
242                            r->n_refs);
243     }
244 }
245
246 static struct ovsdb_error * WARN_UNUSED_RESULT
247 update_ref_counts(struct ovsdb_txn *txn)
248 {
249     struct ovsdb_error *error;
250
251     error = for_each_txn_row(txn, update_row_ref_count);
252     if (error) {
253         return error;
254     }
255
256     return for_each_txn_row(txn, check_ref_count);
257 }
258
259 static struct ovsdb_error *
260 ovsdb_txn_row_commit(struct ovsdb_txn *txn OVS_UNUSED,
261                      struct ovsdb_txn_row *txn_row)
262 {
263     ovsdb_txn_row_prefree(txn_row);
264     if (txn_row->new) {
265         txn_row->new->n_refs = txn_row->n_refs;
266     }
267     ovsdb_row_destroy(txn_row->old);
268     free(txn_row);
269
270     return NULL;
271 }
272
273 static void
274 add_weak_ref(struct ovsdb_txn *txn,
275              const struct ovsdb_row *src_, const struct ovsdb_row *dst_)
276 {
277     struct ovsdb_row *src = (struct ovsdb_row *) src_;
278     struct ovsdb_row *dst = (struct ovsdb_row *) dst_;
279     struct ovsdb_weak_ref *weak;
280
281     if (src == dst) {
282         return;
283     }
284
285     dst = ovsdb_txn_row_modify(txn, dst);
286
287     if (!list_is_empty(&dst->dst_refs)) {
288         /* Omit duplicates. */
289         weak = CONTAINER_OF(list_back(&dst->dst_refs),
290                             struct ovsdb_weak_ref, dst_node);
291         if (weak->src == src) {
292             return;
293         }
294     }
295
296     weak = xmalloc(sizeof *weak);
297     weak->src = src;
298     list_push_back(&dst->dst_refs, &weak->dst_node);
299     list_push_back(&src->src_refs, &weak->src_node);
300 }
301
302 static struct ovsdb_error * WARN_UNUSED_RESULT
303 assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
304 {
305     struct ovsdb_table *table;
306     struct shash_node *node;
307
308     if (txn_row->old) {
309         /* Mark rows that have weak references to 'txn_row' as modified, so
310          * that their weak references will get reassessed. */
311         struct ovsdb_weak_ref *weak, *next;
312
313         LIST_FOR_EACH_SAFE (weak, next, struct ovsdb_weak_ref, dst_node,
314                             &txn_row->old->dst_refs) {
315             if (!weak->src->txn_row) {
316                 ovsdb_txn_row_modify(txn, weak->src);
317             }
318         }
319     }
320
321     if (!txn_row->new) {
322         /* We don't have to do anything about references that originate at
323          * 'txn_row', because ovsdb_row_destroy() will remove those weak
324          * references. */
325         return NULL;
326     }
327
328     table = txn_row->new->table;
329     SHASH_FOR_EACH (node, &table->schema->columns) {
330         const struct ovsdb_column *column = node->data;
331         struct ovsdb_datum *datum = &txn_row->new->fields[column->index];
332         unsigned int orig_n, i;
333         bool zero = false;
334
335         orig_n = datum->n;
336
337         if (ovsdb_base_type_is_weak_ref(&column->type.key)) {
338             for (i = 0; i < datum->n; ) {
339                 const struct ovsdb_row *row;
340
341                 row = ovsdb_table_get_row(column->type.key.u.uuid.refTable,
342                                           &datum->keys[i].uuid);
343                 if (row) {
344                     add_weak_ref(txn, txn_row->new, row);
345                     i++;
346                 } else {
347                     if (uuid_is_zero(&datum->keys[i].uuid)) {
348                         zero = true;
349                     }
350                     ovsdb_datum_remove_unsafe(datum, i, &column->type);
351                 }
352             }
353         }
354
355         if (ovsdb_base_type_is_weak_ref(&column->type.value)) {
356             for (i = 0; i < datum->n; ) {
357                 const struct ovsdb_row *row;
358
359                 row = ovsdb_table_get_row(column->type.value.u.uuid.refTable,
360                                           &datum->values[i].uuid);
361                 if (row) {
362                     add_weak_ref(txn, txn_row->new, row);
363                     i++;
364                 } else {
365                     if (uuid_is_zero(&datum->values[i].uuid)) {
366                         zero = true;
367                     }
368                     ovsdb_datum_remove_unsafe(datum, i, &column->type);
369                 }
370             }
371         }
372
373         if (datum->n != orig_n) {
374             bitmap_set1(txn_row->changed, column->index);
375             ovsdb_datum_sort_assert(datum, column->type.key.type);
376             if (datum->n < column->type.n_min) {
377                 const struct uuid *row_uuid = ovsdb_row_get_uuid(txn_row->new);
378                 if (zero && !txn_row->old) {
379                     return ovsdb_error(
380                         "constraint violation",
381                         "Weak reference column \"%s\" in \"%s\" row "UUID_FMT
382                         " (inserted within this transaction) contained "
383                         "all-zeros UUID (probably as the default value for "
384                         "this column) but deleting this value caused a "
385                         "constraint volation because this column is not "
386                         "allowed to be empty.", column->name,
387                         table->schema->name, UUID_ARGS(row_uuid));
388                 } else {
389                     return ovsdb_error(
390                         "constraint violation",
391                         "Deletion of %u weak reference(s) to deleted (or "
392                         "never-existing) rows from column \"%s\" in \"%s\" "
393                         "row "UUID_FMT" %scaused this column to become empty, "
394                         "but constraints on this column disallow an "
395                         "empty column.",
396                         orig_n - datum->n, column->name, table->schema->name,
397                         UUID_ARGS(row_uuid),
398                         (txn_row->old
399                          ? ""
400                          : "(inserted within this transaction) "));
401                 }
402             }
403         }
404     }
405
406     return NULL;
407 }
408
409 static struct ovsdb_error * WARN_UNUSED_RESULT
410 determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
411 {
412     struct ovsdb_table *table;
413
414     table = (txn_row->old ? txn_row->old : txn_row->new)->table;
415     if (txn_row->old && txn_row->new) {
416         struct shash_node *node;
417         bool changed = false;
418
419         SHASH_FOR_EACH (node, &table->schema->columns) {
420             const struct ovsdb_column *column = node->data;
421             const struct ovsdb_type *type = &column->type;
422             unsigned int idx = column->index;
423
424             if (!ovsdb_datum_equals(&txn_row->old->fields[idx],
425                                     &txn_row->new->fields[idx],
426                                     type)) {
427                 bitmap_set1(txn_row->changed, idx);
428                 changed = true;
429             }
430         }
431
432         if (!changed) {
433             /* Nothing actually changed in this row, so drop it. */
434             ovsdb_txn_row_abort(txn, txn_row);
435         }
436     } else {
437         bitmap_set_multiple(txn_row->changed, 0,
438                             shash_count(&table->schema->columns), 1);
439     }
440
441     return NULL;
442 }
443
444 static struct ovsdb_error * WARN_UNUSED_RESULT
445 check_max_rows(struct ovsdb_txn *txn)
446 {
447     struct ovsdb_txn_table *t;
448
449     LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
450         size_t n_rows = hmap_count(&t->table->rows);
451         unsigned int max_rows = t->table->schema->max_rows;
452
453         if (n_rows > max_rows) {
454             return ovsdb_error("constraint violation",
455                                "transaction causes \"%s\" table to contain "
456                                "%zu rows, greater than the schema-defined "
457                                "limit of %u row(s)",
458                                t->table->schema->name, n_rows, max_rows);
459         }
460     }
461
462     return NULL;
463 }
464
465 struct ovsdb_error *
466 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
467 {
468     struct ovsdb_replica *replica;
469     struct ovsdb_error *error;
470
471     /* Figure out what actually changed, and abort early if the transaction
472      * was really a no-op. */
473     error = for_each_txn_row(txn, determine_changes);
474     if (error) {
475         ovsdb_error_destroy(error);
476         return OVSDB_BUG("can't happen");
477     }
478     if (list_is_empty(&txn->txn_tables)) {
479         ovsdb_txn_abort(txn);
480         return NULL;
481     }
482
483     /* Check maximum rows table constraints. */
484     error = check_max_rows(txn);
485     if (error) {
486         ovsdb_txn_abort(txn);
487         return error;
488     }
489
490     /* Update reference counts and check referential integrity. */
491     error = update_ref_counts(txn);
492     if (error) {
493         ovsdb_txn_abort(txn);
494         return error;
495     }
496
497     /* Check reference counts and remove bad reference for "weak" referential
498      * integrity. */
499     error = for_each_txn_row(txn, assess_weak_refs);
500     if (error) {
501         ovsdb_txn_abort(txn);
502         return error;
503     }
504
505     /* Send the commit to each replica. */
506     LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
507         error = (replica->class->commit)(replica, txn, durable);
508         if (error) {
509             /* We don't support two-phase commit so only the first replica is
510              * allowed to report an error. */
511             assert(&replica->node == txn->db->replicas.next);
512
513             ovsdb_txn_abort(txn);
514             return error;
515         }
516     }
517
518     /* Finalize commit. */
519     txn->db->run_triggers = true;
520     ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
521     ovsdb_txn_free(txn);
522
523     return NULL;
524 }
525
526 void
527 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
528                           ovsdb_txn_row_cb_func *cb, void *aux)
529 {
530     struct ovsdb_txn_table *t;
531     struct ovsdb_txn_row *r;
532
533     LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
534         HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
535             if (!cb(r->old, r->new, r->changed, aux)) {
536                 break;
537             }
538         }
539    }
540 }
541
542 static struct ovsdb_txn_table *
543 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
544 {
545     if (!table->txn_table) {
546         struct ovsdb_txn_table *txn_table;
547
548         table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
549         txn_table->table = table;
550         hmap_init(&txn_table->txn_rows);
551         txn_table->serial = serial - 1;
552         list_push_back(&txn->txn_tables, &txn_table->node);
553     }
554     return table->txn_table;
555 }
556
557 static struct ovsdb_txn_row *
558 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
559                      const struct ovsdb_row *old_, struct ovsdb_row *new)
560 {
561     struct ovsdb_row *old = (struct ovsdb_row *) old_;
562     size_t n_columns = shash_count(&table->schema->columns);
563     struct ovsdb_txn_table *txn_table;
564     struct ovsdb_txn_row *txn_row;
565
566     txn_row = xzalloc(offsetof(struct ovsdb_txn_row, changed)
567                       + bitmap_n_bytes(n_columns));
568     txn_row->old = (struct ovsdb_row *) old;
569     txn_row->new = new;
570     txn_row->n_refs = old ? old->n_refs : 0;
571     txn_row->serial = serial - 1;
572
573     if (old) {
574         old->txn_row = txn_row;
575     }
576     if (new) {
577         new->txn_row = txn_row;
578     }
579
580     txn_table = ovsdb_txn_create_txn_table(txn, table);
581     hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
582                 ovsdb_row_hash(old ? old : new));
583
584     return txn_row;
585 }
586
587 struct ovsdb_row *
588 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
589 {
590     struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
591
592     if (ro_row->txn_row) {
593         assert(ro_row == ro_row->txn_row->new);
594         return ro_row;
595     } else {
596         struct ovsdb_table *table = ro_row->table;
597         struct ovsdb_row *rw_row;
598
599         rw_row = ovsdb_row_clone(ro_row);
600         rw_row->n_refs = ro_row->n_refs;
601         uuid_generate(ovsdb_row_get_version_rw(rw_row));
602         ovsdb_txn_row_create(txn, table, ro_row, rw_row);
603         hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
604
605         return rw_row;
606     }
607 }
608
609 void
610 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
611 {
612     uint32_t hash = ovsdb_row_hash(row);
613     struct ovsdb_table *table = row->table;
614
615     uuid_generate(ovsdb_row_get_version_rw(row));
616
617     ovsdb_txn_row_create(txn, table, NULL, row);
618     hmap_insert(&table->rows, &row->hmap_node, hash);
619 }
620
621 /* 'row' must be assumed destroyed upon return; the caller must not reference
622  * it again. */
623 void
624 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
625 {
626     struct ovsdb_row *row = (struct ovsdb_row *) row_;
627     struct ovsdb_table *table = row->table;
628     struct ovsdb_txn_row *txn_row = row->txn_row;
629
630     hmap_remove(&table->rows, &row->hmap_node);
631
632     if (!txn_row) {
633         ovsdb_txn_row_create(txn, table, row, NULL);
634     } else {
635         assert(txn_row->new == row);
636         if (txn_row->old) {
637             txn_row->new = NULL;
638         } else {
639             hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
640             free(txn_row);
641         }
642         ovsdb_row_destroy(row);
643     }
644 }
645
646 void
647 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
648 {
649     if (txn->comment.length) {
650         ds_put_char(&txn->comment, '\n');
651     }
652     ds_put_cstr(&txn->comment, s);
653 }
654
655 const char *
656 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
657 {
658     return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
659 }
660 \f
661 static void
662 ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
663 {
664     struct ovsdb_row *row = txn_row->old ? txn_row->old : txn_row->new;
665     struct ovsdb_txn_table *txn_table = row->table->txn_table;
666
667     txn_table->n_processed--;
668     hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
669
670     if (txn_row->old) {
671         txn_row->old->txn_row = NULL;
672     }
673     if (txn_row->new) {
674         txn_row->new->txn_row = NULL;
675     }
676 }
677
678 static void
679 ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
680 {
681     assert(hmap_is_empty(&txn_table->txn_rows));
682     txn_table->table->txn_table = NULL;
683     hmap_destroy(&txn_table->txn_rows);
684     list_remove(&txn_table->node);
685     free(txn_table);
686 }
687
688 /* Calls 'cb' for every txn_row within 'txn'.  If 'cb' returns nonnull, this
689  * aborts the iteration and for_each_txn_row() passes the error up.  Otherwise,
690  * returns a null pointer after iteration is complete.
691  *
692  * 'cb' may insert new txn_rows and new txn_tables into 'txn'.  It may delete
693  * the txn_row that it is passed in, or txn_rows in txn_tables other than the
694  * one passed to 'cb'.  It may *not* delete txn_rows other than the one passed
695  * in within the same txn_table.  It may *not* delete any txn_tables.  As long
696  * as these rules are followed, 'cb' will be called exactly once for each
697  * txn_row in 'txn', even those added by 'cb'.
698  */
699 static struct ovsdb_error * WARN_UNUSED_RESULT
700 for_each_txn_row(struct ovsdb_txn *txn,
701                  struct ovsdb_error *(*cb)(struct ovsdb_txn *,
702                                            struct ovsdb_txn_row *))
703 {
704     bool any_work;
705
706     serial++;
707
708     do {
709         struct ovsdb_txn_table *t, *next_txn_table;
710
711         any_work = false;
712         LIST_FOR_EACH_SAFE (t, next_txn_table, struct ovsdb_txn_table, node,
713                             &txn->txn_tables) {
714             if (t->serial != serial) {
715                 t->serial = serial;
716                 t->n_processed = 0;
717             }
718
719             while (t->n_processed < hmap_count(&t->txn_rows)) {
720                 struct ovsdb_txn_row *r, *next_txn_row;
721
722                 HMAP_FOR_EACH_SAFE (r, next_txn_row,
723                                     struct ovsdb_txn_row, hmap_node,
724                                     &t->txn_rows) {
725                     if (r->serial != serial) {
726                         struct ovsdb_error *error;
727
728                         r->serial = serial;
729                         t->n_processed++;
730                         any_work = true;
731
732                         error = cb(txn, r);
733                         if (error) {
734                             return error;
735                         }
736                     }
737                 }
738             }
739             if (hmap_is_empty(&t->txn_rows)) {
740                 /* Table is empty.  Drop it. */
741                 ovsdb_txn_table_destroy(t);
742             }
743         }
744     } while (any_work);
745
746     return NULL;
747 }