ovsdb: Rename ovsdb_file_replica to ovsdb_file.
[cascardo/ovs.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_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     *dbp = NULL;
115     open_mode = read_only ? OVSDB_LOG_READ_ONLY : OVSDB_LOG_READ_WRITE;
116     error = ovsdb_log_open(file_name, open_mode, -1, &log);
117     if (error) {
118         return error;
119     }
120
121     error = ovsdb_log_read(log, &json);
122     if (error) {
123         return error;
124     } else if (!json) {
125         return ovsdb_io_error(EOF, "%s: database file contains no schema",
126                               file_name);
127     }
128
129     if (alternate_schema) {
130         schema = ovsdb_schema_clone(alternate_schema);
131     } else {
132         error = ovsdb_schema_from_json(json, &schema);
133         if (error) {
134             json_destroy(json);
135             return ovsdb_wrap_error(error,
136                                     "failed to parse \"%s\" as ovsdb schema",
137                                     file_name);
138         }
139     }
140     json_destroy(json);
141
142     db = ovsdb_create(schema);
143     while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
144         struct ovsdb_txn *txn;
145
146         error = ovsdb_file_txn_from_json(db, json, alternate_schema != NULL,
147                                          &txn);
148         json_destroy(json);
149         if (error) {
150             break;
151         }
152
153         ovsdb_txn_commit(txn, false);
154     }
155     if (error) {
156         char *msg = ovsdb_error_to_string(error);
157         VLOG_WARN("%s", msg);
158         free(msg);
159
160         ovsdb_error_destroy(error);
161     }
162
163     if (!read_only) {
164         ovsdb_file_create(db, log);
165     } else {
166         ovsdb_log_close(log);
167     }
168
169     *dbp = db;
170     return NULL;
171 }
172
173 static struct ovsdb_error *
174 ovsdb_file_update_row_from_json(struct ovsdb_row *row, bool converting,
175                                 const struct json *json)
176 {
177     struct ovsdb_table_schema *schema = row->table->schema;
178     struct ovsdb_error *error;
179     struct shash_node *node;
180
181     if (json->type != JSON_OBJECT) {
182         return ovsdb_syntax_error(json, NULL, "row must be JSON object");
183     }
184
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;
189
190         column = ovsdb_table_schema_get_column(schema, column_name);
191         if (!column) {
192             if (converting) {
193                 continue;
194             }
195             return ovsdb_syntax_error(json, "unknown column",
196                                       "No column %s in table %s.",
197                                       column_name, schema->name);
198         }
199
200         error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
201         if (error) {
202             return error;
203         }
204         ovsdb_datum_swap(&row->fields[column->index], &datum);
205         ovsdb_datum_destroy(&datum, &column->type);
206     }
207
208     return NULL;
209 }
210
211 static struct ovsdb_error *
212 ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
213                              bool converting,
214                              const struct uuid *row_uuid, struct json *json)
215 {
216     const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
217     if (json->type == JSON_NULL) {
218         if (!row) {
219             return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
220                                       "row "UUID_FMT" that does not exist",
221                                       UUID_ARGS(row_uuid));
222         }
223         ovsdb_txn_row_delete(txn, row);
224         return NULL;
225     } else if (row) {
226         return ovsdb_file_update_row_from_json(ovsdb_txn_row_modify(txn, row),
227                                                converting, json);
228     } else {
229         struct ovsdb_error *error;
230         struct ovsdb_row *new;
231
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);
235         if (error) {
236             ovsdb_row_destroy(new);
237         }
238
239         ovsdb_txn_row_insert(txn, new);
240
241         return error;
242     }
243 }
244
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)
249 {
250     struct shash_node *node;
251
252     if (json->type != JSON_OBJECT) {
253         return ovsdb_syntax_error(json, NULL, "object expected");
254     }
255
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;
261
262         if (!uuid_from_string(&row_uuid, uuid_string)) {
263             return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
264                                       uuid_string);
265         }
266
267         error = ovsdb_file_txn_row_from_json(txn, table, converting,
268                                              &row_uuid, txn_row_json);
269         if (error) {
270             return error;
271         }
272     }
273
274     return NULL;
275 }
276
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)
280 {
281     struct ovsdb_error *error;
282     struct shash_node *node;
283     struct ovsdb_txn *txn;
284
285     *txnp = NULL;
286     if (json->type != JSON_OBJECT) {
287         return ovsdb_syntax_error(json, NULL, "object expected");
288     }
289
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;
295
296         table = shash_find_data(&db->tables, table_name);
297         if (!table) {
298             if (!strcmp(table_name, "_date")
299                 || !strcmp(table_name, "_comment")
300                 || converting) {
301                 continue;
302             }
303
304             error = ovsdb_syntax_error(json, "unknown table",
305                                        "No table named %s.", table_name);
306             goto error;
307         }
308
309         error = ovsdb_file_txn_table_from_json(txn, table, converting,
310                                                txn_table_json);
311         if (error) {
312             goto error;
313         }
314     }
315     *txnp = txn;
316     return NULL;
317
318 error:
319     ovsdb_txn_abort(txn);
320     return error;
321 }
322
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".
326  *
327  * 'locking' is passed along to ovsdb_log_open() untouched. */
328 struct ovsdb_error *
329 ovsdb_file_save_copy(const char *file_name, int locking,
330                      const char *comment, const struct ovsdb *db)
331 {
332     const struct shash_node *node;
333     struct ovsdb_file_txn ftxn;
334     struct ovsdb_error *error;
335     struct ovsdb_log *log;
336     struct json *json;
337
338     error = ovsdb_log_open(file_name, OVSDB_LOG_CREATE, locking, &log);
339     if (error) {
340         return error;
341     }
342
343     /* Write schema. */
344     json = ovsdb_schema_to_json(db->schema);
345     error = ovsdb_log_write(log, json);
346     json_destroy(json);
347     if (error) {
348         goto exit;
349     }
350
351     /* Write data. */
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;
356
357         HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
358             ovsdb_file_txn_add_row(&ftxn, NULL, row, NULL);
359         }
360     }
361     error = ovsdb_file_txn_commit(ftxn.json, comment, true, log);
362
363 exit:
364     ovsdb_log_close(log);
365     if (error) {
366         remove(file_name);
367     }
368     return error;
369 }
370 \f
371 /* Replica implementation. */
372
373 struct ovsdb_file {
374     struct ovsdb_replica replica;
375     struct ovsdb_log *log;
376 };
377
378 static const struct ovsdb_replica_class ovsdb_file_class;
379
380 static void
381 ovsdb_file_create(struct ovsdb *db, struct ovsdb_log *log)
382 {
383     struct ovsdb_file *file = xmalloc(sizeof *file);
384     ovsdb_replica_init(&file->replica, &ovsdb_file_class);
385     file->log = log;
386     ovsdb_add_replica(db, &file->replica);
387 }
388
389 static struct ovsdb_file *
390 ovsdb_file_cast(struct ovsdb_replica *replica)
391 {
392     assert(replica->class == &ovsdb_file_class);
393     return CONTAINER_OF(replica, struct ovsdb_file, replica);
394 }
395
396 static bool
397 ovsdb_file_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_commit(struct ovsdb_replica *replica,
409                   const struct ovsdb_txn *txn, bool durable)
410 {
411     struct ovsdb_file *file = ovsdb_file_cast(replica);
412     struct ovsdb_file_txn ftxn;
413
414     ovsdb_file_txn_init(&ftxn);
415     ovsdb_txn_for_each_change(txn, ovsdb_file_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, file->log);
423 }
424
425 static void
426 ovsdb_file_destroy(struct ovsdb_replica *replica)
427 {
428     struct ovsdb_file *file = ovsdb_file_cast(replica);
429
430     ovsdb_log_close(file->log);
431     free(file);
432 }
433
434 static const struct ovsdb_replica_class ovsdb_file_class = {
435     ovsdb_file_commit,
436     ovsdb_file_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 }