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;
115 open_mode = read_only ? OVSDB_LOG_READ_ONLY : OVSDB_LOG_READ_WRITE;
116 error = ovsdb_log_open(file_name, open_mode, -1, &log);
121 error = ovsdb_log_read(log, &json);
125 return ovsdb_io_error(EOF, "%s: database file contains no schema",
129 if (alternate_schema) {
130 schema = ovsdb_schema_clone(alternate_schema);
132 error = ovsdb_schema_from_json(json, &schema);
135 return ovsdb_wrap_error(error,
136 "failed to parse \"%s\" as ovsdb schema",
142 db = ovsdb_create(schema);
143 while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
144 struct ovsdb_txn *txn;
146 error = ovsdb_file_txn_from_json(db, json, alternate_schema != NULL,
153 ovsdb_txn_commit(txn, false);
156 char *msg = ovsdb_error_to_string(error);
157 VLOG_WARN("%s", msg);
160 ovsdb_error_destroy(error);
164 ovsdb_file_replica_create(db, log);
166 ovsdb_log_close(log);
173 static struct ovsdb_error *
174 ovsdb_file_update_row_from_json(struct ovsdb_row *row, bool converting,
175 const struct json *json)
177 struct ovsdb_table_schema *schema = row->table->schema;
178 struct ovsdb_error *error;
179 struct shash_node *node;
181 if (json->type != JSON_OBJECT) {
182 return ovsdb_syntax_error(json, NULL, "row must be JSON object");
185 SHASH_FOR_EACH (node, json_object(json)) {
186 const char *column_name = node->name;
187 const struct ovsdb_column *column;
188 struct ovsdb_datum datum;
190 column = ovsdb_table_schema_get_column(schema, column_name);
195 return ovsdb_syntax_error(json, "unknown column",
196 "No column %s in table %s.",
197 column_name, schema->name);
200 error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
204 ovsdb_datum_swap(&row->fields[column->index], &datum);
205 ovsdb_datum_destroy(&datum, &column->type);
211 static struct ovsdb_error *
212 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
214 const struct uuid *row_uuid, struct json *json)
216 const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
217 if (json->type == JSON_NULL) {
219 return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
220 "row "UUID_FMT" that does not exist",
221 UUID_ARGS(row_uuid));
223 ovsdb_txn_row_delete(txn, row);
226 return ovsdb_file_update_row_from_json(ovsdb_txn_row_modify(txn, row),
229 struct ovsdb_error *error;
230 struct ovsdb_row *new;
232 new = ovsdb_row_create(table);
233 *ovsdb_row_get_uuid_rw(new) = *row_uuid;
234 error = ovsdb_file_update_row_from_json(new, converting, json);
236 ovsdb_row_destroy(new);
239 ovsdb_txn_row_insert(txn, new);
245 static struct ovsdb_error *
246 ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
247 struct ovsdb_table *table,
248 bool converting, struct json *json)
250 struct shash_node *node;
252 if (json->type != JSON_OBJECT) {
253 return ovsdb_syntax_error(json, NULL, "object expected");
256 SHASH_FOR_EACH (node, json->u.object) {
257 const char *uuid_string = node->name;
258 struct json *txn_row_json = node->data;
259 struct ovsdb_error *error;
260 struct uuid row_uuid;
262 if (!uuid_from_string(&row_uuid, uuid_string)) {
263 return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
267 error = ovsdb_file_txn_row_from_json(txn, table, converting,
268 &row_uuid, txn_row_json);
277 static struct ovsdb_error *
278 ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
279 bool converting, struct ovsdb_txn **txnp)
281 struct ovsdb_error *error;
282 struct shash_node *node;
283 struct ovsdb_txn *txn;
286 if (json->type != JSON_OBJECT) {
287 return ovsdb_syntax_error(json, NULL, "object expected");
290 txn = ovsdb_txn_create(db);
291 SHASH_FOR_EACH (node, json->u.object) {
292 const char *table_name = node->name;
293 struct json *txn_table_json = node->data;
294 struct ovsdb_table *table;
296 table = shash_find_data(&db->tables, table_name);
298 if (!strcmp(table_name, "_date")
299 || !strcmp(table_name, "_comment")
304 error = ovsdb_syntax_error(json, "unknown table",
305 "No table named %s.", table_name);
309 error = ovsdb_file_txn_table_from_json(txn, table, converting,
319 ovsdb_txn_abort(txn);
323 /* Saves a snapshot of 'db''s current contents as 'file_name'. If 'comment' is
324 * nonnull, then it is added along with the data contents and can be viewed
325 * with "ovsdb-tool show-log".
327 * 'locking' is passed along to ovsdb_log_open() untouched. */
329 ovsdb_file_save_copy(const char *file_name, int locking,
330 const char *comment, const struct ovsdb *db)
332 const struct shash_node *node;
333 struct ovsdb_file_txn ftxn;
334 struct ovsdb_error *error;
335 struct ovsdb_log *log;
338 error = ovsdb_log_open(file_name, OVSDB_LOG_CREATE, locking, &log);
344 json = ovsdb_schema_to_json(db->schema);
345 error = ovsdb_log_write(log, json);
352 ovsdb_file_txn_init(&ftxn);
353 SHASH_FOR_EACH (node, &db->tables) {
354 const struct ovsdb_table *table = node->data;
355 const struct ovsdb_row *row;
357 HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
358 ovsdb_file_txn_add_row(&ftxn, NULL, row, NULL);
361 error = ovsdb_file_txn_commit(ftxn.json, comment, true, log);
364 ovsdb_log_close(log);
371 /* Replica implementation. */
373 struct ovsdb_file_replica {
374 struct ovsdb_replica replica;
375 struct ovsdb_log *log;
378 static const struct ovsdb_replica_class ovsdb_file_replica_class;
381 ovsdb_file_replica_create(struct ovsdb *db, struct ovsdb_log *log)
383 struct ovsdb_file_replica *r = xmalloc(sizeof *r);
384 ovsdb_replica_init(&r->replica, &ovsdb_file_replica_class);
386 ovsdb_add_replica(db, &r->replica);
390 static struct ovsdb_file_replica *
391 ovsdb_file_replica_cast(struct ovsdb_replica *replica)
393 assert(replica->class == &ovsdb_file_replica_class);
394 return CONTAINER_OF(replica, struct ovsdb_file_replica, replica);
398 ovsdb_file_replica_change_cb(const struct ovsdb_row *old,
399 const struct ovsdb_row *new,
400 const unsigned long int *changed,
403 struct ovsdb_file_txn *ftxn = ftxn_;
404 ovsdb_file_txn_add_row(ftxn, old, new, changed);
408 static struct ovsdb_error *
409 ovsdb_file_replica_commit(struct ovsdb_replica *r_,
410 const struct ovsdb_txn *txn, bool durable)
412 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
413 struct ovsdb_file_txn ftxn;
415 ovsdb_file_txn_init(&ftxn);
416 ovsdb_txn_for_each_change(txn, ovsdb_file_replica_change_cb, &ftxn);
418 /* Nothing to commit. */
422 return ovsdb_file_txn_commit(ftxn.json, ovsdb_txn_get_comment(txn),
427 ovsdb_file_replica_destroy(struct ovsdb_replica *r_)
429 struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
431 ovsdb_log_close(r->log);
435 static const struct ovsdb_replica_class ovsdb_file_replica_class = {
436 ovsdb_file_replica_commit,
437 ovsdb_file_replica_destroy
441 ovsdb_file_txn_init(struct ovsdb_file_txn *ftxn)
444 ftxn->table_json = NULL;
449 ovsdb_file_txn_add_row(struct ovsdb_file_txn *ftxn,
450 const struct ovsdb_row *old,
451 const struct ovsdb_row *new,
452 const unsigned long int *changed)
457 row = json_null_create();
459 struct shash_node *node;
461 row = old ? NULL : json_object_create();
462 SHASH_FOR_EACH (node, &new->table->schema->columns) {
463 const struct ovsdb_column *column = node->data;
464 const struct ovsdb_type *type = &column->type;
465 unsigned int idx = column->index;
467 if (idx != OVSDB_COL_UUID && column->persistent
469 ? bitmap_is_set(changed, idx)
470 : !ovsdb_datum_is_default(&new->fields[idx], type)))
473 row = json_object_create();
475 json_object_put(row, column->name,
476 ovsdb_datum_to_json(&new->fields[idx], type));
482 struct ovsdb_table *table = new ? new->table : old->table;
483 char uuid[UUID_LEN + 1];
485 if (table != ftxn->table) {
486 /* Create JSON object for transaction overall. */
488 ftxn->json = json_object_create();
491 /* Create JSON object for transaction on this table. */
492 ftxn->table_json = json_object_create();
494 json_object_put(ftxn->json, table->schema->name, ftxn->table_json);
497 /* Add row to transaction for this table. */
498 snprintf(uuid, sizeof uuid,
499 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
500 json_object_put(ftxn->table_json, uuid, row);
504 static struct ovsdb_error *
505 ovsdb_file_txn_commit(struct json *json, const char *comment,
506 bool durable, struct ovsdb_log *log)
508 struct ovsdb_error *error;
511 json = json_object_create();
514 json_object_put_string(json, "_comment", comment);
516 json_object_put(json, "_date", json_integer_create(time_now()));
518 error = ovsdb_log_write(log, json);
521 return ovsdb_wrap_error(error, "writing transaction failed");
525 error = ovsdb_log_commit(log);
527 return ovsdb_wrap_error(error, "committing transaction failed");