1 /* Copyright (c) 2009, 2010 Nicira Networks
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include "ovsdb-error.h"
31 #include "transaction.h"
35 #define THIS_MODULE VLM_ovsdb_file
38 /* A transaction being converted to JSON for writing to a file. */
39 struct ovsdb_file_txn {
40 struct json *json; /* JSON for the whole transaction. */
41 struct json *table_json; /* JSON for 'table''s transaction. */
42 struct ovsdb_table *table; /* Table described in 'table_json'. */
45 static void ovsdb_file_txn_init(struct ovsdb_file_txn *);
46 static void ovsdb_file_txn_add_row(struct ovsdb_file_txn *,
47 const struct ovsdb_row *old,
48 const struct ovsdb_row *new);
49 static struct ovsdb_error *ovsdb_file_txn_commit(struct json *,
54 static struct ovsdb_error *ovsdb_file_open__(const char *file_name,
55 const struct ovsdb_schema *,
56 bool read_only, struct ovsdb **);
57 static struct ovsdb_error *ovsdb_file_txn_from_json(struct ovsdb *,
61 static void ovsdb_file_replica_create(struct ovsdb *, struct ovsdb_log *);
63 /* Opens database 'file_name' and stores a pointer to the new database in
64 * '*dbp'. If 'read_only' is false, then the database will be locked and
65 * changes to the database will be written to disk. If 'read_only' is true,
66 * the database will not be locked and changes to the database will persist
67 * only as long as the "struct ovsdb".
69 * On success, returns NULL. On failure, returns an ovsdb_error (which the
70 * caller must destroy) and sets '*dbp' to NULL. */
72 ovsdb_file_open(const char *file_name, bool read_only, struct ovsdb **dbp)
74 return ovsdb_file_open__(file_name, NULL, read_only, dbp);
77 /* Opens database 'file_name' with an alternate schema. The specified 'schema'
78 * is used to interpret the data in 'file_name', ignoring the schema actually
79 * stored in the file. Data in the file for tables or columns that do not
80 * exist in 'schema' are ignored, but the ovsdb file format must otherwise be
81 * observed, including column constraints.
83 * This function can be useful for upgrading or downgrading databases to
84 * "almost-compatible" formats.
86 * The database will not be locked. Changes to the database will persist only
87 * as long as the "struct ovsdb".
89 * On success, stores a pointer to the new database in '*dbp' and returns a
90 * null pointer. On failure, returns an ovsdb_error (which the caller must
91 * destroy) and sets '*dbp' to NULL. */
93 ovsdb_file_open_as_schema(const char *file_name,
94 const struct ovsdb_schema *schema,
97 return ovsdb_file_open__(file_name, schema, true, dbp);
100 static struct ovsdb_error *
101 ovsdb_file_open__(const char *file_name,
102 const struct ovsdb_schema *alternate_schema,
103 bool read_only, struct ovsdb **dbp)
105 enum ovsdb_log_open_mode open_mode;
106 struct ovsdb_schema *schema;
107 struct ovsdb_error *error;
108 struct ovsdb_log *log;
112 open_mode = read_only ? OVSDB_LOG_READ_ONLY : OVSDB_LOG_READ_WRITE;
113 error = ovsdb_log_open(file_name, open_mode, -1, &log);
118 error = ovsdb_log_read(log, &json);
122 return ovsdb_io_error(EOF, "%s: database file contains no schema",
126 if (alternate_schema) {
127 schema = ovsdb_schema_clone(alternate_schema);
129 error = ovsdb_schema_from_json(json, &schema);
132 return ovsdb_wrap_error(error,
133 "failed to parse \"%s\" as ovsdb schema",
139 db = ovsdb_create(schema);
140 while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
141 struct ovsdb_txn *txn;
143 error = ovsdb_file_txn_from_json(db, json, alternate_schema != NULL,
150 ovsdb_txn_commit(txn, false);
153 char *msg = ovsdb_error_to_string(error);
154 VLOG_WARN("%s", msg);
157 ovsdb_error_destroy(error);
161 ovsdb_file_replica_create(db, log);
163 ovsdb_log_close(log);
170 static struct ovsdb_error *
171 ovsdb_file_update_row_from_json(struct ovsdb_row *row, bool converting,
172 const struct json *json)
174 struct ovsdb_table_schema *schema = row->table->schema;
175 struct ovsdb_error *error;
176 struct shash_node *node;
178 if (json->type != JSON_OBJECT) {
179 return ovsdb_syntax_error(json, NULL, "row must be JSON object");
182 SHASH_FOR_EACH (node, json_object(json)) {
183 const char *column_name = node->name;
184 const struct ovsdb_column *column;
185 struct ovsdb_datum datum;
187 column = ovsdb_table_schema_get_column(schema, column_name);
192 return ovsdb_syntax_error(json, "unknown column",
193 "No column %s in table %s.",
194 column_name, schema->name);
197 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
201 ovsdb_datum_swap(&row->fields[column->index], &datum);
202 ovsdb_datum_destroy(&datum, &column->type);
208 static struct ovsdb_error *
209 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
211 const struct uuid *row_uuid, struct json *json)
213 const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
214 if (json->type == JSON_NULL) {
216 return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
217 "row "UUID_FMT" that does not exist",
218 UUID_ARGS(row_uuid));
220 ovsdb_txn_row_delete(txn, row);
223 return ovsdb_file_update_row_from_json(ovsdb_txn_row_modify(txn, row),
226 struct ovsdb_error *error;
227 struct ovsdb_row *new;
229 new = ovsdb_row_create(table);
230 *ovsdb_row_get_uuid_rw(new) = *row_uuid;
231 error = ovsdb_file_update_row_from_json(new, converting, json);
233 ovsdb_row_destroy(new);
236 ovsdb_txn_row_insert(txn, new);
242 static struct ovsdb_error *
243 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
244 struct ovsdb_table *table,
245 bool converting, struct json *json)
247 struct shash_node *node;
249 if (json->type != JSON_OBJECT) {
250 return ovsdb_syntax_error(json, NULL, "object expected");
253 SHASH_FOR_EACH (node, json->u.object) {
254 const char *uuid_string = node->name;
255 struct json *txn_row_json = node->data;
256 struct ovsdb_error *error;
257 struct uuid row_uuid;
259 if (!uuid_from_string(&row_uuid, uuid_string)) {
260 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
264 error = ovsdb_file_txn_row_from_json(txn, table, converting,
265 &row_uuid, txn_row_json);
274 static struct ovsdb_error *
275 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
276 bool converting, struct ovsdb_txn **txnp)
278 struct ovsdb_error *error;
279 struct shash_node *node;
280 struct ovsdb_txn *txn;
283 if (json->type != JSON_OBJECT) {
284 return ovsdb_syntax_error(json, NULL, "object expected");
287 txn = ovsdb_txn_create(db);
288 SHASH_FOR_EACH (node, json->u.object) {
289 const char *table_name = node->name;
290 struct json *txn_table_json = node->data;
291 struct ovsdb_table *table;
293 table = shash_find_data(&db->tables, table_name);
295 if (!strcmp(table_name, "_date")
296 || !strcmp(table_name, "_comment")
301 error = ovsdb_syntax_error(json, "unknown table",
302 "No table named %s.", table_name);
306 error = ovsdb_file_txn_table_from_json(txn, table, converting,
316 ovsdb_txn_abort(txn);
320 /* Saves a snapshot of 'db''s current contents as 'file_name'. If 'comment' is
321 * nonnull, then it is added along with the data contents and can be viewed
322 * with "ovsdb-tool show-log".
324 * 'locking' is passed along to ovsdb_log_open() untouched. */
326 ovsdb_file_save_copy(const char *file_name, int locking,
327 const char *comment, const struct ovsdb *db)
329 const struct shash_node *node;
330 struct ovsdb_file_txn ftxn;
331 struct ovsdb_error *error;
332 struct ovsdb_log *log;
335 error = ovsdb_log_open(file_name, OVSDB_LOG_CREATE, locking, &log);
341 json = ovsdb_schema_to_json(db->schema);
342 error = ovsdb_log_write(log, json);
349 ovsdb_file_txn_init(&ftxn);
350 SHASH_FOR_EACH (node, &db->tables) {
351 const struct ovsdb_table *table = node->data;
352 const struct ovsdb_row *row;
354 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
355 ovsdb_file_txn_add_row(&ftxn, NULL, row);
358 error = ovsdb_file_txn_commit(ftxn.json, comment, true, log);
361 ovsdb_log_close(log);
368 /* Replica implementation. */
370 struct ovsdb_file_replica {
371 struct ovsdb_replica replica;
372 struct ovsdb_log *log;
375 static const struct ovsdb_replica_class ovsdb_file_replica_class;
378 ovsdb_file_replica_create(struct ovsdb *db, struct ovsdb_log *log)
380 struct ovsdb_file_replica *r = xmalloc(sizeof *r);
381 ovsdb_replica_init(&r->replica, &ovsdb_file_replica_class);
383 ovsdb_add_replica(db, &r->replica);
387 static struct ovsdb_file_replica *
388 ovsdb_file_replica_cast(struct ovsdb_replica *replica)
390 assert(replica->class == &ovsdb_file_replica_class);
391 return CONTAINER_OF(replica, struct ovsdb_file_replica, replica);
395 ovsdb_file_replica_change_cb(const struct ovsdb_row *old,
396 const struct ovsdb_row *new,
399 struct ovsdb_file_txn *ftxn = ftxn_;
400 ovsdb_file_txn_add_row(ftxn, old, new);
404 static struct ovsdb_error *
405 ovsdb_file_replica_commit(struct ovsdb_replica *r_,
406 const struct ovsdb_txn *txn, bool durable)
408 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
409 struct ovsdb_file_txn ftxn;
411 ovsdb_file_txn_init(&ftxn);
412 ovsdb_txn_for_each_change(txn, ovsdb_file_replica_change_cb, &ftxn);
414 /* Nothing to commit. */
418 return ovsdb_file_txn_commit(ftxn.json, ovsdb_txn_get_comment(txn),
423 ovsdb_file_replica_destroy(struct ovsdb_replica *r_)
425 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
427 ovsdb_log_close(r->log);
431 static const struct ovsdb_replica_class ovsdb_file_replica_class = {
432 ovsdb_file_replica_commit,
433 ovsdb_file_replica_destroy
437 ovsdb_file_txn_init(struct ovsdb_file_txn *ftxn)
440 ftxn->table_json = NULL;
445 ovsdb_file_txn_add_row(struct ovsdb_file_txn *ftxn,
446 const struct ovsdb_row *old,
447 const struct ovsdb_row *new)
452 row = json_null_create();
454 struct shash_node *node;
456 row = old ? NULL : json_object_create();
457 SHASH_FOR_EACH (node, &new->table->schema->columns) {
458 const struct ovsdb_column *column = node->data;
459 const struct ovsdb_type *type = &column->type;
460 unsigned int idx = column->index;
462 if (idx != OVSDB_COL_UUID && column->persistent
464 ? !ovsdb_datum_equals(&old->fields[idx], &new->fields[idx],
466 : !ovsdb_datum_is_default(&new->fields[idx], type)))
469 row = json_object_create();
471 json_object_put(row, column->name,
472 ovsdb_datum_to_json(&new->fields[idx], type));
478 struct ovsdb_table *table = new ? new->table : old->table;
479 char uuid[UUID_LEN + 1];
481 if (table != ftxn->table) {
482 /* Create JSON object for transaction overall. */
484 ftxn->json = json_object_create();
487 /* Create JSON object for transaction on this table. */
488 ftxn->table_json = json_object_create();
490 json_object_put(ftxn->json, table->schema->name, ftxn->table_json);
493 /* Add row to transaction for this table. */
494 snprintf(uuid, sizeof uuid,
495 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
496 json_object_put(ftxn->table_json, uuid, row);
500 static struct ovsdb_error *
501 ovsdb_file_txn_commit(struct json *json, const char *comment,
502 bool durable, struct ovsdb_log *log)
504 struct ovsdb_error *error;
507 json = json_object_create();
510 json_object_put_string(json, "_comment", comment);
512 json_object_put(json, "_date", json_integer_create(time_now()));
514 error = ovsdb_log_write(log, json);
517 return ovsdb_wrap_error(error, "writing transaction failed");
521 error = ovsdb_log_commit(log);
523 return ovsdb_wrap_error(error, "committing transaction failed");