ovsdb: Check for changed columns only once per transaction commit.
[sliver-openvswitch.git] / ovsdb / file.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 "file.h"
19
20 #include <assert.h>
21 #include <fcntl.h>
22
23 #include "bitmap.h"
24 #include "column.h"
25 #include "log.h"
26 #include "json.h"
27 #include "ovsdb.h"
28 #include "ovsdb-error.h"
29 #include "row.h"
30 #include "table.h"
31 #include "timeval.h"
32 #include "transaction.h"
33 #include "uuid.h"
34 #include "util.h"
35
36 #define THIS_MODULE VLM_ovsdb_file
37 #include "vlog.h"
38
39 /* A transaction being converted to JSON for writing to a file. */
40 struct ovsdb_file_txn {
41     struct json *json;          /* JSON for the whole transaction. */
42     struct json *table_json;    /* JSON for 'table''s transaction. */
43     struct ovsdb_table *table;  /* Table described in 'table_json'.  */
44 };
45
46 static void ovsdb_file_txn_init(struct ovsdb_file_txn *);
47 static void ovsdb_file_txn_add_row(struct ovsdb_file_txn *,
48                                    const struct ovsdb_row *old,
49                                    const struct ovsdb_row *new,
50                                    const unsigned long int *changed);
51 static struct ovsdb_error *ovsdb_file_txn_commit(struct json *,
52                                                  const char *comment,
53                                                  bool durable,
54                                                  struct ovsdb_log *);
55
56 static struct ovsdb_error *ovsdb_file_open__(const char *file_name,
57                                              const struct ovsdb_schema *,
58                                              bool read_only, struct ovsdb **);
59 static struct ovsdb_error *ovsdb_file_txn_from_json(struct ovsdb *,
60                                                     const struct json *,
61                                                     bool converting,
62                                                     struct ovsdb_txn **);
63 static void ovsdb_file_replica_create(struct ovsdb *, struct ovsdb_log *);
64
65 /* Opens database 'file_name' and stores a pointer to the new database in
66  * '*dbp'.  If 'read_only' is false, then the database will be locked and
67  * changes to the database will be written to disk.  If 'read_only' is true,
68  * the database will not be locked and changes to the database will persist
69  * only as long as the "struct ovsdb".
70  *
71  * On success, returns NULL.  On failure, returns an ovsdb_error (which the
72  * caller must destroy) and sets '*dbp' to NULL. */
73 struct ovsdb_error *
74 ovsdb_file_open(const char *file_name, bool read_only, struct ovsdb **dbp)
75 {
76     return ovsdb_file_open__(file_name, NULL, read_only, dbp);
77 }
78
79 /* Opens database 'file_name' with an alternate schema.  The specified 'schema'
80  * is used to interpret the data in 'file_name', ignoring the schema actually
81  * stored in the file.  Data in the file for tables or columns that do not
82  * exist in 'schema' are ignored, but the ovsdb file format must otherwise be
83  * observed, including column constraints.
84  *
85  * This function can be useful for upgrading or downgrading databases to
86  * "almost-compatible" formats.
87  *
88  * The database will not be locked.  Changes to the database will persist only
89  * as long as the "struct ovsdb".
90  *
91  * On success, stores a pointer to the new database in '*dbp' and returns a
92  * null pointer.  On failure, returns an ovsdb_error (which the caller must
93  * destroy) and sets '*dbp' to NULL. */
94 struct ovsdb_error *
95 ovsdb_file_open_as_schema(const char *file_name,
96                           const struct ovsdb_schema *schema,
97                           struct ovsdb **dbp)
98 {
99     return ovsdb_file_open__(file_name, schema, true, dbp);
100 }
101
102 static struct ovsdb_error *
103 ovsdb_file_open__(const char *file_name,
104                   const struct ovsdb_schema *alternate_schema,
105                   bool read_only, struct ovsdb **dbp)
106 {
107     enum ovsdb_log_open_mode open_mode;
108     struct ovsdb_schema *schema;
109     struct ovsdb_error *error;
110     struct ovsdb_log *log;
111     struct json *json;
112     struct ovsdb *db;
113
114     open_mode = read_only ? OVSDB_LOG_READ_ONLY : OVSDB_LOG_READ_WRITE;
115     error = ovsdb_log_open(file_name, open_mode, -1, &log);
116     if (error) {
117         return error;
118     }
119
120     error = ovsdb_log_read(log, &json);
121     if (error) {
122         return error;
123     } else if (!json) {
124         return ovsdb_io_error(EOF, "%s: database file contains no schema",
125                               file_name);
126     }
127
128     if (alternate_schema) {
129         schema = ovsdb_schema_clone(alternate_schema);
130     } else {
131         error = ovsdb_schema_from_json(json, &schema);
132         if (error) {
133             json_destroy(json);
134             return ovsdb_wrap_error(error,
135                                     "failed to parse \"%s\" as ovsdb schema",
136                                     file_name);
137         }
138     }
139     json_destroy(json);
140
141     db = ovsdb_create(schema);
142     while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
143         struct ovsdb_txn *txn;
144
145         error = ovsdb_file_txn_from_json(db, json, alternate_schema != NULL,
146                                          &txn);
147         json_destroy(json);
148         if (error) {
149             break;
150         }
151
152         ovsdb_txn_commit(txn, false);
153     }
154     if (error) {
155         char *msg = ovsdb_error_to_string(error);
156         VLOG_WARN("%s", msg);
157         free(msg);
158
159         ovsdb_error_destroy(error);
160     }
161
162     if (!read_only) {
163         ovsdb_file_replica_create(db, log);
164     } else {
165         ovsdb_log_close(log);
166     }
167
168     *dbp = db;
169     return NULL;
170 }
171
172 static struct ovsdb_error *
173 ovsdb_file_update_row_from_json(struct ovsdb_row *row, bool converting,
174                                 const struct json *json)
175 {
176     struct ovsdb_table_schema *schema = row->table->schema;
177     struct ovsdb_error *error;
178     struct shash_node *node;
179
180     if (json->type != JSON_OBJECT) {
181         return ovsdb_syntax_error(json, NULL, "row must be JSON object");
182     }
183
184     SHASH_FOR_EACH (node, json_object(json)) {
185         const char *column_name = node->name;
186         const struct ovsdb_column *column;
187         struct ovsdb_datum datum;
188
189         column = ovsdb_table_schema_get_column(schema, column_name);
190         if (!column) {
191             if (converting) {
192                 continue;
193             }
194             return ovsdb_syntax_error(json, "unknown column",
195                                       "No column %s in table %s.",
196                                       column_name, schema->name);
197         }
198
199         error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
200         if (error) {
201             return error;
202         }
203         ovsdb_datum_swap(&row->fields[column->index], &datum);
204         ovsdb_datum_destroy(&datum, &column->type);
205     }
206
207     return NULL;
208 }
209
210 static struct ovsdb_error *
211 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
212                              bool converting,
213                              const struct uuid *row_uuid, struct json *json)
214 {
215     const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
216     if (json->type == JSON_NULL) {
217         if (!row) {
218             return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
219                                       "row "UUID_FMT" that does not exist",
220                                       UUID_ARGS(row_uuid));
221         }
222         ovsdb_txn_row_delete(txn, row);
223         return NULL;
224     } else if (row) {
225         return ovsdb_file_update_row_from_json(ovsdb_txn_row_modify(txn, row),
226                                                converting, json);
227     } else {
228         struct ovsdb_error *error;
229         struct ovsdb_row *new;
230
231         new = ovsdb_row_create(table);
232         *ovsdb_row_get_uuid_rw(new) = *row_uuid;
233         error = ovsdb_file_update_row_from_json(new, converting, json);
234         if (error) {
235             ovsdb_row_destroy(new);
236         }
237
238         ovsdb_txn_row_insert(txn, new);
239
240         return error;
241     }
242 }
243
244 static struct ovsdb_error *
245 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
246                                struct ovsdb_table *table,
247                                bool converting, struct json *json)
248 {
249     struct shash_node *node;
250
251     if (json->type != JSON_OBJECT) {
252         return ovsdb_syntax_error(json, NULL, "object expected");
253     }
254
255     SHASH_FOR_EACH (node, json->u.object) {
256         const char *uuid_string = node->name;
257         struct json *txn_row_json = node->data;
258         struct ovsdb_error *error;
259         struct uuid row_uuid;
260
261         if (!uuid_from_string(&row_uuid, uuid_string)) {
262             return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
263                                       uuid_string);
264         }
265
266         error = ovsdb_file_txn_row_from_json(txn, table, converting,
267                                              &row_uuid, txn_row_json);
268         if (error) {
269             return error;
270         }
271     }
272
273     return NULL;
274 }
275
276 static struct ovsdb_error *
277 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
278                          bool converting, struct ovsdb_txn **txnp)
279 {
280     struct ovsdb_error *error;
281     struct shash_node *node;
282     struct ovsdb_txn *txn;
283
284     *txnp = NULL;
285     if (json->type != JSON_OBJECT) {
286         return ovsdb_syntax_error(json, NULL, "object expected");
287     }
288
289     txn = ovsdb_txn_create(db);
290     SHASH_FOR_EACH (node, json->u.object) {
291         const char *table_name = node->name;
292         struct json *txn_table_json = node->data;
293         struct ovsdb_table *table;
294
295         table = shash_find_data(&db->tables, table_name);
296         if (!table) {
297             if (!strcmp(table_name, "_date")
298                 || !strcmp(table_name, "_comment")
299                 || converting) {
300                 continue;
301             }
302
303             error = ovsdb_syntax_error(json, "unknown table",
304                                        "No table named %s.", table_name);
305             goto error;
306         }
307
308         error = ovsdb_file_txn_table_from_json(txn, table, converting,
309                                                txn_table_json);
310         if (error) {
311             goto error;
312         }
313     }
314     *txnp = txn;
315     return NULL;
316
317 error:
318     ovsdb_txn_abort(txn);
319     return error;
320 }
321
322 /* Saves a snapshot of 'db''s current contents as 'file_name'.  If 'comment' is
323  * nonnull, then it is added along with the data contents and can be viewed
324  * with "ovsdb-tool show-log".
325  *
326  * 'locking' is passed along to ovsdb_log_open() untouched. */
327 struct ovsdb_error *
328 ovsdb_file_save_copy(const char *file_name, int locking,
329                      const char *comment, const struct ovsdb *db)
330 {
331     const struct shash_node *node;
332     struct ovsdb_file_txn ftxn;
333     struct ovsdb_error *error;
334     struct ovsdb_log *log;
335     struct json *json;
336
337     error = ovsdb_log_open(file_name, OVSDB_LOG_CREATE, locking, &log);
338     if (error) {
339         return error;
340     }
341
342     /* Write schema. */
343     json = ovsdb_schema_to_json(db->schema);
344     error = ovsdb_log_write(log, json);
345     json_destroy(json);
346     if (error) {
347         goto exit;
348     }
349
350     /* Write data. */
351     ovsdb_file_txn_init(&ftxn);
352     SHASH_FOR_EACH (node, &db->tables) {
353         const struct ovsdb_table *table = node->data;
354         const struct ovsdb_row *row;
355
356         HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
357             ovsdb_file_txn_add_row(&ftxn, NULL, row, NULL);
358         }
359     }
360     error = ovsdb_file_txn_commit(ftxn.json, comment, true, log);
361
362 exit:
363     ovsdb_log_close(log);
364     if (error) {
365         remove(file_name);
366     }
367     return error;
368 }
369 \f
370 /* Replica implementation. */
371
372 struct ovsdb_file_replica {
373     struct ovsdb_replica replica;
374     struct ovsdb_log *log;
375 };
376
377 static const struct ovsdb_replica_class ovsdb_file_replica_class;
378
379 static void
380 ovsdb_file_replica_create(struct ovsdb *db, struct ovsdb_log *log)
381 {
382     struct ovsdb_file_replica *r = xmalloc(sizeof *r);
383     ovsdb_replica_init(&r->replica, &ovsdb_file_replica_class);
384     r->log = log;
385     ovsdb_add_replica(db, &r->replica);
386
387 }
388
389 static struct ovsdb_file_replica *
390 ovsdb_file_replica_cast(struct ovsdb_replica *replica)
391 {
392     assert(replica->class == &ovsdb_file_replica_class);
393     return CONTAINER_OF(replica, struct ovsdb_file_replica, replica);
394 }
395
396 static bool
397 ovsdb_file_replica_change_cb(const struct ovsdb_row *old,
398                              const struct ovsdb_row *new,
399                              const unsigned long int *changed,
400                              void *ftxn_)
401 {
402     struct ovsdb_file_txn *ftxn = ftxn_;
403     ovsdb_file_txn_add_row(ftxn, old, new, changed);
404     return true;
405 }
406
407 static struct ovsdb_error *
408 ovsdb_file_replica_commit(struct ovsdb_replica *r_,
409                           const struct ovsdb_txn *txn, bool durable)
410 {
411     struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
412     struct ovsdb_file_txn ftxn;
413
414     ovsdb_file_txn_init(&ftxn);
415     ovsdb_txn_for_each_change(txn, ovsdb_file_replica_change_cb, &ftxn);
416     if (!ftxn.json) {
417         /* Nothing to commit. */
418         return NULL;
419     }
420
421     return ovsdb_file_txn_commit(ftxn.json, ovsdb_txn_get_comment(txn),
422                                  durable, r->log);
423 }
424
425 static void
426 ovsdb_file_replica_destroy(struct ovsdb_replica *r_)
427 {
428     struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
429
430     ovsdb_log_close(r->log);
431     free(r);
432 }
433
434 static const struct ovsdb_replica_class ovsdb_file_replica_class = {
435     ovsdb_file_replica_commit,
436     ovsdb_file_replica_destroy
437 };
438 \f
439 static void
440 ovsdb_file_txn_init(struct ovsdb_file_txn *ftxn)
441 {
442     ftxn->json = NULL;
443     ftxn->table_json = NULL;
444     ftxn->table = NULL;
445 }
446
447 static void
448 ovsdb_file_txn_add_row(struct ovsdb_file_txn *ftxn,
449                        const struct ovsdb_row *old,
450                        const struct ovsdb_row *new,
451                        const unsigned long int *changed)
452 {
453     struct json *row;
454
455     if (!new) {
456         row = json_null_create();
457     } else {
458         struct shash_node *node;
459
460         row = old ? NULL : json_object_create();
461         SHASH_FOR_EACH (node, &new->table->schema->columns) {
462             const struct ovsdb_column *column = node->data;
463             const struct ovsdb_type *type = &column->type;
464             unsigned int idx = column->index;
465
466             if (idx != OVSDB_COL_UUID && column->persistent
467                 && (old
468                     ? bitmap_is_set(changed, idx)
469                     : !ovsdb_datum_is_default(&new->fields[idx], type)))
470             {
471                 if (!row) {
472                     row = json_object_create();
473                 }
474                 json_object_put(row, column->name,
475                                 ovsdb_datum_to_json(&new->fields[idx], type));
476             }
477         }
478     }
479
480     if (row) {
481         struct ovsdb_table *table = new ? new->table : old->table;
482         char uuid[UUID_LEN + 1];
483
484         if (table != ftxn->table) {
485             /* Create JSON object for transaction overall. */
486             if (!ftxn->json) {
487                 ftxn->json = json_object_create();
488             }
489
490             /* Create JSON object for transaction on this table. */
491             ftxn->table_json = json_object_create();
492             ftxn->table = table;
493             json_object_put(ftxn->json, table->schema->name, ftxn->table_json);
494         }
495
496         /* Add row to transaction for this table. */
497         snprintf(uuid, sizeof uuid,
498                  UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
499         json_object_put(ftxn->table_json, uuid, row);
500     }
501 }
502
503 static struct ovsdb_error *
504 ovsdb_file_txn_commit(struct json *json, const char *comment,
505                       bool durable, struct ovsdb_log *log)
506 {
507     struct ovsdb_error *error;
508
509     if (!json) {
510         json = json_object_create();
511     }
512     if (comment) {
513         json_object_put_string(json, "_comment", comment);
514     }
515     json_object_put(json, "_date", json_integer_create(time_now()));
516
517     error = ovsdb_log_write(log, json);
518     json_destroy(json);
519     if (error) {
520         return ovsdb_wrap_error(error, "writing transaction failed");
521     }
522
523     if (durable) {
524         error = ovsdb_log_commit(log);
525         if (error) {
526             return ovsdb_wrap_error(error, "committing transaction failed");
527         }
528     }
529
530     return NULL;
531 }