scripts/kvm/kvm_stat: Improve debugfs access checking
[qemu.git] / nbd / client.c
blob83df7ba3788965ea0455ed114cbe3e0ff8602780
1 /*
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 * Network Block Device Client Side
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "nbd-internal.h"
21 static int nbd_errno_to_system_errno(int err)
23 switch (err) {
24 case NBD_SUCCESS:
25 return 0;
26 case NBD_EPERM:
27 return EPERM;
28 case NBD_EIO:
29 return EIO;
30 case NBD_ENOMEM:
31 return ENOMEM;
32 case NBD_ENOSPC:
33 return ENOSPC;
34 case NBD_EINVAL:
35 default:
36 return EINVAL;
40 /* Definitions for opaque data types */
42 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
44 /* That's all folks */
46 /* Basic flow for negotiation
48 Server Client
49 Negotiate
53 Server Client
54 Negotiate #1
55 Option
56 Negotiate #2
58 ----
60 followed by
62 Server Client
63 Request
64 Response
65 Request
66 Response
67 ...
68 ...
69 Request (type == 2)
73 int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
74 off_t *size, Error **errp)
76 char buf[256];
77 uint64_t magic, s;
78 uint16_t tmp;
79 int rc;
81 TRACE("Receiving negotiation.");
83 rc = -EINVAL;
85 if (read_sync(csock, buf, 8) != 8) {
86 error_setg(errp, "Failed to read data");
87 goto fail;
90 buf[8] = '\0';
91 if (strlen(buf) == 0) {
92 error_setg(errp, "Server connection closed unexpectedly");
93 goto fail;
96 TRACE("Magic is %c%c%c%c%c%c%c%c",
97 qemu_isprint(buf[0]) ? buf[0] : '.',
98 qemu_isprint(buf[1]) ? buf[1] : '.',
99 qemu_isprint(buf[2]) ? buf[2] : '.',
100 qemu_isprint(buf[3]) ? buf[3] : '.',
101 qemu_isprint(buf[4]) ? buf[4] : '.',
102 qemu_isprint(buf[5]) ? buf[5] : '.',
103 qemu_isprint(buf[6]) ? buf[6] : '.',
104 qemu_isprint(buf[7]) ? buf[7] : '.');
106 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
107 error_setg(errp, "Invalid magic received");
108 goto fail;
111 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
112 error_setg(errp, "Failed to read magic");
113 goto fail;
115 magic = be64_to_cpu(magic);
116 TRACE("Magic is 0x%" PRIx64, magic);
118 if (name) {
119 uint32_t reserved = 0;
120 uint32_t opt;
121 uint32_t namesize;
123 TRACE("Checking magic (opts_magic)");
124 if (magic != NBD_OPTS_MAGIC) {
125 if (magic == NBD_CLIENT_MAGIC) {
126 error_setg(errp, "Server does not support export names");
127 } else {
128 error_setg(errp, "Bad magic received");
130 goto fail;
132 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
133 error_setg(errp, "Failed to read server flags");
134 goto fail;
136 *flags = be16_to_cpu(tmp) << 16;
137 /* reserved for future use */
138 if (write_sync(csock, &reserved, sizeof(reserved)) !=
139 sizeof(reserved)) {
140 error_setg(errp, "Failed to read reserved field");
141 goto fail;
143 /* write the export name */
144 magic = cpu_to_be64(magic);
145 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
146 error_setg(errp, "Failed to send export name magic");
147 goto fail;
149 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
150 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
151 error_setg(errp, "Failed to send export name option number");
152 goto fail;
154 namesize = cpu_to_be32(strlen(name));
155 if (write_sync(csock, &namesize, sizeof(namesize)) !=
156 sizeof(namesize)) {
157 error_setg(errp, "Failed to send export name length");
158 goto fail;
160 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
161 error_setg(errp, "Failed to send export name");
162 goto fail;
164 } else {
165 TRACE("Checking magic (cli_magic)");
167 if (magic != NBD_CLIENT_MAGIC) {
168 if (magic == NBD_OPTS_MAGIC) {
169 error_setg(errp, "Server requires an export name");
170 } else {
171 error_setg(errp, "Bad magic received");
173 goto fail;
177 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
178 error_setg(errp, "Failed to read export length");
179 goto fail;
181 *size = be64_to_cpu(s);
182 TRACE("Size is %" PRIu64, *size);
184 if (!name) {
185 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
186 error_setg(errp, "Failed to read export flags");
187 goto fail;
189 *flags = be32_to_cpup(flags);
190 } else {
191 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
192 error_setg(errp, "Failed to read export flags");
193 goto fail;
195 *flags |= be16_to_cpu(tmp);
197 if (read_sync(csock, &buf, 124) != 124) {
198 error_setg(errp, "Failed to read reserved block");
199 goto fail;
201 rc = 0;
203 fail:
204 return rc;
207 #ifdef __linux__
208 int nbd_init(int fd, int csock, uint32_t flags, off_t size)
210 TRACE("Setting NBD socket");
212 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
213 int serrno = errno;
214 LOG("Failed to set NBD socket");
215 return -serrno;
218 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);
220 if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) {
221 int serrno = errno;
222 LOG("Failed setting NBD block size");
223 return -serrno;
226 TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));
228 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) {
229 int serrno = errno;
230 LOG("Failed setting size (in blocks)");
231 return -serrno;
234 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0) {
235 if (errno == ENOTTY) {
236 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
237 TRACE("Setting readonly attribute");
239 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
240 int serrno = errno;
241 LOG("Failed setting read-only attribute");
242 return -serrno;
244 } else {
245 int serrno = errno;
246 LOG("Failed setting flags");
247 return -serrno;
251 TRACE("Negotiation ended");
253 return 0;
256 int nbd_client(int fd)
258 int ret;
259 int serrno;
261 TRACE("Doing NBD loop");
263 ret = ioctl(fd, NBD_DO_IT);
264 if (ret < 0 && errno == EPIPE) {
265 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
266 * the socket via NBD_DISCONNECT. We do not want to return 1 in
267 * that case.
269 ret = 0;
271 serrno = errno;
273 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
275 TRACE("Clearing NBD queue");
276 ioctl(fd, NBD_CLEAR_QUE);
278 TRACE("Clearing NBD socket");
279 ioctl(fd, NBD_CLEAR_SOCK);
281 errno = serrno;
282 return ret;
284 #else
285 int nbd_init(int fd, int csock, uint32_t flags, off_t size)
287 return -ENOTSUP;
290 int nbd_client(int fd)
292 return -ENOTSUP;
294 #endif
296 ssize_t nbd_send_request(int csock, struct nbd_request *request)
298 uint8_t buf[NBD_REQUEST_SIZE];
299 ssize_t ret;
301 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
302 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
303 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
304 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
305 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
307 TRACE("Sending request to client: "
308 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
309 request->from, request->len, request->handle, request->type);
311 ret = write_sync(csock, buf, sizeof(buf));
312 if (ret < 0) {
313 return ret;
316 if (ret != sizeof(buf)) {
317 LOG("writing to socket failed");
318 return -EINVAL;
320 return 0;
323 ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
325 uint8_t buf[NBD_REPLY_SIZE];
326 uint32_t magic;
327 ssize_t ret;
329 ret = read_sync(csock, buf, sizeof(buf));
330 if (ret < 0) {
331 return ret;
334 if (ret != sizeof(buf)) {
335 LOG("read failed");
336 return -EINVAL;
339 /* Reply
340 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
341 [ 4 .. 7] error (0 == no error)
342 [ 7 .. 15] handle
345 magic = be32_to_cpup((uint32_t*)buf);
346 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
347 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
349 reply->error = nbd_errno_to_system_errno(reply->error);
351 TRACE("Got reply: "
352 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
353 magic, reply->error, reply->handle);
355 if (magic != NBD_REPLY_MAGIC) {
356 LOG("invalid magic (got 0x%x)", magic);
357 return -EINVAL;
359 return 0;