Update code for API changes
[qemu/ar7.git] / scsi / pr-manager.c
blobd9f4e8c3adbcf2333d1912148423907b80b1aefb
1 /*
2 * Persistent reservation manager abstract class
4 * Copyright (c) 2017 Red Hat, Inc.
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
8 * This code is licensed under the LGPL.
12 #include "qemu/osdep.h"
13 #include <scsi/sg.h>
15 #include "qapi/error.h"
16 #include "block/aio.h"
17 #include "block/thread-pool.h"
18 #include "scsi/pr-manager.h"
19 #include "trace.h"
20 #include "qapi/qapi-types-block.h"
21 #include "qapi/qapi-commands-block.h"
23 #define PR_MANAGER_PATH "/objects"
25 typedef struct PRManagerData {
26 PRManager *pr_mgr;
27 struct sg_io_hdr *hdr;
28 int fd;
29 } PRManagerData;
31 static int pr_manager_worker(void *opaque)
33 PRManagerData *data = opaque;
34 PRManager *pr_mgr = data->pr_mgr;
35 PRManagerClass *pr_mgr_class =
36 PR_MANAGER_GET_CLASS(pr_mgr);
37 struct sg_io_hdr *hdr = data->hdr;
38 int fd = data->fd;
39 int r;
41 g_free(data);
42 trace_pr_manager_run(fd, hdr->cmdp[0], hdr->cmdp[1]);
44 /* The reference was taken in pr_manager_execute. */
45 r = pr_mgr_class->run(pr_mgr, fd, hdr);
46 object_unref(OBJECT(pr_mgr));
47 return r;
51 int coroutine_fn pr_manager_execute(PRManager *pr_mgr, AioContext *ctx, int fd,
52 struct sg_io_hdr *hdr)
54 ThreadPool *pool = aio_get_thread_pool(ctx);
55 PRManagerData data = {
56 .pr_mgr = pr_mgr,
57 .fd = fd,
58 .hdr = hdr,
61 trace_pr_manager_execute(fd, hdr->cmdp[0], hdr->cmdp[1]);
63 /* The matching object_unref is in pr_manager_worker. */
64 object_ref(OBJECT(pr_mgr));
65 return thread_pool_submit_co(pool, pr_manager_worker, &data);
68 bool pr_manager_is_connected(PRManager *pr_mgr)
70 PRManagerClass *pr_mgr_class =
71 PR_MANAGER_GET_CLASS(pr_mgr);
73 return !pr_mgr_class->is_connected || pr_mgr_class->is_connected(pr_mgr);
76 static const TypeInfo pr_manager_info = {
77 .parent = TYPE_OBJECT,
78 .name = TYPE_PR_MANAGER,
79 .class_size = sizeof(PRManagerClass),
80 .abstract = true,
81 .interfaces = (InterfaceInfo[]) {
82 { TYPE_USER_CREATABLE },
83 { }
87 PRManager *pr_manager_lookup(const char *id, Error **errp)
89 Object *obj;
90 PRManager *pr_mgr;
92 obj = object_resolve_path_component(object_get_objects_root(), id);
93 if (!obj) {
94 error_setg(errp, "No persistent reservation manager with id '%s'", id);
95 return NULL;
98 pr_mgr = (PRManager *)
99 object_dynamic_cast(obj,
100 TYPE_PR_MANAGER);
101 if (!pr_mgr) {
102 error_setg(errp,
103 "Object with id '%s' is not a persistent reservation manager",
104 id);
105 return NULL;
108 return pr_mgr;
111 static void
112 pr_manager_register_types(void)
114 type_register_static(&pr_manager_info);
117 static int query_one_pr_manager(Object *object, void *opaque)
119 PRManagerInfoList ***prev = opaque;
120 PRManagerInfoList *elem;
121 PRManagerInfo *info;
122 PRManager *pr_mgr;
124 pr_mgr = (PRManager *)object_dynamic_cast(object, TYPE_PR_MANAGER);
125 if (!pr_mgr) {
126 return 0;
129 elem = g_new0(PRManagerInfoList, 1);
130 info = g_new0(PRManagerInfo, 1);
131 info->id = object_get_canonical_path_component(object);
132 info->connected = pr_manager_is_connected(pr_mgr);
133 elem->value = info;
134 elem->next = NULL;
136 **prev = elem;
137 *prev = &elem->next;
138 return 0;
141 PRManagerInfoList *qmp_query_pr_managers(Error **errp)
143 PRManagerInfoList *head = NULL;
144 PRManagerInfoList **prev = &head;
145 Object *container = container_get(object_get_root(), PR_MANAGER_PATH);
147 object_child_foreach(container, query_one_pr_manager, &prev);
148 return head;
151 type_init(pr_manager_register_types);