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
)
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
73 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
74 off_t
*size
, Error
**errp
)
81 TRACE("Receiving negotiation.");
85 if (read_sync(csock
, buf
, 8) != 8) {
86 error_setg(errp
, "Failed to read data");
91 if (strlen(buf
) == 0) {
92 error_setg(errp
, "Server connection closed unexpectedly");
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");
111 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
112 error_setg(errp
, "Failed to read magic");
115 magic
= be64_to_cpu(magic
);
116 TRACE("Magic is 0x%" PRIx64
, magic
);
119 uint32_t reserved
= 0;
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");
128 error_setg(errp
, "Bad magic received");
132 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
133 error_setg(errp
, "Failed to read server flags");
136 *flags
= be16_to_cpu(tmp
) << 16;
137 /* reserved for future use */
138 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
140 error_setg(errp
, "Failed to read reserved field");
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");
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");
154 namesize
= cpu_to_be32(strlen(name
));
155 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
157 error_setg(errp
, "Failed to send export name length");
160 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
161 error_setg(errp
, "Failed to send export name");
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");
171 error_setg(errp
, "Bad magic received");
177 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
178 error_setg(errp
, "Failed to read export length");
181 *size
= be64_to_cpu(s
);
182 TRACE("Size is %" PRIu64
, *size
);
185 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
186 error_setg(errp
, "Failed to read export flags");
189 *flags
= be32_to_cpup(flags
);
191 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
192 error_setg(errp
, "Failed to read export flags");
195 *flags
|= be16_to_cpu(tmp
);
197 if (read_sync(csock
, &buf
, 124) != 124) {
198 error_setg(errp
, "Failed to read reserved block");
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) {
214 LOG("Failed to set NBD socket");
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) {
222 LOG("Failed setting NBD block size");
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) {
230 LOG("Failed setting size (in blocks)");
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) {
241 LOG("Failed setting read-only attribute");
246 LOG("Failed setting flags");
251 TRACE("Negotiation ended");
256 int nbd_client(int fd
)
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
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
);
285 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
)
290 int nbd_client(int fd
)
296 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
298 uint8_t buf
[NBD_REQUEST_SIZE
];
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
));
316 if (ret
!= sizeof(buf
)) {
317 LOG("writing to socket failed");
323 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
325 uint8_t buf
[NBD_REPLY_SIZE
];
329 ret
= read_sync(csock
, buf
, sizeof(buf
));
334 if (ret
!= sizeof(buf
)) {
340 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
341 [ 4 .. 7] error (0 == no error)
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
);
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
);