static int qemu_rbd_attempt_legacy_options(QDict *options, BlockdevOptionsRbd **opts, char **keypairs) { char *filename; int r; filename = g_strdup(qdict_get_try_str(options, "filename")); if (!filename) { return -EINVAL; } qdict_del(options, "filename"); qemu_rbd_parse_filename(filename, options, NULL); /* keypairs freed by caller */ *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); if (*keypairs) { qdict_del(options, "=keyvalue-pairs"); } r = qemu_rbd_convert_options(options, opts, NULL); g_free(filename); return r; }
Object *user_creatable_add(const QDict *qdict, Visitor *v, Error **errp) { char *type = NULL; char *id = NULL; Object *obj = NULL; Error *local_err = NULL; QDict *pdict; pdict = qdict_clone_shallow(qdict); visit_start_struct(v, NULL, NULL, 0, &local_err); if (local_err) { goto out; } qdict_del(pdict, "qom-type"); visit_type_str(v, "qom-type", &type, &local_err); if (local_err) { goto out_visit; } qdict_del(pdict, "id"); visit_type_str(v, "id", &id, &local_err); if (local_err) { goto out_visit; } visit_check_struct(v, &local_err); if (local_err) { goto out_visit; } obj = user_creatable_add_type(type, id, pdict, v, &local_err); out_visit: visit_end_struct(v, NULL); out: QDECREF(pdict); g_free(id); g_free(type); if (local_err) { error_propagate(errp, local_err); object_unref(obj); return NULL; } return obj; }
/* This function is hooked as final emit function, which can verify the correctness. */ static void event_test_emit(test_QAPIEvent event, QDict *d, Error **errp) { QObject *obj; QDict *t; int64_t s, ms; /* Verify that we have timestamp, then remove it to compare other fields */ obj = qdict_get(d, "timestamp"); g_assert(obj); t = qobject_to_qdict(obj); g_assert(t); obj = qdict_get(t, "seconds"); g_assert(obj && qobject_type(obj) == QTYPE_QINT); s = qint_get_int(qobject_to_qint(obj)); obj = qdict_get(t, "microseconds"); g_assert(obj && qobject_type(obj) == QTYPE_QINT); ms = qint_get_int(qobject_to_qint(obj)); if (s == -1) { g_assert(ms == -1); } else { g_assert(ms >= 0 && ms <= 999999); } g_assert(qdict_size(t) == 2); qdict_del(d, "timestamp"); g_assert(qdict_cmp_simple(d, test_event_data->expect)); }
static void test_qemu_opt_get_size(void) { QemuOptsList *list; QemuOpts *opts; uint64_t opt; QDict *dict; list = qemu_find_opts("opts_list_02"); g_assert(list != NULL); g_assert(QTAILQ_EMPTY(&list->head)); g_assert_cmpstr(list->name, ==, "opts_list_02"); /* should not find anything at this point */ opts = qemu_opts_find(list, NULL); g_assert(opts == NULL); /* create the opts */ opts = qemu_opts_create(list, NULL, 0, &error_abort); g_assert(opts != NULL); g_assert(!QTAILQ_EMPTY(&list->head)); /* haven't set anything to size1 yet, so defval should be returned */ opt = qemu_opt_get_size(opts, "size1", 5); g_assert(opt == 5); dict = qdict_new(); g_assert(dict != NULL); qdict_put(dict, "size1", qstring_from_str("10")); qemu_opts_absorb_qdict(opts, dict, &error_abort); g_assert(error_abort == NULL); /* now we have set size1, should know about it */ opt = qemu_opt_get_size(opts, "size1", 5); g_assert(opt == 10); /* reset value */ qdict_put(dict, "size1", qstring_from_str("15")); qemu_opts_absorb_qdict(opts, dict, &error_abort); g_assert(error_abort == NULL); /* test the reset value */ opt = qemu_opt_get_size(opts, "size1", 5); g_assert(opt == 15); qdict_del(dict, "size1"); g_free(dict); qemu_opts_del(opts); /* should not find anything at this point */ opts = qemu_opts_find(list, NULL); g_assert(opts == NULL); }
static void qdict_array_entries_test(void) { QDict *dict = qdict_new(); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0); qdict_put(dict, "bar", qint_from_int(0)); qdict_put(dict, "baz.0", qint_from_int(0)); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0); qdict_put(dict, "foo.1", qint_from_int(0)); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, -EINVAL); qdict_put(dict, "foo.0", qint_from_int(0)); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 2); qdict_put(dict, "foo.bar", qint_from_int(0)); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, -EINVAL); qdict_del(dict, "foo.bar"); qdict_put(dict, "foo.2.a", qint_from_int(0)); qdict_put(dict, "foo.2.b", qint_from_int(0)); qdict_put(dict, "foo.2.c", qint_from_int(0)); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 3); g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL); QDECREF(dict); dict = qdict_new(); qdict_put(dict, "1", qint_from_int(0)); g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL); qdict_put(dict, "0", qint_from_int(0)); g_assert_cmpint(qdict_array_entries(dict, ""), ==, 2); qdict_put(dict, "bar", qint_from_int(0)); g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL); qdict_del(dict, "bar"); qdict_put(dict, "2.a", qint_from_int(0)); qdict_put(dict, "2.b", qint_from_int(0)); qdict_put(dict, "2.c", qint_from_int(0)); g_assert_cmpint(qdict_array_entries(dict, ""), ==, 3); QDECREF(dict); }
static void qdict_array_entries_test(void) { QDict *dict = qdict_new(); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0); qdict_put_int(dict, "bar", 0); qdict_put_int(dict, "baz.0", 0); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 0); qdict_put_int(dict, "foo.1", 0); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, -EINVAL); qdict_put_int(dict, "foo.0", 0); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 2); qdict_put_int(dict, "foo.bar", 0); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, -EINVAL); qdict_del(dict, "foo.bar"); qdict_put_int(dict, "foo.2.a", 0); qdict_put_int(dict, "foo.2.b", 0); qdict_put_int(dict, "foo.2.c", 0); g_assert_cmpint(qdict_array_entries(dict, "foo."), ==, 3); g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL); qobject_unref(dict); dict = qdict_new(); qdict_put_int(dict, "1", 0); g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL); qdict_put_int(dict, "0", 0); g_assert_cmpint(qdict_array_entries(dict, ""), ==, 2); qdict_put_int(dict, "bar", 0); g_assert_cmpint(qdict_array_entries(dict, ""), ==, -EINVAL); qdict_del(dict, "bar"); qdict_put_int(dict, "2.a", 0); qdict_put_int(dict, "2.b", 0); qdict_put_int(dict, "2.c", 0); g_assert_cmpint(qdict_array_entries(dict, ""), ==, 3); qobject_unref(dict); }
static char *qemu_rbd_mon_host(QDict *options, Error **errp) { const char **vals = g_new(const char *, qdict_size(options) + 1); char keybuf[32]; const char *host, *port; char *rados_str; int i; for (i = 0;; i++) { sprintf(keybuf, "server.%d.host", i); host = qdict_get_try_str(options, keybuf); qdict_del(options, keybuf); sprintf(keybuf, "server.%d.port", i); port = qdict_get_try_str(options, keybuf); qdict_del(options, keybuf); if (!host && !port) { break; } if (!host) { error_setg(errp, "Parameter server.%d.host is missing", i); rados_str = NULL; goto out; } if (strchr(host, ':')) { vals[i] = port ? g_strdup_printf("[%s]:%s", host, port) : g_strdup_printf("[%s]", host); } else { vals[i] = port ? g_strdup_printf("%s:%s", host, port) : g_strdup(host); } } vals[i] = NULL; rados_str = i ? g_strjoinv(";", (char **)vals) : NULL; out: g_strfreev((char **)vals); return rados_str; }
static void qdict_del_test(void) { const char *key = "key test"; QDict *tests_dict = qdict_new(); qdict_put(tests_dict, key, qstring_from_str("foo")); g_assert(qdict_size(tests_dict) == 1); qdict_del(tests_dict, key); g_assert(qdict_size(tests_dict) == 0); g_assert(qdict_haskey(tests_dict, key) == 0); QDECREF(tests_dict); }
/* This function is hooked as final emit function, which can verify the correctness. */ static void event_test_emit(test_QAPIEvent event, QDict *d, Error **errp) { QDict *t; int64_t s, ms; /* Verify that we have timestamp, then remove it to compare other fields */ t = qdict_get_qdict(d, "timestamp"); g_assert(t); s = qdict_get_try_int(t, "seconds", -2); ms = qdict_get_try_int(t, "microseconds", -2); if (s == -1) { g_assert(ms == -1); } else { g_assert(s >= 0); g_assert(ms >= 0 && ms <= 999999); } g_assert(qdict_size(t) == 2); qdict_del(d, "timestamp"); g_assert(qdict_cmp_simple(d, test_event_data->expect)); }
static int connect_to_ssh(BDRVSSHState *s, QDict *options, int ssh_flags, int creat_mode, Error **errp) { int r, ret; const char *host, *user, *path, *host_key_check; int port; if (!qdict_haskey(options, "host")) { ret = -EINVAL; error_setg(errp, "No hostname was specified"); goto err; } host = qdict_get_str(options, "host"); if (qdict_haskey(options, "port")) { port = qdict_get_int(options, "port"); } else { port = 22; } if (!qdict_haskey(options, "path")) { ret = -EINVAL; error_setg(errp, "No path was specified"); goto err; } path = qdict_get_str(options, "path"); if (qdict_haskey(options, "user")) { user = qdict_get_str(options, "user"); } else { user = g_get_user_name(); if (!user) { error_setg_errno(errp, errno, "Can't get user name"); ret = -errno; goto err; } } if (qdict_haskey(options, "host_key_check")) { host_key_check = qdict_get_str(options, "host_key_check"); } else { host_key_check = "yes"; } /* Construct the host:port name for inet_connect. */ g_free(s->hostport); s->hostport = g_strdup_printf("%s:%d", host, port); /* Open the socket and connect. */ s->sock = inet_connect(s->hostport, errp); if (s->sock < 0) { ret = -errno; goto err; } /* Create SSH session. */ s->session = libssh2_session_init(); if (!s->session) { ret = -EINVAL; session_error_setg(errp, s, "failed to initialize libssh2 session"); goto err; } #if TRACE_LIBSSH2 != 0 libssh2_trace(s->session, TRACE_LIBSSH2); #endif r = libssh2_session_handshake(s->session, s->sock); if (r != 0) { ret = -EINVAL; session_error_setg(errp, s, "failed to establish SSH session"); goto err; } /* Check the remote host's key against known_hosts. */ ret = check_host_key(s, host, port, host_key_check, errp); if (ret < 0) { goto err; } /* Authenticate. */ ret = authenticate(s, user, errp); if (ret < 0) { goto err; } /* Start SFTP. */ s->sftp = libssh2_sftp_init(s->session); if (!s->sftp) { session_error_setg(errp, s, "failed to initialize sftp handle"); ret = -EINVAL; goto err; } /* Open the remote file. */ DPRINTF("opening file %s flags=0x%x creat_mode=0%o", path, ssh_flags, creat_mode); s->sftp_handle = libssh2_sftp_open(s->sftp, path, ssh_flags, creat_mode); if (!s->sftp_handle) { session_error_setg(errp, s, "failed to open remote file '%s'", path); ret = -EINVAL; goto err; } r = libssh2_sftp_fstat(s->sftp_handle, &s->attrs); if (r < 0) { sftp_error_setg(errp, s, "failed to read file attributes"); return -EINVAL; } /* Delete the options we've used; any not deleted will cause the * block layer to give an error about unused options. */ qdict_del(options, "host"); qdict_del(options, "port"); qdict_del(options, "user"); qdict_del(options, "path"); qdict_del(options, "host_key_check"); return 0; err: if (s->sftp_handle) { libssh2_sftp_close(s->sftp_handle); } s->sftp_handle = NULL; if (s->sftp) { libssh2_sftp_shutdown(s->sftp); } s->sftp = NULL; if (s->session) { libssh2_session_disconnect(s->session, "from qemu ssh client: " "error opening connection"); libssh2_session_free(s->session); } s->session = NULL; return ret; }
static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) { BDRVRBDState *s = bs->opaque; BlockdevOptionsRbd *opts = NULL; const QDictEntry *e; Error *local_err = NULL; char *keypairs, *secretid; int r; keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); if (keypairs) { qdict_del(options, "=keyvalue-pairs"); } secretid = g_strdup(qdict_get_try_str(options, "password-secret")); if (secretid) { qdict_del(options, "password-secret"); } r = qemu_rbd_convert_options(options, &opts, &local_err); if (local_err) { /* If keypairs are present, that means some options are present in * the modern option format. Don't attempt to parse legacy option * formats, as we won't support mixed usage. */ if (keypairs) { error_propagate(errp, local_err); goto out; } /* If the initial attempt to convert and process the options failed, * we may be attempting to open an image file that has the rbd options * specified in the older format consisting of all key/value pairs * encoded in the filename. Go ahead and attempt to parse the * filename, and see if we can pull out the required options. */ r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs); if (r < 0) { /* Propagate the original error, not the legacy parsing fallback * error, as the latter was just a best-effort attempt. */ error_propagate(errp, local_err); goto out; } /* Take care whenever deciding to actually deprecate; once this ability * is removed, we will not be able to open any images with legacy-styled * backing image strings. */ warn_report("RBD options encoded in the filename as keyvalue pairs " "is deprecated"); } /* Remove the processed options from the QDict (the visitor processes * _all_ options in the QDict) */ while ((e = qdict_first(options))) { qdict_del(options, e->key); } r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts, !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp); if (r < 0) { goto out; } s->snap = g_strdup(opts->snapshot); s->image_name = g_strdup(opts->image); /* rbd_open is always r/w */ r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap); if (r < 0) { error_setg_errno(errp, -r, "error reading header from %s", s->image_name); goto failed_open; } /* If we are using an rbd snapshot, we must be r/o, otherwise * leave as-is */ if (s->snap != NULL) { r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp); if (r < 0) { rbd_close(s->image); goto failed_open; } } r = 0; goto out; failed_open: rados_ioctx_destroy(s->io_ctx); g_free(s->snap); g_free(s->image_name); rados_shutdown(s->cluster); out: qapi_free_BlockdevOptionsRbd(opts); g_free(keypairs); g_free(secretid); return r; }
static void qdict_stress_test(void) { size_t lines; char key[128]; FILE *test_file; QDict *qdict; QString *value; const char *test_file_path = "qdict-test-data.txt"; test_file = fopen(test_file_path, "r"); g_assert(test_file != NULL); // Create the dict qdict = qdict_new(); g_assert(qdict != NULL); // Add everything from the test file for (lines = 0;; lines++) { value = read_line(test_file, key); if (!value) break; qdict_put(qdict, key, value); } g_assert(qdict_size(qdict) == lines); // Check if everything is really in there reset_file(test_file); for (;;) { const char *str1, *str2; value = read_line(test_file, key); if (!value) break; str1 = qstring_get_str(value); str2 = qdict_get_str(qdict, key); g_assert(str2 != NULL); g_assert(strcmp(str1, str2) == 0); QDECREF(value); } // Delete everything reset_file(test_file); for (;;) { value = read_line(test_file, key); if (!value) break; qdict_del(qdict, key); QDECREF(value); g_assert(qdict_haskey(qdict, key) == 0); } fclose(test_file); g_assert(qdict_size(qdict) == 0); QDECREF(qdict); }
static void qdict_join_test(void) { QDict *dict1, *dict2; bool overwrite = false; int i; dict1 = qdict_new(); dict2 = qdict_new(); /* Test everything once without overwrite and once with */ do { /* Test empty dicts */ qdict_join(dict1, dict2, overwrite); g_assert(qdict_size(dict1) == 0); g_assert(qdict_size(dict2) == 0); /* First iteration: Test movement */ /* Second iteration: Test empty source and non-empty destination */ qdict_put(dict2, "foo", qint_from_int(42)); for (i = 0; i < 2; i++) { qdict_join(dict1, dict2, overwrite); g_assert(qdict_size(dict1) == 1); g_assert(qdict_size(dict2) == 0); g_assert(qdict_get_int(dict1, "foo") == 42); } /* Test non-empty source and destination without conflict */ qdict_put(dict2, "bar", qint_from_int(23)); qdict_join(dict1, dict2, overwrite); g_assert(qdict_size(dict1) == 2); g_assert(qdict_size(dict2) == 0); g_assert(qdict_get_int(dict1, "foo") == 42); g_assert(qdict_get_int(dict1, "bar") == 23); /* Test conflict */ qdict_put(dict2, "foo", qint_from_int(84)); qdict_join(dict1, dict2, overwrite); g_assert(qdict_size(dict1) == 2); g_assert(qdict_size(dict2) == !overwrite); g_assert(qdict_get_int(dict1, "foo") == overwrite ? 84 : 42); g_assert(qdict_get_int(dict1, "bar") == 23); if (!overwrite) { g_assert(qdict_get_int(dict2, "foo") == 84); } /* Check the references */ g_assert(qdict_get(dict1, "foo")->refcnt == 1); g_assert(qdict_get(dict1, "bar")->refcnt == 1); if (!overwrite) { g_assert(qdict_get(dict2, "foo")->refcnt == 1); } /* Clean up */ qdict_del(dict1, "foo"); qdict_del(dict1, "bar"); if (!overwrite) { qdict_del(dict2, "foo"); } } while (overwrite ^= true); QDECREF(dict1); QDECREF(dict2); }
static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) { BDRVRBDState *s = bs->opaque; BlockdevOptionsRbd *opts = NULL; Visitor *v; QObject *crumpled = NULL; const QDictEntry *e; Error *local_err = NULL; const char *filename; char *keypairs, *secretid; int r; /* If we are given a filename, parse the filename, with precedence given to * filename encoded options */ filename = qdict_get_try_str(options, "filename"); if (filename) { warn_report("'filename' option specified. " "This is an unsupported option, and may be deprecated " "in the future"); qemu_rbd_parse_filename(filename, options, &local_err); qdict_del(options, "filename"); if (local_err) { error_propagate(errp, local_err); return -EINVAL; } } keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs")); if (keypairs) { qdict_del(options, "=keyvalue-pairs"); } secretid = g_strdup(qdict_get_try_str(options, "password-secret")); if (secretid) { qdict_del(options, "password-secret"); } /* Convert the remaining options into a QAPI object */ crumpled = qdict_crumple(options, errp); if (crumpled == NULL) { r = -EINVAL; goto out; } v = qobject_input_visitor_new_keyval(crumpled); visit_type_BlockdevOptionsRbd(v, NULL, &opts, &local_err); visit_free(v); qobject_unref(crumpled); if (local_err) { error_propagate(errp, local_err); r = -EINVAL; goto out; } /* Remove the processed options from the QDict (the visitor processes * _all_ options in the QDict) */ while ((e = qdict_first(options))) { qdict_del(options, e->key); } r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts, !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp); if (r < 0) { goto out; } s->snap = g_strdup(opts->snapshot); s->image_name = g_strdup(opts->image); /* rbd_open is always r/w */ r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap); if (r < 0) { error_setg_errno(errp, -r, "error reading header from %s", s->image_name); goto failed_open; } /* If we are using an rbd snapshot, we must be r/o, otherwise * leave as-is */ if (s->snap != NULL) { if (!bdrv_is_read_only(bs)) { error_report("Opening rbd snapshots without an explicit " "read-only=on option is deprecated. Future versions " "will refuse to open the image instead of " "automatically marking the image read-only."); r = bdrv_set_read_only(bs, true, &local_err); if (r < 0) { error_propagate(errp, local_err); goto failed_open; } } } r = 0; goto out; failed_open: rados_ioctx_destroy(s->io_ctx); g_free(s->snap); g_free(s->image_name); rados_shutdown(s->cluster); out: qapi_free_BlockdevOptionsRbd(opts); g_free(keypairs); g_free(secretid); return r; }