ovsdb: Raise database corruption log level from warning to error.
[sliver-openvswitch.git] / ovsdb / file.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 "file.h"
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24
25 #include "bitmap.h"
26 #include "column.h"
27 #include "log.h"
28 #include "json.h"
29 #include "lockfile.h"
30 #include "ovsdb.h"
31 #include "ovsdb-error.h"
32 #include "row.h"
33 #include "socket-util.h"
34 #include "table.h"
35 #include "timeval.h"
36 #include "transaction.h"
37 #include "uuid.h"
38 #include "util.h"
39 #include "vlog.h"
40
41 VLOG_DEFINE_THIS_MODULE(ovsdb_file);
42
43 /* Minimum number of milliseconds between database compactions. */
44 #define COMPACT_MIN_MSEC        (10 * 60 * 1000) /* 10 minutes. */
45
46 /* Minimum number of milliseconds between trying to compact the database if
47  * compacting fails. */
48 #define COMPACT_RETRY_MSEC      (60 * 1000)      /* 1 minute. */
49
50 /* A transaction being converted to JSON for writing to a file. */
51 struct ovsdb_file_txn {
52     struct json *json;          /* JSON for the whole transaction. */
53     struct json *table_json;    /* JSON for 'table''s transaction. */
54     struct ovsdb_table *table;  /* Table described in 'table_json'.  */
55 };
56
57 static void ovsdb_file_txn_init(struct ovsdb_file_txn *);
58 static void ovsdb_file_txn_add_row(struct ovsdb_file_txn *,
59                                    const struct ovsdb_row *old,
60                                    const struct ovsdb_row *new,
61                                    const unsigned long int *changed);
62 static struct ovsdb_error *ovsdb_file_txn_commit(struct json *,
63                                                  const char *comment,
64                                                  bool durable,
65                                                  struct ovsdb_log *);
66
67 static struct ovsdb_error *ovsdb_file_open__(const char *file_name,
68                                              const struct ovsdb_schema *,
69                                              bool read_only, struct ovsdb **,
70                                              struct ovsdb_file **);
71 static struct ovsdb_error *ovsdb_file_txn_from_json(
72     struct ovsdb *, const struct json *, bool converting,
73     long long int *date, struct ovsdb_txn **);
74 static struct ovsdb_error *ovsdb_file_create(struct ovsdb *,
75                                              struct ovsdb_log *,
76                                              const char *file_name,
77                                              long long int oldest_commit,
78                                              unsigned int n_transactions,
79                                              struct ovsdb_file **filep);
80
81 /* Opens database 'file_name' and stores a pointer to the new database in
82  * '*dbp'.  If 'read_only' is false, then the database will be locked and
83  * changes to the database will be written to disk.  If 'read_only' is true,
84  * the database will not be locked and changes to the database will persist
85  * only as long as the "struct ovsdb".
86  *
87  * If 'filep' is nonnull and 'read_only' is false, then on success sets
88  * '*filep' to an ovsdb_file that represents the open file.  This ovsdb_file
89  * persists until '*dbp' is destroyed.
90  *
91  * On success, returns NULL.  On failure, returns an ovsdb_error (which the
92  * caller must destroy) and sets '*dbp' and '*filep' to NULL. */
93 struct ovsdb_error *
94 ovsdb_file_open(const char *file_name, bool read_only,
95                 struct ovsdb **dbp, struct ovsdb_file **filep)
96 {
97     return ovsdb_file_open__(file_name, NULL, read_only, dbp, filep);
98 }
99
100 /* Opens database 'file_name' with an alternate schema.  The specified 'schema'
101  * is used to interpret the data in 'file_name', ignoring the schema actually
102  * stored in the file.  Data in the file for tables or columns that do not
103  * exist in 'schema' are ignored, but the ovsdb file format must otherwise be
104  * observed, including column constraints.
105  *
106  * This function can be useful for upgrading or downgrading databases to
107  * "almost-compatible" formats.
108  *
109  * The database will not be locked.  Changes to the database will persist only
110  * as long as the "struct ovsdb".
111  *
112  * On success, stores a pointer to the new database in '*dbp' and returns a
113  * null pointer.  On failure, returns an ovsdb_error (which the caller must
114  * destroy) and sets '*dbp' to NULL. */
115 struct ovsdb_error *
116 ovsdb_file_open_as_schema(const char *file_name,
117                           const struct ovsdb_schema *schema,
118                           struct ovsdb **dbp)
119 {
120     return ovsdb_file_open__(file_name, schema, true, dbp, NULL);
121 }
122
123 static struct ovsdb_error *
124 ovsdb_file_open_log(const char *file_name, enum ovsdb_log_open_mode open_mode,
125                     struct ovsdb_log **logp, struct ovsdb_schema **schemap)
126 {
127     struct ovsdb_schema *schema = NULL;
128     struct ovsdb_log *log = NULL;
129     struct ovsdb_error *error;
130     struct json *json = NULL;
131
132     assert(logp || schemap);
133
134     error = ovsdb_log_open(file_name, open_mode, -1, &log);
135     if (error) {
136         goto error;
137     }
138
139     error = ovsdb_log_read(log, &json);
140     if (error) {
141         goto error;
142     } else if (!json) {
143         error = ovsdb_io_error(EOF, "%s: database file contains no schema",
144                                file_name);
145         goto error;
146     }
147
148     if (schemap) {
149         error = ovsdb_schema_from_json(json, &schema);
150         if (error) {
151             error = ovsdb_wrap_error(error,
152                                      "failed to parse \"%s\" as ovsdb schema",
153                                      file_name);
154             goto error;
155         }
156     }
157     json_destroy(json);
158
159     if (logp) {
160         *logp = log;
161     } else {
162         ovsdb_log_close(log);
163     }
164     if (schemap) {
165         *schemap = schema;
166     }
167     return NULL;
168
169 error:
170     ovsdb_log_close(log);
171     json_destroy(json);
172     if (logp) {
173         *logp = NULL;
174     }
175     if (schemap) {
176         *schemap = NULL;
177     }
178     return error;
179 }
180
181 static struct ovsdb_error *
182 ovsdb_file_open__(const char *file_name,
183                   const struct ovsdb_schema *alternate_schema,
184                   bool read_only, struct ovsdb **dbp,
185                   struct ovsdb_file **filep)
186 {
187     enum ovsdb_log_open_mode open_mode;
188     long long int oldest_commit;
189     unsigned int n_transactions;
190     struct ovsdb_schema *schema = NULL;
191     struct ovsdb_error *error;
192     struct ovsdb_log *log;
193     struct json *json;
194     struct ovsdb *db = NULL;
195
196     /* In read-only mode there is no ovsdb_file so 'filep' must be null. */
197     assert(!(read_only && filep));
198
199     open_mode = read_only ? OVSDB_LOG_READ_ONLY : OVSDB_LOG_READ_WRITE;
200     error = ovsdb_file_open_log(file_name, open_mode, &log,
201                                 alternate_schema ? NULL : &schema);
202     if (error) {
203         goto error;
204     }
205
206     db = ovsdb_create(schema ? schema : ovsdb_schema_clone(alternate_schema));
207
208     oldest_commit = LLONG_MAX;
209     n_transactions = 0;
210     while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
211         struct ovsdb_txn *txn;
212         long long int date;
213
214         error = ovsdb_file_txn_from_json(db, json, alternate_schema != NULL,
215                                          &date, &txn);
216         json_destroy(json);
217         if (error) {
218             break;
219         }
220
221         n_transactions++;
222         if (date < oldest_commit) {
223             oldest_commit = date;
224         }
225
226         ovsdb_error_destroy(ovsdb_txn_commit(txn, false));
227     }
228     if (error) {
229         /* Log error but otherwise ignore it.  Probably the database just got
230          * truncated due to power failure etc. and we should use its current
231          * contents. */
232         char *msg = ovsdb_error_to_string(error);
233         VLOG_ERR("%s", msg);
234         free(msg);
235
236         ovsdb_error_destroy(error);
237     }
238
239     if (!read_only) {
240         struct ovsdb_file *file;
241
242         error = ovsdb_file_create(db, log, file_name, oldest_commit,
243                                   n_transactions, &file);
244         if (error) {
245             goto error;
246         }
247         if (filep) {
248             *filep = file;
249         }
250     } else {
251         ovsdb_log_close(log);
252     }
253
254     *dbp = db;
255     return NULL;
256
257 error:
258     *dbp = NULL;
259     if (filep) {
260         *filep = NULL;
261     }
262     ovsdb_destroy(db);
263     ovsdb_log_close(log);
264     return error;
265 }
266
267 static struct ovsdb_error *
268 ovsdb_file_update_row_from_json(struct ovsdb_row *row, bool converting,
269                                 const struct json *json)
270 {
271     struct ovsdb_table_schema *schema = row->table->schema;
272     struct ovsdb_error *error;
273     struct shash_node *node;
274
275     if (json->type != JSON_OBJECT) {
276         return ovsdb_syntax_error(json, NULL, "row must be JSON object");
277     }
278
279     SHASH_FOR_EACH (node, json_object(json)) {
280         const char *column_name = node->name;
281         const struct ovsdb_column *column;
282         struct ovsdb_datum datum;
283
284         column = ovsdb_table_schema_get_column(schema, column_name);
285         if (!column) {
286             if (converting) {
287                 continue;
288             }
289             return ovsdb_syntax_error(json, "unknown column",
290                                       "No column %s in table %s.",
291                                       column_name, schema->name);
292         }
293
294         error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
295         if (error) {
296             return error;
297         }
298         ovsdb_datum_swap(&row->fields[column->index], &datum);
299         ovsdb_datum_destroy(&datum, &column->type);
300     }
301
302     return NULL;
303 }
304
305 static struct ovsdb_error *
306 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
307                              bool converting,
308                              const struct uuid *row_uuid, struct json *json)
309 {
310     const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
311     if (json->type == JSON_NULL) {
312         if (!row) {
313             return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
314                                       "row "UUID_FMT" that does not exist",
315                                       UUID_ARGS(row_uuid));
316         }
317         ovsdb_txn_row_delete(txn, row);
318         return NULL;
319     } else if (row) {
320         return ovsdb_file_update_row_from_json(ovsdb_txn_row_modify(txn, row),
321                                                converting, json);
322     } else {
323         struct ovsdb_error *error;
324         struct ovsdb_row *new;
325
326         new = ovsdb_row_create(table);
327         *ovsdb_row_get_uuid_rw(new) = *row_uuid;
328         error = ovsdb_file_update_row_from_json(new, converting, json);
329         if (error) {
330             ovsdb_row_destroy(new);
331         }
332
333         ovsdb_txn_row_insert(txn, new);
334
335         return error;
336     }
337 }
338
339 static struct ovsdb_error *
340 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
341                                struct ovsdb_table *table,
342                                bool converting, struct json *json)
343 {
344     struct shash_node *node;
345
346     if (json->type != JSON_OBJECT) {
347         return ovsdb_syntax_error(json, NULL, "object expected");
348     }
349
350     SHASH_FOR_EACH (node, json->u.object) {
351         const char *uuid_string = node->name;
352         struct json *txn_row_json = node->data;
353         struct ovsdb_error *error;
354         struct uuid row_uuid;
355
356         if (!uuid_from_string(&row_uuid, uuid_string)) {
357             return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
358                                       uuid_string);
359         }
360
361         error = ovsdb_file_txn_row_from_json(txn, table, converting,
362                                              &row_uuid, txn_row_json);
363         if (error) {
364             return error;
365         }
366     }
367
368     return NULL;
369 }
370
371 /* Converts 'json' to an ovsdb_txn for 'db', storing the new transaction in
372  * '*txnp'.  Returns NULL if successful, otherwise an error.
373  *
374  * If 'converting' is true, then unknown table and column names are ignored
375  * (which can ease upgrading and downgrading schemas); otherwise, they are
376  * treated as errors.
377  *
378  * If successful, the date associated with the transaction, as the number of
379  * milliseconds since the epoch, is stored in '*date'.  If the transaction does
380  * not include a date, LLONG_MAX is stored. */
381 static struct ovsdb_error *
382 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
383                          bool converting, long long int *date,
384                          struct ovsdb_txn **txnp)
385 {
386     struct ovsdb_error *error;
387     struct shash_node *node;
388     struct ovsdb_txn *txn;
389
390     *txnp = NULL;
391     *date = LLONG_MAX;
392
393     if (json->type != JSON_OBJECT) {
394         return ovsdb_syntax_error(json, NULL, "object expected");
395     }
396
397     txn = ovsdb_txn_create(db);
398     SHASH_FOR_EACH (node, json->u.object) {
399         const char *table_name = node->name;
400         struct json *node_json = node->data;
401         struct ovsdb_table *table;
402
403         table = shash_find_data(&db->tables, table_name);
404         if (!table) {
405             if (!strcmp(table_name, "_date")
406                 && node_json->type == JSON_INTEGER) {
407                 *date = json_integer(node_json);
408                 continue;
409             } else if (!strcmp(table_name, "_comment") || converting) {
410                 continue;
411             }
412
413             error = ovsdb_syntax_error(json, "unknown table",
414                                        "No table named %s.", table_name);
415             goto error;
416         }
417
418         error = ovsdb_file_txn_table_from_json(txn, table, converting,
419                                                node_json);
420         if (error) {
421             goto error;
422         }
423     }
424     *txnp = txn;
425     return NULL;
426
427 error:
428     ovsdb_txn_abort(txn);
429     return error;
430 }
431
432 static struct ovsdb_error *
433 ovsdb_file_save_copy__(const char *file_name, int locking,
434                        const char *comment, const struct ovsdb *db,
435                        struct ovsdb_log **logp)
436 {
437     const struct shash_node *node;
438     struct ovsdb_file_txn ftxn;
439     struct ovsdb_error *error;
440     struct ovsdb_log *log;
441     struct json *json;
442
443     error = ovsdb_log_open(file_name, OVSDB_LOG_CREATE, locking, &log);
444     if (error) {
445         return error;
446     }
447
448     /* Write schema. */
449     json = ovsdb_schema_to_json(db->schema);
450     error = ovsdb_log_write(log, json);
451     json_destroy(json);
452     if (error) {
453         goto exit;
454     }
455
456     /* Write data. */
457     ovsdb_file_txn_init(&ftxn);
458     SHASH_FOR_EACH (node, &db->tables) {
459         const struct ovsdb_table *table = node->data;
460         const struct ovsdb_row *row;
461
462         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
463             ovsdb_file_txn_add_row(&ftxn, NULL, row, NULL);
464         }
465     }
466     error = ovsdb_file_txn_commit(ftxn.json, comment, true, log);
467
468 exit:
469     if (logp) {
470         if (!error) {
471             *logp = log;
472             log = NULL;
473         } else {
474             *logp = NULL;
475         }
476     }
477     ovsdb_log_close(log);
478     if (error) {
479         remove(file_name);
480     }
481     return error;
482 }
483
484 /* Saves a snapshot of 'db''s current contents as 'file_name'.  If 'comment' is
485  * nonnull, then it is added along with the data contents and can be viewed
486  * with "ovsdb-tool show-log".
487  *
488  * 'locking' is passed along to ovsdb_log_open() untouched. */
489 struct ovsdb_error *
490 ovsdb_file_save_copy(const char *file_name, int locking,
491                      const char *comment, const struct ovsdb *db)
492 {
493     return ovsdb_file_save_copy__(file_name, locking, comment, db, NULL);
494 }
495
496 /* Opens database 'file_name', reads its schema, and closes it.  On success,
497  * stores the schema into '*schemap' and returns NULL; the caller then owns the
498  * schema.  On failure, returns an ovsdb_error (which the caller must destroy)
499  * and sets '*dbp' to NULL. */
500 struct ovsdb_error *
501 ovsdb_file_read_schema(const char *file_name, struct ovsdb_schema **schemap)
502 {
503     assert(schemap != NULL);
504     return ovsdb_file_open_log(file_name, OVSDB_LOG_READ_ONLY, NULL, schemap);
505 }
506 \f
507 /* Replica implementation. */
508
509 struct ovsdb_file {
510     struct ovsdb_replica replica;
511     struct ovsdb *db;
512     struct ovsdb_log *log;
513     char *file_name;
514     long long int oldest_commit;
515     long long int next_compact;
516     unsigned int n_transactions;
517 };
518
519 static const struct ovsdb_replica_class ovsdb_file_class;
520
521 static struct ovsdb_error *
522 ovsdb_file_create(struct ovsdb *db, struct ovsdb_log *log,
523                   const char *file_name,
524                   long long int oldest_commit,
525                   unsigned int n_transactions,
526                   struct ovsdb_file **filep)
527 {
528     long long int now = time_msec();
529     struct ovsdb_file *file;
530     char *abs_name;
531
532     /* Use the absolute name of the file because ovsdb-server opens its
533      * database before daemonize() chdirs to "/". */
534     abs_name = abs_file_name(NULL, file_name);
535     if (!abs_name) {
536         *filep = NULL;
537         return ovsdb_io_error(0, "could not determine current "
538                               "working directory");
539     }
540
541     file = xmalloc(sizeof *file);
542     ovsdb_replica_init(&file->replica, &ovsdb_file_class);
543     file->db = db;
544     file->log = log;
545     file->file_name = abs_name;
546     file->oldest_commit = MIN(oldest_commit, now);
547     file->next_compact = file->oldest_commit + COMPACT_MIN_MSEC;
548     file->n_transactions = n_transactions;
549     ovsdb_add_replica(db, &file->replica);
550
551     *filep = file;
552     return NULL;
553 }
554
555 static struct ovsdb_file *
556 ovsdb_file_cast(struct ovsdb_replica *replica)
557 {
558     assert(replica->class == &ovsdb_file_class);
559     return CONTAINER_OF(replica, struct ovsdb_file, replica);
560 }
561
562 static bool
563 ovsdb_file_change_cb(const struct ovsdb_row *old,
564                      const struct ovsdb_row *new,
565                      const unsigned long int *changed,
566                      void *ftxn_)
567 {
568     struct ovsdb_file_txn *ftxn = ftxn_;
569     ovsdb_file_txn_add_row(ftxn, old, new, changed);
570     return true;
571 }
572
573 static struct ovsdb_error *
574 ovsdb_file_commit(struct ovsdb_replica *replica,
575                   const struct ovsdb_txn *txn, bool durable)
576 {
577     struct ovsdb_file *file = ovsdb_file_cast(replica);
578     struct ovsdb_file_txn ftxn;
579     struct ovsdb_error *error;
580
581     ovsdb_file_txn_init(&ftxn);
582     ovsdb_txn_for_each_change(txn, ovsdb_file_change_cb, &ftxn);
583     if (!ftxn.json) {
584         /* Nothing to commit. */
585         return NULL;
586     }
587
588     error = ovsdb_file_txn_commit(ftxn.json, ovsdb_txn_get_comment(txn),
589                                   durable, file->log);
590     if (error) {
591         return error;
592     }
593     file->n_transactions++;
594
595     /* If it has been at least COMPACT_MIN_MSEC millseconds since the last time
596      * we compacted (or at least COMPACT_RETRY_MSEC since the last time we
597      * tried), and if there are at least 100 transactions in the database, and
598      * if the database is at least 1 MB, then compact the database. */
599     if (time_msec() >= file->next_compact
600         && file->n_transactions >= 100
601         && ovsdb_log_get_offset(file->log) >= 10 * 1024 * 1024)
602     {
603         error = ovsdb_file_compact(file);
604         if (error) {
605             char *s = ovsdb_error_to_string(error);
606             ovsdb_error_destroy(error);
607             VLOG_WARN("%s: compacting database failed (%s), retrying in "
608                       "60 seconds", file->file_name, s);
609             free(s);
610
611             file->next_compact = time_msec() + COMPACT_RETRY_MSEC;
612         }
613     }
614
615     return NULL;
616 }
617
618 struct ovsdb_error *
619 ovsdb_file_compact(struct ovsdb_file *file)
620 {
621     struct ovsdb_log *new_log = NULL;
622     struct lockfile *tmp_lock = NULL;
623     struct ovsdb_error *error;
624     char *tmp_name = NULL;
625     char *comment = NULL;
626     int retval;
627
628     comment = xasprintf("compacting database online "
629                         "(%.3f seconds old, %u transactions, %llu bytes)",
630                         (time_msec() - file->oldest_commit) / 1000.0,
631                         file->n_transactions,
632                         (unsigned long long) ovsdb_log_get_offset(file->log));
633     VLOG_INFO("%s: %s", file->file_name, comment);
634
635     /* Commit the old version, so that we can be assured that we'll eventually
636      * have either the old or the new version. */
637     error = ovsdb_log_commit(file->log);
638     if (error) {
639         goto exit;
640     }
641
642     /* Lock temporary file. */
643     tmp_name = xasprintf("%s.tmp", file->file_name);
644     retval = lockfile_lock(tmp_name, 0, &tmp_lock);
645     if (retval) {
646         error = ovsdb_io_error(retval, "could not get lock on %s", tmp_name);
647         goto exit;
648     }
649
650     /* Remove temporary file.  (It might not exist.) */
651     if (unlink(tmp_name) < 0 && errno != ENOENT) {
652         error = ovsdb_io_error(errno, "failed to remove %s", tmp_name);
653         goto exit;
654     }
655
656     /* Save a copy. */
657     error = ovsdb_file_save_copy__(tmp_name, false, comment, file->db,
658                                    &new_log);
659     if (error) {
660         goto exit;
661     }
662
663     /* Replace original by temporary. */
664     if (rename(tmp_name, file->file_name)) {
665         error = ovsdb_io_error(errno, "failed to rename \"%s\" to \"%s\"",
666                                tmp_name, file->file_name);
667         goto exit;
668     }
669     fsync_parent_dir(file->file_name);
670
671 exit:
672     if (!error) {
673         ovsdb_log_close(file->log);
674         file->log = new_log;
675         file->oldest_commit = time_msec();
676         file->next_compact = file->oldest_commit + COMPACT_MIN_MSEC;
677         file->n_transactions = 1;
678     } else {
679         ovsdb_log_close(new_log);
680         if (tmp_lock) {
681             unlink(tmp_name);
682         }
683     }
684
685     lockfile_unlock(tmp_lock);
686     free(tmp_name);
687     free(comment);
688
689     return error;
690 }
691
692 static void
693 ovsdb_file_destroy(struct ovsdb_replica *replica)
694 {
695     struct ovsdb_file *file = ovsdb_file_cast(replica);
696
697     ovsdb_log_close(file->log);
698     free(file->file_name);
699     free(file);
700 }
701
702 static const struct ovsdb_replica_class ovsdb_file_class = {
703     ovsdb_file_commit,
704     ovsdb_file_destroy
705 };
706 \f
707 static void
708 ovsdb_file_txn_init(struct ovsdb_file_txn *ftxn)
709 {
710     ftxn->json = NULL;
711     ftxn->table_json = NULL;
712     ftxn->table = NULL;
713 }
714
715 static void
716 ovsdb_file_txn_add_row(struct ovsdb_file_txn *ftxn,
717                        const struct ovsdb_row *old,
718                        const struct ovsdb_row *new,
719                        const unsigned long int *changed)
720 {
721     struct json *row;
722
723     if (!new) {
724         row = json_null_create();
725     } else {
726         struct shash_node *node;
727
728         row = old ? NULL : json_object_create();
729         SHASH_FOR_EACH (node, &new->table->schema->columns) {
730             const struct ovsdb_column *column = node->data;
731             const struct ovsdb_type *type = &column->type;
732             unsigned int idx = column->index;
733
734             if (idx != OVSDB_COL_UUID && column->persistent
735                 && (old
736                     ? bitmap_is_set(changed, idx)
737                     : !ovsdb_datum_is_default(&new->fields[idx], type)))
738             {
739                 if (!row) {
740                     row = json_object_create();
741                 }
742                 json_object_put(row, column->name,
743                                 ovsdb_datum_to_json(&new->fields[idx], type));
744             }
745         }
746     }
747
748     if (row) {
749         struct ovsdb_table *table = new ? new->table : old->table;
750         char uuid[UUID_LEN + 1];
751
752         if (table != ftxn->table) {
753             /* Create JSON object for transaction overall. */
754             if (!ftxn->json) {
755                 ftxn->json = json_object_create();
756             }
757
758             /* Create JSON object for transaction on this table. */
759             ftxn->table_json = json_object_create();
760             ftxn->table = table;
761             json_object_put(ftxn->json, table->schema->name, ftxn->table_json);
762         }
763
764         /* Add row to transaction for this table. */
765         snprintf(uuid, sizeof uuid,
766                  UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
767         json_object_put(ftxn->table_json, uuid, row);
768     }
769 }
770
771 static struct ovsdb_error *
772 ovsdb_file_txn_commit(struct json *json, const char *comment,
773                       bool durable, struct ovsdb_log *log)
774 {
775     struct ovsdb_error *error;
776
777     if (!json) {
778         json = json_object_create();
779     }
780     if (comment) {
781         json_object_put_string(json, "_comment", comment);
782     }
783     json_object_put(json, "_date", json_integer_create(time_wall()));
784
785     error = ovsdb_log_write(log, json);
786     json_destroy(json);
787     if (error) {
788         return ovsdb_wrap_error(error, "writing transaction failed");
789     }
790
791     if (durable) {
792         error = ovsdb_log_commit(log);
793         if (error) {
794             return ovsdb_wrap_error(error, "committing transaction failed");
795         }
796     }
797
798     return NULL;
799 }