+static struct ovsdb_error * WARN_UNUSED_RESULT
+delete_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *row,
+ const struct ovsdb_base_type *base,
+ const union ovsdb_atom *atoms, unsigned int n)
+{
+ const struct ovsdb_table *table;
+ unsigned int i;
+
+ if (!ovsdb_base_type_is_strong_ref(base)) {
+ return NULL;
+ }
+
+ table = base->u.uuid.refTable;
+ for (i = 0; i < n; i++) {
+ const struct uuid *uuid = &atoms[i].uuid;
+ struct ovsdb_txn_row *txn_row;
+
+ if (uuid_equals(uuid, ovsdb_row_get_uuid(row))) {
+ /* Self-references don't count. */
+ continue;
+ }
+
+ txn_row = find_or_make_txn_row(txn, table, uuid);
+ if (!txn_row) {
+ return OVSDB_BUG("strong ref target missing");
+ } else if (!txn_row->n_refs) {
+ return OVSDB_BUG("strong ref target has zero n_refs");
+ } else if (!txn_row->new) {
+ return OVSDB_BUG("deleted strong ref target");
+ }
+
+ if (--txn_row->n_refs == 0) {
+ struct ovsdb_error *error = delete_garbage_row(txn, txn_row);
+ if (error) {
+ return error;
+ }
+ }
+ }
+
+ return NULL;
+}
+
+static struct ovsdb_error * WARN_UNUSED_RESULT
+delete_garbage_row(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
+{
+ struct shash_node *node;
+ struct ovsdb_row *row;
+
+ if (txn_row->table->schema->is_root) {
+ return NULL;
+ }
+
+ row = txn_row->new;
+ txn_row->new = NULL;
+ hmap_remove(&txn_row->table->rows, &row->hmap_node);
+ SHASH_FOR_EACH (node, &txn_row->table->schema->columns) {
+ const struct ovsdb_column *column = node->data;
+ const struct ovsdb_datum *field = &row->fields[column->index];
+ struct ovsdb_error *error;
+
+ error = delete_row_refs(txn, row,
+ &column->type.key, field->keys, field->n);
+ if (error) {
+ return error;
+ }
+
+ error = delete_row_refs(txn, row,
+ &column->type.value, field->values, field->n);
+ if (error) {
+ return error;
+ }
+ }
+ ovsdb_row_destroy(row);
+
+ return NULL;
+}
+
+static struct ovsdb_error * WARN_UNUSED_RESULT
+collect_garbage(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
+{
+ if (txn_row->new && !txn_row->n_refs) {
+ return delete_garbage_row(txn, txn_row);
+ }
+ return NULL;
+}
+