2 * QEMU Block driver for NBD
4 * Copyright (C) 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
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
29 #include "qemu-common.h"
33 #include <sys/types.h>
36 #define EN_OPTSTR ":exportname="
38 typedef struct BDRVNBDState
{
44 static int nbd_open(BlockDriverState
*bs
, const char* filename
, int flags
)
46 BDRVNBDState
*s
= bs
->opaque
;
59 file
= qemu_strdup(filename
);
61 name
= strstr(file
, EN_OPTSTR
);
63 if (name
[strlen(EN_OPTSTR
)] == 0) {
67 name
+= strlen(EN_OPTSTR
);
70 if (!strstart(file
, "nbd:", &host
)) {
74 if (strstart(host
, "unix:", &unixpath
)) {
76 if (unixpath
[0] != '/') {
80 sock
= unix_socket_outgoing(unixpath
);
83 uint16_t port
= NBD_DEFAULT_PORT
;
87 pstrcpy(hostname
, 128, host
);
89 p
= strchr(hostname
, ':');
94 port
= strtol(p
, &r
, 0);
100 sock
= tcp_socket_outgoing(hostname
, port
);
108 ret
= nbd_receive_negotiate(sock
, name
, &nbdflags
, &size
, &blocksize
);
116 s
->blocksize
= blocksize
;
124 static int nbd_read(BlockDriverState
*bs
, int64_t sector_num
,
125 uint8_t *buf
, int nb_sectors
)
127 BDRVNBDState
*s
= bs
->opaque
;
128 struct nbd_request request
;
129 struct nbd_reply reply
;
131 request
.type
= NBD_CMD_READ
;
132 request
.handle
= (uint64_t)(intptr_t)bs
;
133 request
.from
= sector_num
* 512;;
134 request
.len
= nb_sectors
* 512;
136 if (nbd_send_request(s
->sock
, &request
) == -1)
139 if (nbd_receive_reply(s
->sock
, &reply
) == -1)
145 if (reply
.handle
!= request
.handle
)
148 if (nbd_wr_sync(s
->sock
, buf
, request
.len
, 1) != request
.len
)
154 static int nbd_write(BlockDriverState
*bs
, int64_t sector_num
,
155 const uint8_t *buf
, int nb_sectors
)
157 BDRVNBDState
*s
= bs
->opaque
;
158 struct nbd_request request
;
159 struct nbd_reply reply
;
161 request
.type
= NBD_CMD_WRITE
;
162 request
.handle
= (uint64_t)(intptr_t)bs
;
163 request
.from
= sector_num
* 512;;
164 request
.len
= nb_sectors
* 512;
166 if (nbd_send_request(s
->sock
, &request
) == -1)
169 if (nbd_wr_sync(s
->sock
, (uint8_t*)buf
, request
.len
, 0) != request
.len
)
172 if (nbd_receive_reply(s
->sock
, &reply
) == -1)
178 if (reply
.handle
!= request
.handle
)
184 static void nbd_close(BlockDriverState
*bs
)
186 BDRVNBDState
*s
= bs
->opaque
;
187 struct nbd_request request
;
189 request
.type
= NBD_CMD_DISC
;
190 request
.handle
= (uint64_t)(intptr_t)bs
;
193 nbd_send_request(s
->sock
, &request
);
198 static int64_t nbd_getlength(BlockDriverState
*bs
)
200 BDRVNBDState
*s
= bs
->opaque
;
205 static BlockDriver bdrv_nbd
= {
206 .format_name
= "nbd",
207 .instance_size
= sizeof(BDRVNBDState
),
208 .bdrv_file_open
= nbd_open
,
209 .bdrv_read
= nbd_read
,
210 .bdrv_write
= nbd_write
,
211 .bdrv_close
= nbd_close
,
212 .bdrv_getlength
= nbd_getlength
,
213 .protocol_name
= "nbd",
216 static void bdrv_nbd_init(void)
218 bdrv_register(&bdrv_nbd
);
221 block_init(bdrv_nbd_init
);