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