trace: Add trace-file command to open/close/flush trace file
[qemu/agraf.git] / block / nbd.c
blob5e9d6cb8e655aa961787e32a8280f94a51550b58
1 /*
2 * QEMU Block driver for NBD
4 * Copyright (C) 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
7 * Some parts:
8 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
29 #include "qemu-common.h"
30 #include "nbd.h"
31 #include "module.h"
33 #include <sys/types.h>
34 #include <unistd.h>
36 #define EN_OPTSTR ":exportname="
38 typedef struct BDRVNBDState {
39 int sock;
40 off_t size;
41 size_t blocksize;
42 } BDRVNBDState;
44 static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
46 BDRVNBDState *s = bs->opaque;
47 uint32_t nbdflags;
49 char *file;
50 char *name;
51 const char *host;
52 const char *unixpath;
53 int sock;
54 off_t size;
55 size_t blocksize;
56 int ret;
57 int err = -EINVAL;
59 file = qemu_strdup(filename);
61 name = strstr(file, EN_OPTSTR);
62 if (name) {
63 if (name[strlen(EN_OPTSTR)] == 0) {
64 goto out;
66 name[0] = 0;
67 name += strlen(EN_OPTSTR);
70 if (!strstart(file, "nbd:", &host)) {
71 goto out;
74 if (strstart(host, "unix:", &unixpath)) {
76 if (unixpath[0] != '/') {
77 goto out;
80 sock = unix_socket_outgoing(unixpath);
82 } else {
83 uint16_t port = NBD_DEFAULT_PORT;
84 char *p, *r;
85 char hostname[128];
87 pstrcpy(hostname, 128, host);
89 p = strchr(hostname, ':');
90 if (p != NULL) {
91 *p = '\0';
92 p++;
94 port = strtol(p, &r, 0);
95 if (r == p) {
96 goto out;
98 } else if (name == NULL) {
99 goto out;
102 sock = tcp_socket_outgoing(hostname, port);
105 if (sock == -1) {
106 err = -errno;
107 goto out;
110 ret = nbd_receive_negotiate(sock, name, &nbdflags, &size, &blocksize);
111 if (ret == -1) {
112 err = -errno;
113 goto out;
116 s->sock = sock;
117 s->size = size;
118 s->blocksize = blocksize;
119 err = 0;
121 out:
122 qemu_free(file);
123 return err;
126 static int nbd_read(BlockDriverState *bs, int64_t sector_num,
127 uint8_t *buf, int nb_sectors)
129 BDRVNBDState *s = bs->opaque;
130 struct nbd_request request;
131 struct nbd_reply reply;
133 request.type = NBD_CMD_READ;
134 request.handle = (uint64_t)(intptr_t)bs;
135 request.from = sector_num * 512;;
136 request.len = nb_sectors * 512;
138 if (nbd_send_request(s->sock, &request) == -1)
139 return -errno;
141 if (nbd_receive_reply(s->sock, &reply) == -1)
142 return -errno;
144 if (reply.error !=0)
145 return -reply.error;
147 if (reply.handle != request.handle)
148 return -EIO;
150 if (nbd_wr_sync(s->sock, buf, request.len, 1) != request.len)
151 return -EIO;
153 return 0;
156 static int nbd_write(BlockDriverState *bs, int64_t sector_num,
157 const uint8_t *buf, int nb_sectors)
159 BDRVNBDState *s = bs->opaque;
160 struct nbd_request request;
161 struct nbd_reply reply;
163 request.type = NBD_CMD_WRITE;
164 request.handle = (uint64_t)(intptr_t)bs;
165 request.from = sector_num * 512;;
166 request.len = nb_sectors * 512;
168 if (nbd_send_request(s->sock, &request) == -1)
169 return -errno;
171 if (nbd_wr_sync(s->sock, (uint8_t*)buf, request.len, 0) != request.len)
172 return -EIO;
174 if (nbd_receive_reply(s->sock, &reply) == -1)
175 return -errno;
177 if (reply.error !=0)
178 return -reply.error;
180 if (reply.handle != request.handle)
181 return -EIO;
183 return 0;
186 static void nbd_close(BlockDriverState *bs)
188 BDRVNBDState *s = bs->opaque;
189 struct nbd_request request;
191 request.type = NBD_CMD_DISC;
192 request.handle = (uint64_t)(intptr_t)bs;
193 request.from = 0;
194 request.len = 0;
195 nbd_send_request(s->sock, &request);
197 close(s->sock);
200 static int64_t nbd_getlength(BlockDriverState *bs)
202 BDRVNBDState *s = bs->opaque;
204 return s->size;
207 static BlockDriver bdrv_nbd = {
208 .format_name = "nbd",
209 .instance_size = sizeof(BDRVNBDState),
210 .bdrv_file_open = nbd_open,
211 .bdrv_read = nbd_read,
212 .bdrv_write = nbd_write,
213 .bdrv_close = nbd_close,
214 .bdrv_getlength = nbd_getlength,
215 .protocol_name = "nbd",
218 static void bdrv_nbd_init(void)
220 bdrv_register(&bdrv_nbd);
223 block_init(bdrv_nbd_init);