9e2dd5079923e996db8e12637f4ed50e8b5a343a
[sliver-openvswitch.git] / ovsdb / file.c
1 /* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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             ovsdb_log_unread(log);
219             break;
220         }
221
222         n_transactions++;
223         if (date < oldest_commit) {
224             oldest_commit = date;
225         }
226
227         error = ovsdb_txn_commit(txn, false);
228         if (error) {
229             ovsdb_log_unread(log);
230             break;
231         }
232     }
233     if (error) {
234         /* Log error but otherwise ignore it.  Probably the database just got
235          * truncated due to power failure etc. and we should use its current
236          * contents. */
237         char *msg = ovsdb_error_to_string(error);
238         VLOG_ERR("%s", msg);
239         free(msg);
240
241         ovsdb_error_destroy(error);
242     }
243
244     if (!read_only) {
245         struct ovsdb_file *file;
246
247         error = ovsdb_file_create(db, log, file_name, oldest_commit,
248                                   n_transactions, &file);
249         if (error) {
250             goto error;
251         }
252         if (filep) {
253             *filep = file;
254         }
255     } else {
256         ovsdb_log_close(log);
257     }
258
259     *dbp = db;
260     return NULL;
261
262 error:
263     *dbp = NULL;
264     if (filep) {
265         *filep = NULL;
266     }
267     ovsdb_destroy(db);
268     ovsdb_log_close(log);
269     return error;
270 }
271
272 static struct ovsdb_error *
273 ovsdb_file_update_row_from_json(struct ovsdb_row *row, bool converting,
274                                 const struct json *json)
275 {
276     struct ovsdb_table_schema *schema = row->table->schema;
277     struct ovsdb_error *error;
278     struct shash_node *node;
279
280     if (json->type != JSON_OBJECT) {
281         return ovsdb_syntax_error(json, NULL, "row must be JSON object");
282     }
283
284     SHASH_FOR_EACH (node, json_object(json)) {
285         const char *column_name = node->name;
286         const struct ovsdb_column *column;
287         struct ovsdb_datum datum;
288
289         column = ovsdb_table_schema_get_column(schema, column_name);
290         if (!column) {
291             if (converting) {
292                 continue;
293             }
294             return ovsdb_syntax_error(json, "unknown column",
295                                       "No column %s in table %s.",
296                                       column_name, schema->name);
297         }
298
299         error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
300         if (error) {
301             return error;
302         }
303         ovsdb_datum_swap(&row->fields[column->index], &datum);
304         ovsdb_datum_destroy(&datum, &column->type);
305     }
306
307     return NULL;
308 }
309
310 static struct ovsdb_error *
311 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
312                              bool converting,
313                              const struct uuid *row_uuid, struct json *json)
314 {
315     const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
316     if (json->type == JSON_NULL) {
317         if (!row) {
318             return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
319                                       "row "UUID_FMT" that does not exist",
320                                       UUID_ARGS(row_uuid));
321         }
322         ovsdb_txn_row_delete(txn, row);
323         return NULL;
324     } else if (row) {
325         return ovsdb_file_update_row_from_json(ovsdb_txn_row_modify(txn, row),
326                                                converting, json);
327     } else {
328         struct ovsdb_error *error;
329         struct ovsdb_row *new;
330
331         new = ovsdb_row_create(table);
332         *ovsdb_row_get_uuid_rw(new) = *row_uuid;
333         error = ovsdb_file_update_row_from_json(new, converting, json);
334         if (error) {
335             ovsdb_row_destroy(new);
336         } else {
337             ovsdb_txn_row_insert(txn, new);
338         }
339         return error;
340     }
341 }
342
343 static struct ovsdb_error *
344 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
345                                struct ovsdb_table *table,
346                                bool converting, struct json *json)
347 {
348     struct shash_node *node;
349
350     if (json->type != JSON_OBJECT) {
351         return ovsdb_syntax_error(json, NULL, "object expected");
352     }
353
354     SHASH_FOR_EACH (node, json->u.object) {
355         const char *uuid_string = node->name;
356         struct json *txn_row_json = node->data;
357         struct ovsdb_error *error;
358         struct uuid row_uuid;
359
360         if (!uuid_from_string(&row_uuid, uuid_string)) {
361             return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
362                                       uuid_string);
363         }
364
365         error = ovsdb_file_txn_row_from_json(txn, table, converting,
366                                              &row_uuid, txn_row_json);
367         if (error) {
368             return error;
369         }
370     }
371
372     return NULL;
373 }
374
375 /* Converts 'json' to an ovsdb_txn for 'db', storing the new transaction in
376  * '*txnp'.  Returns NULL if successful, otherwise an error.
377  *
378  * If 'converting' is true, then unknown table and column names are ignored
379  * (which can ease upgrading and downgrading schemas); otherwise, they are
380  * treated as errors.
381  *
382  * If successful, the date associated with the transaction, as the number of
383  * milliseconds since the epoch, is stored in '*date'.  If the transaction does
384  * not include a date, LLONG_MAX is stored. */
385 static struct ovsdb_error *
386 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
387                          bool converting, long long int *date,
388                          struct ovsdb_txn **txnp)
389 {
390     struct ovsdb_error *error;
391     struct shash_node *node;
392     struct ovsdb_txn *txn;
393
394     *txnp = NULL;
395     *date = LLONG_MAX;
396
397     if (json->type != JSON_OBJECT) {
398         return ovsdb_syntax_error(json, NULL, "object expected");
399     }
400
401     txn = ovsdb_txn_create(db);
402     SHASH_FOR_EACH (node, json->u.object) {
403         const char *table_name = node->name;
404         struct json *node_json = node->data;
405         struct ovsdb_table *table;
406
407         table = shash_find_data(&db->tables, table_name);
408         if (!table) {
409             if (!strcmp(table_name, "_date")
410                 && node_json->type == JSON_INTEGER) {
411                 *date = json_integer(node_json);
412                 continue;
413             } else if (!strcmp(table_name, "_comment") || converting) {
414                 continue;
415             }
416
417             error = ovsdb_syntax_error(json, "unknown table",
418                                        "No table named %s.", table_name);
419             goto error;
420         }
421
422         error = ovsdb_file_txn_table_from_json(txn, table, converting,
423                                                node_json);
424         if (error) {
425             goto error;
426         }
427     }
428     *txnp = txn;
429     return NULL;
430
431 error:
432     ovsdb_txn_abort(txn);
433     return error;
434 }
435
436 static struct ovsdb_error *
437 ovsdb_file_save_copy__(const char *file_name, int locking,
438                        const char *comment, const struct ovsdb *db,
439                        struct ovsdb_log **logp)
440 {
441     const struct shash_node *node;
442     struct ovsdb_file_txn ftxn;
443     struct ovsdb_error *error;
444     struct ovsdb_log *log;
445     struct json *json;
446
447     error = ovsdb_log_open(file_name, OVSDB_LOG_CREATE, locking, &log);
448     if (error) {
449         return error;
450     }
451
452     /* Write schema. */
453     json = ovsdb_schema_to_json(db->schema);
454     error = ovsdb_log_write(log, json);
455     json_destroy(json);
456     if (error) {
457         goto exit;
458     }
459
460     /* Write data. */
461     ovsdb_file_txn_init(&ftxn);
462     SHASH_FOR_EACH (node, &db->tables) {
463         const struct ovsdb_table *table = node->data;
464         const struct ovsdb_row *row;
465
466         HMAP_FOR_EACH (row, hmap_node, &table->rows) {
467             ovsdb_file_txn_add_row(&ftxn, NULL, row, NULL);
468         }
469     }
470     error = ovsdb_file_txn_commit(ftxn.json, comment, true, log);
471
472 exit:
473     if (logp) {
474         if (!error) {
475             *logp = log;
476             log = NULL;
477         } else {
478             *logp = NULL;
479         }
480     }
481     ovsdb_log_close(log);
482     if (error) {
483         remove(file_name);
484     }
485     return error;
486 }
487
488 /* Saves a snapshot of 'db''s current contents as 'file_name'.  If 'comment' is
489  * nonnull, then it is added along with the data contents and can be viewed
490  * with "ovsdb-tool show-log".
491  *
492  * 'locking' is passed along to ovsdb_log_open() untouched. */
493 struct ovsdb_error *
494 ovsdb_file_save_copy(const char *file_name, int locking,
495                      const char *comment, const struct ovsdb *db)
496 {
497     return ovsdb_file_save_copy__(file_name, locking, comment, db, NULL);
498 }
499
500 /* Opens database 'file_name', reads its schema, and closes it.  On success,
501  * stores the schema into '*schemap' and returns NULL; the caller then owns the
502  * schema.  On failure, returns an ovsdb_error (which the caller must destroy)
503  * and sets '*dbp' to NULL. */
504 struct ovsdb_error *
505 ovsdb_file_read_schema(const char *file_name, struct ovsdb_schema **schemap)
506 {
507     assert(schemap != NULL);
508     return ovsdb_file_open_log(file_name, OVSDB_LOG_READ_ONLY, NULL, schemap);
509 }
510 \f
511 /* Replica implementation. */
512
513 struct ovsdb_file {
514     struct ovsdb_replica replica;
515     struct ovsdb *db;
516     struct ovsdb_log *log;
517     char *file_name;
518     long long int oldest_commit;
519     long long int next_compact;
520     unsigned int n_transactions;
521 };
522
523 static const struct ovsdb_replica_class ovsdb_file_class;
524
525 static struct ovsdb_error *
526 ovsdb_file_create(struct ovsdb *db, struct ovsdb_log *log,
527                   const char *file_name,
528                   long long int oldest_commit,
529                   unsigned int n_transactions,
530                   struct ovsdb_file **filep)
531 {
532     long long int now = time_msec();
533     struct ovsdb_file *file;
534     char *deref_name;
535     char *abs_name;
536
537     /* Use the absolute name of the file because ovsdb-server opens its
538      * database before daemonize() chdirs to "/". */
539     deref_name = follow_symlinks(file_name);
540     abs_name = abs_file_name(NULL, deref_name);
541     free(deref_name);
542     if (!abs_name) {
543         *filep = NULL;
544         return ovsdb_io_error(0, "could not determine current "
545                               "working directory");
546     }
547
548     file = xmalloc(sizeof *file);
549     ovsdb_replica_init(&file->replica, &ovsdb_file_class);
550     file->db = db;
551     file->log = log;
552     file->file_name = abs_name;
553     file->oldest_commit = MIN(oldest_commit, now);
554     file->next_compact = file->oldest_commit + COMPACT_MIN_MSEC;
555     file->n_transactions = n_transactions;
556     ovsdb_add_replica(db, &file->replica);
557
558     *filep = file;
559     return NULL;
560 }
561
562 static struct ovsdb_file *
563 ovsdb_file_cast(struct ovsdb_replica *replica)
564 {
565     assert(replica->class == &ovsdb_file_class);
566     return CONTAINER_OF(replica, struct ovsdb_file, replica);
567 }
568
569 static bool
570 ovsdb_file_change_cb(const struct ovsdb_row *old,
571                      const struct ovsdb_row *new,
572                      const unsigned long int *changed,
573                      void *ftxn_)
574 {
575     struct ovsdb_file_txn *ftxn = ftxn_;
576     ovsdb_file_txn_add_row(ftxn, old, new, changed);
577     return true;
578 }
579
580 static struct ovsdb_error *
581 ovsdb_file_commit(struct ovsdb_replica *replica,
582                   const struct ovsdb_txn *txn, bool durable)
583 {
584     struct ovsdb_file *file = ovsdb_file_cast(replica);
585     struct ovsdb_file_txn ftxn;
586     struct ovsdb_error *error;
587
588     ovsdb_file_txn_init(&ftxn);
589     ovsdb_txn_for_each_change(txn, ovsdb_file_change_cb, &ftxn);
590     if (!ftxn.json) {
591         /* Nothing to commit. */
592         return NULL;
593     }
594
595     error = ovsdb_file_txn_commit(ftxn.json, ovsdb_txn_get_comment(txn),
596                                   durable, file->log);
597     if (error) {
598         return error;
599     }
600     file->n_transactions++;
601
602     /* If it has been at least COMPACT_MIN_MSEC millseconds since the last time
603      * we compacted (or at least COMPACT_RETRY_MSEC since the last time we
604      * tried), and if there are at least 100 transactions in the database, and
605      * if the database is at least 10 MB, then compact the database. */
606     if (time_msec() >= file->next_compact
607         && file->n_transactions >= 100
608         && ovsdb_log_get_offset(file->log) >= 10 * 1024 * 1024)
609     {
610         error = ovsdb_file_compact(file);
611         if (error) {
612             char *s = ovsdb_error_to_string(error);
613             ovsdb_error_destroy(error);
614             VLOG_WARN("%s: compacting database failed (%s), retrying in "
615                       "%d seconds",
616                       file->file_name, s, COMPACT_RETRY_MSEC / 1000);
617             free(s);
618
619             file->next_compact = time_msec() + COMPACT_RETRY_MSEC;
620         }
621     }
622
623     return NULL;
624 }
625
626 struct ovsdb_error *
627 ovsdb_file_compact(struct ovsdb_file *file)
628 {
629     struct ovsdb_log *new_log = NULL;
630     struct lockfile *tmp_lock = NULL;
631     struct ovsdb_error *error;
632     char *tmp_name = NULL;
633     char *comment = NULL;
634     int retval;
635
636     comment = xasprintf("compacting database online "
637                         "(%.3f seconds old, %u transactions, %llu bytes)",
638                         (time_msec() - file->oldest_commit) / 1000.0,
639                         file->n_transactions,
640                         (unsigned long long) ovsdb_log_get_offset(file->log));
641     VLOG_INFO("%s: %s", file->file_name, comment);
642
643     /* Commit the old version, so that we can be assured that we'll eventually
644      * have either the old or the new version. */
645     error = ovsdb_log_commit(file->log);
646     if (error) {
647         goto exit;
648     }
649
650     /* Lock temporary file. */
651     tmp_name = xasprintf("%s.tmp", file->file_name);
652     retval = lockfile_lock(tmp_name, 0, &tmp_lock);
653     if (retval) {
654         error = ovsdb_io_error(retval, "could not get lock on %s", tmp_name);
655         goto exit;
656     }
657
658     /* Remove temporary file.  (It might not exist.) */
659     if (unlink(tmp_name) < 0 && errno != ENOENT) {
660         error = ovsdb_io_error(errno, "failed to remove %s", tmp_name);
661         goto exit;
662     }
663
664     /* Save a copy. */
665     error = ovsdb_file_save_copy__(tmp_name, false, comment, file->db,
666                                    &new_log);
667     if (error) {
668         goto exit;
669     }
670
671     /* Replace original by temporary. */
672     if (rename(tmp_name, file->file_name)) {
673         error = ovsdb_io_error(errno, "failed to rename \"%s\" to \"%s\"",
674                                tmp_name, file->file_name);
675         goto exit;
676     }
677     fsync_parent_dir(file->file_name);
678
679 exit:
680     if (!error) {
681         ovsdb_log_close(file->log);
682         file->log = new_log;
683         file->oldest_commit = time_msec();
684         file->next_compact = file->oldest_commit + COMPACT_MIN_MSEC;
685         file->n_transactions = 1;
686     } else {
687         ovsdb_log_close(new_log);
688         if (tmp_lock) {
689             unlink(tmp_name);
690         }
691     }
692
693     lockfile_unlock(tmp_lock);
694     free(tmp_name);
695     free(comment);
696
697     return error;
698 }
699
700 static void
701 ovsdb_file_destroy(struct ovsdb_replica *replica)
702 {
703     struct ovsdb_file *file = ovsdb_file_cast(replica);
704
705     ovsdb_log_close(file->log);
706     free(file->file_name);
707     free(file);
708 }
709
710 static const struct ovsdb_replica_class ovsdb_file_class = {
711     ovsdb_file_commit,
712     ovsdb_file_destroy
713 };
714 \f
715 static void
716 ovsdb_file_txn_init(struct ovsdb_file_txn *ftxn)
717 {
718     ftxn->json = NULL;
719     ftxn->table_json = NULL;
720     ftxn->table = NULL;
721 }
722
723 static void
724 ovsdb_file_txn_add_row(struct ovsdb_file_txn *ftxn,
725                        const struct ovsdb_row *old,
726                        const struct ovsdb_row *new,
727                        const unsigned long int *changed)
728 {
729     struct json *row;
730
731     if (!new) {
732         row = json_null_create();
733     } else {
734         struct shash_node *node;
735
736         row = old ? NULL : json_object_create();
737         SHASH_FOR_EACH (node, &new->table->schema->columns) {
738             const struct ovsdb_column *column = node->data;
739             const struct ovsdb_type *type = &column->type;
740             unsigned int idx = column->index;
741
742             if (idx != OVSDB_COL_UUID && column->persistent
743                 && (old
744                     ? bitmap_is_set(changed, idx)
745                     : !ovsdb_datum_is_default(&new->fields[idx], type)))
746             {
747                 if (!row) {
748                     row = json_object_create();
749                 }
750                 json_object_put(row, column->name,
751                                 ovsdb_datum_to_json(&new->fields[idx], type));
752             }
753         }
754     }
755
756     if (row) {
757         struct ovsdb_table *table = new ? new->table : old->table;
758         char uuid[UUID_LEN + 1];
759
760         if (table != ftxn->table) {
761             /* Create JSON object for transaction overall. */
762             if (!ftxn->json) {
763                 ftxn->json = json_object_create();
764             }
765
766             /* Create JSON object for transaction on this table. */
767             ftxn->table_json = json_object_create();
768             ftxn->table = table;
769             json_object_put(ftxn->json, table->schema->name, ftxn->table_json);
770         }
771
772         /* Add row to transaction for this table. */
773         snprintf(uuid, sizeof uuid,
774                  UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
775         json_object_put(ftxn->table_json, uuid, row);
776     }
777 }
778
779 static struct ovsdb_error *
780 ovsdb_file_txn_commit(struct json *json, const char *comment,
781                       bool durable, struct ovsdb_log *log)
782 {
783     struct ovsdb_error *error;
784
785     if (!json) {
786         json = json_object_create();
787     }
788     if (comment) {
789         json_object_put_string(json, "_comment", comment);
790     }
791     json_object_put(json, "_date", json_integer_create(time_wall()));
792
793     error = ovsdb_log_write(log, json);
794     json_destroy(json);
795     if (error) {
796         return ovsdb_wrap_error(error, "writing transaction failed");
797     }
798
799     if (durable) {
800         error = ovsdb_log_commit(log);
801         if (error) {
802             return ovsdb_wrap_error(error, "committing transaction failed");
803         }
804     }
805
806     return NULL;
807 }