summaryrefslogtreecommitdiff
path: root/ovsdb/file.c
blob: 65a535b33634b200665ed426a84707a60911e877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/* Copyright (c) 2009, 2010 Nicira Networks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

#include "file.h"

#include <assert.h>
#include <fcntl.h>

#include "column.h"
#include "log.h"
#include "json.h"
#include "ovsdb.h"
#include "ovsdb-error.h"
#include "row.h"
#include "table.h"
#include "timeval.h"
#include "transaction.h"
#include "uuid.h"
#include "util.h"

#define THIS_MODULE VLM_ovsdb_file
#include "vlog.h"

/* A transaction being converted to JSON for writing to a file. */
struct ovsdb_file_txn {
    struct json *json;          /* JSON for the whole transaction. */
    struct json *table_json;    /* JSON for 'table''s transaction. */
    struct ovsdb_table *table;  /* Table described in 'table_json'.  */
};

static void ovsdb_file_txn_init(struct ovsdb_file_txn *);
static void ovsdb_file_txn_add_row(struct ovsdb_file_txn *,
                                   const struct ovsdb_row *old,
                                   const struct ovsdb_row *new);
static struct ovsdb_error *ovsdb_file_txn_commit(struct json *,
                                                 const char *comment,
                                                 bool durable,
                                                 struct ovsdb_log *);

static struct ovsdb_error *ovsdb_file_open__(const char *file_name,
                                             const struct ovsdb_schema *,
                                             bool read_only, struct ovsdb **);
static struct ovsdb_error *ovsdb_file_txn_from_json(struct ovsdb *,
                                                    const struct json *,
                                                    bool converting,
                                                    struct ovsdb_txn **);
static void ovsdb_file_replica_create(struct ovsdb *, struct ovsdb_log *);

/* Opens database 'file_name' and stores a pointer to the new database in
 * '*dbp'.  If 'read_only' is false, then the database will be locked and
 * changes to the database will be written to disk.  If 'read_only' is true,
 * the database will not be locked and changes to the database will persist
 * only as long as the "struct ovsdb".
 *
 * On success, returns NULL.  On failure, returns an ovsdb_error (which the
 * caller must destroy) and sets '*dbp' to NULL. */
struct ovsdb_error *
ovsdb_file_open(const char *file_name, bool read_only, struct ovsdb **dbp)
{
    return ovsdb_file_open__(file_name, NULL, read_only, dbp);
}

/* Opens database 'file_name' with an alternate schema.  The specified 'schema'
 * is used to interpret the data in 'file_name', ignoring the schema actually
 * stored in the file.  Data in the file for tables or columns that do not
 * exist in 'schema' are ignored, but the ovsdb file format must otherwise be
 * observed, including column constraints.
 *
 * This function can be useful for upgrading or downgrading databases to
 * "almost-compatible" formats.
 *
 * The database will not be locked.  Changes to the database will persist only
 * as long as the "struct ovsdb".
 *
 * On success, stores a pointer to the new database in '*dbp' and returns a
 * null pointer.  On failure, returns an ovsdb_error (which the caller must
 * destroy) and sets '*dbp' to NULL. */
struct ovsdb_error *
ovsdb_file_open_as_schema(const char *file_name,
                          const struct ovsdb_schema *schema,
                          struct ovsdb **dbp)
{
    return ovsdb_file_open__(file_name, schema, true, dbp);
}

static struct ovsdb_error *
ovsdb_file_open__(const char *file_name,
                  const struct ovsdb_schema *alternate_schema,
                  bool read_only, struct ovsdb **dbp)
{
    enum ovsdb_log_open_mode open_mode;
    struct ovsdb_schema *schema;
    struct ovsdb_error *error;
    struct ovsdb_log *log;
    struct json *json;
    struct ovsdb *db;

    open_mode = read_only ? OVSDB_LOG_READ_ONLY : OVSDB_LOG_READ_WRITE;
    error = ovsdb_log_open(file_name, open_mode, -1, &log);
    if (error) {
        return error;
    }

    error = ovsdb_log_read(log, &json);
    if (error) {
        return error;
    } else if (!json) {
        return ovsdb_io_error(EOF, "%s: database file contains no schema",
                              file_name);
    }

    if (alternate_schema) {
        schema = ovsdb_schema_clone(alternate_schema);
    } else {
        error = ovsdb_schema_from_json(json, &schema);
        if (error) {
            json_destroy(json);
            return ovsdb_wrap_error(error,
                                    "failed to parse \"%s\" as ovsdb schema",
                                    file_name);
        }
    }
    json_destroy(json);

    db = ovsdb_create(schema);
    while ((error = ovsdb_log_read(log, &json)) == NULL && json) {
        struct ovsdb_txn *txn;

        error = ovsdb_file_txn_from_json(db, json, alternate_schema != NULL,
                                         &txn);
        json_destroy(json);
        if (error) {
            break;
        }

        ovsdb_txn_commit(txn, false);
    }
    if (error) {
        char *msg = ovsdb_error_to_string(error);
        VLOG_WARN("%s", msg);
        free(msg);

        ovsdb_error_destroy(error);
    }

    if (!read_only) {
        ovsdb_file_replica_create(db, log);
    } else {
        ovsdb_log_close(log);
    }

    *dbp = db;
    return NULL;
}

static struct ovsdb_error *
ovsdb_file_update_row_from_json(struct ovsdb_row *row, bool converting,
                                const struct json *json)
{
    struct ovsdb_table_schema *schema = row->table->schema;
    struct ovsdb_error *error;
    struct shash_node *node;

    if (json->type != JSON_OBJECT) {
        return ovsdb_syntax_error(json, NULL, "row must be JSON object");
    }

    SHASH_FOR_EACH (node, json_object(json)) {
        const char *column_name = node->name;
        const struct ovsdb_column *column;
        struct ovsdb_datum datum;

        column = ovsdb_table_schema_get_column(schema, column_name);
        if (!column) {
            if (converting) {
                continue;
            }
            return ovsdb_syntax_error(json, "unknown column",
                                      "No column %s in table %s.",
                                      column_name, schema->name);
        }

        error = ovsdb_datum_from_json(&datum, &column->type, node->data, NULL);
        if (error) {
            return error;
        }
        ovsdb_datum_swap(&row->fields[column->index], &datum);
        ovsdb_datum_destroy(&datum, &column->type);
    }

    return NULL;
}

static struct ovsdb_error *
ovsdb_file_txn_row_from_json(struct ovsdb_txn *txn, struct ovsdb_table *table,
                             bool converting,
                             const struct uuid *row_uuid, struct json *json)
{
    const struct ovsdb_row *row = ovsdb_table_get_row(table, row_uuid);
    if (json->type == JSON_NULL) {
        if (!row) {
            return ovsdb_syntax_error(NULL, NULL, "transaction deletes "
                                      "row "UUID_FMT" that does not exist",
                                      UUID_ARGS(row_uuid));
        }
        ovsdb_txn_row_delete(txn, row);
        return NULL;
    } else if (row) {
        return ovsdb_file_update_row_from_json(ovsdb_txn_row_modify(txn, row),
                                               converting, json);
    } else {
        struct ovsdb_error *error;
        struct ovsdb_row *new;

        new = ovsdb_row_create(table);
        *ovsdb_row_get_uuid_rw(new) = *row_uuid;
        error = ovsdb_file_update_row_from_json(new, converting, json);
        if (error) {
            ovsdb_row_destroy(new);
        }

        ovsdb_txn_row_insert(txn, new);

        return error;
    }
}

static struct ovsdb_error *
ovsdb_file_txn_table_from_json(struct ovsdb_txn *txn,
                               struct ovsdb_table *table,
                               bool converting, struct json *json)
{
    struct shash_node *node;

    if (json->type != JSON_OBJECT) {
        return ovsdb_syntax_error(json, NULL, "object expected");
    }

    SHASH_FOR_EACH (node, json->u.object) {
        const char *uuid_string = node->name;
        struct json *txn_row_json = node->data;
        struct ovsdb_error *error;
        struct uuid row_uuid;

        if (!uuid_from_string(&row_uuid, uuid_string)) {
            return ovsdb_syntax_error(json, NULL, "\"%s\" is not a valid UUID",
                                      uuid_string);
        }

        error = ovsdb_file_txn_row_from_json(txn, table, converting,
                                             &row_uuid, txn_row_json);
        if (error) {
            return error;
        }
    }

    return NULL;
}

static struct ovsdb_error *
ovsdb_file_txn_from_json(struct ovsdb *db, const struct json *json,
                         bool converting, struct ovsdb_txn **txnp)
{
    struct ovsdb_error *error;
    struct shash_node *node;
    struct ovsdb_txn *txn;

    *txnp = NULL;
    if (json->type != JSON_OBJECT) {
        return ovsdb_syntax_error(json, NULL, "object expected");
    }

    txn = ovsdb_txn_create(db);
    SHASH_FOR_EACH (node, json->u.object) {
        const char *table_name = node->name;
        struct json *txn_table_json = node->data;
        struct ovsdb_table *table;

        table = shash_find_data(&db->tables, table_name);
        if (!table) {
            if (!strcmp(table_name, "_date")
                || !strcmp(table_name, "_comment")
                || converting) {
                continue;
            }

            error = ovsdb_syntax_error(json, "unknown table",
                                       "No table named %s.", table_name);
            goto error;
        }

        error = ovsdb_file_txn_table_from_json(txn, table, converting,
                                               txn_table_json);
        if (error) {
            goto error;
        }
    }
    *txnp = txn;
    return NULL;

error:
    ovsdb_txn_abort(txn);
    return error;
}

/* Saves a snapshot of 'db''s current contents as 'file_name'.  If 'comment' is
 * nonnull, then it is added along with the data contents and can be viewed
 * with "ovsdb-tool show-log".
 *
 * 'locking' is passed along to ovsdb_log_open() untouched. */
struct ovsdb_error *
ovsdb_file_save_copy(const char *file_name, int locking,
                     const char *comment, const struct ovsdb *db)
{
    const struct shash_node *node;
    struct ovsdb_file_txn ftxn;
    struct ovsdb_error *error;
    struct ovsdb_log *log;
    struct json *json;

    error = ovsdb_log_open(file_name, OVSDB_LOG_CREATE, locking, &log);
    if (error) {
        return error;
    }

    /* Write schema. */
    json = ovsdb_schema_to_json(db->schema);
    error = ovsdb_log_write(log, json);
    json_destroy(json);
    if (error) {
        goto exit;
    }

    /* Write data. */
    ovsdb_file_txn_init(&ftxn);
    SHASH_FOR_EACH (node, &db->tables) {
        const struct ovsdb_table *table = node->data;
        const struct ovsdb_row *row;

        HMAP_FOR_EACH (row, struct ovsdb_row, hmap_node, &table->rows) {
            ovsdb_file_txn_add_row(&ftxn, NULL, row);
        }
    }
    error = ovsdb_file_txn_commit(ftxn.json, comment, true, log);

exit:
    ovsdb_log_close(log);
    if (error) {
        remove(file_name);
    }
    return error;
}

/* Replica implementation. */

struct ovsdb_file_replica {
    struct ovsdb_replica replica;
    struct ovsdb_log *log;
};

static const struct ovsdb_replica_class ovsdb_file_replica_class;

static void
ovsdb_file_replica_create(struct ovsdb *db, struct ovsdb_log *log)
{
    struct ovsdb_file_replica *r = xmalloc(sizeof *r);
    ovsdb_replica_init(&r->replica, &ovsdb_file_replica_class);
    r->log = log;
    ovsdb_add_replica(db, &r->replica);

}

static struct ovsdb_file_replica *
ovsdb_file_replica_cast(struct ovsdb_replica *replica)
{
    assert(replica->class == &ovsdb_file_replica_class);
    return CONTAINER_OF(replica, struct ovsdb_file_replica, replica);
}

static bool
ovsdb_file_replica_change_cb(const struct ovsdb_row *old,
                             const struct ovsdb_row *new,
                             void *ftxn_)
{
    struct ovsdb_file_txn *ftxn = ftxn_;
    ovsdb_file_txn_add_row(ftxn, old, new);
    return true;
}

static struct ovsdb_error *
ovsdb_file_replica_commit(struct ovsdb_replica *r_,
                          const struct ovsdb_txn *txn, bool durable)
{
    struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);
    struct ovsdb_file_txn ftxn;

    ovsdb_file_txn_init(&ftxn);
    ovsdb_txn_for_each_change(txn, ovsdb_file_replica_change_cb, &ftxn);
    if (!ftxn.json) {
        /* Nothing to commit. */
        return NULL;
    }

    return ovsdb_file_txn_commit(ftxn.json, ovsdb_txn_get_comment(txn),
                                 durable, r->log);
}

static void
ovsdb_file_replica_destroy(struct ovsdb_replica *r_)
{
    struct ovsdb_file_replica *r = ovsdb_file_replica_cast(r_);

    ovsdb_log_close(r->log);
    free(r);
}

static const struct ovsdb_replica_class ovsdb_file_replica_class = {
    ovsdb_file_replica_commit,
    ovsdb_file_replica_destroy
};

static void
ovsdb_file_txn_init(struct ovsdb_file_txn *ftxn)
{
    ftxn->json = NULL;
    ftxn->table_json = NULL;
    ftxn->table = NULL;
}

static void
ovsdb_file_txn_add_row(struct ovsdb_file_txn *ftxn,
                       const struct ovsdb_row *old,
                       const struct ovsdb_row *new)
{
    struct json *row;

    if (!new) {
        row = json_null_create();
    } else {
        struct shash_node *node;

        row = old ? NULL : json_object_create();
        SHASH_FOR_EACH (node, &new->table->schema->columns) {
            const struct ovsdb_column *column = node->data;
            const struct ovsdb_type *type = &column->type;
            unsigned int idx = column->index;

            if (idx != OVSDB_COL_UUID && column->persistent
                && (old
                    ? !ovsdb_datum_equals(&old->fields[idx], &new->fields[idx],
                                          type)
                    : !ovsdb_datum_is_default(&new->fields[idx], type)))
            {
                if (!row) {
                    row = json_object_create();
                }
                json_object_put(row, column->name,
                                ovsdb_datum_to_json(&new->fields[idx], type));
            }
        }
    }

    if (row) {
        struct ovsdb_table *table = new ? new->table : old->table;
        char uuid[UUID_LEN + 1];

        if (table != ftxn->table) {
            /* Create JSON object for transaction overall. */
            if (!ftxn->json) {
                ftxn->json = json_object_create();
            }

            /* Create JSON object for transaction on this table. */
            ftxn->table_json = json_object_create();
            ftxn->table = table;
            json_object_put(ftxn->json, table->schema->name, ftxn->table_json);
        }

        /* Add row to transaction for this table. */
        snprintf(uuid, sizeof uuid,
                 UUID_FMT, UUID_ARGS(ovsdb_row_get_uuid(new ? new : old)));
        json_object_put(ftxn->table_json, uuid, row);
    }
}

static struct ovsdb_error *
ovsdb_file_txn_commit(struct json *json, const char *comment,
                      bool durable, struct ovsdb_log *log)
{
    struct ovsdb_error *error;

    if (!json) {
        json = json_object_create();
    }
    if (comment) {
        json_object_put_string(json, "_comment", comment);
    }
    json_object_put(json, "_date", json_integer_create(time_now()));

    error = ovsdb_log_write(log, json);
    json_destroy(json);
    if (error) {
        return ovsdb_wrap_error(error, "writing transaction failed");
    }

    if (durable) {
        error = ovsdb_log_commit(log);
        if (error) {
            return ovsdb_wrap_error(error, "committing transaction failed");
        }
    }

    return NULL;
}