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.
28 #include "ovsdb-error.h"
32 #include "transaction.h"
36 #define THIS_MODULE VLM_ovsdb_file
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'. */
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 *,
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 *,
63 static void ovsdb_file_replica_create(struct ovsdb *, struct ovsdb_log *);
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".
71 * On success, returns NULL. On failure, returns an ovsdb_error (which the
72 * caller must destroy) and sets '*dbp' to NULL. */
74 ovsdb_file_open(const char *file_name, bool read_only, struct ovsdb **dbp)
76 return ovsdb_file_open__(file_name, NULL, read_only, dbp);
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.
85 * This function can be useful for upgrading or downgrading databases to
86 * "almost-compatible" formats.
88 * The database will not be locked. Changes to the database will persist only
89 * as long as the "struct ovsdb".
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. */
95 ovsdb_file_open_as_schema(const char *file_name,
96 const struct ovsdb_schema *schema,
99 return ovsdb_file_open__(file_name, schema, true, dbp);
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)
107 enum ovsdb_log_open_mode open_mode;
108 struct ovsdb_schema *schema;
109 struct ovsdb_error *error;
110 struct ovsdb_log *log;
114 open_mode = read_only ? OVSDB_LOG_READ_ONLY : OVSDB_LOG_READ_WRITE;
115 error = ovsdb_log_open(file_name, open_mode, -1, &log);
120 error = ovsdb_log_read(log, &json);
124 return ovsdb_io_error(EOF, "%s: database file contains no schema",
128 if (alternate_schema) {
129 schema = ovsdb_schema_clone(alternate_schema);
131 error = ovsdb_schema_from_json(json, &schema);
134 return ovsdb_wrap_error(error,
135 "failed to parse \"%s\" as ovsdb schema",
141 db = ovsdb_create(schema);
142 while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
143 struct ovsdb_txn *txn;
145 error = ovsdb_file_txn_from_json(db, json, alternate_schema != NULL,
152 ovsdb_txn_commit(txn, false);
155 char *msg = ovsdb_error_to_string(error);
156 VLOG_WARN("%s", msg);
159 ovsdb_error_destroy(error);
163 ovsdb_file_replica_create(db, log);
165 ovsdb_log_close(log);
172 static struct ovsdb_error *
173 ovsdb_file_update_row_from_json(struct ovsdb_row *row, bool converting,
174 const struct json *json)
176 struct ovsdb_table_schema *schema = row->table->schema;
177 struct ovsdb_error *error;
178 struct shash_node *node;
180 if (json->type != JSON_OBJECT) {
181 return ovsdb_syntax_error(json, NULL, "row must be JSON object");
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;
189 column = ovsdb_table_schema_get_column(schema, column_name);
194 return ovsdb_syntax_error(json, "unknown column",
195 "No column %s in table %s.",
196 column_name, schema->name);
199 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
203 ovsdb_datum_swap(&row->fields[column->index], &datum);
204 ovsdb_datum_destroy(&datum, &column->type);
210 static struct ovsdb_error *
211 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
213 const struct uuid *row_uuid, struct json *json)
215 const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
216 if (json->type == JSON_NULL) {
218 return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
219 "row "UUID_FMT" that does not exist",
220 UUID_ARGS(row_uuid));
222 ovsdb_txn_row_delete(txn, row);
225 return ovsdb_file_update_row_from_json(ovsdb_txn_row_modify(txn, row),
228 struct ovsdb_error *error;
229 struct ovsdb_row *new;
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);
235 ovsdb_row_destroy(new);
238 ovsdb_txn_row_insert(txn, new);
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)
249 struct shash_node *node;
251 if (json->type != JSON_OBJECT) {
252 return ovsdb_syntax_error(json, NULL, "object expected");
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;
261 if (!uuid_from_string(&row_uuid, uuid_string)) {
262 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
266 error = ovsdb_file_txn_row_from_json(txn, table, converting,
267 &row_uuid, txn_row_json);
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)
280 struct ovsdb_error *error;
281 struct shash_node *node;
282 struct ovsdb_txn *txn;
285 if (json->type != JSON_OBJECT) {
286 return ovsdb_syntax_error(json, NULL, "object expected");
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;
295 table = shash_find_data(&db->tables, table_name);
297 if (!strcmp(table_name, "_date")
298 || !strcmp(table_name, "_comment")
303 error = ovsdb_syntax_error(json, "unknown table",
304 "No table named %s.", table_name);
308 error = ovsdb_file_txn_table_from_json(txn, table, converting,
318 ovsdb_txn_abort(txn);
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".
326 * 'locking' is passed along to ovsdb_log_open() untouched. */
328 ovsdb_file_save_copy(const char *file_name, int locking,
329 const char *comment, const struct ovsdb *db)
331 const struct shash_node *node;
332 struct ovsdb_file_txn ftxn;
333 struct ovsdb_error *error;
334 struct ovsdb_log *log;
337 error = ovsdb_log_open(file_name, OVSDB_LOG_CREATE, locking, &log);
343 json = ovsdb_schema_to_json(db->schema);
344 error = ovsdb_log_write(log, json);
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;
356 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
357 ovsdb_file_txn_add_row(&ftxn, NULL, row, NULL);
360 error = ovsdb_file_txn_commit(ftxn.json, comment, true, log);
363 ovsdb_log_close(log);
370 /* Replica implementation. */
372 struct ovsdb_file_replica {
373 struct ovsdb_replica replica;
374 struct ovsdb_log *log;
377 static const struct ovsdb_replica_class ovsdb_file_replica_class;
380 ovsdb_file_replica_create(struct ovsdb *db, struct ovsdb_log *log)
382 struct ovsdb_file_replica *r = xmalloc(sizeof *r);
383 ovsdb_replica_init(&r->replica, &ovsdb_file_replica_class);
385 ovsdb_add_replica(db, &r->replica);
389 static struct ovsdb_file_replica *
390 ovsdb_file_replica_cast(struct ovsdb_replica *replica)
392 assert(replica->class == &ovsdb_file_replica_class);
393 return CONTAINER_OF(replica, struct ovsdb_file_replica, replica);
397 ovsdb_file_replica_change_cb(const struct ovsdb_row *old,
398 const struct ovsdb_row *new,
399 const unsigned long int *changed,
402 struct ovsdb_file_txn *ftxn = ftxn_;
403 ovsdb_file_txn_add_row(ftxn, old, new, changed);
407 static struct ovsdb_error *
408 ovsdb_file_replica_commit(struct ovsdb_replica *r_,
409 const struct ovsdb_txn *txn, bool durable)
411 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
412 struct ovsdb_file_txn ftxn;
414 ovsdb_file_txn_init(&ftxn);
415 ovsdb_txn_for_each_change(txn, ovsdb_file_replica_change_cb, &ftxn);
417 /* Nothing to commit. */
421 return ovsdb_file_txn_commit(ftxn.json, ovsdb_txn_get_comment(txn),
426 ovsdb_file_replica_destroy(struct ovsdb_replica *r_)
428 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
430 ovsdb_log_close(r->log);
434 static const struct ovsdb_replica_class ovsdb_file_replica_class = {
435 ovsdb_file_replica_commit,
436 ovsdb_file_replica_destroy
440 ovsdb_file_txn_init(struct ovsdb_file_txn *ftxn)
443 ftxn->table_json = NULL;
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)
456 row = json_null_create();
458 struct shash_node *node;
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;
466 if (idx != OVSDB_COL_UUID && column->persistent
468 ? bitmap_is_set(changed, idx)
469 : !ovsdb_datum_is_default(&new->fields[idx], type)))
472 row = json_object_create();
474 json_object_put(row, column->name,
475 ovsdb_datum_to_json(&new->fields[idx], type));
481 struct ovsdb_table *table = new ? new->table : old->table;
482 char uuid[UUID_LEN + 1];
484 if (table != ftxn->table) {
485 /* Create JSON object for transaction overall. */
487 ftxn->json = json_object_create();
490 /* Create JSON object for transaction on this table. */
491 ftxn->table_json = json_object_create();
493 json_object_put(ftxn->json, table->schema->name, ftxn->table_json);
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);
503 static struct ovsdb_error *
504 ovsdb_file_txn_commit(struct json *json, const char *comment,
505 bool durable, struct ovsdb_log *log)
507 struct ovsdb_error *error;
510 json = json_object_create();
513 json_object_put_string(json, "_comment", comment);
515 json_object_put(json, "_date", json_integer_create(time_now()));
517 error = ovsdb_log_write(log, json);
520 return ovsdb_wrap_error(error, "writing transaction failed");
524 error = ovsdb_log_commit(log);
526 return ovsdb_wrap_error(error, "committing transaction failed");