root / block / nbd.c @ d0f2c4c6
History | View | Annotate | Download (4.7 kB)
1 | 75818250 | ths | /*
|
---|---|---|---|
2 | 75818250 | ths | * QEMU Block driver for NBD
|
3 | 75818250 | ths | *
|
4 | 75818250 | ths | * Copyright (C) 2008 Bull S.A.S.
|
5 | bd5921b4 | malc | * Author: Laurent Vivier <Laurent.Vivier@bull.net>
|
6 | 75818250 | ths | *
|
7 | 75818250 | ths | * Some parts:
|
8 | 75818250 | ths | * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
|
9 | 75818250 | ths | *
|
10 | 75818250 | ths | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
11 | 75818250 | ths | * of this software and associated documentation files (the "Software"), to deal
|
12 | 75818250 | ths | * in the Software without restriction, including without limitation the rights
|
13 | 75818250 | ths | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
14 | 75818250 | ths | * copies of the Software, and to permit persons to whom the Software is
|
15 | 75818250 | ths | * furnished to do so, subject to the following conditions:
|
16 | 75818250 | ths | *
|
17 | 75818250 | ths | * The above copyright notice and this permission notice shall be included in
|
18 | 75818250 | ths | * all copies or substantial portions of the Software.
|
19 | 75818250 | ths | *
|
20 | 75818250 | ths | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
21 | 75818250 | ths | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
22 | 75818250 | ths | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
23 | 75818250 | ths | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
24 | 75818250 | ths | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
25 | 75818250 | ths | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
26 | 75818250 | ths | * THE SOFTWARE.
|
27 | 75818250 | ths | */
|
28 | 75818250 | ths | |
29 | 75818250 | ths | #include "qemu-common.h" |
30 | 75818250 | ths | #include "nbd.h" |
31 | 5efa9d5a | Anthony Liguori | #include "module.h" |
32 | 75818250 | ths | |
33 | 75818250 | ths | #include <sys/types.h> |
34 | 75818250 | ths | #include <unistd.h> |
35 | 75818250 | ths | |
36 | 75818250 | ths | typedef struct BDRVNBDState { |
37 | 75818250 | ths | int sock;
|
38 | 75818250 | ths | off_t size; |
39 | 75818250 | ths | size_t blocksize; |
40 | 75818250 | ths | } BDRVNBDState; |
41 | 75818250 | ths | |
42 | 75818250 | ths | static int nbd_open(BlockDriverState *bs, const char* filename, int flags) |
43 | 75818250 | ths | { |
44 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
45 | 75818250 | ths | const char *host; |
46 | 75818250 | ths | const char *unixpath; |
47 | 75818250 | ths | int sock;
|
48 | 75818250 | ths | off_t size; |
49 | 75818250 | ths | size_t blocksize; |
50 | 75818250 | ths | int ret;
|
51 | 75818250 | ths | |
52 | 75818250 | ths | if (!strstart(filename, "nbd:", &host)) |
53 | 75818250 | ths | return -EINVAL;
|
54 | 75818250 | ths | |
55 | 75818250 | ths | if (strstart(host, "unix:", &unixpath)) { |
56 | 75818250 | ths | |
57 | 75818250 | ths | if (unixpath[0] != '/') |
58 | 75818250 | ths | return -EINVAL;
|
59 | 75818250 | ths | |
60 | 75818250 | ths | sock = unix_socket_outgoing(unixpath); |
61 | 75818250 | ths | |
62 | 75818250 | ths | } else {
|
63 | 75818250 | ths | uint16_t port; |
64 | 75818250 | ths | char *p, *r;
|
65 | 75818250 | ths | char hostname[128]; |
66 | 75818250 | ths | |
67 | 75818250 | ths | pstrcpy(hostname, 128, host);
|
68 | 75818250 | ths | |
69 | 75818250 | ths | p = strchr(hostname, ':');
|
70 | 75818250 | ths | if (p == NULL) |
71 | 75818250 | ths | return -EINVAL;
|
72 | 75818250 | ths | |
73 | 75818250 | ths | *p = '\0';
|
74 | 75818250 | ths | p++; |
75 | 75818250 | ths | |
76 | 75818250 | ths | port = strtol(p, &r, 0);
|
77 | 75818250 | ths | if (r == p)
|
78 | 75818250 | ths | return -EINVAL;
|
79 | 75818250 | ths | sock = tcp_socket_outgoing(hostname, port); |
80 | 75818250 | ths | } |
81 | 75818250 | ths | |
82 | 75818250 | ths | if (sock == -1) |
83 | 75818250 | ths | return -errno;
|
84 | 75818250 | ths | |
85 | 75818250 | ths | ret = nbd_receive_negotiate(sock, &size, &blocksize); |
86 | 75818250 | ths | if (ret == -1) |
87 | 75818250 | ths | return -errno;
|
88 | 75818250 | ths | |
89 | 75818250 | ths | s->sock = sock; |
90 | 75818250 | ths | s->size = size; |
91 | 75818250 | ths | s->blocksize = blocksize; |
92 | 75818250 | ths | |
93 | 75818250 | ths | return 0; |
94 | 75818250 | ths | } |
95 | 75818250 | ths | |
96 | 75818250 | ths | static int nbd_read(BlockDriverState *bs, int64_t sector_num, |
97 | 75818250 | ths | uint8_t *buf, int nb_sectors)
|
98 | 75818250 | ths | { |
99 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
100 | 75818250 | ths | struct nbd_request request;
|
101 | 75818250 | ths | struct nbd_reply reply;
|
102 | 75818250 | ths | |
103 | 75818250 | ths | request.type = NBD_CMD_READ; |
104 | 75818250 | ths | request.handle = (uint64_t)(intptr_t)bs; |
105 | 75818250 | ths | request.from = sector_num * 512;;
|
106 | 75818250 | ths | request.len = nb_sectors * 512;
|
107 | 75818250 | ths | |
108 | 75818250 | ths | if (nbd_send_request(s->sock, &request) == -1) |
109 | 75818250 | ths | return -errno;
|
110 | 75818250 | ths | |
111 | 75818250 | ths | if (nbd_receive_reply(s->sock, &reply) == -1) |
112 | 75818250 | ths | return -errno;
|
113 | 75818250 | ths | |
114 | 75818250 | ths | if (reply.error !=0) |
115 | 75818250 | ths | return -reply.error;
|
116 | 75818250 | ths | |
117 | 75818250 | ths | if (reply.handle != request.handle)
|
118 | 75818250 | ths | return -EIO;
|
119 | 75818250 | ths | |
120 | 75818250 | ths | if (nbd_wr_sync(s->sock, buf, request.len, 1) != request.len) |
121 | 75818250 | ths | return -EIO;
|
122 | 75818250 | ths | |
123 | 75818250 | ths | return 0; |
124 | 75818250 | ths | } |
125 | 75818250 | ths | |
126 | 75818250 | ths | static int nbd_write(BlockDriverState *bs, int64_t sector_num, |
127 | 75818250 | ths | const uint8_t *buf, int nb_sectors) |
128 | 75818250 | ths | { |
129 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
130 | 75818250 | ths | struct nbd_request request;
|
131 | 75818250 | ths | struct nbd_reply reply;
|
132 | 75818250 | ths | |
133 | 75818250 | ths | request.type = NBD_CMD_WRITE; |
134 | 75818250 | ths | request.handle = (uint64_t)(intptr_t)bs; |
135 | 75818250 | ths | request.from = sector_num * 512;;
|
136 | 75818250 | ths | request.len = nb_sectors * 512;
|
137 | 75818250 | ths | |
138 | 75818250 | ths | if (nbd_send_request(s->sock, &request) == -1) |
139 | 75818250 | ths | return -errno;
|
140 | 75818250 | ths | |
141 | 75818250 | ths | if (nbd_wr_sync(s->sock, (uint8_t*)buf, request.len, 0) != request.len) |
142 | 75818250 | ths | return -EIO;
|
143 | 75818250 | ths | |
144 | 75818250 | ths | if (nbd_receive_reply(s->sock, &reply) == -1) |
145 | 75818250 | ths | return -errno;
|
146 | 75818250 | ths | |
147 | 75818250 | ths | if (reply.error !=0) |
148 | 75818250 | ths | return -reply.error;
|
149 | 75818250 | ths | |
150 | 75818250 | ths | if (reply.handle != request.handle)
|
151 | 75818250 | ths | return -EIO;
|
152 | 75818250 | ths | |
153 | 75818250 | ths | return 0; |
154 | 75818250 | ths | } |
155 | 75818250 | ths | |
156 | 75818250 | ths | static void nbd_close(BlockDriverState *bs) |
157 | 75818250 | ths | { |
158 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
159 | 75818250 | ths | struct nbd_request request;
|
160 | 75818250 | ths | |
161 | 75818250 | ths | request.type = NBD_CMD_DISC; |
162 | 75818250 | ths | request.handle = (uint64_t)(intptr_t)bs; |
163 | 75818250 | ths | request.from = 0;
|
164 | 75818250 | ths | request.len = 0;
|
165 | 75818250 | ths | nbd_send_request(s->sock, &request); |
166 | 75818250 | ths | |
167 | 75818250 | ths | close(s->sock); |
168 | 75818250 | ths | } |
169 | 75818250 | ths | |
170 | 75818250 | ths | static int64_t nbd_getlength(BlockDriverState *bs)
|
171 | 75818250 | ths | { |
172 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
173 | 75818250 | ths | |
174 | 75818250 | ths | return s->size;
|
175 | 75818250 | ths | } |
176 | 75818250 | ths | |
177 | 5efa9d5a | Anthony Liguori | static BlockDriver bdrv_nbd = {
|
178 | e60f469c | aurel32 | .format_name = "nbd",
|
179 | e60f469c | aurel32 | .instance_size = sizeof(BDRVNBDState),
|
180 | e60f469c | aurel32 | .bdrv_open = nbd_open, |
181 | e60f469c | aurel32 | .bdrv_read = nbd_read, |
182 | e60f469c | aurel32 | .bdrv_write = nbd_write, |
183 | e60f469c | aurel32 | .bdrv_close = nbd_close, |
184 | e60f469c | aurel32 | .bdrv_getlength = nbd_getlength, |
185 | e60f469c | aurel32 | .protocol_name = "nbd",
|
186 | 75818250 | ths | }; |
187 | 5efa9d5a | Anthony Liguori | |
188 | 5efa9d5a | Anthony Liguori | static void bdrv_nbd_init(void) |
189 | 5efa9d5a | Anthony Liguori | { |
190 | 5efa9d5a | Anthony Liguori | bdrv_register(&bdrv_nbd); |
191 | 5efa9d5a | Anthony Liguori | } |
192 | 5efa9d5a | Anthony Liguori | |
193 | 5efa9d5a | Anthony Liguori | block_init(bdrv_nbd_init); |