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 "qemu/osdep.h"
20 #include "nbd-internal.h"
22 static int nbd_errno_to_system_errno(int err
)
41 /* Definitions for opaque data types */
43 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
45 /* That's all folks */
47 /* Basic flow for negotiation
74 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
75 off_t
*size
, Error
**errp
)
82 TRACE("Receiving negotiation.");
86 if (read_sync(csock
, buf
, 8) != 8) {
87 error_setg(errp
, "Failed to read data");
92 if (strlen(buf
) == 0) {
93 error_setg(errp
, "Server connection closed unexpectedly");
97 TRACE("Magic is %c%c%c%c%c%c%c%c",
98 qemu_isprint(buf
[0]) ? buf
[0] : '.',
99 qemu_isprint(buf
[1]) ? buf
[1] : '.',
100 qemu_isprint(buf
[2]) ? buf
[2] : '.',
101 qemu_isprint(buf
[3]) ? buf
[3] : '.',
102 qemu_isprint(buf
[4]) ? buf
[4] : '.',
103 qemu_isprint(buf
[5]) ? buf
[5] : '.',
104 qemu_isprint(buf
[6]) ? buf
[6] : '.',
105 qemu_isprint(buf
[7]) ? buf
[7] : '.');
107 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
108 error_setg(errp
, "Invalid magic received");
112 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
113 error_setg(errp
, "Failed to read magic");
116 magic
= be64_to_cpu(magic
);
117 TRACE("Magic is 0x%" PRIx64
, magic
);
120 uint32_t reserved
= 0;
124 TRACE("Checking magic (opts_magic)");
125 if (magic
!= NBD_OPTS_MAGIC
) {
126 if (magic
== NBD_CLIENT_MAGIC
) {
127 error_setg(errp
, "Server does not support export names");
129 error_setg(errp
, "Bad magic received");
133 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
134 error_setg(errp
, "Failed to read server flags");
137 *flags
= be16_to_cpu(tmp
) << 16;
138 /* reserved for future use */
139 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
141 error_setg(errp
, "Failed to read reserved field");
144 /* write the export name */
145 magic
= cpu_to_be64(magic
);
146 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
147 error_setg(errp
, "Failed to send export name magic");
150 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
151 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
152 error_setg(errp
, "Failed to send export name option number");
155 namesize
= cpu_to_be32(strlen(name
));
156 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
158 error_setg(errp
, "Failed to send export name length");
161 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
162 error_setg(errp
, "Failed to send export name");
166 TRACE("Checking magic (cli_magic)");
168 if (magic
!= NBD_CLIENT_MAGIC
) {
169 if (magic
== NBD_OPTS_MAGIC
) {
170 error_setg(errp
, "Server requires an export name");
172 error_setg(errp
, "Bad magic received");
178 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
179 error_setg(errp
, "Failed to read export length");
182 *size
= be64_to_cpu(s
);
183 TRACE("Size is %" PRIu64
, *size
);
186 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
187 error_setg(errp
, "Failed to read export flags");
190 *flags
= be32_to_cpup(flags
);
192 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
193 error_setg(errp
, "Failed to read export flags");
196 *flags
|= be16_to_cpu(tmp
);
198 if (read_sync(csock
, &buf
, 124) != 124) {
199 error_setg(errp
, "Failed to read reserved block");
209 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
)
211 TRACE("Setting NBD socket");
213 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
215 LOG("Failed to set NBD socket");
219 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE
);
221 if (ioctl(fd
, NBD_SET_BLKSIZE
, (size_t)BDRV_SECTOR_SIZE
) < 0) {
223 LOG("Failed setting NBD block size");
227 TRACE("Setting size to %zd block(s)", (size_t)(size
/ BDRV_SECTOR_SIZE
));
229 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, (size_t)(size
/ BDRV_SECTOR_SIZE
)) < 0) {
231 LOG("Failed setting size (in blocks)");
235 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
236 if (errno
== ENOTTY
) {
237 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
238 TRACE("Setting readonly attribute");
240 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
242 LOG("Failed setting read-only attribute");
247 LOG("Failed setting flags");
252 TRACE("Negotiation ended");
257 int nbd_client(int fd
)
262 TRACE("Doing NBD loop");
264 ret
= ioctl(fd
, NBD_DO_IT
);
265 if (ret
< 0 && errno
== EPIPE
) {
266 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
267 * the socket via NBD_DISCONNECT. We do not want to return 1 in
274 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
276 TRACE("Clearing NBD queue");
277 ioctl(fd
, NBD_CLEAR_QUE
);
279 TRACE("Clearing NBD socket");
280 ioctl(fd
, NBD_CLEAR_SOCK
);
286 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
)
291 int nbd_client(int fd
)
297 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
299 uint8_t buf
[NBD_REQUEST_SIZE
];
302 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
303 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
304 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
305 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
306 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
308 TRACE("Sending request to client: "
309 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
310 request
->from
, request
->len
, request
->handle
, request
->type
);
312 ret
= write_sync(csock
, buf
, sizeof(buf
));
317 if (ret
!= sizeof(buf
)) {
318 LOG("writing to socket failed");
324 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
326 uint8_t buf
[NBD_REPLY_SIZE
];
330 ret
= read_sync(csock
, buf
, sizeof(buf
));
335 if (ret
!= sizeof(buf
)) {
341 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
342 [ 4 .. 7] error (0 == no error)
346 magic
= be32_to_cpup((uint32_t*)buf
);
347 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
348 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
350 reply
->error
= nbd_errno_to_system_errno(reply
->error
);
353 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
354 magic
, reply
->error
, reply
->handle
);
356 if (magic
!= NBD_REPLY_MAGIC
) {
357 LOG("invalid magic (got 0x%x)", magic
);