]>
Commit | Line | Data |
---|---|---|
9bad2a6b PB |
1 | /* |
2 | * Persistent reservation manager that talks to qemu-pr-helper | |
3 | * | |
4 | * Copyright (c) 2017 Red Hat, Inc. | |
5 | * | |
6 | * Author: Paolo Bonzini <[email protected]> | |
7 | * | |
8 | * This code is licensed under the LGPL v2.1 or later. | |
9 | * | |
10 | */ | |
11 | ||
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" | |
e2c81a45 | 20 | #include "qapi/qapi-events-block.h" |
9bad2a6b PB |
21 | |
22 | #include <scsi/sg.h> | |
23 | ||
24 | #define PR_MAX_RECONNECT_ATTEMPTS 5 | |
25 | ||
26 | #define TYPE_PR_MANAGER_HELPER "pr-manager-helper" | |
27 | ||
28 | #define PR_MANAGER_HELPER(obj) \ | |
29 | OBJECT_CHECK(PRManagerHelper, (obj), \ | |
30 | TYPE_PR_MANAGER_HELPER) | |
31 | ||
32 | typedef struct PRManagerHelper { | |
33 | /* <private> */ | |
34 | PRManager parent; | |
35 | ||
36 | char *path; | |
37 | ||
38 | QemuMutex lock; | |
39 | QIOChannel *ioc; | |
40 | } PRManagerHelper; | |
41 | ||
e2c81a45 PB |
42 | static void pr_manager_send_status_changed_event(PRManagerHelper *pr_mgr) |
43 | { | |
44 | char *id = object_get_canonical_path_component(OBJECT(pr_mgr)); | |
45 | ||
46 | if (id) { | |
3ab72385 | 47 | qapi_event_send_pr_manager_status_changed(id, !!pr_mgr->ioc); |
ea3d77c8 | 48 | g_free(id); |
e2c81a45 PB |
49 | } |
50 | } | |
51 | ||
9bad2a6b PB |
52 | /* Called with lock held. */ |
53 | static int pr_manager_helper_read(PRManagerHelper *pr_mgr, | |
54 | void *buf, int sz, Error **errp) | |
55 | { | |
56 | ssize_t r = qio_channel_read_all(pr_mgr->ioc, buf, sz, errp); | |
57 | ||
58 | if (r < 0) { | |
59 | object_unref(OBJECT(pr_mgr->ioc)); | |
60 | pr_mgr->ioc = NULL; | |
e2c81a45 | 61 | pr_manager_send_status_changed_event(pr_mgr); |
9bad2a6b PB |
62 | return -EINVAL; |
63 | } | |
64 | ||
65 | return 0; | |
66 | } | |
67 | ||
68 | /* Called with lock held. */ | |
69 | static int pr_manager_helper_write(PRManagerHelper *pr_mgr, | |
70 | int fd, | |
71 | const void *buf, int sz, Error **errp) | |
72 | { | |
73 | size_t nfds = (fd != -1); | |
74 | while (sz > 0) { | |
75 | struct iovec iov; | |
76 | ssize_t n_written; | |
77 | ||
78 | iov.iov_base = (void *)buf; | |
79 | iov.iov_len = sz; | |
80 | n_written = qio_channel_writev_full(QIO_CHANNEL(pr_mgr->ioc), &iov, 1, | |
81 | nfds ? &fd : NULL, nfds, errp); | |
82 | ||
83 | if (n_written <= 0) { | |
84 | assert(n_written != QIO_CHANNEL_ERR_BLOCK); | |
85 | object_unref(OBJECT(pr_mgr->ioc)); | |
aad10040 | 86 | pr_mgr->ioc = NULL; |
e2c81a45 | 87 | pr_manager_send_status_changed_event(pr_mgr); |
9bad2a6b PB |
88 | return n_written < 0 ? -EINVAL : 0; |
89 | } | |
90 | ||
91 | nfds = 0; | |
92 | buf += n_written; | |
93 | sz -= n_written; | |
94 | } | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | /* Called with lock held. */ | |
100 | static int pr_manager_helper_initialize(PRManagerHelper *pr_mgr, | |
101 | Error **errp) | |
102 | { | |
103 | char *path = g_strdup(pr_mgr->path); | |
104 | SocketAddress saddr = { | |
105 | .type = SOCKET_ADDRESS_TYPE_UNIX, | |
106 | .u.q_unix.path = path | |
107 | }; | |
108 | QIOChannelSocket *sioc = qio_channel_socket_new(); | |
109 | Error *local_err = NULL; | |
110 | ||
111 | uint32_t flags; | |
112 | int r; | |
113 | ||
114 | assert(!pr_mgr->ioc); | |
115 | qio_channel_set_name(QIO_CHANNEL(sioc), "pr-manager-helper"); | |
116 | qio_channel_socket_connect_sync(sioc, | |
117 | &saddr, | |
118 | &local_err); | |
119 | g_free(path); | |
120 | if (local_err) { | |
121 | object_unref(OBJECT(sioc)); | |
122 | error_propagate(errp, local_err); | |
123 | return -ENOTCONN; | |
124 | } | |
125 | ||
126 | qio_channel_set_delay(QIO_CHANNEL(sioc), false); | |
127 | pr_mgr->ioc = QIO_CHANNEL(sioc); | |
128 | ||
129 | /* A simple feature negotation protocol, even though there is | |
130 | * no optional feature right now. | |
131 | */ | |
132 | r = pr_manager_helper_read(pr_mgr, &flags, sizeof(flags), errp); | |
133 | if (r < 0) { | |
134 | goto out_close; | |
135 | } | |
136 | ||
137 | flags = 0; | |
138 | r = pr_manager_helper_write(pr_mgr, -1, &flags, sizeof(flags), errp); | |
139 | if (r < 0) { | |
140 | goto out_close; | |
141 | } | |
142 | ||
e2c81a45 | 143 | pr_manager_send_status_changed_event(pr_mgr); |
9bad2a6b PB |
144 | return 0; |
145 | ||
146 | out_close: | |
147 | object_unref(OBJECT(pr_mgr->ioc)); | |
148 | pr_mgr->ioc = NULL; | |
149 | return r; | |
150 | } | |
151 | ||
152 | static int pr_manager_helper_run(PRManager *p, | |
153 | int fd, struct sg_io_hdr *io_hdr) | |
154 | { | |
155 | PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(p); | |
156 | ||
157 | uint32_t len; | |
158 | PRHelperResponse resp; | |
159 | int ret; | |
160 | int expected_dir; | |
161 | int attempts; | |
162 | uint8_t cdb[PR_HELPER_CDB_SIZE] = { 0 }; | |
163 | ||
164 | if (!io_hdr->cmd_len || io_hdr->cmd_len > PR_HELPER_CDB_SIZE) { | |
165 | return -EINVAL; | |
166 | } | |
167 | ||
168 | memcpy(cdb, io_hdr->cmdp, io_hdr->cmd_len); | |
169 | assert(cdb[0] == PERSISTENT_RESERVE_OUT || cdb[0] == PERSISTENT_RESERVE_IN); | |
170 | expected_dir = | |
171 | (cdb[0] == PERSISTENT_RESERVE_OUT ? SG_DXFER_TO_DEV : SG_DXFER_FROM_DEV); | |
172 | if (io_hdr->dxfer_direction != expected_dir) { | |
173 | return -EINVAL; | |
174 | } | |
175 | ||
176 | len = scsi_cdb_xfer(cdb); | |
177 | if (io_hdr->dxfer_len < len || len > PR_HELPER_DATA_SIZE) { | |
178 | return -EINVAL; | |
179 | } | |
180 | ||
181 | qemu_mutex_lock(&pr_mgr->lock); | |
182 | ||
183 | /* Try to reconnect while sending the CDB. */ | |
184 | for (attempts = 0; attempts < PR_MAX_RECONNECT_ATTEMPTS; attempts++) { | |
185 | if (!pr_mgr->ioc) { | |
186 | ret = pr_manager_helper_initialize(pr_mgr, NULL); | |
187 | if (ret < 0) { | |
188 | qemu_mutex_unlock(&pr_mgr->lock); | |
189 | g_usleep(G_USEC_PER_SEC); | |
190 | qemu_mutex_lock(&pr_mgr->lock); | |
191 | continue; | |
192 | } | |
193 | } | |
194 | ||
195 | ret = pr_manager_helper_write(pr_mgr, fd, cdb, ARRAY_SIZE(cdb), NULL); | |
196 | if (ret >= 0) { | |
197 | break; | |
198 | } | |
199 | } | |
200 | if (ret < 0) { | |
201 | goto out; | |
202 | } | |
203 | ||
204 | /* After sending the CDB, any communications failure causes the | |
205 | * command to fail. The failure is transient, retrying the command | |
206 | * will invoke pr_manager_helper_initialize again. | |
207 | */ | |
208 | if (expected_dir == SG_DXFER_TO_DEV) { | |
209 | io_hdr->resid = io_hdr->dxfer_len - len; | |
210 | ret = pr_manager_helper_write(pr_mgr, -1, io_hdr->dxferp, len, NULL); | |
211 | if (ret < 0) { | |
212 | goto out; | |
213 | } | |
214 | } | |
215 | ret = pr_manager_helper_read(pr_mgr, &resp, sizeof(resp), NULL); | |
216 | if (ret < 0) { | |
217 | goto out; | |
218 | } | |
219 | ||
220 | resp.result = be32_to_cpu(resp.result); | |
221 | resp.sz = be32_to_cpu(resp.sz); | |
222 | if (io_hdr->dxfer_direction == SG_DXFER_FROM_DEV) { | |
223 | assert(resp.sz <= io_hdr->dxfer_len); | |
224 | ret = pr_manager_helper_read(pr_mgr, io_hdr->dxferp, resp.sz, NULL); | |
225 | if (ret < 0) { | |
226 | goto out; | |
227 | } | |
228 | io_hdr->resid = io_hdr->dxfer_len - resp.sz; | |
229 | } else { | |
230 | assert(resp.sz == 0); | |
231 | } | |
232 | ||
233 | io_hdr->status = resp.result; | |
234 | if (resp.result == CHECK_CONDITION) { | |
235 | io_hdr->driver_status = SG_ERR_DRIVER_SENSE; | |
236 | io_hdr->sb_len_wr = MIN(io_hdr->mx_sb_len, PR_HELPER_SENSE_SIZE); | |
237 | memcpy(io_hdr->sbp, resp.sense, io_hdr->sb_len_wr); | |
238 | } | |
239 | ||
240 | out: | |
241 | if (ret < 0) { | |
242 | int sense_len = scsi_build_sense(io_hdr->sbp, | |
243 | SENSE_CODE(LUN_COMM_FAILURE)); | |
244 | io_hdr->driver_status = SG_ERR_DRIVER_SENSE; | |
245 | io_hdr->sb_len_wr = MIN(io_hdr->mx_sb_len, sense_len); | |
246 | io_hdr->status = CHECK_CONDITION; | |
247 | } | |
248 | qemu_mutex_unlock(&pr_mgr->lock); | |
249 | return ret; | |
250 | } | |
251 | ||
5f640894 PB |
252 | static bool pr_manager_helper_is_connected(PRManager *p) |
253 | { | |
254 | PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(p); | |
255 | bool result; | |
256 | ||
257 | qemu_mutex_lock(&pr_mgr->lock); | |
258 | result = (pr_mgr->ioc != NULL); | |
259 | qemu_mutex_unlock(&pr_mgr->lock); | |
260 | ||
261 | return result; | |
262 | } | |
263 | ||
9bad2a6b PB |
264 | static void pr_manager_helper_complete(UserCreatable *uc, Error **errp) |
265 | { | |
266 | PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(uc); | |
267 | ||
268 | qemu_mutex_lock(&pr_mgr->lock); | |
269 | pr_manager_helper_initialize(pr_mgr, errp); | |
270 | qemu_mutex_unlock(&pr_mgr->lock); | |
271 | } | |
272 | ||
273 | static char *get_path(Object *obj, Error **errp) | |
274 | { | |
275 | PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj); | |
276 | ||
277 | return g_strdup(pr_mgr->path); | |
278 | } | |
279 | ||
280 | static void set_path(Object *obj, const char *str, Error **errp) | |
281 | { | |
282 | PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj); | |
283 | ||
284 | g_free(pr_mgr->path); | |
285 | pr_mgr->path = g_strdup(str); | |
286 | } | |
287 | ||
288 | static void pr_manager_helper_instance_finalize(Object *obj) | |
289 | { | |
290 | PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj); | |
291 | ||
292 | object_unref(OBJECT(pr_mgr->ioc)); | |
293 | qemu_mutex_destroy(&pr_mgr->lock); | |
294 | } | |
295 | ||
296 | static void pr_manager_helper_instance_init(Object *obj) | |
297 | { | |
298 | PRManagerHelper *pr_mgr = PR_MANAGER_HELPER(obj); | |
299 | ||
300 | qemu_mutex_init(&pr_mgr->lock); | |
301 | } | |
302 | ||
303 | static void pr_manager_helper_class_init(ObjectClass *klass, | |
304 | void *class_data G_GNUC_UNUSED) | |
305 | { | |
306 | PRManagerClass *prmgr_klass = PR_MANAGER_CLASS(klass); | |
307 | UserCreatableClass *uc_klass = USER_CREATABLE_CLASS(klass); | |
308 | ||
309 | object_class_property_add_str(klass, "path", get_path, set_path, | |
310 | &error_abort); | |
311 | uc_klass->complete = pr_manager_helper_complete; | |
312 | prmgr_klass->run = pr_manager_helper_run; | |
5f640894 | 313 | prmgr_klass->is_connected = pr_manager_helper_is_connected; |
9bad2a6b PB |
314 | } |
315 | ||
316 | static const TypeInfo pr_manager_helper_info = { | |
317 | .parent = TYPE_PR_MANAGER, | |
318 | .name = TYPE_PR_MANAGER_HELPER, | |
319 | .instance_size = sizeof(PRManagerHelper), | |
320 | .instance_init = pr_manager_helper_instance_init, | |
321 | .instance_finalize = pr_manager_helper_instance_finalize, | |
322 | .class_init = pr_manager_helper_class_init, | |
323 | }; | |
324 | ||
325 | static void pr_manager_helper_register_types(void) | |
326 | { | |
327 | type_register_static(&pr_manager_helper_info); | |
328 | } | |
329 | ||
330 | type_init(pr_manager_helper_register_types); |