Merge "next" branch into "master".
[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 struct ovsdb_error *
445 ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
446 {
447     struct ovsdb_replica *replica;
448     struct ovsdb_error *error;
449
450     /* Figure out what actually changed, and abort early if the transaction
451      * was really a no-op. */
452     error = for_each_txn_row(txn, determine_changes);
453     if (error) {
454         ovsdb_error_destroy(error);
455         return OVSDB_BUG("can't happen");
456     }
457     if (list_is_empty(&txn->txn_tables)) {
458         ovsdb_txn_abort(txn);
459         return NULL;
460     }
461
462     /* Update reference counts and check referential integrity. */
463     error = update_ref_counts(txn);
464     if (error) {
465         ovsdb_txn_abort(txn);
466         return error;
467     }
468
469     /* Check reference counts and remove bad reference for "weak" referential
470      * integrity. */
471     error = for_each_txn_row(txn, assess_weak_refs);
472     if (error) {
473         ovsdb_txn_abort(txn);
474         return error;
475     }
476
477     /* Send the commit to each replica. */
478     LIST_FOR_EACH (replica, struct ovsdb_replica, node, &txn->db->replicas) {
479         error = (replica->class->commit)(replica, txn, durable);
480         if (error) {
481             /* We don't support two-phase commit so only the first replica is
482              * allowed to report an error. */
483             assert(&replica->node == txn->db->replicas.next);
484
485             ovsdb_txn_abort(txn);
486             return error;
487         }
488     }
489
490     /* Finalize commit. */
491     txn->db->run_triggers = true;
492     ovsdb_error_assert(for_each_txn_row(txn, ovsdb_txn_row_commit));
493     ovsdb_txn_free(txn);
494
495     return NULL;
496 }
497
498 void
499 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
500                           ovsdb_txn_row_cb_func *cb, void *aux)
501 {
502     struct ovsdb_txn_table *t;
503     struct ovsdb_txn_row *r;
504
505     LIST_FOR_EACH (t, struct ovsdb_txn_table, node, &txn->txn_tables) {
506         HMAP_FOR_EACH (r, struct ovsdb_txn_row, hmap_node, &t->txn_rows) {
507             if (!cb(r->old, r->new, r->changed, aux)) {
508                 break;
509             }
510         }
511    }
512 }
513
514 static struct ovsdb_txn_table *
515 ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
516 {
517     if (!table->txn_table) {
518         struct ovsdb_txn_table *txn_table;
519
520         table->txn_table = txn_table = xmalloc(sizeof *table->txn_table);
521         txn_table->table = table;
522         hmap_init(&txn_table->txn_rows);
523         txn_table->serial = serial - 1;
524         list_push_back(&txn->txn_tables, &txn_table->node);
525     }
526     return table->txn_table;
527 }
528
529 static struct ovsdb_txn_row *
530 ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
531                      const struct ovsdb_row *old_, struct ovsdb_row *new)
532 {
533     struct ovsdb_row *old = (struct ovsdb_row *) old_;
534     size_t n_columns = shash_count(&table->schema->columns);
535     struct ovsdb_txn_table *txn_table;
536     struct ovsdb_txn_row *txn_row;
537
538     txn_row = xzalloc(offsetof(struct ovsdb_txn_row, changed)
539                       + bitmap_n_bytes(n_columns));
540     txn_row->old = (struct ovsdb_row *) old;
541     txn_row->new = new;
542     txn_row->n_refs = old ? old->n_refs : 0;
543     txn_row->serial = serial - 1;
544
545     if (old) {
546         old->txn_row = txn_row;
547     }
548     if (new) {
549         new->txn_row = txn_row;
550     }
551
552     txn_table = ovsdb_txn_create_txn_table(txn, table);
553     hmap_insert(&txn_table->txn_rows, &txn_row->hmap_node,
554                 ovsdb_row_hash(old ? old : new));
555
556     return txn_row;
557 }
558
559 struct ovsdb_row *
560 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
561 {
562     struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
563
564     if (ro_row->txn_row) {
565         assert(ro_row == ro_row->txn_row->new);
566         return ro_row;
567     } else {
568         struct ovsdb_table *table = ro_row->table;
569         struct ovsdb_row *rw_row;
570
571         rw_row = ovsdb_row_clone(ro_row);
572         rw_row->n_refs = ro_row->n_refs;
573         uuid_generate(ovsdb_row_get_version_rw(rw_row));
574         ovsdb_txn_row_create(txn, table, ro_row, rw_row);
575         hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
576
577         return rw_row;
578     }
579 }
580
581 void
582 ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
583 {
584     uint32_t hash = ovsdb_row_hash(row);
585     struct ovsdb_table *table = row->table;
586
587     uuid_generate(ovsdb_row_get_version_rw(row));
588
589     ovsdb_txn_row_create(txn, table, NULL, row);
590     hmap_insert(&table->rows, &row->hmap_node, hash);
591 }
592
593 /* 'row' must be assumed destroyed upon return; the caller must not reference
594  * it again. */
595 void
596 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
597 {
598     struct ovsdb_row *row = (struct ovsdb_row *) row_;
599     struct ovsdb_table *table = row->table;
600     struct ovsdb_txn_row *txn_row = row->txn_row;
601
602     hmap_remove(&table->rows, &row->hmap_node);
603
604     if (!txn_row) {
605         ovsdb_txn_row_create(txn, table, row, NULL);
606     } else {
607         assert(txn_row->new == row);
608         if (txn_row->old) {
609             txn_row->new = NULL;
610         } else {
611             hmap_remove(&table->txn_table->txn_rows, &txn_row->hmap_node);
612             free(txn_row);
613         }
614         ovsdb_row_destroy(row);
615     }
616 }
617
618 void
619 ovsdb_txn_add_comment(struct ovsdb_txn *txn, const char *s)
620 {
621     if (txn->comment.length) {
622         ds_put_char(&txn->comment, '\n');
623     }
624     ds_put_cstr(&txn->comment, s);
625 }
626
627 const char *
628 ovsdb_txn_get_comment(const struct ovsdb_txn *txn)
629 {
630     return txn->comment.length ? ds_cstr_ro(&txn->comment) : NULL;
631 }
632 \f
633 static void
634 ovsdb_txn_row_prefree(struct ovsdb_txn_row *txn_row)
635 {
636     struct ovsdb_row *row = txn_row->old ? txn_row->old : txn_row->new;
637     struct ovsdb_txn_table *txn_table = row->table->txn_table;
638
639     txn_table->n_processed--;
640     hmap_remove(&txn_table->txn_rows, &txn_row->hmap_node);
641
642     if (txn_row->old) {
643         txn_row->old->txn_row = NULL;
644     }
645     if (txn_row->new) {
646         txn_row->new->txn_row = NULL;
647     }
648 }
649
650 static void
651 ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
652 {
653     assert(hmap_is_empty(&txn_table->txn_rows));
654     txn_table->table->txn_table = NULL;
655     hmap_destroy(&txn_table->txn_rows);
656     list_remove(&txn_table->node);
657     free(txn_table);
658 }
659
660 /* Calls 'cb' for every txn_row within 'txn'.  If 'cb' returns nonnull, this
661  * aborts the iteration and for_each_txn_row() passes the error up.  Otherwise,
662  * returns a null pointer after iteration is complete.
663  *
664  * 'cb' may insert new txn_rows and new txn_tables into 'txn'.  It may delete
665  * the txn_row that it is passed in, or txn_rows in txn_tables other than the
666  * one passed to 'cb'.  It may *not* delete txn_rows other than the one passed
667  * in within the same txn_table.  It may *not* delete any txn_tables.  As long
668  * as these rules are followed, 'cb' will be called exactly once for each
669  * txn_row in 'txn', even those added by 'cb'.
670  */
671 static struct ovsdb_error * WARN_UNUSED_RESULT
672 for_each_txn_row(struct ovsdb_txn *txn,
673                  struct ovsdb_error *(*cb)(struct ovsdb_txn *,
674                                            struct ovsdb_txn_row *))
675 {
676     bool any_work;
677
678     serial++;
679
680     do {
681         struct ovsdb_txn_table *t, *next_txn_table;
682
683         any_work = false;
684         LIST_FOR_EACH_SAFE (t, next_txn_table, struct ovsdb_txn_table, node,
685                             &txn->txn_tables) {
686             if (t->serial != serial) {
687                 t->serial = serial;
688                 t->n_processed = 0;
689             }
690
691             while (t->n_processed < hmap_count(&t->txn_rows)) {
692                 struct ovsdb_txn_row *r, *next_txn_row;
693
694                 HMAP_FOR_EACH_SAFE (r, next_txn_row,
695                                     struct ovsdb_txn_row, hmap_node,
696                                     &t->txn_rows) {
697                     if (r->serial != serial) {
698                         struct ovsdb_error *error;
699
700                         r->serial = serial;
701                         t->n_processed++;
702                         any_work = true;
703
704                         error = cb(txn, r);
705                         if (error) {
706                             return error;
707                         }
708                     }
709                 }
710             }
711             if (hmap_is_empty(&t->txn_rows)) {
712                 /* Table is empty.  Drop it. */
713                 ovsdb_txn_table_destroy(t);
714             }
715         }
716     } while (any_work);
717
718     return NULL;
719 }