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