root / block / nbd.c @ 0e607a80
History | View | Annotate | Download (4.8 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 ((flags & BDRV_O_CREAT))
|
53 | 75818250 | ths | return -EINVAL;
|
54 | 75818250 | ths | |
55 | 75818250 | ths | if (!strstart(filename, "nbd:", &host)) |
56 | 75818250 | ths | return -EINVAL;
|
57 | 75818250 | ths | |
58 | 75818250 | ths | if (strstart(host, "unix:", &unixpath)) { |
59 | 75818250 | ths | |
60 | 75818250 | ths | if (unixpath[0] != '/') |
61 | 75818250 | ths | return -EINVAL;
|
62 | 75818250 | ths | |
63 | 75818250 | ths | sock = unix_socket_outgoing(unixpath); |
64 | 75818250 | ths | |
65 | 75818250 | ths | } else {
|
66 | 75818250 | ths | uint16_t port; |
67 | 75818250 | ths | char *p, *r;
|
68 | 75818250 | ths | char hostname[128]; |
69 | 75818250 | ths | |
70 | 75818250 | ths | pstrcpy(hostname, 128, host);
|
71 | 75818250 | ths | |
72 | 75818250 | ths | p = strchr(hostname, ':');
|
73 | 75818250 | ths | if (p == NULL) |
74 | 75818250 | ths | return -EINVAL;
|
75 | 75818250 | ths | |
76 | 75818250 | ths | *p = '\0';
|
77 | 75818250 | ths | p++; |
78 | 75818250 | ths | |
79 | 75818250 | ths | port = strtol(p, &r, 0);
|
80 | 75818250 | ths | if (r == p)
|
81 | 75818250 | ths | return -EINVAL;
|
82 | 75818250 | ths | sock = tcp_socket_outgoing(hostname, port); |
83 | 75818250 | ths | } |
84 | 75818250 | ths | |
85 | 75818250 | ths | if (sock == -1) |
86 | 75818250 | ths | return -errno;
|
87 | 75818250 | ths | |
88 | 75818250 | ths | ret = nbd_receive_negotiate(sock, &size, &blocksize); |
89 | 75818250 | ths | if (ret == -1) |
90 | 75818250 | ths | return -errno;
|
91 | 75818250 | ths | |
92 | 75818250 | ths | s->sock = sock; |
93 | 75818250 | ths | s->size = size; |
94 | 75818250 | ths | s->blocksize = blocksize; |
95 | 75818250 | ths | |
96 | 75818250 | ths | return 0; |
97 | 75818250 | ths | } |
98 | 75818250 | ths | |
99 | 75818250 | ths | static int nbd_read(BlockDriverState *bs, int64_t sector_num, |
100 | 75818250 | ths | uint8_t *buf, int nb_sectors)
|
101 | 75818250 | ths | { |
102 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
103 | 75818250 | ths | struct nbd_request request;
|
104 | 75818250 | ths | struct nbd_reply reply;
|
105 | 75818250 | ths | |
106 | 75818250 | ths | request.type = NBD_CMD_READ; |
107 | 75818250 | ths | request.handle = (uint64_t)(intptr_t)bs; |
108 | 75818250 | ths | request.from = sector_num * 512;;
|
109 | 75818250 | ths | request.len = nb_sectors * 512;
|
110 | 75818250 | ths | |
111 | 75818250 | ths | if (nbd_send_request(s->sock, &request) == -1) |
112 | 75818250 | ths | return -errno;
|
113 | 75818250 | ths | |
114 | 75818250 | ths | if (nbd_receive_reply(s->sock, &reply) == -1) |
115 | 75818250 | ths | return -errno;
|
116 | 75818250 | ths | |
117 | 75818250 | ths | if (reply.error !=0) |
118 | 75818250 | ths | return -reply.error;
|
119 | 75818250 | ths | |
120 | 75818250 | ths | if (reply.handle != request.handle)
|
121 | 75818250 | ths | return -EIO;
|
122 | 75818250 | ths | |
123 | 75818250 | ths | if (nbd_wr_sync(s->sock, buf, request.len, 1) != request.len) |
124 | 75818250 | ths | return -EIO;
|
125 | 75818250 | ths | |
126 | 75818250 | ths | return 0; |
127 | 75818250 | ths | } |
128 | 75818250 | ths | |
129 | 75818250 | ths | static int nbd_write(BlockDriverState *bs, int64_t sector_num, |
130 | 75818250 | ths | const uint8_t *buf, int nb_sectors) |
131 | 75818250 | ths | { |
132 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
133 | 75818250 | ths | struct nbd_request request;
|
134 | 75818250 | ths | struct nbd_reply reply;
|
135 | 75818250 | ths | |
136 | 75818250 | ths | request.type = NBD_CMD_WRITE; |
137 | 75818250 | ths | request.handle = (uint64_t)(intptr_t)bs; |
138 | 75818250 | ths | request.from = sector_num * 512;;
|
139 | 75818250 | ths | request.len = nb_sectors * 512;
|
140 | 75818250 | ths | |
141 | 75818250 | ths | if (nbd_send_request(s->sock, &request) == -1) |
142 | 75818250 | ths | return -errno;
|
143 | 75818250 | ths | |
144 | 75818250 | ths | if (nbd_wr_sync(s->sock, (uint8_t*)buf, request.len, 0) != request.len) |
145 | 75818250 | ths | return -EIO;
|
146 | 75818250 | ths | |
147 | 75818250 | ths | if (nbd_receive_reply(s->sock, &reply) == -1) |
148 | 75818250 | ths | return -errno;
|
149 | 75818250 | ths | |
150 | 75818250 | ths | if (reply.error !=0) |
151 | 75818250 | ths | return -reply.error;
|
152 | 75818250 | ths | |
153 | 75818250 | ths | if (reply.handle != request.handle)
|
154 | 75818250 | ths | return -EIO;
|
155 | 75818250 | ths | |
156 | 75818250 | ths | return 0; |
157 | 75818250 | ths | } |
158 | 75818250 | ths | |
159 | 75818250 | ths | static void nbd_close(BlockDriverState *bs) |
160 | 75818250 | ths | { |
161 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
162 | 75818250 | ths | struct nbd_request request;
|
163 | 75818250 | ths | |
164 | 75818250 | ths | request.type = NBD_CMD_DISC; |
165 | 75818250 | ths | request.handle = (uint64_t)(intptr_t)bs; |
166 | 75818250 | ths | request.from = 0;
|
167 | 75818250 | ths | request.len = 0;
|
168 | 75818250 | ths | nbd_send_request(s->sock, &request); |
169 | 75818250 | ths | |
170 | 75818250 | ths | close(s->sock); |
171 | 75818250 | ths | } |
172 | 75818250 | ths | |
173 | 75818250 | ths | static int64_t nbd_getlength(BlockDriverState *bs)
|
174 | 75818250 | ths | { |
175 | 75818250 | ths | BDRVNBDState *s = bs->opaque; |
176 | 75818250 | ths | |
177 | 75818250 | ths | return s->size;
|
178 | 75818250 | ths | } |
179 | 75818250 | ths | |
180 | 5efa9d5a | Anthony Liguori | static BlockDriver bdrv_nbd = {
|
181 | e60f469c | aurel32 | .format_name = "nbd",
|
182 | e60f469c | aurel32 | .instance_size = sizeof(BDRVNBDState),
|
183 | e60f469c | aurel32 | .bdrv_open = nbd_open, |
184 | e60f469c | aurel32 | .bdrv_read = nbd_read, |
185 | e60f469c | aurel32 | .bdrv_write = nbd_write, |
186 | e60f469c | aurel32 | .bdrv_close = nbd_close, |
187 | e60f469c | aurel32 | .bdrv_getlength = nbd_getlength, |
188 | e60f469c | aurel32 | .protocol_name = "nbd",
|
189 | 75818250 | ths | }; |
190 | 5efa9d5a | Anthony Liguori | |
191 | 5efa9d5a | Anthony Liguori | static void bdrv_nbd_init(void) |
192 | 5efa9d5a | Anthony Liguori | { |
193 | 5efa9d5a | Anthony Liguori | bdrv_register(&bdrv_nbd); |
194 | 5efa9d5a | Anthony Liguori | } |
195 | 5efa9d5a | Anthony Liguori | |
196 | 5efa9d5a | Anthony Liguori | block_init(bdrv_nbd_init); |