Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2019-10-29' into staging
[qemu.git] / scsi / pr-manager-helper.c
blobca27c93283d20d1c2befbf7c5c3e63890442ac47
1 /*
2 * Persistent reservation manager that talks to qemu-pr-helper
4 * Copyright (c) 2017 Red Hat, Inc.
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
8 * This code is licensed under the LGPL v2.1 or later.
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "scsi/constants.h"
15 #include "scsi/pr-manager.h"
16 #include "scsi/utils.h"
17 #include "io/channel.h"
18 #include "io/channel-socket.h"
19 #include "pr-helper.h"
20 #include "qapi/qapi-events-block.h"
21 #include "qemu/module.h"
23 #include <scsi/sg.h>
25 #define PR_MAX_RECONNECT_ATTEMPTS 5
27 #define TYPE_PR_MANAGER_HELPER "pr-manager-helper"
29 #define PR_MANAGER_HELPER(obj) \
30 OBJECT_CHECK(PRManagerHelper, (obj), \
31 TYPE_PR_MANAGER_HELPER)
33 typedef struct PRManagerHelper {
34 /* <private> */
35 PRManager parent;
37 char *path;
39 QemuMutex lock;
40 QIOChannel *ioc;
41 } PRManagerHelper;
43 static void pr_manager_send_status_changed_event(PRManagerHelper *pr_mgr)
45 char *id = object_get_canonical_path_component(OBJECT(pr_mgr));
47 if (id) {
48 qapi_event_send_pr_manager_status_changed(id, !!pr_mgr->ioc);
49 g_free(id);
53 /* Called with lock held. */
54 static int pr_manager_helper_read(PRManagerHelper *pr_mgr,
55 void *buf, int sz, Error **errp)
57 ssize_t r = qio_channel_read_all(pr_mgr->ioc, buf, sz, errp);
59 if (r < 0) {
60 object_unref(OBJECT(pr_mgr->ioc));
61 pr_mgr->ioc = NULL;
62 pr_manager_send_status_changed_event(pr_mgr);
63 return -EINVAL;
66 return 0;
69 /* Called with lock held. */
70 static int pr_manager_helper_write(PRManagerHelper *pr_mgr,
71 int fd,
72 const void *buf, int sz, Error **errp)
74 size_t nfds = (fd != -1);
75 while (sz > 0) {
76 struct iovec iov;
77 ssize_t n_written;
79 iov.iov_base = (void *)buf;
80 iov.iov_len = sz;
81 n_written = qio_channel_writev_full(QIO_CHANNEL(pr_mgr->ioc), &iov, 1,
82 nfds ? &fd : NULL, nfds, errp);
84 if (n_written <= 0) {
85 assert(n_written != QIO_CHANNEL_ERR_BLOCK);
86 object_unref(OBJECT(pr_mgr->ioc));
87 pr_mgr->ioc = NULL;
88 pr_manager_send_status_changed_event(pr_mgr);
89 return n_written < 0 ? -EINVAL : 0;
92 nfds = 0;
93 buf += n_written;
94 sz -= n_written;
97 return 0;
100 /* Called with lock held. */
101 static int pr_manager_helper_initialize(PRManagerHelper *pr_mgr,
102 Error **errp)
104 char *path = g_strdup(pr_mgr->path);
105 SocketAddress saddr = {
106 .type = SOCKET_ADDRESS_TYPE_UNIX,
107 .u.q_unix.path = path
109 QIOChannelSocket *sioc = qio_channel_socket_new();
110 Error *local_err = NULL;
112 uint32_t flags;
113 int r;
115 assert(!pr_mgr->ioc);
116 qio_channel_set_name(QIO_CHANNEL(sioc), "pr-manager-helper");
117 qio_channel_socket_connect_sync(sioc,
118 &saddr,
119 &local_err);
120 g_free(path);
121 if (local_err) {
122 object_unref(OBJECT(sioc));
123 error_propagate(errp, local_err);
124 return -ENOTCONN;
127 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
128 pr_mgr->ioc = QIO_CHANNEL(sioc);
130 /* A simple feature negotation protocol, even though there is
131 * no optional feature right now.
133 r = pr_manager_helper_read(pr_mgr, &flags, sizeof(flags), errp);
134 if (r < 0) {
135 goto out_close;
138 flags = 0;
139 r = pr_manager_helper_write(pr_mgr, -1, &flags, sizeof(flags), errp);
140 if (r < 0) {
141 goto out_close;
144 pr_manager_send_status_changed_event(pr_mgr);
145 return 0;
147 out_close:
148 object_unref(OBJECT(pr_mgr->ioc));
149 pr_mgr->ioc = NULL;
150 return r;
153 static int pr_manager_helper_run(PRManager *p,
154 int fd, struct sg_io_hdr *io_hdr)
156 PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(p);
158 uint32_t len;
159 PRHelperResponse resp;
160 int ret;
161 int expected_dir;
162 int attempts;
163 uint8_t cdb[PR_HELPER_CDB_SIZE] = { 0 };
165 if (!io_hdr->cmd_len || io_hdr->cmd_len > PR_HELPER_CDB_SIZE) {
166 return -EINVAL;
169 memcpy(cdb, io_hdr->cmdp, io_hdr->cmd_len);
170 assert(cdb[0] == PERSISTENT_RESERVE_OUT || cdb[0] == PERSISTENT_RESERVE_IN);
171 expected_dir =
172 (cdb[0] == PERSISTENT_RESERVE_OUT ? SG_DXFER_TO_DEV : SG_DXFER_FROM_DEV);
173 if (io_hdr->dxfer_direction != expected_dir) {
174 return -EINVAL;
177 len = scsi_cdb_xfer(cdb);
178 if (io_hdr->dxfer_len < len || len > PR_HELPER_DATA_SIZE) {
179 return -EINVAL;
182 qemu_mutex_lock(&pr_mgr->lock);
184 /* Try to reconnect while sending the CDB. */
185 for (attempts = 0; attempts < PR_MAX_RECONNECT_ATTEMPTS; attempts++) {
186 if (!pr_mgr->ioc) {
187 ret = pr_manager_helper_initialize(pr_mgr, NULL);
188 if (ret < 0) {
189 qemu_mutex_unlock(&pr_mgr->lock);
190 g_usleep(G_USEC_PER_SEC);
191 qemu_mutex_lock(&pr_mgr->lock);
192 continue;
196 ret = pr_manager_helper_write(pr_mgr, fd, cdb, ARRAY_SIZE(cdb), NULL);
197 if (ret >= 0) {
198 break;
201 if (ret < 0) {
202 goto out;
205 /* After sending the CDB, any communications failure causes the
206 * command to fail. The failure is transient, retrying the command
207 * will invoke pr_manager_helper_initialize again.
209 if (expected_dir == SG_DXFER_TO_DEV) {
210 io_hdr->resid = io_hdr->dxfer_len - len;
211 ret = pr_manager_helper_write(pr_mgr, -1, io_hdr->dxferp, len, NULL);
212 if (ret < 0) {
213 goto out;
216 ret = pr_manager_helper_read(pr_mgr, &resp, sizeof(resp), NULL);
217 if (ret < 0) {
218 goto out;
221 resp.result = be32_to_cpu(resp.result);
222 resp.sz = be32_to_cpu(resp.sz);
223 if (io_hdr->dxfer_direction == SG_DXFER_FROM_DEV) {
224 assert(resp.sz <= io_hdr->dxfer_len);
225 ret = pr_manager_helper_read(pr_mgr, io_hdr->dxferp, resp.sz, NULL);
226 if (ret < 0) {
227 goto out;
229 io_hdr->resid = io_hdr->dxfer_len - resp.sz;
230 } else {
231 assert(resp.sz == 0);
234 io_hdr->status = resp.result;
235 if (resp.result == CHECK_CONDITION) {
236 io_hdr->driver_status = SG_ERR_DRIVER_SENSE;
237 io_hdr->sb_len_wr = MIN(io_hdr->mx_sb_len, PR_HELPER_SENSE_SIZE);
238 memcpy(io_hdr->sbp, resp.sense, io_hdr->sb_len_wr);
241 out:
242 if (ret < 0) {
243 int sense_len = scsi_build_sense(io_hdr->sbp,
244 SENSE_CODE(LUN_COMM_FAILURE));
245 io_hdr->driver_status = SG_ERR_DRIVER_SENSE;
246 io_hdr->sb_len_wr = MIN(io_hdr->mx_sb_len, sense_len);
247 io_hdr->status = CHECK_CONDITION;
249 qemu_mutex_unlock(&pr_mgr->lock);
250 return ret;
253 static bool pr_manager_helper_is_connected(PRManager *p)
255 PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(p);
256 bool result;
258 qemu_mutex_lock(&pr_mgr->lock);
259 result = (pr_mgr->ioc != NULL);
260 qemu_mutex_unlock(&pr_mgr->lock);
262 return result;
265 static void pr_manager_helper_complete(UserCreatable *uc, Error **errp)
267 PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(uc);
269 qemu_mutex_lock(&pr_mgr->lock);
270 pr_manager_helper_initialize(pr_mgr, errp);
271 qemu_mutex_unlock(&pr_mgr->lock);
274 static char *get_path(Object *obj, Error **errp)
276 PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj);
278 return g_strdup(pr_mgr->path);
281 static void set_path(Object *obj, const char *str, Error **errp)
283 PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj);
285 g_free(pr_mgr->path);
286 pr_mgr->path = g_strdup(str);
289 static void pr_manager_helper_instance_finalize(Object *obj)
291 PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj);
293 object_unref(OBJECT(pr_mgr->ioc));
294 qemu_mutex_destroy(&pr_mgr->lock);
297 static void pr_manager_helper_instance_init(Object *obj)
299 PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj);
301 qemu_mutex_init(&pr_mgr->lock);
304 static void pr_manager_helper_class_init(ObjectClass *klass,
305 void *class_data G_GNUC_UNUSED)
307 PRManagerClass *prmgr_klass = PR_MANAGER_CLASS(klass);
308 UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass);
310 object_class_property_add_str(klass, "path", get_path, set_path,
311 &error_abort);
312 uc_klass->complete = pr_manager_helper_complete;
313 prmgr_klass->run = pr_manager_helper_run;
314 prmgr_klass->is_connected = pr_manager_helper_is_connected;
317 static const TypeInfo pr_manager_helper_info = {
318 .parent = TYPE_PR_MANAGER,
319 .name = TYPE_PR_MANAGER_HELPER,
320 .instance_size = sizeof(PRManagerHelper),
321 .instance_init = pr_manager_helper_instance_init,
322 .instance_finalize = pr_manager_helper_instance_finalize,
323 .class_init = pr_manager_helper_class_init,
326 static void pr_manager_helper_register_types(void)
328 type_register_static(&pr_manager_helper_info);
331 type_init(pr_manager_helper_register_types);