Statistics
| Branch: | Revision:

root / block-nbd.c @ 75818250

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